diff --git a/source/cli/CMakeLists.txt b/source/cli/CMakeLists.txt index 23e6c75..1d961e7 100644 --- a/source/cli/CMakeLists.txt +++ b/source/cli/CMakeLists.txt @@ -31,6 +31,8 @@ add_executable(MAABH3_CLI Config/Config.cpp Config/ControlConfig.h Config/ControlConfig.cpp + Config/DeviceConfig.h + Config/DeviceConfig.cpp ) target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolKit) diff --git a/source/cli/Config/DeviceConfig.cpp b/source/cli/Config/DeviceConfig.cpp new file mode 100644 index 0000000..ffdd200 --- /dev/null +++ b/source/cli/Config/DeviceConfig.cpp @@ -0,0 +1,92 @@ +#include "DeviceConfig.h" + +DeviceConfig::DeviceConfig() +{ + build_target_path(); +} +bool DeviceConfig::parse(const json::value& config_opt) +{ + _config_adb = config_opt.get("adb", _config_adb); + _config_device_name = config_opt.get("device_name", _config_device_name); + _config_device_SN = config_opt.get("device_SN", _config_device_SN); + + return true; +} +bool DeviceConfig::load() +{ + auto config_opt = json::open(_target_path); + if (!config_opt) { + std::cerr << "Failed to open device 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 DeviceConfig::to_json() +{ + json::value root = { { "device_name", _config_device_name }, + { "device_SN", _config_device_SN }, + { "adb", _config_adb } }; + + return root; +} + +bool DeviceConfig::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 device config file: " << _target_path << std::endl; + return false; + } + ofs << root; + ofs.close(); + return true; +} + +void DeviceConfig::set_config_device_name(std::string name) +{ + _config_device_name = name; +} + +void DeviceConfig::set_config_device_SN(std::string SN) +{ + _config_device_SN = SN; +} + +void DeviceConfig::set_config_adb(std::string adb) +{ + _config_adb = adb; +} + +std::string DeviceConfig::get_config_device_name() +{ + return _config_device_name; +} + +std::string DeviceConfig::get_config_device_SN() +{ + return _config_device_SN; +} + +std::string DeviceConfig::get_config_adb() +{ + return _config_adb; +} + +void DeviceConfig::build_target_path() +{ + _target_path = config_dir / _target; +} diff --git a/source/cli/Config/DeviceConfig.h b/source/cli/Config/DeviceConfig.h new file mode 100644 index 0000000..d6a8e61 --- /dev/null +++ b/source/cli/Config/DeviceConfig.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include + +#include "Config.h" + +class DeviceConfig : public Config +{ +public: + DeviceConfig(); + virtual ~DeviceConfig() = default; + +public: + inline static const std::filesystem::path local_dir = "."; + inline static const std::filesystem::path config_dir = local_dir / "config" / "device"; + +public: + bool parse(const json::value& config_opt) override; + bool load() override; + json::value to_json() override; + bool save(const json::value& root) override; + +public: + void set_config_device_name(std::string name); + void set_config_device_SN(std::string SN); + void set_config_adb(std::string adb); + +public: + std::string get_config_device_name(); + std::string get_config_device_SN(); + std::string get_config_adb(); + +private: + void build_target_path() override; + +private: + std::string _config_device_name; + std::string _config_device_SN; + std::string _config_adb; + +private: + std::filesystem::path _target_path; +}; \ No newline at end of file