Skip to content

Commit

Permalink
feat: init MaaAgentClient
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Feb 15, 2025
1 parent 11b20b2 commit b184c35
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 19 deletions.
21 changes: 21 additions & 0 deletions include/MaaAgentClient/MaaAgentClientAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "MaaAgentClientDef.h"

#ifdef __cplusplus
extern "C"
{
#endif

MAA_AGENT_CLIENT_API MaaAgentClient* MaaAgentClientCreate(MaaNotificationCallback notify, void* notify_trans_arg);

MAA_AGENT_CLIENT_API void MaaAgentClientDestroy(MaaAgentClient* client);

MAA_AGENT_CLIENT_API MaaBool
MaaAgentClientStartChild(MaaAgentClient* client, const char* child_exec, const MaaStringListBuffer* child_args);

MAA_AGENT_CLIENT_API MaaBool MaaAgentClientBindResource(MaaAgentClient* client, MaaResource* res);

#ifdef __cplusplus
}
#endif
5 changes: 5 additions & 0 deletions include/MaaAgentClient/MaaAgentClientDef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "MaaFramework/MaaDef.h" // IWYU pragma: export

typedef struct MaaAgentClient MaaAgentClient;
6 changes: 6 additions & 0 deletions include/MaaFramework/MaaPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@
#else
#define MAA_TOOLKIT_API MAA_DLL_IMPORT
#endif

#ifdef MAA_AGENT_CLIENT_EXPORTS
#define MAA_AGENT_CLIENT_API MAA_DLL_EXPORT
#else
#define MAA_AGENT_CLIENT_API MAA_DLL_IMPORT
#endif
1 change: 1 addition & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ add_subdirectory(LibraryHolder)
add_subdirectory(MaaFramework)
add_subdirectory(MaaToolkit)
add_subdirectory(MaaProjectInterface)
add_subdirectory(MaaAgentClient)

add_subdirectory(binding)
58 changes: 58 additions & 0 deletions source/MaaAgentClient/API/MaaAgentClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "MaaAgentClient/MaaAgentClientAPI.h"

#include "Client/AgentClient.h"
#include "Utils/Buffer/StringBuffer.hpp"
#include "Utils/Logger.h"
#include "Utils/Platform.h"

MaaAgentClient* MaaAgentClientCreate(MaaNotificationCallback notify, void* notify_trans_arg)
{
LogFunc << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);

return new MAA_AGENT_CLIENT_NS::AgentClient(notify, notify_trans_arg);
}

void MaaAgentClientDestroy(MaaAgentClient* client)
{
LogFunc << VAR_VOIDP(client);

if (client == nullptr) {
LogError << "handle is null";
return;
}

delete client;
}

MaaBool MaaAgentClientStartChild(MaaAgentClient* client, const char* child_exec, const MaaStringListBuffer* child_args)
{
LogFunc << VAR_VOIDP(client) << VAR(child_exec) << VAR(child_args);

if (!client) {
LogError << "handle is null";
return false;
}

std::vector<std::string> args;

if (child_args) {
size_t size = child_args->size();
for (size_t i = 0; i < size; ++i) {
args.emplace_back(child_args->at(i).get());
}
}

return client->start_clild(MAA_NS::path(child_exec), args);
}

MaaBool MaaAgentClientBindResource(MaaAgentClient* client, MaaResource* res)
{
LogFunc << VAR_VOIDP(client) << VAR_VOIDP(res);

if (!client || !res) {
LogError << "handle is null";
return false;
}

return client->bind_resource(res);
}
12 changes: 12 additions & 0 deletions source/MaaAgentClient/API/MaaAgentClientTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "MaaAgentClient/MaaAgentClientDef.h"

struct MaaAgentClient
{
public:
virtual ~MaaAgentClient() = default;

virtual bool start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args) = 0;
virtual bool bind_resource(MaaResource* resource) = 0;
};
29 changes: 29 additions & 0 deletions source/MaaAgentClient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
file(GLOB_RECURSE maa_agent_client_src *.h *.hpp *.cpp)
file(GLOB_RECURSE maa_agent_client_header ${MAA_PUBLIC_INC}/MaaAgentClient/*)

add_library(MaaAgentClient SHARED ${maa_agent_client_src} ${maa_agent_client_header})

target_include_directories(
MaaAgentClient
PUBLIC $<BUILD_INTERFACE:${MAA_PUBLIC_INC}> $<INSTALL_INTERFACE:include>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${MAA_PRIVATE_INC} ${MAA_PUBLIC_INC})

target_compile_definitions(MaaAgentClient PRIVATE MAA_AGENT_CLIENT_EXPORTS)

target_link_libraries(MaaAgentClient PRIVATE MaaUtils MaaFramework LibraryHolder HeaderOnlyLibraries)

add_dependencies(MaaAgentClient MaaUtils MaaFramework)

install(
TARGETS MaaAgentClient
RUNTIME DESTINATION bin
LIBRARY DESTINATION bin
ARCHIVE DESTINATION lib)

if(WIN32)
install(FILES $<TARGET_PDB_FILE:MaaAgentClient> DESTINATION symbol OPTIONAL)
endif()

install(DIRECTORY "${MAA_PUBLIC_INC}/MaaAgentClient" DESTINATION "include")

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${maa_agent_client_src})
27 changes: 27 additions & 0 deletions source/MaaAgentClient/Client/AgentClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "AgentClient.h"

MAA_AGENT_CLIENT_NS_BEGIN

AgentClient::AgentClient(MaaNotificationCallback notify, void* notify_trans_arg)
: notifier_(notify, notify_trans_arg)
{
LogFunc << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);
}

bool AgentClient::start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args)
{
LogFunc << VAR(child_exec) << VAR(child_args);

return false;
}

bool AgentClient::bind_resource(MaaResource* resource)
{
LogInfo << VAR_VOIDP(this) << VAR_VOIDP(resource);

resource_ = resource;

return true;
}

MAA_AGENT_CLIENT_NS_END
25 changes: 25 additions & 0 deletions source/MaaAgentClient/Client/AgentClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <filesystem>

#include "API/MaaAgentClientTypes.h"
#include "Conf/Conf.h"
#include "Utils/MessageNotifier.hpp"

MAA_AGENT_CLIENT_NS_BEGIN

class AgentClient : public MaaAgentClient
{
public:
AgentClient(MaaNotificationCallback notify, void* notify_trans_arg);
virtual ~AgentClient() override = default;

virtual bool start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args) override;
virtual bool bind_resource(MaaResource* resource) override;

private:
MaaResource* resource_ = nullptr;
MessageNotifier notifier_;
};

MAA_AGENT_CLIENT_NS_END
6 changes: 3 additions & 3 deletions source/MaaFramework/Controller/ControllerAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ MAA_CTRL_NS_BEGIN
std::minstd_rand ControllerAgent::rand_engine_(std::random_device {}());

ControllerAgent::ControllerAgent(MaaNotificationCallback notify, void* notify_trans_arg)
: notifier(notify, notify_trans_arg)
: notifier_(notify, notify_trans_arg)
{
LogFunc << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);

Expand Down Expand Up @@ -678,7 +678,7 @@ bool ControllerAgent::run_action(typename AsyncRunner<Action>::Id id, Action act
{ "action", std::move(ss).str() },
};
if (notify) {
notifier.notify(MaaMsg_Controller_Action_Starting, cb_detail);
notifier_.notify(MaaMsg_Controller_Action_Starting, cb_detail);
}

switch (action.type) {
Expand Down Expand Up @@ -730,7 +730,7 @@ bool ControllerAgent::run_action(typename AsyncRunner<Action>::Id id, Action act
}

if (notify) {
notifier.notify(ret ? MaaMsg_Controller_Action_Succeeded : MaaMsg_Controller_Action_Failed, cb_detail);
notifier_.notify(ret ? MaaMsg_Controller_Action_Succeeded : MaaMsg_Controller_Action_Failed, cb_detail);
}

return ret;
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Controller/ControllerAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class ControllerAgent : public MaaController
virtual bool _input_text(InputTextParam param) = 0;

protected:
MessageNotifier notifier;
MessageNotifier notifier_;

private:
MaaCtrlId post_connection_impl();
Expand Down
6 changes: 3 additions & 3 deletions source/MaaFramework/Resource/ResourceMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
MAA_RES_NS_BEGIN

ResourceMgr::ResourceMgr(MaaNotificationCallback notify, void* notify_trans_arg)
: notifier(notify, notify_trans_arg)
: notifier_(notify, notify_trans_arg)
{
LogFunc << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);

Expand Down Expand Up @@ -493,13 +493,13 @@ bool ResourceMgr::run_load(typename AsyncRunner<std::filesystem::path>::Id id, s
{ "hash", get_hash() },
};

notifier.notify(MaaMsg_Resource_Loading_Starting, cb_detail);
notifier_.notify(MaaMsg_Resource_Loading_Starting, cb_detail);

valid_ = load(path);

cb_detail["hash"] = calc_hash();

notifier.notify(valid_ ? MaaMsg_Resource_Loading_Succeeded : MaaMsg_Resource_Loading_Failed, cb_detail);
notifier_.notify(valid_ ? MaaMsg_Resource_Loading_Succeeded : MaaMsg_Resource_Loading_Failed, cb_detail);

return valid_;
}
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Resource/ResourceMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ResourceMgr : public MaaResource
std::atomic_bool valid_ = true;

std::unique_ptr<AsyncRunner<std::filesystem::path>> res_loader_ = nullptr;
MessageNotifier notifier;
MessageNotifier notifier_;

MaaInferenceDevice inference_device_ = MaaInferenceDevice_Auto;
MaaInferenceExecutionProvider inference_ep_ = MaaInferenceExecutionProvider_Auto;
Expand Down
23 changes: 13 additions & 10 deletions source/MaaFramework/Tasker/Tasker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
MAA_NS_BEGIN

Tasker::Tasker(MaaNotificationCallback notify, void* notify_trans_arg)
: notifier(notify, notify_trans_arg)
: notifier_(notify, notify_trans_arg)
{
LogFunc << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);

Expand All @@ -30,28 +30,31 @@ Tasker::~Tasker()

bool Tasker::bind_resource(MaaResource* resource)
{
LogInfo << VAR_VOIDP(this) << VAR_VOIDP(resource);
auto* derived = dynamic_cast<MAA_RES_NS::ResourceMgr*>(resource);

resource_ = dynamic_cast<MAA_RES_NS::ResourceMgr*>(resource);
LogInfo << VAR_VOIDP(this) << VAR_VOIDP(resource) << VAR_VOIDP(derived) << VAR_VOIDP(resource_);

if (!resource) {
if (resource && !derived) {
LogError << "Invalid resource";
return false;
}

resource_ = derived;
return true;
}

bool Tasker::bind_controller(MaaController* controller)
{
LogInfo << VAR_VOIDP(this) << VAR_VOIDP(controller);
auto* derived = dynamic_cast<MAA_CTRL_NS::ControllerAgent*>(controller);

controller_ = dynamic_cast<MAA_CTRL_NS::ControllerAgent*>(controller);
LogInfo << VAR_VOIDP(this) << VAR_VOIDP(controller) << VAR_VOIDP(derived) << VAR_VOIDP(controller_);

if (!controller) {
if (controller && !derived) {
LogError << "Invalid controller";
return false;
}

controller_ = derived;
return true;
}

Expand Down Expand Up @@ -185,7 +188,7 @@ const RuntimeCache& Tasker::runtime_cache() const

void Tasker::notify(std::string_view msg, const json::value& detail)
{
notifier.notify(msg, detail);
notifier_.notify(msg, detail);
}

MaaTaskId Tasker::post_task(TaskPtr task_ptr, const json::object& pipeline_override)
Expand Down Expand Up @@ -249,7 +252,7 @@ bool Tasker::run_task(RunnerId runner_id, TaskPtr task_ptr)
task_detail.status = MaaStatus_Running;
runtime_cache_.set_task_detail(task_id, std::move(task_detail));
}
notifier.notify(MaaMsg_Tasker_Task_Starting, cb_detail);
notifier_.notify(MaaMsg_Tasker_Task_Starting, cb_detail);

bool ret = task_ptr->run();

Expand All @@ -260,7 +263,7 @@ bool Tasker::run_task(RunnerId runner_id, TaskPtr task_ptr)
task_detail.status = ret ? MaaStatus_Succeeded : MaaStatus_Failed;
runtime_cache_.set_task_detail(task_id, std::move(task_detail));
}
notifier.notify(ret ? MaaMsg_Tasker_Task_Succeeded : MaaMsg_Tasker_Task_Failed, cb_detail);
notifier_.notify(ret ? MaaMsg_Tasker_Task_Succeeded : MaaMsg_Tasker_Task_Failed, cb_detail);

running_task_ = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Tasker/Tasker.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Tasker : public MaaTasker
TaskPtr running_task_ = nullptr;

RuntimeCache runtime_cache_;
MessageNotifier notifier;
MessageNotifier notifier_;
};

MAA_NS_END
7 changes: 7 additions & 0 deletions source/include/Conf/Conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,10 @@
namespace MAA_PROJECT_INTERFACE_NS \
{
#define MAA_PROJECT_INTERFACE_NS_END }

#define MAA_AGENT_CLIENT_NS MAA_NS::AgentClientNS

#define MAA_AGENT_CLIENT_NS_BEGIN \
namespace MAA_AGENT_CLIENT_NS \
{
#define MAA_AGENT_CLIENT_NS_END }

0 comments on commit b184c35

Please sign in to comment.