Skip to content

Commit 7309441

Browse files
committed
refactor: 重构ProjectInterface项目结构
1 parent b926e58 commit 7309441

File tree

9 files changed

+98
-67
lines changed

9 files changed

+98
-67
lines changed

source/MaaProjectInterfaceCli/interactor.cpp

+48-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <unordered_set>
55

66
#include "MaaToolkit/Device/MaaToolkitDevice.h"
7+
#include "ProjectInterface/Runner.h"
78
#include "Utils/Logger.h"
89
#include "Utils/Platform.h"
910

@@ -53,11 +54,13 @@ bool Interactor::load(const std::filesystem::path& project_dir)
5354
LogFunc << VAR(project_dir);
5455

5556
if (!config_.load(project_dir)) {
57+
mpause();
5658
return false;
5759
}
5860

5961
if (!config_.check_configuration()) {
6062
std::cout << "### The interface has changed and incompatible configurations have been deleted. ###\n\n";
63+
mpause();
6164
}
6265

6366
return true;
@@ -72,16 +75,32 @@ void Interactor::interact()
7275

7376
while (true) {
7477
print_config();
75-
if (interact_once()) {
78+
if (!interact_once()) {
7679
break;
7780
}
7881
config_.save();
7982
}
8083
}
8184

82-
std::optional<MAA_PROJECT_INTERFACE_NS::RuntimeParam> Interactor::generate_runtime() const
85+
bool Interactor::run()
8386
{
84-
return config_.generate_runtime();
87+
auto runtime = config_.generate_runtime();
88+
if (!runtime) {
89+
LogError << "Failed to generate runtime";
90+
return false;
91+
}
92+
93+
bool ret = MAA_PROJECT_INTERFACE_NS::Runner::run(runtime.value(), on_maafw_notify, this);
94+
95+
if (!ret) {
96+
std::cout << "### Failed to run tasks ###\n\n";
97+
}
98+
else {
99+
std::cout << "### All tasks have been completed ###\n\n";
100+
}
101+
102+
mpause();
103+
return ret;
85104
}
86105

87106
void Interactor::print_config() const
@@ -108,11 +127,12 @@ void Interactor::print_config() const
108127
void Interactor::welcome() const
109128
{
110129
if (config_.interface_data().message.empty()) {
111-
std::cout << "Welcome to use Maa Project Interface CLI!\n\n";
130+
std::cout << "Welcome to use Maa Project Interface CLI!\n";
112131
}
113132
else {
114-
std::cout << MaaNS::utf8_to_crt(config_.interface_data().message) << "\n\n";
133+
std::cout << MaaNS::utf8_to_crt(config_.interface_data().message) << "\n";
115134
}
135+
std::cout << "MaaFramework: " << MAA_VERSION << "\n\n";
116136

117137
std::cout << "Version: " << MaaNS::utf8_to_crt(config_.interface_data().version) << "\n\n";
118138
}
@@ -126,9 +146,10 @@ bool Interactor::interact_once()
126146
std::cout << "\t4. Move task\n";
127147
std::cout << "\t5. Delete task\n";
128148
std::cout << "\t6. Run tasks\n";
149+
std::cout << "\t7. Exit\n";
129150
std::cout << "\n";
130151

131-
int action = input(6);
152+
int action = input(7);
132153

133154
switch (action) {
134155
case 1:
@@ -147,10 +168,13 @@ bool Interactor::interact_once()
147168
delete_task();
148169
break;
149170
case 6:
150-
return true;
171+
run();
172+
break;
173+
case 7:
174+
return false;
151175
}
152176

153-
return false;
177+
return true;
154178
}
155179

156180
void Interactor::select_controller()
@@ -394,3 +418,19 @@ void Interactor::print_config_tasks(bool with_index) const
394418
}
395419
std::cout << "\n";
396420
}
421+
422+
void Interactor::mpause() const
423+
{
424+
std::cout << "\nPress Enter to continue...";
425+
std::cin.sync();
426+
std::cin.get();
427+
}
428+
429+
void Interactor::on_maafw_notify(MaaStringView msg, MaaStringView details_json, MaaTransparentArg callback_arg)
430+
{
431+
Interactor* pthis = static_cast<Interactor*>(callback_arg);
432+
std::ignore = pthis;
433+
434+
std::string entry = json::parse(details_json).value_or(json::value())["entry"].as_string();
435+
std::cout << MaaNS::utf8_to_crt(std::format("on_maafw_notify: {} {}", msg, entry)) << std::endl;
436+
}

source/MaaProjectInterfaceCli/interactor.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#pragma once
22

3-
#include "configurator.h"
3+
#include "ProjectInterface/configurator.h"
44

55
class Interactor
66
{
77
public:
88
bool load(const std::filesystem::path& project_dir);
99
void print_config() const;
1010
void interact();
11-
std::optional<MAA_PROJECT_INTERFACE_NS::RuntimeParam> generate_runtime() const;
11+
bool run();
1212

1313
private:
1414
void interact_for_first_time_use();
@@ -29,6 +29,10 @@ class Interactor
2929

3030
void print_config_tasks(bool with_index = true) const;
3131

32+
void mpause() const;
33+
34+
static void on_maafw_notify(MaaStringView msg, MaaStringView details_json, MaaTransparentArg callback_arg);
35+
3236
private:
33-
Configurator config_;
37+
MAA_PROJECT_INTERFACE_NS::Configurator config_;
3438
};

source/MaaProjectInterfaceCli/main.cpp

+3-26
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@
22
#include <iostream>
33

44
#include "MaaToolkit/MaaToolkitAPI.h"
5-
#include "Utils/Platform.h"
65
#include "Utils/Runtime.h"
76

87
#include "interactor.h"
9-
#include "runner.h"
10-
11-
void mpause()
12-
{
13-
std::cout << "\nPress Enter to continue...";
14-
std::cin.sync();
15-
std::cin.get();
16-
}
178

189
int main(int argc, char** argv)
1910
{
@@ -22,7 +13,6 @@ int main(int argc, char** argv)
2213
Interactor interactor;
2314

2415
if (!interactor.load(MAA_NS::library_dir())) {
25-
mpause();
2616
return -1;
2717
}
2818

@@ -37,23 +27,10 @@ int main(int argc, char** argv)
3727

3828
if (direct) {
3929
interactor.print_config();
40-
}
41-
else {
42-
interactor.interact();
43-
}
44-
45-
auto runtime_opt = interactor.generate_runtime();
46-
if (!runtime_opt) {
47-
mpause();
48-
return -1;
49-
}
50-
51-
bool ret = Runner::run(*runtime_opt);
52-
if (!ret) {
53-
mpause();
54-
return -1;
30+
bool ret = interactor.run();
31+
return ret ? 0 : -1;
5532
}
5633

57-
mpause();
34+
interactor.interact();
5835
return 0;
5936
}

source/MaaProjectInterfaceCli/runner.h

-14
This file was deleted.

source/MaaProjectInterfaceCli/configurator.cpp source/ProjectInterface/Configurator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "configurator.h"
1+
#include "ProjectInterface/Configurator.h"
22

33
#include <ranges>
44

File renamed without changes.

source/MaaProjectInterfaceCli/runner.cpp source/ProjectInterface/Runner.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "runner.h"
1+
#include "ProjectInterface/Runner.h"
22

33
#include <format>
44
#include <iostream>
@@ -10,14 +10,20 @@
1010

1111
#include "Utils/ScopeLeave.hpp"
1212

13-
bool Runner::run(const MAA_PROJECT_INTERFACE_NS::RuntimeParam& param)
13+
MAA_PROJECT_INTERFACE_NS_BEGIN
14+
15+
bool Runner::run(const MAA_PROJECT_INTERFACE_NS::RuntimeParam& param, //
16+
MaaInstanceCallback callback, MaaCallbackTransparentArg callback_arg,
17+
MaaResourceCallback resource_callback, MaaCallbackTransparentArg resource_callback_arg,
18+
MaaControllerCallback controller_callback, MaaCallbackTransparentArg controller_callback_arg)
1419
{
15-
auto maa_handle = MaaCreate(&Runner::on_maafw_notify, nullptr);
20+
auto maa_handle = MaaCreate(callback, callback_arg);
1621

17-
auto controller_handle = MaaAdbControllerCreateV2(param.adb_param.adb_path.c_str(), param.adb_param.address.c_str(),
18-
param.adb_param.controller_type, param.adb_param.config.c_str(),
19-
param.adb_param.agent_path.c_str(), nullptr, nullptr);
20-
auto resource_handle = MaaResourceCreate(nullptr, nullptr);
22+
auto controller_handle =
23+
MaaAdbControllerCreateV2(param.adb_param.adb_path.c_str(), param.adb_param.address.c_str(),
24+
param.adb_param.controller_type, param.adb_param.config.c_str(),
25+
param.adb_param.agent_path.c_str(), controller_callback, controller_callback_arg);
26+
auto resource_handle = MaaResourceCreate(resource_callback, resource_callback_arg);
2127

2228
int64_t cid = MaaControllerPostConnection(controller_handle);
2329
int64_t rid = 0;
@@ -68,10 +74,4 @@ bool Runner::run(const MAA_PROJECT_INTERFACE_NS::RuntimeParam& param)
6874
return true;
6975
}
7076

71-
void Runner::on_maafw_notify(MaaStringView msg, MaaStringView details_json, MaaTransparentArg callback_arg)
72-
{
73-
std::ignore = callback_arg;
74-
75-
std::string entry = json::parse(details_json).value_or(json::value())["entry"].as_string();
76-
std::cout << std::format("on_maafw_notify: {} {}", msg, entry) << std::endl;
77-
}
77+
MAA_PROJECT_INTERFACE_NS_END

source/MaaProjectInterfaceCli/configurator.h source/include/ProjectInterface/Configurator.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <filesystem>
44
#include <string_view>
55

6-
#include "ProjectInterface/Types.h"
6+
#include "Types.h"
7+
8+
MAA_PROJECT_INTERFACE_NS_BEGIN
79

810
class Configurator
911
{
@@ -34,3 +36,5 @@ class Configurator
3436
bool first_time_use_ = false;
3537
MAA_PROJECT_INTERFACE_NS::Configuration config_;
3638
};
39+
40+
MAA_PROJECT_INTERFACE_NS_END
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include "Types.h"
4+
5+
#include "MaaFramework/MaaDef.h"
6+
7+
MAA_PROJECT_INTERFACE_NS_BEGIN
8+
9+
class Runner
10+
{
11+
public:
12+
static bool run(const MAA_PROJECT_INTERFACE_NS::RuntimeParam& param, //
13+
MaaInstanceCallback callback = nullptr, MaaCallbackTransparentArg callback_arg = nullptr,
14+
MaaResourceCallback resource_callback = nullptr,
15+
MaaCallbackTransparentArg resource_callback_arg = nullptr,
16+
MaaControllerCallback controller_callback = nullptr,
17+
MaaCallbackTransparentArg controller_callback_arg = nullptr);
18+
};
19+
20+
MAA_PROJECT_INTERFACE_NS_END

0 commit comments

Comments
 (0)