From e22424ffa4bc7da41deca2b7a2ef3a5193537acd Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 29 Dec 2024 23:53:23 +0100 Subject: [PATCH] fix: Settings not being saved correctly on the web version --- lib/libimhex/source/api/content_registry.cpp | 82 +++++++++++--------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 70c8d5877..311aac226 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -38,6 +38,34 @@ namespace hex { static AutoReset>>> s_onChangeCallbacks; + static void runAllOnChangeCallbacks() { + for (const auto &[category, rest] : *impl::s_onChangeCallbacks) { + for (const auto &[name, callbacks] : rest) { + for (const auto &[id, callback] : callbacks) { + try { + callback(getSetting(category, name, {})); + } catch (const std::exception &e) { + log::error("Failed to load setting [{}/{}]: {}", category, name, e.what()); + } + } + } + } + } + + void runOnChangeHandlers(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &value) { + if (auto categoryIt = s_onChangeCallbacks->find(unlocalizedCategory); categoryIt != s_onChangeCallbacks->end()) { + if (auto nameIt = categoryIt->second.find(unlocalizedName); nameIt != categoryIt->second.end()) { + for (const auto &[id, callback] : nameIt->second) { + try { + callback(value); + } catch (const nlohmann::json::exception &e) { + log::error("Failed to run onChange handler for setting {}/{}: {}", unlocalizedCategory.get(), unlocalizedName.get(), e.what()); + } + } + } + } + } + static AutoReset s_settings; const nlohmann::json& getSettingsData() { return s_settings; @@ -72,24 +100,26 @@ namespace hex { s_settings = nlohmann::json::parse(data); } - for (const auto &[category, rest] : *impl::s_onChangeCallbacks) { - for (const auto &[name, callbacks] : rest) { - for (const auto &[id, callback] : callbacks) { - try { - callback(getSetting(category, name, {})); - } catch (const std::exception &e) { - log::error("Failed to load setting [{}/{}]: {}", category, name, e.what()); - } - } - } - } + runAllOnChangeCallbacks(); } void store() { - auto data = s_settings->dump(); + if (!s_settings.isValid()) + return; + + // During a crash settings can be empty, causing them to be overwritten. + if (settingsData.empty()) { + return; + } + + const auto result = settingsData.dump(4); + if (result.empty()) { + return; + } + MAIN_THREAD_EM_ASM({ localStorage.setItem("config", UTF8ToString($0)); - }, data.c_str()); + }, result.c_str()); } void clear() { @@ -115,17 +145,7 @@ namespace hex { if (!loaded) store(); - for (const auto &[category, rest] : *impl::s_onChangeCallbacks) { - for (const auto &[name, callbacks] : rest) { - for (const auto &[id, callback] : callbacks) { - try { - callback(getSetting(category, name, {})); - } catch (const std::exception &e) { - log::error("Failed to load setting [{}/{}]: {}", category, name, e.what()); - } - } - } - } + runAllOnChangeCallbacks(); } void store() { @@ -210,20 +230,6 @@ namespace hex { hex::log::error("Failed to read setting {}/{}: {}", unlocalizedCategory.get(), unlocalizedName.get(), e.what()); } - void runOnChangeHandlers(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedName, const nlohmann::json &value) { - if (auto categoryIt = s_onChangeCallbacks->find(unlocalizedCategory); categoryIt != s_onChangeCallbacks->end()) { - if (auto nameIt = categoryIt->second.find(unlocalizedName); nameIt != categoryIt->second.end()) { - for (const auto &[id, callback] : nameIt->second) { - try { - callback(value); - } catch (const nlohmann::json::exception &e) { - log::error("Failed to run onChange handler for setting {}/{}: {}", unlocalizedCategory.get(), unlocalizedName.get(), e.what()); - } - } - } - } - } - } void setCategoryDescription(const UnlocalizedString &unlocalizedCategory, const UnlocalizedString &unlocalizedDescription) {