Skip to content

Commit

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

Expand Down
15 changes: 15 additions & 0 deletions source/cli/Config/Config.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
27 changes: 27 additions & 0 deletions source/cli/Config/Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <string>

#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";
};
92 changes: 92 additions & 0 deletions source/cli/Config/ControlConfig.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
45 changes: 45 additions & 0 deletions source/cli/Config/ControlConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <filesystem>
#include <iostream>
#include <fstream>

#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;
};

0 comments on commit f316887

Please sign in to comment.