Skip to content

Commit 3dfae40

Browse files
committed
feat: use LocalStorage for settings instead of a file
1 parent 13c8e72 commit 3dfae40

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

src/manager/settings_manager.cpp

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,64 @@
33
#include "input/keyboard_input.hpp"
44
#include "input/touch_input.hpp"
55

6+
#if defined(__EMSCRIPTEN__)
7+
#include "helper/web_utils.hpp"
8+
#endif
9+
610
namespace {
11+
#if defined(__EMSCRIPTEN__)
12+
constexpr const auto settings_key = "settings_key";
13+
#else
714
constexpr const auto settings_filename = "settings.json";
15+
#endif
816

9-
}
17+
} // namespace
1018

1119

1220
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
1345
const std::filesystem::path settings_file = utils::get_root_folder() / settings_filename;
1446

1547
const auto result = json::try_parse_json_file<settings::Settings>(settings_file);
48+
#endif
1649

1750
if (result.has_value()) {
1851
m_settings = result.value();
1952
} else {
2053
auto [error, error_type] = result.error();
2154

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+
);
2364
spdlog::warn("applying default settings");
2465

2566
m_settings = {
@@ -48,14 +89,32 @@ void SettingsManager::add_callback(Callback&& callback) {
4889
}
4990

5091
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
51107
const std::filesystem::path settings_file = utils::get_root_folder() / settings_filename;
52108

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+
54111

55112
if (result.has_value()) {
56113
spdlog::error("unable to save settings to \"{}\": {}", settings_filename, result.value());
57114
return;
58115
}
116+
#endif
117+
59118

60119
this->fire_callbacks();
61120
}

0 commit comments

Comments
 (0)