Skip to content

Commit

Permalink
refactor: done
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Mar 29, 2024
1 parent 3c0721f commit 7fbafa8
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 154 deletions.
8 changes: 4 additions & 4 deletions source/MaaFramework/Instance/InstanceInternalAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <meojson/json.hpp>

#include "Conf/Conf.h"
#include "MaaFramework/Task/MaaCustomAction.h"
#include "MaaFramework/Task/MaaCustomRecognizer.h"
#include "Utils/NonCopyable.hpp"

MAA_RES_NS_BEGIN
Expand All @@ -24,14 +26,12 @@ struct CustomRecognizerSession
{
MaaCustomRecognizerHandle recognizer = nullptr;
MaaTransparentArg recognizer_arg = nullptr;
InstanceInternalAPI* inst = nullptr;
};

struct CustomActionSession
{
MaaCustomActionHandle action = nullptr;
MaaTransparentArg action_arg = nullptr;
InstanceInternalAPI* inst = nullptr;
};

struct InstanceInternalAPI : public NonCopyable
Expand All @@ -41,8 +41,8 @@ struct InstanceInternalAPI : public NonCopyable
virtual MAA_CTRL_NS::ControllerAgent* inter_controller() = 0;
virtual InstanceStatus* inter_status() = 0;
virtual void notify(std::string_view msg, const json::value& details = json::value()) = 0;
virtual CustomRecognizerSession custom_recognizer(const std::string& name) = 0;
virtual CustomActionSession custom_action(const std::string& name) = 0;
virtual CustomRecognizerSession* custom_recognizer_session(const std::string& name) = 0;
virtual CustomActionSession* custom_action_session(const std::string& name) = 0;
};

MAA_NS_END
33 changes: 16 additions & 17 deletions source/MaaFramework/Instance/InstanceMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,20 @@ bool InstanceMgr::register_custom_recognizer(
return false;
}

auto recognizer_ptr =
std::make_shared<MAA_VISION_NS::CustomRecognizer>(handle, handle_arg, this);
return custom_recognizers_.insert_or_assign(std::move(name), std::move(recognizer_ptr)).second;
CustomRecognizerSession session { handle, handle_arg };
return custom_recognizer_sessions_.insert_or_assign(std::move(name), std::move(session)).second;
}

bool InstanceMgr::unregister_custom_recognizer(std::string name)
{
LogInfo << VAR(name);
return custom_recognizers_.erase(name) > 0;
return custom_recognizer_sessions_.erase(name) > 0;
}

void InstanceMgr::clear_custom_recognizer()
{
LogInfo;
custom_recognizers_.clear();
custom_recognizer_sessions_.clear();
}

bool InstanceMgr::register_custom_action(
Expand All @@ -167,20 +166,20 @@ bool InstanceMgr::register_custom_action(
LogError << "Invalid handle";
return false;
}
auto action_ptr = std::make_shared<MAA_TASK_NS::CustomAction>(handle, handle_arg, this);
return custom_actions_.insert_or_assign(std::move(name), std::move(action_ptr)).second;
CustomActionSession session { handle, handle_arg };
return custom_action_sessions_.insert_or_assign(std::move(name), std::move(session)).second;
}

bool InstanceMgr::unregister_custom_action(std::string name)
{
LogInfo << VAR(name);
return custom_actions_.erase(name) > 0;
return custom_action_sessions_.erase(name) > 0;
}

void InstanceMgr::clear_custom_action()
{
LogInfo;
custom_actions_.clear();
custom_action_sessions_.clear();
}

MaaStatus InstanceMgr::task_status(MaaTaskId task_id) const
Expand Down Expand Up @@ -263,24 +262,24 @@ void InstanceMgr::notify(std::string_view msg, const json::value& details)
notifier.notify(msg, details);
}

MAA_VISION_NS::CustomRecognizerPtr InstanceMgr::custom_recognizer(const std::string& name)
CustomRecognizerSession* InstanceMgr::custom_recognizer_session(const std::string& name)
{
auto it = custom_recognizers_.find(name);
if (it == custom_recognizers_.end()) {
auto it = custom_recognizer_sessions_.find(name);
if (it == custom_recognizer_sessions_.end()) {
LogError << "Custom recognizer not found:" << name;
return nullptr;
}
return it->second;
return &it->second;
}

MAA_TASK_NS::CustomActionPtr InstanceMgr::custom_action(const std::string& name)
CustomActionSession* InstanceMgr::custom_action_session(const std::string& name)
{
auto it = custom_actions_.find(name);
if (it == custom_actions_.end()) {
auto it = custom_action_sessions_.find(name);
if (it == custom_action_sessions_.end()) {
LogError << "Custom action not found:" << name;
return nullptr;
}
return it->second;
return &it->second;
}

bool InstanceMgr::run_task(TaskId id, TaskPtr task_ptr)
Expand Down
8 changes: 4 additions & 4 deletions source/MaaFramework/Instance/InstanceMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class InstanceMgr
virtual MAA_CTRL_NS::ControllerAgent* inter_controller() override;
virtual InstanceStatus* inter_status() override;
virtual void notify(std::string_view msg, const json::value& details = json::value()) override;
virtual MAA_VISION_NS::CustomRecognizerPtr custom_recognizer(const std::string& name) override;
virtual MAA_TASK_NS::CustomActionPtr custom_action(const std::string& name) override;
virtual CustomRecognizerSession* custom_recognizer_session(const std::string& name) override;
virtual CustomActionSession* custom_action_session(const std::string& name) override;

private:
using TaskPtr = std::shared_ptr<TaskNS::PipelineTask>;
Expand All @@ -73,8 +73,8 @@ class InstanceMgr
InstanceStatus status_;
bool need_to_stop_ = false;

std::unordered_map<std::string, MAA_VISION_NS::CustomRecognizerPtr> custom_recognizers_;
std::unordered_map<std::string, MAA_TASK_NS::CustomActionPtr> custom_actions_;
std::unordered_map<std::string, CustomRecognizerSession> custom_recognizer_sessions_;
std::unordered_map<std::string, CustomActionSession> custom_action_sessions_;

std::unique_ptr<AsyncRunner<TaskPtr>> task_runner_ = nullptr;
MessageNotifier<MaaInstanceCallback> notifier;
Expand Down
20 changes: 10 additions & 10 deletions source/MaaFramework/Task/Actuator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,13 @@ void Actuator::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv:
LogFunc << "Wait freezes:" << VAR(param.time) << VAR(param.threshold) << VAR(param.method);

cv::Rect target = get_target_rect(param.target, cur_box);
cv::Mat pre_image = controller()->screencap();

TemplateComparator comp;
comp.set_param({
TemplateComparatorParam comp_param {
.roi = { target },
.threshold = param.threshold,
.method = param.method,
});

cv::Mat pre_image = controller()->screencap();
};

auto pre_time = std::chrono::steady_clock::now();

Expand All @@ -148,7 +146,9 @@ void Actuator::wait_freezes(const MAA_RES_NS::WaitFreezesParam& param, const cv:
break;
}

auto ret = comp.analyze(pre_image, cur_image);
TemplateComparator comparator(pre_image, cur_image, comp_param);

auto ret = comparator.filtered_results();
if (ret.empty()) {
pre_image = cur_image;
pre_time = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -199,13 +199,13 @@ bool Actuator::custom_action(
LogError << "Inst is null";
return false;
}
auto action = inst_->custom_action(param.name);
if (!action) {
auto* session = inst_->custom_action_session(param.name);
if (!session) {
LogError << "Custom task not found" << VAR(param.name);
return false;
}

return action->run(task_name, param, cur_box, cur_rec_detail);
return CustomAction(*session, inst_).run(task_name, param, cur_box, cur_rec_detail);
}

cv::Rect Actuator::get_target_rect(const MAA_RES_NS::Action::Target target, const cv::Rect& cur_box)
Expand Down Expand Up @@ -264,4 +264,4 @@ void Actuator::sleep(std::chrono::milliseconds ms) const
LogTrace << "end of sleep" << ms << VAR(interval);
}

MAA_TASK_NS_END
MAA_TASK_NS_END
24 changes: 10 additions & 14 deletions source/MaaFramework/Task/CustomAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

MAA_TASK_NS_BEGIN

CustomAction::CustomAction(
MaaCustomActionHandle handle,
MaaTransparentArg handle_arg_,
InstanceInternalAPI* inst)
: action_(handle)
, action_arg_(handle_arg_)
, inst_(inst)
CustomAction::CustomAction(CustomActionSession session, InstanceInternalAPI* inst)
: session_(std::move(session)), inst_(inst)
{
}

Expand All @@ -24,10 +19,11 @@ bool CustomAction::run(
const cv::Rect& cur_box,
const json::value& cur_rec_detail)
{
LogFunc << VAR(task_name) << VAR_VOIDP(action_) << VAR(param.custom_param) << VAR(cur_box);
LogFunc << VAR(task_name) << VAR_VOIDP(session_.action) << VAR(param.custom_param)
<< VAR(cur_box);

if (!action_ || !action_->run) {
LogError << "Action is null" << VAR_VOIDP(action_) << VAR_VOIDP(action_->run);
if (!session_.action || !session_.action->run) {
LogError << "Action is null" << VAR(task_name);
return false;
}

Expand All @@ -39,16 +35,16 @@ bool CustomAction::run(
.height = cur_box.height };
std::string cur_rec_detail_string = cur_rec_detail.to_string();

bool ret = action_->run(
bool ret = session_.action->run(
&sync_ctx,
task_name.c_str(),
custom_param_string.c_str(),
&box,
cur_rec_detail_string.c_str(),
action_arg_);
LogTrace << VAR_VOIDP(action_) << VAR_VOIDP(action_->run) << VAR(ret);
session_.action_arg);
LogTrace << VAR_VOIDP(session_.action) << VAR_VOIDP(session_.action->run) << VAR(ret);

return ret;
}

MAA_TASK_NS_END
MAA_TASK_NS_END
8 changes: 2 additions & 6 deletions source/MaaFramework/Task/CustomAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ MAA_TASK_NS_BEGIN
class CustomAction
{
public:
CustomAction(
MaaCustomActionHandle handle,
MaaTransparentArg handle_arg,
InstanceInternalAPI* inst);
CustomAction(CustomActionSession session, InstanceInternalAPI* inst);

bool
run(const std::string& task_name,
Expand All @@ -23,8 +20,7 @@ class CustomAction
const json::value& cur_rec_detail);

private:
MaaCustomActionHandle action_ = nullptr;
MaaTransparentArg action_arg_ = nullptr;
CustomActionSession session_;
InstanceInternalAPI* inst_ = nullptr;
};

Expand Down
Loading

0 comments on commit 7fbafa8

Please sign in to comment.