Skip to content

Commit 88b8e9c

Browse files
committed
refactor: 重构agent_path相关逻辑
1 parent b2b1720 commit 88b8e9c

File tree

18 files changed

+143
-92
lines changed

18 files changed

+143
-92
lines changed

include/MaaFramework/Instance/MaaController.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ extern "C"
88
{
99
#endif
1010

11-
MaaControllerHandle MAA_FRAMEWORK_API MaaAdbControllerCreate(MaaStringView adb_path, MaaStringView address,
12-
MaaAdbControllerType type, MaaStringView config,
13-
MaaControllerCallback callback,
14-
MaaCallbackTransparentArg callback_arg);
11+
/* Deprecated, please use MaaAdbControllerCreateV2 */ MaaControllerHandle MAA_FRAMEWORK_API MaaAdbControllerCreate(
12+
MaaStringView adb_path, MaaStringView address, MaaAdbControllerType type, MaaStringView config,
13+
MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg);
14+
MaaControllerHandle MAA_FRAMEWORK_API MaaAdbControllerCreateV2(MaaStringView adb_path, MaaStringView address,
15+
MaaAdbControllerType type, MaaStringView agent_path,
16+
MaaStringView config, MaaControllerCallback callback,
17+
MaaCallbackTransparentArg callback_arg);
1518
MaaControllerHandle MAA_FRAMEWORK_API MaaCustomControllerCreate(MaaCustomControllerHandle handle,
1619
MaaTransparentArg handle_arg,
1720
MaaControllerCallback callback,

source/MaaControlUnit/ControlUnitMgr.cpp

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ std::shared_ptr<DeviceListAPI> create_adb_device_list_obj(MaaStringView adb_path
113113
}
114114

115115
std::shared_ptr<ControlUnitAPI> create_adb_controller_unit(MaaStringView adb_path, MaaStringView adb_serial,
116-
MaaAdbControllerType type, MaaStringView config)
116+
MaaAdbControllerType type, MaaStringView agent_path,
117+
MaaStringView config)
117118
{
118119
LogFunc << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config);
119120

@@ -127,19 +128,32 @@ std::shared_ptr<ControlUnitAPI> create_adb_controller_unit(MaaStringView adb_pat
127128

128129
std::shared_ptr<MaatouchInput> maatouch_unit = nullptr;
129130

131+
auto agent_stdpath = MAA_NS::path(agent_path);
132+
auto minitouch_path = agent_stdpath / MAA_NS::path("minitouch");
133+
auto maatouch_path = agent_stdpath / MAA_NS::path("maatouch");
134+
auto minicap_path = agent_stdpath / MAA_NS::path("minicap");
135+
130136
switch (touch_type) {
131137
case MaaAdbControllerType_Touch_Adb:
132138
LogInfo << "touch_type: TapTouchInput";
133139
touch_unit = std::make_shared<TapTouchInput>();
134140
break;
135141
case MaaAdbControllerType_Touch_MiniTouch:
136142
LogInfo << "touch_type: MinitouchInput";
137-
touch_unit = std::make_shared<MinitouchInput>();
143+
if (!std::filesystem::exists(minitouch_path)) {
144+
LogError << "minitouch path not exists" << VAR(minitouch_path);
145+
return nullptr;
146+
}
147+
touch_unit = std::make_shared<MinitouchInput>(minitouch_path);
138148
break;
139149
case MaaAdbControllerType_Touch_MaaTouch:
140150
LogInfo << "touch_type: MaatouchInput";
151+
if (!std::filesystem::exists(maatouch_path)) {
152+
LogError << "maatouch path not exists" << VAR(maatouch_path);
153+
return nullptr;
154+
}
141155
if (!maatouch_unit) {
142-
maatouch_unit = std::make_shared<MaatouchInput>();
156+
maatouch_unit = std::make_shared<MaatouchInput>(maatouch_path);
143157
}
144158
touch_unit = maatouch_unit;
145159
break;
@@ -155,8 +169,12 @@ std::shared_ptr<ControlUnitAPI> create_adb_controller_unit(MaaStringView adb_pat
155169
break;
156170
case MaaAdbControllerType_Key_MaaTouch:
157171
LogInfo << "key_type: MaatouchInput";
172+
if (!std::filesystem::exists(maatouch_path)) {
173+
LogError << "maatouch path not exists" << VAR(maatouch_path);
174+
return nullptr;
175+
}
158176
if (!maatouch_unit) {
159-
maatouch_unit = std::make_shared<MaatouchInput>();
177+
maatouch_unit = std::make_shared<MaatouchInput>(maatouch_path);
160178
}
161179
key_unit = maatouch_unit;
162180
break;
@@ -168,7 +186,11 @@ std::shared_ptr<ControlUnitAPI> create_adb_controller_unit(MaaStringView adb_pat
168186
switch (screencap_type) {
169187
case MaaAdbControllerType_Screencap_FastestWay:
170188
LogInfo << "screencap_type: ScreencapFastestWay";
171-
screencap_unit = std::make_shared<ScreencapFastestWay>();
189+
if (!std::filesystem::exists(minicap_path)) {
190+
LogError << "minicap path not exists" << VAR(minicap_path);
191+
return nullptr;
192+
}
193+
screencap_unit = std::make_shared<ScreencapFastestWay>(minicap_path);
172194
break;
173195
case MaaAdbControllerType_Screencap_RawByNetcat:
174196
LogInfo << "screencap_type: ScreencapRawByNetcat";
@@ -188,11 +210,19 @@ std::shared_ptr<ControlUnitAPI> create_adb_controller_unit(MaaStringView adb_pat
188210
break;
189211
case MaaAdbControllerType_Screencap_MinicapDirect:
190212
LogInfo << "screencap_type: MinicapDirect";
191-
screencap_unit = std::make_shared<MinicapDirect>();
213+
if (!std::filesystem::exists(minicap_path)) {
214+
LogError << "minicap path not exists" << VAR(minicap_path);
215+
return nullptr;
216+
}
217+
screencap_unit = std::make_shared<MinicapDirect>(minicap_path);
192218
break;
193219
case MaaAdbControllerType_Screencap_MinicapStream:
194220
LogInfo << "screencap_type: MinicapStream";
195-
screencap_unit = std::make_shared<MinicapStream>();
221+
if (!std::filesystem::exists(minicap_path)) {
222+
LogError << "minicap path not exists" << VAR(minicap_path);
223+
return nullptr;
224+
}
225+
screencap_unit = std::make_shared<MinicapStream>(minicap_path);
196226
break;
197227
default:
198228
LogError << "Unknown screencap type" << VAR(screencap_type);
@@ -334,14 +364,19 @@ std::shared_ptr<ActivityAPI> create_adb_activity(MaaStringView adb_path, MaaStri
334364
}
335365

336366
std::shared_ptr<TouchInputAPI> create_adb_touch_input(MaaStringView adb_path, MaaStringView adb_serial,
337-
MaaAdbControllerType type, MaaStringView config)
367+
MaaAdbControllerType type, MaaStringView agent_path,
368+
MaaStringView config)
338369
{
339370
auto json_opt = json::parse(std::string_view(config));
340371
if (!json_opt) {
341372
LogError << "Parse config failed, invalid config:" << config;
342373
return nullptr;
343374
}
344375

376+
auto agent_stdpath = MAA_NS::path(agent_path);
377+
auto minitouch_path = agent_stdpath / MAA_NS::path("minitouch");
378+
auto maatouch_path = agent_stdpath / MAA_NS::path("maatouch");
379+
345380
std::shared_ptr<TouchInputBase> touch_unit = nullptr;
346381
switch (type & MaaAdbControllerType_Touch_Mask) {
347382
case MaaAdbControllerType_Touch_Adb:
@@ -350,11 +385,19 @@ std::shared_ptr<TouchInputAPI> create_adb_touch_input(MaaStringView adb_path, Ma
350385
break;
351386
case MaaAdbControllerType_Touch_MiniTouch:
352387
LogInfo << "touch_type: MinitouchInput";
353-
touch_unit = std::make_shared<MinitouchInput>();
388+
if (!std::filesystem::exists(minitouch_path)) {
389+
LogError << "minitouch path not exists" << VAR(minitouch_path);
390+
return nullptr;
391+
}
392+
touch_unit = std::make_shared<MinitouchInput>(minitouch_path);
354393
break;
355394
case MaaAdbControllerType_Touch_MaaTouch:
356395
LogInfo << "touch_type: MaatouchInput";
357-
touch_unit = std::make_shared<MaatouchInput>();
396+
if (!std::filesystem::exists(maatouch_path)) {
397+
LogError << "maatouch path not exists" << VAR(maatouch_path);
398+
return nullptr;
399+
}
400+
touch_unit = std::make_shared<MaatouchInput>(maatouch_path);
358401
break;
359402
default:
360403
LogError << "Unknown touch input type" << VAR(type);
@@ -382,13 +425,16 @@ std::shared_ptr<TouchInputAPI> create_adb_touch_input(MaaStringView adb_path, Ma
382425
}
383426

384427
std::shared_ptr<KeyInputAPI> create_adb_key_input(MaaStringView adb_path, MaaStringView adb_serial,
385-
MaaAdbControllerType type, MaaStringView config)
428+
MaaAdbControllerType type, MaaStringView agent_path,
429+
MaaStringView config)
386430
{
387431
auto json_opt = json::parse(std::string_view(config));
388432
if (!json_opt) {
389433
LogError << "Parse config failed, invalid config:" << config;
390434
return nullptr;
391435
}
436+
auto agent_stdpath = MAA_NS::path(agent_path);
437+
auto maatouch_path = agent_stdpath / MAA_NS::path("maatouch");
392438

393439
std::shared_ptr<KeyInputBase> key_unit = nullptr;
394440
switch (type & MaaAdbControllerType_Key_Mask) {
@@ -398,7 +444,11 @@ std::shared_ptr<KeyInputAPI> create_adb_key_input(MaaStringView adb_path, MaaStr
398444
break;
399445
case MaaAdbControllerType_Key_MaaTouch:
400446
LogInfo << "key_type: MaatouchInput";
401-
key_unit = std::make_shared<MaatouchInput>();
447+
if (!std::filesystem::exists(maatouch_path)) {
448+
LogError << "maatouch path not exists" << VAR(maatouch_path);
449+
return nullptr;
450+
}
451+
key_unit = std::make_shared<MaatouchInput>(maatouch_path);
402452
break;
403453
default:
404454
LogError << "Unknown key input type" << VAR(type);
@@ -426,19 +476,27 @@ std::shared_ptr<KeyInputAPI> create_adb_key_input(MaaStringView adb_path, MaaStr
426476
}
427477

428478
std::shared_ptr<ScreencapAPI> create_adb_screencap(MaaStringView adb_path, MaaStringView adb_serial,
429-
MaaAdbControllerType type, MaaStringView config)
479+
MaaAdbControllerType type, MaaStringView agent_path,
480+
MaaStringView config)
430481
{
431482
auto json_opt = json::parse(std::string_view(config));
432483
if (!json_opt) {
433484
LogError << "Parse config failed, invalid config:" << config;
434485
return nullptr;
435486
}
436487

488+
auto agent_stdpath = MAA_NS::path(agent_path);
489+
auto minicap_path = agent_stdpath / MAA_NS::path("minicap");
490+
437491
std::shared_ptr<ScreencapBase> screencap_unit = nullptr;
438492
switch (type & MaaAdbControllerType_Screencap_Mask) {
439493
case MaaAdbControllerType_Screencap_FastestWay:
440494
LogInfo << "screencap_type: ScreencapFastestWay";
441-
screencap_unit = std::make_shared<ScreencapFastestWay>();
495+
if (!std::filesystem::exists(minicap_path)) {
496+
LogError << "minicap path not exists" << VAR(minicap_path);
497+
return nullptr;
498+
}
499+
screencap_unit = std::make_shared<ScreencapFastestWay>(minicap_path);
442500
break;
443501
case MaaAdbControllerType_Screencap_RawByNetcat:
444502
LogInfo << "screencap_type: ScreencapRawByNetcat";
@@ -458,11 +516,19 @@ std::shared_ptr<ScreencapAPI> create_adb_screencap(MaaStringView adb_path, MaaSt
458516
break;
459517
case MaaAdbControllerType_Screencap_MinicapDirect:
460518
LogInfo << "screencap_type: MinicapDirect";
461-
screencap_unit = std::make_shared<MinicapDirect>();
519+
if (!std::filesystem::exists(minicap_path)) {
520+
LogError << "minicap path not exists" << VAR(minicap_path);
521+
return nullptr;
522+
}
523+
screencap_unit = std::make_shared<MinicapDirect>(minicap_path);
462524
break;
463525
case MaaAdbControllerType_Screencap_MinicapStream:
464526
LogInfo << "screencap_type: MinicapStream";
465-
screencap_unit = std::make_shared<MinicapStream>();
527+
if (!std::filesystem::exists(minicap_path)) {
528+
LogError << "minicap path not exists" << VAR(minicap_path);
529+
return nullptr;
530+
}
531+
screencap_unit = std::make_shared<MinicapStream>(minicap_path);
466532
break;
467533
default:
468534
LogError << "Unknown screencap type" << VAR(type);

source/MaaControlUnit/Input/MaatouchInput.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@ bool MaatouchInput::parse(const json::value& config)
2323
return false;
2424
}
2525

26-
{
27-
auto opt = mopt->find<json::value>("root");
28-
if (!opt) {
29-
LogError << "Cannot find entry prebuilt.maatouch.root";
30-
return false;
31-
}
32-
33-
if (!opt->is_string()) {
34-
return false;
35-
}
36-
37-
root_ = opt->as_string();
38-
}
39-
4026
{
4127
auto opt = mopt->find<json::value>("package");
4228
if (!opt) {
@@ -62,9 +48,8 @@ bool MaatouchInput::init(int swidth, int sheight, int orientation)
6248
return false;
6349
}
6450

65-
auto bin = MAA_FMT::format("{}/universal/maatouch", root_);
66-
67-
if (!invoke_app_->push(bin)) {
51+
const auto bin_path = agent_path_ / MAA_NS::path("universal") / MAA_NS::path("maatouch");
52+
if (!invoke_app_->push(bin_path)) {
6853
return false;
6954
}
7055

source/MaaControlUnit/Input/MaatouchInput.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
#include "UnitBase.h"
44

5+
#include <filesystem>
6+
57
#include "Invoke/InvokeApp.h"
68

79
MAA_CTRL_UNIT_NS_BEGIN
810

911
class MaatouchInput : public TouchInputBase, public KeyInputBase
1012
{
1113
public:
12-
MaatouchInput()
14+
MaatouchInput(std::filesystem::path agent_path) : agent_path_(std::move(agent_path))
1315
{
1416
TouchInputBase::children_.emplace_back(invoke_app_);
1517
KeyInputBase::children_.emplace_back(invoke_app_);
@@ -60,7 +62,7 @@ class MaatouchInput : public TouchInputBase, public KeyInputBase
6062
std::shared_ptr<InvokeApp> invoke_app_ = std::make_shared<InvokeApp>();
6163
std::shared_ptr<IOHandler> shell_handler_ = nullptr;
6264

63-
std::string root_;
65+
std::filesystem::path agent_path_;
6466
std::string package_name_;
6567
int screen_width_ = 0;
6668
int screen_height_ = 0;

source/MaaControlUnit/Input/MinitouchInput.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,6 @@ bool MinitouchInput::parse(const json::value& config)
2323
return false;
2424
}
2525

26-
{
27-
auto opt = mopt->find<json::value>("root");
28-
if (!opt) {
29-
LogError << "Cannot find entry prebuilt.minitouch.root";
30-
return false;
31-
}
32-
33-
if (!opt->is_string()) {
34-
return false;
35-
}
36-
37-
root_ = opt->as_string();
38-
}
39-
4026
{
4127
auto opt = mopt->find<json::value>("arch");
4228
if (!opt) {
@@ -82,9 +68,9 @@ bool MinitouchInput::init(int swidth, int sheight, int orientation)
8268
return false;
8369
}
8470
const std::string& target_arch = *arch_iter;
85-
auto bin = MAA_FMT::format("{}/{}/minitouch", root_, target_arch);
8671

87-
if (!invoke_app_->push(bin)) {
72+
const auto bin_path = agent_path_ / MAA_NS::path(target_arch) / MAA_NS::path("minitouch");
73+
if (!invoke_app_->push(bin_path)) {
8874
return false;
8975
}
9076

source/MaaControlUnit/Input/MinitouchInput.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
#include "UnitBase.h"
44

5+
#include <filesystem>
6+
57
#include "Invoke/InvokeApp.h"
68

79
MAA_CTRL_UNIT_NS_BEGIN
810

911
class MinitouchInput : public TouchInputBase
1012
{
1113
public:
12-
MinitouchInput() { children_.emplace_back(invoke_app_); }
14+
MinitouchInput(std::filesystem::path agent_path) : agent_path_(std::move(agent_path))
15+
{
16+
children_.emplace_back(invoke_app_);
17+
}
1318
virtual ~MinitouchInput() override = default;
1419

1520
public: // from UnitBase
@@ -51,7 +56,7 @@ class MinitouchInput : public TouchInputBase
5156
std::shared_ptr<InvokeApp> invoke_app_ = std::make_shared<InvokeApp>();
5257
std::shared_ptr<IOHandler> shell_handler_ = nullptr;
5358

54-
std::string root_;
59+
std::filesystem::path agent_path_;
5560
std::vector<std::string> arch_list_;
5661
int screen_width_ = 0;
5762
int screen_height_ = 0;

source/MaaControlUnit/Invoke/InvokeApp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ std::optional<int> InvokeApp::sdk()
5959
return atoi(cmd_ret.value().c_str());
6060
}
6161

62-
bool InvokeApp::push(const std::string& path)
62+
bool InvokeApp::push(const std::filesystem::path& path)
6363
{
6464
LogFunc;
6565

@@ -68,7 +68,7 @@ bool InvokeApp::push(const std::string& path)
6868
return false;
6969
}
7070

71-
std::string absolute_path = path_to_crt_string(std::filesystem::absolute(MAA_NS::path(path)));
71+
std::string absolute_path = path_to_crt_string(std::filesystem::absolute(path));
7272
merge_replacement({ { "{BIN_PATH}", absolute_path }, { "{BIN_WORKING_FILE}", tempname_ } });
7373
auto cmd_ret = command(push_bin_argv_.gen(argv_replace_));
7474

source/MaaControlUnit/Invoke/InvokeApp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class InvokeApp : public UnitBase
1717

1818
std::optional<std::vector<std::string>> abilist();
1919
std::optional<int> sdk();
20-
bool push(const std::string& path);
20+
bool push(const std::filesystem::path& path);
2121
bool chmod();
2222

2323
std::optional<std::string> invoke_bin_stdout(const std::string& extra);

source/MaaControlUnit/MaaControlUnit.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
8888
<ConfigurationType>DynamicLibrary</ConfigurationType>
8989
<UseDebugLibraries>false</UseDebugLibraries>
90-
<PlatformToolset>v143</PlatformToolset>
90+
<PlatformToolset>v142</PlatformToolset>
9191
<WholeProgramOptimization>false</WholeProgramOptimization>
9292
<CharacterSet>Unicode</CharacterSet>
9393
</PropertyGroup>

0 commit comments

Comments
 (0)