From 0f893c1ae11c1610cdc3e9cc4a0c92d77b6231d6 Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 10 Nov 2023 00:43:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E6=95=B4?= =?UTF-8?q?=E4=B8=AAControlUnit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 15 +- CMakeLists.txt | 6 +- cmake/grpc-gen.cmake | 2 + cmake/thrift-gen.cmake | 6 +- ...rolUnit.thrift => ThriftController.thrift} | 23 +- include/MaaFramework/MaaDef.h | 9 + .../MaaAdbControlUnit/AdbControlUnitAPI.cpp | 293 ++++++++++ source/MaaAdbControlUnit/ControlUnitMgr.cpp | 546 ------------------ .../API/DbgControlUnitAPI.cpp | 26 +- source/MaaFramework/CMakeLists.txt | 3 - source/MaaRpc/CMakeLists.txt | 17 +- source/MaaRpc/generated/.gitkeep | 0 .../API/ThriftControlUnitAPI.cpp | 35 ++ source/MaaThriftControlUnit/CMakeLists.txt | 33 +- .../impl/ThriftControllerAgent.cpp | 207 +++++++ .../impl/ThriftControllerAgent.h | 38 ++ .../impl/ThriftControllerSocketAgent.cpp | 31 + .../impl/ThriftControllerSocketAgent.h | 13 + .../ThriftControllerUnixDomainSocketAgent.cpp | 31 + .../ThriftControllerUnixDomainSocketAgent.h | 13 + source/include/Conf/Conf.h | 6 + .../include/ControlUnit/AdbControlUnitAPI.h | 51 +- .../include/ControlUnit/DbgControlUnitAPI.h | 19 +- .../ControlUnit/ThriftControlUnitAPI.h | 53 ++ test/grpc/CMakeLists.txt | 7 +- 25 files changed, 854 insertions(+), 629 deletions(-) rename include/Interface/{ThriftControlUnit.thrift => ThriftController.thrift} (74%) create mode 100644 source/MaaAdbControlUnit/AdbControlUnitAPI.cpp delete mode 100644 source/MaaRpc/generated/.gitkeep create mode 100644 source/MaaThriftControlUnit/API/ThriftControlUnitAPI.cpp create mode 100644 source/MaaThriftControlUnit/impl/ThriftControllerAgent.cpp create mode 100644 source/MaaThriftControlUnit/impl/ThriftControllerAgent.h create mode 100644 source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.cpp create mode 100644 source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.h create mode 100644 source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.cpp create mode 100644 source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.h create mode 100644 source/include/ControlUnit/ThriftControlUnitAPI.h diff --git a/.gitignore b/.gitignore index 28e2c2d8f..6167e185f 100644 --- a/.gitignore +++ b/.gitignore @@ -434,16 +434,7 @@ FodyWeavers.xsd .idea/ *.sln.iml -enc_temp_folder/* - -# Nuke -.nuke/temp/* - -# test install dir -install - +# chore .DS_Store - -source/MaaRpc/generated/*.h -source/MaaRpc/generated/*.cc -source/MaaRpc/generated/*.timestamp +.nuke/temp/* +enc_temp_folder/* diff --git a/CMakeLists.txt b/CMakeLists.txt index 55c9b56ae..e6021942b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.24) -project(MAA) +project(MaaFw) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules") @@ -9,12 +9,16 @@ set(CMAKE_MAP_IMPORTED_CONFIG_DebWithRelDeps "DebWithRelDeps;Release;") set(Boost_NO_WARN_NEW_VERSIONS 1) option(USE_MAADEPS "use third-party libraries built by MaaDeps" ON) + option(WITH_ADB_CONTROLLER "build with adb controller" ON) option(WITH_THRIFT_CONTROLLER "build with thrift controller" ON) option(WITH_DBG_CONTROLLER "build with debugging controller" ON) option(WITH_GRPC "build with protobuf and grpc" ON) + option(BUILD_GRPC_CLI "build grpc CLI exec" ON) + option(BUILD_SAMPLE "build a demo" OFF) + option(BUILD_PIPELINE_TESTING "build pipeline testing" OFF) option(BUILD_GRPC_TESTING "build grpc testing" OFF) diff --git a/cmake/grpc-gen.cmake b/cmake/grpc-gen.cmake index 8b3177e99..072ba73a1 100644 --- a/cmake/grpc-gen.cmake +++ b/cmake/grpc-gen.cmake @@ -23,6 +23,8 @@ macro(GENERATE_GRPC_LIB LIB_NAME IN_DIR OUT_DIR GEN_SRCS) set(${LIB_NAME}_INCLUDE_DIRS ${OUT_DIR}) set(${GEN_SRCS} ${SRCS}) + set_target_properties(${LIB_NAME} PROPERTIES FOLDER "Generated") + source_group("generated" FILES ${SRCS}) source_group("proto" FILES ${PROTOS}) endmacro() diff --git a/cmake/thrift-gen.cmake b/cmake/thrift-gen.cmake index 59d80df1e..6fde1243a 100644 --- a/cmake/thrift-gen.cmake +++ b/cmake/thrift-gen.cmake @@ -8,14 +8,16 @@ macro(GENERATE_THRIFT_LIB LIB_NAME FILENAME OUTPUTDIR SOURCES) if(CMD_RESULT) message(FATAL_ERROR "Error generating ${FILENAME} with generator ${GENERATOR}") endif() - file(GLOB_RECURSE GENERATED_SOURCES ${OUTPUTDIR}/*.cpp) - add_library(${LIB_NAME} SHARED ${GENERATED_SOURCES}) + file(GLOB_RECURSE GENERATED_SOURCES ${OUTPUTDIR}/*.cpp ${OUTPUTDIR}/*.h ${OUTPUTDIR}/*.hpp) + add_library(${LIB_NAME} STATIC ${GENERATED_SOURCES} ${FILENAME}) set_target_properties(${LIB_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON) target_link_libraries(${LIB_NAME} thrift::thrift) set(${LIB_NAME}_INCLUDE_DIRS ${OUTPUTDIR} PARENT_SCOPE) set(${SOURCES} ${GENERATED_SOURCES}) + + set_target_properties(${LIB_NAME} PROPERTIES FOLDER "Generated") source_group("generated" FILES ${GENERATED_SOURCES}) source_group("thrift" FILES ${FILENAME}) diff --git a/include/Interface/ThriftControlUnit.thrift b/include/Interface/ThriftController.thrift similarity index 74% rename from include/Interface/ThriftControlUnit.thrift rename to include/Interface/ThriftController.thrift index fc4082534..ba0183755 100644 --- a/include/Interface/ThriftControlUnit.thrift +++ b/include/Interface/ThriftController.thrift @@ -1,4 +1,4 @@ -namespace cpp ThriftControlUnit +namespace cpp ThriftController struct Point { 1: i32 x, @@ -31,15 +31,18 @@ struct Size { } struct CustomImage { - 1: Size size, - 2: i32 type, - 3: binary data, + 1: binary png_data, } -service ThriftControlUnit { - bool set_option(1: string key, 2: string value), - +service ThriftController { bool connect(), + + string get_uuid(), + Size get_resolution(), + + bool start_app(1: string entry), + bool stop_app(1: string entry), + bool click(1: ClickParam param), bool swipe(1: SwipeParam param), @@ -49,11 +52,5 @@ service ThriftControlUnit { bool press_key(1: PressKeyParam param), - bool start_game(1: string activity), - bool stop_game(1: string activity), - - Size get_resolution(), - - string get_uuid(), CustomImage screencap(), } diff --git a/include/MaaFramework/MaaDef.h b/include/MaaFramework/MaaDef.h index fe95809c0..95e8eaac8 100644 --- a/include/MaaFramework/MaaDef.h +++ b/include/MaaFramework/MaaDef.h @@ -156,6 +156,15 @@ enum MaaDbgControllerTypeEnum MaaDbgControllerType_ReplayRecording = 2, }; +typedef int32_t MaaThriftControllerType; +enum MaaThriftControllerTypeEnum +{ + MaaThriftController_Invalid = 0, + + MaaThriftControllerType_Socket = 1, + MaaThriftControllerType_UnixDomainSocket = 2, +}; + typedef void* MaaTransparentArg; typedef MaaTransparentArg MaaCallbackTransparentArg; diff --git a/source/MaaAdbControlUnit/AdbControlUnitAPI.cpp b/source/MaaAdbControlUnit/AdbControlUnitAPI.cpp new file mode 100644 index 000000000..d70d11470 --- /dev/null +++ b/source/MaaAdbControlUnit/AdbControlUnitAPI.cpp @@ -0,0 +1,293 @@ +#include "ControlUnit/AdbControlUnitAPI.h" + +#include + +#include "ControlUnitMgr.h" +#include "General/Activity.h" +#include "General/Connection.h" +#include "General/DeviceInfo.h" +#include "General/DeviceList.h" +#include "Input/MaatouchInput.h" +#include "Input/MinitouchInput.h" +#include "Input/TapInput.h" +#include "Platform/PlatformFactory.h" +#include "Screencap/Encode.h" +#include "Screencap/EncodeToFile.h" +#include "Screencap/FastestWay.h" +#include "Screencap/Minicap/MinicapDirect.h" +#include "Screencap/Minicap/MinicapStream.h" +#include "Screencap/RawByNetcat.h" +#include "Screencap/RawWithGzip.h" +#include "Utils/Logger.h" + +using namespace MAA_ADB_CTRL_UNIT_NS; +using MAA_NS::path; + +MaaStringView get_version() +{ +#pragma message("MaaAdbControlUnit MAA_VERSION: " MAA_VERSION) + + return MAA_VERSION; +} + +MaaBool create_controller( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + MaaStringView agent_path, + /*out*/ std::shared_ptr& out_handle) +{ + LogFunc << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path); + + std::shared_ptr touch_unit = nullptr; + std::shared_ptr key_unit = nullptr; + std::shared_ptr screencap_unit = nullptr; + + auto touch_type = type & MaaAdbControllerType_Touch_Mask; + auto key_type = type & MaaAdbControllerType_Key_Mask; + auto screencap_type = type & MaaAdbControllerType_Screencap_Mask; + + std::shared_ptr maatouch_unit = nullptr; + + auto agent_stdpath = path(agent_path); + auto minitouch_path = agent_stdpath / path("minitouch"); + auto maatouch_path = agent_stdpath / path("maatouch"); + auto minicap_path = agent_stdpath / path("minicap"); + + switch (touch_type) { + case MaaAdbControllerType_Touch_Adb: + LogInfo << "touch_type: TapTouchInput"; + touch_unit = std::make_shared(); + break; + case MaaAdbControllerType_Touch_MiniTouch: + LogInfo << "touch_type: MinitouchInput"; + if (!std::filesystem::exists(minitouch_path)) { + LogError << "minitouch path not exists" << VAR(minitouch_path); + return false; + } + touch_unit = std::make_shared(minitouch_path); + break; + case MaaAdbControllerType_Touch_MaaTouch: + LogInfo << "touch_type: MaatouchInput"; + if (!std::filesystem::exists(maatouch_path)) { + LogError << "maatouch path not exists" << VAR(maatouch_path); + return false; + } + if (!maatouch_unit) { + maatouch_unit = std::make_shared(maatouch_path); + } + touch_unit = maatouch_unit; + break; + default: + LogError << "Unknown touch input type" << VAR(touch_type); + return false; + } + + switch (key_type) { + case MaaAdbControllerType_Key_Adb: + LogInfo << "key_type: TapKeyInput"; + key_unit = std::make_shared(); + break; + case MaaAdbControllerType_Key_MaaTouch: + LogInfo << "key_type: MaatouchInput"; + if (!std::filesystem::exists(maatouch_path)) { + LogError << "maatouch path not exists" << VAR(maatouch_path); + return false; + } + if (!maatouch_unit) { + maatouch_unit = std::make_shared(maatouch_path); + } + key_unit = maatouch_unit; + break; + default: + LogError << "Unknown key input type" << VAR(key_type); + return false; + } + + switch (screencap_type) { + case MaaAdbControllerType_Screencap_FastestWay: + LogInfo << "screencap_type: ScreencapFastestWay"; + if (!std::filesystem::exists(minicap_path)) { + LogError << "minicap path not exists" << VAR(minicap_path); + return false; + } + screencap_unit = std::make_shared(minicap_path); + break; + case MaaAdbControllerType_Screencap_RawByNetcat: + LogInfo << "screencap_type: ScreencapRawByNetcat"; + screencap_unit = std::make_shared(); + break; + case MaaAdbControllerType_Screencap_RawWithGzip: + LogInfo << "screencap_type: ScreencapRawWithGzip"; + screencap_unit = std::make_shared(); + break; + case MaaAdbControllerType_Screencap_Encode: + LogInfo << "screencap_type: ScreencapEncode"; + screencap_unit = std::make_shared(); + break; + case MaaAdbControllerType_Screencap_EncodeToFile: + LogInfo << "screencap_type: ScreencapEncodeToFile"; + screencap_unit = std::make_shared(); + break; + case MaaAdbControllerType_Screencap_MinicapDirect: + LogInfo << "screencap_type: MinicapDirect"; + if (!std::filesystem::exists(minicap_path)) { + LogError << "minicap path not exists" << VAR(minicap_path); + return false; + } + screencap_unit = std::make_shared(minicap_path); + break; + case MaaAdbControllerType_Screencap_MinicapStream: + LogInfo << "screencap_type: MinicapStream"; + if (!std::filesystem::exists(minicap_path)) { + LogError << "minicap path not exists" << VAR(minicap_path); + return false; + } + screencap_unit = std::make_shared(minicap_path); + break; + default: + LogError << "Unknown screencap type" << VAR(screencap_type); + return false; + } + + auto unit_mgr = std::make_shared(); + + unit_mgr->set_connection_obj(std::make_shared()); + unit_mgr->set_device_info_obj(std::make_shared()); + unit_mgr->set_activity_obj(std::make_shared()); + unit_mgr->set_touch_input_obj(touch_unit); + unit_mgr->set_key_input_obj(key_unit); + unit_mgr->set_screencap_obj(screencap_unit); + + auto json_opt = json::parse(std::string_view(config)); + if (!json_opt) { + LogError << "Parse config failed, invalid config:" << config; + return false; + } + bool parsed = unit_mgr->parse(*json_opt); + if (!parsed) { + LogError << "Parse json failed, invalid json:" << *json_opt; + return false; + } + + auto platform_io = PlatformFactory::create(); + if (!platform_io) { + LogError << "Create platform io failed"; + return false; + } + unit_mgr->set_io(platform_io); + + unit_mgr->set_replacement({ + { "{ADB}", adb_path }, + { "{ADB_SERIAL}", adb_serial }, + }); + + out_handle = std::move(unit_mgr); + return true; +} + +MaaBool create_device_list_obj( // + MaaStringView adb_path, MaaStringView config, + /*out*/ std::shared_ptr& out_handle) +{ + LogFunc << VAR(adb_path) << VAR(config); + + auto device_list_mgr = std::make_shared(); + + auto json_opt = json::parse(std::string_view(config)); + if (!json_opt) { + LogError << "Parse config failed, invalid config:" << config; + return false; + } + bool parsed = device_list_mgr->parse(*json_opt); + if (!parsed) { + LogError << "Parse json failed, invalid json:" << *json_opt; + return false; + } + + auto platform_io = PlatformFactory::create(); + if (!platform_io) { + LogError << "Create platform io failed"; + return false; + } + device_list_mgr->set_io(platform_io); + + device_list_mgr->set_replacement({ + { "{ADB}", adb_path }, + }); + + out_handle = std::move(device_list_mgr); + return true; +} + +MaaBool create_connection( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + /*out*/ std::shared_ptr& out_handle) +{ + LogFunc << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config); + + std::ignore = type; + + auto json_opt = json::parse(std::string_view(config)); + if (!json_opt) { + LogError << "Parse config failed, invalid config:" << config; + return false; + } + + auto connection = std::make_shared(); + + bool parsed = connection->parse(*json_opt); + if (!parsed) { + LogError << "Parse json failed, invalid json:" << *json_opt; + return false; + } + + auto platform_io = PlatformFactory::create(); + if (!platform_io) { + LogError << "Create platform io failed"; + return false; + } + connection->set_io(platform_io); + connection->set_replacement({ + { "{ADB}", adb_path }, + { "{ADB_SERIAL}", adb_serial }, + }); + + out_handle = std::move(connection); + return true; +} + +MaaBool create_device_info( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + /*out*/ std::shared_ptr& out_handle) +{ + LogFunc << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config); + + std::ignore = type; + + auto json_opt = json::parse(std::string_view(config)); + if (!json_opt) { + LogError << "Parse config failed, invalid config:" << config; + return false; + } + + auto device_info = std::make_shared(); + + bool parsed = device_info->parse(*json_opt); + if (!parsed) { + LogError << "Parse json failed, invalid json:" << *json_opt; + return false; + } + + auto platform_io = PlatformFactory::create(); + if (!platform_io) { + LogError << "Create platform io failed"; + return false; + } + device_info->set_io(platform_io); + device_info->set_replacement({ + { "{ADB}", adb_path }, + { "{ADB_SERIAL}", adb_serial }, + }); + + out_handle = std::move(device_info); + return true; +} diff --git a/source/MaaAdbControlUnit/ControlUnitMgr.cpp b/source/MaaAdbControlUnit/ControlUnitMgr.cpp index 2e453299c..edf8aee9e 100644 --- a/source/MaaAdbControlUnit/ControlUnitMgr.cpp +++ b/source/MaaAdbControlUnit/ControlUnitMgr.cpp @@ -2,25 +2,8 @@ #include -#include "General/Activity.h" -#include "General/Connection.h" -#include "General/DeviceInfo.h" -#include "General/DeviceList.h" -#include "Input/MaatouchInput.h" -#include "Input/MinitouchInput.h" -#include "Input/TapInput.h" -#include "Platform/PlatformFactory.h" -#include "Screencap/Encode.h" -#include "Screencap/EncodeToFile.h" -#include "Screencap/FastestWay.h" -#include "Screencap/Minicap/MinicapDirect.h" -#include "Screencap/Minicap/MinicapStream.h" -#include "Screencap/RawByNetcat.h" -#include "Screencap/RawWithGzip.h" #include "Utils/Logger.h" -#pragma message("MaaControlUnit MAA_VERSION: " MAA_VERSION) - MAA_ADB_CTRL_UNIT_NS_BEGIN bool ControlUnitMgr::parse(const json::value& config) @@ -81,533 +64,4 @@ void ControlUnitMgr::set_replacement(const std::map& r } } -void create_device_list_obj(std::shared_ptr* result, MaaStringView adb_path, MaaStringView config) -{ - LogFunc << VAR(result) << VAR(adb_path) << VAR(config); - - if (!result) { - return; - } - *result = nullptr; - - auto device_list_mgr = std::make_shared(); - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return; - } - bool parsed = device_list_mgr->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return; - } - device_list_mgr->set_io(platform_io); - - device_list_mgr->set_replacement({ - { "{ADB}", adb_path }, - }); - - *result = device_list_mgr; -} - -void create_controller_unit(std::shared_ptr* result, MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, MaaStringView agent_path) -{ - LogFunc << VAR(result) << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path); - - if (!result) { - return; - } - - *result = nullptr; - - std::shared_ptr touch_unit = nullptr; - std::shared_ptr key_unit = nullptr; - std::shared_ptr screencap_unit = nullptr; - - auto touch_type = type & MaaAdbControllerType_Touch_Mask; - auto key_type = type & MaaAdbControllerType_Key_Mask; - auto screencap_type = type & MaaAdbControllerType_Screencap_Mask; - - std::shared_ptr maatouch_unit = nullptr; - - auto agent_stdpath = path(agent_path); - auto minitouch_path = agent_stdpath / path("minitouch"); - auto maatouch_path = agent_stdpath / path("maatouch"); - auto minicap_path = agent_stdpath / path("minicap"); - - switch (touch_type) { - case MaaAdbControllerType_Touch_Adb: - LogInfo << "touch_type: TapTouchInput"; - touch_unit = std::make_shared(); - break; - case MaaAdbControllerType_Touch_MiniTouch: - LogInfo << "touch_type: MinitouchInput"; - if (!std::filesystem::exists(minitouch_path)) { - LogError << "minitouch path not exists" << VAR(minitouch_path); - return; - } - touch_unit = std::make_shared(minitouch_path); - break; - case MaaAdbControllerType_Touch_MaaTouch: - LogInfo << "touch_type: MaatouchInput"; - if (!std::filesystem::exists(maatouch_path)) { - LogError << "maatouch path not exists" << VAR(maatouch_path); - return; - } - if (!maatouch_unit) { - maatouch_unit = std::make_shared(maatouch_path); - } - touch_unit = maatouch_unit; - break; - default: - LogError << "Unknown touch input type" << VAR(touch_type); - return; - } - - switch (key_type) { - case MaaAdbControllerType_Key_Adb: - LogInfo << "key_type: TapKeyInput"; - key_unit = std::make_shared(); - break; - case MaaAdbControllerType_Key_MaaTouch: - LogInfo << "key_type: MaatouchInput"; - if (!std::filesystem::exists(maatouch_path)) { - LogError << "maatouch path not exists" << VAR(maatouch_path); - return; - } - if (!maatouch_unit) { - maatouch_unit = std::make_shared(maatouch_path); - } - key_unit = maatouch_unit; - break; - default: - LogError << "Unknown key input type" << VAR(key_type); - return; - } - - switch (screencap_type) { - case MaaAdbControllerType_Screencap_FastestWay: - LogInfo << "screencap_type: ScreencapFastestWay"; - if (!std::filesystem::exists(minicap_path)) { - LogError << "minicap path not exists" << VAR(minicap_path); - return; - } - screencap_unit = std::make_shared(minicap_path); - break; - case MaaAdbControllerType_Screencap_RawByNetcat: - LogInfo << "screencap_type: ScreencapRawByNetcat"; - screencap_unit = std::make_shared(); - break; - case MaaAdbControllerType_Screencap_RawWithGzip: - LogInfo << "screencap_type: ScreencapRawWithGzip"; - screencap_unit = std::make_shared(); - break; - case MaaAdbControllerType_Screencap_Encode: - LogInfo << "screencap_type: ScreencapEncode"; - screencap_unit = std::make_shared(); - break; - case MaaAdbControllerType_Screencap_EncodeToFile: - LogInfo << "screencap_type: ScreencapEncodeToFile"; - screencap_unit = std::make_shared(); - break; - case MaaAdbControllerType_Screencap_MinicapDirect: - LogInfo << "screencap_type: MinicapDirect"; - if (!std::filesystem::exists(minicap_path)) { - LogError << "minicap path not exists" << VAR(minicap_path); - return; - } - screencap_unit = std::make_shared(minicap_path); - break; - case MaaAdbControllerType_Screencap_MinicapStream: - LogInfo << "screencap_type: MinicapStream"; - if (!std::filesystem::exists(minicap_path)) { - LogError << "minicap path not exists" << VAR(minicap_path); - return; - } - screencap_unit = std::make_shared(minicap_path); - break; - default: - LogError << "Unknown screencap type" << VAR(screencap_type); - return; - } - - auto unit_mgr = std::make_shared(); - - unit_mgr->set_connection_obj(std::make_shared()); - unit_mgr->set_device_info_obj(std::make_shared()); - unit_mgr->set_activity_obj(std::make_shared()); - unit_mgr->set_touch_input_obj(touch_unit); - unit_mgr->set_key_input_obj(key_unit); - unit_mgr->set_screencap_obj(screencap_unit); - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return; - } - bool parsed = unit_mgr->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return; - } - unit_mgr->set_io(platform_io); - - unit_mgr->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - *result = unit_mgr; -} - -void create_connection(std::shared_ptr* result, MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config) -{ - LogFunc << VAR(result) << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config); - - std::ignore = type; - - if (!result) { - return; - } - - *result = nullptr; - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return; - } - - auto connection = std::make_shared(); - - bool parsed = connection->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return; - } - connection->set_io(platform_io); - connection->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - *result = connection; -} - -void create_device_info(std::shared_ptr* result, MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config) -{ - LogFunc << VAR(result) << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config); - - std::ignore = type; - - if (!result) { - return; - } - - *result = nullptr; - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return; - } - - auto device_info = std::make_shared(); - - bool parsed = device_info->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return; - } - device_info->set_io(platform_io); - device_info->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - *result = device_info; -} - -void create_activity(std::shared_ptr* result, MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config) -{ - LogFunc << VAR(result) << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config); - - std::ignore = type; - - if (!result) { - return; - } - - *result = nullptr; - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return; - } - - auto activity = std::make_shared(); - - bool parsed = activity->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return; - } - activity->set_io(platform_io); - activity->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - *result = activity; -} - -void create_touch_input(std::shared_ptr* result, MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, MaaStringView agent_path) -{ - LogFunc << VAR(result) << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path); - - if (!result) { - return; - } - - *result = nullptr; - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return; - } - - auto agent_stdpath = path(agent_path); - auto minitouch_path = agent_stdpath / path("minitouch"); - auto maatouch_path = agent_stdpath / path("maatouch"); - - std::shared_ptr touch_unit = nullptr; - switch (type & MaaAdbControllerType_Touch_Mask) { - case MaaAdbControllerType_Touch_Adb: - LogInfo << "touch_type: TapInput"; - touch_unit = std::make_shared(); - break; - case MaaAdbControllerType_Touch_MiniTouch: - LogInfo << "touch_type: MinitouchInput"; - if (!std::filesystem::exists(minitouch_path)) { - LogError << "minitouch path not exists" << VAR(minitouch_path); - return; - } - touch_unit = std::make_shared(minitouch_path); - break; - case MaaAdbControllerType_Touch_MaaTouch: - LogInfo << "touch_type: MaatouchInput"; - if (!std::filesystem::exists(maatouch_path)) { - LogError << "maatouch path not exists" << VAR(maatouch_path); - return; - } - touch_unit = std::make_shared(maatouch_path); - break; - default: - LogError << "Unknown touch input type" << VAR(type); - return; - } - - bool parsed = touch_unit->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return; - } - touch_unit->set_io(platform_io); - touch_unit->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - *result = touch_unit; -} - -void create_key_input(std::shared_ptr* result, MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, MaaStringView agent_path) -{ - LogFunc << VAR(result) << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path); - - if (!result) { - return; - } - - *result = nullptr; - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return; - } - auto agent_stdpath = path(agent_path); - auto maatouch_path = agent_stdpath / path("maatouch"); - - std::shared_ptr key_unit = nullptr; - switch (type & MaaAdbControllerType_Key_Mask) { - case MaaAdbControllerType_Key_Adb: - LogInfo << "key_type: TapKeyInput"; - key_unit = std::make_shared(); - break; - case MaaAdbControllerType_Key_MaaTouch: - LogInfo << "key_type: MaatouchInput"; - if (!std::filesystem::exists(maatouch_path)) { - LogError << "maatouch path not exists" << VAR(maatouch_path); - return; - } - key_unit = std::make_shared(maatouch_path); - break; - default: - LogError << "Unknown key input type" << VAR(type); - return; - } - - bool parsed = key_unit->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return; - } - key_unit->set_io(platform_io); - key_unit->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - *result = key_unit; -} - -void create_screencap(std::shared_ptr* result, MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, MaaStringView agent_path) -{ - LogFunc << VAR(result) << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path); - - if (!result) { - return; - } - - *result = nullptr; - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return; - } - - auto agent_stdpath = path(agent_path); - auto minicap_path = agent_stdpath / path("minicap"); - - std::shared_ptr screencap_unit = nullptr; - switch (type & MaaAdbControllerType_Screencap_Mask) { - case MaaAdbControllerType_Screencap_FastestWay: - LogInfo << "screencap_type: ScreencapFastestWay"; - if (!std::filesystem::exists(minicap_path)) { - LogError << "minicap path not exists" << VAR(minicap_path); - return; - } - screencap_unit = std::make_shared(minicap_path); - break; - case MaaAdbControllerType_Screencap_RawByNetcat: - LogInfo << "screencap_type: ScreencapRawByNetcat"; - screencap_unit = std::make_shared(); - break; - case MaaAdbControllerType_Screencap_RawWithGzip: - LogInfo << "screencap_type: ScreencapRawWithGzip"; - screencap_unit = std::make_shared(); - break; - case MaaAdbControllerType_Screencap_Encode: - LogInfo << "screencap_type: ScreencapEncode"; - screencap_unit = std::make_shared(); - break; - case MaaAdbControllerType_Screencap_EncodeToFile: - LogInfo << "screencap_type: ScreencapEncodeToFile"; - screencap_unit = std::make_shared(); - break; - case MaaAdbControllerType_Screencap_MinicapDirect: - LogInfo << "screencap_type: MinicapDirect"; - if (!std::filesystem::exists(minicap_path)) { - LogError << "minicap path not exists" << VAR(minicap_path); - return; - } - screencap_unit = std::make_shared(minicap_path); - break; - case MaaAdbControllerType_Screencap_MinicapStream: - LogInfo << "screencap_type: MinicapStream"; - if (!std::filesystem::exists(minicap_path)) { - LogError << "minicap path not exists" << VAR(minicap_path); - return; - } - screencap_unit = std::make_shared(minicap_path); - break; - default: - LogError << "Unknown screencap type" << VAR(type); - return; - } - - bool parsed = screencap_unit->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return; - } - screencap_unit->set_io(platform_io); - screencap_unit->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - *result = screencap_unit; -} - MAA_ADB_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp b/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp index e9ef201b7..e95cc0731 100644 --- a/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp +++ b/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp @@ -6,17 +6,25 @@ #include "ReplayRecording/ReplayRecordingMgr.h" #include "Utils/Logger.h" -MAA_DBG_CTRL_UNIT_NS_BEGIN +using namespace MAA_DBG_CTRL_UNIT_NS; -std::shared_ptr create_controller(MaaDbgControllerType type, MaaStringView read_path, - MaaStringView write_path, MaaStringView config) +MaaStringView get_version() +{ +#pragma message("MaaDbgControlUnit MAA_VERSION: " MAA_VERSION) + + return MAA_VERSION; +} + +MaaBool create_controller( // + MaaDbgControllerType type, MaaStringView read_path, MaaStringView write_path, MaaStringView config, + std::shared_ptr& out_handle) { LogFunc << VAR(type) << VAR(read_path) << VAR(write_path) << VAR(config); auto config_parsed = json::parse(config); if (!config_parsed) { LogError << "Failed to parse config" << VAR(config); - return nullptr; + return false; } auto read_stdpath = MAA_NS::path(read_path); @@ -24,12 +32,12 @@ std::shared_ptr create_controller(MaaDbgControllerType type, MaaS switch (type) { case MaaDbgControllerType_CarouselImage: - return std::make_shared(read_stdpath); + out_handle = std::make_shared(read_stdpath); + return true; case MaaDbgControllerType_ReplayRecording: - return create_replay_recording(read_stdpath); + out_handle = create_replay_recording(read_stdpath); + return true; } - return nullptr; + return false; } - -MAA_DBG_CTRL_UNIT_NS_END diff --git a/source/MaaFramework/CMakeLists.txt b/source/MaaFramework/CMakeLists.txt index 614b411f3..5ac5cbf65 100644 --- a/source/MaaFramework/CMakeLists.txt +++ b/source/MaaFramework/CMakeLists.txt @@ -13,9 +13,6 @@ target_include_directories( target_compile_definitions(MaaFramework PRIVATE MAA_FRAMEWORK_EXPORTS) target_link_libraries(MaaFramework MaaUtils) -if(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE) - target_link_libraries(MaaFramework MaaThriftControlUnit) -endif(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE) target_link_libraries(MaaFramework ${OpenCV_LIBS} fastdeploy_ppocr ONNXRuntime::ONNXRuntime HeaderOnlyLibraries) # clang 15之后有ranges if (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") find_package(range-v3 REQUIRED) diff --git a/source/MaaRpc/CMakeLists.txt b/source/MaaRpc/CMakeLists.txt index 912849b4c..19d69a58d 100644 --- a/source/MaaRpc/CMakeLists.txt +++ b/source/MaaRpc/CMakeLists.txt @@ -1,17 +1,20 @@ -generate_grpc_lib(MaaRpcProto ${CMAKE_SOURCE_DIR}/include/Interface/proto ${CMAKE_CURRENT_SOURCE_DIR}/generated - maa_proto_src) +generate_grpc_lib( + RpcProto + ${CMAKE_SOURCE_DIR}/include/Interface/proto + ${CMAKE_CURRENT_BINARY_DIR}/generated + maa_rpc_proto_src) -file(GLOB maa_proto ../../include/Interface/proto/*.proto) -file(GLOB maa_rpc_src implement/* implement/* API/*) -file(GLOB maa_rpc_header ../../include/MaaRpc/*) +file(GLOB_RECURSE maa_rpc_src implement/* implement/* API/*) +file(GLOB_RECURSE maa_rpc_header ../../include/MaaRpc/*) add_library(MaaRpc SHARED ${maa_rpc_src} ${maa_rpc_header}) target_compile_definitions(MaaRpc PRIVATE MAA_RPC_EXPORTS) target_include_directories( MaaRpc INTERFACE ../../include - PRIVATE . ../include ../../include ${CMAKE_CURRENT_SOURCE_DIR}/generated) -target_link_libraries(MaaRpc PUBLIC MaaRpcProto MaaUtils MaaToolKit protobuf::libprotobuf gRPC::grpc++) + PRIVATE . ../include ../../include + ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(MaaRpc PUBLIC RpcProto MaaUtils MaaToolKit protobuf::libprotobuf gRPC::grpc++) install( TARGETS MaaRpc diff --git a/source/MaaRpc/generated/.gitkeep b/source/MaaRpc/generated/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/source/MaaThriftControlUnit/API/ThriftControlUnitAPI.cpp b/source/MaaThriftControlUnit/API/ThriftControlUnitAPI.cpp new file mode 100644 index 000000000..550b13b3c --- /dev/null +++ b/source/MaaThriftControlUnit/API/ThriftControlUnitAPI.cpp @@ -0,0 +1,35 @@ +#include "ControlUnit/ThriftControlUnitAPI.h" + +#include + +#include "Utils/Logger.h" +#include "impl/ThriftControllerSocketAgent.h" +#include "impl/ThriftControllerUnixDomainSocketAgent.h" + +using namespace MAA_THRIFT_CTRL_UNIT_NS; + +MaaStringView get_version() +{ +#pragma message("MaaThriftControlUnit MAA_VERSION: " MAA_VERSION) + + return MAA_VERSION; +} + +MaaBool create_controller( // + MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config, + std::shared_ptr& out_handle) +{ + LogFunc << VAR(type) << VAR(host) << VAR(port) << VAR(config); + + switch (type) { + case MaaThriftControllerType_Socket: + out_handle = std::make_shared(host, port); + return true; + case MaaThriftControllerType_UnixDomainSocket: + out_handle = std::make_shared(host); + return true; + } + + LogError << "Unknown controller type: " << type; + return false; +} diff --git a/source/MaaThriftControlUnit/CMakeLists.txt b/source/MaaThriftControlUnit/CMakeLists.txt index 35e0eff10..d777c2226 100644 --- a/source/MaaThriftControlUnit/CMakeLists.txt +++ b/source/MaaThriftControlUnit/CMakeLists.txt @@ -1,4 +1,31 @@ -generate_thrift_lib(MaaThriftControlUnit ${PROJECT_SOURCE_DIR}/include/Interface/ThriftControlUnit.thrift - ${CMAKE_CURRENT_BINARY_DIR}/MaaThriftControlUnit maa_thrift_controlunit_src) +generate_thrift_lib( + ThriftControlUnitThrift + ${PROJECT_SOURCE_DIR}/include/Interface/ThriftController.thrift + ${CMAKE_CURRENT_BINARY_DIR}/generated + maa_thrift_controlunit_thrift_src) -source_group(TREE ${CMAKE_CURRENT_BINARY_DIR} FILES ${maa_thrift_controlunit_src}) +file(GLOB_RECURSE maa_thrift_control_unit_src *.h *.hpp *.cpp) +file(GLOB_RECURSE maa_thrift_control_unit_header ../include/ControlUnit/ThriftControlUnitAPI.h) + +add_library(MaaThriftControlUnit SHARED ${maa_thrift_control_unit_src} ${maa_thrift_control_unit_header}) + +target_include_directories(MaaThriftControlUnit PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../include + ${CMAKE_CURRENT_BINARY_DIR}) + +target_link_libraries(MaaThriftControlUnit MaaUtils ThriftControlUnitThrift HeaderOnlyLibraries ${OpenCV_LIBS}) + +target_compile_definitions(MaaThriftControlUnit PRIVATE MAA_CONTROL_UNIT_EXPORTS) + +add_dependencies(MaaThriftControlUnit MaaUtils) + +install( + TARGETS MaaThriftControlUnit + RUNTIME DESTINATION bin + LIBRARY DESTINATION bin + #ARCHIVE DESTINATION lib + ) + +source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${maa_thrift_control_unit_src}) diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerAgent.cpp b/source/MaaThriftControlUnit/impl/ThriftControllerAgent.cpp new file mode 100644 index 000000000..347e39cf9 --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerAgent.cpp @@ -0,0 +1,207 @@ +#include "ThriftControllerAgent.h" + +#include "Utils/Logger.h" +#include "Utils/NoWarningCV.hpp" + +MAA_THRIFT_CTRL_UNIT_NS_BEGIN + +ThriftControllerAgent::~ThriftControllerAgent() +{ + LogFunc; + + if (transport_) { + transport_->close(); + } +} + +bool ThriftControllerAgent::connect() +{ + LogFunc; + + if (!client_ || !transport_) { + LogError << "client_ or transport_ is nullptr or transport_ is not open"; + return {}; + } + + try { + transport_->open(); + } + catch (const std::exception& e) { + LogError << "transport_->open() failed: " << e.what(); + return false; + } + + return client_->connect(); +} + +std::string ThriftControllerAgent::uuid() const +{ + LogFunc; + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return {}; + } + + std::string uuid; + client_->get_uuid(uuid); + return uuid; +} + +cv::Size ThriftControllerAgent::resolution() const +{ + LogFunc; + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return {}; + } + + ThriftController::Size resolution; + client_->get_resolution(resolution); + return { resolution.width, resolution.height }; +} + +bool ThriftControllerAgent::start_app(const std::string& intent) +{ + LogFunc << VAR(intent); + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + return client_->start_app(intent); +} + +bool ThriftControllerAgent::stop_app(const std::string& intent) +{ + LogFunc << VAR(intent); + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + return client_->stop_app(intent); +} + +bool ThriftControllerAgent::click(int x, int y) +{ + LogFunc << VAR(x) << VAR(y); + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + ThriftController::ClickParam thrift_param; + thrift_param.point.x = x; + thrift_param.point.y = y; + + return client_->click(thrift_param); +} + +bool ThriftControllerAgent::swipe(int x1, int y1, int x2, int y2, int duration) +{ + LogFunc << VAR(x1) << VAR(x2) << VAR(y1) << VAR(y2) << VAR(duration); + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + ThriftController::SwipeParam thrift_param; + thrift_param.point1.x = x1; + thrift_param.point1.y = y1; + thrift_param.point2.x = x2; + thrift_param.point2.y = y2; + thrift_param.duration = duration; + return client_->swipe(thrift_param); +} + +bool ThriftControllerAgent::touch_down(int contact, int x, int y, int pressure) +{ + LogFunc << VAR(contact) << VAR(x) << VAR(y) << VAR(pressure); + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + ThriftController::TouchParam thrift_param; + thrift_param.contact = contact; + thrift_param.point.x = x; + thrift_param.point.y = y; + thrift_param.pressure = pressure; + + return client_->touch_down(thrift_param); +} + +bool ThriftControllerAgent::touch_move(int contact, int x, int y, int pressure) +{ + LogFunc << VAR(contact) << VAR(x) << VAR(y) << VAR(pressure); + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + ThriftController::TouchParam thrift_param; + thrift_param.contact = contact; + thrift_param.point.x = x; + thrift_param.point.y = y; + thrift_param.pressure = pressure; + + return client_->touch_move(thrift_param); +} + +bool ThriftControllerAgent::touch_up(int contact) +{ + LogFunc << VAR(contact); + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + ThriftController::TouchParam thrift_param; + thrift_param.contact = contact; + + return client_->touch_up(thrift_param); +} + +bool ThriftControllerAgent::press_key(int key) +{ + LogFunc << VAR(key); + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + ThriftController::PressKeyParam thrift_param; + thrift_param.keycode = key; + + return client_->press_key(thrift_param); +} + +std::optional ThriftControllerAgent::screencap() +{ + LogFunc; + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return std::nullopt; + } + + ThriftController::CustomImage image; + client_->screencap(image); + if (image.png_data.empty()) { + LogError << "image.data is empty"; + return std::nullopt; + } + + return cv::imdecode({ image.png_data.data(), static_cast(image.png_data.size()) }, cv::IMREAD_COLOR); +} + +MAA_THRIFT_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerAgent.h b/source/MaaThriftControlUnit/impl/ThriftControllerAgent.h new file mode 100644 index 000000000..f2b92ee36 --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerAgent.h @@ -0,0 +1,38 @@ +#pragma once + +#include "ControlUnit/ThriftControlUnitAPI.h" + +#include "generated/ThriftController.h" + +MAA_THRIFT_CTRL_UNIT_NS_BEGIN + +class ThriftControllerAgent : public ControllerAPI +{ +public: + virtual ~ThriftControllerAgent(); + +public: // from ControllerAPI + virtual bool connect() override; + + virtual std::string uuid() const override; + virtual cv::Size resolution() const override; + + virtual bool start_app(const std::string& entry) override; + virtual bool stop_app(const std::string& entry) override; + + virtual bool click(int x, int y) override; + virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override; + + virtual bool touch_down(int contact, int x, int y, int pressure) override; + virtual bool touch_move(int contact, int x, int y, int pressure) override; + virtual bool touch_up(int contact) override; + + virtual bool press_key(int key) override; + + virtual std::optional screencap() override; + +protected: + std::shared_ptr client_ = nullptr; + std::shared_ptr transport_ = nullptr; +}; +MAA_THRIFT_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.cpp b/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.cpp new file mode 100644 index 000000000..09588169c --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.cpp @@ -0,0 +1,31 @@ +#include "ThriftControllerSocketAgent.h" + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4245 4706) +#endif +#include +#include +#include +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +#include "Utils/Logger.h" + +MAA_THRIFT_CTRL_UNIT_NS_BEGIN + +ThriftControllerSocketAgent::ThriftControllerSocketAgent(const std::string& host, int port) +{ + LogFunc << VAR(host) << VAR(port); + + using namespace apache::thrift; + + std::shared_ptr socket = std::make_shared(host, port); + + transport_ = std::make_shared(socket); + auto protocol = std::make_shared(transport_); + client_ = std::make_shared(protocol); +} + +MAA_THRIFT_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.h b/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.h new file mode 100644 index 000000000..85e8dd511 --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.h @@ -0,0 +1,13 @@ +#pragma once + +#include "ThriftControllerAgent.h" + +MAA_THRIFT_CTRL_UNIT_NS_BEGIN + +class ThriftControllerSocketAgent : public ThriftControllerAgent +{ +public: + ThriftControllerSocketAgent(const std::string& host, int port); + virtual ~ThriftControllerSocketAgent() = default; +}; +MAA_THRIFT_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.cpp b/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.cpp new file mode 100644 index 000000000..8861a5075 --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.cpp @@ -0,0 +1,31 @@ +#include "ThriftControllerUnixDomainSocketAgent.h" + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4245 4706) +#endif +#include +#include +#include +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +#include "Utils/Logger.h" + +MAA_THRIFT_CTRL_UNIT_NS_BEGIN + +ThriftControllerUnixDomainSocketAgent::ThriftControllerUnixDomainSocketAgent(const std::string& path) +{ + LogFunc << VAR(path); + + using namespace apache::thrift; + + std::shared_ptr socket = std::make_shared(path); + + transport_ = std::make_shared(socket); + auto protocol = std::make_shared(transport_); + client_ = std::make_shared(protocol); +} + +MAA_THRIFT_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.h b/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.h new file mode 100644 index 000000000..0e5ec458c --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.h @@ -0,0 +1,13 @@ +#pragma once + +#include "ThriftControllerAgent.h" + +MAA_THRIFT_CTRL_UNIT_NS_BEGIN + +class ThriftControllerUnixDomainSocketAgent : public ThriftControllerAgent +{ +public: + ThriftControllerUnixDomainSocketAgent(const std::string& path); + virtual ~ThriftControllerUnixDomainSocketAgent() = default; +}; +MAA_THRIFT_CTRL_UNIT_NS_END diff --git a/source/include/Conf/Conf.h b/source/include/Conf/Conf.h index 0cca69ddf..53b290968 100644 --- a/source/include/Conf/Conf.h +++ b/source/include/Conf/Conf.h @@ -131,6 +131,12 @@ { #define MAA_DBG_CTRL_UNIT_NS_END } +#define MAA_THRIFT_CTRL_UNIT_NS MAA_CTRL_NS::ThriftUnitNs +#define MAA_THRIFT_CTRL_UNIT_NS_BEGIN \ + namespace MAA_THRIFT_CTRL_UNIT_NS \ + { +#define MAA_THRIFT_CTRL_UNIT_NS_END } + /* MaaToolKit */ #define MAA_TOOLKIT_NS MAA_NS::ToolKitNS diff --git a/source/include/ControlUnit/AdbControlUnitAPI.h b/source/include/ControlUnit/AdbControlUnitAPI.h index feefdc45c..5541a6812 100644 --- a/source/include/ControlUnit/AdbControlUnitAPI.h +++ b/source/include/ControlUnit/AdbControlUnitAPI.h @@ -117,31 +117,32 @@ class ControlUnitAPI virtual std::shared_ptr screencap_obj() = 0; }; +MAA_ADB_CTRL_UNIT_NS_END + +#ifdef __cplusplus extern "C" { - void MAA_CONTROL_UNIT_API create_device_list_obj(std::shared_ptr* result, MaaStringView adb_path, - MaaStringView config); - void MAA_CONTROL_UNIT_API create_controller_unit(std::shared_ptr* result, MaaStringView adb_path, - MaaStringView adb_serial, MaaAdbControllerType type, - MaaStringView config, MaaStringView agent_path); - void MAA_CONTROL_UNIT_API create_connection(std::shared_ptr* result, MaaStringView adb_path, - MaaStringView adb_serial, MaaAdbControllerType type, - MaaStringView config); - void MAA_CONTROL_UNIT_API create_device_info(std::shared_ptr* result, MaaStringView adb_path, - MaaStringView adb_serial, MaaAdbControllerType type, - MaaStringView config); - void MAA_CONTROL_UNIT_API create_activity(std::shared_ptr* result, MaaStringView adb_path, - MaaStringView adb_serial, MaaAdbControllerType type, - MaaStringView config); - void MAA_CONTROL_UNIT_API create_touch_input(std::shared_ptr* result, MaaStringView adb_path, - MaaStringView adb_serial, MaaAdbControllerType type, - MaaStringView config, MaaStringView agent_path); - void MAA_CONTROL_UNIT_API create_key_input(std::shared_ptr* result, MaaStringView adb_path, - MaaStringView adb_serial, MaaAdbControllerType type, - MaaStringView config, MaaStringView agent_path); - void MAA_CONTROL_UNIT_API create_screencap(std::shared_ptr* result, MaaStringView adb_path, - MaaStringView adb_serial, MaaAdbControllerType type, - MaaStringView config, MaaStringView agent_path); -} +#endif -MAA_ADB_CTRL_UNIT_NS_END + MaaStringView MAA_CONTROL_UNIT_API get_version(); + + MaaBool MAA_CONTROL_UNIT_API create_controller( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + MaaStringView agent_path, + /*out*/ std::shared_ptr& out_handle); + + MaaBool MAA_CONTROL_UNIT_API create_device_list_obj( // + MaaStringView adb_path, MaaStringView config, + /*out*/ std::shared_ptr& out_handle); + + MaaBool MAA_CONTROL_UNIT_API create_connection( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + /*out*/ std::shared_ptr& out_handle); + + MaaBool MAA_CONTROL_UNIT_API create_device_info( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + /*out*/ std::shared_ptr& out_handle); + +#ifdef __cplusplus +} +#endif diff --git a/source/include/ControlUnit/DbgControlUnitAPI.h b/source/include/ControlUnit/DbgControlUnitAPI.h index ccf8961e7..89c2cd9a5 100644 --- a/source/include/ControlUnit/DbgControlUnitAPI.h +++ b/source/include/ControlUnit/DbgControlUnitAPI.h @@ -34,8 +34,19 @@ class ControllerAPI virtual std::optional screencap() = 0; }; -std::shared_ptr MAA_CONTROL_UNIT_API create_controller(MaaDbgControllerType type, - MaaStringView read_path, MaaStringView write_path, - MaaStringView config); - MAA_DBG_CTRL_UNIT_NS_END + +#ifdef __cplusplus +extern "C" +{ +#endif + + MaaStringView MAA_CONTROL_UNIT_API get_version(); + + MaaBool MAA_CONTROL_UNIT_API create_controller( // + MaaDbgControllerType type, MaaStringView read_path, MaaStringView write_path, MaaStringView config, + std::shared_ptr& out_handle); + +#ifdef __cplusplus +} +#endif diff --git a/source/include/ControlUnit/ThriftControlUnitAPI.h b/source/include/ControlUnit/ThriftControlUnitAPI.h new file mode 100644 index 000000000..19f762c02 --- /dev/null +++ b/source/include/ControlUnit/ThriftControlUnitAPI.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include +#include + +#include "Conf/Conf.h" +#include "MaaFramework/MaaDef.h" +#include "Utils/NoWarningCVMat.hpp" + +MAA_THRIFT_CTRL_UNIT_NS_BEGIN + +class ControllerAPI +{ +public: + virtual ~ControllerAPI() = default; + + virtual bool connect() = 0; + + virtual std::string uuid() const = 0; + virtual cv::Size resolution() const = 0; + + virtual bool start_app(const std::string& intent) = 0; + virtual bool stop_app(const std::string& intent) = 0; + + virtual bool click(int x, int y) = 0; + virtual bool swipe(int x1, int y1, int x2, int y2, int duration) = 0; + + virtual bool touch_down(int contact, int x, int y, int pressure) = 0; + virtual bool touch_move(int contact, int x, int y, int pressure) = 0; + virtual bool touch_up(int contact) = 0; + + virtual bool press_key(int key) = 0; + + virtual std::optional screencap() = 0; +}; + +MAA_THRIFT_CTRL_UNIT_NS_END + +#ifdef __cplusplus +extern "C" +{ +#endif + + MaaStringView MAA_CONTROL_UNIT_API get_version(); + + MaaBool MAA_CONTROL_UNIT_API create_controller( // + MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config, + std::shared_ptr& out_handle); + +#ifdef __cplusplus +} +#endif diff --git a/test/grpc/CMakeLists.txt b/test/grpc/CMakeLists.txt index b38d01e26..b2bed311c 100644 --- a/test/grpc/CMakeLists.txt +++ b/test/grpc/CMakeLists.txt @@ -1,10 +1,9 @@ file(GLOB grpc_testing_src ./*.cpp) add_executable(GrpcTesting ${grpc_testing_src}) -# 我觉得这个应该 generated 扔到 build 文件夹里去 -target_include_directories(GrpcTesting PRIVATE ${CMAKE_SOURCE_DIR}/source/MaaRpc/generated/) -target_link_libraries(GrpcTesting PUBLIC MaaRpcProto protobuf::libprotobuf gRPC::grpc++) -add_dependencies(GrpcTesting MaaRpcProto) +target_include_directories(GrpcTesting PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../../source/MaaRpc/generated/) +target_link_libraries(GrpcTesting PUBLIC RpcProto protobuf::libprotobuf gRPC::grpc++) +add_dependencies(GrpcTesting RpcProto) set_target_properties(GrpcTesting PROPERTIES FOLDER Testing) install(TARGETS GrpcTesting RUNTIME DESTINATION bin)