Skip to content

Commit

Permalink
feat(settings): add method that saves settings to file
Browse files Browse the repository at this point in the history
  • Loading branch information
SrGesus committed Nov 22, 2024
1 parent 35720aa commit 3df89fd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions engine/include/cubos/engine/settings/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ namespace cubos::engine
/// @param settingsToMerge Settings to be merged to this instance.
void merge(const Settings& settingsToMerge);

/// @brief Saves settings as json to path specified in the settings.path value.
bool save();

/// @return Underlying `std::unordered_map` with the settings.
const std::unordered_map<std::string, std::string>& getValues() const;

Expand Down
8 changes: 8 additions & 0 deletions engine/samples/settings/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ int main(int argc, char** argv)

cubos.startupSystem("print setting value").after(settingsTag).call([](Settings& settings) {
CUBOS_INFO("{}", settings.getString("greeting", "Hello!"));
settings.save();
});

cubos.startupSystem("print number setting value").after(settingsTag).call([](Settings& settings) {
CUBOS_INFO("{}", settings.getInteger("number", 1));
CUBOS_INFO("{}", settings.getDouble("double", 1e-07));
CUBOS_INFO("{}", settings.getBool("boolean", false));
settings.save();
});

cubos.run();
Expand Down
49 changes: 49 additions & 0 deletions engine/src/settings/settings.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#include <nlohmann/json.hpp>

#include <cubos/core/data/fs/file_system.hpp>
#include <cubos/core/data/fs/standard_archive.hpp>
#include <cubos/core/ecs/reflection.hpp>

#include <cubos/engine/settings/settings.hpp>

using cubos::core::data::File;
using cubos::core::data::FileSystem;
using cubos::core::data::StandardArchive;
using cubos::core::memory::Stream;

using namespace cubos::engine;

CUBOS_REFLECT_IMPL(Settings)
Expand Down Expand Up @@ -96,6 +105,46 @@ void Settings::merge(const Settings& settingsToMerge)
}
}

static inline void saveSettingsAsJson(const Settings& settings, Stream& stream)

Check warning on line 108 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L108

Added line #L108 was not covered by tests
{
auto json = nlohmann::json();

Check warning on line 110 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L110

Added line #L110 was not covered by tests
for (const auto& value : settings.getValues())
{
char* pEnd;
strtod(value.second.c_str(), &pEnd);

Check warning on line 114 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L114

Added line #L114 was not covered by tests
// Store as string if second value is not number nor boolean
if (pEnd != value.second.c_str() + value.second.length() && (value.second != "true" && value.second != "false"))
{
json[value.first] = value.second;

Check warning on line 118 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L118

Added line #L118 was not covered by tests
}
else
{
json[value.first] = nlohmann::json::parse(value.second);

Check warning on line 122 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L122

Added line #L122 was not covered by tests
}
}
stream.print(json.dump(2));
}

Check warning on line 126 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L125-L126

Added lines #L125 - L126 were not covered by tests

bool Settings::save()

Check warning on line 128 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L128

Added line #L128 was not covered by tests
{
// If the settings file is not mounted, mount it.
if (FileSystem::find("/settings.json") == nullptr)
{
const std::string& path = getString("settings.path", "settings.json");

Check warning on line 133 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L133

Added line #L133 was not covered by tests
// Mount the real settings file as `/settings.json`.
if (!FileSystem::mount("/settings.json",
std::make_unique<StandardArchive>(path, false /*isDirectory*/, false /*readOnly*/)))

Check warning on line 136 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L136

Added line #L136 was not covered by tests
{
// CUBOS_ERROR("Couldn't mount the settings file at {}", path);
return false;

Check warning on line 139 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L139

Added line #L139 was not covered by tests
}
}

Check warning on line 141 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L141

Added line #L141 was not covered by tests

auto stream = FileSystem::open("/settings.json", File::OpenMode::Write);
saveSettingsAsJson(*this, *stream);
return true;
}

Check warning on line 146 in engine/src/settings/settings.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/settings/settings.cpp#L143-L146

Added lines #L143 - L146 were not covered by tests

const std::unordered_map<std::string, std::string>& Settings::getValues() const
{
return mValues;
Expand Down

0 comments on commit 3df89fd

Please sign in to comment.