diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9eab1dd6b..5aeb99dde 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -87,7 +87,7 @@ jobs: with: submodules: true - # Download host arch tools for Thrift of CustomThriftController + # Download host arch tools for Thrift of ThriftControlUnit - name: Download host tools from MaaDeps env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -161,7 +161,7 @@ jobs: with: remove_stale_cache: false - # Download host arch tools for Thrift of CustomThriftController + # Download host arch tools for Thrift of ThriftControlUnit - name: Download host tools from MaaDeps env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -230,7 +230,7 @@ jobs: brew install llvm # 16 - # Download host arch tools for Thrift of CustomThriftController + # Download host arch tools for Thrift of ThriftControlUnit - name: Download host tools from MaaDeps env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 28e2c2d8f..5930c4ab9 100644 --- a/.gitignore +++ b/.gitignore @@ -434,16 +434,8 @@ FodyWeavers.xsd .idea/ *.sln.iml -enc_temp_folder/* - -# Nuke +# chore +.DS_Store .nuke/temp/* - -# test install dir +enc_temp_folder/* install - -.DS_Store - -source/MaaRpc/generated/*.h -source/MaaRpc/generated/*.cc -source/MaaRpc/generated/*.timestamp diff --git a/CMakeLists.txt b/CMakeLists.txt index f734a6400..9effe2396 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,13 +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(WITH_RESTFUL "build with beast for restful server" ON) option(BUILD_GRPC_CLI "build grpc CLI exec" ON) +option(WITH_RESTFUL "build with beast for restful server" 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 fcd64573c..6fde1243a 100644 --- a/cmake/thrift-gen.cmake +++ b/cmake/thrift-gen.cmake @@ -8,7 +8,7 @@ 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}/*) + 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) @@ -16,6 +16,8 @@ macro(GENERATE_THRIFT_LIB LIB_NAME FILENAME OUTPUTDIR SOURCES) ${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/ThriftController.thrift b/include/Interface/ThriftController.thrift index 0dae46005..79299a5d8 100644 --- a/include/Interface/ThriftController.thrift +++ b/include/Interface/ThriftController.thrift @@ -31,15 +31,20 @@ struct Size { } struct CustomImage { - 1: Size size, - 2: i32 type, - 3: binary data, + 1: binary png_data, } service ThriftController { - bool set_option(1: string key, 2: string value), - bool connect(), + + string request_uuid(), + Size request_resolution(), + + bool start_app(1: string entry), + bool stop_app(1: string entry), + + CustomImage screencap(), + bool click(1: ClickParam param), bool swipe(1: SwipeParam param), @@ -48,12 +53,4 @@ service ThriftController { bool touch_up(1: TouchParam param), 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/Interface/proto/types.proto b/include/Interface/proto/types.proto index b282d4eae..4afaca186 100644 --- a/include/Interface/proto/types.proto +++ b/include/Interface/proto/types.proto @@ -132,8 +132,8 @@ message CustomControllerRequest { oneof result { string init = 101; - Size resolution = 201; - string uuid = 202; + string uuid = 201; + Size resolution = 202; } } @@ -142,18 +142,17 @@ message CustomControllerResponse { string init = 101; // handle bool connect = 201; // void - ClickParam click = 202; - SwipeParam swipe = 203; - KeyParam key = 204; - TouchParam touch_down = 205; - TouchParam touch_move = 206; - TouchParam touch_up = 207; - string start = 208; - string stop = 209; - bool resolution = 210; // void - string image = 211; - bool uuid = 212; // void - CustomControllerSetOptionParam set_option = 213; + bool request_uuid = 202; // void + bool request_resolution = 203; // void + string start_app = 301; + string stop_app = 302; + string screencap = 401; + ClickParam click = 501; + SwipeParam swipe = 502; + TouchParam touch_down = 601; + TouchParam touch_move = 602; + TouchParam touch_up = 603; + KeyParam press_key = 701; } } diff --git a/include/MaaFramework/Instance/MaaController.h b/include/MaaFramework/Instance/MaaController.h index 6791b0774..54475f158 100644 --- a/include/MaaFramework/Instance/MaaController.h +++ b/include/MaaFramework/Instance/MaaController.h @@ -8,21 +8,24 @@ extern "C" { #endif - /* Deprecated, please use MaaAdbControllerCreateV2 */ MaaControllerHandle MAA_FRAMEWORK_API MaaAdbControllerCreate( + // Deprecated, please use MaaAdbControllerCreateV2 + MaaControllerHandle MAA_FRAMEWORK_API MaaAdbControllerCreate( // MaaStringView adb_path, MaaStringView address, MaaAdbControllerType type, MaaStringView config, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); - MaaControllerHandle MAA_FRAMEWORK_API MaaAdbControllerCreateV2(MaaStringView adb_path, MaaStringView address, - MaaAdbControllerType type, MaaStringView config, - MaaStringView agent_path, - MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg); - MaaControllerHandle MAA_FRAMEWORK_API MaaCustomControllerCreate(MaaCustomControllerHandle handle, - MaaTransparentArg handle_arg, - MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg); - MaaControllerHandle MAA_FRAMEWORK_API MaaThriftControllerCreate(MaaStringView param, MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg); - MaaControllerHandle MAA_FRAMEWORK_API MaaDbgControllerCreate( + + MaaControllerHandle MAA_FRAMEWORK_API MaaAdbControllerCreateV2( // + MaaStringView adb_path, MaaStringView address, MaaAdbControllerType type, MaaStringView config, + MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); + + MaaControllerHandle MAA_FRAMEWORK_API MaaCustomControllerCreate( // + MaaCustomControllerHandle handle, MaaTransparentArg handle_arg, MaaControllerCallback callback, + MaaCallbackTransparentArg callback_arg); + + MaaControllerHandle MAA_FRAMEWORK_API MaaThriftControllerCreate( // + MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config, + MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); + + MaaControllerHandle MAA_FRAMEWORK_API MaaDbgControllerCreate( // MaaStringView read_path, MaaStringView write_path, MaaDbgControllerType type, MaaStringView config, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); diff --git a/include/MaaFramework/Instance/MaaCustomController.h b/include/MaaFramework/Instance/MaaCustomController.h index e11957752..9b075f304 100644 --- a/include/MaaFramework/Instance/MaaCustomController.h +++ b/include/MaaFramework/Instance/MaaCustomController.h @@ -10,26 +10,25 @@ extern "C" struct MaaCustomControllerAPI { - MaaBool (*set_option)(MaaCtrlOption key, MaaStringView value, MaaTransparentArg handle_arg); - MaaBool (*connect)(MaaTransparentArg handle_arg); + + MaaBool (*request_uuid)(MaaTransparentArg handle_arg, /* out */ MaaStringBufferHandle buffer); + MaaBool (*request_resolution)(MaaTransparentArg handle_arg, /* out */ int32_t* width, + /* out */ int32_t* height); + + MaaBool (*start_app)(MaaStringView intent, MaaTransparentArg handle_arg); + MaaBool (*stop_app)(MaaStringView intent, MaaTransparentArg handle_arg); + + MaaBool (*screencap)(MaaTransparentArg handle_arg, /* out */ MaaImageBufferHandle buffer); + MaaBool (*click)(int32_t x, int32_t y, MaaTransparentArg handle_arg); MaaBool (*swipe)(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t duration, MaaTransparentArg handle_arg); - MaaBool (*press_key)(int32_t keycode, MaaTransparentArg handle_arg); - MaaBool (*touch_down)(int32_t contact, int32_t x, int32_t y, int32_t pressure, MaaTransparentArg handle_arg); MaaBool (*touch_move)(int32_t contact, int32_t x, int32_t y, int32_t pressure, MaaTransparentArg handle_arg); MaaBool (*touch_up)(int32_t contact, MaaTransparentArg handle_arg); - MaaBool (*start_app)(MaaStringView package_name, MaaTransparentArg handle_arg); - MaaBool (*stop_app)(MaaStringView package_name, MaaTransparentArg handle_arg); - - MaaBool (*get_resolution)(MaaTransparentArg handle_arg, /* out */ int32_t* width, - /* out */ int32_t* height); - - MaaBool (*get_image)(MaaTransparentArg handle_arg, /* out */ MaaImageBufferHandle buffer); - MaaBool (*get_uuid)(MaaTransparentArg handle_arg, /* out */ MaaStringBufferHandle buffer); + MaaBool (*press_key)(int32_t keycode, MaaTransparentArg handle_arg); }; #ifdef __cplusplus 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/include/MaaToolKit/Device/MaaToolKitDevice.h b/include/MaaToolKit/Device/MaaToolKitDevice.h index 9413064cd..9e19ce962 100644 --- a/include/MaaToolKit/Device/MaaToolKitDevice.h +++ b/include/MaaToolKit/Device/MaaToolKitDevice.h @@ -7,6 +7,8 @@ extern "C" { #endif + /* For AdbController */ + MaaSize MAA_TOOLKIT_API MaaToolKitFindDevice(); MaaSize MAA_TOOLKIT_API MaaToolKitFindDeviceWithAdb(MaaStringView adb_path); MaaStringView MAA_TOOLKIT_API MaaToolKitGetDeviceName(MaaSize index); diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index c1c221247..c76a0cc5e 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -4,7 +4,7 @@ if(WITH_ADB_CONTROLLER) add_subdirectory(MaaAdbControlUnit) endif(WITH_ADB_CONTROLLER) if(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE) - add_subdirectory(MaaThriftController) + add_subdirectory(MaaThriftControlUnit) endif(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE) if(WITH_DBG_CONTROLLER) add_subdirectory(MaaDbgControlUnit) diff --git a/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp b/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp new file mode 100644 index 000000000..112a7ee6a --- /dev/null +++ b/source/MaaAdbControlUnit/API/AdbControlUnitAPI.cpp @@ -0,0 +1,187 @@ +#include "ControlUnit/AdbControlUnitAPI.h" + +#include + +#include "Input/MaatouchInput.h" +#include "Input/MinitouchInput.h" +#include "Input/TapInput.h" +#include "Manager/ControlUnitMgr.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 MAA_NS::path; + +MaaStringView get_version() +{ +#pragma message("MaaAdbControlUnit MAA_VERSION: " MAA_VERSION) + + return MAA_VERSION; +} + +MaaControlUnitHandle create_control_unit( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) +{ + using namespace MAA_CTRL_UNIT_NS; + + LogFunc << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path) << VAR_VOIDP(callback) + << VAR_VOIDP(callback_arg); + + 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 nullptr; + } + 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 nullptr; + } + if (!maatouch_unit) { + maatouch_unit = std::make_shared(maatouch_path); + } + touch_unit = maatouch_unit; + break; + default: + LogWarn << "Unknown touch input type" << VAR(touch_type); + break; + } + + 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 nullptr; + } + if (!maatouch_unit) { + maatouch_unit = std::make_shared(maatouch_path); + } + key_unit = maatouch_unit; + break; + default: + LogWarn << "Unknown key input type" << VAR(key_type); + break; + } + + 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 nullptr; + } + 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 nullptr; + } + 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 nullptr; + } + screencap_unit = std::make_shared(minicap_path); + break; + default: + LogWarn << "Unknown screencap type" << VAR(screencap_type); + break; + } + + auto unit_mgr = std::make_unique(MAA_NS::path(adb_path), adb_serial, callback, callback_arg); + + 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(config); + if (!json_opt) { + LogError << "Parse config failed, invalid config:" << config; + return nullptr; + } + bool parsed = unit_mgr->parse(*json_opt); + if (!parsed) { + LogError << "unit_mgr->parse failed, invalid json:" << *json_opt; + return nullptr; + } + + auto platform_io = PlatformFactory::create(); + if (!platform_io) { + LogError << "Create platform io failed"; + return nullptr; + } + unit_mgr->set_io(platform_io); + + unit_mgr->set_replacement({ + { "{ADB}", adb_path }, + { "{ADB_SERIAL}", adb_serial }, + }); + + return unit_mgr.release(); +} + +void destroy_control_unit(MaaControlUnitHandle handle) +{ + LogFunc << VAR_VOIDP(handle); + + if (handle) { + delete handle; + } +} diff --git a/source/MaaAdbControlUnit/UnitBase.cpp b/source/MaaAdbControlUnit/Base/UnitBase.cpp similarity index 97% rename from source/MaaAdbControlUnit/UnitBase.cpp rename to source/MaaAdbControlUnit/Base/UnitBase.cpp index 1bb678c8a..3a3906525 100644 --- a/source/MaaAdbControlUnit/UnitBase.cpp +++ b/source/MaaAdbControlUnit/Base/UnitBase.cpp @@ -1,9 +1,8 @@ #include "UnitBase.h" -#include "UnitBase.h" #include "Utils/Logger.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN void UnitBase::set_io(std::shared_ptr io_ptr) { @@ -90,4 +89,4 @@ std::optional UnitBase::command(const Argv::value& cmd, bool recv_b return recv_by_socket ? sock_data : pipe_data; } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/UnitBase.h b/source/MaaAdbControlUnit/Base/UnitBase.h similarity index 57% rename from source/MaaAdbControlUnit/UnitBase.h rename to source/MaaAdbControlUnit/Base/UnitBase.h index 61577a8d3..739b622f5 100644 --- a/source/MaaAdbControlUnit/UnitBase.h +++ b/source/MaaAdbControlUnit/Base/UnitBase.h @@ -7,7 +7,7 @@ #include "Screencap/ScreencapHelper.h" #include "Utils/ArgvWrapper.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class UnitBase { @@ -34,52 +34,47 @@ class UnitBase Argv::replacement argv_replace_; }; -class DeviceListBase : public DeviceListAPI, public UnitBase +class ScreencapBase : public UnitBase { public: - virtual ~DeviceListBase() override = default; -}; + virtual ~ScreencapBase() override = default; -class ConnectionBase : public ConnectionAPI, public UnitBase -{ public: - virtual ~ConnectionBase() override = default; -}; + virtual bool init(int swidth, int sheight) = 0; + virtual void deinit() = 0; + virtual bool set_wh(int swidth, int sheight) { return screencap_helper_.set_wh(swidth, sheight); } -class ActivityBase : public ActivityAPI, public UnitBase -{ -public: - virtual ~ActivityBase() override = default; -}; + virtual std::optional screencap() = 0; -class DeviceInfoBase : public DeviceInfoAPI, public UnitBase -{ -public: - virtual ~DeviceInfoBase() override = default; +protected: + ScreencapHelper screencap_helper_; }; -class ScreencapBase : public ScreencapAPI, public UnitBase +class TouchInputBase : virtual public UnitBase { public: - virtual ~ScreencapBase() override = default; + virtual ~TouchInputBase() override = default; public: - virtual bool set_wh(int swidth, int sheight) override { return screencap_helper_.set_wh(swidth, sheight); } + virtual bool init(int swidth, int sheight, int orientation) = 0; + virtual void deinit() = 0; + virtual bool set_wh(int swidth, int sheight, int orientation) = 0; -protected: - ScreencapHelper screencap_helper_; -}; + virtual bool click(int x, int y) = 0; + virtual bool swipe(int x1, int y1, int x2, int y2, int duration) = 0; -class TouchInputBase : public TouchInputAPI, virtual public UnitBase -{ -public: - virtual ~TouchInputBase() override = default; + 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; }; -class KeyInputBase : public KeyInputAPI, virtual public UnitBase +class KeyInputBase : virtual public UnitBase { public: virtual ~KeyInputBase() override = default; + +public: + virtual bool press_key(int key) = 0; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/CMakeLists.txt b/source/MaaAdbControlUnit/CMakeLists.txt index 73b9f64d5..d8b9105cd 100644 --- a/source/MaaAdbControlUnit/CMakeLists.txt +++ b/source/MaaAdbControlUnit/CMakeLists.txt @@ -1,5 +1,5 @@ file(GLOB_RECURSE maa_adb_control_unit_src *.h *.hpp *.cpp) -file(GLOB_RECURSE maa_adb_control_unit_header ../include/ControlUnit/AdbControlUnitAPI.h) +file(GLOB_RECURSE maa_adb_control_unit_header ../include/ControlUnit/AdbControlUnitAPI.h ../include/ControlUnit/ControlUnitAPI.h) add_library(MaaAdbControlUnit SHARED ${maa_adb_control_unit_src} ${maa_adb_control_unit_header}) diff --git a/source/MaaAdbControlUnit/ControlUnitMgr.cpp b/source/MaaAdbControlUnit/ControlUnitMgr.cpp deleted file mode 100644 index ae94a6532..000000000 --- a/source/MaaAdbControlUnit/ControlUnitMgr.cpp +++ /dev/null @@ -1,569 +0,0 @@ -#include "ControlUnitMgr.h" - -#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) -{ - bool ret = true; - - ret &= connection_ && connection_->parse(config); - ret &= device_info_ && device_info_->parse(config); - ret &= activity_ && activity_->parse(config); - ret &= screencap_ && screencap_->parse(config); - ret &= touch_input_ && touch_input_->parse(config); - ret &= key_input_ && key_input_->parse(config); - - return ret; -} - -void ControlUnitMgr::set_io(const std::shared_ptr& io_ptr) -{ - if (connection_) { - connection_->set_io(io_ptr); - } - if (device_info_) { - device_info_->set_io(io_ptr); - } - if (activity_) { - activity_->set_io(io_ptr); - } - if (touch_input_) { - touch_input_->set_io(io_ptr); - } - if (key_input_) { - key_input_->set_io(io_ptr); - } - if (screencap_) { - screencap_->set_io(io_ptr); - } -} - -void ControlUnitMgr::set_replacement(const std::map& replacement) -{ - if (connection_) { - connection_->set_replacement(replacement); - } - if (device_info_) { - device_info_->set_replacement(replacement); - } - if (activity_) { - activity_->set_replacement(replacement); - } - if (touch_input_) { - touch_input_->set_replacement(replacement); - } - if (key_input_) { - key_input_->set_replacement(replacement); - } - if (screencap_) { - screencap_->set_replacement(replacement); - } -} - -std::shared_ptr create_device_list_obj(MaaStringView adb_path, MaaStringView config) -{ - 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 nullptr; - } - bool parsed = device_list_mgr->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return nullptr; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return nullptr; - } - device_list_mgr->set_io(platform_io); - - device_list_mgr->set_replacement({ - { "{ADB}", adb_path }, - }); - - return device_list_mgr; -} - -std::shared_ptr create_controller_unit(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, - MaaStringView agent_path) -{ - 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 nullptr; - } - 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 nullptr; - } - 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 nullptr; - } - - 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 nullptr; - } - 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 nullptr; - } - - 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 nullptr; - } - 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 nullptr; - } - 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 nullptr; - } - screencap_unit = std::make_shared(minicap_path); - break; - default: - LogError << "Unknown screencap type" << VAR(screencap_type); - return nullptr; - } - - 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 nullptr; - } - bool parsed = unit_mgr->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return nullptr; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return nullptr; - } - unit_mgr->set_io(platform_io); - - unit_mgr->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - return unit_mgr; -} - -std::shared_ptr create_connection(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config) -{ - 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 nullptr; - } - - auto connection = std::make_shared(); - - bool parsed = connection->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return nullptr; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return nullptr; - } - connection->set_io(platform_io); - connection->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - return connection; -} - -std::shared_ptr create_device_info(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config) -{ - 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 nullptr; - } - - auto device_info = std::make_shared(); - - bool parsed = device_info->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return nullptr; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return nullptr; - } - device_info->set_io(platform_io); - device_info->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - return device_info; -} - -std::shared_ptr create_activity(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config) -{ - 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 nullptr; - } - - auto activity = std::make_shared(); - - bool parsed = activity->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return nullptr; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return nullptr; - } - activity->set_io(platform_io); - activity->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - return activity; -} - -std::shared_ptr create_touch_input(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, - MaaStringView agent_path) -{ - LogFunc << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path); - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return nullptr; - } - - 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 nullptr; - } - 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 nullptr; - } - touch_unit = std::make_shared(maatouch_path); - break; - default: - LogError << "Unknown touch input type" << VAR(type); - return nullptr; - } - - bool parsed = touch_unit->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return nullptr; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return nullptr; - } - touch_unit->set_io(platform_io); - touch_unit->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - return touch_unit; -} - -std::shared_ptr create_key_input(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, MaaStringView agent_path) -{ - LogFunc << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path); - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return nullptr; - } - 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 nullptr; - } - key_unit = std::make_shared(maatouch_path); - break; - default: - LogError << "Unknown key input type" << VAR(type); - return nullptr; - } - - bool parsed = key_unit->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return nullptr; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return nullptr; - } - key_unit->set_io(platform_io); - key_unit->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - return key_unit; -} - -std::shared_ptr create_screencap(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, - MaaStringView agent_path) -{ - LogFunc << VAR(adb_path) << VAR(adb_serial) << VAR(type) << VAR(config) << VAR(agent_path); - - auto json_opt = json::parse(std::string_view(config)); - if (!json_opt) { - LogError << "Parse config failed, invalid config:" << config; - return nullptr; - } - - 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 nullptr; - } - 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 nullptr; - } - 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 nullptr; - } - screencap_unit = std::make_shared(minicap_path); - break; - default: - LogError << "Unknown screencap type" << VAR(type); - return nullptr; - } - - bool parsed = screencap_unit->parse(*json_opt); - if (!parsed) { - LogError << "Parse json failed, invalid json:" << *json_opt; - return nullptr; - } - - auto platform_io = PlatformFactory::create(); - if (!platform_io) { - LogError << "Create platform io failed"; - return nullptr; - } - screencap_unit->set_io(platform_io); - screencap_unit->set_replacement({ - { "{ADB}", adb_path }, - { "{ADB_SERIAL}", adb_serial }, - }); - - return screencap_unit; -} - -MAA_ADB_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/ControlUnitMgr.h b/source/MaaAdbControlUnit/ControlUnitMgr.h deleted file mode 100644 index 4b249931b..000000000 --- a/source/MaaAdbControlUnit/ControlUnitMgr.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "ControlUnit/AdbControlUnitAPI.h" - -#include "UnitBase.h" - -MAA_ADB_CTRL_UNIT_NS_BEGIN - -class ControlUnitMgr : public ControlUnitAPI -{ -public: - virtual ~ControlUnitMgr() override = default; - -public: // from ControlUnitAPI - virtual std::shared_ptr connection_obj() override { return connection_; } - virtual std::shared_ptr device_info_obj() override { return device_info_; } - virtual std::shared_ptr activity_obj() override { return activity_; } - virtual std::shared_ptr touch_input_obj() override { return touch_input_; } - virtual std::shared_ptr key_input_obj() override { return key_input_; } - virtual std::shared_ptr screencap_obj() override { return screencap_; } - -public: - bool parse(const json::value& config); - void set_io(const std::shared_ptr& io_ptr); - void set_replacement(const std::map& replacement); - - void set_connection_obj(std::shared_ptr obj) { connection_ = std::move(obj); } - void set_device_info_obj(std::shared_ptr obj) { device_info_ = std::move(obj); } - void set_activity_obj(std::shared_ptr obj) { activity_ = std::move(obj); } - void set_touch_input_obj(std::shared_ptr obj) { touch_input_ = std::move(obj); } - void set_key_input_obj(std::shared_ptr obj) { key_input_ = std::move(obj); } - void set_screencap_obj(std::shared_ptr obj) { screencap_ = std::move(obj); } - -private: - std::shared_ptr connection_ = nullptr; - std::shared_ptr device_info_ = nullptr; - std::shared_ptr activity_ = nullptr; - std::shared_ptr touch_input_ = nullptr; - std::shared_ptr key_input_ = nullptr; - std::shared_ptr screencap_ = nullptr; -}; - -MAA_ADB_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/General/Activity.cpp b/source/MaaAdbControlUnit/General/Activity.cpp index c1ce7cb6f..6973af887 100644 --- a/source/MaaAdbControlUnit/General/Activity.cpp +++ b/source/MaaAdbControlUnit/General/Activity.cpp @@ -2,14 +2,14 @@ #include "Utils/Logger.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool Activity::parse(const json::value& config) { return parse_argv("StartApp", config, start_app_argv_) && parse_argv("StopApp", config, stop_app_argv_); } -bool Activity::start(const std::string& intent) +bool Activity::start_app(const std::string& intent) { LogFunc; @@ -19,7 +19,7 @@ bool Activity::start(const std::string& intent) return cmd_ret.has_value(); } -bool Activity::stop(const std::string& intent) +bool Activity::stop_app(const std::string& intent) { LogFunc; @@ -29,4 +29,4 @@ bool Activity::stop(const std::string& intent) return cmd_ret.has_value(); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/General/Activity.h b/source/MaaAdbControlUnit/General/Activity.h index 139d70aea..339db1c7e 100644 --- a/source/MaaAdbControlUnit/General/Activity.h +++ b/source/MaaAdbControlUnit/General/Activity.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -class Activity : public ActivityBase +class Activity : public UnitBase { public: virtual ~Activity() override = default; @@ -12,13 +12,13 @@ class Activity : public ActivityBase public: // from UnitBase virtual bool parse(const json::value& config) override; -public: // from ActivityAPI - virtual bool start(const std::string& intent) override; - virtual bool stop(const std::string& intent) override; +public: + bool start_app(const std::string& intent); + bool stop_app(const std::string& intent); private: Argv start_app_argv_; Argv stop_app_argv_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/General/Connection.cpp b/source/MaaAdbControlUnit/General/Connection.cpp index 608fdaba7..b235ead71 100644 --- a/source/MaaAdbControlUnit/General/Connection.cpp +++ b/source/MaaAdbControlUnit/General/Connection.cpp @@ -2,7 +2,7 @@ #include "Utils/Logger.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool Connection::parse(const json::value& config) { @@ -39,4 +39,4 @@ bool Connection::kill_server() return command(kill_server_argv_.gen(argv_replace_), false, 60LL * 1000).has_value(); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/General/Connection.h b/source/MaaAdbControlUnit/General/Connection.h index e3ed9ab66..81aee545a 100644 --- a/source/MaaAdbControlUnit/General/Connection.h +++ b/source/MaaAdbControlUnit/General/Connection.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -class Connection : public ConnectionBase +class Connection : public UnitBase { public: virtual ~Connection() override = default; @@ -12,13 +12,13 @@ class Connection : public ConnectionBase public: // from UnitBase virtual bool parse(const json::value& config) override; -public: // from ConnectionAPI - virtual bool connect() override; - virtual bool kill_server() override; +public: + bool connect(); + bool kill_server(); private: Argv connect_argv_; Argv kill_server_argv_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/General/DeviceInfo.cpp b/source/MaaAdbControlUnit/General/DeviceInfo.cpp index 45941ae38..eb6634a92 100644 --- a/source/MaaAdbControlUnit/General/DeviceInfo.cpp +++ b/source/MaaAdbControlUnit/General/DeviceInfo.cpp @@ -2,7 +2,7 @@ #include "Utils/Logger.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool DeviceInfo::parse(const json::value& config) { @@ -23,11 +23,10 @@ std::optional DeviceInfo::request_uuid() auto& uuid_str = cmd_ret.value(); std::erase_if(uuid_str, [](char c) { return !std::isdigit(c) && !std::isalpha(c); }); - uuid_ = std::move(uuid_str); - return uuid_; + return uuid_str; } -std::optional DeviceInfo::request_resolution() +std::optional> DeviceInfo::request_resolution() { LogFunc; @@ -37,9 +36,11 @@ std::optional DeviceInfo::request_resolution() return std::nullopt; } + int width = 0, height = 0; + std::istringstream iss(cmd_ret.value()); - iss >> resolution_.width >> resolution_.height; - return resolution_; + iss >> width >> height; + return std::make_pair(width, height); } std::optional DeviceInfo::request_orientation() @@ -61,12 +62,10 @@ std::optional DeviceInfo::request_orientation() int ori = s.front() - '0'; if (!(ori >= 0 && ori <= 3)) { - orientation_ = -1; return std::nullopt; } - orientation_ = ori; - return orientation_; + return ori; } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/General/DeviceInfo.h b/source/MaaAdbControlUnit/General/DeviceInfo.h index 58b980ec6..f205a5316 100644 --- a/source/MaaAdbControlUnit/General/DeviceInfo.h +++ b/source/MaaAdbControlUnit/General/DeviceInfo.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -class DeviceInfo : public DeviceInfoBase +class DeviceInfo : public UnitBase { public: virtual ~DeviceInfo() override = default; @@ -12,24 +12,15 @@ class DeviceInfo : public DeviceInfoBase public: // from UnitBase virtual bool parse(const json::value& config) override; -public: // from DeviceInfoAPI - virtual std::optional request_uuid() override; - virtual std::optional request_resolution() override; - virtual std::optional request_orientation() override; - - virtual std::string get_uuid() const override { return uuid_; } - virtual DeviceResolution get_resolution() const override { return resolution_; } - virtual int get_orientation() const override { return orientation_; } +public: + std::optional request_uuid(); + std::optional> request_resolution(); + std::optional request_orientation(); private: Argv uuid_argv_; Argv resolution_argv_; Argv orientation_argv_; - -private: - std::string uuid_; - DeviceResolution resolution_; - int orientation_ = -1; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/General/DeviceList.cpp b/source/MaaAdbControlUnit/General/DeviceList.cpp index dc0899c99..0f34ac8b7 100644 --- a/source/MaaAdbControlUnit/General/DeviceList.cpp +++ b/source/MaaAdbControlUnit/General/DeviceList.cpp @@ -4,18 +4,17 @@ #include "Utils/Ranges.hpp" #include "Utils/StringMisc.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool DeviceList::parse(const json::value& config) { return parse_argv("Devices", config, devices_argv_); } -std::optional DeviceList::request_devices() +std::optional> DeviceList::request_devices() { LogFunc; - devices_.clear(); auto cmd_ret = command(devices_argv_.gen(argv_replace_)); if (!cmd_ret) { @@ -33,21 +32,17 @@ std::optional DeviceList::request_devices() } lines.erase(lines.begin()); // remove "List of devices attached" + std::vector devices; for (auto&& line : lines) { if (line.find("device") == std::string::npos) { continue; } string_trim_(line); - devices_.emplace_back(string_split(line, '\t')[0]); + devices.emplace_back(string_split(line, '\t')[0]); } - LogInfo << VAR(devices_); + LogInfo << VAR(devices); - return devices_; + return devices; } -DeviceList::Devices DeviceList::get_devices() const -{ - return devices_; -} - -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/General/DeviceList.h b/source/MaaAdbControlUnit/General/DeviceList.h index a1666c34e..a9d3db4c8 100644 --- a/source/MaaAdbControlUnit/General/DeviceList.h +++ b/source/MaaAdbControlUnit/General/DeviceList.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -class DeviceList : public DeviceListBase +class DeviceList : public UnitBase { public: virtual ~DeviceList() override = default; @@ -12,16 +12,11 @@ class DeviceList : public DeviceListBase public: // from UnitBase virtual bool parse(const json::value& config) override; -public: // from DeviceListAPI - virtual std::optional request_devices() override; - - virtual Devices get_devices() const override; +public: + std::optional> request_devices(); private: Argv devices_argv_; - -private: - Devices devices_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Input/MaatouchInput.cpp b/source/MaaAdbControlUnit/Input/MaatouchInput.cpp index ecb1e2508..91eb25d42 100644 --- a/source/MaaAdbControlUnit/Input/MaatouchInput.cpp +++ b/source/MaaAdbControlUnit/Input/MaatouchInput.cpp @@ -7,7 +7,7 @@ #include #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool MaatouchInput::parse(const json::value& config) { @@ -91,4 +91,4 @@ bool MaatouchInput::press_key(int key) return ret; } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Input/MaatouchInput.h b/source/MaaAdbControlUnit/Input/MaatouchInput.h index 9e6ddb6d4..908cb62ff 100644 --- a/source/MaaAdbControlUnit/Input/MaatouchInput.h +++ b/source/MaaAdbControlUnit/Input/MaatouchInput.h @@ -1,13 +1,13 @@ #pragma once +#include "Base/UnitBase.h" #include "MtouchHelper.h" -#include "UnitBase.h" #include #include "Invoke/InvokeApp.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class MaatouchInput : public MtouchHelper, public KeyInputBase { @@ -62,4 +62,4 @@ class MaatouchInput : public MtouchHelper, public KeyInputBase std::shared_ptr invoke_app_ = std::make_shared(); }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Input/MinitouchInput.cpp b/source/MaaAdbControlUnit/Input/MinitouchInput.cpp index e968be6bf..02265e1fe 100644 --- a/source/MaaAdbControlUnit/Input/MinitouchInput.cpp +++ b/source/MaaAdbControlUnit/Input/MinitouchInput.cpp @@ -7,7 +7,7 @@ #include #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool MinitouchInput::parse(const json::value& config) { @@ -93,4 +93,4 @@ bool MinitouchInput::set_wh(int swidth, int sheight, int orientation) return read_info(swidth, sheight, orientation); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Input/MinitouchInput.h b/source/MaaAdbControlUnit/Input/MinitouchInput.h index f5bee7783..2f30a8a9d 100644 --- a/source/MaaAdbControlUnit/Input/MinitouchInput.h +++ b/source/MaaAdbControlUnit/Input/MinitouchInput.h @@ -6,7 +6,7 @@ #include "Invoke/InvokeApp.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class MinitouchInput : public MtouchHelper { @@ -55,4 +55,4 @@ class MinitouchInput : public MtouchHelper std::shared_ptr invoke_app_ = std::make_shared(); }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Input/MtouchHelper.cpp b/source/MaaAdbControlUnit/Input/MtouchHelper.cpp index 1cb5ca746..48d4b1c02 100644 --- a/source/MaaAdbControlUnit/Input/MtouchHelper.cpp +++ b/source/MaaAdbControlUnit/Input/MtouchHelper.cpp @@ -7,7 +7,7 @@ #include #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool MtouchHelper::read_info(int swidth, int sheight, int orientation) { @@ -230,4 +230,4 @@ bool MtouchHelper::touch_up(int contact) return ret; } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Input/MtouchHelper.h b/source/MaaAdbControlUnit/Input/MtouchHelper.h index ab9f5ff27..0ee1b3b25 100644 --- a/source/MaaAdbControlUnit/Input/MtouchHelper.h +++ b/source/MaaAdbControlUnit/Input/MtouchHelper.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" #include "Invoke/InvokeApp.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class MtouchHelper : public TouchInputBase { @@ -36,4 +36,4 @@ class MtouchHelper : public TouchInputBase int orientation_ = 0; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Input/TapInput.cpp b/source/MaaAdbControlUnit/Input/TapInput.cpp index 0272b3ad8..38f9cb157 100644 --- a/source/MaaAdbControlUnit/Input/TapInput.cpp +++ b/source/MaaAdbControlUnit/Input/TapInput.cpp @@ -4,7 +4,7 @@ #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool TapTouchInput::parse(const json::value& config) { @@ -84,4 +84,4 @@ bool TapKeyInput::press_key(int key) return cmd_ret.has_value() && cmd_ret.value().empty(); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Input/TapInput.h b/source/MaaAdbControlUnit/Input/TapInput.h index cb6406029..7f38dceb2 100644 --- a/source/MaaAdbControlUnit/Input/TapInput.h +++ b/source/MaaAdbControlUnit/Input/TapInput.h @@ -1,8 +1,8 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class TapTouchInput : public TouchInputBase { @@ -44,4 +44,4 @@ class TapKeyInput : public KeyInputBase Argv press_key_argv_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp b/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp index 0194375b4..f883da3c7 100644 --- a/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp +++ b/source/MaaAdbControlUnit/Invoke/InvokeApp.cpp @@ -4,7 +4,7 @@ #include "Utils/Platform.h" #include "Utils/Time.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool InvokeApp::parse(const json::value& config) { @@ -139,4 +139,4 @@ std::shared_ptr InvokeApp::invoke_app(const std::string& package) return cmd_ret; } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Invoke/InvokeApp.h b/source/MaaAdbControlUnit/Invoke/InvokeApp.h index 12c06849a..e4ee0ca4b 100644 --- a/source/MaaAdbControlUnit/Invoke/InvokeApp.h +++ b/source/MaaAdbControlUnit/Invoke/InvokeApp.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class InvokeApp : public UnitBase { @@ -41,4 +41,4 @@ class InvokeApp : public UnitBase std::string tempname_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp b/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp new file mode 100644 index 000000000..1f38e5f54 --- /dev/null +++ b/source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp @@ -0,0 +1,268 @@ +#include "ControlUnitMgr.h" + +#include + +#include "MaaFramework/MaaMsg.h" +#include "Utils/Logger.h" + +MAA_CTRL_UNIT_NS_BEGIN + +ControlUnitMgr::ControlUnitMgr(std::filesystem::path adb_path, std::string adb_serial, MaaControllerCallback callback, + MaaCallbackTransparentArg callback_arg) + : adb_path_(std::move(adb_path)), adb_serial_(std::move(adb_serial)), notifier(callback, callback_arg) +{} + +bool ControlUnitMgr::find_device(std::vector& devices) +{ + auto opt = device_list_.request_devices(); + if (!opt) { + LogError << "failed to find_device"; + return false; + } + + devices = std::move(opt).value(); + return true; +} + +bool ControlUnitMgr::connect() +{ + json::value details = { + { "adb", path_to_utf8_string(adb_path_) }, + { "address", adb_serial_ }, + }; + + bool connected = connection_.connect(); + bool is_remote = adb_serial_.find(':') != std::string::npos; + if (!connected && is_remote) { + notifier.notify(MaaMsg_Controller_ConnectFailed, details | json::object { { "why", "ConnectFailed" } }); + LogError << "failed to connect"; + return false; + } + + auto uuid_opt = device_info_.request_uuid(); + if (!uuid_opt) { + notifier.notify(MaaMsg_Controller_UUIDGetFailed, details); + notifier.notify(MaaMsg_Controller_ConnectFailed, details | json::object { { "why", "UUIDGetFailed" } }); + LogError << "failed to request_uuid"; + return false; + } + const auto& uuid = uuid_opt.value(); + details |= { { "uuid", uuid } }; + + notifier.notify(MaaMsg_Controller_UUIDGot, details | json::object { { "uuid", uuid } }); + + const auto& resolution_opt = device_info_.request_resolution(); + if (!resolution_opt) { + notifier.notify(MaaMsg_Controller_ResolutionGetFailed, details); + notifier.notify(MaaMsg_Controller_ConnectFailed, details | json::object { { "why", "ResolutionGetFailed" } }); + LogError << "failed to request_resolution"; + return false; + } + auto [width, height] = *resolution_opt; + int orientation = device_info_.request_orientation().value_or(0); + details |= { { "resolution", { { "width", width }, { "height", height } } }, { "orientation", orientation } }; + + notifier.notify(MaaMsg_Controller_ResolutionGot, details); + + notifier.notify(MaaMsg_Controller_ConnectSuccess, details); + + if (screencap_) { + if (screencap_->init(width, height)) { + LogError << "failed to init screencap"; + notifier.notify(MaaMsg_Controller_ScreencapInitFailed, details); + return false; + } + notifier.notify(MaaMsg_Controller_ScreencapInited, details); + } + else { + LogWarn << "screencap_ is null"; + notifier.notify(MaaMsg_Controller_ScreencapInitFailed, details); + } + + if (touch_input_) { + if (touch_input_->init(width, height, orientation)) { + LogError << "failed to init touch_input"; + notifier.notify(MaaMsg_Controller_TouchInputInitFailed, details); + return false; + } + notifier.notify(MaaMsg_Controller_TouchInputInited, details); + } + else { + LogWarn << "touch_input_ is null"; + notifier.notify(MaaMsg_Controller_TouchInputInitFailed, details); + } + + return true; +} + +bool ControlUnitMgr::request_uuid(std::string& uuid) +{ + auto opt = device_info_.request_uuid(); + if (!opt) { + LogError << "failed to request_uuid"; + return false; + } + uuid = std::move(opt).value(); + return true; +} + +bool ControlUnitMgr::request_resolution(int& width, int& height) +{ + auto opt = device_info_.request_resolution(); + if (!opt) { + LogError << "failed to request_uuid"; + return false; + } + width = opt->first; + height = opt->second; + return true; +} + +bool ControlUnitMgr::start_app(const std::string& intent) +{ + return activity_.start_app(intent); +} + +bool ControlUnitMgr::stop_app(const std::string& intent) +{ + return activity_.stop_app(intent); +} + +bool ControlUnitMgr::screencap(cv::Mat& image) +{ + if (!screencap_) { + LogError << "screencap_ is null"; + return false; + } + + auto opt = screencap_->screencap(); + if (!opt) { + LogError << "failed to screencap"; + return false; + } + + image = std::move(opt).value(); + return true; +} + +bool ControlUnitMgr::click(int x, int y) +{ + if (!touch_input_) { + LogError << "touch_input_ is null"; + return false; + } + + return touch_input_->click(x, y); +} + +bool ControlUnitMgr::swipe(int x1, int y1, int x2, int y2, int duration) +{ + if (!touch_input_) { + LogError << "touch_input_ is null"; + return false; + } + + return touch_input_->swipe(x1, y1, x2, y2, duration); +} + +bool ControlUnitMgr::touch_down(int contact, int x, int y, int pressure) +{ + if (!touch_input_) { + LogError << "touch_input_ is null"; + return false; + } + + return touch_input_->touch_down(contact, x, y, pressure); +} + +bool ControlUnitMgr::touch_move(int contact, int x, int y, int pressure) +{ + if (!touch_input_) { + LogError << "touch_input_ is null"; + return false; + } + + return touch_input_->touch_move(contact, x, y, pressure); +} + +bool ControlUnitMgr::touch_up(int contact) +{ + if (!touch_input_) { + LogError << "touch_input_ is null"; + return false; + } + + return touch_input_->touch_up(contact); +} + +bool ControlUnitMgr::press_key(int key) +{ + if (!key_input_) { + LogError << "key_input_ is null"; + return false; + } + + return key_input_->press_key(key); +} + +bool ControlUnitMgr::parse(const json::value& config) +{ + bool ret = true; + + ret &= connection_.parse(config); + ret &= device_info_.parse(config); + ret &= activity_.parse(config); + ret &= device_list_.parse(config); + + if (screencap_) { + ret &= screencap_->parse(config); + } + + if (touch_input_) { + ret &= touch_input_->parse(config); + } + + if (key_input_) { + ret &= key_input_->parse(config); + } + + return ret; +} + +void ControlUnitMgr::set_io(const std::shared_ptr& io_ptr) +{ + device_list_.set_io(io_ptr); + connection_.set_io(io_ptr); + device_info_.set_io(io_ptr); + activity_.set_io(io_ptr); + + if (touch_input_) { + touch_input_->set_io(io_ptr); + } + if (key_input_) { + key_input_->set_io(io_ptr); + } + if (screencap_) { + screencap_->set_io(io_ptr); + } +} + +void ControlUnitMgr::set_replacement(const std::map& replacement) +{ + device_list_.set_replacement(replacement); + connection_.set_replacement(replacement); + device_info_.set_replacement(replacement); + activity_.set_replacement(replacement); + + if (touch_input_) { + touch_input_->set_replacement(replacement); + } + if (key_input_) { + key_input_->set_replacement(replacement); + } + if (screencap_) { + screencap_->set_replacement(replacement); + } +} + +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Manager/ControlUnitMgr.h b/source/MaaAdbControlUnit/Manager/ControlUnitMgr.h new file mode 100644 index 000000000..bd33eda9c --- /dev/null +++ b/source/MaaAdbControlUnit/Manager/ControlUnitMgr.h @@ -0,0 +1,70 @@ +#pragma once + +#include "ControlUnit/ControlUnitAPI.h" + +#include + +#include "Base/UnitBase.h" +#include "General/Activity.h" +#include "General/Connection.h" +#include "General/DeviceInfo.h" +#include "General/DeviceList.h" +#include "Utils/MessageNotifier.hpp" + +MAA_CTRL_UNIT_NS_BEGIN + +class ControlUnitMgr : public ControlUnitAPI +{ +public: + ControlUnitMgr(std::filesystem::path adb_path, std::string adb_serial, MaaControllerCallback callback, + MaaCallbackTransparentArg callback_arg); + virtual ~ControlUnitMgr() override = default; + +public: // from ControlUnitAPI + virtual bool find_device(/*out*/ std::vector& devices) override; + + virtual bool connect() override; + + virtual bool request_uuid(/*out*/ std::string& uuid) override; + virtual bool request_resolution(/*out*/ int& width, /*out*/ int& height) override; + + virtual bool start_app(const std::string& intent) override; + virtual bool stop_app(const std::string& intent) override; + + virtual bool screencap(/*out*/ cv::Mat& image) 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; + +public: + bool parse(const json::value& config); + void set_io(const std::shared_ptr& io_ptr); + void set_replacement(const std::map& replacement); + + void set_touch_input_obj(std::shared_ptr obj) { touch_input_ = std::move(obj); } + void set_key_input_obj(std::shared_ptr obj) { key_input_ = std::move(obj); } + void set_screencap_obj(std::shared_ptr obj) { screencap_ = std::move(obj); } + +private: + std::filesystem::path adb_path_; + std::string adb_serial_; + + MessageNotifier notifier; + + DeviceList device_list_; + Connection connection_; + DeviceInfo device_info_; + Activity activity_; + + std::shared_ptr touch_input_ = nullptr; + std::shared_ptr key_input_ = nullptr; + std::shared_ptr screencap_ = nullptr; +}; + +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Platform/BoostIO.cpp b/source/MaaAdbControlUnit/Platform/BoostIO.cpp index 09e5fde43..3f2c38f4c 100644 --- a/source/MaaAdbControlUnit/Platform/BoostIO.cpp +++ b/source/MaaAdbControlUnit/Platform/BoostIO.cpp @@ -11,7 +11,7 @@ #define BOOST_CREATE_NO_WINDOW #endif -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN BoostIO::BoostIO() : ios_(std::make_shared()), server_sock_(*ios_) { @@ -320,4 +320,4 @@ std::string IOHandlerBoostStream::read(unsigned timeout_sec, size_t expect) return result; } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Platform/BoostIO.h b/source/MaaAdbControlUnit/Platform/BoostIO.h index 53fee5380..dc59dee27 100644 --- a/source/MaaAdbControlUnit/Platform/BoostIO.h +++ b/source/MaaAdbControlUnit/Platform/BoostIO.h @@ -7,7 +7,7 @@ #include "Utils/NonCopyable.hpp" #include "Utils/SingletonHolder.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class BoostIO : public PlatformIO { @@ -71,4 +71,4 @@ class IOHandlerBoostStream : public IOHandler, NonCopyable std::shared_ptr proc_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Platform/PlatformFactory.h b/source/MaaAdbControlUnit/Platform/PlatformFactory.h index 58c768918..c2ded3a78 100644 --- a/source/MaaAdbControlUnit/Platform/PlatformFactory.h +++ b/source/MaaAdbControlUnit/Platform/PlatformFactory.h @@ -4,7 +4,7 @@ #include "BoostIO.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN using NativeIO = BoostIO; @@ -14,4 +14,4 @@ class PlatformFactory static std::shared_ptr create() { return std::make_shared(); } }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Platform/PlatformIO.h b/source/MaaAdbControlUnit/Platform/PlatformIO.h index 0f58eab3d..46121b002 100644 --- a/source/MaaAdbControlUnit/Platform/PlatformIO.h +++ b/source/MaaAdbControlUnit/Platform/PlatformIO.h @@ -7,7 +7,7 @@ #include "Conf/Conf.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class IOHandler; @@ -38,4 +38,4 @@ class IOHandler virtual std::string read(unsigned timeout_sec, size_t expect) = 0; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Encode.cpp b/source/MaaAdbControlUnit/Screencap/Encode.cpp index 5762439ad..1091f5432 100644 --- a/source/MaaAdbControlUnit/Screencap/Encode.cpp +++ b/source/MaaAdbControlUnit/Screencap/Encode.cpp @@ -2,7 +2,7 @@ #include "Utils/Logger.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool ScreencapEncode::parse(const json::value& config) { @@ -31,4 +31,4 @@ std::optional ScreencapEncode::screencap() cmd_ret.value(), std::bind(&ScreencapHelper::decode_png, &screencap_helper_, std::placeholders::_1)); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Encode.h b/source/MaaAdbControlUnit/Screencap/Encode.h index 8c6a29d5a..b60d6b455 100644 --- a/source/MaaAdbControlUnit/Screencap/Encode.h +++ b/source/MaaAdbControlUnit/Screencap/Encode.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" #include "ScreencapHelper.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class ScreencapEncode : public ScreencapBase { @@ -24,4 +24,4 @@ class ScreencapEncode : public ScreencapBase Argv screencap_encode_argv_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp b/source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp index ee0b04d93..0de81fa63 100644 --- a/source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp +++ b/source/MaaAdbControlUnit/Screencap/EncodeToFile.cpp @@ -5,7 +5,7 @@ #include "Utils/NoWarningCV.hpp" #include "Utils/Time.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool ScreencapEncodeToFileAndPull::parse(const json::value& config) { @@ -44,4 +44,4 @@ std::optional ScreencapEncodeToFileAndPull::screencap() return imread(dst_path); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/EncodeToFile.h b/source/MaaAdbControlUnit/Screencap/EncodeToFile.h index 8f8970366..689c5a04f 100644 --- a/source/MaaAdbControlUnit/Screencap/EncodeToFile.h +++ b/source/MaaAdbControlUnit/Screencap/EncodeToFile.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" #include "ScreencapHelper.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class ScreencapEncodeToFileAndPull : public ScreencapBase { @@ -27,4 +27,4 @@ class ScreencapEncodeToFileAndPull : public ScreencapBase std::string tempname_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/FastestWay.cpp b/source/MaaAdbControlUnit/Screencap/FastestWay.cpp index 4823fefe2..27ad8c77e 100644 --- a/source/MaaAdbControlUnit/Screencap/FastestWay.cpp +++ b/source/MaaAdbControlUnit/Screencap/FastestWay.cpp @@ -6,7 +6,7 @@ #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN ScreencapFastestWay::ScreencapFastestWay(const std::filesystem::path& minicap_path) { @@ -157,4 +157,4 @@ std::ostream& operator<<(std::ostream& os, ScreencapFastestWay::Method m) return os; } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/FastestWay.h b/source/MaaAdbControlUnit/Screencap/FastestWay.h index 213982c55..74ee7a2e1 100644 --- a/source/MaaAdbControlUnit/Screencap/FastestWay.h +++ b/source/MaaAdbControlUnit/Screencap/FastestWay.h @@ -7,7 +7,7 @@ #include "RawByNetcat.h" #include "RawWithGzip.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class ScreencapFastestWay : public ScreencapBase { @@ -46,4 +46,4 @@ class ScreencapFastestWay : public ScreencapBase std::ostream& operator<<(std::ostream& os, ScreencapFastestWay::Method m); -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp index 5d93c4698..1b7911157 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.cpp @@ -7,7 +7,7 @@ #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool MinicapBase::parse(const json::value& config) { @@ -121,4 +121,4 @@ bool MinicapBase::init(int swidth, int sheight) return set_wh(swidth, sheight); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.h b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.h index 3c32aeae7..9d0dac9cb 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.h +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapBase.h @@ -1,13 +1,13 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" #include #include "../ScreencapHelper.h" #include "Invoke/InvokeApp.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class MinicapBase : public ScreencapBase { @@ -37,4 +37,4 @@ class MinicapBase : public ScreencapBase std::vector sdk_list_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDef.h b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDef.h index 569652fd8..dd00987cd 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDef.h +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDef.h @@ -4,7 +4,7 @@ #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN #pragma pack(push, 1) @@ -30,4 +30,4 @@ struct MinicapHeader #pragma pack(pop) -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDirect.cpp b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDirect.cpp index 16810c7ca..8464a3cc2 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDirect.cpp +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDirect.cpp @@ -4,7 +4,7 @@ #include "Utils/Logger.h" #include "Utils/NoWarningCV.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN std::optional MinicapDirect::screencap() { @@ -20,4 +20,4 @@ std::optional MinicapDirect::screencap() return screencap_helper_.process_data( res.value(), std::bind(&ScreencapHelper::trunc_decode_jpg, &screencap_helper_, std::placeholders::_1)); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDirect.h b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDirect.h index c89758922..a477e7812 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDirect.h +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapDirect.h @@ -2,7 +2,7 @@ #include "MinicapBase.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class MinicapDirect : public MinicapBase { @@ -15,4 +15,4 @@ class MinicapDirect : public MinicapBase virtual std::optional screencap() override; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.cpp b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.cpp index a35688c28..370b186d7 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.cpp +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.cpp @@ -5,7 +5,7 @@ #include "Utils/Logger.h" #include "Utils/NoWarningCV.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN MinicapStream::~MinicapStream() { @@ -189,4 +189,4 @@ void MinicapStream::working_thread() } } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.h b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.h index 57b05ce32..324711bf3 100644 --- a/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.h +++ b/source/MaaAdbControlUnit/Screencap/Minicap/MinicapStream.h @@ -5,7 +5,7 @@ #include #include -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class MinicapStream : public MinicapBase { @@ -39,4 +39,4 @@ class MinicapStream : public MinicapBase std::shared_ptr stream_handle_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/RawByNetcat.cpp b/source/MaaAdbControlUnit/Screencap/RawByNetcat.cpp index b51c2c5a6..b37b62095 100644 --- a/source/MaaAdbControlUnit/Screencap/RawByNetcat.cpp +++ b/source/MaaAdbControlUnit/Screencap/RawByNetcat.cpp @@ -2,7 +2,7 @@ #include "Utils/Logger.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool ScreencapRawByNetcat::parse(const json::value& config) { @@ -91,4 +91,4 @@ std::optional ScreencapRawByNetcat::request_netcat_address() } } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/RawByNetcat.h b/source/MaaAdbControlUnit/Screencap/RawByNetcat.h index 769165208..bd3c6e9b3 100644 --- a/source/MaaAdbControlUnit/Screencap/RawByNetcat.h +++ b/source/MaaAdbControlUnit/Screencap/RawByNetcat.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" #include "ScreencapHelper.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class ScreencapRawByNetcat : public ScreencapBase { @@ -30,4 +30,4 @@ class ScreencapRawByNetcat : public ScreencapBase uint16_t netcat_port_ = 0; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/RawWithGzip.cpp b/source/MaaAdbControlUnit/Screencap/RawWithGzip.cpp index cd829e5ad..4eefe7835 100644 --- a/source/MaaAdbControlUnit/Screencap/RawWithGzip.cpp +++ b/source/MaaAdbControlUnit/Screencap/RawWithGzip.cpp @@ -2,7 +2,7 @@ #include "Utils/Logger.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool ScreencapRawWithGzip::parse(const json::value& config) { @@ -31,4 +31,4 @@ std::optional ScreencapRawWithGzip::screencap() cmd_ret.value(), std::bind(&ScreencapHelper::decode_gzip, &screencap_helper_, std::placeholders::_1)); } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/RawWithGzip.h b/source/MaaAdbControlUnit/Screencap/RawWithGzip.h index b0523f948..c228ab898 100644 --- a/source/MaaAdbControlUnit/Screencap/RawWithGzip.h +++ b/source/MaaAdbControlUnit/Screencap/RawWithGzip.h @@ -1,10 +1,10 @@ #pragma once -#include "UnitBase.h" +#include "Base/UnitBase.h" #include "ScreencapHelper.h" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class ScreencapRawWithGzip : public ScreencapBase { @@ -24,4 +24,4 @@ class ScreencapRawWithGzip : public ScreencapBase Argv screencap_raw_with_gzip_argv_; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/ScreencapHelper.cpp b/source/MaaAdbControlUnit/Screencap/ScreencapHelper.cpp index 282a36b51..69df69492 100644 --- a/source/MaaAdbControlUnit/Screencap/ScreencapHelper.cpp +++ b/source/MaaAdbControlUnit/Screencap/ScreencapHelper.cpp @@ -12,7 +12,7 @@ #pragma warning(pop) #endif -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN bool ScreencapHelper::set_wh(int w, int h) { @@ -220,4 +220,4 @@ bool ScreencapHelper::check_head_tail(std::string_view input, std::string_view h return true; } -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaAdbControlUnit/Screencap/ScreencapHelper.h b/source/MaaAdbControlUnit/Screencap/ScreencapHelper.h index 229b5c9e9..a6169a3ad 100644 --- a/source/MaaAdbControlUnit/Screencap/ScreencapHelper.h +++ b/source/MaaAdbControlUnit/Screencap/ScreencapHelper.h @@ -7,7 +7,7 @@ #include "Conf/Conf.h" #include "Utils/NoWarningCVMat.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class ScreencapHelper { @@ -42,4 +42,4 @@ class ScreencapHelper } end_of_line_ = EndOfLine::UnknownYet; }; -MAA_ADB_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp b/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp index e9ef201b7..8a8419750 100644 --- a/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp +++ b/source/MaaDbgControlUnit/API/DbgControlUnitAPI.cpp @@ -6,30 +6,41 @@ #include "ReplayRecording/ReplayRecordingMgr.h" #include "Utils/Logger.h" -MAA_DBG_CTRL_UNIT_NS_BEGIN - -std::shared_ptr create_controller(MaaDbgControllerType type, MaaStringView read_path, - MaaStringView write_path, MaaStringView config) +MaaStringView get_version() { - LogFunc << VAR(type) << VAR(read_path) << VAR(write_path) << VAR(config); +#pragma message("MaaDbgControlUnit MAA_VERSION: " MAA_VERSION) - auto config_parsed = json::parse(config); - if (!config_parsed) { - LogError << "Failed to parse config" << VAR(config); - return nullptr; - } + return MAA_VERSION; +} + +MaaControlUnitHandle create_control_unit(MaaDbgControllerType type, MaaStringView read_path) +{ + LogFunc << VAR(type) << VAR(read_path); auto read_stdpath = MAA_NS::path(read_path); - auto write_stdpath = MAA_NS::path(write_path); + + MaaControlUnitHandle handle = nullptr; switch (type) { case MaaDbgControllerType_CarouselImage: - return std::make_shared(read_stdpath); + handle = new MAA_CTRL_UNIT_NS::CarouselImage(read_stdpath); + break; + case MaaDbgControllerType_ReplayRecording: - return create_replay_recording(read_stdpath); + handle = MAA_CTRL_UNIT_NS::create_replay_recording(read_stdpath); + break; } - return nullptr; + LogDebug << VAR_VOIDP(handle); + + return handle; } -MAA_DBG_CTRL_UNIT_NS_END +void destroy_control_unit(MaaControlUnitHandle handle) +{ + LogFunc << VAR_VOIDP(handle); + + if (handle) { + delete handle; + } +} diff --git a/source/MaaDbgControlUnit/CMakeLists.txt b/source/MaaDbgControlUnit/CMakeLists.txt index 204d3fc11..c21b2419a 100644 --- a/source/MaaDbgControlUnit/CMakeLists.txt +++ b/source/MaaDbgControlUnit/CMakeLists.txt @@ -1,5 +1,5 @@ file(GLOB_RECURSE maa_dbg_control_unit_src *.h *.hpp *.cpp) -file(GLOB_RECURSE maa_dbg_control_unit_header ../include/ControlUnit/DbgControlUnitAPI.h) +file(GLOB_RECURSE maa_dbg_control_unit_header ../include/ControlUnit/DbgControlUnitAPI.h ../include/ControlUnit/ControlUnitAPI.h) add_library(MaaDbgControlUnit SHARED ${maa_dbg_control_unit_src} ${maa_dbg_control_unit_header}) diff --git a/source/MaaDbgControlUnit/CarouselImage/CarouselImage.cpp b/source/MaaDbgControlUnit/CarouselImage/CarouselImage.cpp index d93104140..eeb849e87 100644 --- a/source/MaaDbgControlUnit/CarouselImage/CarouselImage.cpp +++ b/source/MaaDbgControlUnit/CarouselImage/CarouselImage.cpp @@ -4,16 +4,12 @@ #include "Utils/Logger.h" #include "Utils/StringMisc.hpp" -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -std::string CarouselImage::uuid() const +bool CarouselImage::find_device(std::vector& devices) { - return path_to_utf8_string(path_); -} - -cv::Size CarouselImage::resolution() const -{ - return resolution_; + std::ignore = devices; + return true; } bool CarouselImage::connect() @@ -62,6 +58,19 @@ bool CarouselImage::connect() return true; } +bool CarouselImage::request_uuid(std::string& uuid) +{ + uuid = path_to_utf8_string(path_); + return true; +} + +bool CarouselImage::request_resolution(int& width, int& height) +{ + width = resolution_.width; + height = resolution_.height; + return true; +} + bool CarouselImage::start_app(const std::string& intent) { std::ignore = intent; @@ -74,6 +83,21 @@ bool CarouselImage::stop_app(const std::string& intent) return true; } +bool CarouselImage::screencap(cv::Mat& image) +{ + if (images_.empty()) { + LogError << "no image" << VAR(path_); + return false; + } + + if (image_index_ >= images_.size()) { + image_index_ = 0; + } + + image = images_.at(image_index_++); + return true; +} + bool CarouselImage::click(int x, int y) { std::ignore = x; @@ -124,18 +148,4 @@ bool CarouselImage::press_key(int key) return true; } -std::optional CarouselImage::screencap() -{ - if (images_.empty()) { - LogError << "no image" << VAR(path_); - return std::nullopt; - } - - if (image_index_ >= images_.size()) { - image_index_ = 0; - } - - return images_.at(image_index_++); -} - -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/CarouselImage/CarouselImage.h b/source/MaaDbgControlUnit/CarouselImage/CarouselImage.h index e0afc5e6c..6a0e91213 100644 --- a/source/MaaDbgControlUnit/CarouselImage/CarouselImage.h +++ b/source/MaaDbgControlUnit/CarouselImage/CarouselImage.h @@ -1,28 +1,32 @@ #pragma once -#include "ControlUnit/DbgControlUnitAPI.h" +#include "ControlUnit/ControlUnitAPI.h" #include #include -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -class CarouselImage : public ControllerAPI +class CarouselImage : public ControlUnitAPI { public: CarouselImage(std::filesystem::path path) : path_(std::move(path)) {} virtual ~CarouselImage() = default; -public: // from ControllerAPI - virtual std::string uuid() const override; - virtual cv::Size resolution() const override; +public: // from ControlUnitAPI + virtual bool find_device(/*out*/ std::vector& devices) override; virtual bool connect() override; + virtual bool request_uuid(/*out*/ std::string& uuid) override; + virtual bool request_resolution(/*out*/ int& width, /*out*/ int& height) override; + virtual bool start_app(const std::string& intent) override; virtual bool stop_app(const std::string& intent) override; + virtual bool screencap(/*out*/ cv::Mat& image) override; + virtual bool click(int x, int y) override; virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override; @@ -32,8 +36,6 @@ class CarouselImage : public ControllerAPI virtual bool press_key(int key) override; - virtual std::optional screencap() override; - private: std::filesystem::path path_; std::vector images_; @@ -41,4 +43,4 @@ class CarouselImage : public ControllerAPI cv::Size resolution_ {}; }; -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/ReplayRecording/Record.h b/source/MaaDbgControlUnit/ReplayRecording/Record.h index bf2ad3fd3..65e4bc9c6 100644 --- a/source/MaaDbgControlUnit/ReplayRecording/Record.h +++ b/source/MaaDbgControlUnit/ReplayRecording/Record.h @@ -11,7 +11,7 @@ #include -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN struct DeviceInfo { @@ -141,4 +141,4 @@ inline std::ostream& operator<<(std::ostream& os, Record::Action::Type type) return os; } -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/ReplayRecording/RecordParser.cpp b/source/MaaDbgControlUnit/ReplayRecording/RecordParser.cpp index 623ff6b3d..158bbb00c 100644 --- a/source/MaaDbgControlUnit/ReplayRecording/RecordParser.cpp +++ b/source/MaaDbgControlUnit/ReplayRecording/RecordParser.cpp @@ -8,7 +8,7 @@ #include "Utils/ImageIo.h" #include "Utils/Logger.h" -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN std::optional RecordParser::parse(const std::filesystem::path& path) { @@ -333,4 +333,4 @@ std::optional RecordParser::parse_app(const json::value& record_j return result; } -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/ReplayRecording/RecordParser.h b/source/MaaDbgControlUnit/ReplayRecording/RecordParser.h index 622b2d132..947515df2 100644 --- a/source/MaaDbgControlUnit/ReplayRecording/RecordParser.h +++ b/source/MaaDbgControlUnit/ReplayRecording/RecordParser.h @@ -6,7 +6,7 @@ #include "Record.h" -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN class RecordParser { @@ -25,4 +25,4 @@ class RecordParser static std::optional parse_app(const json::value& record_json); }; -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.cpp b/source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.cpp index bced3bfaf..57601a0f5 100644 --- a/source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.cpp +++ b/source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.cpp @@ -2,7 +2,13 @@ #include "Utils/Logger.h" -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN + +bool ReplayRecording::find_device(std::vector& devices) +{ + std::ignore = devices; + return true; +} bool ReplayRecording::connect() { @@ -24,6 +30,19 @@ bool ReplayRecording::connect() return record.success; } +bool ReplayRecording::request_uuid(std::string& uuid) +{ + uuid = recording_.device_info.uuid; + return true; +} + +bool ReplayRecording::request_resolution(int& width, int& height) +{ + width = recording_.device_info.resolution.width; + height = recording_.device_info.resolution.height; + return true; +} + bool ReplayRecording::start_app(const std::string& intent) { LogInfo << VAR(intent); @@ -78,6 +97,31 @@ bool ReplayRecording::stop_app(const std::string& intent) return record.success; } +bool ReplayRecording::screencap(cv::Mat& image) +{ + LogInfo; + + if (record_index_ >= recording_.records.size()) { + LogError << "record index out of range" << VAR(record_index_) << VAR(recording_.records.size()); + return false; + } + + const Record& record = recording_.records.at(record_index_); + + if (record.action.type != Record::Action::Type::screencap) { + LogError << "record type is not screencap" << VAR(record.action.type) << VAR(record.raw_data); + return false; + } + + auto param = std::get(record.action.param); + + sleep(record.cost); + ++record_index_; + + image = record.success ? param.image : cv::Mat(); + return true; +} + bool ReplayRecording::click(int x, int y) { LogInfo << VAR(x) << VAR(y); @@ -251,33 +295,10 @@ bool ReplayRecording::press_key(int key) return record.success; } -std::optional ReplayRecording::screencap() -{ - LogInfo; - - if (record_index_ >= recording_.records.size()) { - LogError << "record index out of range" << VAR(record_index_) << VAR(recording_.records.size()); - return std::nullopt; - } - - const Record& record = recording_.records.at(record_index_); - - if (record.action.type != Record::Action::Type::screencap) { - LogError << "record type is not screencap" << VAR(record.action.type) << VAR(record.raw_data); - return std::nullopt; - } - - auto param = std::get(record.action.param); - - sleep(record.cost); - ++record_index_; - return record.success ? param.image : cv::Mat(); -} - void ReplayRecording::sleep(int ms) { LogTrace << VAR(ms); std::this_thread::sleep_for(std::chrono::milliseconds(ms)); } -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.h b/source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.h index e68d6c731..7204f8835 100644 --- a/source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.h +++ b/source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.h @@ -1,6 +1,6 @@ #pragma once -#include "ControlUnit/DbgControlUnitAPI.h" +#include "ControlUnit/ControlUnitAPI.h" #include @@ -8,23 +8,27 @@ #include "Record.h" -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -class ReplayRecording : public ControllerAPI +class ReplayRecording : public ControlUnitAPI { public: ReplayRecording(Recording recording) : recording_(std::move(recording)) {} virtual ~ReplayRecording() = default; -public: // from ControllerAPI - virtual std::string uuid() const override { return recording_.device_info.uuid; } - virtual cv::Size resolution() const override { return recording_.device_info.resolution; } +public: // from ControlUnitAPI + virtual bool find_device(/*out*/ std::vector& devices) override; virtual bool connect() override; + virtual bool request_uuid(/*out*/ std::string& uuid) override; + virtual bool request_resolution(/*out*/ int& width, /*out*/ int& height) override; + virtual bool start_app(const std::string& intent) override; virtual bool stop_app(const std::string& intent) override; + virtual bool screencap(/*out*/ cv::Mat& image) override; + virtual bool click(int x, int y) override; virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override; @@ -34,8 +38,6 @@ class ReplayRecording : public ControllerAPI virtual bool press_key(int key) override; - virtual std::optional screencap() override; - private: void sleep(int ms); @@ -44,4 +46,4 @@ class ReplayRecording : public ControllerAPI size_t record_index_ = 0; }; -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/ReplayRecording/ReplayRecordingMgr.cpp b/source/MaaDbgControlUnit/ReplayRecording/ReplayRecordingMgr.cpp index ea25f4386..111120fee 100644 --- a/source/MaaDbgControlUnit/ReplayRecording/ReplayRecordingMgr.cpp +++ b/source/MaaDbgControlUnit/ReplayRecording/ReplayRecordingMgr.cpp @@ -3,16 +3,16 @@ #include "ReplayRecording/RecordParser.h" #include "Utils/Logger.h" -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -std::shared_ptr create_replay_recording(const std::filesystem::path& path) +ReplayRecording* create_replay_recording(const std::filesystem::path& path) { auto record_opt = RecordParser::parse(path); if (!record_opt) { LogError << "Failed to parse record file:" << path; return nullptr; } - return std::make_shared(std::move(*record_opt)); + return new ReplayRecording(std::move(*record_opt)); } -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaDbgControlUnit/ReplayRecording/ReplayRecordingMgr.h b/source/MaaDbgControlUnit/ReplayRecording/ReplayRecordingMgr.h index 6778f5ed3..3c41e9ae2 100644 --- a/source/MaaDbgControlUnit/ReplayRecording/ReplayRecordingMgr.h +++ b/source/MaaDbgControlUnit/ReplayRecording/ReplayRecordingMgr.h @@ -2,8 +2,8 @@ #include "ReplayRecording/ReplayRecording.h" -MAA_DBG_CTRL_UNIT_NS_BEGIN +MAA_CTRL_UNIT_NS_BEGIN -std::shared_ptr create_replay_recording(const std::filesystem::path& path); +ReplayRecording* create_replay_recording(const std::filesystem::path& path); -MAA_DBG_CTRL_UNIT_NS_END +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaFramework/API/MaaController.cpp b/source/MaaFramework/API/MaaController.cpp index 5a0d19c67..a67c0ca50 100644 --- a/source/MaaFramework/API/MaaController.cpp +++ b/source/MaaFramework/API/MaaController.cpp @@ -2,17 +2,15 @@ #include "Buffer/ImageBuffer.hpp" #include "Buffer/StringBuffer.hpp" -#include "ControlUnit/AdbControlUnitAPI.h" -#include "Controller/AdbController.h" -#include "Controller/CustomController.h" -#include "Controller/CustomThriftController.h" -#include "Controller/DebuggingController.h" +#include "Controller/CustomControllerAgent.h" +#include "Controller/GeneralControllerAgent.h" +#include "Utils/ControlUnitLibraryHolder.h" #include "Utils/Logger.h" #include "Utils/Platform.h" -MaaControllerHandle MaaAdbControllerCreate(MaaStringView adb_path, MaaStringView address, MaaAdbControllerType type, - MaaStringView config, MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg) +MaaControllerHandle MaaAdbControllerCreate( // + MaaStringView adb_path, MaaStringView address, MaaAdbControllerType type, MaaStringView config, + MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { LogWarn << "This API" << __FUNCTION__ << "is about to be deprecated, and we recommend using MaaAdbControllerCreateV2 instead."; @@ -21,37 +19,27 @@ MaaControllerHandle MaaAdbControllerCreate(MaaStringView adb_path, MaaStringView return MaaAdbControllerCreateV2(adb_path, address, type, config, kDefaultAgentPath.data(), callback, callback_arg); } -MaaControllerHandle MaaAdbControllerCreateV2(MaaStringView adb_path, MaaStringView address, MaaAdbControllerType type, - MaaStringView config, MaaStringView agent_path, - MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) +MaaControllerHandle MaaAdbControllerCreateV2( // + MaaStringView adb_path, MaaStringView address, MaaAdbControllerType type, MaaStringView config, + MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { LogFunc << VAR(adb_path) << VAR(address) << VAR(type) << VAR(agent_path) << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg); -#ifdef WITH_ADB_CONTROLLER + auto control_unit = MAA_CTRL_NS::AdbControlUnitLibraryHolder::create_control_unit( + adb_path, address, type, config, agent_path, callback, callback_arg); - auto unit_mgr = MAA_ADB_CTRL_UNIT_NS::create_controller_unit(adb_path, address, type, config, agent_path); - if (!unit_mgr) { - LogError << "Failed to create controller unit"; + if (!control_unit) { + LogError << "Failed to create control unit"; return nullptr; } - return new MAA_CTRL_NS::AdbController(adb_path, address, std::move(unit_mgr), callback, callback_arg); - -#else - -#pragma message("The build without adb controller") - - std::ignore = config; - - LogError << "The build without adb controller"; - return nullptr; - -#endif // WITH_ADB_CONTROLLER + return new MAA_CTRL_NS::GeneralControllerAgent(std::move(control_unit), callback, callback_arg); } -MaaControllerHandle MaaCustomControllerCreate(MaaCustomControllerHandle handle, MaaTransparentArg handle_arg, - MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) +MaaControllerHandle MaaCustomControllerCreate( // + MaaCustomControllerHandle handle, MaaTransparentArg handle_arg, MaaControllerCallback callback, + MaaCallbackTransparentArg callback_arg) { LogFunc << VAR(handle) << VAR(handle_arg) << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg); @@ -60,60 +48,44 @@ MaaControllerHandle MaaCustomControllerCreate(MaaCustomControllerHandle handle, return nullptr; } - return new MAA_CTRL_NS::CustomController(handle, handle_arg, callback, callback_arg); + return new MAA_CTRL_NS::CustomControllerAgent(handle, handle_arg, callback, callback_arg); } -MaaControllerHandle MaaThriftControllerCreate(MaaStringView param, MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg) +MaaControllerHandle MaaThriftControllerCreate( // + MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config, + MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { - LogFunc << VAR(param) << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg); + LogFunc << VAR(type) << VAR(host) << VAR(type) << VAR(port) << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg); -#ifdef WITH_THRIFT_CONTROLLER + std::ignore = config; - try { - return new MAA_CTRL_NS::CustomThriftController(param, callback, callback_arg); - } - catch (const std::exception& e) { - LogError << "Failed to create thrift controller: " << e.what(); + auto control_unit = MAA_CTRL_NS::ThriftControlUnitLibraryHolder::create_control_unit(type, host, port, config); + + if (!control_unit) { + LogError << "Failed to create control unit"; return nullptr; } -#else - -#pragma message("The build without thrift controller") - - LogError << "The build without thrift controller"; - return nullptr; - -#endif // WITH_THRIFT_CONTROLLER + return new MAA_CTRL_NS::GeneralControllerAgent(std::move(control_unit), callback, callback_arg); } -MaaControllerHandle MaaDbgControllerCreate(MaaStringView read_path, MaaStringView write_path, MaaDbgControllerType type, - MaaStringView config, MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg) +MaaControllerHandle MaaDbgControllerCreate( // + MaaStringView read_path, MaaStringView write_path, MaaDbgControllerType type, MaaStringView config, + MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { LogFunc << VAR(read_path) << VAR(write_path) << VAR(type) << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg); -#ifdef WITH_DBG_CONTROLLER + std::ignore = write_path; + std::ignore = config; - auto unit_mgr = MAA_DBG_CTRL_UNIT_NS::create_controller(type, read_path, write_path, config); - if (!unit_mgr) { - LogError << "Failed to create controller unit"; + auto control_unit = MAA_CTRL_NS::DbgControlUnitLibraryHolder::create_control_unit(type, read_path); + + if (!control_unit) { + LogError << "Failed to create control unit"; return nullptr; } - return new MAA_CTRL_NS::DebuggingController(read_path, write_path, std::move(unit_mgr), callback, callback_arg); - -#else - -#pragma message("The build without debugging controller") - - std::ignore = config; - - LogError << "The build without debugging controller"; - return nullptr; - -#endif // WITH_DBG_CONTROLLER + return new MAA_CTRL_NS::GeneralControllerAgent(std::move(control_unit), callback, callback_arg); } void MaaControllerDestroy(MaaControllerHandle ctrl) diff --git a/source/MaaFramework/API/MaaInstance.cpp b/source/MaaFramework/API/MaaInstance.cpp index 9a7619529..a5dc4aabf 100644 --- a/source/MaaFramework/API/MaaInstance.cpp +++ b/source/MaaFramework/API/MaaInstance.cpp @@ -3,8 +3,6 @@ #include "Instance/InstanceMgr.h" #include "Utils/Logger.h" -#pragma message("MaaFramework MAA_VERSION: " MAA_VERSION) - MaaInstanceHandle MaaCreate(MaaInstanceCallback callback, MaaCallbackTransparentArg callback_arg) { LogFunc << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg); diff --git a/source/MaaFramework/API/MaaTypes.h b/source/MaaFramework/API/MaaTypes.h index 9dbef0a1d..6444c3d49 100644 --- a/source/MaaFramework/API/MaaTypes.h +++ b/source/MaaFramework/API/MaaTypes.h @@ -1,6 +1,5 @@ #pragma once - #include #include #include @@ -55,8 +54,9 @@ struct MaaControllerAPI : public MaaInstanceSink virtual MaaStatus wait(MaaCtrlId ctrl_id) const = 0; virtual MaaBool connected() const = 0; - virtual cv::Mat get_image() const = 0; - virtual std::string get_uuid() const = 0; + virtual cv::Mat get_image() = 0; + virtual std::string get_uuid() = 0; + virtual std::pair get_resolution() = 0; }; struct MaaInstanceAPI diff --git a/source/MaaFramework/API/MaaUtility.cpp b/source/MaaFramework/API/MaaUtility.cpp index aa77cb6e3..a62a5b112 100644 --- a/source/MaaFramework/API/MaaUtility.cpp +++ b/source/MaaFramework/API/MaaUtility.cpp @@ -5,6 +5,8 @@ MaaStringView MaaVersion() { +#pragma message("MaaFramework MAA_VERSION: " MAA_VERSION) + return MAA_VERSION; } diff --git a/source/MaaFramework/CMakeLists.txt b/source/MaaFramework/CMakeLists.txt index f462ca7e9..83db867ad 100644 --- a/source/MaaFramework/CMakeLists.txt +++ b/source/MaaFramework/CMakeLists.txt @@ -8,21 +8,11 @@ set(MaaFramework_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/../../include/MaaFramework) target_include_directories( MaaFramework INTERFACE ../../include - PRIVATE . ../include ../../include ${MaaThriftController_INCLUDE_DIRS}) + PRIVATE . ../include ../../include) target_compile_definitions(MaaFramework PRIVATE MAA_FRAMEWORK_EXPORTS) -target_link_libraries(MaaFramework MaaUtils) -if (WITH_ADB_CONTROLLER) - target_link_libraries(MaaFramework MaaAdbControlUnit) -endif(WITH_ADB_CONTROLLER) -if(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE) - target_link_libraries(MaaFramework MaaThriftController) -endif(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE) -if (WITH_DBG_CONTROLLER) - target_link_libraries(MaaFramework MaaDbgControlUnit) -endif(WITH_DBG_CONTROLLER) -target_link_libraries(MaaFramework ${OpenCV_LIBS} fastdeploy_ppocr ONNXRuntime::ONNXRuntime HeaderOnlyLibraries) +target_link_libraries(MaaFramework MaaUtils ${OpenCV_LIBS} fastdeploy_ppocr ONNXRuntime::ONNXRuntime HeaderOnlyLibraries) # clang 15之后有ranges if (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") find_package(range-v3 REQUIRED) # target_link_libraries(MaaFramework range-v3::range-v3) endif () @@ -33,7 +23,7 @@ if (WITH_ADB_CONTROLLER) add_dependencies(MaaFramework MaaAdbControlUnit) endif(WITH_ADB_CONTROLLER) if(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE) - add_dependencies(MaaFramework MaaThriftController) + add_dependencies(MaaFramework MaaThriftControlUnit) endif(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE) if (WITH_DBG_CONTROLLER) add_dependencies(MaaFramework MaaDbgControlUnit) diff --git a/source/MaaFramework/Controller/AdbController.cpp b/source/MaaFramework/Controller/AdbController.cpp deleted file mode 100644 index e076e210c..000000000 --- a/source/MaaFramework/Controller/AdbController.cpp +++ /dev/null @@ -1,284 +0,0 @@ -#include "AdbController.h" - -#include "MaaFramework/MaaMsg.h" -#include "Utils/Logger.h" -#include "Utils/StringMisc.hpp" - -#include - -MAA_CTRL_NS_BEGIN - -AdbController::AdbController(std::string adb_path, std::string address, - std::shared_ptr unit_mgr, - MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) - : ControllerMgr(callback, callback_arg), adb_path_(std::move(adb_path)), address_(std::move(address)), - unit_mgr_(std::move(unit_mgr)) -{ - LogDebug << VAR(adb_path_) << VAR(address_) << VAR(unit_mgr_) << VAR(unit_mgr_->activity_obj()) - << VAR(unit_mgr_->connection_obj()) << VAR(unit_mgr_->device_info_obj()) << VAR(unit_mgr_->key_input_obj()) - << VAR(unit_mgr_->screencap_obj()) << VAR(unit_mgr_->touch_input_obj()); -} - -AdbController::~AdbController() -{ - if (!unit_mgr_) { - return; - } - - if (unit_mgr_->screencap_obj()) { - unit_mgr_->screencap_obj()->deinit(); - } - if (unit_mgr_->touch_input_obj()) { - unit_mgr_->touch_input_obj()->deinit(); - } -} - -bool AdbController::_connect() -{ - if (!unit_mgr_ || !unit_mgr_->connection_obj() || !unit_mgr_->device_info_obj() || !unit_mgr_->screencap_obj() || - !unit_mgr_->touch_input_obj() || !unit_mgr_->key_input_obj() || !unit_mgr_->activity_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->connection_obj()) - << VAR(unit_mgr_->device_info_obj()) << VAR(unit_mgr_->screencap_obj()) - << VAR(unit_mgr_->touch_input_obj()) << VAR(unit_mgr_->key_input_obj()) - << VAR(unit_mgr_->activity_obj()); - return false; - } - - json::value details = { - { "adb", path_to_utf8_string(adb_path_) }, - { "address", address_ }, - }; - - bool connected = unit_mgr_->connection_obj()->connect(); - bool is_remote = address_.find(':') != std::string::npos; - if (!connected && is_remote) { - notifier.notify(MaaMsg_Controller_ConnectFailed, details | json::object { { "why", "ConnectFailed" } }); - LogError << "failed to connect"; - return false; - } - - if (!unit_mgr_->device_info_obj()->request_uuid()) { - notifier.notify(MaaMsg_Controller_UUIDGetFailed, details); - notifier.notify(MaaMsg_Controller_ConnectFailed, details | json::object { { "why", "UUIDGetFailed" } }); - LogError << "failed to request_uuid"; - return false; - } - const auto& uuid = unit_mgr_->device_info_obj()->get_uuid(); - details |= { { "uuid", uuid } }; - - notifier.notify(MaaMsg_Controller_UUIDGot, details | json::object { { "uuid", uuid } }); - - if (!unit_mgr_->device_info_obj()->request_resolution()) { - notifier.notify(MaaMsg_Controller_ResolutionGetFailed, details); - notifier.notify(MaaMsg_Controller_ConnectFailed, details | json::object { { "why", "ResolutionGetFailed" } }); - LogError << "failed to request_resolution"; - return false; - } - auto [w, h] = unit_mgr_->device_info_obj()->get_resolution(); - resolution_ = { w, h }; - int orientation = unit_mgr_->device_info_obj()->request_orientation().value_or(0); - details |= { { "resolution", { { "width", w }, { "height", h } } }, { "orientation", orientation } }; - - notifier.notify(MaaMsg_Controller_ResolutionGot, details); - - if (!unit_mgr_->screencap_obj()->init(w, h)) { - notifier.notify(MaaMsg_Controller_ScreencapInitFailed, details); - notifier.notify(MaaMsg_Controller_ConnectFailed, details | json::object { { "why", "ScreencapInitFailed" } }); - LogError << "failed to init screencap"; - return false; - } - notifier.notify(MaaMsg_Controller_ScreencapInited, details); - - if (!unit_mgr_->touch_input_obj()->init(w, h, orientation)) { - notifier.notify(MaaMsg_Controller_TouchInputInitFailed, details); - notifier.notify(MaaMsg_Controller_ConnectFailed, details | json::object { { "why", "TouchInputInitFailed" } }); - LogError << "failed to init touch_input"; - return false; - } - notifier.notify(MaaMsg_Controller_TouchInputInited, details); - - notifier.notify(MaaMsg_Controller_ConnectSuccess, details); - - return true; -} - -std::string AdbController::_get_uuid() const -{ - if (!unit_mgr_ || !unit_mgr_->device_info_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->device_info_obj()); - return {}; - } - - return unit_mgr_->device_info_obj()->get_uuid(); -} - -std::pair AdbController::_get_resolution() const -{ - return resolution_; -} - -bool AdbController::_click(ClickParam param) -{ - if (!unit_mgr_ || !unit_mgr_->touch_input_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->touch_input_obj()); - return false; - } - - bool ret = unit_mgr_->touch_input_obj()->click(param.x, param.y); - - if (!ret) { - LogError << "failed to click"; - } - - return ret; -} - -bool AdbController::_swipe(SwipeParam param) -{ - if (!unit_mgr_ || !unit_mgr_->touch_input_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->touch_input_obj()); - return false; - } - - bool ret = unit_mgr_->touch_input_obj()->swipe(param.x1, param.y1, param.x2, param.y2, param.duration); - - if (!ret) { - LogError << "failed to swipe"; - } - - return ret; -} - -bool AdbController::_touch_down(TouchParam param) -{ - if (!unit_mgr_ || !unit_mgr_->touch_input_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->touch_input_obj()); - return false; - } - - bool ret = unit_mgr_->touch_input_obj()->touch_down(param.contact, param.x, param.y, param.pressure); - - if (!ret) { - LogError << "failed to touch_down"; - } - - return ret; -} - -bool AdbController::_touch_move(TouchParam param) -{ - if (!unit_mgr_ || !unit_mgr_->touch_input_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->touch_input_obj()); - return false; - } - - bool ret = unit_mgr_->touch_input_obj()->touch_move(param.contact, param.x, param.y, param.pressure); - - if (!ret) { - LogError << "failed to touch_move"; - } - - return ret; -} - -bool AdbController::_touch_up(TouchParam param) -{ - if (!unit_mgr_ || !unit_mgr_->touch_input_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->touch_input_obj()); - return false; - } - - bool ret = unit_mgr_->touch_input_obj()->touch_up(param.contact); - - if (!ret) { - LogError << "failed to touch_up"; - } - - return ret; -} - -bool AdbController::_press_key(PressKeyParam param) -{ - if (!unit_mgr_ || !unit_mgr_->key_input_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->key_input_obj()); - return false; - } - - bool ret = unit_mgr_->key_input_obj()->press_key(param.keycode); - - if (!ret) { - LogError << "failed to press_key"; - } - - return ret; -} - -cv::Mat AdbController::_screencap() -{ - if (!unit_mgr_ || !unit_mgr_->screencap_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->screencap_obj()); - return {}; - } - - auto ret = unit_mgr_->screencap_obj()->screencap(); - if (!ret) { - return cv::Mat(); - } - return std::move(ret.value()); -} - -bool AdbController::_start_app(AppParam param) -{ - if (!unit_mgr_ || !unit_mgr_->activity_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->activity_obj()); - return false; - } - - return unit_mgr_->activity_obj()->start(param.package) && reinit_resolution(); -} - -bool AdbController::_stop_app(AppParam param) -{ - if (!unit_mgr_ || !unit_mgr_->activity_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->activity_obj()); - return false; - } - - return unit_mgr_->activity_obj()->stop(param.package) && reinit_resolution(); -} - -bool AdbController::reinit_resolution() -{ - LogFunc; - - if (!unit_mgr_ || !unit_mgr_->device_info_obj() || !unit_mgr_->screencap_obj() || !unit_mgr_->touch_input_obj()) { - LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->device_info_obj()) - << VAR(unit_mgr_->screencap_obj()) << VAR(unit_mgr_->touch_input_obj()); - return false; - } - - json::value details = { - { "adb", path_to_utf8_string(adb_path_) }, - { "address", address_ }, - { "uuid", unit_mgr_->device_info_obj()->get_uuid() }, - }; - - if (!unit_mgr_->device_info_obj()->request_resolution()) { - notifier.notify(MaaMsg_Controller_ResolutionGetFailed, details); - LogError << "failed to request_resolution"; - return false; - } - auto [w, h] = unit_mgr_->device_info_obj()->get_resolution(); - resolution_ = { w, h }; - int orientation = unit_mgr_->device_info_obj()->request_orientation().value_or(0); - details |= { { "resolution", { { "width", w }, { "height", h } } }, { "orientation", orientation } }; - - notifier.notify(MaaMsg_Controller_ResolutionGot, details); - - unit_mgr_->screencap_obj()->set_wh(w, h); - unit_mgr_->touch_input_obj()->set_wh(w, h, orientation); - - return true; -} - -MAA_CTRL_NS_END diff --git a/source/MaaFramework/Controller/AdbController.h b/source/MaaFramework/Controller/AdbController.h deleted file mode 100644 index b58cfe7f2..000000000 --- a/source/MaaFramework/Controller/AdbController.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include "ControllerMgr.h" - -#include "ControlUnit/AdbControlUnitAPI.h" - -#include - -MAA_CTRL_NS_BEGIN - -class AdbController : public ControllerMgr -{ -public: - AdbController(std::string adb_path, std::string address, - std::shared_ptr unit_mgr, MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg); - virtual ~AdbController() override; - -protected: - virtual bool _connect() override; - virtual std::string _get_uuid() const override; - virtual std::pair _get_resolution() const override; - virtual bool _click(ClickParam param) override; - virtual bool _swipe(SwipeParam param) override; - virtual bool _touch_down(TouchParam param) override; - virtual bool _touch_move(TouchParam param) override; - virtual bool _touch_up(TouchParam param) override; - virtual bool _press_key(PressKeyParam param) override; - virtual cv::Mat _screencap() override; - virtual bool _start_app(AppParam param) override; - virtual bool _stop_app(AppParam param) override; - -private: - bool reinit_resolution(); - - std::string adb_path_; - std::string address_; - std::pair resolution_ = { 0, 0 }; - std::shared_ptr unit_mgr_ = nullptr; -}; - -MAA_CTRL_NS_END diff --git a/source/MaaFramework/Controller/ControllerMgr.cpp b/source/MaaFramework/Controller/ControllerAgent.cpp similarity index 77% rename from source/MaaFramework/Controller/ControllerMgr.cpp rename to source/MaaFramework/Controller/ControllerAgent.cpp index f34ee4722..05b9b3b72 100644 --- a/source/MaaFramework/Controller/ControllerMgr.cpp +++ b/source/MaaFramework/Controller/ControllerAgent.cpp @@ -1,4 +1,4 @@ -#include "ControllerMgr.h" +#include "ControllerAgent.h" #include "MaaFramework/MaaMsg.h" #include "Option/GlobalOptionMgr.h" @@ -10,18 +10,18 @@ MAA_CTRL_NS_BEGIN -std::minstd_rand ControllerMgr::rand_engine_(std::random_device {}()); +std::minstd_rand ControllerAgent::rand_engine_(std::random_device {}()); -ControllerMgr::ControllerMgr(MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) +ControllerAgent::ControllerAgent(MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) : notifier(callback, callback_arg) { LogFunc << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg); action_runner_ = std::make_unique>( - std::bind(&ControllerMgr::run_action, this, std::placeholders::_1, std::placeholders::_2)); + std::bind(&ControllerAgent::run_action, this, std::placeholders::_1, std::placeholders::_2)); } -ControllerMgr::~ControllerMgr() +ControllerAgent::~ControllerAgent() { LogFunc; @@ -30,7 +30,7 @@ ControllerMgr::~ControllerMgr() } } -bool ControllerMgr::set_option(MaaCtrlOption key, MaaOptionValue value, MaaOptionValueSize val_size) +bool ControllerAgent::set_option(MaaCtrlOption key, MaaOptionValue value, MaaOptionValueSize val_size) { LogInfo << VAR(key) << VAR(value) << VAR(val_size); @@ -54,7 +54,7 @@ bool ControllerMgr::set_option(MaaCtrlOption key, MaaOptionValue value, MaaOptio } } -MaaCtrlId ControllerMgr::post_connection() +MaaCtrlId ControllerAgent::post_connection() { auto id = action_runner_->post({ .type = Action::Type::connect }); std::unique_lock lock { post_ids_mutex_ }; @@ -62,7 +62,7 @@ MaaCtrlId ControllerMgr::post_connection() return id; } -MaaCtrlId ControllerMgr::post_click(int x, int y) +MaaCtrlId ControllerAgent::post_click(int x, int y) { auto [xx, yy] = preproc_touch_point(x, y); ClickParam param { .x = xx, .y = yy }; @@ -72,7 +72,7 @@ MaaCtrlId ControllerMgr::post_click(int x, int y) return id; } -MaaCtrlId ControllerMgr::post_swipe(int x1, int y1, int x2, int y2, int duration) +MaaCtrlId ControllerAgent::post_swipe(int x1, int y1, int x2, int y2, int duration) { auto [xx1, yy1] = preproc_touch_point(x1, y1); auto [xx2, yy2] = preproc_touch_point(x2, y2); @@ -83,7 +83,7 @@ MaaCtrlId ControllerMgr::post_swipe(int x1, int y1, int x2, int y2, int duration return id; } -MaaCtrlId ControllerMgr::post_press_key(int keycode) +MaaCtrlId ControllerAgent::post_press_key(int keycode) { PressKeyParam param { .keycode = keycode }; auto id = action_runner_->post({ .type = Action::Type::press_key, .param = std::move(param) }); @@ -92,7 +92,7 @@ MaaCtrlId ControllerMgr::post_press_key(int keycode) return id; } -MaaCtrlId ControllerMgr::post_screencap() +MaaCtrlId ControllerAgent::post_screencap() { auto id = action_runner_->post({ .type = Action::Type::screencap }); std::unique_lock lock { post_ids_mutex_ }; @@ -100,7 +100,7 @@ MaaCtrlId ControllerMgr::post_screencap() return id; } -MaaCtrlId ControllerMgr::post_touch_down(int contact, int x, int y, int pressure) +MaaCtrlId ControllerAgent::post_touch_down(int contact, int x, int y, int pressure) { auto [xx, yy] = preproc_touch_point(x, y); TouchParam param { .contact = contact, .x = xx, .y = yy, .pressure = pressure }; @@ -110,7 +110,7 @@ MaaCtrlId ControllerMgr::post_touch_down(int contact, int x, int y, int pressure return id; } -MaaCtrlId ControllerMgr::post_touch_move(int contact, int x, int y, int pressure) +MaaCtrlId ControllerAgent::post_touch_move(int contact, int x, int y, int pressure) { auto [xx, yy] = preproc_touch_point(x, y); TouchParam param { .contact = contact, .x = xx, .y = yy, .pressure = pressure }; @@ -120,7 +120,7 @@ MaaCtrlId ControllerMgr::post_touch_move(int contact, int x, int y, int pressure return id; } -MaaCtrlId ControllerMgr::post_touch_up(int contact) +MaaCtrlId ControllerAgent::post_touch_up(int contact) { TouchParam param { .contact = contact }; auto id = action_runner_->post({ .type = Action::Type::touch_up, .param = std::move(param) }); @@ -129,7 +129,7 @@ MaaCtrlId ControllerMgr::post_touch_up(int contact) return id; } -MaaStatus ControllerMgr::status(MaaCtrlId ctrl_id) const +MaaStatus ControllerAgent::status(MaaCtrlId ctrl_id) const { if (!action_runner_) { LogError << "action_runner_ is nullptr"; @@ -138,7 +138,7 @@ MaaStatus ControllerMgr::status(MaaCtrlId ctrl_id) const return action_runner_->status(ctrl_id); } -MaaStatus ControllerMgr::wait(MaaCtrlId ctrl_id) const +MaaStatus ControllerAgent::wait(MaaCtrlId ctrl_id) const { if (!action_runner_) { LogError << "action_runner_ is nullptr"; @@ -148,55 +148,66 @@ MaaStatus ControllerMgr::wait(MaaCtrlId ctrl_id) const return action_runner_->status(ctrl_id); } -MaaBool ControllerMgr::connected() const +MaaBool ControllerAgent::connected() const { return connected_; } -cv::Mat ControllerMgr::get_image() const +cv::Mat ControllerAgent::get_image() { return image_; } -std::string ControllerMgr::get_uuid() const +std::string ControllerAgent::get_uuid() { - return _get_uuid(); + if (uuid_cache_.empty()) { + request_uuid(); + } + return uuid_cache_; } -void ControllerMgr::on_stop() +std::pair ControllerAgent::get_resolution() +{ + if (resolution_cache_.first == 0 || resolution_cache_.second == 0) { + request_resolution(); + } + return resolution_cache_; +} + +void ControllerAgent::on_stop() { action_runner_->release(); } -bool ControllerMgr::click(const cv::Rect& r) +bool ControllerAgent::click(const cv::Rect& r) { return click(rand_point(r)); } -bool ControllerMgr::click(const cv::Point& p) +bool ControllerAgent::click(const cv::Point& p) { auto id = post_click(p.x, p.y); return wait(id) == MaaStatus_Success; } -bool ControllerMgr::swipe(const cv::Rect& r1, const cv::Rect& r2, int duration) +bool ControllerAgent::swipe(const cv::Rect& r1, const cv::Rect& r2, int duration) { return swipe(rand_point(r1), rand_point(r2), duration); } -bool ControllerMgr::swipe(const cv::Point& p1, const cv::Point& p2, int duration) +bool ControllerAgent::swipe(const cv::Point& p1, const cv::Point& p2, int duration) { auto id = post_swipe(p1.x, p1.y, p2.x, p2.y, duration); return wait(id) == MaaStatus_Success; } -bool ControllerMgr::press_key(int keycode) +bool ControllerAgent::press_key(int keycode) { auto id = post_press_key(keycode); return wait(id) == MaaStatus_Success; } -cv::Mat ControllerMgr::screencap() +cv::Mat ControllerAgent::screencap() { std::unique_lock lock(image_mutex_); auto id = action_runner_->post({ .type = Action::Type::screencap }, true); @@ -206,7 +217,7 @@ cv::Mat ControllerMgr::screencap() return image_.clone(); } -bool ControllerMgr::start_app() +bool ControllerAgent::start_app() { if (default_app_package_entry_.empty()) { LogError << "default_app_package_entry_ is empty"; @@ -215,7 +226,7 @@ bool ControllerMgr::start_app() return start_app(default_app_package_entry_); } -bool ControllerMgr::stop_app() +bool ControllerAgent::stop_app() { if (default_app_package_.empty()) { LogError << "default_app_package_ is empty"; @@ -224,19 +235,19 @@ bool ControllerMgr::stop_app() return stop_app(default_app_package_); } -bool ControllerMgr::start_app(const std::string& package) +bool ControllerAgent::start_app(const std::string& package) { auto id = action_runner_->post({ .type = Action::Type::start_app, .param = AppParam { .package = package } }, true); return wait(id) == MaaStatus_Success; } -bool ControllerMgr::stop_app(const std::string& package) +bool ControllerAgent::stop_app(const std::string& package) { auto id = action_runner_->post({ .type = Action::Type::stop_app, .param = AppParam { .package = package } }, true); return wait(id) == MaaStatus_Success; } -bool ControllerMgr::handle_connect() +bool ControllerAgent::handle_connect() { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -247,11 +258,15 @@ bool ControllerMgr::handle_connect() connected_ = _connect(); + request_uuid(); + request_resolution(); + if (recording()) { - auto [res_w, res_h] = _get_resolution(); json::value info { - { "type", "connect" }, { "success", connected_ }, - { "uuid", _get_uuid() }, { "resolution", { { "width", res_w }, { "height", res_h } } }, + { "type", "connect" }, + { "success", connected_ }, + { "uuid", get_uuid() }, + { "resolution", { { "width", get_resolution().first }, { "height", get_resolution().second } } }, { "version", MAA_VERSION }, }; append_recording(std::move(info), start_time, connected_); @@ -260,7 +275,7 @@ bool ControllerMgr::handle_connect() return connected_; } -bool ControllerMgr::handle_click(const ClickParam& param) +bool ControllerAgent::handle_click(const ClickParam& param) { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -281,7 +296,7 @@ bool ControllerMgr::handle_click(const ClickParam& param) return ret; } -bool ControllerMgr::handle_swipe(const SwipeParam& param) +bool ControllerAgent::handle_swipe(const SwipeParam& param) { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -301,7 +316,7 @@ bool ControllerMgr::handle_swipe(const SwipeParam& param) return ret; } -bool ControllerMgr::handle_touch_down(const TouchParam& param) +bool ControllerAgent::handle_touch_down(const TouchParam& param) { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -321,7 +336,7 @@ bool ControllerMgr::handle_touch_down(const TouchParam& param) return ret; } -bool ControllerMgr::handle_touch_move(const TouchParam& param) +bool ControllerAgent::handle_touch_move(const TouchParam& param) { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -341,7 +356,7 @@ bool ControllerMgr::handle_touch_move(const TouchParam& param) return ret; } -bool ControllerMgr::handle_touch_up(const TouchParam& param) +bool ControllerAgent::handle_touch_up(const TouchParam& param) { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -361,7 +376,7 @@ bool ControllerMgr::handle_touch_up(const TouchParam& param) return ret; } -bool ControllerMgr::handle_press_key(const PressKeyParam& param) +bool ControllerAgent::handle_press_key(const PressKeyParam& param) { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -381,14 +396,20 @@ bool ControllerMgr::handle_press_key(const PressKeyParam& param) return ret; } -bool ControllerMgr::handle_screencap() +bool ControllerAgent::handle_screencap() { std::chrono::steady_clock::time_point start_time; if (recording()) { start_time = std::chrono::steady_clock::now(); } - cv::Mat raw_image = _screencap(); + auto opt = _screencap(); + if (!opt) { + LogError << "controller screencap failed"; + return false; + } + + cv::Mat raw_image = std::move(*opt); bool ret = postproc_screenshot(raw_image); if (recording()) { @@ -406,7 +427,7 @@ bool ControllerMgr::handle_screencap() return ret; } -bool ControllerMgr::handle_start_app(const AppParam& param) +bool ControllerAgent::handle_start_app(const AppParam& param) { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -414,7 +435,7 @@ bool ControllerMgr::handle_start_app(const AppParam& param) } bool ret = _start_app(param); - clear_target_image_size(); + request_resolution(); if (recording()) { json::value info = { @@ -426,7 +447,7 @@ bool ControllerMgr::handle_start_app(const AppParam& param) return ret; } -bool ControllerMgr::handle_stop_app(const AppParam& param) +bool ControllerAgent::handle_stop_app(const AppParam& param) { std::chrono::steady_clock::time_point start_time; if (recording()) { @@ -434,7 +455,7 @@ bool ControllerMgr::handle_stop_app(const AppParam& param) } bool ret = _stop_app(param); - clear_target_image_size(); + request_resolution(); if (recording()) { json::value info = { @@ -446,20 +467,20 @@ bool ControllerMgr::handle_stop_app(const AppParam& param) return ret; } -bool ControllerMgr::recording() const +bool ControllerAgent::recording() const { return recording_ || GlobalOptionMgr::get_instance().recording(); } -void ControllerMgr::init_recording() +void ControllerAgent::init_recording() { auto recording_dir = GlobalOptionMgr::get_instance().log_dir() / "Recording"; std::filesystem::create_directories(recording_dir); recording_path_ = recording_dir / MAA_FMT::format("MaaRecording_{}.txt", now_filestem()); } -void ControllerMgr::append_recording(json::value info, const std::chrono::steady_clock::time_point& start_time, - bool success) +void ControllerAgent::append_recording(json::value info, const std::chrono::steady_clock::time_point& start_time, + bool success) { if (!recording()) { return; @@ -474,7 +495,7 @@ void ControllerMgr::append_recording(json::value info, const std::chrono::steady ofs.close(); } -cv::Point ControllerMgr::rand_point(const cv::Rect& r) +cv::Point ControllerAgent::rand_point(const cv::Rect& r) { int x = 0, y = 0; @@ -497,7 +518,7 @@ cv::Point ControllerMgr::rand_point(const cv::Rect& r) return { x, y }; } -bool ControllerMgr::run_action(typename AsyncRunner::Id id, Action action) +bool ControllerAgent::run_action(typename AsyncRunner::Id id, Action action) { // LogFunc << VAR(id) << VAR(action); @@ -566,9 +587,9 @@ bool ControllerMgr::run_action(typename AsyncRunner::Id id, Action actio return ret; } -std::pair ControllerMgr::preproc_touch_point(int x, int y) +std::pair ControllerAgent::preproc_touch_point(int x, int y) { - auto [res_w, res_h] = _get_resolution(); + auto [res_w, res_h] = get_resolution(); if (image_target_width_ == 0 || image_target_height_ == 0) { // 正常来说连接完后都会截个图测试,那时候就会走到 check_and_calc_target_image_size,这里不应该是 0 @@ -585,7 +606,7 @@ std::pair ControllerMgr::preproc_touch_point(int x, int y) return { proced_x, proced_y }; } -bool ControllerMgr::postproc_screenshot(const cv::Mat& raw) +bool ControllerAgent::postproc_screenshot(const cv::Mat& raw) { if (raw.empty()) { image_ = cv::Mat(); @@ -593,7 +614,7 @@ bool ControllerMgr::postproc_screenshot(const cv::Mat& raw) return false; } - auto [res_w, res_h] = _get_resolution(); + auto [res_w, res_h] = get_resolution(); if (raw.cols != res_w || raw.rows != res_h) { LogWarn << "Invalid resolution" << VAR(raw.cols) << VAR(raw.rows) << VAR(res_w) << VAR(res_h); } @@ -608,7 +629,7 @@ bool ControllerMgr::postproc_screenshot(const cv::Mat& raw) return !image_.empty(); } -bool ControllerMgr::check_and_calc_target_image_size(const cv::Mat& raw) +bool ControllerAgent::check_and_calc_target_image_size(const cv::Mat& raw) { if (image_target_width_ != 0 && image_target_height_ != 0) { return true; @@ -652,13 +673,42 @@ bool ControllerMgr::check_and_calc_target_image_size(const cv::Mat& raw) return true; } -void ControllerMgr::clear_target_image_size() +void ControllerAgent::clear_target_image_size() { image_target_width_ = 0; image_target_height_ = 0; } -bool ControllerMgr::set_image_target_long_side(MaaOptionValue value, MaaOptionValueSize val_size) +bool ControllerAgent::request_uuid() +{ + uuid_cache_.clear(); + + auto uuid_opt = _request_uuid(); + if (!uuid_opt) { + LogError << "controller request uuid failed"; + return false; + } + uuid_cache_ = *uuid_opt; + return true; +} + +bool ControllerAgent::request_resolution() +{ + clear_target_image_size(); + + resolution_cache_ = { 0, 0 }; + + auto resolution_opt = _request_resolution(); + if (!resolution_opt) { + LogError << "controller request resolution failed"; + return false; + } + resolution_cache_ = *resolution_opt; + + return true; +} + +bool ControllerAgent::set_image_target_long_side(MaaOptionValue value, MaaOptionValueSize val_size) { if (val_size != sizeof(image_target_long_side_)) { LogError << "invalid value size: " << val_size; @@ -673,7 +723,7 @@ bool ControllerMgr::set_image_target_long_side(MaaOptionValue value, MaaOptionVa return true; } -bool ControllerMgr::set_image_target_short_side(MaaOptionValue value, MaaOptionValueSize val_size) +bool ControllerAgent::set_image_target_short_side(MaaOptionValue value, MaaOptionValueSize val_size) { if (val_size != sizeof(image_target_short_side_)) { LogError << "invalid value size: " << val_size; @@ -688,21 +738,21 @@ bool ControllerMgr::set_image_target_short_side(MaaOptionValue value, MaaOptionV return true; } -bool ControllerMgr::set_default_app_package_entry(MaaOptionValue value, MaaOptionValueSize val_size) +bool ControllerAgent::set_default_app_package_entry(MaaOptionValue value, MaaOptionValueSize val_size) { std::string_view package(reinterpret_cast(value), val_size); default_app_package_entry_ = package; return true; } -bool ControllerMgr::set_default_app_package(MaaOptionValue value, MaaOptionValueSize val_size) +bool ControllerAgent::set_default_app_package(MaaOptionValue value, MaaOptionValueSize val_size) { std::string_view package(reinterpret_cast(value), val_size); default_app_package_ = package; return true; } -bool ControllerMgr::set_recording(MaaOptionValue value, MaaOptionValueSize val_size) +bool ControllerAgent::set_recording(MaaOptionValue value, MaaOptionValueSize val_size) { if (val_size != sizeof(recording_)) { LogError << "invalid value size: " << val_size; diff --git a/source/MaaFramework/Controller/ControllerMgr.h b/source/MaaFramework/Controller/ControllerAgent.h similarity index 88% rename from source/MaaFramework/Controller/ControllerMgr.h rename to source/MaaFramework/Controller/ControllerAgent.h index 48a005c99..488f1d363 100644 --- a/source/MaaFramework/Controller/ControllerMgr.h +++ b/source/MaaFramework/Controller/ControllerAgent.h @@ -71,12 +71,12 @@ struct Action std::ostream& operator<<(std::ostream& os, const Action& action); -class ControllerMgr : public MaaControllerAPI +class ControllerAgent : public MaaControllerAPI { public: - ControllerMgr(MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); + ControllerAgent(MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); - virtual ~ControllerMgr() override; + virtual ~ControllerAgent() override; virtual bool set_option(MaaCtrlOption key, MaaOptionValue value, MaaOptionValueSize val_size) override; @@ -94,8 +94,9 @@ class ControllerMgr : public MaaControllerAPI virtual MaaStatus wait(MaaCtrlId ctrl_id) const override; virtual MaaBool connected() const override; - virtual cv::Mat get_image() const override; - virtual std::string get_uuid() const override; + virtual cv::Mat get_image() override; + virtual std::string get_uuid() override; + virtual std::pair get_resolution() override; virtual void on_stop() override; @@ -114,17 +115,17 @@ class ControllerMgr : public MaaControllerAPI protected: virtual bool _connect() = 0; - virtual std::string _get_uuid() const = 0; - virtual std::pair _get_resolution() const = 0; + virtual std::optional _request_uuid() = 0; + virtual std::optional> _request_resolution() = 0; + virtual bool _start_app(AppParam param) = 0; + virtual bool _stop_app(AppParam param) = 0; + virtual std::optional _screencap() = 0; virtual bool _click(ClickParam param) = 0; virtual bool _swipe(SwipeParam param) = 0; virtual bool _touch_down(TouchParam param) = 0; virtual bool _touch_move(TouchParam param) = 0; virtual bool _touch_up(TouchParam param) = 0; virtual bool _press_key(PressKeyParam param) = 0; - virtual cv::Mat _screencap() = 0; - virtual bool _start_app(AppParam param) = 0; - virtual bool _stop_app(AppParam param) = 0; protected: MessageNotifier notifier; @@ -153,6 +154,8 @@ class ControllerMgr : public MaaControllerAPI bool postproc_screenshot(const cv::Mat& raw); bool check_and_calc_target_image_size(const cv::Mat& raw); void clear_target_image_size(); + bool request_uuid(); + bool request_resolution(); private: // options bool set_image_target_long_side(MaaOptionValue value, MaaOptionValueSize val_size); @@ -176,6 +179,9 @@ class ControllerMgr : public MaaControllerAPI int image_target_width_ = 0; int image_target_height_ = 0; + std::string uuid_cache_; + std::pair resolution_cache_ = { 0, 0 }; + std::string default_app_package_entry_; std::string default_app_package_; diff --git a/source/MaaFramework/Controller/CustomController.cpp b/source/MaaFramework/Controller/CustomControllerAgent.cpp similarity index 62% rename from source/MaaFramework/Controller/CustomController.cpp rename to source/MaaFramework/Controller/CustomControllerAgent.cpp index 3eba26443..38f4f86a5 100644 --- a/source/MaaFramework/Controller/CustomController.cpp +++ b/source/MaaFramework/Controller/CustomControllerAgent.cpp @@ -1,4 +1,4 @@ -#include "CustomController.h" +#include "CustomControllerAgent.h" #include "Buffer/ImageBuffer.hpp" #include "Buffer/StringBuffer.hpp" @@ -8,12 +8,12 @@ MAA_CTRL_NS_BEGIN -CustomController::CustomController(MaaCustomControllerHandle handle, MaaTransparentArg handle_arg, - MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) - : ControllerMgr(callback, callback_arg), handle_(handle), handle_arg_(handle_arg) +CustomControllerAgent::CustomControllerAgent(MaaCustomControllerHandle handle, MaaTransparentArg handle_arg, + MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) + : ControllerAgent(callback, callback_arg), handle_(handle), handle_arg_(handle_arg) {} -bool CustomController::_connect() +bool CustomControllerAgent::_connect() { LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->connect); @@ -25,37 +25,80 @@ bool CustomController::_connect() return handle_->connect(handle_arg_); } -std::string CustomController::_get_uuid() const +std::optional CustomControllerAgent::_request_uuid() { - LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->get_uuid); + LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->request_uuid); - if (!handle_ || !handle_->get_uuid) { - LogError << "handle_ or handle_->get_uuid is nullptr"; - return {}; + if (!handle_ || !handle_->request_uuid) { + LogError << "handle_ or handle_->request_uuid is nullptr"; + return std::nullopt; } StringBuffer buffer; - MaaBool ret = handle_->get_uuid(handle_arg_, &buffer); + MaaBool ret = handle_->request_uuid(handle_arg_, &buffer); if (!ret) { - LogError << "failed to get_uuid" << VAR(ret); - return {}; + LogError << "failed to request_uuid" << VAR(ret); + return std::nullopt; } return buffer.get(); } -std::pair CustomController::_get_resolution() const +std::optional> CustomControllerAgent::_request_resolution() { - LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->get_resolution); - if (!handle_ || !handle_->get_resolution) { - LogError << "handle_ or handle_->get_resolution is nullptr"; + LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->request_resolution); + if (!handle_ || !handle_->request_resolution) { + LogError << "handle_ or handle_->request_resolution is nullptr"; return {}; } int width = 0, height = 0; - handle_->get_resolution(handle_arg_, &width, &height); - return { width, height }; + handle_->request_resolution(handle_arg_, &width, &height); + return std::make_pair(width, height); +} + +bool CustomControllerAgent::_start_app(AppParam param) +{ + LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->start_app) << VAR(param.package); + + if (!handle_ || !handle_->start_app) { + LogError << "handle_ or handle_->start_app is nullptr"; + return false; + } + + return handle_->start_app(param.package.c_str(), handle_arg_); +} + +bool CustomControllerAgent::_stop_app(AppParam param) +{ + LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->stop_app) << VAR(param.package); + + if (!handle_ || !handle_->stop_app) { + LogError << "handle_ or handle_->stop_app is nullptr"; + return false; + } + + return handle_->stop_app(param.package.c_str(), handle_arg_); +} + +std::optional CustomControllerAgent::_screencap() +{ + LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->screencap); + + if (!handle_ || !handle_->screencap) { + LogError << "handle_ or handle_->screencap is nullptr"; + return std::nullopt; + } + + ImageBuffer buffer; + MaaBool ret = handle_->screencap(handle_arg_, &buffer); + if (!ret) { + LogError << "failed to get_image" << VAR(ret); + return std::nullopt; + } + + return buffer.get(); } -bool CustomController::_click(ClickParam param) +bool CustomControllerAgent::_click(ClickParam param) { LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->click) << VAR(param.x) << VAR(param.y); @@ -67,7 +110,7 @@ bool CustomController::_click(ClickParam param) return handle_->click(param.x, param.y, handle_arg_); } -bool CustomController::_swipe(SwipeParam param) +bool CustomControllerAgent::_swipe(SwipeParam param) { LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->swipe) << VAR(param.x1) << VAR(param.x2) << VAR(param.y1) << VAR(param.y2) << VAR(param.duration); @@ -79,7 +122,7 @@ bool CustomController::_swipe(SwipeParam param) return handle_->swipe(param.x1, param.y1, param.x2, param.y2, param.duration, handle_arg_); } -bool CustomController::_touch_down(TouchParam param) +bool CustomControllerAgent::_touch_down(TouchParam param) { LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->touch_down) << VAR(param.contact) << VAR(param.x) << VAR(param.y) << VAR(param.pressure); @@ -92,7 +135,7 @@ bool CustomController::_touch_down(TouchParam param) return handle_->touch_down(param.contact, param.x, param.y, param.pressure, handle_arg_); } -bool CustomController::_touch_move(TouchParam param) +bool CustomControllerAgent::_touch_move(TouchParam param) { LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->touch_move) << VAR(param.contact) << VAR(param.x) << VAR(param.y) << VAR(param.pressure); @@ -105,7 +148,7 @@ bool CustomController::_touch_move(TouchParam param) return handle_->touch_move(param.contact, param.x, param.y, param.pressure, handle_arg_); } -bool CustomController::_touch_up(TouchParam param) +bool CustomControllerAgent::_touch_up(TouchParam param) { LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->touch_up) << VAR(param.contact); @@ -117,7 +160,7 @@ bool CustomController::_touch_up(TouchParam param) return handle_->touch_up(param.contact, handle_arg_); } -bool CustomController::_press_key(PressKeyParam param) +bool CustomControllerAgent::_press_key(PressKeyParam param) { LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->press_key) << VAR(param.keycode); @@ -129,47 +172,4 @@ bool CustomController::_press_key(PressKeyParam param) return handle_->press_key(param.keycode, handle_arg_); } -cv::Mat CustomController::_screencap() -{ - LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->get_image); - - if (!handle_ || !handle_->get_image) { - LogError << "handle_ or handle_->get_image is nullptr"; - return {}; - } - - ImageBuffer buffer; - MaaBool ret = handle_->get_image(handle_arg_, &buffer); - if (!ret) { - LogError << "failed to get_image" << VAR(ret); - return {}; - } - - return buffer.get(); -} - -bool CustomController::_start_app(AppParam param) -{ - LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->start_app) << VAR(param.package); - - if (!handle_ || !handle_->start_app) { - LogError << "handle_ or handle_->start_app is nullptr"; - return false; - } - - return handle_->start_app(param.package.c_str(), handle_arg_); -} - -bool CustomController::_stop_app(AppParam param) -{ - LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->stop_app) << VAR(param.package); - - if (!handle_ || !handle_->stop_app) { - LogError << "handle_ or handle_->stop_app is nullptr"; - return false; - } - - return handle_->stop_app(param.package.c_str(), handle_arg_); -} - MAA_CTRL_NS_END diff --git a/source/MaaFramework/Controller/CustomController.h b/source/MaaFramework/Controller/CustomControllerAgent.h similarity index 56% rename from source/MaaFramework/Controller/CustomController.h rename to source/MaaFramework/Controller/CustomControllerAgent.h index 359ea8435..ee00b6242 100644 --- a/source/MaaFramework/Controller/CustomController.h +++ b/source/MaaFramework/Controller/CustomControllerAgent.h @@ -1,29 +1,29 @@ #pragma once -#include "ControllerMgr.h" +#include "ControllerAgent.h" #include "MaaFramework/MaaDef.h" MAA_CTRL_NS_BEGIN -class CustomController : public ControllerMgr +class CustomControllerAgent : public ControllerAgent { public: - CustomController(MaaCustomControllerHandle handle, MaaTransparentArg handle_arg, MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg); - virtual ~CustomController() override = default; + CustomControllerAgent(MaaCustomControllerHandle handle, MaaTransparentArg handle_arg, + MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); + virtual ~CustomControllerAgent() override = default; protected: virtual bool _connect() override; - virtual std::string _get_uuid() const override; - virtual std::pair _get_resolution() const override; + virtual std::optional _request_uuid() override; + virtual std::optional> _request_resolution() override; + virtual bool _start_app(AppParam param) override; + virtual bool _stop_app(AppParam param) override; + virtual std::optional _screencap() override; virtual bool _click(ClickParam param) override; virtual bool _swipe(SwipeParam param) override; virtual bool _touch_down(TouchParam param) override; virtual bool _touch_move(TouchParam param) override; virtual bool _touch_up(TouchParam param) override; virtual bool _press_key(PressKeyParam param) override; - virtual cv::Mat _screencap() override; - virtual bool _start_app(AppParam param) override; - virtual bool _stop_app(AppParam param) override; private: MaaCustomControllerHandle handle_ = nullptr; diff --git a/source/MaaFramework/Controller/CustomThriftController.cpp b/source/MaaFramework/Controller/CustomThriftController.cpp deleted file mode 100644 index 1c669b2b9..000000000 --- a/source/MaaFramework/Controller/CustomThriftController.cpp +++ /dev/null @@ -1,273 +0,0 @@ -#ifdef WITH_THRIFT_CONTROLLER - -#include "CustomThriftController.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" -#include "Utils/NoWarningCV.hpp" - -MAA_CTRL_NS_BEGIN - -CustomThriftController::CustomThriftController(const std::string& param_str, MaaControllerCallback callback, - void* callback_arg) - : ControllerMgr(callback, callback_arg) -{ - LogDebug << VAR(param_str); - - static const std::unordered_map type_map = { - { "Socket", ThriftControllerTypeEnum::MaaThriftControllerType_Socket }, - { "UnixDomainSocket", ThriftControllerTypeEnum::MaaThriftControllerType_UnixDomainSocket }, - }; - - using namespace apache::thrift; - - std::shared_ptr socket; - - auto param_json = json::parse(param_str); - if (!param_json.has_value()) { - LogError << "Invalid param: " << param_str; - throw std::runtime_error("MaaThriftController: Invalid param"); - } - - auto type = param_json->at("type").as_string(); - - switch (type_map.at(type)) { - case ThriftControllerTypeEnum::MaaThriftControllerType_Socket: - if (param_json->at("param").is_object()) { - auto host = param_json->at("param").at("host").as_string(); - auto port = param_json->at("param").at("port").as_integer(); - socket = std::make_shared(host, port); - } - else { - LogError << "Invalid param: " << param_json->at("param"); - throw std::runtime_error("MaaThriftController: Invalid param"); - } - break; - case ThriftControllerTypeEnum::MaaThriftControllerType_UnixDomainSocket: - if (param_json->at("param").is_string()) { - auto path = param_json->at("param").as_string(); - socket = std::make_shared(path); - } - else { - LogError << "Invalid param: " << param_json->at("param"); - throw std::runtime_error("MaaThriftController: Invalid param"); - } - break; - default: - LogError << "Unknown type: " << type; - throw std::runtime_error("MaaThriftController: Unknown type"); - } - - transport_ = std::make_shared(socket); - auto protocol = std::make_shared(transport_); - - client_ = std::make_shared(protocol); -} - -CustomThriftController::~CustomThriftController() -{ - if (transport_) { - transport_->close(); - } -} - -bool CustomThriftController::_connect() -{ - try { - transport_->open(); - } - catch (const std::exception& e) { - LogError << "transport_->open() failed: " << e.what(); - return false; - } - - return client_->connect(); -} - -std::string CustomThriftController::_get_uuid() const -{ - LogFunc; - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return {}; - } - - std::string uuid; - client_->get_uuid(uuid); - return uuid; -} - -std::pair CustomThriftController::_get_resolution() const -{ - LogFunc; - - if (!client_ || !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 CustomThriftController::_click(ClickParam param) -{ - LogFunc << VAR(param.x) << VAR(param.y); - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return false; - } - - ThriftController::ClickParam thrift_param; - thrift_param.point.x = param.x; - thrift_param.point.y = param.y; - - return client_->click(thrift_param); -} - -bool CustomThriftController::_swipe(SwipeParam param) -{ - LogFunc << VAR(param.x1) << VAR(param.x2) << VAR(param.y1) << VAR(param.y2) << VAR(param.duration); - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return false; - } - ThriftController::SwipeParam thrift_param; - thrift_param.point1.x = param.x1; - thrift_param.point1.y = param.y1; - thrift_param.point2.x = param.x2; - thrift_param.point2.y = param.y2; - thrift_param.duration = param.duration; - return client_->swipe(thrift_param); -} - -bool CustomThriftController::_touch_down(TouchParam param) -{ - LogFunc << VAR(param.contact) << VAR(param.x) << VAR(param.y) << VAR(param.pressure); - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return false; - } - - ThriftController::TouchParam thrift_param; - thrift_param.contact = param.contact; - thrift_param.point.x = param.x; - thrift_param.point.y = param.y; - thrift_param.pressure = param.pressure; - - return client_->touch_down(thrift_param); -} - -bool CustomThriftController::_touch_move(TouchParam param) -{ - LogFunc << VAR(param.contact) << VAR(param.x) << VAR(param.y) << VAR(param.pressure); - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return false; - } - - ThriftController::TouchParam thrift_param; - thrift_param.contact = param.contact; - thrift_param.point.x = param.x; - thrift_param.point.y = param.y; - thrift_param.pressure = param.pressure; - - return client_->touch_move(thrift_param); -} - -bool CustomThriftController::_touch_up(TouchParam param) -{ - LogFunc << VAR(param.contact); - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return false; - } - - ThriftController::TouchParam thrift_param; - thrift_param.contact = param.contact; - - return client_->touch_up(thrift_param); -} - -bool CustomThriftController::_press_key(PressKeyParam param) -{ - LogFunc; - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return false; - } - - ThriftController::PressKeyParam thrift_param; - thrift_param.keycode = param.keycode; - - return client_->press_key(thrift_param); -} - -cv::Mat CustomThriftController::_screencap() -{ - LogFunc; - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return {}; - } - - ThriftController::CustomImage img; - client_->screencap(img); - if (img.data.empty()) { - LogError << "client_->screencap() return empty buffer"; - return {}; - } - - cv::Mat orig_mat(img.size.height, img.size.width, img.type, img.data.data()); - cv::Mat out_mat; - orig_mat.copyTo(out_mat); - - return out_mat; -} - -bool CustomThriftController::_start_app(AppParam param) -{ - LogFunc << VAR(param.package); - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return false; - } - - return client_->start_game(param.package); -} - -bool CustomThriftController::_stop_app(AppParam param) -{ - LogFunc << VAR(param.package); - - if (!client_ || !transport_->isOpen()) { - LogError << "client_ is nullptr or transport_ is not open"; - return false; - } - - return client_->stop_game(param.package); -} - -MAA_CTRL_NS_END - -#endif // WITH_THRIFT_CONTROLLER diff --git a/source/MaaFramework/Controller/CustomThriftController.h b/source/MaaFramework/Controller/CustomThriftController.h deleted file mode 100644 index ea538fd39..000000000 --- a/source/MaaFramework/Controller/CustomThriftController.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#ifdef WITH_THRIFT_CONTROLLER - -#include "ControllerMgr.h" - -#include "ThriftController.h" - -MAA_CTRL_NS_BEGIN - -class CustomThriftController : public ControllerMgr -{ -public: - CustomThriftController(const std::string& param, MaaControllerCallback callback, void* callback_arg); - virtual ~CustomThriftController() override; - -protected: - virtual bool _connect() override; - virtual std::string _get_uuid() const override; - virtual std::pair _get_resolution() const override; - virtual bool _click(ClickParam param) override; - virtual bool _swipe(SwipeParam param) override; - virtual bool _touch_down(TouchParam param) override; - virtual bool _touch_move(TouchParam param) override; - virtual bool _touch_up(TouchParam param) override; - virtual bool _press_key(PressKeyParam param) override; - virtual cv::Mat _screencap() override; - virtual bool _start_app(AppParam param) override; - virtual bool _stop_app(AppParam param) override; - -private: - std::shared_ptr client_ = nullptr; - std::shared_ptr transport_ = nullptr; - - enum ThriftControllerTypeEnum - { - // param format should be "host:port" - MaaThriftControllerType_Socket = 1, - - // param should be unix domain socket path - MaaThriftControllerType_UnixDomainSocket = 2, - }; -}; - -MAA_CTRL_NS_END - -#endif // WITH_THRIFT_CONTROLLER diff --git a/source/MaaFramework/Controller/DebuggingController.cpp b/source/MaaFramework/Controller/DebuggingController.cpp deleted file mode 100644 index bcce6aa6b..000000000 --- a/source/MaaFramework/Controller/DebuggingController.cpp +++ /dev/null @@ -1,204 +0,0 @@ -#include "DebuggingController.h" - -#include "MaaFramework/MaaMsg.h" -#include "Utils/Logger.h" -#include "Utils/StringMisc.hpp" - -#include - -MAA_CTRL_NS_BEGIN - -DebuggingController::DebuggingController(std::string read_path, std::string write_path, - std::shared_ptr controller, - MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) - : ControllerMgr(callback, callback_arg), read_path_(std::move(read_path)), write_path_(std::move(write_path)), - controller_(std::move(controller)) -{ - LogDebug << VAR(read_path_) << VAR(write_path_) << VAR(controller_); -} - -DebuggingController::~DebuggingController() {} - -bool DebuggingController::_connect() -{ - LogFunc; - - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->connect()) { - LogError << "controller connect failed"; - return false; - } - - return true; -} - -std::string DebuggingController::_get_uuid() const -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return {}; - } - - return controller_->uuid(); -} - -std::pair DebuggingController::_get_resolution() const -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return { 0, 0 }; - } - - auto [screen_width, screen_height] = controller_->resolution(); - return { screen_width, screen_height }; -} - -bool DebuggingController::_click(ClickParam param) -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->click(param.x, param.y)) { - LogError << "controller click failed" << VAR(param.x) << VAR(param.y); - return false; - } - - return true; -} - -bool DebuggingController::_swipe(SwipeParam param) -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->swipe(param.x1, param.y1, param.x2, param.y2, param.duration)) { - LogError << "controller swipe failed" << VAR(param.x1) << VAR(param.y1) << VAR(param.x2) << VAR(param.y2) - << VAR(param.duration); - return false; - } - - return true; -} - -bool DebuggingController::_touch_down(TouchParam param) -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->touch_down(param.contact, param.x, param.y, param.pressure)) { - LogError << "controller touch_down failed" << VAR(param.contact) << VAR(param.x) << VAR(param.y) - << VAR(param.pressure); - return false; - } - - return true; -} - -bool DebuggingController::_touch_move(TouchParam param) -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->touch_move(param.contact, param.x, param.y, param.pressure)) { - LogError << "controller touch_move failed" << VAR(param.contact) << VAR(param.x) << VAR(param.y) - << VAR(param.pressure); - return false; - } - - return true; -} - -bool DebuggingController::_touch_up(TouchParam param) -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->touch_up(param.contact)) { - LogError << "controller touch_up failed" << VAR(param.contact); - return false; - } - - return true; -} - -bool DebuggingController::_press_key(PressKeyParam param) -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->press_key(param.keycode)) { - LogError << "controller press_key failed" << VAR(param.keycode); - return false; - } - - return true; -} - -cv::Mat DebuggingController::_screencap() -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return {}; - } - - auto opt_mat = controller_->screencap(); - if (!opt_mat) { - LogError << "controller screencap failed"; - return {}; - } - - if (opt_mat->empty()) { - LogError << "controller screencap empty"; - return {}; - } - - return *opt_mat; -} - -bool DebuggingController::_start_app(AppParam param) -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->start_app(param.package)) { - LogError << "controller start failed" << VAR(param.package); - return false; - } - - return true; -} - -bool DebuggingController::_stop_app(AppParam param) -{ - if (!controller_) { - LogError << "controller is nullptr" << VAR(controller_); - return false; - } - - if (!controller_->stop_app(param.package)) { - LogError << "controller stop failed" << VAR(param.package); - return false; - } - - return true; -} - -MAA_CTRL_NS_END diff --git a/source/MaaFramework/Controller/DebuggingController.h b/source/MaaFramework/Controller/DebuggingController.h deleted file mode 100644 index 1992cc4a3..000000000 --- a/source/MaaFramework/Controller/DebuggingController.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include "ControllerMgr.h" - -#include "ControlUnit/DbgControlUnitAPI.h" - -#include - -MAA_CTRL_NS_BEGIN - -class DebuggingController : public ControllerMgr -{ -public: - DebuggingController(std::string read_path, std::string write_path, - std::shared_ptr controller, MaaControllerCallback callback, - MaaCallbackTransparentArg callback_arg); - virtual ~DebuggingController() override; - -protected: - virtual bool _connect() override; - virtual std::string _get_uuid() const override; - virtual std::pair _get_resolution() const override; - virtual bool _click(ClickParam param) override; - virtual bool _swipe(SwipeParam param) override; - virtual bool _touch_down(TouchParam param) override; - virtual bool _touch_move(TouchParam param) override; - virtual bool _touch_up(TouchParam param) override; - virtual bool _press_key(PressKeyParam param) override; - virtual cv::Mat _screencap() override; - virtual bool _start_app(AppParam param) override; - virtual bool _stop_app(AppParam param) override; - -private: - std::string read_path_; - std::string write_path_; - - std::shared_ptr controller_ = nullptr; -}; - -MAA_CTRL_NS_END diff --git a/source/MaaFramework/Controller/GeneralControllerAgent.cpp b/source/MaaFramework/Controller/GeneralControllerAgent.cpp new file mode 100644 index 000000000..848fe2d26 --- /dev/null +++ b/source/MaaFramework/Controller/GeneralControllerAgent.cpp @@ -0,0 +1,211 @@ +#include "GeneralControllerAgent.h" + +#include "MaaFramework/MaaMsg.h" +#include "Utils/Logger.h" +#include "Utils/StringMisc.hpp" + +#include + +MAA_CTRL_NS_BEGIN + +GeneralControllerAgent::GeneralControllerAgent(std::shared_ptr control_unit, + MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) + : ControllerAgent(callback, callback_arg), control_unit_(std::move(control_unit)) +{} + +bool GeneralControllerAgent::_connect() +{ + LogFunc; + + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->connect()) { + LogError << "controller connect failed"; + return false; + } + + return true; +} + +std::optional GeneralControllerAgent::_request_uuid() +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return std::nullopt; + } + + std::string uuid; + if (!control_unit_->request_uuid(uuid)) { + LogError << "controller request_uuid failed"; + return std::nullopt; + } + + return uuid; +} + +std::optional> GeneralControllerAgent::_request_resolution() +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return std::nullopt; + } + + int width = 0; + int height = 0; + + if (!control_unit_->request_resolution(width, height)) { + LogError << "controller request_resolution failed"; + return std::nullopt; + } + + return std::make_pair(width, height); +} + +bool GeneralControllerAgent::_start_app(AppParam param) +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->start_app(param.package)) { + LogError << "controller start failed" << VAR(param.package); + return false; + } + + return true; +} + +bool GeneralControllerAgent::_stop_app(AppParam param) +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->stop_app(param.package)) { + LogError << "controller stop failed" << VAR(param.package); + return false; + } + + return true; +} + +std::optional GeneralControllerAgent::_screencap() +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return std::nullopt; + } + + cv::Mat image; + if (!control_unit_->screencap(image)) { + LogError << "controller screencap failed"; + return std::nullopt; + } + + // if (image.empty()) { + // LogError << "controller screencap empty"; + // return std::nullopt; + // } + + return image; +} + +bool GeneralControllerAgent::_click(ClickParam param) +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->click(param.x, param.y)) { + LogError << "controller click failed" << VAR(param.x) << VAR(param.y); + return false; + } + + return true; +} + +bool GeneralControllerAgent::_swipe(SwipeParam param) +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->swipe(param.x1, param.y1, param.x2, param.y2, param.duration)) { + LogError << "controller swipe failed" << VAR(param.x1) << VAR(param.y1) << VAR(param.x2) << VAR(param.y2) + << VAR(param.duration); + return false; + } + + return true; +} + +bool GeneralControllerAgent::_touch_down(TouchParam param) +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->touch_down(param.contact, param.x, param.y, param.pressure)) { + LogError << "controller touch_down failed" << VAR(param.contact) << VAR(param.x) << VAR(param.y) + << VAR(param.pressure); + return false; + } + + return true; +} + +bool GeneralControllerAgent::_touch_move(TouchParam param) +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->touch_move(param.contact, param.x, param.y, param.pressure)) { + LogError << "controller touch_move failed" << VAR(param.contact) << VAR(param.x) << VAR(param.y) + << VAR(param.pressure); + return false; + } + + return true; +} + +bool GeneralControllerAgent::_touch_up(TouchParam param) +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->touch_up(param.contact)) { + LogError << "controller touch_up failed" << VAR(param.contact); + return false; + } + + return true; +} + +bool GeneralControllerAgent::_press_key(PressKeyParam param) +{ + if (!control_unit_) { + LogError << "controller is nullptr" << VAR(control_unit_); + return false; + } + + if (!control_unit_->press_key(param.keycode)) { + LogError << "controller press_key failed" << VAR(param.keycode); + return false; + } + + return true; +} + +MAA_CTRL_NS_END diff --git a/source/MaaFramework/Controller/GeneralControllerAgent.h b/source/MaaFramework/Controller/GeneralControllerAgent.h new file mode 100644 index 000000000..7719a8612 --- /dev/null +++ b/source/MaaFramework/Controller/GeneralControllerAgent.h @@ -0,0 +1,36 @@ +#pragma once +#include "ControllerAgent.h" + +#include "ControlUnit/DbgControlUnitAPI.h" +#include "Utils/LibraryHolder.h" + +#include + +MAA_CTRL_NS_BEGIN + +class GeneralControllerAgent : public ControllerAgent +{ +public: + GeneralControllerAgent(std::shared_ptr control_unit, + MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); + virtual ~GeneralControllerAgent() override = default; + +protected: + virtual bool _connect() override; + virtual std::optional _request_uuid() override; + virtual std::optional> _request_resolution() override; + virtual bool _start_app(AppParam param) override; + virtual bool _stop_app(AppParam param) override; + virtual std::optional _screencap() override; + virtual bool _click(ClickParam param) override; + virtual bool _swipe(SwipeParam param) override; + virtual bool _touch_down(TouchParam param) override; + virtual bool _touch_move(TouchParam param) override; + virtual bool _touch_up(TouchParam param) override; + virtual bool _press_key(PressKeyParam param) override; + +private: + std::shared_ptr control_unit_ = nullptr; +}; + +MAA_CTRL_NS_END diff --git a/source/MaaFramework/Instance/InstanceInternalAPI.hpp b/source/MaaFramework/Instance/InstanceInternalAPI.hpp index f2037b73c..7cfbb651c 100644 --- a/source/MaaFramework/Instance/InstanceInternalAPI.hpp +++ b/source/MaaFramework/Instance/InstanceInternalAPI.hpp @@ -13,7 +13,7 @@ class ResourceMgr; MAA_RES_NS_END MAA_CTRL_NS_BEGIN -class ControllerMgr; +class ControllerAgent; MAA_CTRL_NS_END MAA_NS_BEGIN @@ -36,7 +36,7 @@ struct InstanceInternalAPI : public NonCopyable { public: virtual MAA_RES_NS::ResourceMgr* inter_resource() = 0; - virtual MAA_CTRL_NS::ControllerMgr* inter_controller() = 0; + virtual MAA_CTRL_NS::ControllerAgent* inter_controller() = 0; virtual InstanceStatus* inter_status() = 0; virtual void notify(std::string_view msg, const json::value& details = json::value()) = 0; virtual MAA_VISION_NS::CustomRecognizerPtr custom_recognizer(const std::string& name) = 0; diff --git a/source/MaaFramework/Instance/InstanceMgr.cpp b/source/MaaFramework/Instance/InstanceMgr.cpp index e62c02243..887e0762d 100644 --- a/source/MaaFramework/Instance/InstanceMgr.cpp +++ b/source/MaaFramework/Instance/InstanceMgr.cpp @@ -1,6 +1,6 @@ #include "InstanceMgr.h" -#include "Controller/ControllerMgr.h" +#include "Controller/ControllerAgent.h" #include "MaaFramework/MaaMsg.h" #include "Resource/ResourceMgr.h" #include "Task/CustomAction.h" @@ -245,9 +245,9 @@ MAA_RES_NS::ResourceMgr* InstanceMgr::inter_resource() return dynamic_cast(resource()); } -MAA_CTRL_NS::ControllerMgr* InstanceMgr::inter_controller() +MAA_CTRL_NS::ControllerAgent* InstanceMgr::inter_controller() { - return dynamic_cast(controller()); + return dynamic_cast(controller()); } InstanceStatus* InstanceMgr::inter_status() diff --git a/source/MaaFramework/Instance/InstanceMgr.h b/source/MaaFramework/Instance/InstanceMgr.h index 2285d84fb..47a7aca71 100644 --- a/source/MaaFramework/Instance/InstanceMgr.h +++ b/source/MaaFramework/Instance/InstanceMgr.h @@ -47,7 +47,7 @@ class InstanceMgr : public MaaInstanceAPI, public InstanceInternalAPI public: // from InstanceInternalAPI virtual MAA_RES_NS::ResourceMgr* inter_resource() override; - virtual MAA_CTRL_NS::ControllerMgr* inter_controller() override; + virtual MAA_CTRL_NS::ControllerAgent* inter_controller() override; virtual InstanceStatus* inter_status() override; virtual void notify(std::string_view msg, const json::value& details = json::value()) override; virtual MAA_VISION_NS::CustomRecognizerPtr custom_recognizer(const std::string& name) override; diff --git a/source/MaaFramework/Task/Actuator.cpp b/source/MaaFramework/Task/Actuator.cpp index fa9a4f25a..2a48c84c8 100644 --- a/source/MaaFramework/Task/Actuator.cpp +++ b/source/MaaFramework/Task/Actuator.cpp @@ -1,6 +1,6 @@ #include "Actuator.h" -#include "Controller/ControllerMgr.h" +#include "Controller/ControllerAgent.h" #include "Instance/InstanceStatus.h" #include "Task/CustomAction.h" #include "Utils/Logger.h" diff --git a/source/MaaFramework/Task/Actuator.h b/source/MaaFramework/Task/Actuator.h index 84c483a4a..f59a4ff93 100644 --- a/source/MaaFramework/Task/Actuator.h +++ b/source/MaaFramework/Task/Actuator.h @@ -42,7 +42,7 @@ class Actuator : public MaaInstanceSink cv::Rect get_target_rect(const MAA_RES_NS::Action::Target target, const cv::Rect& cur_box); private: - MAA_CTRL_NS::ControllerMgr* controller() { return inst_ ? inst_->inter_controller() : nullptr; } + MAA_CTRL_NS::ControllerAgent* controller() { return inst_ ? inst_->inter_controller() : nullptr; } InstanceStatus* status() { return inst_ ? inst_->inter_status() : nullptr; } bool need_exit() const { return need_exit_; } void sleep(unsigned ms) const; diff --git a/source/MaaFramework/Task/PipelineTask.cpp b/source/MaaFramework/Task/PipelineTask.cpp index 6bfd706fa..74a1123a8 100644 --- a/source/MaaFramework/Task/PipelineTask.cpp +++ b/source/MaaFramework/Task/PipelineTask.cpp @@ -2,7 +2,7 @@ #include -#include "Controller/ControllerMgr.h" +#include "Controller/ControllerAgent.h" #include "Instance/InstanceStatus.h" #include "MaaFramework/MaaMsg.h" #include "Resource/ResourceMgr.h" diff --git a/source/MaaFramework/Task/PipelineTask.h b/source/MaaFramework/Task/PipelineTask.h index 8cafe0301..8729af22e 100644 --- a/source/MaaFramework/Task/PipelineTask.h +++ b/source/MaaFramework/Task/PipelineTask.h @@ -60,7 +60,7 @@ class PipelineTask : public MaaInstanceSink private: MAA_RES_NS::ResourceMgr* resource() { return inst_ ? inst_->inter_resource() : nullptr; } - MAA_CTRL_NS::ControllerMgr* controller() { return inst_ ? inst_->inter_controller() : nullptr; } + MAA_CTRL_NS::ControllerAgent* controller() { return inst_ ? inst_->inter_controller() : nullptr; } InstanceStatus* status() { return inst_ ? inst_->inter_status() : nullptr; } void notify(std::string_view msg, json::value detail = json::value()) { diff --git a/source/MaaFramework/Task/SyncContext.cpp b/source/MaaFramework/Task/SyncContext.cpp index 30e2f6265..243ad48ea 100644 --- a/source/MaaFramework/Task/SyncContext.cpp +++ b/source/MaaFramework/Task/SyncContext.cpp @@ -2,7 +2,7 @@ #include -#include "Controller/ControllerMgr.h" +#include "Controller/ControllerAgent.h" #include "Instance/InstanceStatus.h" #include "PipelineTask.h" #include "Utils/Logger.h" 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/MaaRpc/implement/Buffer.h b/source/MaaRpc/implement/Buffer.h index cb4d05fc9..7e96a01fe 100644 --- a/source/MaaRpc/implement/Buffer.h +++ b/source/MaaRpc/implement/Buffer.h @@ -2,7 +2,7 @@ #include "AtomicMap.h" #include "MaaFramework/MaaDef.h" -#include "buffer.grpc.pb.h" +#include "generated/buffer.grpc.pb.h" class ImageImpl final : public ::maarpc::Image::Service { diff --git a/source/MaaRpc/implement/Config.h b/source/MaaRpc/implement/Config.h index 85ebf2f93..fe32c736f 100644 --- a/source/MaaRpc/implement/Config.h +++ b/source/MaaRpc/implement/Config.h @@ -1,7 +1,7 @@ #pragma once #include "MaaToolKit/MaaToolKitAPI.h" -#include "config.grpc.pb.h" +#include "generated/config.grpc.pb.h" class ConfigImpl final : public ::maarpc::Config::Service { diff --git a/source/MaaRpc/implement/Controller.cpp b/source/MaaRpc/implement/Controller.cpp index 85c11316c..2d532b9c8 100644 --- a/source/MaaRpc/implement/Controller.cpp +++ b/source/MaaRpc/implement/Controller.cpp @@ -62,22 +62,6 @@ Status ControllerImpl::destroy(::grpc::ServerContext* context, const ::maarpc::H return Status::OK; } -static MaaBool _set_option(MaaCtrlOption key, MaaStringView value, MaaTransparentArg arg) -{ - auto info = reinterpret_cast(arg); - auto stream = info->stream; - - ::maarpc::CustomControllerResponse response; - response.mutable_set_option()->set_key(key); - response.mutable_set_option()->set_value(value); - stream->Write(response); - - ::maarpc::CustomControllerRequest request; - stream->Read(&request); - - return request.ok(); -} - static MaaBool _connect(MaaTransparentArg arg) { auto info = reinterpret_cast(arg); @@ -134,7 +118,7 @@ static MaaBool _press_key(int32_t key, MaaTransparentArg arg) auto stream = info->stream; ::maarpc::CustomControllerResponse response; - response.mutable_key()->set_key(key); + response.mutable_press_key()->set_key(key); stream->Write(response); ::maarpc::CustomControllerRequest request; @@ -200,7 +184,7 @@ static MaaBool _start_app(MaaStringView entry, MaaTransparentArg arg) auto stream = info->stream; ::maarpc::CustomControllerResponse response; - response.set_start(entry); + response.set_start_app(entry); stream->Write(response); ::maarpc::CustomControllerRequest request; @@ -215,7 +199,7 @@ static MaaBool _stop_app(MaaStringView entry, MaaTransparentArg arg) auto stream = info->stream; ::maarpc::CustomControllerResponse response; - response.set_stop(entry); + response.set_stop_app(entry); stream->Write(response); ::maarpc::CustomControllerRequest request; @@ -224,13 +208,13 @@ static MaaBool _stop_app(MaaStringView entry, MaaTransparentArg arg) return request.ok(); } -static MaaBool _get_resolution(MaaTransparentArg arg, int32_t* width, int32_t* height) +static MaaBool _request_resolution(MaaTransparentArg arg, int32_t* width, int32_t* height) { auto info = reinterpret_cast(arg); auto stream = info->stream; ::maarpc::CustomControllerResponse response; - response.set_resolution(true); + response.set_request_resolution(true); stream->Write(response); ::maarpc::CustomControllerRequest request; @@ -246,7 +230,7 @@ static MaaBool _get_resolution(MaaTransparentArg arg, int32_t* width, int32_t* h return request.ok(); } -static MaaBool _get_image(MaaTransparentArg arg, MaaImageBufferHandle buffer) +static MaaBool _screencap(MaaTransparentArg arg, MaaImageBufferHandle buffer) { auto info = reinterpret_cast(arg); auto stream = info->stream; @@ -255,7 +239,7 @@ static MaaBool _get_image(MaaTransparentArg arg, MaaImageBufferHandle buffer) auto id = make_uuid(); info->image_impl->handles().add(id, buffer); - response.set_image(id); + response.set_screencap(id); stream->Write(response); @@ -267,13 +251,13 @@ static MaaBool _get_image(MaaTransparentArg arg, MaaImageBufferHandle buffer) return request.ok(); } -static MaaBool _get_uuid(MaaTransparentArg arg, MaaStringBufferHandle buffer) +static MaaBool _request_uuid(MaaTransparentArg arg, MaaStringBufferHandle buffer) { auto info = reinterpret_cast(arg); auto stream = info->stream; ::maarpc::CustomControllerResponse response; - response.set_uuid(true); + response.set_request_uuid(true); stream->Write(response); ::maarpc::CustomControllerRequest request; @@ -289,9 +273,20 @@ static MaaBool _get_uuid(MaaTransparentArg arg, MaaStringBufferHandle buffer) return request.ok(); } -static MaaCustomControllerAPI custom_controller_api = { _set_option, _connect, _click, _swipe, _press_key, - _touch_down, _touch_move, _touch_up, _start_app, _stop_app, - _get_resolution, _get_image, _get_uuid }; +static MaaCustomControllerAPI custom_controller_api = { + .connect = _connect, + .request_uuid = _request_uuid, + .request_resolution = _request_resolution, + .start_app = _start_app, + .stop_app = _stop_app, + .screencap = _screencap, + .click = _click, + .swipe = _swipe, + .touch_down = _touch_down, + .touch_move = _touch_move, + .touch_up = _touch_up, + .press_key = _press_key, +}; Status ControllerImpl::create_custom( ServerContext* context, diff --git a/source/MaaRpc/implement/Controller.h b/source/MaaRpc/implement/Controller.h index 54466d991..860794f65 100644 --- a/source/MaaRpc/implement/Controller.h +++ b/source/MaaRpc/implement/Controller.h @@ -4,7 +4,7 @@ #include "Buffer.h" #include "MaaFramework/MaaDef.h" #include "Utility.h" -#include "controller.grpc.pb.h" +#include "generated/controller.grpc.pb.h" #include @@ -13,7 +13,8 @@ class ControllerImpl final : public ::maarpc::Controller::Service public: struct CustomControllerInfo { - ::grpc::ServerReaderWriter<::maarpc::CustomControllerResponse, ::maarpc::CustomControllerRequest>* stream = nullptr; + ::grpc::ServerReaderWriter<::maarpc::CustomControllerResponse, ::maarpc::CustomControllerRequest>* stream = + nullptr; std::shared_ptr image_impl = nullptr; std::binary_semaphore finish { 0 }; }; diff --git a/source/MaaRpc/implement/Device.h b/source/MaaRpc/implement/Device.h index 1ef82d07c..590e54f26 100644 --- a/source/MaaRpc/implement/Device.h +++ b/source/MaaRpc/implement/Device.h @@ -1,7 +1,7 @@ #pragma once #include "MaaToolKit/Device/MaaToolKitDevice.h" -#include "device.grpc.pb.h" +#include "generated/device.grpc.pb.h" class DeviceImpl final : public ::maarpc::Device::Service { diff --git a/source/MaaRpc/implement/Instance.h b/source/MaaRpc/implement/Instance.h index 7cea9fdfc..ad996f785 100644 --- a/source/MaaRpc/implement/Instance.h +++ b/source/MaaRpc/implement/Instance.h @@ -7,7 +7,7 @@ #include "Resource.h" #include "SyncContext.h" #include "Utility.h" -#include "instance.grpc.pb.h" +#include "generated/instance.grpc.pb.h" class InstanceImpl final : public ::maarpc::Instance::Service { @@ -15,7 +15,8 @@ class InstanceImpl final : public ::maarpc::Instance::Service struct CustomRecognizerInfo { std::string name; - ::grpc::ServerReaderWriter<::maarpc::CustomRecognizerResponse, ::maarpc::CustomRecognizerRequest>* stream = nullptr; + ::grpc::ServerReaderWriter<::maarpc::CustomRecognizerResponse, ::maarpc::CustomRecognizerRequest>* stream = + nullptr; std::shared_ptr image_impl = nullptr; std::shared_ptr syncctx_impl = nullptr; std::binary_semaphore finish { 0 }; diff --git a/source/MaaRpc/implement/Resource.h b/source/MaaRpc/implement/Resource.h index ac0d5ff3f..68c68f55d 100644 --- a/source/MaaRpc/implement/Resource.h +++ b/source/MaaRpc/implement/Resource.h @@ -3,7 +3,7 @@ #include "AtomicMap.h" #include "MaaFramework/MaaDef.h" #include "Utility.h" -#include "resource.grpc.pb.h" +#include "generated/resource.grpc.pb.h" class ResourceImpl final : public ::maarpc::Resource::Service { diff --git a/source/MaaRpc/implement/SyncContext.h b/source/MaaRpc/implement/SyncContext.h index d1234b40b..a75af4bad 100644 --- a/source/MaaRpc/implement/SyncContext.h +++ b/source/MaaRpc/implement/SyncContext.h @@ -3,7 +3,7 @@ #include "AtomicMap.h" #include "Buffer.h" #include "MaaFramework/MaaDef.h" -#include "sync.context.grpc.pb.h" +#include "generated/sync.context.grpc.pb.h" class SyncContextImpl final : public ::maarpc::SyncContext::Service { diff --git a/source/MaaRpc/implement/Utility.h b/source/MaaRpc/implement/Utility.h index 8c44df9be..16cdbf795 100644 --- a/source/MaaRpc/implement/Utility.h +++ b/source/MaaRpc/implement/Utility.h @@ -2,7 +2,7 @@ #include "AtomicMap.h" #include "MaaFramework/MaaDef.h" -#include "utility.grpc.pb.h" +#include "generated/utility.grpc.pb.h" #include #include diff --git a/source/MaaThriftControlUnit/API/ThriftControlUnitAPI.cpp b/source/MaaThriftControlUnit/API/ThriftControlUnitAPI.cpp new file mode 100644 index 000000000..5487e0b1e --- /dev/null +++ b/source/MaaThriftControlUnit/API/ThriftControlUnitAPI.cpp @@ -0,0 +1,47 @@ +#include "ControlUnit/ThriftControlUnitAPI.h" + +#include + +#include "Utils/Logger.h" +#include "impl/ThriftControllerSocketAgent.h" +#include "impl/ThriftControllerUnixDomainSocketAgent.h" + +using namespace MAA_CTRL_UNIT_NS; + +MaaStringView get_version() +{ +#pragma message("MaaThriftControlUnit MAA_VERSION: " MAA_VERSION) + + return MAA_VERSION; +} + +MaaControlUnitHandle MAA_CONTROL_UNIT_API create_control_unit( // + MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config) +{ + LogFunc << VAR(type) << VAR(host) << VAR(port) << VAR(config); + + MaaControlUnitHandle handle = nullptr; + + switch (type) { + case MaaThriftControllerType_Socket: + handle = new ThriftControllerSocketAgent(host, port); + break; + + case MaaThriftControllerType_UnixDomainSocket: + handle = new ThriftControllerUnixDomainSocketAgent(host); + break; + } + + LogDebug << VAR_VOIDP(handle); + + return handle; +} + +void destroy_control_unit(MaaControlUnitHandle handle) +{ + LogFunc << VAR_VOIDP(handle); + + if (handle) { + delete handle; + } +} diff --git a/source/MaaThriftControlUnit/CMakeLists.txt b/source/MaaThriftControlUnit/CMakeLists.txt new file mode 100644 index 000000000..5e1d97a4f --- /dev/null +++ b/source/MaaThriftControlUnit/CMakeLists.txt @@ -0,0 +1,31 @@ +generate_thrift_lib( + ThriftControlUnitThrift + ${PROJECT_SOURCE_DIR}/include/Interface/ThriftController.thrift + ${CMAKE_CURRENT_BINARY_DIR}/generated + maa_thrift_controlunit_thrift_src) + +file(GLOB_RECURSE maa_thrift_control_unit_src *.h *.hpp *.cpp) +file(GLOB_RECURSE maa_thrift_control_unit_header ../include/ControlUnit/ThriftControlUnitAPI.h ../include/ControlUnit/ControlUnitAPI.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..6d5c6225f --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerAgent.cpp @@ -0,0 +1,213 @@ +#include "ThriftControllerAgent.h" + +#include "Utils/Logger.h" +#include "Utils/NoWarningCV.hpp" + +MAA_CTRL_UNIT_NS_BEGIN + +ThriftControllerAgent::~ThriftControllerAgent() +{ + LogFunc; + + if (transport_) { + transport_->close(); + } +} + +bool ThriftControllerAgent::find_device(std::vector& devices) +{ + std::ignore = devices; + + return false; +} + +bool ThriftControllerAgent::connect() +{ + LogFunc; + + if (!client_ || !transport_) { + LogError << "client_ or transport_ is nullptr or transport_ is not open"; + return false; + } + + try { + transport_->open(); + } + catch (const std::exception& e) { + LogError << "transport_->open() failed: " << e.what(); + return false; + } + + return client_->connect(); +} + +bool ThriftControllerAgent::request_uuid(std::string& uuid) +{ + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + client_->request_uuid(uuid); + return true; +} + +bool ThriftControllerAgent::request_resolution(int& width, int& height) +{ + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + ThriftController::Size resolution; + client_->request_resolution(resolution); + width = resolution.width; + height = resolution.height; + return true; +} + +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::screencap(cv::Mat& image) +{ + LogFunc; + + if (!client_ || !transport_ || !transport_->isOpen()) { + LogError << "client_ is nullptr or transport_ is not open"; + return false; + } + + ThriftController::CustomImage custom_image; + client_->screencap(custom_image); + if (custom_image.png_data.empty()) { + LogError << "custom_image.png_data is empty"; + return false; + } + + image = cv::imdecode({ custom_image.png_data.data(), static_cast(custom_image.png_data.size()) }, + cv::IMREAD_COLOR); + return true; +} + +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); +} + +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerAgent.h b/source/MaaThriftControlUnit/impl/ThriftControllerAgent.h new file mode 100644 index 000000000..4530d7d45 --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerAgent.h @@ -0,0 +1,40 @@ +#pragma once + +#include "ControlUnit/ControlUnitAPI.h" + +#include "generated/ThriftController.h" + +MAA_CTRL_UNIT_NS_BEGIN + +class ThriftControllerAgent : public ControlUnitAPI +{ +public: + virtual ~ThriftControllerAgent(); + +public: // from ControllerAPI + virtual bool find_device(/*out*/ std::vector& devices) override; + + virtual bool connect() override; + + virtual bool request_uuid(/*out*/ std::string& uuid) override; + virtual bool request_resolution(/*out*/ int& width, /*out*/ int& height) override; + + virtual bool start_app(const std::string& intent) override; + virtual bool stop_app(const std::string& intent) override; + + virtual bool screencap(/*out*/ cv::Mat& image) 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; + +protected: + std::shared_ptr client_ = nullptr; + std::shared_ptr transport_ = nullptr; +}; +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.cpp b/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.cpp new file mode 100644 index 000000000..a2b057b33 --- /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_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_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.h b/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.h new file mode 100644 index 000000000..bbdb481a7 --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerSocketAgent.h @@ -0,0 +1,13 @@ +#pragma once + +#include "ThriftControllerAgent.h" + +MAA_CTRL_UNIT_NS_BEGIN + +class ThriftControllerSocketAgent : public ThriftControllerAgent +{ +public: + ThriftControllerSocketAgent(const std::string& host, int port); + virtual ~ThriftControllerSocketAgent() = default; +}; +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.cpp b/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.cpp new file mode 100644 index 000000000..fb79dfdc3 --- /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_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_CTRL_UNIT_NS_END diff --git a/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.h b/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.h new file mode 100644 index 000000000..0a3547153 --- /dev/null +++ b/source/MaaThriftControlUnit/impl/ThriftControllerUnixDomainSocketAgent.h @@ -0,0 +1,13 @@ +#pragma once + +#include "ThriftControllerAgent.h" + +MAA_CTRL_UNIT_NS_BEGIN + +class ThriftControllerUnixDomainSocketAgent : public ThriftControllerAgent +{ +public: + ThriftControllerUnixDomainSocketAgent(const std::string& path); + virtual ~ThriftControllerUnixDomainSocketAgent() = default; +}; +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaThriftController/CMakeLists.txt b/source/MaaThriftController/CMakeLists.txt deleted file mode 100644 index 13483e1f8..000000000 --- a/source/MaaThriftController/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -generate_thrift_lib(MaaThriftController ${PROJECT_SOURCE_DIR}/include/Interface/ThriftController.thrift - ${CMAKE_CURRENT_BINARY_DIR}/MaaThriftController maa_thrift_controller_src) diff --git a/source/MaaToolKit/CMakeLists.txt b/source/MaaToolKit/CMakeLists.txt index 0bc2ed0cb..0050d7dfe 100644 --- a/source/MaaToolKit/CMakeLists.txt +++ b/source/MaaToolKit/CMakeLists.txt @@ -14,14 +14,8 @@ target_include_directories( target_compile_definitions(MaaToolKit PRIVATE MAA_TOOLKIT_EXPORTS) target_link_libraries(MaaToolKit MaaFramework MaaUtils HeaderOnlyLibraries Boost::system) -if (WITH_ADB_CONTROLLER) - target_link_libraries(MaaFramework MaaAdbControlUnit) -endif(WITH_ADB_CONTROLLER) add_dependencies(MaaToolKit MaaFramework MaaUtils) -if (WITH_ADB_CONTROLLER) - add_dependencies(MaaFramework MaaAdbControlUnit) -endif(WITH_ADB_CONTROLLER) install( TARGETS MaaToolKit diff --git a/source/MaaToolKit/Config/ConfigMgr.h b/source/MaaToolKit/Config/ConfigMgr.h index 1ac4e4c85..13283f618 100644 --- a/source/MaaToolKit/Config/ConfigMgr.h +++ b/source/MaaToolKit/Config/ConfigMgr.h @@ -52,7 +52,12 @@ class ConfigMgr : public SingletonHolder, public MaaToolKitConfigMgrA bool policy_logging_ = true; bool policy_save_draw_ = false; bool policy_recording_ = false; + +#ifdef MAA_DEBUG + MaaLoggingLevel policy_stdout_level_ = MaaLoggingLevel_All; +#else MaaLoggingLevel policy_stdout_level_ = MaaLoggingLevel_Error; +#endif }; MAA_TOOLKIT_CONFIG_NS_END diff --git a/source/MaaToolKit/Device/DeviceMgr.cpp b/source/MaaToolKit/Device/DeviceMgr.cpp index cce032fef..cde577077 100644 --- a/source/MaaToolKit/Device/DeviceMgr.cpp +++ b/source/MaaToolKit/Device/DeviceMgr.cpp @@ -1,6 +1,6 @@ #include "DeviceMgr.h" -#include "ControlUnit/AdbControlUnitAPI.h" +#include "Utils/ControlUnitLibraryHolder.h" #include "Utils/Logger.h" MAA_TOOLKIT_DEVICE_NS_BEGIN @@ -36,29 +36,26 @@ std::vector DeviceMgr::request_adb_serials(const std::filesystem::p { LogFunc << VAR(adb_path); -#ifdef WITH_ADB_CONTROLLER - std::string str_adb = path_to_utf8_string(adb_path); std::string str_config = adb_config.to_string(); - auto mgr = MAA_ADB_CTRL_UNIT_NS::create_device_list_obj(str_adb.c_str(), str_config.c_str()); - if (!mgr) { - LogError << "Failed to create_device_list_obj" << VAR(str_adb) << VAR(str_config); - return {}; - } - auto devices_opt = mgr->request_devices(); - if (!devices_opt) { + auto control_unit = MAA_CTRL_NS::AdbControlUnitLibraryHolder::create_control_unit( + str_adb.c_str(), "", 0, str_config.c_str(), "", nullptr, nullptr); + + if (!control_unit) { + LogError << "Failed to create control unit"; return {}; } - return *devices_opt; - -#else + std::vector devices; + auto found = control_unit->find_device(devices); - std::ignore = adb_config; - return {}; + if (!found) { + LogError << "Failed to find_device"; + return {}; + } -#endif + return devices; } bool DeviceMgr::request_adb_connect(const std::filesystem::path& adb_path, const std::string& serial, @@ -66,24 +63,26 @@ bool DeviceMgr::request_adb_connect(const std::filesystem::path& adb_path, const { LogFunc << VAR(adb_path) << VAR(serial); -#ifdef WITH_ADB_CONTROLLER - std::string str_adb = path_to_utf8_string(adb_path); + std::string str_serial = serial; std::string str_config = adb_config.to_string(); - auto mgr = MAA_ADB_CTRL_UNIT_NS::create_connection(str_adb.c_str(), serial.c_str(), 0, str_config.c_str()); - if (!mgr) { - LogError << "Failed to create_connection" << VAR(str_adb) << VAR(serial) << VAR(str_config); + + auto control_unit = MAA_CTRL_NS::AdbControlUnitLibraryHolder::create_control_unit( + str_adb.c_str(), str_serial.c_str(), 0, str_config.c_str(), "", nullptr, nullptr); + + if (!control_unit) { + LogError << "Failed to create control unit"; return false; } - return mgr->connect(); - -#else + auto connected = control_unit->connect(); - std::ignore = adb_config; - return false; + if (!connected) { + LogError << "Failed to connect"; + return false; + } -#endif + return true; } std::vector DeviceMgr::check_available_adb_serials(const std::filesystem::path& adb_path, diff --git a/source/MaaUtils/CMakeLists.txt b/source/MaaUtils/CMakeLists.txt index 454e53cff..a89ad19cb 100644 --- a/source/MaaUtils/CMakeLists.txt +++ b/source/MaaUtils/CMakeLists.txt @@ -4,7 +4,7 @@ file(GLOB_RECURSE maa_utils_header ../include/Utils/*) add_library(MaaUtils SHARED ${maa_utils_src} ${maa_utils_header}) target_include_directories(MaaUtils PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../../include) -target_link_libraries(MaaUtils HeaderOnlyLibraries) +target_link_libraries(MaaUtils HeaderOnlyLibraries Boost::system) target_compile_definitions(MaaUtils PRIVATE MAA_UTILS_EXPORTS) install( diff --git a/source/MaaUtils/LibraryHolder/ControlUnitLibraryHolder.cpp b/source/MaaUtils/LibraryHolder/ControlUnitLibraryHolder.cpp new file mode 100644 index 000000000..3199b853c --- /dev/null +++ b/source/MaaUtils/LibraryHolder/ControlUnitLibraryHolder.cpp @@ -0,0 +1,136 @@ +#include "Utils/ControlUnitLibraryHolder.h" + +MAA_CTRL_NS_BEGIN + +template +bool check_version(const std::string& func_name) +{ + using get_version_t = MaaStringView(void); + auto version_func = ControlUnitT::template get_function(func_name); + if (!version_func) { + LogError << "Failed to get function get_version"; + return false; + } + auto version = version_func(); + LogInfo << "Version:" << version; + + if (std::strcmp(version, MAA_VERSION) != 0) { + LogWarn << "ControlUnit and MaaFramework are not same version," + << "ControlUnit:" << version << "MaaFramework:" << MAA_VERSION; + } + return true; +} + +std::shared_ptr AdbControlUnitLibraryHolder::create_control_unit( + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) +{ + if (!load_library(libname_)) { + LogError << "Failed to load library" << VAR(libname_); + return nullptr; + } + + check_version(version_func_name_); + + using create_control_unit_t = MaaControlUnitHandle( // + MaaStringView, MaaStringView, MaaAdbControllerType, MaaStringView, MaaStringView, MaaControllerCallback, + MaaCallbackTransparentArg); + + using destroy_control_unit_t = void(MaaControlUnitHandle); + + auto create_control_unit_func = get_function(create_func_name_); + if (!create_control_unit_func) { + LogError << "Failed to get function create_control_unit"; + return nullptr; + } + + auto destroy_control_unit_func = get_function(destroy_func_name_); + if (!destroy_control_unit_func) { + LogError << "Failed to get function destroy_control_unit"; + return nullptr; + } + + auto control_unit_handle = create_control_unit_func( // + adb_path, adb_serial, type, config, agent_path, callback, callback_arg); + + if (!control_unit_handle) { + LogError << "Failed to create control unit"; + return nullptr; + } + + return std::shared_ptr(control_unit_handle, destroy_control_unit_func); +} + +std::shared_ptr DbgControlUnitLibraryHolder::create_control_unit( + MaaDbgControllerType type, MaaStringView read_path) +{ + if (!load_library(libname_)) { + LogError << "Failed to load library" << VAR(libname_); + return nullptr; + } + + check_version(version_func_name_); + + using create_control_unit_t = MaaControlUnitHandle(MaaDbgControllerType, MaaStringView); + + using destroy_control_unit_t = void(MaaControlUnitHandle); + + auto create_control_unit_func = get_function(create_func_name_); + if (!create_control_unit_func) { + LogError << "Failed to get function create_control_unit"; + return nullptr; + } + + auto destroy_control_unit_func = get_function(destroy_func_name_); + if (!destroy_control_unit_func) { + LogError << "Failed to get function destroy_control_unit"; + return nullptr; + } + + auto control_unit_handle = create_control_unit_func(type, read_path); + + if (!control_unit_handle) { + LogError << "Failed to create control unit"; + return nullptr; + } + + return std::shared_ptr(control_unit_handle, destroy_control_unit_func); +} + +std::shared_ptr ThriftControlUnitLibraryHolder::create_control_unit( + MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config) +{ + if (!load_library(libname_)) { + LogError << "Failed to load library" << VAR(libname_); + return nullptr; + } + + check_version(version_func_name_); + + using create_control_unit_t = MaaControlUnitHandle(MaaThriftControllerType, MaaStringView, int32_t, MaaStringView); + + using destroy_control_unit_t = void(MaaControlUnitHandle); + + auto create_control_unit_func = get_function(create_func_name_); + if (!create_control_unit_func) { + LogError << "Failed to get function create_control_unit"; + return nullptr; + } + + auto destroy_control_unit_func = get_function(destroy_func_name_); + if (!destroy_control_unit_func) { + LogError << "Failed to get function destroy_control_unit"; + return nullptr; + } + + auto control_unit_handle = create_control_unit_func(type, host, port, config); + + if (!control_unit_handle) { + LogError << "Failed to create control unit"; + return nullptr; + } + + return std::shared_ptr(control_unit_handle, destroy_control_unit_func); +} + +MAA_CTRL_NS_END diff --git a/source/include/Conf/Conf.h b/source/include/Conf/Conf.h index 3cd88b146..688214398 100644 --- a/source/include/Conf/Conf.h +++ b/source/include/Conf/Conf.h @@ -105,6 +105,12 @@ { #define MAA_CTRL_NS_END } +#define MAA_CTRL_UNIT_NS MAA_NS::CtrlUnitNs +#define MAA_CTRL_UNIT_NS_BEGIN \ + namespace MAA_CTRL_UNIT_NS \ + { +#define MAA_CTRL_UNIT_NS_END } + #define MAA_VISION_NS MAA_NS::VisionNS #define MAA_VISION_NS_BEGIN \ namespace MAA_VISION_NS \ @@ -117,20 +123,6 @@ { #define MAA_TASK_NS_END } -/* MaaControlUnit */ - -#define MAA_ADB_CTRL_UNIT_NS MAA_CTRL_NS::AdbUnitNs -#define MAA_ADB_CTRL_UNIT_NS_BEGIN \ - namespace MAA_ADB_CTRL_UNIT_NS \ - { -#define MAA_ADB_CTRL_UNIT_NS_END } - -#define MAA_DBG_CTRL_UNIT_NS MAA_CTRL_NS::DbgUnitNs -#define MAA_DBG_CTRL_UNIT_NS_BEGIN \ - namespace MAA_DBG_CTRL_UNIT_NS \ - { -#define MAA_DBG_CTRL_UNIT_NS_END } - /* MaaToolKit */ #define MAA_TOOLKIT_NS MAA_NS::ToolKitNS @@ -151,12 +143,6 @@ { #define MAA_TOOLKIT_CONFIG_NS_END } -#define MAA_TOOLKIT_SERVER_NS MAA_TOOLKIT_NS::ServerNS -#define MAA_TOOLKIT_SERVER_NS_BEGIN \ - namespace MAA_TOOLKIT_SERVER_NS \ - { -#define MAA_TOOLKIT_SERVER_NS_END } - #define MAA_RESTFUL_NS MAA_NS::RestfulNS #define MAA_RESTFUL_NS_BEGIN \ namespace MAA_RESTFUL_NS \ diff --git a/source/include/ControlUnit/AdbControlUnitAPI.h b/source/include/ControlUnit/AdbControlUnitAPI.h index d13402cd6..3d61a1812 100644 --- a/source/include/ControlUnit/AdbControlUnitAPI.h +++ b/source/include/ControlUnit/AdbControlUnitAPI.h @@ -3,148 +3,22 @@ #include #include -#include "Conf/Conf.h" +#include "ControlUnit/ControlUnitAPI.h" #include "MaaFramework/MaaDef.h" -#include "Utils/NoWarningCVMat.hpp" -MAA_ADB_CTRL_UNIT_NS_BEGIN - -/* General */ - -class ConnectionAPI -{ -public: - virtual ~ConnectionAPI() = default; - - virtual bool connect() = 0; - virtual bool kill_server() = 0; -}; - -struct DeviceResolution +#ifdef __cplusplus +extern "C" { - int width = 0; - int height = 0; -}; - -class DeviceInfoAPI -{ -public: - virtual ~DeviceInfoAPI() = default; - - virtual std::optional request_uuid() = 0; - virtual std::optional request_resolution() = 0; - virtual std::optional request_orientation() = 0; - - virtual std::string get_uuid() const = 0; - virtual DeviceResolution get_resolution() const = 0; - virtual int get_orientation() const = 0; -}; - -class ActivityAPI -{ -public: - virtual ~ActivityAPI() = default; - - virtual bool start(const std::string& intent) = 0; - virtual bool stop(const std::string& intent) = 0; -}; - -/* Input */ - -class TouchInputAPI -{ -public: - virtual ~TouchInputAPI() = default; - - virtual bool init(int swidth, int sheight, int orientation) = 0; - virtual void deinit() = 0; - virtual bool set_wh(int swidth, int sheight, int orientation) = 0; - - virtual bool click(int x, int y) = 0; - virtual bool swipe(int x1, int y1, int x2, int y2, int duration) = 0; +#endif - 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; -}; - -class KeyInputAPI -{ -public: - virtual ~KeyInputAPI() = default; - - virtual bool press_key(int key) = 0; -}; - -/* Screencap */ - -class ScreencapAPI -{ -public: - virtual ~ScreencapAPI() = default; - - virtual bool init(int swidth, int sheight) = 0; - virtual void deinit() = 0; - virtual bool set_wh(int swidth, int sheight) = 0; - - virtual std::optional screencap() = 0; -}; - -/* Main */ - -class DeviceListAPI -{ -public: - using Devices = std::vector; - - virtual ~DeviceListAPI() = default; - - virtual std::optional request_devices() = 0; - - virtual Devices get_devices() const = 0; -}; - -class ControlUnitAPI -{ -public: - virtual ~ControlUnitAPI() = default; + MaaStringView MAA_CONTROL_UNIT_API get_version(); - virtual std::shared_ptr connection_obj() = 0; - virtual std::shared_ptr device_info_obj() = 0; - virtual std::shared_ptr activity_obj() = 0; - virtual std::shared_ptr touch_input_obj() = 0; - virtual std::shared_ptr key_input_obj() = 0; - virtual std::shared_ptr screencap_obj() = 0; -}; + MaaControlUnitHandle MAA_CONTROL_UNIT_API create_control_unit( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); -std::shared_ptr MAA_CONTROL_UNIT_API create_device_list_obj(MaaStringView adb_path, - MaaStringView config); -std::shared_ptr MAA_CONTROL_UNIT_API create_controller_unit(MaaStringView adb_path, - MaaStringView adb_serial, - MaaAdbControllerType type, - MaaStringView config, - MaaStringView agent_path); -std::shared_ptr MAA_CONTROL_UNIT_API create_connection(MaaStringView adb_path, - MaaStringView adb_serial, - MaaAdbControllerType type, - MaaStringView config); -std::shared_ptr MAA_CONTROL_UNIT_API create_device_info(MaaStringView adb_path, - MaaStringView adb_serial, - MaaAdbControllerType type, - MaaStringView config); -std::shared_ptr MAA_CONTROL_UNIT_API create_activity(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config); -std::shared_ptr MAA_CONTROL_UNIT_API create_touch_input(MaaStringView adb_path, - MaaStringView adb_serial, - MaaAdbControllerType type, - MaaStringView config, - MaaStringView agent_path); -std::shared_ptr MAA_CONTROL_UNIT_API create_key_input(MaaStringView adb_path, MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, - MaaStringView agent_path); -std::shared_ptr MAA_CONTROL_UNIT_API create_screencap(MaaStringView adb_path, - MaaStringView adb_serial, - MaaAdbControllerType type, MaaStringView config, - MaaStringView agent_path); + void MAA_CONTROL_UNIT_API destroy_control_unit(MaaControlUnitHandle handle); -MAA_ADB_CTRL_UNIT_NS_END +#ifdef __cplusplus +} +#endif diff --git a/source/include/ControlUnit/ControlUnitAPI.h b/source/include/ControlUnit/ControlUnitAPI.h new file mode 100644 index 000000000..a6e5ae4c5 --- /dev/null +++ b/source/include/ControlUnit/ControlUnitAPI.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +#include "Conf/Conf.h" +#include "Utils/NoWarningCVMat.hpp" + +MAA_CTRL_UNIT_NS_BEGIN + +class ControlUnitAPI +{ +public: + virtual ~ControlUnitAPI() = default; + + virtual bool find_device(/*out*/ std::vector& devices) = 0; + + virtual bool connect() = 0; + + virtual bool request_uuid(/*out*/ std::string& uuid) = 0; + virtual bool request_resolution(/*out*/ int& width, /*out*/ int& height) = 0; + + virtual bool start_app(const std::string& intent) = 0; + virtual bool stop_app(const std::string& intent) = 0; + + virtual bool screencap(/*out*/ cv::Mat& image) = 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; +}; + +MAA_CTRL_UNIT_NS_END + +using MaaControlUnitHandle = MAA_CTRL_UNIT_NS::ControlUnitAPI*; diff --git a/source/include/ControlUnit/DbgControlUnitAPI.h b/source/include/ControlUnit/DbgControlUnitAPI.h index ccf8961e7..ae373e7fc 100644 --- a/source/include/ControlUnit/DbgControlUnitAPI.h +++ b/source/include/ControlUnit/DbgControlUnitAPI.h @@ -3,39 +3,21 @@ #include #include -#include "Conf/Conf.h" +#include "ControlUnitAPI.h" #include "MaaFramework/MaaDef.h" -#include "Utils/NoWarningCVMat.hpp" -MAA_DBG_CTRL_UNIT_NS_BEGIN - -class ControllerAPI +#ifdef __cplusplus +extern "C" { -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; +#endif - virtual bool press_key(int key) = 0; + MaaStringView MAA_CONTROL_UNIT_API get_version(); - virtual std::optional screencap() = 0; -}; + MaaControlUnitHandle MAA_CONTROL_UNIT_API create_control_unit( // + MaaDbgControllerType type, MaaStringView read_path); -std::shared_ptr MAA_CONTROL_UNIT_API create_controller(MaaDbgControllerType type, - MaaStringView read_path, MaaStringView write_path, - MaaStringView config); + void MAA_CONTROL_UNIT_API destroy_control_unit(MaaControlUnitHandle handle); -MAA_DBG_CTRL_UNIT_NS_END +#ifdef __cplusplus +} +#endif diff --git a/source/include/ControlUnit/ThriftControlUnitAPI.h b/source/include/ControlUnit/ThriftControlUnitAPI.h new file mode 100644 index 000000000..3bf628bb6 --- /dev/null +++ b/source/include/ControlUnit/ThriftControlUnitAPI.h @@ -0,0 +1,20 @@ +#pragma once + +#include "ControlUnitAPI.h" +#include "MaaFramework/MaaDef.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + MaaStringView MAA_CONTROL_UNIT_API get_version(); + + MaaControlUnitHandle MAA_CONTROL_UNIT_API create_control_unit( // + MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config); + + void MAA_CONTROL_UNIT_API destroy_control_unit(MaaControlUnitHandle handle); + +#ifdef __cplusplus +} +#endif diff --git a/source/include/Utils/Boost.hpp b/source/include/Utils/Boost.hpp index 2fcd2e295..87a350f57 100644 --- a/source/include/Utils/Boost.hpp +++ b/source/include/Utils/Boost.hpp @@ -9,10 +9,12 @@ #endif #define BOOST_PROCESS_USE_STD_FS 1 +#define BOOST_DLL_USE_STD_FS 1 MAA_SUPPRESS_BOOST_WARNINGS_BEGIN #include #include +#include #include #include #include diff --git a/source/include/Utils/ControlUnitLibraryHolder.h b/source/include/Utils/ControlUnitLibraryHolder.h new file mode 100644 index 000000000..2af3faf42 --- /dev/null +++ b/source/include/Utils/ControlUnitLibraryHolder.h @@ -0,0 +1,52 @@ +#pragma once + +#include "Utils/LibraryHolder.h" + +#include + +#include "ControlUnit/ControlUnitAPI.h" +#include "Utils/Platform.h" + +MAA_CTRL_NS_BEGIN + +class MAA_UTILS_API AdbControlUnitLibraryHolder : public LibraryHolder +{ +public: + static std::shared_ptr create_control_unit( // + MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, + MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); + +private: + inline static const std::filesystem::path libname_ = MAA_NS::path("MaaAdbControlUnit"); + inline static const std::string version_func_name_ = "get_version"; + inline static const std::string create_func_name_ = "create_control_unit"; + inline static const std::string destroy_func_name_ = "destroy_control_unit"; +}; + +class MAA_UTILS_API DbgControlUnitLibraryHolder : public LibraryHolder +{ +public: + static std::shared_ptr create_control_unit( // + MaaDbgControllerType type, MaaStringView read_path); + +private: + inline static const std::filesystem::path libname_ = MAA_NS::path("MaaDbgControlUnit"); + inline static const std::string version_func_name_ = "get_version"; + inline static const std::string create_func_name_ = "create_control_unit"; + inline static const std::string destroy_func_name_ = "destroy_control_unit"; +}; + +class MAA_UTILS_API ThriftControlUnitLibraryHolder : public LibraryHolder +{ +public: + static std::shared_ptr create_control_unit( // + MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config); + +private: + inline static const std::filesystem::path libname_ = MAA_NS::path("MaaThriftControlUnit"); + inline static const std::string version_func_name_ = "get_version"; + inline static const std::string create_func_name_ = "create_control_unit"; + inline static const std::string destroy_func_name_ = "destroy_control_unit"; +}; + +MAA_CTRL_NS_END diff --git a/source/include/Utils/LibraryHolder.h b/source/include/Utils/LibraryHolder.h new file mode 100644 index 000000000..7ad04e2e3 --- /dev/null +++ b/source/include/Utils/LibraryHolder.h @@ -0,0 +1,134 @@ +#pragma once + +#include +#include +#include + +#include "Conf/Conf.h" +#include "NonCopyable.hpp" +#include "Utils/Boost.hpp" +#include "Utils/Logger.h" + +MAA_NS_BEGIN + +// template for ref_count +template +class LibraryHolder +{ +public: + virtual ~LibraryHolder(); + + static bool load_library(const std::filesystem::path& libname); + + template + static boost::function get_function(const std::string& func_name); + +protected: + LibraryHolder() = default; + +private: + static void unload_library(); + +private: + inline static std::filesystem::path libname_; + inline static int ref_count_ = 0; + inline static std::mutex mutex_; + + inline static boost::dll::shared_library module_; +}; + +template +inline LibraryHolder::~LibraryHolder() +{ + LogFunc; + + unload_library(); +} + +template +inline bool LibraryHolder::load_library(const std::filesystem::path& libname) +{ + LogFunc << VAR(libname); + + std::unique_lock lock(mutex_); + + if (module_.is_loaded()) { + if (libname_ != libname) { + LogError << "already loaded with different library" << VAR(libname_) << VAR(libname); + return false; + } + + ++ref_count_; + LogDebug << "already loaded" << VAR(ref_count_); + return true; + } + + LogInfo << "Loading library" << VAR(libname); + + boost::dll::fs::error_code ec; + module_.load(libname, ec, boost::dll::load_mode::append_decorations | boost::dll::load_mode::search_system_folders); + + if (ec.value() != boost::system::errc::success) { + auto message = ec.message(); + LogError << "Failed to load library" << VAR(libname) << VAR(message); + return false; + } + + if (!module_.is_loaded()) { + LogError << "Failed to load library" << VAR(libname); + return false; + } + + libname_ = libname; + ++ref_count_; + return true; +} + +template +inline void LibraryHolder::unload_library() +{ + LogFunc << VAR(libname_); + + std::unique_lock lock(mutex_); + + if (!module_.is_loaded()) { + LogDebug << "LibraryHolder already unloaded"; + return; + } + + --ref_count_; + if (ref_count_ > 0) { + LogDebug << "LibraryHolder ref count" << VAR(ref_count_); + return; + } + + LogInfo << "Unloading library" << VAR(libname_); + + module_.unload(); + + libname_.clear(); + ref_count_ = 0; +} + +template +template +inline boost::function LibraryHolder::get_function(const std::string& func_name) +{ + LogFunc << VAR(func_name); + + std::unique_lock lock(mutex_); + + if (!module_.is_loaded()) { + LogError << "LibraryHolder not loaded"; + return {}; + } + + if (!module_.has(func_name)) { + LogError << "Failed to find exported function" << VAR(func_name); + return {}; + } + + return module_.get(func_name); +} + +MAA_NS_END diff --git a/source/include/Utils/MessageNotifier.hpp b/source/include/Utils/MessageNotifier.hpp new file mode 100644 index 000000000..424ffc9a6 --- /dev/null +++ b/source/include/Utils/MessageNotifier.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include "Logger.h" +#include "NonCopyable.hpp" + +#include + +MAA_NS_BEGIN + +template + requires std::is_constructible_v +class MessageNotifier : public NonCopyable +{ +public: + MessageNotifier(Callback callback, CallbackArg callback_arg) : callback_(callback), callback_arg_(callback_arg) {} + void notify(std::string_view msg, const json::value& details = json::value()) + { + LogFunc << VAR_VOIDP(callback_) << VAR_VOIDP(callback_arg_) << VAR(msg) << VAR(details); + + if (!callback_) { + return; + } + callback_(msg.data(), details.to_string().c_str(), callback_arg_); + } + +private: + Callback callback_ = nullptr; + CallbackArg callback_arg_ = nullptr; +}; + +MAA_NS_END 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)