This commit is contained in:
80
src/resource/xml/settings.cpp
Normal file
80
src/resource/xml/settings.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "settings.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
#include <tinyxml2/tinyxml2.h>
|
||||
|
||||
#include "../../log.hpp"
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include "../../util/web_filesystem.hpp"
|
||||
#endif
|
||||
|
||||
using namespace tinyxml2;
|
||||
using namespace game::util;
|
||||
|
||||
namespace game::resource::xml
|
||||
{
|
||||
Settings::Settings(const std::filesystem::path& path)
|
||||
{
|
||||
XMLDocument document;
|
||||
|
||||
if (document.LoadFile(path.c_str()) != XML_SUCCESS)
|
||||
{
|
||||
logger.error(
|
||||
std::format("Could not initialize character save file: {} ({})", path.string(), document.ErrorStr()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (auto root = document.RootElement())
|
||||
{
|
||||
std::string measurementSystemString{};
|
||||
query_string_attribute(root, "MeasurementSystem", &measurementSystemString);
|
||||
measurementSystem = measurementSystemString == "Imperial" ? measurement::IMPERIAL : measurement::METRIC;
|
||||
root->QueryIntAttribute("Volume", &volume);
|
||||
root->QueryFloatAttribute("ColorR", &color.r);
|
||||
root->QueryFloatAttribute("ColorG", &color.g);
|
||||
root->QueryFloatAttribute("ColorB", &color.b);
|
||||
root->QueryFloatAttribute("WindowX", &windowPosition.x);
|
||||
root->QueryFloatAttribute("WindowY", &windowPosition.y);
|
||||
root->QueryIntAttribute("WindowW", &windowSize.x);
|
||||
root->QueryIntAttribute("WindowH", &windowSize.y);
|
||||
}
|
||||
|
||||
logger.info(std::format("Initialized settings: {}", path.string()));
|
||||
|
||||
isValid = true;
|
||||
}
|
||||
|
||||
bool Settings::is_valid() const { return isValid; }
|
||||
|
||||
void Settings::serialize(const std::filesystem::path& path)
|
||||
{
|
||||
XMLDocument document;
|
||||
|
||||
auto element = document.NewElement("Settings");
|
||||
|
||||
element->SetAttribute("MeasurementSystem", measurementSystem == measurement::IMPERIAL ? "Imperial" : "Metric");
|
||||
element->SetAttribute("Volume", volume);
|
||||
element->SetAttribute("ColorR", color.r);
|
||||
element->SetAttribute("ColorG", color.g);
|
||||
element->SetAttribute("ColorB", color.b);
|
||||
element->SetAttribute("WindowX", windowPosition.x);
|
||||
element->SetAttribute("WindowY", windowPosition.y);
|
||||
element->SetAttribute("WindowW", windowSize.x);
|
||||
element->SetAttribute("WindowH", windowSize.y);
|
||||
|
||||
document.InsertFirstChild(element);
|
||||
|
||||
if (document.SaveFile(path.c_str()) != XML_SUCCESS)
|
||||
{
|
||||
logger.info(std::format("Failed to initialize settings: {} ({})", path.string(), document.ErrorStr()));
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(std::format("Saved settings: {}", path.string()));
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
web_filesystem::flush_async();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user