From 5725d20809fb8a0f1c68315b26e2d407e852438e Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 19 Feb 2022 22:36:45 +0800 Subject: [PATCH] =?UTF-8?q?feat.=E6=96=B0=E5=A2=9E=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=9B=B4=E6=96=B0?= =?UTF-8?q?python=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/AsstCaller.h | 3 ++ src/MeoAssistant/AsstCaller.cpp | 10 ++++++ src/MeoAssistant/Logger.hpp | 5 +++ src/Python/interface.py | 55 ++++++++++++++++----------------- src/Python/sample.py | 2 +- 5 files changed, 46 insertions(+), 29 deletions(-) diff --git a/include/AsstCaller.h b/include/AsstCaller.h index 450e808ddb..cb5b7a22d0 100644 --- a/include/AsstCaller.h +++ b/include/AsstCaller.h @@ -41,6 +41,9 @@ extern "C" { bool ASSTAPI AsstCtrlerClick(asst::Assistant* p_asst, int x, int y, bool block); ASSTAPI_PORT const char* ASST_CALL AsstGetVersion(); + + void ASSTAPI AsstLog(asst::Assistant* p_asst, const char* level, const char* message); + #ifdef __cplusplus } #endif diff --git a/src/MeoAssistant/AsstCaller.cpp b/src/MeoAssistant/AsstCaller.cpp index 8dc7c779cf..fdb49ff277 100644 --- a/src/MeoAssistant/AsstCaller.cpp +++ b/src/MeoAssistant/AsstCaller.cpp @@ -8,6 +8,7 @@ #include "Assistant.h" #include "AsstDef.h" #include "Version.h" +#include "Logger.hpp" #if 0 #if _MSC_VER @@ -262,6 +263,15 @@ const char* AsstGetVersion() return asst::Version; } +void AsstLog(asst::Assistant* p_asst, const char* level, const char* message) +{ + if (p_asst == nullptr) { + return; + } + + asst::Log.log_with_custom_level(level, message); +} + bool AsstAppendDebug(asst::Assistant* p_asst) { if (p_asst == nullptr) { diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 95697ac18a..bd92be8360 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -61,6 +61,11 @@ namespace asst std::string_view level = "ERR"; log(level, std::forward(args)...); } + template + inline void log_with_custom_level(std::string_view level, Args&&... args) + { + log(level, std::forward(args)...); + } void flush() { std::unique_lock trace_lock(m_trace_mutex); diff --git a/src/Python/interface.py b/src/Python/interface.py index a3b614a002..87a6d301b2 100644 --- a/src/Python/interface.py +++ b/src/Python/interface.py @@ -38,29 +38,19 @@ class Asst: def __del__(self): self.__lib.AsstDestroy(self.__ptr) - def catch_default(self) -> bool: + def connect(self, adb_path: str, address: str, config: str = 'General'): """ - 连接配置文件中的默认选项(模拟器or安卓手机) + 连接设备 + + :params: + ``adb_path``: adb 程序的路径 + ``address``: adb 地址+端口 + ``config``: adb 配置,可参考 resource/config.json :return: 是否连接成功 """ - return self.__lib.AsstCatchDefault(self.__ptr) - - def catch_emulator(self) -> bool: - """ - 连接模拟器 - - :return: 是否连接成功 - """ - return self.__lib.AsstCatchEmulator(self.__ptr) - - def catch_custom(self, address: str) -> bool: - """ - 连接自定义设备 - - :return: 是否连接成功 - """ - return self.__lib.AsstCatchCustom(self.__ptr, address.encode('utf-8')) + return self.__lib.AsstConnect(self.__ptr, + adb_path.encode('utf-8'), address.encode('utf-8'), config.encode('utf-8')) def append_fight(self, stage: str, max_medicine: int, max_stone: int, max_times: int) -> bool: """ @@ -194,6 +184,17 @@ class Asst: return self.__lib.AsstSetPenguinId(self.__ptr, id.encode('utf-8')) + def log(self, level: str, message: str): + ''' + 打印日志 + + :params: + ``level``: 日志等级标签 + ``message``: 日志内容 + ''' + + return self.__lib.AsstLog(self.__ptr, level.encode('utf-8'), message.encode('utf-8')) + def get_version(self) -> str: """ 获取DLL版本号 @@ -209,15 +210,9 @@ class Asst: self.__lib.AsstDestroy.argtypes = (ctypes.c_void_p,) - self.__lib.AsstCatchDefault.restype = ctypes.c_bool - self.__lib.AsstCatchDefault.argtypes = (ctypes.c_void_p,) - - self.__lib.AsstCatchEmulator.restype = ctypes.c_bool - self.__lib.AsstCatchEmulator.argtypes = (ctypes.c_void_p,) - - self.__lib.AsstCatchCustom.restype = ctypes.c_bool - self.__lib.AsstCatchCustom.argtypes = ( - ctypes.c_void_p, ctypes.c_char_p,) + self.__lib.AsstConnect.restype = ctypes.c_bool + self.__lib.AsstConnect.argtypes = ( + ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p,) self.__lib.AsstAppendFight.restype = ctypes.c_bool self.__lib.AsstAppendFight.argtypes = ( @@ -261,3 +256,7 @@ class Asst: ctypes.c_void_p, ctypes.c_char_p) self.__lib.AsstGetVersion.restype = ctypes.c_char_p + + self.__lib.AsstLog.restype = ctypes.c_none + self.__lib.AsstLog.argtypes = ( + ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p) diff --git a/src/Python/sample.py b/src/Python/sample.py index 7555af268a..a6ecb0617c 100644 --- a/src/Python/sample.py +++ b/src/Python/sample.py @@ -17,7 +17,7 @@ if __name__ == "__main__": print('version', asst.get_version()) - if asst.catch_custom('127.0.0.1:7555'): + if asst.connect('adb', '127.0.0.1:5555'): print('连接成功') else: print('连接失败')