Skip to content

Commit 2e9288c

Browse files
authored
refactor: 整理Task体系,修正部分Action相关返回逻辑 (#853)
1 parent 833e901 commit 2e9288c

18 files changed

Lines changed: 165 additions & 156 deletions

docs/en_us/2.3-CallbackProtocol.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ Sent when a node begins executing an action.
201201
```
202202

203203
- `task_id`: Task ID (number)
204-
- `node_id`: Node ID (number)
205204
- `action_id`: Action ID (number)
206205
- `name`: Node name (string)
207206
- `focus`: Focus-related data (any type)

docs/en_us/3.1-PipelineProtocol.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,6 @@ When MaaFW sends a callback message, it provides the following information:
957957
"message": "Node.Action.Starting",
958958
"details_json": {
959959
"task_id": 12345,
960-
"node_id": 67890,
961960
"action_id": 11111,
962961
"name": "NodeA",
963962
"focus": {

docs/zh_cn/2.3-回调协议.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ typedef void(MAA_CALL* MaaEventCallback)(void* handle, const char* message, cons
201201
```
202202

203203
- `task_id`: 任务 ID(数字)
204-
- `node_id`: 节点 ID(数字)
205204
- `action_id`: 操作 ID(数字)
206205
- `name`: 节点名称(字符串)
207206
- `focus`: 焦点相关数据(任意类型)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,6 @@ C:/MaaXXX/resource/my_exec/my_exec.exe
962962
"message": "Node.Action.Starting",
963963
"details_json": {
964964
"task_id": 12345,
965-
"node_id": 67890,
966965
"action_id": 11111,
967966
"name": "NodeA",
968967
"focus": {

include/MaaFramework/MaaMsg.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
*
104104
* details_json: {
105105
* task_id: number,
106-
* node_id: number,
107106
* action_id: number,
108107
* name: string,
109108
* focus: any,

source/MaaFramework/Task/ActionTask.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@
1010

1111
MAA_TASK_NS_BEGIN
1212

13-
bool ActionTask::run()
14-
{
15-
LogFunc << VAR(entry_);
16-
17-
return run_with_param({}, {}) != MaaInvalidId;
18-
}
19-
2013
MaaActId ActionTask::run_with_param(const cv::Rect& box, const json::value& reco_detail)
2114
{
2215
LogFunc << VAR(entry_);
@@ -26,17 +19,30 @@ MaaActId ActionTask::run_with_param(const cv::Rect& box, const json::value& reco
2619
return MaaInvalidId;
2720
}
2821

22+
auto node_opt = context_->get_pipeline_data(entry_);
23+
if (!node_opt) {
24+
LogError << "get_pipeline_data failed, task not exist" << VAR(entry_);
25+
return MaaInvalidId;
26+
}
27+
2928
RecoResult fake_reco {
30-
.reco_id = MAA_VISION_NS::VisionBase::generate_uid(),
31-
.name = entry_,
32-
.algorithm = "DirectHit",
29+
.reco_id = MaaInvalidId,
3330
.box = box,
3431
.detail = reco_detail,
3532
};
3633

37-
tasker_->runtime_cache().set_reco_detail(fake_reco.reco_id, fake_reco);
34+
auto act = run_action(fake_reco, *node_opt);
35+
36+
NodeDetail result {
37+
.node_id = generate_node_id(),
38+
.name = entry_,
39+
.reco_id = fake_reco.reco_id,
40+
.action_id = act.action_id,
41+
.completed = act.success,
42+
};
43+
set_node_detail(result.node_id, result);
3844

39-
return run_action(fake_reco).action_id;
45+
return act.action_id;
4046
}
4147

4248
MAA_TASK_NS_END

source/MaaFramework/Task/ActionTask.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ class ActionTask : public TaskBase
1414

1515
virtual ~ActionTask() override = default;
1616

17-
virtual bool run() override;
18-
19-
virtual void post_stop() override {}
20-
2117
public:
2218
MaaActId run_with_param(const cv::Rect& box, const json::value& reco_detail);
2319
};

source/MaaFramework/Task/EmptyTask.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#include "TaskBase.h"
44

5-
#include "Common/Conf.h"
6-
75
MAA_TASK_NS_BEGIN
86

97
// for MaaTaskerPostStop, as a stop mark

source/MaaFramework/Task/PipelineTask.cpp

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool PipelineTask::run()
4141
PipelineData::NextList list = std::move(next);
4242
list.insert(list.end(), std::make_move_iterator(interrupt.begin()), std::make_move_iterator(interrupt.end()));
4343

44-
auto node_detail = run_reco_and_action(list, node);
44+
auto node_detail = run_next(list, node);
4545

4646
if (context_->need_to_stop()) {
4747
LogWarn << "need_to_stop" << VAR(node.name);
@@ -119,7 +119,7 @@ void PipelineTask::post_stop()
119119
context_->need_to_stop() = true;
120120
}
121121

122-
NodeDetail PipelineTask::run_reco_and_action(const PipelineData::NextList& list, const PipelineData& pretask)
122+
NodeDetail PipelineTask::run_next(const PipelineData::NextList& list, const PipelineData& pretask)
123123
{
124124
if (!tasker_) {
125125
LogError << "tasker is null";
@@ -148,7 +148,7 @@ NodeDetail PipelineTask::run_reco_and_action(const PipelineData::NextList& list,
148148
current_clock = std::chrono::steady_clock::now();
149149
cv::Mat image = screencap();
150150

151-
reco = run_recognition(image, list);
151+
reco = recognize_list(image, list);
152152
if (reco.box) { // hit
153153
break;
154154
}
@@ -167,8 +167,85 @@ NodeDetail PipelineTask::run_reco_and_action(const PipelineData::NextList& list,
167167
std::this_thread::sleep_until(current_clock + pretask.rate_limit);
168168
}
169169

170-
auto node_detail = run_action(reco);
171-
return node_detail;
170+
auto hit_opt = context_->get_pipeline_data(reco.name);
171+
if (!hit_opt) {
172+
LogError << "get_pipeline_data failed, node not exist" << VAR(reco.name);
173+
return {};
174+
}
175+
176+
auto act = run_action(reco, *hit_opt);
177+
178+
NodeDetail result {
179+
.node_id = generate_node_id(),
180+
.name = hit_opt->name,
181+
.reco_id = reco.reco_id,
182+
.action_id = act.action_id,
183+
.completed = act.success,
184+
};
185+
set_node_detail(result.node_id, result);
186+
187+
return result;
188+
}
189+
190+
RecoResult PipelineTask::recognize_list(const cv::Mat& image, const PipelineData::NextList& list)
191+
{
192+
LogFunc << VAR(cur_node_) << VAR(list);
193+
194+
if (!context_) {
195+
LogError << "context is null";
196+
return {};
197+
}
198+
199+
if (image.empty()) {
200+
LogError << "Image is empty";
201+
return {};
202+
}
203+
204+
auto cur_opt = context_->get_pipeline_data(cur_node_);
205+
if (!cur_opt) {
206+
LogError << "get_pipeline_data failed, node not exist" << VAR(cur_node_);
207+
return {};
208+
}
209+
210+
const auto& cur_node = *cur_opt;
211+
212+
const json::value reco_list_cb_detail {
213+
{ "task_id", task_id() },
214+
{ "name", cur_node_ },
215+
{ "list", json::array(list) },
216+
{ "focus", cur_node.focus },
217+
};
218+
219+
if (debug_mode() || !cur_node.focus.is_null()) {
220+
notify(MaaMsg_Node_NextList_Starting, reco_list_cb_detail);
221+
}
222+
223+
for (const auto& node : list) {
224+
auto node_opt = context_->get_pipeline_data(node);
225+
if (!node_opt) {
226+
LogError << "get_pipeline_data failed, node not exist" << VAR(node);
227+
continue;
228+
}
229+
const auto& pipeline_data = *node_opt;
230+
231+
RecoResult result = run_recognition(image, pipeline_data);
232+
233+
if (!result.box) {
234+
continue;
235+
}
236+
237+
if (debug_mode() || !cur_node.focus.is_null()) {
238+
notify(MaaMsg_Node_NextList_Succeeded, reco_list_cb_detail);
239+
}
240+
241+
return result;
242+
}
243+
244+
if (debug_mode() || !cur_node.focus.is_null()) {
245+
notify(MaaMsg_Node_NextList_Failed, reco_list_cb_detail);
246+
}
247+
248+
return {};
172249
}
173250

174251
MAA_TASK_NS_END

source/MaaFramework/Task/PipelineTask.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class PipelineTask : public TaskBase
1717
virtual void post_stop() override;
1818

1919
private:
20-
NodeDetail run_reco_and_action(const PipelineData::NextList& list, const PipelineData& pretask);
20+
NodeDetail run_next(const PipelineData::NextList& list, const PipelineData& pretask);
21+
RecoResult recognize_list(const cv::Mat& image, const PipelineData::NextList& list);
2122
};
2223

2324
MAA_TASK_NS_END

0 commit comments

Comments
 (0)