Skip to content

Commit f316887

Browse files
committed
feat: Control Config
1 parent b827173 commit f316887

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

source/cli/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ add_executable(MAABH3_CLI
2727
CustomRecognizer/CustomRecognizerRegistry.h
2828
CustomRecognizer/CombatRecognizer.h
2929
CustomRecognizer/CombatRecognizer.cpp
30+
Config/Config.h
31+
Config/Config.cpp
32+
Config/ControlConfig.h
33+
Config/ControlConfig.cpp
3034
)
3135
target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolKit)
3236

source/cli/Config/Config.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "Config.h"
2+
3+
bool Config::dump()
4+
{
5+
return save(to_json());
6+
}
7+
8+
void Config::set_target(std::string target)
9+
{
10+
_target = target;
11+
if (!_target.ends_with(".json")) {
12+
_target += ".json";
13+
}
14+
build_target_path();
15+
}

source/cli/Config/Config.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include "../meojson/json.hpp"
6+
7+
class Config
8+
{
9+
public:
10+
virtual ~Config() = default;
11+
12+
public:
13+
virtual bool parse(const json::value& config_json) = 0;
14+
virtual bool load() = 0;
15+
virtual json::value to_json() = 0;
16+
virtual bool save(const json::value& root) = 0;
17+
bool dump();
18+
19+
public:
20+
virtual void set_target(const std::string target);
21+
22+
private:
23+
virtual void build_target_path() = 0;
24+
25+
protected:
26+
std::string _target = "default";
27+
};

source/cli/Config/ControlConfig.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "ControlConfig.h"
2+
3+
ControlConfig::ControlConfig()
4+
{
5+
build_target_path();
6+
}
7+
8+
bool ControlConfig::parse(const json::value& config_opt)
9+
{
10+
_config_server = config_opt.get("server", _config_server);
11+
_config_screencap = config_opt.get("screencap", _config_screencap);
12+
_config_touch = config_opt.get("touch", _config_touch);
13+
14+
return true;
15+
}
16+
17+
bool ControlConfig::load()
18+
{
19+
auto config_opt = json::open(_target_path);
20+
if (!config_opt) {
21+
std::cerr << "Failed to open control config file: " << _target_path << std::endl;
22+
return false;
23+
}
24+
25+
auto& config = *config_opt;
26+
if (!config.is_object()) {
27+
std::cerr << "Json is not an object: " << config << std::endl;
28+
return false;
29+
}
30+
31+
if (!parse(config)) {
32+
std::cerr << "Failed to parse control: " << config << std::endl;
33+
return false;
34+
}
35+
36+
return true;
37+
}
38+
39+
json::value ControlConfig::to_json()
40+
{
41+
json::value root;
42+
root = { { "server", _config_server }, { "screencap", _config_screencap }, { "touch", _config_touch } };
43+
return root;
44+
}
45+
46+
bool ControlConfig::save(const json::value& root)
47+
{
48+
std::filesystem::create_directories(config_dir);
49+
std::ofstream ofs(_target_path, std::ios::out);
50+
if (!ofs.is_open()) {
51+
std::cerr << "Failed to open control config file: " << _target_path << std::endl;
52+
return false;
53+
}
54+
ofs << root;
55+
ofs.close();
56+
return true;
57+
}
58+
59+
void ControlConfig::set_config_server(int server)
60+
{
61+
_config_server = server;
62+
}
63+
64+
void ControlConfig::set_config_screencap(int screencap)
65+
{
66+
_config_screencap = screencap;
67+
}
68+
69+
void ControlConfig::set_config_touch(int touch)
70+
{
71+
_config_touch = touch;
72+
}
73+
74+
int ControlConfig::get_config_server()
75+
{
76+
return _config_server;
77+
}
78+
79+
int ControlConfig::get_config_screencap()
80+
{
81+
return _config_screencap;
82+
}
83+
84+
int ControlConfig::get_config_touch()
85+
{
86+
return _config_touch;
87+
}
88+
89+
void ControlConfig::build_target_path()
90+
{
91+
_target_path = config_dir / _target;
92+
}

source/cli/Config/ControlConfig.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include <filesystem>
4+
#include <iostream>
5+
#include <fstream>
6+
7+
#include "Config.h"
8+
9+
class ControlConfig : public Config
10+
{
11+
public:
12+
ControlConfig();
13+
virtual ~ControlConfig() = default;
14+
15+
public:
16+
inline static const std::filesystem::path local_dir = ".";
17+
inline static const std::filesystem::path config_dir = local_dir / "config" / "control";
18+
19+
public:
20+
bool parse(const json::value& config_json) override;
21+
bool load() override;
22+
json::value to_json() override;
23+
bool save(const json::value& root) override;
24+
25+
public:
26+
void set_config_server(int server);
27+
void set_config_screencap(int screencap);
28+
void set_config_touch(int touch);
29+
30+
public:
31+
int get_config_server();
32+
int get_config_screencap();
33+
int get_config_touch();
34+
35+
private:
36+
void build_target_path() override;
37+
38+
private:
39+
int _config_server = 1;
40+
int _config_screencap = 3;
41+
int _config_touch = 3;
42+
43+
private:
44+
std::filesystem::path _target_path;
45+
};

0 commit comments

Comments
 (0)