Skip to content

Commit

Permalink
feat: Tasks Config
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Nov 18, 2023
1 parent a00a9c6 commit 00b1d46
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
71 changes: 71 additions & 0 deletions source/cli/Config/TasksConfig.cpp
Original file line number Diff line number Diff line change
@@ -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<Task> tasklist)
{
_config_tasklist = tasklist;
}

std::vector<Task> TasksConfig::get_config_tasklist()
{
return _config_tasklist;
}

void TasksConfig::build_target_path()
{
_target_path = config_dir / _target;
}
41 changes: 41 additions & 0 deletions source/cli/Config/TasksConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <vector>

#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<Task> tasklist);

public:
std::vector<Task> get_config_tasklist();

private:
void build_target_path() override;

private:
std::vector<Task> _config_tasklist;
};

0 comments on commit 00b1d46

Please sign in to comment.