h2-mod/src/client/component/config.cpp

144 lines
3.1 KiB
C++
Raw Normal View History

2023-02-16 11:01:53 -05:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "config.hpp"
#include "console.hpp"
2023-02-21 22:36:28 -05:00
#include "language.hpp"
2023-02-16 11:01:53 -05:00
#include <utils/hook.hpp>
#include <utils/io.hpp>
2023-02-22 17:12:15 -05:00
#include <utils/properties.hpp>
2023-02-16 11:01:53 -05:00
2023-02-22 17:12:15 -05:00
#define OLD_CONFIG_FILE "players2/default/h2_mod.json"
2023-02-16 11:01:53 -05:00
namespace config
{
2023-02-21 22:36:28 -05:00
namespace
{
using validate_type_callback_t = std::function<bool(const field_value&)>;
using validate_callback_t = std::function<bool(const field_value&)>;
struct field_definition_t
{
field_type type;
field_value default_value;
std::optional<validate_callback_t> validate_value = {};
};
template <typename... Args>
std::pair<std::string, field_definition_t> define_field(const std::string& name, Args&&... args)
{
return std::make_pair(name, field_definition_t{std::forward<Args>(args)...});
}
std::unordered_map<std::string, field_definition_t> field_definitions =
{
2023-02-21 23:48:38 -05:00
{define_field("language", field_type::string, language::get_default_language(), language::is_valid_language)},
2023-02-21 22:36:28 -05:00
};
2023-02-22 17:12:15 -05:00
std::string get_config_file_path()
{
return (utils::properties::get_appdata_path() / "config.json").generic_string();
}
2023-02-21 22:36:28 -05:00
}
nlohmann::json validate_config_field(const std::string& key, const nlohmann::json& value)
{
const auto iter = field_definitions.find(key);
if (iter == field_definitions.end())
{
return value;
}
if (value.type() != iter->second.type)
{
return iter->second.default_value;
}
if (iter->second.validate_value.has_value())
{
const auto& validate_value = iter->second.validate_value.value();
if (!validate_value(value))
{
iter->second.default_value;
}
}
return value;
}
2023-02-21 23:48:38 -05:00
nlohmann::json get_default_value(const std::string& key)
{
const auto iter = field_definitions.find(key);
if (iter == field_definitions.end())
{
return {};
}
return iter->second.default_value;
}
nlohmann::json get_raw(const std::string& key)
{
const auto cfg = read_config();
if (!cfg.is_object() || !cfg.contains(key))
{
return get_default_value(key);
}
return validate_config_field(key, cfg[key]);
}
2023-02-16 11:01:53 -05:00
void write_config(const nlohmann::json& json)
{
try
{
2023-02-22 17:12:15 -05:00
const auto path = get_config_file_path();
2023-02-16 11:01:53 -05:00
const auto str = json.dump(4);
2023-02-22 17:12:15 -05:00
utils::io::write_file(path, str, false);
2023-02-16 11:01:53 -05:00
}
catch (const std::exception& e)
{
console::error("Failed to write config file: %s\n", e.what());
}
}
nlohmann::json read_config()
{
2023-02-22 17:12:15 -05:00
const auto path = get_config_file_path();
if (!utils::io::file_exists(path))
2023-02-16 11:01:53 -05:00
{
return {};
}
try
{
2023-02-22 17:12:15 -05:00
const auto data = utils::io::read_file(path);
2023-02-16 11:01:53 -05:00
return nlohmann::json::parse(data);
}
catch (const std::exception& e)
{
console::error("Failed to parse config file: %s\n", e.what());
2023-02-22 17:12:15 -05:00
utils::io::write_file(path, "{}", false);
2023-02-16 11:01:53 -05:00
}
return {};
}
class component final : public component_interface
{
public:
void post_unpack() override
{
2023-02-22 17:12:15 -05:00
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);
}
2023-02-16 11:01:53 -05:00
}
};
}
REGISTER_COMPONENT(config::component)