diff --git a/src/Context.cpp b/src/Context.cpp index 031b935bd..cddeaf340 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -57,39 +57,12 @@ Context::Context(std::string name, std::string shortName, std::string configFile : mName(std::move(name)), mShortName(std::move(shortName)), mConfigFilePath(std::move(configFilePath)) { } -void Context::CreateDefaultSettings() { - if (GetConfig()->IsNewInstance()) { - GetConfig()->SetInt("Window.Width", 640); - GetConfig()->SetInt("Window.Height", 480); - GetConfig()->SetInt("Window.PositionX", 100); - GetConfig()->SetInt("Window.PositionY", 100); - - GetConfig()->SetString("Window.GfxBackend", ""); - GetConfig()->SetString("Window.GfxApi", ""); - GetConfig()->SetString("Window.AudioBackend", ""); - - GetConfig()->SetBool("Window.Fullscreen.Enabled", false); - GetConfig()->SetInt("Window.Fullscreen.Width", 1920); - GetConfig()->SetInt("Window.Fullscreen.Height", 1080); - - GetConfig()->SetString("Game.SaveName", ""); - GetConfig()->SetString("Game.Main Archive", ""); - GetConfig()->SetString("Game.Patches Archive", ""); - - GetConfig()->SetInt("Shortcuts.Fullscreen", KbScancode::LUS_KB_F11); - GetConfig()->SetInt("Shortcuts.Console", KbScancode::LUS_KB_OEM_3); - - GetConfig()->Save(); - } -} - void Context::Init(const std::vector& otrFiles, const std::unordered_set& validHashes, uint32_t reservedThreadCount) { InitLogging(); InitConfiguration(); InitConsoleVariables(); InitResourceManager(otrFiles, validHashes, reservedThreadCount); - CreateDefaultSettings(); InitControlDeck(); InitCrashHandler(); InitConsole(); diff --git a/src/Context.h b/src/Context.h index 3f1b75089..4ba7468e5 100644 --- a/src/Context.h +++ b/src/Context.h @@ -50,7 +50,6 @@ class Context { std::string GetName(); std::string GetShortName(); - void CreateDefaultSettings(); void InitLogging(); void InitConfiguration(); void InitConsoleVariables(); diff --git a/src/config/Config.cpp b/src/config/Config.cpp index b01c9c457..9d61ddaf0 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -265,4 +265,27 @@ void Config::SetWindowBackend(WindowBackend backend) { } } +bool Config::RegisterConfigVersionUpdater(std::shared_ptr versionUpdater) { + auto [_, emplaced] = mVersionUpdaters.emplace(versionUpdater->GetVersion(), versionUpdater); + return emplaced; +} + +void Config::RunVersionUpdates() { + for (auto [_, versionUpdater] : mVersionUpdaters) { + uint32_t version = GetUInt("ConfigVersion", 0); + if (version < versionUpdater->GetVersion()) { + versionUpdater->Update(this); + SetUInt("ConfigVersion", versionUpdater->GetVersion()); + } + } + Save(); +} + +ConfigVersionUpdater::ConfigVersionUpdater(uint32_t toVersion) : mVersion(toVersion) { +} + +uint32_t ConfigVersionUpdater::GetVersion() { + return mVersion; +} + } // namespace LUS diff --git a/src/config/Config.h b/src/config/Config.h index aa3cacf57..0dea4c735 100644 --- a/src/config/Config.h +++ b/src/config/Config.h @@ -8,6 +8,38 @@ #include "window/Window.h" namespace LUS { + +/** + * @brief Abstract class representing a Config Version Updater, intended to express how to + * upgrade a Configuration file from one version of a config to another (i.e. removing + * default values, changing option names, etc.) It can be used by subclassing `ConfigVersionUpdater`, + * implementing the Update function, and implementing the Constructor passing the version that the + * Config is being updated to to this class' constructor from the child class' default constructor. + * For example: \code ConfigVersion1Updater() : ConfigVersionUpdater(1) {} \endcode + * Finally, give an instance of this subclass to a Config object via + * RegisterConfigVersionUpdater and call RunVersionUpdates. + */ +class ConfigVersionUpdater { + protected: + uint32_t mVersion; + + public: + ConfigVersionUpdater(uint32_t toVersion); + /** + * @brief Performs actions on a Config object via the provided pointer to update it + * to the next version. (i.e. removing/changing default values or renaming options) + * + * @param conf + */ + virtual void Update(Config* conf) = 0; + + /** + * @brief Get the value of mVersion + * + * @return uint32_t + */ + uint32_t GetVersion(); +}; class Config { public: Config(std::string path); @@ -36,6 +68,23 @@ class Config { WindowBackend GetWindowBackend(); void SetWindowBackend(WindowBackend backend); + /** + * @brief Adds a ConfigVersionUpdater instance to the list to be run later via RunVersionUpdates + * + * @param versionUpdater + * @return true if the insert was successful, or + * @return false if the insert failed, i.e. if the list already has a ConfigVersionUpdater with + * a matching version. + */ + bool RegisterConfigVersionUpdater(std::shared_ptr versionUpdater); + + /** + * @brief Runs the Update function on each ConfigVersionUpdater instance if the version matches\ + * the current ConfigVersion value of the config object. + * + */ + void RunVersionUpdates(); + protected: nlohmann::json Nested(const std::string& key); static std::string FormatNestedKey(const std::string& key); @@ -47,5 +96,6 @@ class Config { nlohmann::json mNestedJson; std::string mPath; bool mIsNewInstance; + std::map> mVersionUpdaters; }; } // namespace LUS diff --git a/src/window/Window.cpp b/src/window/Window.cpp index cc285737b..071f65086 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -70,8 +70,8 @@ void Window::Init() { mHeight = LUS::Context::GetInstance()->GetConfig()->GetInt("Window.Fullscreen.Height", steamDeckGameMode ? 800 : 1080); } else { - mWidth = LUS::Context::GetInstance()->GetConfig()->GetInt("Window.Width", mWidth); - mHeight = LUS::Context::GetInstance()->GetConfig()->GetInt("Window.Height", mHeight); + mWidth = LUS::Context::GetInstance()->GetConfig()->GetInt("Window.Width", 640); + mHeight = LUS::Context::GetInstance()->GetConfig()->GetInt("Window.Height", 480); mPosX = LUS::Context::GetInstance()->GetConfig()->GetInt("Window.PositionX", mPosX); mPosY = LUS::Context::GetInstance()->GetConfig()->GetInt("Window.PositionY", mPosY); }