-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathControlUnit.cpp
More file actions
201 lines (160 loc) · 7.37 KB
/
ControlUnit.cpp
File metadata and controls
201 lines (160 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "LibraryHolder/ControlUnit.h"
#include <filesystem>
#include "ControlUnit/AdbControlUnitAPI.h"
#include "ControlUnit/AndroidControlUnitAPI.h"
#include "ControlUnit/CustomControlUnitAPI.h"
#include "ControlUnit/DbgControlUnitAPI.h"
#include "ControlUnit/Win32ControlUnitAPI.h"
#include "MaaUtils/Logger.h"
#include "MaaUtils/Runtime.h"
MAA_NS_BEGIN
template <typename ControlUnitT, typename GetVersionT>
bool check_version(const std::string& func_name)
{
auto version_func = ControlUnitT::template get_function<GetVersionT>(func_name);
if (!version_func) {
LogError << "Failed to get function get_version";
return false;
}
auto version = version_func();
LogInfo << typeid(ControlUnitT).name() << "Library version:" << version;
if (std::strcmp(version, MAA_VERSION) != 0) {
LogWarn << "ControlUnit and MaaFramework are not same version," << "ControlUnit:" << version << "MaaFramework:" << MAA_VERSION;
}
return true;
}
std::shared_ptr<MAA_CTRL_UNIT_NS::AdbControlUnitAPI> AdbControlUnitLibraryHolder::create_control_unit(
const char* adb_path,
const char* adb_serial,
MaaAdbScreencapMethod screencap_methods,
MaaAdbInputMethod input_methods,
const char* config,
const char* agent_path)
{
if (!load_library(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}
check_version<AdbControlUnitLibraryHolder, decltype(MaaAdbControlUnitGetVersion)>(version_func_name_);
auto create_control_unit_func = get_function<decltype(MaaAdbControlUnitCreate)>(create_func_name_);
if (!create_control_unit_func) {
LogError << "Failed to get function create_control_unit";
return nullptr;
}
auto destroy_control_unit_func = get_function<decltype(MaaAdbControlUnitDestroy)>(destroy_func_name_);
if (!destroy_control_unit_func) {
LogError << "Failed to get function destroy_control_unit";
return nullptr;
}
auto control_unit_handle = create_control_unit_func(adb_path, adb_serial, screencap_methods, input_methods, config, agent_path);
if (!control_unit_handle) {
LogError << "Failed to create control unit";
return nullptr;
}
return std::shared_ptr<MAA_CTRL_UNIT_NS::AdbControlUnitAPI>(control_unit_handle, destroy_control_unit_func);
}
std::shared_ptr<MAA_CTRL_UNIT_NS::AndroidControlUnitAPI> AndroidControlUnitLibraryHolder::create_control_unit(
MaaAndroidScreencapMethod screencap_methods,
MaaAndroidInputMethod input_methods)
{
if (!load_library(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}
check_version<AndroidControlUnitLibraryHolder, decltype(MaaAndroidControlUnitGetVersion)>(version_func_name_);
auto create_control_unit_func = get_function<decltype(MaaAndroidControlUnitCreate)>(create_func_name_);
if (!create_control_unit_func) {
LogError << "Failed to get function create_control_unit";
return nullptr;
}
auto destroy_control_unit_func = get_function<decltype(MaaAndroidControlUnitDestroy)>(destroy_func_name_);
if (!destroy_control_unit_func) {
LogError << "Failed to get function destroy_control_unit";
return nullptr;
}
auto control_unit_handle = create_control_unit_func(screencap_methods, input_methods);
if (!control_unit_handle) {
LogError << "Failed to create control unit";
return nullptr;
}
return std::shared_ptr<MAA_CTRL_UNIT_NS::AndroidControlUnitAPI>(control_unit_handle, destroy_control_unit_func);
}
std::shared_ptr<MAA_CTRL_UNIT_NS::Win32ControlUnitAPI> Win32ControlUnitLibraryHolder::create_control_unit(
void* hWnd,
MaaWin32ScreencapMethod screencap_method,
MaaWin32InputMethod mouse_method,
MaaWin32InputMethod keyboard_method)
{
if (!load_library(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}
check_version<Win32ControlUnitLibraryHolder, decltype(MaaWin32ControlUnitGetVersion)>(version_func_name_);
auto create_control_unit_func = get_function<decltype(MaaWin32ControlUnitCreate)>(create_func_name_);
if (!create_control_unit_func) {
LogError << "Failed to get function create_control_unit";
return nullptr;
}
auto destroy_control_unit_func = get_function<decltype(MaaWin32ControlUnitDestroy)>(destroy_func_name_);
if (!destroy_control_unit_func) {
LogError << "Failed to get function destroy_control_unit";
return nullptr;
}
auto control_unit_handle = create_control_unit_func(hWnd, screencap_method, mouse_method, keyboard_method);
if (!control_unit_handle) {
LogError << "Failed to create control unit";
return nullptr;
}
return std::shared_ptr<MAA_CTRL_UNIT_NS::Win32ControlUnitAPI>(control_unit_handle, destroy_control_unit_func);
}
std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>
DbgControlUnitLibraryHolder::create_control_unit(MaaDbgControllerType type, const char* read_path)
{
if (!load_library(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}
check_version<DbgControlUnitLibraryHolder, decltype(MaaDbgControlUnitGetVersion)>(version_func_name_);
auto create_control_unit_func = get_function<decltype(MaaDbgControlUnitCreate)>(create_func_name_);
if (!create_control_unit_func) {
LogError << "Failed to get function create_control_unit";
return nullptr;
}
auto destroy_control_unit_func = get_function<decltype(MaaDbgControlUnitDestroy)>(destroy_func_name_);
if (!destroy_control_unit_func) {
LogError << "Failed to get function destroy_control_unit";
return nullptr;
}
auto control_unit_handle = create_control_unit_func(type, read_path);
if (!control_unit_handle) {
LogError << "Failed to create control unit";
return nullptr;
}
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit_func);
}
std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>
CustomControlUnitLibraryHolder::create_control_unit(MaaCustomControllerCallbacks* controller, void* controller_arg)
{
if (!load_library(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}
check_version<CustomControlUnitLibraryHolder, decltype(MaaCustomControlUnitGetVersion)>(version_func_name_);
auto create_control_unit_func = get_function<decltype(MaaCustomControlUnitCreate)>(create_func_name_);
if (!create_control_unit_func) {
LogError << "Failed to get function create_control_unit";
return nullptr;
}
auto destroy_control_unit_func = get_function<decltype(MaaCustomControlUnitDestroy)>(destroy_func_name_);
if (!destroy_control_unit_func) {
LogError << "Failed to get function destroy_control_unit";
return nullptr;
}
auto control_unit_handle = create_control_unit_func(controller, controller_arg);
if (!control_unit_handle) {
LogError << "Failed to create control unit";
return nullptr;
}
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit_func);
}
MAA_NS_END