Skip to content

Commit

Permalink
refactor: merge code, move macro (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para authored Mar 22, 2024
1 parent d87a9fb commit 5222c64
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 709 deletions.
5 changes: 5 additions & 0 deletions source/MaaHttp/source/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
MacroBlockBegin: '^LHG.*Begin'
MacroBlockEnd: '^LHG.*End'
StatementMacros:
- 'LHGArgToJsonMiddle'
- 'LHGArgFromJsonMiddle'
451 changes: 180 additions & 271 deletions source/MaaHttp/source/spec/call.hpp

Large diffs are not rendered by default.

37 changes: 16 additions & 21 deletions source/MaaHttp/source/spec/call_forward.hpp
Original file line number Diff line number Diff line change
@@ -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);
129 changes: 41 additions & 88 deletions source/MaaHttp/source/spec/callback.hpp
Original file line number Diff line number Diff line change
@@ -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<maa::func_type_CustomActionRun::_3_cur_box::index>(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<maa::func_type_CustomRecognizerAnalyze::_5_out_box::index>(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<maa::func_type_CustomRecognizerAnalyze::_6_out_detail::index>(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<maa::callback_CustomActionRun, true>
{
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<arg_tuple>;
static context_info* get(const call_arg_tuple& arg)
{
auto ptr =
reinterpret_cast<pri_maa::custom_action_context*>(std::get<callback_tag::context>(arg));
return ptr->run.get();
}
};

template <>
struct get_context<maa::callback_CustomActionStop, true>
{
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<arg_tuple>;
static context_info* get(const call_arg_tuple& arg)
{
auto ptr =
reinterpret_cast<pri_maa::custom_action_context*>(std::get<callback_tag::context>(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<index>(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<index>(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<index>(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<pri_maa::custom_action_context *>(
std::get<callback_tag::context>(arg));
return ptr->run.get();
LHGArgGetContextEnd()

LHGArgGetContextBegin(maa::callback_CustomActionStop)
auto ptr = reinterpret_cast<pri_maa::custom_action_context *>(
std::get<callback_tag::context>(arg));
return ptr->stop.get();
LHGArgGetContextEnd()
22 changes: 6 additions & 16 deletions source/MaaHttp/source/spec/callback_forward.hpp
Original file line number Diff line number Diff line change
@@ -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<MaaSyncContextHandle>::ScopedHandle)

__CALLBACK_DECLARE_OUTER_STATE(
HandleManager<MaaSyncContextHandle>::ScopedHandle);
LHGArgOuterState(
maa::func_type_CustomRecognizerAnalyze::_0_sync_context,
HandleManager<MaaSyncContextHandle>::ScopedHandle)

__CALLBACK_DECLARE_OUTER_STATE(
HandleManager<MaaSyncContextHandle>::ScopedHandle);
LHGArgOuterState(
maa::func_type_CustomRecognizerAnalyze::_1_image,
HandleManager<MaaImageBufferHandle>::ScopedHandle)

}; // namespace lhg::callback
HandleManager<MaaImageBufferHandle>::ScopedHandle);
40 changes: 31 additions & 9 deletions source/MaaHttp/source/spec/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,42 @@

#include <memory>

#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<lhg::callback::context_info> run;
std::shared_ptr<lhg::callback::context_info> stop;
struct custom_action_context {
std::shared_ptr<lhg::callback::context_info> run;
std::shared_ptr<lhg::callback::context_info> 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
Loading

0 comments on commit 5222c64

Please sign in to comment.