diff --git a/src/Context.cpp b/src/Context.cpp index d96c090..1eb464a 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -6,7 +6,6 @@ #include #include "install_config.h" #include "graphic/Fast3D/debug/GfxDebugger.h" -#include "graphic/Fast3D/Fast3dWindow.h" #ifdef _WIN32 #include @@ -42,16 +41,19 @@ Context::~Context() { spdlog::shutdown(); } -std::shared_ptr Context::CreateInstance(const std::string name, const std::string shortName, - const std::string configFilePath, - const std::vector& archivePaths, - const std::unordered_set& validHashes, - uint32_t reservedThreadCount, AudioSettings audioSettings) { +std::shared_ptr +Context::CreateInstance(const std::string name, const std::string shortName, const std::string configFilePath, + const std::vector& archivePaths, const std::unordered_set& validHashes, + uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr window) { if (mContext.expired()) { auto shared = std::make_shared(name, shortName, configFilePath); mContext = shared; - shared->Init(archivePaths, validHashes, reservedThreadCount, audioSettings); - return shared; + if (shared->Init(archivePaths, validHashes, reservedThreadCount, audioSettings, window)) { + return shared; + } else { + SPDLOG_ERROR("Failed to initialize"); + return nullptr; + }; } SPDLOG_DEBUG("Trying to create a context when it already exists. Returning existing."); @@ -76,23 +78,16 @@ Context::Context(std::string name, std::string shortName, std::string configFile : mConfigFilePath(std::move(configFilePath)), mName(std::move(name)), mShortName(std::move(shortName)) { } -void Context::Init(const std::vector& archivePaths, const std::unordered_set& validHashes, - uint32_t reservedThreadCount, AudioSettings audioSettings) { - InitLogging(); - InitConfiguration(); - InitConsoleVariables(); - InitResourceManager(archivePaths, validHashes, reservedThreadCount); - InitControlDeck(); - InitCrashHandler(); - InitConsole(); - InitWindow(); - InitAudio(audioSettings); - InitGfxDebugger(); +bool Context::Init(const std::vector& archivePaths, const std::unordered_set& validHashes, + uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr window) { + return InitLogging() && InitConfiguration() && InitConsoleVariables() && + InitResourceManager(archivePaths, validHashes, reservedThreadCount) && InitControlDeck() && + InitCrashHandler() && InitConsole() && InitWindow(window) && InitAudio(audioSettings) && InitGfxDebugger(); } -void Context::InitLogging() { +bool Context::InitLogging() { if (GetLogger() != nullptr) { - return; + return true; } try { @@ -162,29 +157,47 @@ void Context::InitLogging() { spdlog::register_logger(GetLogger()); spdlog::set_default_logger(GetLogger()); - } catch (const spdlog::spdlog_ex& ex) { std::cout << "Log initialization failed: " << ex.what() << std::endl; } + return true; + } catch (const spdlog::spdlog_ex& ex) { + std::cout << "Log initialization failed: " << ex.what() << std::endl; + return false; + } } -void Context::InitConfiguration() { +bool Context::InitConfiguration() { if (GetConfig() != nullptr) { - return; + return true; } mConfig = std::make_shared(GetPathRelativeToAppDirectory(mConfigFilePath)); + + if (GetConfig() == nullptr) { + SPDLOG_ERROR("Failed to initialize config"); + return false; + } + + return true; } -void Context::InitConsoleVariables() { +bool Context::InitConsoleVariables() { if (GetConsoleVariables() != nullptr) { - return; + return true; } mConsoleVariables = std::make_shared(); + + if (GetConsoleVariables() == nullptr) { + SPDLOG_ERROR("Failed to initialize console variables"); + return false; + } + + return true; } -void Context::InitResourceManager(const std::vector& archivePaths, +bool Context::InitResourceManager(const std::vector& archivePaths, const std::unordered_set& validHashes, uint32_t reservedThreadCount) { if (GetResourceManager() != nullptr) { - return; + return true; } mMainPath = GetConfig()->GetString("Game.Main Archive", GetAppDirectoryPath()); @@ -209,59 +222,105 @@ void Context::InitResourceManager(const std::vector& archivePaths, // We need this exit to close the app when we dismiss the dialog exit(0); #endif - return; + return false; } + + return true; } -void Context::InitControlDeck(std::vector additionalBitmasks) { +bool Context::InitControlDeck(std::vector additionalBitmasks) { if (GetControlDeck() != nullptr) { - return; + return true; } mControlDeck = std::make_shared(additionalBitmasks); + + if (GetControlDeck() == nullptr) { + SPDLOG_ERROR("Failed to initialize control deck"); + return false; + } + + return true; } -void Context::InitCrashHandler() { +bool Context::InitCrashHandler() { if (GetCrashHandler() != nullptr) { - return; + return true; } mCrashHandler = std::make_shared(); + + if (GetCrashHandler() == nullptr) { + SPDLOG_ERROR("Failed to initialize crash handler"); + return false; + } + + return true; } -void Context::InitAudio(AudioSettings settings) { +bool Context::InitAudio(AudioSettings settings) { if (GetAudio() != nullptr) { - return; + return true; } mAudio = std::make_shared