Skip to content

Commit

Permalink
block config save until all settings are registered
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz-h committed Sep 1, 2024
1 parent 6d5c19f commit 06c8b35
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions map/src/MapWindow/BaseWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ void Satisfactory3DMap::BaseWindow::run() {
return;
}
running_ = true;
config_->registerDone();
while (!glfwWindowShouldClose(window_)) {
draw();
glfwSwapBuffers(window_);
Expand Down
18 changes: 16 additions & 2 deletions map/src/MapWindow/Config/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Satisfactory3DMap::Configuration::Configuration() {
}

void Satisfactory3DMap::Configuration::registerSetting(std::shared_ptr<Setting> setting) {
if (registerDone_) {
spdlog::error("Register setting after register done: {}", setting->name());
}
setting->registerConfig(weak_from_this());
try {
if (json_.contains(setting->name())) {
Expand All @@ -39,13 +42,24 @@ void Satisfactory3DMap::Configuration::registerSetting(std::shared_ptr<Setting>
settings_.emplace_back(std::move(setting));
}

void Satisfactory3DMap::Configuration::registerDone() {
registerDone_ = true;
if (requestSave_) {
saveOnDisk();
}
}

void Satisfactory3DMap::Configuration::requestSave() {
requestSave_ = true;
// TODO cache save request and only write every x seconds to disk.
saveOnDisk();
if (registerDone_) {
saveOnDisk();
}
}

void Satisfactory3DMap::Configuration::saveOnDisk() const {
void Satisfactory3DMap::Configuration::saveOnDisk() {
try {
requestSave_ = false;
nlohmann::json j;
for (const auto& s : settings_) {
nlohmann::json serialized;
Expand Down
10 changes: 9 additions & 1 deletion map/src/MapWindow/Config/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ namespace Satisfactory3DMap {

void registerSetting(std::shared_ptr<Setting> setting);

/*
* Call once after all initialization is done! All save requests are blocked by this to avoid writing an
* incomplete configuration before all settings are registered.
*/
void registerDone();

void requestSave();

protected:
Configuration();

void saveOnDisk() const;
void saveOnDisk();

friend class SettingsWindow;

Expand All @@ -36,5 +42,7 @@ namespace Satisfactory3DMap {

nlohmann::json json_;
std::vector<std::shared_ptr<Setting>> settings_;
bool registerDone_ = false;
bool requestSave_ = false;
};
} // namespace Satisfactory3DMap

0 comments on commit 06c8b35

Please sign in to comment.