Add h2 mod config
This commit is contained in:
parent
e28dd09cba
commit
adf00ff48a
58
src/client/component/config.cpp
Normal file
58
src/client/component/config.cpp
Normal 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)
|
27
src/client/component/config.hpp
Normal file
27
src/client/component/config.hpp
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -54,6 +54,11 @@ namespace console
|
|||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
int invoke_printf(const char* fmt, Args&&... 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)...);
|
return printf_hook.invoke<int>(fmt, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,10 +341,13 @@ namespace console
|
|||||||
ShowWindow(GetConsoleWindow(), SW_HIDE);
|
ShowWindow(GetConsoleWindow(), SW_HIDE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void post_unpack() override
|
void post_start() override
|
||||||
{
|
{
|
||||||
printf_hook.create(printf, printf_stub);
|
printf_hook.create(printf, printf_stub);
|
||||||
|
}
|
||||||
|
|
||||||
|
void post_unpack() override
|
||||||
|
{
|
||||||
ShowWindow(GetConsoleWindow(), SW_SHOW);
|
ShowWindow(GetConsoleWindow(), SW_SHOW);
|
||||||
SetConsoleTitle("H2-Mod");
|
SetConsoleTitle("H2-Mod");
|
||||||
|
|
||||||
|
@ -2,14 +2,13 @@
|
|||||||
#include "loader/component_loader.hpp"
|
#include "loader/component_loader.hpp"
|
||||||
|
|
||||||
#include "language.hpp"
|
#include "language.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
|
|
||||||
#include "localized_strings.hpp"
|
#include "localized_strings.hpp"
|
||||||
|
|
||||||
#include <utils/hook.hpp>
|
#include <utils/hook.hpp>
|
||||||
#include <utils/io.hpp>
|
#include <utils/io.hpp>
|
||||||
|
|
||||||
#define LANGUAGE_FILE "players2/default/language"
|
|
||||||
|
|
||||||
namespace language
|
namespace language
|
||||||
{
|
{
|
||||||
namespace
|
namespace
|
||||||
@ -47,14 +46,15 @@ namespace language
|
|||||||
|
|
||||||
const char* get_loc_language_string()
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char language[0x200] = {0};
|
static char language[0x200] = {0};
|
||||||
const auto data = utils::io::read_file(LANGUAGE_FILE);
|
const auto& value = data.value();
|
||||||
strcpy_s(language, data.data());
|
strcpy_s(language, value.data());
|
||||||
return language;
|
return language;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ namespace language
|
|||||||
|
|
||||||
void set(const std::string& lang)
|
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)
|
void set_from_index(const int index)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user