Skip to content

Commit

Permalink
feat: impl for ExecAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 5, 2023
1 parent e434ed7 commit deec098
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 18 deletions.
4 changes: 0 additions & 4 deletions source/MaaToolKit/API/MaaToolKitExecAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ MaaBool RegisterExecutor(ExecutorType type, MaaInstanceHandle handle, MaaStringV
}

auto path = MAA_NS::path(exec_path);
if (!std::filesystem::exists(path)) {
LogError << "exec path not exists:" << path;
return false;
}

auto params_opt = json::parse(exec_param_json);
if (!params_opt) {
Expand Down
2 changes: 1 addition & 1 deletion source/MaaToolKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ target_compile_definitions(MaaToolKit PRIVATE MAA_TOOLKIT_EXPORTS)
target_link_libraries(
MaaToolKit
PUBLIC MaaFramework
PRIVATE MaaUtils LibraryHolder HeaderOnlyLibraries Boost::system)
PRIVATE MaaUtils LibraryHolder HeaderOnlyLibraries Boost::system ${OpenCV_LIBS})

add_dependencies(MaaToolKit MaaFramework MaaUtils LibraryHolder)

Expand Down
33 changes: 31 additions & 2 deletions source/MaaToolKit/ExecAgent/ActionExecAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,39 @@ bool ActionExecAgent::unregister_for_maa_inst(MaaInstanceHandle handle, std::str
bool ActionExecAgent::run(MaaSyncContextHandle sync_context, std::string_view task_name,
std::string_view custom_action_param, MaaRectHandle cur_box, std::string_view cur_rec_detail)
{
return false;
LogFunc << VAR_VOIDP(sync_context) << VAR(task_name) << VAR(custom_action_param) << VAR_VOIDP(cur_box)
<< VAR(cur_rec_detail);

auto exec_it = executors_.find(std::string(task_name));
if (exec_it == executors_.end()) {
LogError << "executor not found" << VAR(task_name);
return false;
}
auto& exec = exec_it->second;

std::string handle_arg = arg_cvt_.sync_context_to_arg(sync_context);
std::string box_arg = json::array({ cur_box->x, cur_box->y, cur_box->width, cur_box->height }).to_string();

std::vector<std::string> extra_args = { handle_arg, std::string(task_name), std::string(custom_action_param),
box_arg, std::string(cur_rec_detail) };
std::vector<std::string> args = exec.exec_args;
args.insert(args.end(), std::make_move_iterator(extra_args.begin()), std::make_move_iterator(extra_args.end()));

auto output_opt = run_executor(exec.text_mode, exec.exec_path, args);
if (!output_opt) {
LogError << "run_executor failed" << VAR(exec.exec_path) << VAR(exec.exec_args);
return false;
}

return true;
}

void ActionExecAgent::stop() {}
void ActionExecAgent::stop()
{
LogFunc;

// TODO
}

MaaBool ActionExecAgent::maa_api_run(MaaSyncContextHandle sync_context, MaaStringView task_name,
MaaStringView custom_action_param, MaaRectHandle cur_box,
Expand Down
2 changes: 1 addition & 1 deletion source/MaaToolKit/ExecAgent/ActionExecAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ActionExecAgent final : public ExecAgentBase, public SingletonHolder<Actio
static void maa_api_stop(MaaTransparentArg action_arg);

private:
MaaCustomActionAPI custom_action_;
MaaCustomActionAPI custom_action_ {};
};

MAA_TOOLKIT_NS_END
52 changes: 48 additions & 4 deletions source/MaaToolKit/ExecAgent/ExecAgentBase.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "ExecAgentBase.h"

#include "MaaFramework/MaaAPI.h"
#include "Utils/Boost.hpp"
#include "Utils/IOStream/ChildPipeIOStream.h"
#include "Utils/Logger.h"

MAA_TOOLKIT_NS_BEGIN

bool ExecAgentBase::register_executor(MaaInstanceHandle handle, std::string_view name, std::filesystem::path exec_path,
std::vector<std::string> exec_params, TextTransferMode text_mode,
std::vector<std::string> exec_args, TextTransferMode text_mode,
ImageTransferMode image_mode)
{
LogFunc << VAR_VOIDP(handle) << VAR(name) << VAR(exec_path) << VAR(exec_params) << VAR(text_mode)
<< VAR(image_mode);
LogFunc << VAR_VOIDP(handle) << VAR(name) << VAR(exec_path) << VAR(exec_args) << VAR(text_mode) << VAR(image_mode);

bool registered = register_for_maa_inst(handle, name);
if (!registered) {
Expand All @@ -20,7 +22,7 @@ bool ExecAgentBase::register_executor(MaaInstanceHandle handle, std::string_view
Executor executor {
.name = std::string(name),
.exec_path = std::move(exec_path),
.exec_params = std::move(exec_params),
.exec_args = std::move(exec_args),
.text_mode = text_mode,
.image_mode = image_mode,
};
Expand All @@ -46,4 +48,46 @@ bool ExecAgentBase::unregister_executor(MaaInstanceHandle handle, std::string_vi
return ret;
}

std::optional<std::string> ExecAgentBase::run_executor(TextTransferMode mode, const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args)
{
switch (mode) {
case TextTransferMode::StdIO: {
return run_executor_with_stdio(exec_path, exec_args);
}
case TextTransferMode::FileIO:
default:
LogError << "not implemented";
return std::nullopt;
}
}

std::optional<std::string> ExecAgentBase::run_executor_with_stdio(const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args)
{
auto searched_path = boost::process::search_path(exec_path);
if (!std::filesystem::exists(searched_path)) {
LogError << "path not exists" << VAR(searched_path);
return std::nullopt;
}

ChildPipeIOStream ios(searched_path, exec_args);

while (ios.is_open()) {
std::string line = ios.read_until("\n");
LogInfo << "read line:" << line;

// TODO
}

int exit_code = ios.release();
if (exit_code != 0) {
LogError << "process exit with code:" << exit_code;
return std::nullopt;
}

// TODO
return {};
}

MAA_TOOLKIT_NS_END
10 changes: 9 additions & 1 deletion source/MaaToolKit/ExecAgent/ExecAgentBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string_view>
#include <unordered_map>

#include "ExecAgent/ExecArgConverter.h"
#include "ExecAgentType.h"
#include "MaaFramework/MaaDef.h"

Expand All @@ -16,16 +17,23 @@ class ExecAgentBase
virtual ~ExecAgentBase() = default;

bool register_executor(MaaInstanceHandle handle, std::string_view name, std::filesystem::path exec_path,
std::vector<std::string> exec_params, TextTransferMode text_mode,
std::vector<std::string> exec_args, TextTransferMode text_mode,
ImageTransferMode image_mode);
bool unregister_executor(MaaInstanceHandle handle, std::string_view name);

protected:
virtual bool register_for_maa_inst(MaaInstanceHandle handle, std::string_view name) = 0;
virtual bool unregister_for_maa_inst(MaaInstanceHandle handle, std::string_view name) = 0;

std::optional<std::string> run_executor(TextTransferMode mode, const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args);
std::optional<std::string> run_executor_with_stdio(const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args);

protected:
std::unordered_map</*name*/ std::string, Executor> executors_;

ExecArgConverter arg_cvt_;
};

MAA_TOOLKIT_NS_END
2 changes: 1 addition & 1 deletion source/MaaToolKit/ExecAgent/ExecAgentType.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct Executor
{
std::string name;
std::filesystem::path exec_path;
std::vector<std::string> exec_params;
std::vector<std::string> exec_args;
TextTransferMode text_mode;
ImageTransferMode image_mode;
};
Expand Down
49 changes: 49 additions & 0 deletions source/MaaToolKit/ExecAgent/ExecArgConverter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "ExecArgConverter.h"

#include "MaaFramework/MaaAPI.h"
#include "Utils/ImageIo.h"
#include "Utils/Logger.h"
#include "Utils/Time.hpp"

MAA_TOOLKIT_NS_BEGIN

ExecArgConverter::~ExecArgConverter()
{
for (const auto& image : images_) {
std::filesystem::remove(image);
}
}

std::string ExecArgConverter::sync_context_to_arg(MaaSyncContextHandle sync_context)
{
std::string uuid = std::to_string(reinterpret_cast<uintptr_t>(sync_context));
sync_contexts_.insert_or_assign(uuid, sync_context);
return uuid;
}

std::string ExecArgConverter::image_to_arg(ImageTransferMode mode, const cv::Mat& image)
{
switch (mode) {
case ImageTransferMode::FileIO: {
auto path = std::filesystem::temp_directory_path() / now_filestem() / ".png";
imwrite(path, image);
images_.push_back(path);
return path_to_crt_string(path);
}
default:
LogError << "not implemented";
return {};
}
}

MaaSyncContextHandle ExecArgConverter::arg_to_sync_context(const std::string& arg) const
{
auto it = sync_contexts_.find(arg);
if (it == sync_contexts_.end()) {
LogError << "sync context not found";
return nullptr;
}
return it->second;
}

MAA_TOOLKIT_NS_END
30 changes: 30 additions & 0 deletions source/MaaToolKit/ExecAgent/ExecArgConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "Conf/Conf.h"

#include <filesystem>
#include <map>

#include "ExecAgentType.h"
#include "MaaFramework/MaaDef.h"
#include "Utils/NoWarningCVMat.hpp"

MAA_TOOLKIT_NS_BEGIN

class ExecArgConverter
{
public:
~ExecArgConverter();

public:
std::string sync_context_to_arg(MaaSyncContextHandle sync_context);
std::string image_to_arg(ImageTransferMode mode, const cv::Mat& image);

MaaSyncContextHandle arg_to_sync_context(const std::string& arg) const;

private:
std::map<std::string, MaaSyncContextHandle> sync_contexts_;
std::vector<std::filesystem::path> images_;
};

MAA_TOOLKIT_NS_END
25 changes: 22 additions & 3 deletions source/MaaToolKit/ExecAgent/RecognizerExecAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <functional>

#include "MaaFramework/MaaAPI.h"
#include "Utils/ImageIo.h"
#include "Utils/Logger.h"
#include "Utils/Time.hpp"

MAA_TOOLKIT_NS_BEGIN

Expand All @@ -28,12 +30,29 @@ std::optional<RecognizerExecAgent::AnalyzeResult> RecognizerExecAgent::analyze(
{
LogFunc << VAR_VOIDP(sync_context) << VAR(image.size()) << VAR(task_name) << VAR(custom_recognition_param);

auto exec_it = executors_.find(task_name.data());
auto exec_it = executors_.find(std::string(task_name));
if (exec_it == executors_.end()) {
LogError << "no executor found for task: " << task_name;
LogError << "executor not found" << VAR(task_name);
return std::nullopt;
}
auto& executor = exec_it->second;
auto& exec = exec_it->second;

std::string handle_arg = arg_cvt_.sync_context_to_arg(sync_context);
std::string image_arg = arg_cvt_.image_to_arg(exec.image_mode, image);

std::vector<std::string> extra_args = { handle_arg, image_arg, std::string(task_name),
std::string(custom_recognition_param) };
std::vector<std::string> args = exec.exec_args;
args.insert(args.end(), std::make_move_iterator(extra_args.begin()), std::make_move_iterator(extra_args.end()));

auto output_opt = run_executor(exec.text_mode, exec.exec_path, args);
if (!output_opt) {
LogError << "run_executor failed" << VAR(exec.exec_path) << VAR(exec.exec_args);
return std::nullopt;
}

// TODO: parse output to get result
return {};
}

MaaBool RecognizerExecAgent::maa_api_analyze( //
Expand Down
5 changes: 4 additions & 1 deletion source/MaaToolKit/ExecAgent/RecognizerExecAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "Conf/Conf.h"
#include "ExecAgentBase.h"

#include <functional>
#include <map>

#include "MaaFramework/Task/MaaCustomRecognizer.h"
#include "Utils/NoWarningCVMat.hpp"
#include "Utils/SingletonHolder.hpp"
Expand Down Expand Up @@ -38,7 +41,7 @@ class RecognizerExecAgent final : public ExecAgentBase, public SingletonHolder<R
/*out*/ MaaStringBufferHandle out_detail);

private:
MaaCustomRecognizerAPI custom_recognizer_;
MaaCustomRecognizerAPI custom_recognizer_ {};
};

MAA_TOOLKIT_NS_END
5 changes: 5 additions & 0 deletions source/MaaUtils/IOStream/ChildPipeIOStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ int ChildPipeIOStream::release()
return child_.exit_code();
}

bool ChildPipeIOStream::is_open() const
{
return pin_.is_open();
}

MAA_NS_END
5 changes: 5 additions & 0 deletions source/MaaUtils/IOStream/SockIOStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ std::string SockIOStream::read_until(std::string_view delimiter, duration_t time
return result;
}

bool SockIOStream::is_open() const
{
return sock_.is_open();
}

MAA_NS_END
1 change: 1 addition & 0 deletions source/include/Utils/IOStream/ChildPipeIOStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MAA_UTILS_API ChildPipeIOStream : public NonCopyButMovable
std::string read_until(std::string_view delimiter, duration_t timeout = duration_t::max());

int release();
bool is_open() const;

private:
boost::process::ipstream pin_;
Expand Down
2 changes: 2 additions & 0 deletions source/include/Utils/IOStream/SockIOStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class MAA_UTILS_API SockIOStream : public NonCopyButMovable
std::string read_some(size_t count, duration_t timeout = duration_t::max());
std::string read_until(std::string_view delimiter, duration_t timeout = duration_t::max());

bool is_open() const;

private:
boost::asio::ip::tcp::socket sock_;

Expand Down

0 comments on commit deec098

Please sign in to comment.