From 248acd5e26bfc0a2a60d9853344813937c5f0b85 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 25 Dec 2024 16:17:33 +0100 Subject: [PATCH] fix: Properly save achievements in web version --- .../source/api/achievement_manager.cpp | 34 ++++++++++++++----- .../content/views/view_achievements.cpp | 9 ++++- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/libimhex/source/api/achievement_manager.cpp b/lib/libimhex/source/api/achievement_manager.cpp index 102089566..c0f5aa2d3 100644 --- a/lib/libimhex/source/api/achievement_manager.cpp +++ b/lib/libimhex/source/api/achievement_manager.cpp @@ -215,7 +215,16 @@ namespace hex { } try { - auto json = nlohmann::json::parse(file.readString()); + #if defined(OS_WEB) + auto data = (char *) MAIN_THREAD_EM_ASM_INT({ + let data = localStorage.getItem("achievements"); + return data ? stringToNewUTF8(data) : null; + }); + #else + auto data = file.readString(); + #endif + + auto json = nlohmann::json::parse(data); for (const auto &[categoryName, achievements] : getAchievements()) { for (const auto &[achievementName, achievement] : achievements) { @@ -254,16 +263,23 @@ namespace hex { if (json.empty()) return; - for (const auto &directory : paths::Config.write()) { - auto path = directory / AchievementsFile; + #if defined(OS_WEB) + auto data = json.dump(); + MAIN_THREAD_EM_ASM({ + localStorage.setItem("config", UTF8ToString($0)); + }, data.c_str()); + #else + for (const auto &directory : paths::Config.write()) { + auto path = directory / AchievementsFile; - wolv::io::File file(path, wolv::io::File::Mode::Create); - if (!file.isValid()) - continue; + wolv::io::File file(path, wolv::io::File::Mode::Create); + if (!file.isValid()) + continue; - file.writeString(json.dump(4)); - break; - } + file.writeString(json.dump(4)); + break; + } + #endif } } \ No newline at end of file diff --git a/plugins/builtin/source/content/views/view_achievements.cpp b/plugins/builtin/source/content/views/view_achievements.cpp index b7c2c3827..eb489d1aa 100644 --- a/plugins/builtin/source/content/views/view_achievements.cpp +++ b/plugins/builtin/source/content/views/view_achievements.cpp @@ -30,7 +30,14 @@ namespace hex::plugin::builtin { }); // Load settings - m_showPopup = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.achievement_popup", true); + { + bool defaultValue = true; + #if defined(OS_WEB) + defaultValue = false; + #endif + + m_showPopup = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.achievement_popup", defaultValue); + } } ViewAchievements::~ViewAchievements() {