Add h2 mod config

This commit is contained in:
fed 2023-02-16 17:01:53 +01:00
parent e28dd09cba
commit adf00ff48a
4 changed files with 100 additions and 7 deletions

View File

@ -0,0 +1,58 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "config.hpp"
#include "console.hpp"
#include <utils/hook.hpp>
#include <utils/io.hpp>
#define CONFIG_FILE "players2/default/h2_mod.json"
namespace config
{
void write_config(const nlohmann::json& json)
{
try
{
const auto str = json.dump(4);
utils::io::write_file(CONFIG_FILE, str, false);
}
catch (const std::exception& e)
{
console::error("Failed to write config file: %s\n", e.what());
}
}
nlohmann::json read_config()
{
if (!utils::io::file_exists(CONFIG_FILE))
{
return {};
}
try
{
const auto data = utils::io::read_file(CONFIG_FILE);
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);
}
return {};
}
class component final : public component_interface
{
public:
void post_unpack() override
{
}
};
}
REGISTER_COMPONENT(config::component)

View File

@ -0,0 +1,27 @@
#pragma once
namespace config
{
nlohmann::json read_config();
void write_config(const nlohmann::json& json);
template <typename T>
std::optional<T> get(const std::string& key)
{
const auto cfg = read_config();
if (!cfg.is_object() || !cfg.contains(key))
{
return {};
}
return {cfg[key].get<T>()};
}
template <typename T>
void set(const std::string& key, const T& value)
{
auto cfg = read_config();
cfg[key] = value;
write_config(cfg);
}
}

View File

@ -54,6 +54,11 @@ namespace console
template <typename... Args>
int invoke_printf(const char* fmt, Args&&... args)
{
if (printf_hook.get_original() == nullptr)
{
return printf(fmt, std::forward<Args>(args)...);
}
return printf_hook.invoke<int>(fmt, std::forward<Args>(args)...);
}
@ -336,10 +341,13 @@ namespace console
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
void post_unpack() override
void post_start() override
{
printf_hook.create(printf, printf_stub);
}
void post_unpack() override
{
ShowWindow(GetConsoleWindow(), SW_SHOW);
SetConsoleTitle("H2-Mod");

View File

@ -2,14 +2,13 @@
#include "loader/component_loader.hpp"
#include "language.hpp"
#include "config.hpp"
#include "localized_strings.hpp"
#include <utils/hook.hpp>
#include <utils/io.hpp>
#define LANGUAGE_FILE "players2/default/language"
namespace language
{
namespace
@ -47,14 +46,15 @@ namespace language
const char* get_loc_language_string()
{
if (!utils::io::file_exists(LANGUAGE_FILE))
const auto data = config::get<std::string>("language");
if (!data.has_value())
{
return nullptr;
}
static char language[0x200] = {0};
const auto data = utils::io::read_file(LANGUAGE_FILE);
strcpy_s(language, data.data());
const auto& value = data.value();
strcpy_s(language, value.data());
return language;
}
}
@ -87,7 +87,7 @@ namespace language
void set(const std::string& lang)
{
utils::io::write_file(LANGUAGE_FILE, lang, false);
config::set("language", lang);
}
void set_from_index(const int index)