diff --git a/source/MaaHttp/LHG b/source/MaaHttp/LHG index 0e739c979..fd2625ae6 160000 --- a/source/MaaHttp/LHG +++ b/source/MaaHttp/LHG @@ -1 +1 @@ -Subproject commit 0e739c979081c9f6dd4970919553078ee6939f7f +Subproject commit fd2625ae665faaeb8abfa6e08f435a8d64ae5e19 diff --git a/source/MaaHttp/source/.clang-format b/source/MaaHttp/source/.clang-format new file mode 100644 index 000000000..ec248ec1a --- /dev/null +++ b/source/MaaHttp/source/.clang-format @@ -0,0 +1,5 @@ +MacroBlockBegin: '^LHG.*Begin' +MacroBlockEnd: '^LHG.*End' +StatementMacros: + - 'LHGArgToJsonMiddle' + - 'LHGArgFromJsonMiddle' diff --git a/source/MaaHttp/source/spec/call.hpp b/source/MaaHttp/source/spec/call.hpp index 68437b660..5709cc36f 100644 --- a/source/MaaHttp/source/spec/call.hpp +++ b/source/MaaHttp/source/spec/call.hpp @@ -1,283 +1,192 @@ #pragma once #include "./type.hpp" -#include "./utils.hpp" -#include "utils/base64.hpp" #include "utils/phony.hpp" #include "../info.hpp" -namespace lhg::call -{ - -#pragma region hwnd - -#define __DECLARE_RETURN_HWND(func_tag) \ - __CALL_DECLARE_ARG_TO_JSON_BEGIN(func_tag, ret) \ - std::ignore = state; \ - json::value value; \ - value = \ - std::format("{:#018x}", reinterpret_cast(std::get(arg))); \ - res[name] = value; \ - __CALL_DECLARE_ARG_TO_JSON_END() - -__DECLARE_RETURN_HWND(maa::func_type_MaaToolkitGetWindow) -__DECLARE_RETURN_HWND(maa::func_type_MaaToolkitGetCursorWindow) -__DECLARE_RETURN_HWND(maa::func_type_MaaToolkitGetDesktopWindow) -__DECLARE_RETURN_HWND(maa::func_type_MaaToolkitGetForegroundWindow) - -#undef __DECLARE_RETURN_HWND - -__CALL_DECLARE_JSON_TO_ARG_BEGIN(maa::func_type_MaaWin32ControllerCreate, _0_hWnd) -std::get(arg) = - reinterpret_cast(std::stoull(value.as_string())); -return true; -__CALL_DECLARE_JSON_TO_ARG_END() - -#pragma endregion hwnd - -#pragma region MaaGetImageEncoded - -__CALL_DECLARE_ARG_TO_JSON_BEGIN(maa::func_type_MaaGetImageEncoded, ret) -std::ignore = state; -json::value value; -auto size = - MaaGetImageEncodedSize(std::get(arg)); -std::string_view data( - reinterpret_cast(std::get(arg)), - size); -value = lhg::to_base64(data); -res[name] = value; -__CALL_DECLARE_ARG_TO_JSON_END() - -#pragma endregion MaaGetImageEncoded - -#pragma region MaaSetImageEncoded - -__CALL_DECLARE_INPUT(maa::func_type_MaaSetImageEncoded::_2_size, true) - -__CALL_DECLARE_JSON_TO_ARG_BEGIN(maa::func_type_MaaSetImageEncoded, _1_data) -std::string& data = std::get(state); -auto opt = lhg::from_base64(value.as_string()); -if (!opt.has_value()) { +LHGArgHandle(maa::func_type_MaaWin32ControllerCreate::ret, alloc); +LHGArgHandle(maa::func_type_MaaAdbControllerCreateV2::ret, alloc); +LHGArgHandle(maa::func_type_MaaThriftControllerCreate::ret, alloc); +LHGArgHandle(maa::func_type_MaaDbgControllerCreate::ret, alloc); +LHGArgHandle(maa::func_type_MaaControllerDestroy::_0_ctrl, free); + +LHGArgHandle(maa::func_type_MaaResourceCreate::ret, alloc); +LHGArgHandle(maa::func_type_MaaResourceDestroy::_0_res, free); + +LHGArgHandle(maa::func_type_MaaCreate::ret, alloc); +LHGArgHandle(maa::func_type_MaaDestroy::_0_inst, free); + +LHGArgHandle(maa::func_type_MaaCreateImageBuffer::ret, alloc); +LHGArgHandle(maa::func_type_MaaDestroyImageBuffer::_0_handle, free); + +#define ReturnHwnd(func_tag) \ + LHGArgToJsonBegin(func_tag, ret, true) \ + value = std::format("{:#018x}", \ + reinterpret_cast(std::get(arg))); \ + LHGArgToJsonMiddle() \ + b.type("string"); \ + LHGArgToJsonEnd() + +ReturnHwnd(maa::func_type_MaaToolkitGetWindow); +ReturnHwnd(maa::func_type_MaaToolkitGetCursorWindow); +ReturnHwnd(maa::func_type_MaaToolkitGetDesktopWindow); +ReturnHwnd(maa::func_type_MaaToolkitGetForegroundWindow); + +#undef ReturnHwnd + +LHGArgFromJsonBegin(maa::func_type_MaaWin32ControllerCreate, _0_hWnd, true) + std::get(arg) = + reinterpret_cast(std::stoull(value.as_string())); + LHGArgFromJsonMiddle() + b.type("string"); +LHGArgFromJsonEnd() + +LHGArgToJsonBegin(maa::func_type_MaaGetImageEncoded, ret, true) + auto size = MaaGetImageEncodedSize( + std::get(arg)); + std::string_view data(reinterpret_cast(std::get(arg)), size); + value = to_base64(data); + LHGArgToJsonMiddle() + b.type("string"); +LHGArgToJsonEnd() + +LHGArgHide(maa::func_type_MaaSetImageEncoded::_2_size); +LHGArgFromJsonBegin(maa::func_type_MaaSetImageEncoded, _1_data, true) + std::string &data = std::get(state); + auto opt = from_base64(value.as_string()); + if (!opt.has_value()) { return false; -} -data = opt.value(); -std::get(arg) = - reinterpret_cast(const_cast(data.c_str())); -std::get(arg) = data.size(); -return true; -__CALL_DECLARE_JSON_TO_ARG_END() - -#pragma endregion MaaSetImageEncoded - -#define __DECLARE_STRING_BUFFER_OUTPUT(func_tag, arg_tag) \ - __CALL_DECLARE_INPUT(func_tag::arg_tag, false) \ - __CALL_DECLARE_OUTPUT(func_tag::arg_tag, true) \ - \ - __CALL_DECLARE_PREPARE_STATE_BEGIN(func_tag, arg_tag) \ - std::ignore = provider; \ - std::ignore = req; \ - std::ignore = arg; \ - std::get(state) = MaaCreateStringBuffer(); \ - std::get(arg) = std::get(state); \ - return true; \ - __CALL_DECLARE_PREPARE_STATE_END() \ - \ - __CALL_DECLARE_ARG_TO_JSON_BEGIN(func_tag, arg_tag) \ - std::ignore = state; \ - auto handle = std::get(state); \ - auto size = MaaGetStringSize(handle); \ - std::string data(MaaGetString(handle), size); \ - MaaDestroyStringBuffer(handle); \ - res[name] = data; \ - __CALL_DECLARE_ARG_TO_JSON_END() - -__DECLARE_STRING_BUFFER_OUTPUT(maa::func_type_MaaResourceGetTaskList, _1_buffer) -__DECLARE_STRING_BUFFER_OUTPUT(maa::func_type_MaaResourceGetHash, _1_buffer) -__DECLARE_STRING_BUFFER_OUTPUT(maa::func_type_MaaControllerGetUUID, _1_buffer) -__DECLARE_STRING_BUFFER_OUTPUT(maa::func_type_MaaSyncContextGetTaskResult, _2_out_task_result) -__DECLARE_STRING_BUFFER_OUTPUT(maa::func_type_MaaSyncContextRunRecognizer, _5_out_detail) - -#undef __DECLARE_STRING_BUFFER_OUTPUT - -#define __DECLARE_APICALLBACK(func_tag, cb_tag, ctx_tag) \ - __CALL_DECLARE_INPUT(func_tag::ctx_tag, false) \ - \ - __CALL_DECLARE_JSON_TO_ARG_BEGIN(func_tag, cb_tag) \ - std::string id = value.as_string(); \ - auto manager = provider.get, void>(); \ - auto ctx = manager->query(id); \ - if (!ctx.get()) { \ - return false; \ - } \ - std::get(arg) = \ - callback::create_callback(); \ - std::get(arg) = ctx.get(); \ - return true; \ - __CALL_DECLARE_JSON_TO_ARG_END() - -__DECLARE_APICALLBACK(maa::func_type_MaaWin32ControllerCreate, _2_callback, _3_callback_arg) -__DECLARE_APICALLBACK(maa::func_type_MaaAdbControllerCreateV2, _5_callback, _6_callback_arg) -__DECLARE_APICALLBACK(maa::func_type_MaaThriftControllerCreate, _4_callback, _5_callback_arg) -__DECLARE_APICALLBACK(maa::func_type_MaaDbgControllerCreate, _4_callback, _5_callback_arg) -__DECLARE_APICALLBACK(maa::func_type_MaaResourceCreate, _0_callback, _1_callback_arg) -__DECLARE_APICALLBACK(maa::func_type_MaaCreate, _0_callback, _1_callback_arg) - -#undef __DECLARE_APICALLBACK - -__CALL_DECLARE_INPUT(maa::func_type_MaaRegisterCustomRecognizer::_3_recognizer_arg, false) - -__CALL_DECLARE_JSON_TO_ARG_BEGIN(maa::func_type_MaaRegisterCustomRecognizer, _2_recognizer) -std::string id = value.as_string(); -auto manager = provider.get, void>(); -auto ctx = manager->query(id); -if (!ctx.get()) { + } + data = opt.value(); + std::get(arg) = + reinterpret_cast(const_cast(data.c_str())); + std::get(arg) = + data.size(); + LHGArgFromJsonMiddle() + b.type("string"); +LHGArgFromJsonEnd() + +#define OutputString(func_tag, atag) \ + LHGArgOutput(func_tag::atag); \ + LHGArgPrepareStateBegin(func_tag, atag) \ + std::get(arg) = std::get(state) = \ + MaaCreateStringBuffer(); \ + LHGArgPrepareStateEnd() \ + LHGArgToJsonBegin(func_tag, atag, true) \ + auto handle = std::get(state); \ + auto size = MaaGetStringSize(handle); \ + std::string data(MaaGetString(handle), size); \ + MaaDestroyStringBuffer(handle); \ + value = data; \ + LHGArgToJsonMiddle() \ + b.type("string"); \ + LHGArgToJsonEnd() + +OutputString(maa::func_type_MaaResourceGetTaskList, _1_buffer); +OutputString(maa::func_type_MaaResourceGetHash, _1_buffer); +OutputString(maa::func_type_MaaControllerGetUUID, _1_buffer); +OutputString(maa::func_type_MaaSyncContextGetTaskResult, _2_out_task_result); +OutputString(maa::func_type_MaaSyncContextRunRecognizer, _5_out_detail); + +#undef OutputString + +#define ApiCallback(func_tag, cb_tag, ctx_tag) \ + LHGArgHide(func_tag::ctx_tag); \ + LHGArgFromJsonBegin(func_tag, cb_tag, true) \ + std::string id = value.as_string(); \ + auto manager = \ + provider.get, void>(); \ + auto ctx = manager->query(id); \ + if (!ctx.get()) { \ + return false; \ + } \ + std::get(arg) = \ + callback::create_callback(); \ + std::get(arg) = ctx.get(); \ + LHGArgFromJsonMiddle() \ + b.type("string"); \ + LHGArgFromJsonEnd() + +ApiCallback(maa::func_type_MaaWin32ControllerCreate, _2_callback, + _3_callback_arg); +ApiCallback(maa::func_type_MaaAdbControllerCreateV2, _5_callback, + _6_callback_arg); +ApiCallback(maa::func_type_MaaThriftControllerCreate, _4_callback, + _5_callback_arg); +ApiCallback(maa::func_type_MaaDbgControllerCreate, _4_callback, + _5_callback_arg); +ApiCallback(maa::func_type_MaaResourceCreate, _0_callback, _1_callback_arg); +ApiCallback(maa::func_type_MaaCreate, _0_callback, _1_callback_arg); + +#undef ApiCallback + +LHGArgHide(maa::func_type_MaaRegisterCustomRecognizer::_3_recognizer_arg); +LHGArgFromJsonBegin(maa::func_type_MaaRegisterCustomRecognizer, _2_recognizer, + true) + std::string id = value.as_string(); + auto manager = + provider + .get, void>(); + auto ctx = manager->query(id); + if (!ctx.get()) { return false; -} -static MaaCustomRecognizerAPI api = { - callback::create_callback() -}; -std::get(arg) = &api; -std::get(arg) = ctx.get(); -return true; -__CALL_DECLARE_JSON_TO_ARG_END() - -__CALL_DECLARE_INPUT(maa::func_type_MaaRegisterCustomAction::_3_action_arg, false) - -__CALL_DECLARE_JSON_TO_ARG_BEGIN(maa::func_type_MaaRegisterCustomAction, _2_action) -json::object ids = value.as_object(); -std::string run_id = ids["run"].as_string(); -std::string stop_id = ids["stop"].as_string(); -auto run_manager = provider.get, void>(); -auto run_ctx = run_manager->query(run_id); -if (!run_ctx.get()) { + } + static MaaCustomRecognizerAPI api = { + callback::create_callback()}; + std::get(arg) = &api; + std::get< + maa::func_type_MaaRegisterCustomRecognizer::_3_recognizer_arg::index>( + arg) = ctx.get(); + LHGArgFromJsonMiddle() + b.type("string"); +LHGArgFromJsonEnd() + +LHGArgHide(maa::func_type_MaaRegisterCustomAction::_3_action_arg); +LHGArgFromJsonBegin(maa::func_type_MaaRegisterCustomAction, _2_action, true) + json::object ids = value.as_object(); + std::string run_id = ids["run"].as_string(); + std::string stop_id = ids["stop"].as_string(); + auto run_manager = + provider.get, void>(); + auto run_ctx = run_manager->query(run_id); + if (!run_ctx.get()) { return false; -} -auto stop_manager = provider.get, void>(); -auto stop_ctx = stop_manager->query(stop_id); -if (!stop_ctx.get()) { + } + auto stop_manager = + provider.get, void>(); + auto stop_ctx = stop_manager->query(stop_id); + if (!stop_ctx.get()) { return false; -} -auto ctx = provider.get()->alloc( - std::vector { run_id, stop_id }); -ctx->run = run_ctx; -ctx->stop = stop_ctx; -static MaaCustomActionAPI api = { callback::create_callback(), - callback::create_callback() }; -std::get(arg) = &api; -std::get(arg) = ctx; -return true; -__CALL_DECLARE_JSON_TO_ARG_END() - -__CALL_DECLARE_JSON_TO_ARG_BEGIN(maa::func_type_MaaSyncContextRunAction, _3_cur_box) -auto rec = &std::get(state); -rec->x = value.at("x").as_integer(); -rec->y = value.at("y").as_integer(); -rec->width = value.at("width").as_integer(); -rec->height = value.at("height").as_integer(); -return true; -__CALL_DECLARE_JSON_TO_ARG_END() - -__CALL_DECLARE_INPUT(maa::func_type_MaaSyncContextRunRecognizer::_4_out_box, false) -__CALL_DECLARE_OUTPUT(maa::func_type_MaaSyncContextRunRecognizer::_4_out_box, true) - -__CALL_DECLARE_PREPARE_STATE_BEGIN(maa::func_type_MaaSyncContextRunRecognizer, _4_out_box) -std::ignore = provider; -std::ignore = req; -std::ignore = arg; -std::ignore = state; -return true; -__CALL_DECLARE_PREPARE_STATE_END() - -__CALL_DECLARE_ARG_TO_JSON_BEGIN(maa::func_type_MaaSyncContextRunRecognizer, _4_out_box) -auto rec = std::get(state); -res[name] = { - { "x", rec.x }, - { "y", rec.y }, - { "width", rec.width }, - { "height", rec.height }, -}; -__CALL_DECLARE_ARG_TO_JSON_END() - -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaWin32ControllerCreate::ret, alloc) -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaAdbControllerCreateV2::ret, alloc) -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaThriftControllerCreate::ret, alloc) -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaDbgControllerCreate::ret, alloc) -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaControllerDestroy::_0_ctrl, free) - -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaResourceCreate::ret, alloc) -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaResourceDestroy::_0_res, free) - -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaCreate::ret, alloc) -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaDestroy::_0_inst, free) - -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaCreateImageBuffer::ret, alloc) -__CALL_DECLARE_HANDLE_OPER(maa::func_type_MaaDestroyImageBuffer::_0_handle, free) - -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(maa::func_type_MaaGetImageEncoded, ret, "string") -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE(maa::func_type_MaaSetImageEncoded, _1_data, "string") - -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(maa::func_type_MaaToolkitGetWindow, ret, "string") -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(maa::func_type_MaaToolkitGetCursorWindow, ret, "string") -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(maa::func_type_MaaToolkitGetDesktopWindow, ret, "string") -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(maa::func_type_MaaToolkitGetForegroundWindow, ret, "string") - -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(maa::func_type_MaaResourceGetTaskList, _1_buffer, "string") -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(maa::func_type_MaaResourceGetHash, _1_buffer, "string") -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(maa::func_type_MaaControllerGetUUID, _1_buffer, "string") -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE( - maa::func_type_MaaSyncContextGetTaskResult, - _2_out_task_result, - "string") -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE( - maa::func_type_MaaSyncContextRunRecognizer, - _5_out_detail, - "string") - -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE(maa::func_type_MaaWin32ControllerCreate, _0_hWnd, "string") - -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE( - maa::func_type_MaaWin32ControllerCreate, - _2_callback, - "string") -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE( - maa::func_type_MaaAdbControllerCreateV2, - _5_callback, - "string") -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE( - maa::func_type_MaaThriftControllerCreate, - _4_callback, - "string") -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE(maa::func_type_MaaDbgControllerCreate, _4_callback, "string") -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE(maa::func_type_MaaResourceCreate, _0_callback, "string") -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE(maa::func_type_MaaCreate, _0_callback, "string") - -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE( - maa::func_type_MaaRegisterCustomRecognizer, - _2_recognizer, - "string") - -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_BEGIN(maa::func_type_MaaRegisterCustomAction, _2_action) -b.type("object").prop({ - { "run", schema::Builder().type("string").obj }, - { "stop", schema::Builder().type("string").obj }, -}); -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_END() - -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_BEGIN(maa::func_type_MaaSyncContextRunAction, _3_cur_box) -b.type("object").prop({ { "x", schema::Builder().type("number").obj }, - { "y", schema::Builder().type("number").obj }, - { "width", schema::Builder().type("number").obj }, - { "height", schema::Builder().type("number").obj } }); -__CALL_DECLARE_JSON_TO_ARG_SCHEMA_END() - -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_BEGIN(maa::func_type_MaaSyncContextRunRecognizer, _4_out_box) -b.type("object").prop({ { "x", schema::Builder().type("number").obj }, - { "y", schema::Builder().type("number").obj }, - { "width", schema::Builder().type("number").obj }, - { "height", schema::Builder().type("number").obj } }); -__CALL_DECLARE_ARG_TO_JSON_SCHEMA_END() - -}; // namespace lhg::call + } + auto ctx = + provider.get()->alloc( + std::vector{run_id, stop_id}); + ctx->run = run_ctx; + ctx->stop = stop_ctx; + static MaaCustomActionAPI api = { + callback::create_callback(), + callback::create_callback()}; + std::get(arg) = &api; + std::get(arg) = + ctx; + LHGArgFromJsonMiddle() + b.type("object").prop({{"run", schema::Builder().type("string").obj}, + {"stop", schema::Builder().type("string").obj}}); +LHGArgFromJsonEnd() + +LHGArgFromJsonBegin(maa::func_type_MaaSyncContextRunAction, _3_cur_box, true) + auto rec = &std::get(state); + pri_maa::to_rect(value, rec); + LHGArgFromJsonMiddle() + pri_maa::schema_rect(b); +LHGArgFromJsonEnd() + +LHGArgOutput(maa::func_type_MaaSyncContextRunRecognizer::_4_out_box); +LHGArgToJsonBegin(maa::func_type_MaaSyncContextRunRecognizer, _4_out_box, true) + auto rec = &std::get(state); + value = pri_maa::from_rect(rec); + LHGArgToJsonMiddle() + pri_maa::schema_rect(b); +LHGArgToJsonEnd() diff --git a/source/MaaHttp/source/spec/call_forward.hpp b/source/MaaHttp/source/spec/call_forward.hpp index d6e9b85a0..ac926e6dc 100644 --- a/source/MaaHttp/source/spec/call_forward.hpp +++ b/source/MaaHttp/source/spec/call_forward.hpp @@ -1,27 +1,22 @@ #pragma once -#include "./utils.hpp" - #include "utils/phony.hpp" #include "../info.hpp" -namespace lhg::call -{ - -__CALL_DECLARE_OUTER_STATE(maa::func_type_MaaSetImageEncoded::_1_data, std::string) - -__CALL_DECLARE_OUTER_STATE(maa::func_type_MaaResourceGetTaskList::_1_buffer, MaaStringBufferHandle) -__CALL_DECLARE_OUTER_STATE(maa::func_type_MaaResourceGetHash::_1_buffer, MaaStringBufferHandle) -__CALL_DECLARE_OUTER_STATE(maa::func_type_MaaControllerGetUUID::_1_buffer, MaaStringBufferHandle) -__CALL_DECLARE_OUTER_STATE( - maa::func_type_MaaSyncContextGetTaskResult::_2_out_task_result, - MaaStringBufferHandle) -__CALL_DECLARE_OUTER_STATE( - maa::func_type_MaaSyncContextRunRecognizer::_5_out_detail, - MaaStringBufferHandle) - -__CALL_DECLARE_OUTER_STATE(maa::func_type_MaaSyncContextRunAction::_3_cur_box, MaaRect); -__CALL_DECLARE_OUTER_STATE(maa::func_type_MaaSyncContextRunRecognizer::_4_out_box, MaaRect); - -}; // namespace lhg::call +LHGArgOuterState(maa::func_type_MaaSetImageEncoded::_1_data, std::string); + +LHGArgOuterState(maa::func_type_MaaResourceGetTaskList::_1_buffer, + MaaStringBufferHandle); +LHGArgOuterState(maa::func_type_MaaResourceGetHash::_1_buffer, + MaaStringBufferHandle); +LHGArgOuterState(maa::func_type_MaaControllerGetUUID::_1_buffer, + MaaStringBufferHandle); +LHGArgOuterState(maa::func_type_MaaSyncContextGetTaskResult::_2_out_task_result, + MaaStringBufferHandle); +LHGArgOuterState(maa::func_type_MaaSyncContextRunRecognizer::_5_out_detail, + MaaStringBufferHandle); + +LHGArgOuterState(maa::func_type_MaaSyncContextRunAction::_3_cur_box, MaaRect); +LHGArgOuterState(maa::func_type_MaaSyncContextRunRecognizer::_4_out_box, + MaaRect); diff --git a/source/MaaHttp/source/spec/callback.hpp b/source/MaaHttp/source/spec/callback.hpp index b7aac7908..497404d57 100644 --- a/source/MaaHttp/source/spec/callback.hpp +++ b/source/MaaHttp/source/spec/callback.hpp @@ -1,96 +1,49 @@ #pragma once #include "./type.hpp" -#include "./utils.hpp" -#include "MaaFramework/Utility/MaaBuffer.h" #include "utils/phony.hpp" #include "../info.hpp" -namespace lhg::callback -{ - -__CALLBACK_DECLARE_HANDLE_OPER(maa::func_type_CustomActionRun::_0_sync_context, scope) -__CALLBACK_DECLARE_ARG_TO_JSON_BEGIN(maa::func_type_CustomActionRun, _3_cur_box) -auto rec = std::get(arg); -req[name] = { - { "x", rec->x }, - { "y", rec->y }, - { "width", rec->width }, - { "height", rec->height }, -}; -__CALLBACK_DECLARE_ARG_TO_JSON_END() - -__CALLBACK_DECLARE_HANDLE_OPER(maa::func_type_CustomRecognizerAnalyze::_0_sync_context, scope) -__CALLBACK_DECLARE_HANDLE_OPER(maa::func_type_CustomRecognizerAnalyze::_1_image, scope) -__CALLBACK_DECLARE_INPUT(maa::func_type_CustomRecognizerAnalyze::_5_out_box, false) -__CALLBACK_DECLARE_INPUT(maa::func_type_CustomRecognizerAnalyze::_6_out_detail, false) -__CALLBACK_DECLARE_OUTPUT(maa::func_type_CustomRecognizerAnalyze::_5_out_box, true) -__CALLBACK_DECLARE_OUTPUT(maa::func_type_CustomRecognizerAnalyze::_6_out_detail, true) - -__CALLBACK_DECLARE_JSON_TO_ARG_BEGIN(maa::func_type_CustomRecognizerAnalyze, _5_out_box) -auto rec = std::get(arg); -rec->x = value.at("x").as_integer(); -rec->y = value.at("y").as_integer(); -rec->width = value.at("width").as_integer(); -rec->height = value.at("height").as_integer(); -return true; -__CALLBACK_DECLARE_ARG_TO_JSON_END() - -__CALLBACK_DECLARE_JSON_TO_ARG_BEGIN(maa::func_type_CustomRecognizerAnalyze, _6_out_detail) -auto det = std::get(arg); -std::string detail = value.as_string(); -MaaSetStringEx(det, detail.c_str(), detail.size()); -return true; -__CALLBACK_DECLARE_ARG_TO_JSON_END() - -template <> -struct get_context -{ - using callback_tag = maa::callback_CustomActionRun; - using func_type = typename callback_tag::type; - using arg_tuple = typename func_type::args; - using call_arg_tuple = convert_arg_type; - static context_info* get(const call_arg_tuple& arg) - { - auto ptr = - reinterpret_cast(std::get(arg)); - return ptr->run.get(); - } -}; - -template <> -struct get_context -{ - using callback_tag = maa::callback_CustomActionStop; - using func_type = typename callback_tag::type; - using arg_tuple = typename func_type::args; - using call_arg_tuple = convert_arg_type; - static context_info* get(const call_arg_tuple& arg) - { - auto ptr = - reinterpret_cast(std::get(arg)); - return ptr->stop.get(); - } -}; - -__CALLBACK_DECLARE_JSON_TO_ARG_SCHEMA_BEGIN(maa::func_type_CustomRecognizerAnalyze, _5_out_box) -b.type("object").prop({ { "x", schema::Builder().type("number").obj }, - { "y", schema::Builder().type("number").obj }, - { "width", schema::Builder().type("number").obj }, - { "height", schema::Builder().type("number").obj } }); -__CALLBACK_DECLARE_JSON_TO_ARG_SCHEMA_END() -__CALLBACK_DECLARE_JSON_TO_ARG_SCHEMA_TYPE( - maa::func_type_CustomRecognizerAnalyze, - _6_out_detail, - "string") - -__CALLBACK_DECLARE_ARG_TO_JSON_SCHEMA_BEGIN(maa::func_type_CustomActionRun, _3_cur_box) -b.type("object").prop({ { "x", schema::Builder().type("number").obj }, - { "y", schema::Builder().type("number").obj }, - { "width", schema::Builder().type("number").obj }, - { "height", schema::Builder().type("number").obj } }); -__CALLBACK_DECLARE_ARG_TO_JSON_SCHEMA_END() - -}; // namespace lhg::callback +LHGArgHandle(maa::func_type_CustomActionRun::_0_sync_context, scope); +LHGArgHandle(maa::func_type_CustomRecognizerAnalyze::_0_sync_context, scope); +LHGArgHandle(maa::func_type_CustomRecognizerAnalyze::_1_image, scope); + +LHGArgToJsonBegin(maa::func_type_CustomActionRun, _3_cur_box, false) + auto rec = std::get(arg); + value = pri_maa::from_rect(rec); + LHGArgToJsonMiddle() + pri_maa::schema_rect(b); +LHGArgToJsonEnd() + +LHGArgOutput(maa::func_type_CustomRecognizerAnalyze::_5_out_box); +LHGArgOutput(maa::func_type_CustomRecognizerAnalyze::_6_out_detail); + +LHGArgFromJsonBegin(maa::func_type_CustomRecognizerAnalyze, _5_out_box, false) + auto rec = std::get(arg); + pri_maa::to_rect(value, rec); + LHGArgFromJsonMiddle() + pri_maa::schema_rect(b); +LHGArgFromJsonEnd() + +LHGArgFromJsonBegin(maa::func_type_CustomRecognizerAnalyze, _6_out_detail, + false) + auto det = std::get(arg); + std::string detail = value.as_string(); + MaaSetStringEx(det, detail.c_str(), detail.size()); + LHGArgFromJsonMiddle() + b.type("string"); +LHGArgFromJsonEnd() + +LHGArgGetContextBegin(maa::callback_CustomActionRun) + auto ptr = reinterpret_cast( + std::get(arg)); + return ptr->run.get(); +LHGArgGetContextEnd() + +LHGArgGetContextBegin(maa::callback_CustomActionStop) + auto ptr = reinterpret_cast( + std::get(arg)); + return ptr->stop.get(); +LHGArgGetContextEnd() diff --git a/source/MaaHttp/source/spec/callback_forward.hpp b/source/MaaHttp/source/spec/callback_forward.hpp index f0ae7a70d..951dc1687 100644 --- a/source/MaaHttp/source/spec/callback_forward.hpp +++ b/source/MaaHttp/source/spec/callback_forward.hpp @@ -1,25 +1,15 @@ #pragma once -#include "./utils.hpp" - -#include "MaaFramework/MaaDef.h" #include "utils/phony.hpp" #include "../info.hpp" -namespace lhg::callback -{ - -__CALLBACK_DECLARE_OUTER_STATE( +LHGArgOuterState( maa::func_type_CustomActionRun::_0_sync_context, - HandleManager::ScopedHandle) - -__CALLBACK_DECLARE_OUTER_STATE( + HandleManager::ScopedHandle); +LHGArgOuterState( maa::func_type_CustomRecognizerAnalyze::_0_sync_context, - HandleManager::ScopedHandle) - -__CALLBACK_DECLARE_OUTER_STATE( + HandleManager::ScopedHandle); +LHGArgOuterState( maa::func_type_CustomRecognizerAnalyze::_1_image, - HandleManager::ScopedHandle) - -}; // namespace lhg::callback + HandleManager::ScopedHandle); diff --git a/source/MaaHttp/source/spec/type.hpp b/source/MaaHttp/source/spec/type.hpp index a95b74aab..eda3ea189 100644 --- a/source/MaaHttp/source/spec/type.hpp +++ b/source/MaaHttp/source/spec/type.hpp @@ -2,20 +2,42 @@ #include -#include "callback/callback.hpp" +#include "MaaFramework/MaaDef.h" +#include "utils/phony.hpp" -namespace lhg -{ +namespace lhg { -namespace pri_maa -{ +namespace pri_maa { -struct custom_action_context -{ - std::shared_ptr run; - std::shared_ptr stop; +struct custom_action_context { + std::shared_ptr run; + std::shared_ptr stop; }; +inline json::object from_rect(MaaRectHandle rec) { + return { + {"x", rec->x}, + {"y", rec->y}, + {"width", rec->width}, + {"height", rec->height}, + }; } +inline bool to_rect(const json::value &value, MaaRectHandle rec) { + rec->x = value.at("x").as_integer(); + rec->y = value.at("y").as_integer(); + rec->width = value.at("width").as_integer(); + rec->height = value.at("height").as_integer(); + return true; } + +inline void schema_rect(schema::Builder &b) { + b.type("object").prop({{"x", schema::Builder().type("number").obj}, + {"y", schema::Builder().type("number").obj}, + {"width", schema::Builder().type("number").obj}, + {"height", schema::Builder().type("number").obj}}); +} + +} // namespace pri_maa + +} // namespace lhg diff --git a/source/MaaHttp/source/spec/utils.hpp b/source/MaaHttp/source/spec/utils.hpp deleted file mode 100644 index dc3c3c7a2..000000000 --- a/source/MaaHttp/source/spec/utils.hpp +++ /dev/null @@ -1,303 +0,0 @@ -#pragma once - -#pragma region call - -#define __CALL_DECLARE_INPUT(arg_tag, v) \ - template <> \ - struct is_input \ - { \ - constexpr static bool value = v; \ - }; - -#define __CALL_DECLARE_OUTPUT(arg_tag, v) \ - template <> \ - struct is_output \ - { \ - constexpr static bool value = v; \ - }; - -#define __CALL_DECLARE_OUTER_STATE(arg_tag, t) \ - template <> \ - struct is_outer_state \ - { \ - constexpr static bool value = true; \ - using type = t; \ - }; - -#define __CALL_DECLARE_HANDLE_OPER(arg_tag, o) \ - template <> \ - struct is_handle \ - { \ - constexpr static bool value = true; \ - constexpr static handle_oper oper = handle_oper::o; \ - }; - -#define __CALL_DECLARE_JSON_TO_ARG_SCHEMA_BEGIN(func_tag, atag) \ - template <> \ - struct json_to_arg_schema \ - { \ - static void get(json::object& req) \ - { \ - using arg_tag = std::tuple_element_t; \ - if constexpr ( \ - !is_input::value || std::is_same_v) { \ - ; \ - } \ - else { \ - auto name = arg_tag::name; \ - schema::Builder b; -#define __CALL_DECLARE_JSON_TO_ARG_SCHEMA_END() \ - req[name] = b.obj; \ - } \ - } \ - } \ - ; - -#define __CALL_DECLARE_JSON_TO_ARG_SCHEMA_TYPE(func_tag, atag, t) \ - __CALL_DECLARE_JSON_TO_ARG_SCHEMA_BEGIN(func_tag, atag) \ - b.type(t); \ - __CALL_DECLARE_JSON_TO_ARG_SCHEMA_END() - -#define __CALL_DECLARE_ARG_TO_JSON_SCHEMA_BEGIN(func_tag, atag) \ - template <> \ - struct arg_to_json_schema \ - { \ - static void get(json::object& res) \ - { \ - using arg_tag = std::tuple_element_t; \ - if constexpr ( \ - !is_output::value || std::is_same_v) { \ - ; \ - } \ - else { \ - auto name = arg_tag::name; \ - schema::Builder b; -#define __CALL_DECLARE_ARG_TO_JSON_SCHEMA_END() \ - res[name] = b.obj; \ - } \ - } \ - } \ - ; - -#define __CALL_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(func_tag, atag, t) \ - __CALL_DECLARE_ARG_TO_JSON_SCHEMA_BEGIN(func_tag, atag) \ - b.type(t); \ - __CALL_DECLARE_ARG_TO_JSON_SCHEMA_END() - -#define __CALL_DECLARE_PREPARE_STATE_BEGIN(func_tag, atag) \ - template <> \ - struct prepare_state \ - { \ - using arg_tuple = func_tag::args; \ - using arg_tag = func_tag::atag; \ - using call_arg_tuple = convert_arg_type; \ - using call_arg_state_tuple = convert_outer_state; \ - static bool prepare( \ - lhg::ManagerProvider& provider, \ - const json::object& req, \ - call_arg_tuple& arg, \ - call_arg_state_tuple& state) \ - { -#define __CALL_DECLARE_PREPARE_STATE_END() \ - } \ - } \ - ; - -#define __CALL_DECLARE_JSON_TO_ARG_BEGIN(func_tag, atag) \ - template <> \ - struct json_to_arg \ - { \ - using arg_tuple = func_tag::args; \ - using arg_tag = func_tag::atag; \ - using call_arg_tuple = convert_arg_type; \ - using call_arg_state_tuple = convert_outer_state; \ - static bool convert( \ - lhg::ManagerProvider& provider, \ - const json::object& req, \ - call_arg_tuple& arg, \ - call_arg_state_tuple& state) \ - { \ - if constexpr ( \ - !is_input::value || std::is_same_v) { \ - return true; \ - } \ - else { \ - auto name = arg_tag::name; \ - if (!req.contains(name)) { \ - return false; \ - } \ - const json::value& value = req.at(name); -#define __CALL_DECLARE_JSON_TO_ARG_END() \ - } \ - } \ - } \ - ; - -#define __CALL_DECLARE_ARG_TO_JSON_BEGIN(func_tag, atag) \ - template <> \ - struct arg_to_json \ - { \ - using arg_tuple = func_tag::args; \ - using arg_tag = func_tag::atag; \ - using call_arg_tuple = convert_arg_type; \ - using call_arg_state_tuple = convert_outer_state; \ - static void convert( \ - lhg::ManagerProvider& provider, \ - json::object& res, \ - call_arg_tuple& arg, \ - call_arg_state_tuple& state) \ - { \ - if constexpr ( \ - !is_output::value || std::is_same_v) { \ - return; \ - } \ - else { \ - auto name = arg_tag::name; -#define __CALL_DECLARE_ARG_TO_JSON_END() \ - } \ - } \ - } \ - ; - -#pragma endregion call - -#pragma region callback - -#define __CALLBACK_DECLARE_INPUT(arg_tag, v) \ - template <> \ - struct is_input \ - { \ - constexpr static bool value = v; \ - }; - -#define __CALLBACK_DECLARE_OUTPUT(arg_tag, v) \ - template <> \ - struct is_output \ - { \ - constexpr static bool value = v; \ - }; - -#define __CALLBACK_DECLARE_OUTER_STATE(arg_tag, t) \ - template <> \ - struct is_outer_state \ - { \ - constexpr static bool value = true; \ - using type = t; \ - }; - -#define __CALLBACK_DECLARE_HANDLE_OPER(arg_tag, o) \ - template <> \ - struct is_handle \ - { \ - constexpr static bool value = true; \ - constexpr static handle_oper oper = handle_oper::o; \ - }; - -#define __CALLBACK_DECLARE_JSON_TO_ARG_SCHEMA_BEGIN(func_tag, atag) \ - template <> \ - struct json_to_arg_schema \ - { \ - static void get(json::object& req) \ - { \ - using arg_tag = std::tuple_element_t; \ - if constexpr ( \ - !is_output::value || std::is_same_v) { \ - ; \ - } \ - else { \ - auto name = arg_tag::name; \ - schema::Builder b; -#define __CALLBACK_DECLARE_JSON_TO_ARG_SCHEMA_END() \ - req[name] = b.obj; \ - } \ - } \ - } \ - ; - -#define __CALLBACK_DECLARE_JSON_TO_ARG_SCHEMA_TYPE(func_tag, atag, t) \ - __CALLBACK_DECLARE_JSON_TO_ARG_SCHEMA_BEGIN(func_tag, atag) \ - b.type(t); \ - __CALLBACK_DECLARE_JSON_TO_ARG_SCHEMA_END() - -#define __CALLBACK_DECLARE_ARG_TO_JSON_SCHEMA_BEGIN(func_tag, atag) \ - template <> \ - struct arg_to_json_schema \ - { \ - static void get(json::object& res) \ - { \ - using arg_tag = std::tuple_element_t; \ - if constexpr ( \ - !is_input::value || std::is_same_v) { \ - ; \ - } \ - else { \ - auto name = arg_tag::name; \ - schema::Builder b; -#define __CALLBACK_DECLARE_ARG_TO_JSON_SCHEMA_END() \ - res[name] = b.obj; \ - } \ - } \ - } \ - ; - -#define __CALLBACK_DECLARE_ARG_TO_JSON_SCHEMA_TYPE(func_tag, atag, t) \ - __CALLBACK_DECLARE_ARG_TO_JSON_SCHEMA_BEGIN(func_tag, atag) \ - b.type(t); \ - __CALLBACK_DECLARE_ARG_TO_JSON_SCHEMA_END() - -#define __CALLBACK_DECLARE_JSON_TO_ARG_BEGIN(func_tag, atag) \ - template <> \ - struct json_to_arg \ - { \ - using arg_tuple = func_tag::args; \ - using arg_tag = func_tag::atag; \ - using call_arg_tuple = convert_arg_type; \ - using call_arg_state_tuple = convert_outer_state; \ - static bool convert( \ - lhg::ManagerProvider& provider, \ - const json::object& res, \ - call_arg_tuple& arg, \ - call_arg_state_tuple& state) \ - { \ - if constexpr (!is_output::value) { \ - return true; \ - } \ - else { \ - auto name = arg_tag::name; \ - if (!res.contains(name)) { \ - return false; \ - } \ - const json::value& value = res.at(name); -#define __CALLBACK_DECLARE_JSON_TO_ARG_END() \ - } \ - } \ - } \ - ; - -#define __CALLBACK_DECLARE_ARG_TO_JSON_BEGIN(func_tag, atag) \ - template <> \ - struct arg_to_json \ - { \ - using arg_tuple = func_tag::args; \ - using arg_tag = func_tag::atag; \ - using call_arg_tuple = convert_arg_type; \ - using call_arg_state_tuple = convert_outer_state; \ - static void convert( \ - lhg::ManagerProvider& provider, \ - json::object& req, \ - call_arg_tuple& arg, \ - call_arg_state_tuple& state) \ - { \ - if constexpr ( \ - !is_input::value || std::is_same_v) { \ - return; \ - } \ - else { \ - auto name = arg_tag::name; -#define __CALLBACK_DECLARE_ARG_TO_JSON_END() \ - } \ - } \ - } \ - ; - -#pragma endregion callback