Skip to content

Commit

Permalink
Implements Config Versioning (#308)
Browse files Browse the repository at this point in the history
* Adds ConfigVersionUpdater class and a way to run the update functions.

* Remove CreateDefaultSettings and fix crash

* Fixes first launch tiny window due to lack of default values.

* Refactors for ConfigVersionUpdater to store the version

* Fixes clang-format/tidy issues possibly

* Another format fix

* Change `ConfigVersionUpdater` to store `toVersion`

As opposed to storing a `fromVersion` as before.
  • Loading branch information
leggettc18 committed Jun 10, 2023
1 parent 22bd2a0 commit cf75ad3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 30 deletions.
27 changes: 0 additions & 27 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& otrFiles, const std::unordered_set<uint32_t>& validHashes,
uint32_t reservedThreadCount) {
InitLogging();
InitConfiguration();
InitConsoleVariables();
InitResourceManager(otrFiles, validHashes, reservedThreadCount);
CreateDefaultSettings();
InitControlDeck();
InitCrashHandler();
InitConsole();
Expand Down
1 change: 0 additions & 1 deletion src/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class Context {
std::string GetName();
std::string GetShortName();

void CreateDefaultSettings();
void InitLogging();
void InitConfiguration();
void InitConsoleVariables();
Expand Down
23 changes: 23 additions & 0 deletions src/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,27 @@ void Config::SetWindowBackend(WindowBackend backend) {
}
}

bool Config::RegisterConfigVersionUpdater(std::shared_ptr<ConfigVersionUpdater> 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
50 changes: 50 additions & 0 deletions src/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<ConfigVersionUpdater> 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);
Expand All @@ -47,5 +96,6 @@ class Config {
nlohmann::json mNestedJson;
std::string mPath;
bool mIsNewInstance;
std::map<uint32_t, std::shared_ptr<ConfigVersionUpdater>> mVersionUpdaters;
};
} // namespace LUS
4 changes: 2 additions & 2 deletions src/window/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit cf75ad3

Please sign in to comment.