|
3 | 3 | #include "input/keyboard_input.hpp" |
4 | 4 | #include "input/touch_input.hpp" |
5 | 5 |
|
| 6 | +#if defined(__EMSCRIPTEN__) |
| 7 | +#include "helper/web_utils.hpp" |
| 8 | +#endif |
| 9 | + |
6 | 10 | namespace { |
| 11 | +#if defined(__EMSCRIPTEN__) |
| 12 | + constexpr const auto settings_key = "settings_key"; |
| 13 | +#else |
7 | 14 | constexpr const auto settings_filename = "settings.json"; |
| 15 | +#endif |
8 | 16 |
|
9 | | -} |
| 17 | +} // namespace |
10 | 18 |
|
11 | 19 |
|
12 | 20 | SettingsManager::SettingsManager() { |
| 21 | + |
| 22 | +#if defined(__EMSCRIPTEN__) |
| 23 | + const auto content = web::LocalStorage::get_item(settings_key); |
| 24 | + |
| 25 | + helper::expected<settings::Settings, std::pair<std::string, json::ParseError>> result = |
| 26 | + helper::unexpected<std::pair<std::string, json::ParseError>>{ std::make_pair<std::string, json::ParseError>( |
| 27 | + "Key not present in LocalStorage", json::ParseError::OpenError |
| 28 | + ) }; |
| 29 | + |
| 30 | + if (content.has_value()) { |
| 31 | + auto parse_result = json::try_parse_json<settings::Settings>(content.value()); |
| 32 | + |
| 33 | + if (not parse_result.has_value()) { |
| 34 | + result = helper::unexpected<std::pair<std::string, json::ParseError>>{ |
| 35 | + std::make_pair<std::string, json::ParseError>( |
| 36 | + std::move(parse_result.error()), json::ParseError::FormatError |
| 37 | + ) |
| 38 | + }; |
| 39 | + } else { |
| 40 | + result = parse_result.value(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | +#else |
13 | 45 | const std::filesystem::path settings_file = utils::get_root_folder() / settings_filename; |
14 | 46 |
|
15 | 47 | const auto result = json::try_parse_json_file<settings::Settings>(settings_file); |
| 48 | +#endif |
16 | 49 |
|
17 | 50 | if (result.has_value()) { |
18 | 51 | m_settings = result.value(); |
19 | 52 | } else { |
20 | 53 | auto [error, error_type] = result.error(); |
21 | 54 |
|
22 | | - spdlog::error("unable to load settings from \"{}\": {}", settings_filename, error); |
| 55 | + spdlog::error( |
| 56 | + "unable to load settings from \"{}\": {}", |
| 57 | +#if defined(__EMSCRIPTEN__) |
| 58 | + settings_key, |
| 59 | +#else |
| 60 | + settings_filename, |
| 61 | +#endif |
| 62 | + error |
| 63 | + ); |
23 | 64 | spdlog::warn("applying default settings"); |
24 | 65 |
|
25 | 66 | m_settings = { |
@@ -48,14 +89,32 @@ void SettingsManager::add_callback(Callback&& callback) { |
48 | 89 | } |
49 | 90 |
|
50 | 91 | void SettingsManager::save() const { |
| 92 | + |
| 93 | +#if defined(__EMSCRIPTEN__) |
| 94 | + const auto maybe_settings_json = json::try_json_to_string<settings::Settings>(m_settings); |
| 95 | + |
| 96 | + if (not maybe_settings_json.has_value()) { |
| 97 | + spdlog::error( |
| 98 | + "unable to save settings to LocalStorage\"{}\": unable to convert settings to json: {}", settings_key, |
| 99 | + maybe_settings_json.error() |
| 100 | + ); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + web::LocalStorage::set_item(settings_key, maybe_settings_json.value()); |
| 105 | + |
| 106 | +#else |
51 | 107 | const std::filesystem::path settings_file = utils::get_root_folder() / settings_filename; |
52 | 108 |
|
53 | | - const auto result = json::try_write_json_to_file(settings_file, m_settings, true); |
| 109 | + const auto result = json::try_write_json_to_file<settings::Settings>(settings_file, m_settings, true); |
| 110 | + |
54 | 111 |
|
55 | 112 | if (result.has_value()) { |
56 | 113 | spdlog::error("unable to save settings to \"{}\": {}", settings_filename, result.value()); |
57 | 114 | return; |
58 | 115 | } |
| 116 | +#endif |
| 117 | + |
59 | 118 |
|
60 | 119 | this->fire_callbacks(); |
61 | 120 | } |
|
0 commit comments