Skip to content

Commit

Permalink
feat: Win32 Controller 框架
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 12, 2023
1 parent 597804b commit 6960eae
Show file tree
Hide file tree
Showing 18 changed files with 543 additions and 11 deletions.
11 changes: 2 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ 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_WIN32_CONTROLLER "build with win32 controller" ON)

option(WITH_GRPC "build with protobuf and grpc" ON)

option(BUILD_GRPC_CLI "build grpc CLI exec" ON)
Expand Down Expand Up @@ -43,20 +45,11 @@ include(${PROJECT_SOURCE_DIR}/cmake/utils.cmake)
include(${PROJECT_SOURCE_DIR}/cmake/version.cmake)
# include(${PROJECT_SOURCE_DIR}/cmake/nuget.cmake)

if (WITH_ADB_CONTROLLER)
add_compile_definitions(WITH_ADB_CONTROLLER)
endif()
if(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE)
include(${PROJECT_SOURCE_DIR}/cmake/thrift-gen.cmake)
add_compile_definitions(WITH_THRIFT_CONTROLLER)
endif()
if (WITH_DBG_CONTROLLER)
add_compile_definitions(WITH_DBG_CONTROLLER)
endif()

if(WITH_GRPC)
include(${PROJECT_SOURCE_DIR}/cmake/grpc-gen.cmake)
add_compile_definitions(WITH_GRPC)
endif()

find_package(OpenCV REQUIRED COMPONENTS core imgproc imgcodecs)
Expand Down
4 changes: 4 additions & 0 deletions include/MaaFramework/Instance/MaaController.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ extern "C"
MaaStringView adb_path, MaaStringView address, MaaAdbControllerType type, MaaStringView config,
MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg);

MaaControllerHandle MAA_FRAMEWORK_API MaaWin32ControllerCreate( //
void* hWnd, MaaWin32ControllerType type, 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);
Expand Down
15 changes: 15 additions & 0 deletions include/MaaFramework/MaaDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ enum MaaThriftControllerTypeEnum
MaaThriftControllerType_UnixDomainSocket = 2,
};

#ifdef _WIN32
typedef int32_t MaaWin32ControllerType;
enum MaaWin32ControllerTypeEnum
{
MaaWin32Controller_Invalid = 0,

MaaWin32ControllerType_Touch_SendMessage = 1,
MaaWin32ControllerType_Touch_Mask = 0xFF,

MaaWin32ControllerType_Key_Mask = 0xFF00,

MaaWin32ControllerType_Screencap_Mask = 0xFF0000,
};
#endif

typedef void* MaaTransparentArg;
typedef MaaTransparentArg MaaCallbackTransparentArg;

Expand Down
3 changes: 3 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ add_subdirectory(MaaUtils)
if (WITH_ADB_CONTROLLER)
add_subdirectory(MaaAdbControlUnit)
endif(WITH_ADB_CONTROLLER)
if (WITH_WIN32_CONTROLLER)
add_subdirectory(MaaWin32ControlUnit)
endif(WITH_WIN32_CONTROLLER)
if(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE)
add_subdirectory(MaaThriftControlUnit)
endif(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <map>
#include <meojson/json.hpp>

#include "StringMisc.hpp"
#include "Utils/StringMisc.hpp"

MAA_NS_BEGIN

Expand Down
2 changes: 1 addition & 1 deletion source/MaaAdbControlUnit/Base/UnitBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#include <meojson/json.hpp>

#include "Base/ArgvWrapper.hpp"
#include "ControlUnit/AdbControlUnitAPI.h"
#include "Platform/PlatformIO.h"
#include "Screencap/ScreencapHelper.h"
#include "Utils/ArgvWrapper.hpp"

MAA_CTRL_UNIT_NS_BEGIN

Expand Down
21 changes: 21 additions & 0 deletions source/MaaFramework/API/MaaController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ MaaControllerHandle MaaAdbControllerCreateV2( //
return new MAA_CTRL_NS::GeneralControllerAgent(std::move(control_unit), callback, callback_arg);
}

MaaControllerHandle MaaWin32ControllerCreate( //
void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg)
{
LogFunc << VAR_VOIDP(hWnd) << VAR(type) << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg);

if (!hWnd) {
LogError << "hWnd is nullptr";
return nullptr;
}

auto control_unit =
MAA_CTRL_NS::Win32ControlUnitLibraryHolder::create_control_unit(hWnd, type, callback, callback_arg);

if (!control_unit) {
LogError << "Failed to create control unit";
return nullptr;
}

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)
Expand Down
3 changes: 3 additions & 0 deletions source/MaaFramework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ add_dependencies(MaaFramework MaaUtils)
if (WITH_ADB_CONTROLLER)
add_dependencies(MaaFramework MaaAdbControlUnit)
endif(WITH_ADB_CONTROLLER)
if (WITH_WIN32_CONTROLLER)
add_dependencies(MaaFramework MaaWin32ControlUnit)
endif(WITH_WIN32_CONTROLLER)
if(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE)
add_dependencies(MaaFramework MaaThriftControlUnit)
endif(WITH_THRIFT_CONTROLLER AND NOT MAA_CROSSCOMPILE)
Expand Down
47 changes: 47 additions & 0 deletions source/MaaUtils/LibraryHolder/ControlUnitLibraryHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,53 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> AdbControlUnitLibraryHolder::c
return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit);
}

std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> Win32ControlUnitLibraryHolder::create_control_unit(
void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg)
{
if (!hWnd) {
LogError << "hWnd is nullptr";
return nullptr;
}

if (!load_library(libname_)) {
LogError << "Failed to load library" << VAR(libname_);
return nullptr;
}

check_version<Win32ControlUnitLibraryHolder>(version_func_name_);

using create_control_unit_t =
MaaControlUnitHandle(void*, MaaWin32ControllerType, MaaControllerCallback, MaaCallbackTransparentArg);

using destroy_control_unit_t = void(MaaControlUnitHandle);

auto create_control_unit_func = get_function<create_control_unit_t>(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_control_unit_t>(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(hWnd, type, callback, callback_arg);

if (!control_unit_handle) {
LogError << "Failed to create control unit";
return nullptr;
}

auto destroy_control_unit = [destroy_control_unit_func](MaaControlUnitHandle handle) {
destroy_control_unit_func(handle);
unload_library();
};

return std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI>(control_unit_handle, destroy_control_unit);
}

std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> DbgControlUnitLibraryHolder::create_control_unit(
MaaDbgControllerType type, MaaStringView read_path)
{
Expand Down
63 changes: 63 additions & 0 deletions source/MaaWin32ControlUnit/API/Win32ControlUnitAPI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "ControlUnit/Win32ControlUnitAPI.h"

#include "Base/UnitBase.h"
#include "Manager/ControlUnitMgr.h"
#include "Touch/SendMessageToucher.h"
#include "Utils/Logger.h"
#include "Utils/SafeWindows.hpp"

MaaStringView get_version()
{
#pragma message("MaaWin32ControlUnit MAA_VERSION: " MAA_VERSION)

return MAA_VERSION;
}

MaaControlUnitHandle create_control_unit( //
void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg)
{
using namespace MAA_CTRL_UNIT_NS;

LogFunc << VAR_VOIDP(hWnd) << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg);

if (!hWnd) {
LogError << "hWnd is nullptr";
return nullptr;
}
HWND h_wnd = reinterpret_cast<HWND>(hWnd);

std::shared_ptr<TouchInputBase> touch_unit = nullptr;
std::shared_ptr<KeyInputBase> key_unit = nullptr;
std::shared_ptr<ScreencapBase> screencap_unit = nullptr;

auto touch_type = type & MaaWin32ControllerType_Touch_Mask;
// auto key_type = type & MaaWin32ControllerType_Key_Mask;
// auto screencap_type = type & MaaWin32ControllerType_Screencap_Mask;

switch (touch_type) {
case MaaWin32ControllerType_Touch_SendMessage:
LogInfo << "touch_type: SendMessage";
touch_unit = std::make_shared<SendMessageToucher>();
break;
default:
LogWarn << "Unknown touch input type" << VAR(touch_type);
break;
}

auto unit_mgr = std::make_unique<ControlUnitMgr>(h_wnd, 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);

return unit_mgr.release();
}

void destroy_control_unit(MaaControlUnitHandle handle)
{
LogFunc << VAR_VOIDP(handle);

if (handle) {
delete handle;
}
}
43 changes: 43 additions & 0 deletions source/MaaWin32ControlUnit/Base/UnitBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <meojson/json.hpp>

#include "ControlUnit/Win32ControlUnitAPI.h"

MAA_CTRL_UNIT_NS_BEGIN

class ScreencapBase
{
public:
virtual ~ScreencapBase() = default;

public:
virtual std::optional<cv::Mat> screencap() = 0;

protected:
};

class TouchInputBase
{
public:
virtual ~TouchInputBase() = default;

public:
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;
};

class KeyInputBase
{
public:
virtual ~KeyInputBase() = default;

public:
virtual bool press_key(int key) = 0;
};

MAA_CTRL_UNIT_NS_END
25 changes: 25 additions & 0 deletions source/MaaWin32ControlUnit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
file(GLOB_RECURSE maa_win32_control_unit_src *.h *.hpp *.cpp)
file(GLOB_RECURSE maa_win32_control_unit_header ../include/ControlUnit/Win32ControlUnitAPI.h ../include/ControlUnit/ControlUnitAPI.h)

add_library(MaaWin32ControlUnit SHARED ${maa_win32_control_unit_src} ${maa_win32_control_unit_header})

target_include_directories(MaaWin32ControlUnit PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../../include)

target_link_libraries(MaaWin32ControlUnit MaaUtils HeaderOnlyLibraries ${OpenCV_LIBS} ZLIB::ZLIB Boost::system)
if(WIN32)
target_link_libraries(MaaWin32ControlUnit ws2_32)
endif()

target_compile_definitions(MaaWin32ControlUnit PRIVATE MAA_CONTROL_UNIT_EXPORTS)

add_dependencies(MaaWin32ControlUnit MaaUtils)

install(
TARGETS MaaWin32ControlUnit
RUNTIME DESTINATION bin
LIBRARY DESTINATION bin
#ARCHIVE DESTINATION lib
)

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${maa_win32_control_unit_src})
Loading

0 comments on commit 6960eae

Please sign in to comment.