From 00b1d468714b0373e48cc229bb600765f3f7fc95 Mon Sep 17 00:00:00 2001 From: dongwlin Date: Sun, 19 Nov 2023 00:25:55 +0800 Subject: [PATCH] feat: Tasks Config --- source/cli/CMakeLists.txt | 2 + source/cli/Config/TasksConfig.cpp | 71 +++++++++++++++++++++++++++++++ source/cli/Config/TasksConfig.h | 41 ++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 source/cli/Config/TasksConfig.cpp create mode 100644 source/cli/Config/TasksConfig.h diff --git a/source/cli/CMakeLists.txt b/source/cli/CMakeLists.txt index 1d961e7..b4c96c6 100644 --- a/source/cli/CMakeLists.txt +++ b/source/cli/CMakeLists.txt @@ -33,6 +33,8 @@ add_executable(MAABH3_CLI Config/ControlConfig.cpp Config/DeviceConfig.h Config/DeviceConfig.cpp + Config/TasksConfig.h + Config/TasksConfig.cpp ) target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolKit) diff --git a/source/cli/Config/TasksConfig.cpp b/source/cli/Config/TasksConfig.cpp new file mode 100644 index 0000000..c322dd9 --- /dev/null +++ b/source/cli/Config/TasksConfig.cpp @@ -0,0 +1,71 @@ +#include "TasksConfig.h" + +TasksConfig::TasksConfig() +{ + build_target_path(); +} + +bool TasksConfig::parse(const json::value& config_opt) +{ + auto arr = config_opt.as_array(); + for (auto& el : arr) { + Task task; + task.name = el.get("name", ""); + task.status = el.get("status", true); + task.param = el.get("param"); + task.type = el.get("type", ""); + _config_tasklist.emplace_back(task); + } + + return true; +} + +bool TasksConfig::load() +{ + auto config_opt = json::open(_target_path); + if (!config_opt) { + std::cerr << "Failed to open config file: " << _target_path << std::endl; + return false; + } + + auto& config = *config_opt; + if (!config.is_array()) { + std::cerr << "Json is not an array: " << config << std::endl; + return false; + } + + if (!parse(config)) { + std::cerr << "Failed to parse control: " << config << std::endl; + return false; + } + + return true; +} + +json::value TasksConfig::to_json() +{ + json::value root = json::array(); + for (auto& task : _config_tasklist) { + json::object obj = { + { "name", task.name }, { "status", task.status }, { "param", task.param }, { "type", task.type } + }; + root.emplace(obj); + } + + return root; +} + +void TasksConfig::set_config_tasklist(std::vector tasklist) +{ + _config_tasklist = tasklist; +} + +std::vector TasksConfig::get_config_tasklist() +{ + return _config_tasklist; +} + +void TasksConfig::build_target_path() +{ + _target_path = config_dir / _target; +} diff --git a/source/cli/Config/TasksConfig.h b/source/cli/Config/TasksConfig.h new file mode 100644 index 0000000..9db91fc --- /dev/null +++ b/source/cli/Config/TasksConfig.h @@ -0,0 +1,41 @@ +#pragma once + +#include + +#include "Config.h" + +struct Task +{ + std::string name; + bool status = true; + std::string type; + json::value param = json::object(); +}; + +class TasksConfig : public Config +{ +public: + TasksConfig(); + virtual ~TasksConfig() = default; + +public: + inline static const std::filesystem::path local_dir = "."; + inline static const std::filesystem::path config_dir = local_dir / "config" / "tasks"; + +public: + bool parse(const json::value& config_opt) override; + bool load() override; + json::value to_json() override; + +public: + void set_config_tasklist(std::vector tasklist); + +public: + std::vector get_config_tasklist(); + +private: + void build_target_path() override; + +private: + std::vector _config_tasklist; +}; \ No newline at end of file