Skip to content

Commit

Permalink
refactor: custom action
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Oct 11, 2023
1 parent f3ed5e2 commit ff44bba
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 83 deletions.
12 changes: 11 additions & 1 deletion source/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions source/cli/CustomAction/CustomAction.cpp
Original file line number Diff line number Diff line change
@@ -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_;
}
28 changes: 28 additions & 0 deletions source/cli/CustomAction/CustomAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <string>

#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_;
};
28 changes: 28 additions & 0 deletions source/cli/CustomAction/CustomActionRegistrar.cpp
Original file line number Diff line number Diff line change
@@ -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<MaaCustomActionAPI> custom_action =
std::make_shared<MaaCustomActionAPI>(action.get_custom_action());
custom_actions_.push_back(custom_action);
MaaRegisterCustomAction(maa_handle, action.get_name().c_str(), custom_action.get(), nullptr);
}
}
32 changes: 32 additions & 0 deletions source/cli/CustomAction/CustomActionRegistrar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <memory>
#include <string>
#include <vector>

#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<CustomAction> actions_;
std::vector<std::shared_ptr<MaaCustomActionAPI>> custom_actions_;
};
35 changes: 35 additions & 0 deletions source/cli/CustomAction/CustomActionRegistry.h
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 0 additions & 9 deletions source/cli/RegisterCustomAciton.h

This file was deleted.

72 changes: 0 additions & 72 deletions source/cli/RegisterCustomAction.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion source/cli/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "MaaToolKit/MaaToolKitAPI.h"
#include "meojson/json.hpp"

#include "RegisterCustomAciton.h"
#include "CustomAction/CustomActionRegistry.h"

struct Task
{
Expand Down

0 comments on commit ff44bba

Please sign in to comment.