From a94ebd65a743d22872b02ec17da77a7b4e36946c Mon Sep 17 00:00:00 2001 From: fed <58637860+fedddddd@users.noreply.github.com> Date: Wed, 22 Feb 2023 23:12:15 +0100 Subject: [PATCH] Move config file to appdata --- src/client/component/config.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/client/component/config.cpp b/src/client/component/config.cpp index 5927454a..3432ea30 100644 --- a/src/client/component/config.cpp +++ b/src/client/component/config.cpp @@ -7,8 +7,9 @@ #include #include +#include -#define CONFIG_FILE "players2/default/h2_mod.json" +#define OLD_CONFIG_FILE "players2/default/h2_mod.json" namespace config { @@ -34,6 +35,11 @@ namespace config { {define_field("language", field_type::string, language::get_default_language(), language::is_valid_language)}, }; + + std::string get_config_file_path() + { + return (utils::properties::get_appdata_path() / "config.json").generic_string(); + } } nlohmann::json validate_config_field(const std::string& key, const nlohmann::json& value) @@ -87,8 +93,9 @@ namespace config { try { + const auto path = get_config_file_path(); const auto str = json.dump(4); - utils::io::write_file(CONFIG_FILE, str, false); + utils::io::write_file(path, str, false); } catch (const std::exception& e) { @@ -98,20 +105,21 @@ namespace config nlohmann::json read_config() { - if (!utils::io::file_exists(CONFIG_FILE)) + const auto path = get_config_file_path(); + if (!utils::io::file_exists(path)) { return {}; } try { - const auto data = utils::io::read_file(CONFIG_FILE); + const auto data = utils::io::read_file(path); return nlohmann::json::parse(data); } catch (const std::exception& e) { console::error("Failed to parse config file: %s\n", e.what()); - utils::io::write_file(CONFIG_FILE, "{}", false); + utils::io::write_file(path, "{}", false); } return {}; @@ -122,7 +130,12 @@ namespace config public: void post_unpack() override { - + if (utils::io::file_exists(OLD_CONFIG_FILE)) + { + const auto data = utils::io::read_file(OLD_CONFIG_FILE); + utils::io::write_file(get_config_file_path(), data); + utils::io::remove_file(OLD_CONFIG_FILE); + } } }; }