Skip to content

Commit

Permalink
feat: pi_cli 接口新增 inst 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Sep 13, 2024
1 parent d318fa1 commit 2a7bc91
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ extern "C"
#endif

MAA_TOOLKIT_API void
MaaToolkitProjectInterfaceRegisterCustomRecognition(const char* name, MaaCustomRecognizerCallback recognizer, void* trans_arg);
MAA_TOOLKIT_API void MaaToolkitProjectInterfaceRegisterCustomAction(const char* name, MaaCustomActionCallback action, void* trans_arg);
MaaToolkitProjectInterfaceRegisterCustomRecognition(uint64_t inst_id, const char* name, MaaCustomRecognizerCallback recognizer, void* trans_arg);

This comment has been minimized.

Copy link
@neko-para

neko-para Sep 13, 2024

Contributor

感觉这样改没啥意义啊
本身cli特性决定了它同时只能开一个, 那id的作用仅仅是为了方便, 比如在两种custom preset中切换.
但是这本身好像还不如直接加一个clear custom, 每次run之前重新注册来的方便

This comment has been minimized.

Copy link
@MistEO

MistEO Sep 13, 2024

Author Member

cli 是一个,server 就不一定是一个了(

MAA_TOOLKIT_API void MaaToolkitProjectInterfaceRegisterCustomAction(uint64_t inst_id, const char* name, MaaCustomActionCallback action, void* trans_arg);

MAA_TOOLKIT_API MaaBool MaaToolkitProjectInterfaceRunCli(
uint64_t inst_id,
const char* resource_path,
const char* user_path,
MaaBool directly,
Expand Down
2 changes: 1 addition & 1 deletion source/MaaProjectInterface/CLI/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ int main(int argc, char** argv)
break;
}

MaaBool ret = MaaToolkitProjectInterfaceRunCli(resource_path.c_str(), user_path.c_str(), directly, nullptr, nullptr);
MaaBool ret = MaaToolkitProjectInterfaceRunCli(0, resource_path.c_str(), user_path.c_str(), directly, nullptr, nullptr);
return ret ? 0 : -1;
}
14 changes: 10 additions & 4 deletions source/MaaToolkit/API/MaaToolkitProjectInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@
#include "Utils/Logger.h"
#include "Utils/Platform.h"

void MaaToolkitProjectInterfaceRegisterCustomRecognition(const char* name, MaaCustomRecognizerCallback recognizer, void* trans_arg)
void MaaToolkitProjectInterfaceRegisterCustomRecognition(
uint64_t inst_id,
const char* name,
MaaCustomRecognizerCallback recognizer,
void* trans_arg)
{
MAA_TOOLKIT_NS::ProjectInterfaceMgr::get_instance().register_custom_recognizer(
inst_id,
name,
{ .recoginzer = recognizer, .trans_arg = trans_arg });
}

void MaaToolkitProjectInterfaceRegisterCustomAction(const char* name, MaaCustomActionCallback action, void* trans_arg)
void MaaToolkitProjectInterfaceRegisterCustomAction(uint64_t inst_id, const char* name, MaaCustomActionCallback action, void* trans_arg)
{
MAA_TOOLKIT_NS::ProjectInterfaceMgr::get_instance().register_custom_action(name, { .action = action, .trans_arg = trans_arg });
MAA_TOOLKIT_NS::ProjectInterfaceMgr::get_instance().register_custom_action(inst_id, name, { .action = action, .trans_arg = trans_arg });
}

MaaBool MaaToolkitProjectInterfaceRunCli(
uint64_t inst_id,
const char* resource_path,
const char* user_path,
MaaBool directly,
MaaNotificationCallback callback,
void* callback_arg)
{
return MAA_TOOLKIT_NS::ProjectInterfaceMgr::get_instance()
.run_cli(MAA_NS::path(resource_path), MAA_NS::path(user_path), directly, callback, callback_arg);
.run_cli(inst_id, MAA_NS::path(resource_path), MAA_NS::path(user_path), directly, callback, callback_arg);
}
17 changes: 12 additions & 5 deletions source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@

MAA_TOOLKIT_NS_BEGIN

void ProjectInterfaceMgr::register_custom_recognizer(const std::string& name, MAA_PROJECT_INTERFACE_NS::CustomRecognizerSession recognizer)
void ProjectInterfaceMgr::register_custom_recognizer(
uint64_t inst_id,
const std::string& name,
MAA_PROJECT_INTERFACE_NS::CustomRecognizerSession recognizer)
{
custom_recognizers_.insert_or_assign(name, recognizer);
custom_recognizers_[inst_id].insert_or_assign(name, recognizer);
}

void ProjectInterfaceMgr::register_custom_action(const std::string& name, MAA_PROJECT_INTERFACE_NS::CustomActionSession action)
void ProjectInterfaceMgr::register_custom_action(
uint64_t inst_id,
const std::string& name,
MAA_PROJECT_INTERFACE_NS::CustomActionSession action)
{
custom_actions_.insert_or_assign(name, action);
custom_actions_[inst_id].insert_or_assign(name, action);
}

bool ProjectInterfaceMgr::run_cli(
uint64_t inst_id,
const std::filesystem::path& resource_path,
const std::filesystem::path& user_path,
bool directly,
Expand All @@ -29,7 +36,7 @@ bool ProjectInterfaceMgr::run_cli(

Interactor interactor;

if (!interactor.load(resource_path, callback, callback_arg, custom_recognizers_, custom_actions_)) {
if (!interactor.load(resource_path, callback, callback_arg, custom_recognizers_[inst_id], custom_actions_[inst_id])) {
return false;
}
if (directly) {
Expand Down
10 changes: 6 additions & 4 deletions source/MaaToolkit/ProjectInterface/ProjectInterfaceMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ class ProjectInterfaceMgr : public SingletonHolder<ProjectInterfaceMgr>
friend class SingletonHolder<ProjectInterfaceMgr>;

public:
void register_custom_recognizer(const std::string& name, MAA_PROJECT_INTERFACE_NS::CustomRecognizerSession recognizer);
void register_custom_action(const std::string& name, MAA_PROJECT_INTERFACE_NS::CustomActionSession action);
void
register_custom_recognizer(uint64_t inst_id, const std::string& name, MAA_PROJECT_INTERFACE_NS::CustomRecognizerSession recognizer);
void register_custom_action(uint64_t inst_id, const std::string& name, MAA_PROJECT_INTERFACE_NS::CustomActionSession action);

bool run_cli(
uint64_t inst_id,
const std::filesystem::path& resource_path,
const std::filesystem::path& user_path,
bool directly,
MaaNotificationCallback callback,
void* callback_arg);

private:
std::map<std::string, MAA_PROJECT_INTERFACE_NS::CustomRecognizerSession> custom_recognizers_;
std::map<std::string, MAA_PROJECT_INTERFACE_NS::CustomActionSession> custom_actions_;
std::map<uint64_t, std::map<std::string, MAA_PROJECT_INTERFACE_NS::CustomRecognizerSession>> custom_recognizers_;
std::map<uint64_t, std::map<std::string, MAA_PROJECT_INTERFACE_NS::CustomActionSession>> custom_actions_;
};

MAA_TOOLKIT_NS_END
25 changes: 20 additions & 5 deletions source/binding/Python/maa/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,40 +124,50 @@ def find_desktop_windows(cls) -> List[DesktopWindow]:

@classmethod
def register_pi_custom_recognition(
cls, name: str, recognizer: "CustomRecognizer"
cls, name: str, recognizer: "CustomRecognizer", inst_id: int = 0
) -> None:
cls._set_api_properties()

if not cls._custom_recognizer_holder:
cls._custom_recognizer_holder = {}

if not cls._custom_recognizer_holder[inst_id]:
cls._custom_recognizer_holder[inst_id] = {}

# avoid gc
cls._custom_recognizer_holder[name] = recognizer
cls._custom_recognizer_holder[inst_id][name] = recognizer

return bool(
Library.framework.MaaToolkitProjectInterfaceRegisterCustomRecognition(
ctypes.c_uint64(inst_id),
name.encode("utf-8"),
recognizer.c_handle,
recognizer.c_arg,
)
)

@classmethod
def register_pi_custom_action(cls, name: str, action: "CustomAction") -> None:
def register_pi_custom_action(
cls, name: str, action: "CustomAction", inst_id: int = 0
) -> None:
cls._set_api_properties()

if not cls._custom_action_holder:
cls._custom_action_holder = {}

if not cls._custom_action_holder[inst_id]:
cls._custom_action_holder[inst_id] = {}

# avoid gc
cls._custom_action_holder[name] = action
cls._custom_recognizer_holder[inst_id][name] = action

return bool(
ctypes.c_uint64(inst_id),
Library.framework.MaaToolkitProjectInterfaceRegisterCustomAction(
name.encode("utf-8"),
action.c_handle,
action.c_arg,
)
),
)

@classmethod
Expand All @@ -168,13 +178,15 @@ def run_pi_cli(
directly: bool = False,
callback: Optional[Callback] = None,
callback_arg: Any = None,
inst_id: int = 0,
) -> bool:
cls._set_api_properties()

cls._callback_agent = CallbackAgent(callback, callback_arg)

return bool(
Library.toolkit.MaaToolkitRunCli(
ctypes.c_uint64(inst_id),
str(resource_path).encode("utf-8"),
str(user_path).encode("utf-8"),
directly,
Expand Down Expand Up @@ -315,20 +327,23 @@ def _set_api_properties():
None
)
Library.toolkit.MaaToolkitProjectInterfaceRegisterCustomRecognition.argtypes = [
ctypes.c_uint64,
ctypes.c_char_p,
MaaCustomRecognizerCallback,
ctypes.c_void_p,
]

Library.toolkit.MaaToolkitProjectInterfaceRegisterCustomAction.restype = None
Library.toolkit.MaaToolkitProjectInterfaceRegisterCustomAction.argtypes = [
ctypes.c_uint64,
ctypes.c_char_p,
MaaCustomActionCallback,
ctypes.c_void_p,
]

Library.toolkit.MaaToolkitProjectInterfaceRunCli.restype = MaaBool
Library.toolkit.MaaToolkitProjectInterfaceRunCli.argtypes = [
ctypes.c_uint64,
ctypes.c_char_p,
ctypes.c_char_p,
MaaBool,
Expand Down

0 comments on commit 2a7bc91

Please sign in to comment.