Skip to content

Commit 0eab5ba

Browse files
committed
refactor: 重新整理Command运行期参数替换
1 parent 2e6e93c commit 0eab5ba

File tree

6 files changed

+108
-43
lines changed

6 files changed

+108
-43
lines changed

docs/en_us/3.1-PipelineProtocol.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,12 +564,14 @@ This action attribute requires additional fields:
564564

565565
- `args`: *list<string,>*
566566
The arguments to be executed. Optional.
567-
Some runtime parameters can be passed in:
567+
supports runtime parameters replacement:
568568

569-
- `{ENTRY}`: Task entry name
570-
- `{NODE}`: Current task name
571-
- `{IMAGE}`: Current screenshot file path
572-
- `{BOX}`: Identify the hit target
569+
- `{ENTRY}`: Entry name.
570+
- `{NODE}`: Node name.
571+
- `{IMAGE}`: The path to the file where the screenshot is saved. The file is deleted before the process exits. Please copy it by yourself if you want to save it permanently.
572+
- `{BOX}`: Identify the hit target, the format is `[x, y, w, h]`.
573+
- `{RESOURCE_DIR}`: The path of the resource folder loaded last time.
574+
- `{LIBRARY_DIR}`: The path of the folder where the MaaFW library is located.
573575

574576
- `detach`: *bool*
575577
Detach the child process, that is, do not wait for the child process to complete, and directly continue with the subsequent tasks. Optional, default false.
@@ -580,21 +582,30 @@ Example:
580582
{
581583
"TaskA": {
582584
"action": "Command",
583-
"exec": "my_exec.exe",
585+
"exec": "Python",
584586
"args": [
587+
"{RESOURCE_DIR}/my_script/test.py"
585588
"Haha",
586589
"{IMAGE}",
587590
"{NODE}",
588591
"{BOX}"
589592
]
593+
},
594+
"TaskB": {
595+
"action": "Command",
596+
"exec": "{RESOURCE_DIR}/my_exec/my_exec.exe"
590597
}
591598
}
592599
```
593600

594601
The actual command is:
595602

596603
```bash
597-
my_exec.exe Haha C:/temp/123.png TaskA [0,0,0,0]
604+
# TaskA
605+
Python C:/MaaXXX/resource/my_script/test.py Haha C:/temp/123.png TaskA [0,0,0,0]
606+
607+
# TaskB
608+
C:/MaaXXX/resource/my_exec/my_exec.exe
598609
```
599610

600611
### `Custom`

docs/zh_cn/3.1-任务流水线协议.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,14 @@ Orange 的 next 中,
574574
575575
- `args`: *list<string,>*
576576
执行的参数。可选。
577-
可传入部分运行期参数
577+
支持部分运行期参数替换
578578
579579
- `{ENTRY}`: 任务入口名
580580
- `{NODE}`: 当前任务名
581-
- `{IMAGE}`: 当前截图文件路径
582-
- `{BOX}`: 识别命中的目标
581+
- `{IMAGE}`: 截图保存到文件的路径。该文件在进程退出前删除,若要持久保存请自行复制
582+
- `{BOX}`: 识别命中的目标,格式为 `[x, y, w, h]`
583+
- `{RESOURCE_DIR}`: 最后一次加载的资源文件夹路径
584+
- `{LIBRARY_DIR}`: MaaFW 库所在的文件夹路径
583585
584586
- `detach`: *bool*
585587
分离子进程,即不等待子进程执行完成,直接继续之后后面的任务。可选,默认 false。
@@ -590,21 +592,30 @@ Orange 的 next 中,
590592
{
591593
"TaskA": {
592594
"action": "Command",
593-
"exec": "my_exec.exe",
595+
"exec": "Python",
594596
"args": [
597+
"{RESOURCE_DIR}/my_script/test.py"
595598
"Haha",
596599
"{IMAGE}",
597600
"{NODE}",
598601
"{BOX}"
599602
]
603+
},
604+
"TaskB": {
605+
"action": "Command",
606+
"exec": "{RESOURCE_DIR}/my_exec/my_exec.exe"
600607
}
601608
}
602609
```
603610
604611
实际将会执行命令
605612
606613
```bash
607-
my_exec.exe Haha C:/temp/123.png TaskA [0,0,0,0]
614+
# TaskA
615+
Python C:/MaaXXX/resource/my_script/test.py Haha C:/temp/123.png TaskA [0,0,0,0]
616+
617+
# TaskB
618+
C:/MaaXXX/resource/my_exec/my_exec.exe
608619
```
609620
610621
### `Custom`

source/MaaFramework/Resource/ResourceMgr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class ResourceMgr : public MaaResource
7575

7676
const auto& default_pipeline() const { return default_pipeline_; }
7777

78+
const std::vector<std::filesystem::path>& paths() const { return paths_; }
79+
7880
CustomRecognitionSession custom_recognition(const std::string& name) const;
7981
CustomActionSession custom_action(const std::string& name) const;
8082

source/MaaFramework/Task/Component/Actuator.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,24 @@ bool Actuator::stop_app(const MAA_RES_NS::Action::AppParam& param)
221221
return controller()->stop_app(param.package);
222222
}
223223

224-
bool Actuator::command(const MAA_RES_NS::Action::CommandParam& param, const cv::Rect& box, const std::string& name, const std::string& entry)
224+
bool Actuator::command(
225+
const MAA_RES_NS::Action::CommandParam& param,
226+
const cv::Rect& box,
227+
const std::string& name,
228+
const std::string& entry)
225229
{
226230
if (!controller()) {
227231
LogError << "Controller is null";
228232
return false;
229233
}
234+
auto* resource = tasker_ ? tasker_->resource() : nullptr;
235+
if (!resource) {
236+
LogError << "Resource is null";
237+
return false;
238+
}
239+
230240
CommandAction::Runtime rt {
241+
.resource_paths = resource->paths(),
231242
.entry = entry,
232243
.node = name,
233244
.image = controller()->cached_image(),

source/MaaFramework/Task/Component/CommandAction.cpp

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,37 @@ bool CommandAction::run(const MAA_RES_NS::Action::CommandParam& command, const R
2929
{
3030
LogFunc << VAR(command.exec) << VAR(command.args) << VAR(command.detach);
3131

32-
static std::unordered_map<std::string, std::string> kExecReplacement = {
33-
{ "{LIBRARY_DIR}", path_to_utf8_string(library_dir()) },
32+
auto gen_runtime = [&](const std::string& src) -> std::string {
33+
static std::unordered_map<std::string, std::function<std::string(const Runtime&)>> kArgvReplacement = {
34+
{ "{ENTRY}", std::bind(&CommandAction::get_entry_name, this, std::placeholders::_1) },
35+
{ "{NODE}", std::bind(&CommandAction::get_node_name, this, std::placeholders::_1) },
36+
{ "{IMAGE}", std::bind(&CommandAction::get_image_path, this, std::placeholders::_1) },
37+
{ "{BOX}", std::bind(&CommandAction::get_box, this, std::placeholders::_1) },
38+
{ "{LIBRARY_DIR}", std::bind(&CommandAction::get_library_dir, this, std::placeholders::_1) },
39+
{ "{RESOURCE_DIR}", std::bind(&CommandAction::get_resource_dir, this, std::placeholders::_1) },
40+
};
41+
42+
std::string dst = src;
43+
for (const auto& [key, func] : kArgvReplacement) {
44+
if (src.find(key) == std::string::npos) {
45+
continue;
46+
}
47+
string_replace_all(dst, key, func(runtime));
48+
}
49+
return dst;
3450
};
35-
std::string conv_exec = string_replace_all(command.exec, kExecReplacement);
3651

52+
std::string conv_exec = gen_runtime(command.exec);
3753
std::filesystem::path exec = boost::process::search_path(path(conv_exec));
3854
if (!std::filesystem::exists(exec)) {
3955
LogError << "exec not exists" << VAR(command.exec) << VAR(conv_exec) << VAR(exec);
4056
return false;
4157
}
4258

43-
static std::unordered_map<std::string, std::function<std::string(const Runtime&)>> kArgvReplacement = {
44-
{ "{ENTRY}", std::bind(&CommandAction::gen_entry_name, this, std::placeholders::_1) },
45-
{ "{NODE}", std::bind(&CommandAction::gen_node_name, this, std::placeholders::_1) },
46-
{ "{IMAGE}", std::bind(&CommandAction::gen_image_path, this, std::placeholders::_1) },
47-
{ "{BOX}", std::bind(&CommandAction::gen_box, this, std::placeholders::_1) },
48-
};
49-
5059
std::vector<os_string> args;
5160
for (const std::string& arg : command.args) {
52-
auto iter = kArgvReplacement.find(arg);
61+
std::string dst = gen_runtime(arg);
5362

54-
std::string dst;
55-
if (iter == kArgvReplacement.end()) {
56-
dst = arg;
57-
}
58-
else {
59-
dst = iter->second(runtime);
60-
}
6163
#ifdef _WIN32
6264
args.emplace_back(to_u16(dst));
6365
#else
@@ -82,27 +84,49 @@ bool CommandAction::run(const MAA_RES_NS::Action::CommandParam& command, const R
8284
return true;
8385
}
8486

85-
std::string CommandAction::gen_entry_name(const Runtime& runtime)
87+
std::string CommandAction::get_entry_name(const Runtime& runtime)
8688
{
8789
return runtime.entry;
8890
}
8991

90-
std::string CommandAction::gen_node_name(const Runtime& runtime)
92+
std::string CommandAction::get_node_name(const Runtime& runtime)
9193
{
9294
return runtime.node;
9395
}
9496

95-
std::string CommandAction::gen_image_path(const Runtime& runtime)
97+
std::string CommandAction::get_image_path(const Runtime& runtime)
9698
{
99+
if (!image_path_.empty()) {
100+
return image_path_;
101+
}
102+
97103
auto dst_path = std::filesystem::temp_directory_path() / (format_now_for_filename() + ".png");
98104
cached_images_.emplace_back(dst_path);
99105
imwrite(dst_path, runtime.image);
100-
return path_to_utf8_string(dst_path);
106+
image_path_ = path_to_utf8_string(dst_path);
107+
return image_path_;
101108
}
102109

103-
std::string CommandAction::gen_box(const Runtime& runtime)
110+
std::string CommandAction::get_box(const Runtime& runtime)
104111
{
105112
return std::format("[{},{},{},{}]", runtime.box.x, runtime.box.y, runtime.box.width, runtime.box.height);
106113
}
107114

115+
std::string CommandAction::get_library_dir(const Runtime& runtime)
116+
{
117+
std::ignore = runtime;
118+
119+
return path_to_utf8_string(library_dir());
120+
}
121+
122+
std::string CommandAction::get_resource_dir(const Runtime& runtime)
123+
{
124+
if (runtime.resource_paths.empty()) {
125+
LogWarn << "no resource";
126+
return {};
127+
}
128+
129+
return path_to_utf8_string(runtime.resource_paths.back());
130+
}
131+
108132
MAA_TASK_NS_END
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
#pragma once
22

3+
#include <filesystem>
34
#include <string>
45
#include <vector>
56

67
#include "Conf/Conf.h"
78
#include "Resource/PipelineTypes.h"
8-
#include "Utils/SingletonHolder.hpp"
99
#include "Utils/Platform.h"
1010

1111
MAA_TASK_NS_BEGIN
1212

13-
class CommandAction : public SingletonHolder<CommandAction>
13+
class CommandAction
1414
{
1515
public:
1616
struct Runtime
1717
{
18+
std::vector<std::filesystem::path> resource_paths;
19+
1820
std::string entry;
1921
std::string node;
2022
cv::Mat image;
@@ -27,13 +29,17 @@ class CommandAction : public SingletonHolder<CommandAction>
2729
bool run(const MAA_RES_NS::Action::CommandParam& command, const Runtime& runtime);
2830

2931
private:
30-
std::string gen_entry_name(const Runtime& runtime);
31-
std::string gen_node_name(const Runtime& runtime);
32-
std::string gen_image_path(const Runtime& runtime);
33-
std::string gen_box(const Runtime& runtime);
32+
std::string get_entry_name(const Runtime& runtime);
33+
std::string get_node_name(const Runtime& runtime);
34+
std::string get_image_path(const Runtime& runtime);
35+
std::string get_box(const Runtime& runtime);
36+
std::string get_library_dir(const Runtime& runtime);
37+
std::string get_resource_dir(const Runtime& runtime);
3438

3539
private:
36-
std::vector<std::filesystem::path> cached_images_;
40+
static std::vector<std::filesystem::path> cached_images_;
41+
42+
std::string image_path_;
3743
};
3844

3945
MAA_TASK_NS_END

0 commit comments

Comments
 (0)