From ff44bba8a4780088faed7accca9c8ff611b297ab Mon Sep 17 00:00:00 2001 From: Dongw Date: Wed, 11 Oct 2023 11:56:09 +0800 Subject: [PATCH] refactor: custom action --- source/cli/CMakeLists.txt | 12 +++- source/cli/{ => CustomAction}/Combat.cpp | 0 source/cli/{ => CustomAction}/Combat.h | 0 source/cli/CustomAction/CustomAction.cpp | 31 ++++++++ source/cli/CustomAction/CustomAction.h | 28 ++++++++ .../CustomAction/CustomActionRegistrar.cpp | 28 ++++++++ .../cli/CustomAction/CustomActionRegistrar.h | 32 +++++++++ .../cli/CustomAction/CustomActionRegistry.h | 35 +++++++++ source/cli/RegisterCustomAciton.h | 9 --- source/cli/RegisterCustomAction.cpp | 72 ------------------- source/cli/main.h | 2 +- 11 files changed, 166 insertions(+), 83 deletions(-) rename source/cli/{ => CustomAction}/Combat.cpp (100%) rename source/cli/{ => CustomAction}/Combat.h (100%) create mode 100644 source/cli/CustomAction/CustomAction.cpp create mode 100644 source/cli/CustomAction/CustomAction.h create mode 100644 source/cli/CustomAction/CustomActionRegistrar.cpp create mode 100644 source/cli/CustomAction/CustomActionRegistrar.h create mode 100644 source/cli/CustomAction/CustomActionRegistry.h delete mode 100644 source/cli/RegisterCustomAciton.h delete mode 100644 source/cli/RegisterCustomAction.cpp diff --git a/source/cli/CMakeLists.txt b/source/cli/CMakeLists.txt index a79c8e6..8f1f4af 100644 --- a/source/cli/CMakeLists.txt +++ b/source/cli/CMakeLists.txt @@ -8,7 +8,17 @@ endif () set(CMAKE_BUILD_TYPE "Release") set(CMAKE_CONFIGURATION_TYPES "Release") -add_executable(MAABH3_CLI main.cpp main.h Combat.h Combat.cpp RegisterCustomAciton.h RegisterCustomAction.cpp) +add_executable(MAABH3_CLI + main.cpp + main.h + CustomAction/Combat.h + CustomAction/Combat.cpp + CustomAction/CustomActionRegistrar.h + CustomAction/CustomActionRegistrar.cpp + CustomAction/CustomAction.h + CustomAction/CustomAction.cpp + CustomAction/CustomActionRegistry.h +) target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolKit) add_dependencies(MAABH3_CLI AssetsResource) diff --git a/source/cli/Combat.cpp b/source/cli/CustomAction/Combat.cpp similarity index 100% rename from source/cli/Combat.cpp rename to source/cli/CustomAction/Combat.cpp diff --git a/source/cli/Combat.h b/source/cli/CustomAction/Combat.h similarity index 100% rename from source/cli/Combat.h rename to source/cli/CustomAction/Combat.h diff --git a/source/cli/CustomAction/CustomAction.cpp b/source/cli/CustomAction/CustomAction.cpp new file mode 100644 index 0000000..bce04f2 --- /dev/null +++ b/source/cli/CustomAction/CustomAction.cpp @@ -0,0 +1,31 @@ +#include "CustomAction.h" + +CustomAction::CustomAction(const std::string& action_name, + MaaBool (*action_run)(MaaSyncContextHandle sync_context, MaaStringView task_name, + MaaStringView custom_action_param, MaaRectHandle cur_box, + MaaStringView cur_rec_detail, MaaTransparentArg arg)) + : name_(action_name) +{ + custom_action_.run = action_run; +} + +CustomAction::CustomAction(const std::string& action_name, + MaaBool (*action_run)(MaaSyncContextHandle sync_context, MaaStringView task_name, + MaaStringView custom_action_param, MaaRectHandle cur_box, + MaaStringView cur_rec_detail, MaaTransparentArg arg), + void (*action_stop)(MaaTransparentArg arg)) + : name_(action_name) +{ + custom_action_.run = action_run; + custom_action_.stop = action_stop; +} + +std::string CustomAction::get_name() const +{ + return name_; +} + +MaaCustomActionAPI CustomAction::get_custom_action() const +{ + return custom_action_; +} \ No newline at end of file diff --git a/source/cli/CustomAction/CustomAction.h b/source/cli/CustomAction/CustomAction.h new file mode 100644 index 0000000..6087d23 --- /dev/null +++ b/source/cli/CustomAction/CustomAction.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "MaaFramework/Task/MaaCustomAction.h" + +class CustomAction +{ +public: + CustomAction(const std::string& action_name, + MaaBool (*action_run)(MaaSyncContextHandle sync_context, MaaStringView task_name, + MaaStringView custom_action_param, MaaRectHandle cur_box, + MaaStringView cur_rec_detail, MaaTransparentArg arg)); + CustomAction(const std::string& action_name, + MaaBool (*action_run)(MaaSyncContextHandle sync_context, MaaStringView task_name, + MaaStringView custom_action_param, MaaRectHandle cur_box, + MaaStringView cur_rec_detail, MaaTransparentArg arg), + void (*action_stop)(MaaTransparentArg arg)); + ~CustomAction() = default; + +public: + std::string get_name() const; + MaaCustomActionAPI get_custom_action() const; + +private: + std::string name_; + MaaCustomActionAPI custom_action_; +}; \ No newline at end of file diff --git a/source/cli/CustomAction/CustomActionRegistrar.cpp b/source/cli/CustomAction/CustomActionRegistrar.cpp new file mode 100644 index 0000000..c883308 --- /dev/null +++ b/source/cli/CustomAction/CustomActionRegistrar.cpp @@ -0,0 +1,28 @@ +#include "CustomActionRegistrar.h" + +void CustomActionRegistrar::add_action(const std::string& name, + MaaBool (*action_run)(MaaSyncContextHandle sync_context, MaaStringView task_name, + MaaStringView custom_action_param, MaaRectHandle cur_box, + MaaStringView cur_rec_detail, MaaTransparentArg arg)) +{ + actions_.emplace_back(name, action_run); +} + +void CustomActionRegistrar::add_action(const std::string& name, + MaaBool (*action_run)(MaaSyncContextHandle sync_context, MaaStringView task_name, + MaaStringView custom_action_param, MaaRectHandle cur_box, + MaaStringView cur_rec_detail, MaaTransparentArg arg), + void (*action_stop)(MaaTransparentArg arg)) +{ + actions_.emplace_back(name, action_run, action_stop); +} + +void CustomActionRegistrar::register_actions(MaaInstanceHandle maa_handle) +{ + for (const auto& action : actions_) { + std::shared_ptr custom_action = + std::make_shared(action.get_custom_action()); + custom_actions_.push_back(custom_action); + MaaRegisterCustomAction(maa_handle, action.get_name().c_str(), custom_action.get(), nullptr); + } +} \ No newline at end of file diff --git a/source/cli/CustomAction/CustomActionRegistrar.h b/source/cli/CustomAction/CustomActionRegistrar.h new file mode 100644 index 0000000..31a8a17 --- /dev/null +++ b/source/cli/CustomAction/CustomActionRegistrar.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +#include "CustomAction.h" +#include "MaaFramework/Instance/MaaInstance.h" +#include "MaaFramework/Task/MaaCustomAction.h" + +class CustomActionRegistrar +{ +public: + CustomActionRegistrar() = default; + ~CustomActionRegistrar() = default; + +public: + void add_action(const std::string& name, + MaaBool (*action_run)(MaaSyncContextHandle sync_context, MaaStringView task_name, + MaaStringView custom_action_param, MaaRectHandle cur_box, + MaaStringView cur_rec_detail, MaaTransparentArg arg)); + void add_action(const std::string& name, + MaaBool (*action_run)(MaaSyncContextHandle sync_context, MaaStringView task_name, + MaaStringView custom_action_param, MaaRectHandle cur_box, + MaaStringView cur_rec_detail, MaaTransparentArg arg), + void (*action_stop)(MaaTransparentArg arg)); + void register_actions(MaaInstanceHandle maa_handle); + +private: + std::vector actions_; + std::vector> custom_actions_; +}; \ No newline at end of file diff --git a/source/cli/CustomAction/CustomActionRegistry.h b/source/cli/CustomAction/CustomActionRegistry.h new file mode 100644 index 0000000..532fc61 --- /dev/null +++ b/source/cli/CustomAction/CustomActionRegistry.h @@ -0,0 +1,35 @@ +#pragma once + +#include "Combat.h" +#include "CustomAction/CustomActionRegistrar.h" + +void custom_action_init(CustomActionRegistrar& registrar) +{ + registrar.add_action("Forward", combat_forward); + registrar.add_action("Backward", combat_backward); + registrar.add_action("Left", combat_left); + registrar.add_action("Right", combat_right); + registrar.add_action("LeftForward", combat_left_forward); + registrar.add_action("RightForward", combat_right_forward); + registrar.add_action("LeftBackward", combat_left_backward); + registrar.add_action("RightBackward", combat_right_backward); + registrar.add_action("BasicATK", combat_basic_ATK); + registrar.add_action("BasicATKChargeBase", combat_basic_ATK_charge_base); + registrar.add_action("BasicATKCharge", combat_basic_ATK_charge); + registrar.add_action("Evade", combat_evade); + registrar.add_action("Ultimate", combat_ultimate); + registrar.add_action("UltimateChargeBase", combat_ultimate_charge_base); + registrar.add_action("UltimateCharge", combat_ultimate_charge); + registrar.add_action("WeaponSkill", combat_weapon_skill); + registrar.add_action("ELFSkill", combat_ELF_skill); + registrar.add_action("ExtraSkill", combat_extra_skill); + registrar.add_action("QTE1", combat_QTE1); + registrar.add_action("QTE2", combat_QTE2); +} + +void register_custom_action(MaaInstanceHandle maa_handle) +{ + CustomActionRegistrar registerar; + custom_action_init(registerar); + registerar.register_actions(maa_handle); +} \ No newline at end of file diff --git a/source/cli/RegisterCustomAciton.h b/source/cli/RegisterCustomAciton.h deleted file mode 100644 index cdaf348..0000000 --- a/source/cli/RegisterCustomAciton.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include "MaaFramework/Instance/MaaInstance.h" -#include "MaaFramework/Task/MaaCustomAction.h" -#include "Combat.h" - -void custom_action_init(); - -void register_custom_action(MaaInstanceHandle maa_handle); \ No newline at end of file diff --git a/source/cli/RegisterCustomAction.cpp b/source/cli/RegisterCustomAction.cpp deleted file mode 100644 index bc0d934..0000000 --- a/source/cli/RegisterCustomAction.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "RegisterCustomAciton.h" - -MaaCustomActionAPI action_combat_forward; -MaaCustomActionAPI action_combat_backward; -MaaCustomActionAPI action_combat_left; -MaaCustomActionAPI action_combat_right; -MaaCustomActionAPI action_combat_left_forward; -MaaCustomActionAPI action_combat_right_forward; -MaaCustomActionAPI action_combat_left_backward; -MaaCustomActionAPI action_combat_right_backward; -MaaCustomActionAPI action_combat_basic_ATK; -MaaCustomActionAPI action_combat_basic_ATK_charge_base; -MaaCustomActionAPI action_combat_basic_ATK_charge; -MaaCustomActionAPI action_combat_evade; -MaaCustomActionAPI action_combat_ultimate; -MaaCustomActionAPI action_combat_ultimate_charge_base; -MaaCustomActionAPI action_combat_ultimate_charge; -MaaCustomActionAPI action_combat_weapon_skill; -MaaCustomActionAPI action_combat_ELF_skill; -MaaCustomActionAPI action_combat_extra_skill; -MaaCustomActionAPI action_combat_QTE1; -MaaCustomActionAPI action_combat_QTE2; - -void custom_action_init() -{ - action_combat_forward.run = combat_forward; - action_combat_backward.run = combat_backward; - action_combat_left.run = combat_left; - action_combat_right.run = combat_right; - action_combat_left_forward.run = combat_left_forward; - action_combat_right_forward.run = combat_right_forward; - action_combat_left_backward.run = combat_left_backward; - action_combat_right_backward.run = combat_right_backward; - action_combat_basic_ATK.run = combat_basic_ATK; - action_combat_basic_ATK_charge_base.run = combat_basic_ATK_charge_base; - action_combat_basic_ATK_charge.run = combat_basic_ATK_charge; - action_combat_evade.run = combat_evade; - action_combat_ultimate.run = combat_ultimate; - action_combat_ultimate_charge_base.run = combat_ultimate_charge_base; - action_combat_ultimate_charge.run = combat_ultimate_charge; - action_combat_weapon_skill.run = combat_weapon_skill; - action_combat_ELF_skill.run = combat_ELF_skill; - action_combat_extra_skill.run = combat_extra_skill; - action_combat_QTE1.run = combat_QTE1; - action_combat_QTE2.run = combat_QTE2; -} - -void register_custom_action(MaaInstanceHandle maa_handle) -{ - custom_action_init(); - - MaaRegisterCustomAction(maa_handle, "Forward", &action_combat_forward, nullptr); - MaaRegisterCustomAction(maa_handle, "Backward", &action_combat_backward, nullptr); - MaaRegisterCustomAction(maa_handle, "Left", &action_combat_left, nullptr); - MaaRegisterCustomAction(maa_handle, "Right", &action_combat_right, nullptr); - MaaRegisterCustomAction(maa_handle, "LeftForward", &action_combat_left_forward, nullptr); - MaaRegisterCustomAction(maa_handle, "RightForward", &action_combat_right_forward, nullptr); - MaaRegisterCustomAction(maa_handle, "LeftBackward", &action_combat_left_backward, nullptr); - MaaRegisterCustomAction(maa_handle, "RightBackward", &action_combat_right_backward, nullptr); - MaaRegisterCustomAction(maa_handle, "BasicATK", &action_combat_basic_ATK, nullptr); - MaaRegisterCustomAction(maa_handle, "BasicATKChargeBase", &action_combat_basic_ATK_charge_base, nullptr); - MaaRegisterCustomAction(maa_handle, "BasicATKCharge", &action_combat_basic_ATK_charge, nullptr); - MaaRegisterCustomAction(maa_handle, "Evade", &action_combat_evade, nullptr); - MaaRegisterCustomAction(maa_handle, "Ultimate", &action_combat_ultimate, nullptr); - MaaRegisterCustomAction(maa_handle, "UltimateChargeBase", &action_combat_ultimate_charge_base, nullptr); - MaaRegisterCustomAction(maa_handle, "UltimateCharge", &action_combat_ultimate_charge, nullptr); - MaaRegisterCustomAction(maa_handle, "WeaponSkill", &action_combat_weapon_skill, nullptr); - MaaRegisterCustomAction(maa_handle, "ELFSkill", &action_combat_ELF_skill, nullptr); - MaaRegisterCustomAction(maa_handle, "ExtraSkill", &action_combat_extra_skill, nullptr); - MaaRegisterCustomAction(maa_handle, "QTE1", &action_combat_QTE1, nullptr); - MaaRegisterCustomAction(maa_handle, "QTE2", &action_combat_QTE2, nullptr); -} \ No newline at end of file diff --git a/source/cli/main.h b/source/cli/main.h index 3a200ff..7bf0a22 100644 --- a/source/cli/main.h +++ b/source/cli/main.h @@ -9,7 +9,7 @@ #include "MaaToolKit/MaaToolKitAPI.h" #include "meojson/json.hpp" -#include "RegisterCustomAciton.h" +#include "CustomAction/CustomActionRegistry.h" struct Task {