Skip to content

Commit 989a458

Browse files
committed
fix: delay等逻辑移出action (#1190)
1 parent 3dc5dec commit 989a458

10 files changed

Lines changed: 127 additions & 115 deletions

File tree

docs/en_us/3.1-PipelineProtocol.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ This algorithm property requires additional fields:
456456
- `roi`: *array<int, 4>* | *string*
457457
Region of Interest (ROI), defining the image recognition boundary; related image processing is performed only within this area. Optional, default [0, 0, 0, 0], i.e. full screen.
458458
- *array<int, 4>*: Recognition area coordinates [x, y, w, h]. Supports negative values **💡 v5.6**: negative x/y means calculating from right/bottom edge; w/h of 0 means extending to edge, negative means taking absolute value and treating (x, y) as bottom-right corner.
459-
- *string*: Fill in the node name, and identify within the target range identified by a previously executed node. Also supports `[Anchor]AnchorName` format to reference the node corresponding to an anchor **💡 v5.9**. If the referenced pre-task or anchor has no recognition result, the recognition is treated as failed.
459+
- *string*: Fill in the node name to perform recognition within the target range produced by a previously executed node. Also supports `[Anchor]AnchorName` format to reference the node corresponding to an anchor **💡 v5.9**. If the referenced pre-task or anchor has no recognition result, the recognition is treated as failed.
460460
461461
- `roi_offset`: *array<int, 4>*
462462
Move additionally based on `roi` as the range, and add the four values separately. Optional, default [0, 0, 0, 0].
@@ -1079,7 +1079,7 @@ Additional properties for this action:
10791079
- `target`: *true* | *string* | *array<int, 2>* | *array<int, 4>* **💡 v5.5**
10801080
Position of the scroll target. The mouse will first move to this position before scrolling. Optional, default is `true`.
10811081
- *true*: Target is the position just recognized in this node (i.e., itself).
1082-
- *string*: Fill in a node name; the target is the position recognized by a previously executed node. Also supports `[Anchor]AnchorName` format to reference the node corresponding to an anchor **💡 v5.9**.
1082+
- *string*: Fill in a node name; the target is the position recognized by a previously executed node. Also supports `[Anchor]AnchorName` format to reference the node corresponding to an anchor **💡 v5.9**. If the referenced pre-task or anchor has no recognition result, the action is treated as failed.
10831083
- *array<int, 2>*: Fixed coordinate point `[x, y]`.
10841084
- *array<int, 4>*: Fixed coordinate area `[x, y, w, h]`, a random point will be selected within the rectangle (with higher probability towards the center and lower probability at the edges). Use [0, 0, 0, 0] for full screen.
10851085

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ Pipeline v2 时,将这些字段放到 `recognition.param` 中即可。
10881088
- `target`: *true* | *string* | *array<int, 2>* | *array<int, 4>* **💡 v5.5**
10891089
滚动目标的位置,鼠标会先移动到该位置再进行滚动。可选,默认 true 。
10901090
- *true*: 目标为本节点中刚刚识别到的位置(即自身)。
1091-
- *string*: 填写节点名,目标为之前执行过的某节点识别到的位置。也支持 `[Anchor]锚点名` 格式引用锚点对应的节点 **💡 v5.9**
1091+
- *string*: 填写节点名,目标为之前执行过的某节点识别到的位置。也支持 `[Anchor]锚点名` 格式引用锚点对应的节点 **💡 v5.9**若引用的前置节点或锚点识别结果为空,则视为动作失败。
10921092
- *array<int, 2>*: 固定坐标点 `[x, y]`
10931093
- *array<int, 4>*: 固定坐标区域 `[x, y, w, h]`,会在矩形内随机选取一点(越靠近中心概率越高,边缘概率相对较低),若希望全屏可设为 [0, 0, 0, 0]
10941094

source/MaaFramework/Task/Component/ActionHelper.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ ActionHelper::ActionHelper(Context* context)
1414
{
1515
}
1616

17-
bool ActionHelper::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box, const std::string& name)
17+
bool ActionHelper::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& ref_box, const std::string& name)
1818
{
1919
if (param.time <= std::chrono::milliseconds(0)) {
2020
return true;
2121
}
2222

23+
auto roi = get_target_rect(param.target, ref_box);
24+
if (roi.empty()) {
25+
LogError << "failed to get target rect for wait_freezes" << VAR(name);
26+
return false;
27+
}
28+
2329
if (!controller()) {
2430
LogError << "Controller is null";
2531
return false;
@@ -35,9 +41,9 @@ bool ActionHelper::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const
3541
auto screencap_clock = std::chrono::steady_clock::now();
3642
cv::Mat pre_image = controller()->screencap();
3743

38-
auto corrected_box = correct_roi(box, pre_image);
39-
if (!corrected_box) {
40-
LogError << "corrected box is empty" << VAR(box);
44+
auto corrected_roi = correct_roi(roi, pre_image);
45+
if (!corrected_roi) {
46+
LogError << "corrected roi is empty" << VAR(roi);
4147
return false;
4248
}
4349

@@ -68,7 +74,7 @@ bool ActionHelper::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const
6874
}
6975

7076
std::string draw_name = name.empty() ? "wait_freezes" : std::format("{}_wait_freezes", name);
71-
TemplateComparator comparator(pre_image, cur_image, { *corrected_box }, comp_param, draw_name);
77+
TemplateComparator comparator(pre_image, cur_image, { *corrected_roi }, comp_param, draw_name);
7278

7379
VisionBase::save_draws(draw_name, comparator.draws());
7480

@@ -104,6 +110,10 @@ cv::Rect ActionHelper::get_target_rect(const MAA_RES_NS::Action::Target& target,
104110
case Target::Type::PreTask: {
105111
const auto& name = std::get<std::string>(target.param);
106112
raw = get_rect_from_node(name);
113+
if (raw.empty()) {
114+
LogWarn << "pre task has no rect" << VAR(name);
115+
return { };
116+
}
107117
LogDebug << "pre task" << VAR(name) << VAR(raw);
108118
} break;
109119

@@ -115,6 +125,10 @@ cv::Rect ActionHelper::get_target_rect(const MAA_RES_NS::Action::Target& target,
115125
return { };
116126
}
117127
raw = get_rect_from_node(*node_name);
128+
if (raw.empty()) {
129+
LogWarn << "anchor node has no rect" << VAR(anchor_name) << VAR(*node_name);
130+
return { };
131+
}
118132
LogDebug << "anchor" << VAR(anchor_name) << VAR(*node_name) << VAR(raw);
119133
} break;
120134

source/MaaFramework/Task/Component/ActionHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ActionHelper : public NonCopyable
1414
public:
1515
explicit ActionHelper(Context* context);
1616

17-
bool wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box, const std::string& name = "");
17+
bool wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& ref_box, const std::string& name = "");
1818

1919
cv::Rect get_target_rect(const MAA_RES_NS::Action::Target& target, const cv::Rect& box = { });
2020

source/MaaFramework/Task/Component/Actuator.cpp

Lines changed: 46 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -36,110 +36,101 @@ ActionResult Actuator::run(const cv::Rect& reco_hit, MaaRecoId reco_id, const Pi
3636
return { };
3737
}
3838

39-
wait_freezes(pipeline_data.pre_wait_freezes, reco_hit, pipeline_data.name);
40-
sleep(pipeline_data.pre_delay);
41-
42-
auto& rt_cache = tasker_->runtime_cache();
4339
ActionResult result;
4440

45-
for (uint i = 0; i < pipeline_data.repeat; ++i) {
46-
if (i > 0) {
47-
wait_freezes(pipeline_data.repeat_wait_freezes, reco_hit, pipeline_data.name);
48-
sleep(pipeline_data.repeat_delay);
49-
}
50-
51-
if (context_.need_to_stop()) {
52-
return { };
53-
}
54-
55-
result = execute_action(reco_hit, reco_id, pipeline_data, entry);
56-
LogInfo << "action" << VAR(i) << VAR(pipeline_data.repeat) << VAR(result);
57-
rt_cache.set_action_detail(result.action_id, result);
58-
59-
if (context_.need_to_stop()) {
60-
return { };
61-
}
62-
}
63-
64-
wait_freezes(pipeline_data.post_wait_freezes, reco_hit, pipeline_data.name);
65-
sleep(pipeline_data.post_delay);
66-
67-
return result;
68-
}
69-
70-
ActionResult
71-
Actuator::execute_action(const cv::Rect& reco_hit, MaaRecoId reco_id, const PipelineData& pipeline_data, const std::string& entry)
72-
{
73-
using namespace MAA_RES_NS::Action;
74-
7541
switch (pipeline_data.action_type) {
7642
case Type::DoNothing:
77-
return do_nothing(pipeline_data.name);
43+
result = do_nothing(pipeline_data.name);
44+
break;
7845

7946
case Type::Click:
80-
return click(std::get<ClickParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
47+
result = click(std::get<ClickParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
48+
break;
8149

8250
case Type::LongPress:
83-
return long_press(std::get<LongPressParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
51+
result = long_press(std::get<LongPressParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
52+
break;
8453

8554
case Type::Swipe:
86-
return swipe(std::get<SwipeParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
55+
result = swipe(std::get<SwipeParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
56+
break;
8757

8858
case Type::MultiSwipe:
89-
return multi_swipe(std::get<MultiSwipeParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
59+
result = multi_swipe(std::get<MultiSwipeParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
60+
break;
9061

9162
case Type::TouchDown:
92-
return touch_down(std::get<TouchParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
63+
result = touch_down(std::get<TouchParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
64+
break;
9365

9466
case Type::TouchMove:
95-
return touch_move(std::get<TouchParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
67+
result = touch_move(std::get<TouchParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
68+
break;
9669

9770
case Type::TouchUp:
98-
return touch_up(std::get<TouchUpParam>(pipeline_data.action_param), pipeline_data.name);
71+
result = touch_up(std::get<TouchUpParam>(pipeline_data.action_param), pipeline_data.name);
72+
break;
9973

10074
case Type::ClickKey:
101-
return click_key(std::get<ClickKeyParam>(pipeline_data.action_param), pipeline_data.name);
75+
result = click_key(std::get<ClickKeyParam>(pipeline_data.action_param), pipeline_data.name);
76+
break;
10277

10378
case Type::LongPressKey:
104-
return long_press_key(std::get<LongPressKeyParam>(pipeline_data.action_param), pipeline_data.name);
79+
result = long_press_key(std::get<LongPressKeyParam>(pipeline_data.action_param), pipeline_data.name);
80+
break;
10581

10682
case Type::KeyDown:
107-
return key_down(std::get<KeyParam>(pipeline_data.action_param), pipeline_data.name);
83+
result = key_down(std::get<KeyParam>(pipeline_data.action_param), pipeline_data.name);
84+
break;
10885

10986
case Type::KeyUp:
110-
return key_up(std::get<KeyParam>(pipeline_data.action_param), pipeline_data.name);
87+
result = key_up(std::get<KeyParam>(pipeline_data.action_param), pipeline_data.name);
88+
break;
11189

11290
case Type::InputText:
113-
return input_text(std::get<InputTextParam>(pipeline_data.action_param), pipeline_data.name);
91+
result = input_text(std::get<InputTextParam>(pipeline_data.action_param), pipeline_data.name);
92+
break;
11493

11594
case Type::StartApp:
116-
return start_app(std::get<AppParam>(pipeline_data.action_param), pipeline_data.name);
95+
result = start_app(std::get<AppParam>(pipeline_data.action_param), pipeline_data.name);
96+
break;
11797

11898
case Type::StopApp:
119-
return stop_app(std::get<AppParam>(pipeline_data.action_param), pipeline_data.name);
99+
result = stop_app(std::get<AppParam>(pipeline_data.action_param), pipeline_data.name);
100+
break;
120101

121102
case Type::Scroll:
122-
return scroll(std::get<ScrollParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
103+
result = scroll(std::get<ScrollParam>(pipeline_data.action_param), reco_hit, pipeline_data.name);
104+
break;
123105

124106
case Type::StopTask:
125-
return stop_task(pipeline_data.name);
107+
result = stop_task(pipeline_data.name);
108+
break;
126109

127110
case Type::Command:
128-
return command(std::get<CommandParam>(pipeline_data.action_param), reco_hit, pipeline_data.name, entry);
111+
result = command(std::get<CommandParam>(pipeline_data.action_param), reco_hit, pipeline_data.name, entry);
112+
break;
129113

130114
case Type::Shell:
131-
return shell(std::get<ShellParam>(pipeline_data.action_param), pipeline_data.name);
115+
result = shell(std::get<ShellParam>(pipeline_data.action_param), pipeline_data.name);
116+
break;
132117

133118
case Type::Screencap:
134-
return screencap(std::get<ScreencapParam>(pipeline_data.action_param), pipeline_data.name);
119+
result = screencap(std::get<ScreencapParam>(pipeline_data.action_param), pipeline_data.name);
120+
break;
135121

136122
case Type::Custom:
137-
return custom_action(std::get<CustomParam>(pipeline_data.action_param), reco_hit, reco_id, pipeline_data.name);
123+
result = custom_action(std::get<CustomParam>(pipeline_data.action_param), reco_hit, reco_id, pipeline_data.name);
124+
break;
138125

139126
default:
140127
LogError << "Unknown action" << VAR(static_cast<int>(pipeline_data.action_type));
141128
return { };
142129
}
130+
131+
tasker_->runtime_cache().set_action_detail(result.action_id, result);
132+
133+
return result;
143134
}
144135

145136
cv::Point Actuator::rand_point(const cv::Rect& r)
@@ -602,20 +593,6 @@ ActionResult Actuator::screencap(const MAA_RES_NS::Action::ScreencapParam& param
602593
};
603594
}
604595

605-
void Actuator::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box, const std::string& name)
606-
{
607-
if (param.time <= std::chrono::milliseconds(0)) {
608-
return;
609-
}
610-
611-
auto roi = helper_.get_target_rect(param.target, box);
612-
if (roi.empty()) {
613-
LogError << "failed to get target rect for wait_freezes" << VAR(name);
614-
return;
615-
}
616-
helper_.wait_freezes(param, roi, name);
617-
}
618-
619596
ActionResult Actuator::start_app(const MAA_RES_NS::Action::AppParam& param, const std::string& name)
620597
{
621598
if (!controller()) {
@@ -749,16 +726,4 @@ MAA_CTRL_NS::ControllerAgent* Actuator::controller()
749726
return tasker_ ? tasker_->controller() : nullptr;
750727
}
751728

752-
void Actuator::sleep(unsigned ms) const
753-
{
754-
sleep(std::chrono::milliseconds(ms));
755-
}
756-
757-
void Actuator::sleep(std::chrono::milliseconds ms) const
758-
{
759-
LogDebug << ms;
760-
761-
std::this_thread::sleep_for(ms);
762-
}
763-
764729
MAA_TASK_NS_END

source/MaaFramework/Task/Component/Actuator.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class Actuator : public NonCopyable
3434
inline static std::atomic<MaaActId> s_global_action_id = kActIdBase;
3535

3636
private:
37-
ActionResult execute_action(const cv::Rect& reco_hit, MaaRecoId reco_id, const PipelineData& pipeline_data, const std::string& entry);
38-
3937
ActionResult click(const MAA_RES_NS::Action::ClickParam& param, const cv::Rect& box, const std::string& name);
4038
ActionResult long_press(const MAA_RES_NS::Action::LongPressParam& param, const cv::Rect& box, const std::string& name);
4139
ActionResult swipe(const MAA_RES_NS::Action::SwipeParam& param, const cv::Rect& box, const std::string& name);
@@ -62,14 +60,9 @@ class Actuator : public NonCopyable
6260
ActionResult do_nothing(const std::string& name);
6361
ActionResult stop_task(const std::string& name);
6462

65-
void wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv::Rect& box, const std::string& name);
66-
6763
private:
6864
MAA_CTRL_NS::ControllerAgent* controller();
6965

70-
void sleep(unsigned ms) const;
71-
void sleep(std::chrono::milliseconds ms) const;
72-
7366
private:
7467
Tasker* tasker_ = nullptr;
7568
Context& context_;

source/MaaFramework/Task/Component/Recognizer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,12 @@ std::vector<cv::Rect> Recognizer::get_rois_from_pretask(const std::string& name,
603603
}
604604
NodeDetail node_detail = cache.get_node_detail(*node_id).value_or(NodeDetail { });
605605
RecoResult reco_result = cache.get_reco_result(node_detail.reco_id).value_or(RecoResult { });
606-
cv::Rect raw = reco_result.box.value_or(cv::Rect { });
607-
LogDebug << "pre task from cache" << VAR(name) << VAR(raw);
608-
return std::vector { raw };
606+
if (!reco_result.box) {
607+
LogWarn << "node has no recognition box" << VAR(name);
608+
return { };
609+
}
610+
LogDebug << "pre task from cache" << VAR(name) << VAR(*reco_result.box);
611+
return { *reco_result.box };
609612
}
610613

611614
void Recognizer::save_draws(const std::string& node_name, const RecoResult& result) const

source/MaaFramework/Task/Context.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,7 @@ bool Context::wait_freezes(std::chrono::milliseconds time, const cv::Rect& box,
187187
}
188188

189189
ActionHelper helper(this);
190-
auto roi = helper.get_target_rect(param.target, box);
191-
if (roi.empty()) {
192-
LogError << "failed to get target rect for wait_freezes";
193-
return false;
194-
}
195-
196-
return helper.wait_freezes(param, roi);
190+
return helper.wait_freezes(param, box);
197191
}
198192

199193
bool Context::override_pipeline(const json::value& pipeline_override)

0 commit comments

Comments
 (0)