From f316887fd65b244a8953eac612a3900c487569f0 Mon Sep 17 00:00:00 2001 From: dongwlin Date: Sat, 18 Nov 2023 21:16:11 +0800 Subject: [PATCH] feat: Control Config --- source/cli/CMakeLists.txt | 4 ++ source/cli/Config/Config.cpp | 15 +++++ source/cli/Config/Config.h | 27 +++++++++ source/cli/Config/ControlConfig.cpp | 92 +++++++++++++++++++++++++++++ source/cli/Config/ControlConfig.h | 45 ++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 source/cli/Config/Config.cpp create mode 100644 source/cli/Config/Config.h create mode 100644 source/cli/Config/ControlConfig.cpp create mode 100644 source/cli/Config/ControlConfig.h diff --git a/source/cli/CMakeLists.txt b/source/cli/CMakeLists.txt index 1e5e088..23e6c75 100644 --- a/source/cli/CMakeLists.txt +++ b/source/cli/CMakeLists.txt @@ -27,6 +27,10 @@ add_executable(MAABH3_CLI CustomRecognizer/CustomRecognizerRegistry.h CustomRecognizer/CombatRecognizer.h CustomRecognizer/CombatRecognizer.cpp + Config/Config.h + Config/Config.cpp + Config/ControlConfig.h + Config/ControlConfig.cpp ) target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolKit) diff --git a/source/cli/Config/Config.cpp b/source/cli/Config/Config.cpp new file mode 100644 index 0000000..2a58c51 --- /dev/null +++ b/source/cli/Config/Config.cpp @@ -0,0 +1,15 @@ +#include "Config.h" + +bool Config::dump() +{ + return save(to_json()); +} + +void Config::set_target(std::string target) +{ + _target = target; + if (!_target.ends_with(".json")) { + _target += ".json"; + } + build_target_path(); +} \ No newline at end of file diff --git a/source/cli/Config/Config.h b/source/cli/Config/Config.h new file mode 100644 index 0000000..e9714c7 --- /dev/null +++ b/source/cli/Config/Config.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "../meojson/json.hpp" + +class Config +{ +public: + virtual ~Config() = default; + +public: + virtual bool parse(const json::value& config_json) = 0; + virtual bool load() = 0; + virtual json::value to_json() = 0; + virtual bool save(const json::value& root) = 0; + bool dump(); + +public: + virtual void set_target(const std::string target); + +private: + virtual void build_target_path() = 0; + +protected: + std::string _target = "default"; +}; \ No newline at end of file diff --git a/source/cli/Config/ControlConfig.cpp b/source/cli/Config/ControlConfig.cpp new file mode 100644 index 0000000..b07f70c --- /dev/null +++ b/source/cli/Config/ControlConfig.cpp @@ -0,0 +1,92 @@ +#include "ControlConfig.h" + +ControlConfig::ControlConfig() +{ + build_target_path(); +} + +bool ControlConfig::parse(const json::value& config_opt) +{ + _config_server = config_opt.get("server", _config_server); + _config_screencap = config_opt.get("screencap", _config_screencap); + _config_touch = config_opt.get("touch", _config_touch); + + return true; +} + +bool ControlConfig::load() +{ + auto config_opt = json::open(_target_path); + if (!config_opt) { + std::cerr << "Failed to open control config file: " << _target_path << std::endl; + return false; + } + + auto& config = *config_opt; + if (!config.is_object()) { + std::cerr << "Json is not an object: " << config << std::endl; + return false; + } + + if (!parse(config)) { + std::cerr << "Failed to parse control: " << config << std::endl; + return false; + } + + return true; +} + +json::value ControlConfig::to_json() +{ + json::value root; + root = { { "server", _config_server }, { "screencap", _config_screencap }, { "touch", _config_touch } }; + return root; +} + +bool ControlConfig::save(const json::value& root) +{ + std::filesystem::create_directories(config_dir); + std::ofstream ofs(_target_path, std::ios::out); + if (!ofs.is_open()) { + std::cerr << "Failed to open control config file: " << _target_path << std::endl; + return false; + } + ofs << root; + ofs.close(); + return true; +} + +void ControlConfig::set_config_server(int server) +{ + _config_server = server; +} + +void ControlConfig::set_config_screencap(int screencap) +{ + _config_screencap = screencap; +} + +void ControlConfig::set_config_touch(int touch) +{ + _config_touch = touch; +} + +int ControlConfig::get_config_server() +{ + return _config_server; +} + +int ControlConfig::get_config_screencap() +{ + return _config_screencap; +} + +int ControlConfig::get_config_touch() +{ + return _config_touch; +} + +void ControlConfig::build_target_path() +{ + _target_path = config_dir / _target; +} diff --git a/source/cli/Config/ControlConfig.h b/source/cli/Config/ControlConfig.h new file mode 100644 index 0000000..90dd803 --- /dev/null +++ b/source/cli/Config/ControlConfig.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include + +#include "Config.h" + +class ControlConfig : public Config +{ +public: + ControlConfig(); + virtual ~ControlConfig() = default; + +public: + inline static const std::filesystem::path local_dir = "."; + inline static const std::filesystem::path config_dir = local_dir / "config" / "control"; + +public: + bool parse(const json::value& config_json) override; + bool load() override; + json::value to_json() override; + bool save(const json::value& root) override; + +public: + void set_config_server(int server); + void set_config_screencap(int screencap); + void set_config_touch(int touch); + +public: + int get_config_server(); + int get_config_screencap(); + int get_config_touch(); + +private: + void build_target_path() override; + +private: + int _config_server = 1; + int _config_screencap = 3; + int _config_touch = 3; + +private: + std::filesystem::path _target_path; +}; \ No newline at end of file