diff --git a/include/AsstCaller.h b/include/AsstCaller.h index 4223555bd3..aaae22e68d 100644 --- a/include/AsstCaller.h +++ b/include/AsstCaller.h @@ -1,59 +1,49 @@ #pragma once #include "AsstPort.h" +#include -#ifndef __cplusplus -#include -#endif +struct AsstExtAPI; +typedef struct AsstExtAPI* AsstHandle; -#ifdef __cplusplus -namespace asst -{ - class AsstExtAPI; -} -#endif +typedef uint8_t AsstBool; +typedef uint64_t AsstSize; +typedef int32_t AsstMsgId; +typedef int32_t AsstTaskId; +typedef int32_t AsstAsyncCallId; +typedef int32_t AsstStaticOptionKey; +typedef int32_t AsstInstanceOptionKey; + +typedef void(ASST_CALL* AsstApiCallback)(AsstMsgId msg, const char* details_json, void* custom_arg); #ifdef __cplusplus extern "C" { #endif -#ifdef __cplusplus - typedef asst::AsstExtAPI* AsstHandle; -#else -typedef void* AsstHandle; -#endif - typedef int AsstTaskId; - typedef int AsstStaticOptionKey; - typedef int AsstInstanceOptionKey; - typedef unsigned long long AsstSize; - typedef int AsstAsyncCallId; - - typedef void(ASST_CALL* AsstApiCallback)(int msg, const char* detail_json, void* custom_arg); - - bool ASSTAPI AsstSetUserDir(const char* path); - bool ASSTAPI AsstLoadResource(const char* path); - bool ASSTAPI AsstSetStaticOption(AsstStaticOptionKey key, const char* value); + AsstBool ASSTAPI AsstSetUserDir(const char* path); + AsstBool ASSTAPI AsstLoadResource(const char* path); + AsstBool ASSTAPI AsstSetStaticOption(AsstStaticOptionKey key, const char* value); AsstHandle ASSTAPI AsstCreate(); AsstHandle ASSTAPI AsstCreateEx(AsstApiCallback callback, void* custom_arg); void ASSTAPI AsstDestroy(AsstHandle handle); - bool ASSTAPI AsstSetInstanceOption(AsstHandle handle, AsstInstanceOptionKey key, const char* value); - /* deprecated */ bool ASSTAPI AsstConnect(AsstHandle handle, const char* adb_path, const char* address, - const char* config); + AsstBool ASSTAPI AsstSetInstanceOption(AsstHandle handle, AsstInstanceOptionKey key, const char* value); + /* deprecated */ AsstBool ASSTAPI AsstConnect(AsstHandle handle, const char* adb_path, const char* address, + const char* config); AsstTaskId ASSTAPI AsstAppendTask(AsstHandle handle, const char* type, const char* params); - bool ASSTAPI AsstSetTaskParams(AsstHandle handle, AsstTaskId id, const char* params); + AsstBool ASSTAPI AsstSetTaskParams(AsstHandle handle, AsstTaskId id, const char* params); - bool ASSTAPI AsstStart(AsstHandle handle); - bool ASSTAPI AsstStop(AsstHandle handle); - bool ASSTAPI AsstRunning(AsstHandle handle); + AsstBool ASSTAPI AsstStart(AsstHandle handle); + AsstBool ASSTAPI AsstStop(AsstHandle handle); + AsstBool ASSTAPI AsstRunning(AsstHandle handle); /* Aysnc with AsstMsg::AsyncCallInfo Callback*/ AsstAsyncCallId ASSTAPI AsstAsyncConnect(AsstHandle handle, const char* adb_path, const char* address, - const char* config, bool block); - AsstAsyncCallId ASSTAPI AsstAsyncClick(AsstHandle handle, int x, int y, bool block); - AsstAsyncCallId ASSTAPI AsstAsyncScreencap(AsstHandle handle, bool block); + const char* config, AsstBool block); + AsstAsyncCallId ASSTAPI AsstAsyncClick(AsstHandle handle, int32_t x, int32_t y, AsstBool block); + AsstAsyncCallId ASSTAPI AsstAsyncScreencap(AsstHandle handle, AsstBool block); AsstSize ASSTAPI AsstGetImage(AsstHandle handle, void* buff, AsstSize buff_size); AsstSize ASSTAPI AsstGetUUID(AsstHandle handle, char* buff, AsstSize buff_size); diff --git a/src/MaaCore/Assistant.cpp b/src/MaaCore/Assistant.cpp index 0e6d355841..7e0f3755ac 100644 --- a/src/MaaCore/Assistant.cpp +++ b/src/MaaCore/Assistant.cpp @@ -25,7 +25,7 @@ using namespace asst; -bool AsstExtAPI::set_static_option(StaticOptionKey key, const std::string& value) +bool ::AsstExtAPI::set_static_option(StaticOptionKey key, const std::string& value) { Log.info(__FUNCTION__, "| key", static_cast(key), "value", value); switch (key) { @@ -49,7 +49,7 @@ bool AsstExtAPI::set_static_option(StaticOptionKey key, const std::string& value return false; } -Assistant::Assistant(AsstApiCallback callback, void* callback_arg) : m_callback(callback), m_callback_arg(callback_arg) +Assistant::Assistant(ApiCallback callback, void* callback_arg) : m_callback(callback), m_callback_arg(callback_arg) { LogTraceFunction; @@ -396,7 +396,7 @@ void Assistant::msg_proc() lock.unlock(); if (m_callback) { - m_callback(static_cast(msg), detail.to_string().c_str(), m_callback_arg); + m_callback(static_cast(msg), detail.to_string().c_str(), m_callback_arg); } } else { diff --git a/src/MaaCore/Assistant.h b/src/MaaCore/Assistant.h index 65d0d37064..b7b12b4baa 100644 --- a/src/MaaCore/Assistant.h +++ b/src/MaaCore/Assistant.h @@ -11,53 +11,51 @@ #include "Common/AsstMsg.h" #include "Common/AsstTypes.h" +struct AsstExtAPI +{ +public: + using TaskId = int; + using AsyncCallId = int; + + virtual ~AsstExtAPI() = default; + + // 设置进程级参数 + static bool set_static_option(asst::StaticOptionKey key, const std::string& value); + // 设置实例级参数 + virtual bool set_instance_option(asst::InstanceOptionKey key, const std::string& value) = 0; + // 连接adb + virtual bool connect(const std::string& adb_path, const std::string& address, const std::string& config) = 0; + + // 添加任务 + virtual TaskId append_task(const std::string& type, const std::string& params) = 0; + // 动态设置任务参数 + virtual bool set_task_params(TaskId task_id, const std::string& params) = 0; + + // 开始执行任务队列 + virtual bool start(bool block = true) = 0; + // 停止任务队列并清空 + virtual bool stop(bool block = true) = 0; + // 是否正在运行 + virtual bool running() const = 0; + + // 异步连接 + virtual AsyncCallId async_connect(const std::string& adb_path, const std::string& address, + const std::string& config, bool block = false) = 0; + // 异步点击 + virtual AsyncCallId async_click(int x, int y, bool block = false) = 0; + // 异步截图 + virtual AsyncCallId async_screencap(bool block = false) = 0; + + // 获取上次的截图 + virtual std::vector get_image() const = 0; + // 获取 UUID + virtual std::string get_uuid() const = 0; + // 获取任务列表 + virtual std::vector get_tasks_list() const = 0; +}; + namespace asst { - using uchar = unsigned char; - - class AsstExtAPI - { - public: - using TaskId = int; - using AsyncCallId = int; - - virtual ~AsstExtAPI() = default; - - // 设置进程级参数 - static bool set_static_option(StaticOptionKey key, const std::string& value); - // 设置实例级参数 - virtual bool set_instance_option(InstanceOptionKey key, const std::string& value) = 0; - // 连接adb - virtual bool connect(const std::string& adb_path, const std::string& address, const std::string& config) = 0; - - // 添加任务 - virtual TaskId append_task(const std::string& type, const std::string& params) = 0; - // 动态设置任务参数 - virtual bool set_task_params(TaskId task_id, const std::string& params) = 0; - - // 开始执行任务队列 - virtual bool start(bool block = true) = 0; - // 停止任务队列并清空 - virtual bool stop(bool block = true) = 0; - // 是否正在运行 - virtual bool running() const = 0; - - // 异步连接 - virtual AsyncCallId async_connect(const std::string& adb_path, const std::string& address, - const std::string& config, bool block = false) = 0; - // 异步点击 - virtual AsyncCallId async_click(int x, int y, bool block = false) = 0; - // 异步截图 - virtual AsyncCallId async_screencap(bool block = false) = 0; - - // 获取上次的截图 - virtual std::vector get_image() const = 0; - // 获取 UUID - virtual std::string get_uuid() const = 0; - // 获取任务列表 - virtual std::vector get_tasks_list() const = 0; - }; - class Controller; class InterfaceTask; class Status; @@ -65,7 +63,7 @@ namespace asst class Assistant : public AsstExtAPI { public: - Assistant(AsstApiCallback callback = nullptr, void* callback_arg = nullptr); + Assistant(ApiCallback callback = nullptr, void* callback_arg = nullptr); virtual ~Assistant() override; virtual bool set_instance_option(InstanceOptionKey key, const std::string& value) override; @@ -84,7 +82,7 @@ namespace asst virtual AsyncCallId async_click(int x, int y, bool block = false) override; virtual AsyncCallId async_screencap(bool block = false) override; - virtual std::vector get_image() const override; + virtual std::vector get_image() const override; virtual std::string get_uuid() const override; virtual std::vector get_tasks_list() const override; @@ -111,7 +109,7 @@ namespace asst bool m_thread_exit = false; std::list>> m_tasks_list; inline static TaskId m_task_id = 0; // 进程级唯一 - AsstApiCallback m_callback = nullptr; + ApiCallback m_callback = nullptr; void* m_callback_arg = nullptr; bool m_thread_idle = true; diff --git a/src/MaaCore/AsstCaller.cpp b/src/MaaCore/AsstCaller.cpp index 49542212fc..9e8ca51d0a 100644 --- a/src/MaaCore/AsstCaller.cpp +++ b/src/MaaCore/AsstCaller.cpp @@ -39,14 +39,17 @@ BOOL APIENTRY DllMain(HINSTANCE hModule, #endif #endif -static bool inited = false; +bool inited() +{ + return asst::ResourceLoader::get_instance().loaded(); +} -bool AsstSetUserDir(const char* path) +AsstBool AsstSetUserDir(const char* path) { return asst::UserDir.set(path); } -bool AsstLoadResource(const char* path) +AsstBool AsstLoadResource(const char* path) { using namespace asst::utils::path_literals; @@ -58,21 +61,20 @@ bool AsstLoadResource(const char* path) if (asst::UserDir.empty()) { asst::UserDir.set(os_path); } - inited = asst::ResourceLoader::get_instance().load(res_path); - return inited; + return asst::ResourceLoader::get_instance().load(res_path); } -bool AsstSetStaticOption(AsstStaticOptionKey key, const char* value) +AsstBool AsstSetStaticOption(AsstStaticOptionKey key, const char* value) { - if (inited) { + if (!inited()) { return false; } - return asst::AsstExtAPI::set_static_option(static_cast(key), value); + return AsstExtAPI::set_static_option(static_cast(key), value); } AsstHandle AsstCreate() { - if (!inited) { + if (!inited()) { return nullptr; } return new asst::Assistant(); @@ -80,10 +82,10 @@ AsstHandle AsstCreate() AsstHandle AsstCreateEx(AsstApiCallback callback, void* custom_arg) { - if (!inited) { + if (!inited()) { return nullptr; } - return new asst::Assistant(callback, custom_arg); + return new asst::Assistant(static_cast(callback), custom_arg); } void AsstDestroy(AsstHandle handle) @@ -96,7 +98,7 @@ void AsstDestroy(AsstHandle handle) handle = nullptr; } -bool AsstSetInstanceOption(AsstHandle handle, AsstInstanceOptionKey key, const char* value) +AsstBool AsstSetInstanceOption(AsstHandle handle, AsstInstanceOptionKey key, const char* value) { if (handle == nullptr) { return false; @@ -105,36 +107,36 @@ bool AsstSetInstanceOption(AsstHandle handle, AsstInstanceOptionKey key, const c return handle->set_instance_option(static_cast(key), value); } -bool AsstConnect(AsstHandle handle, const char* adb_path, const char* address, const char* config) +AsstBool AsstConnect(AsstHandle handle, const char* adb_path, const char* address, const char* config) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return false; } return handle->connect(adb_path, address, config ? config : std::string()); } -bool AsstStart(AsstHandle handle) +AsstBool AsstStart(AsstHandle handle) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return false; } return handle->start(); } -bool AsstStop(AsstHandle handle) +AsstBool AsstStop(AsstHandle handle) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return false; } return handle->stop(); } -bool AsstRunning(AsstHandle handle) +AsstBool AsstRunning(AsstHandle handle) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return false; } @@ -142,9 +144,9 @@ bool AsstRunning(AsstHandle handle) } AsstAsyncCallId AsstAsyncConnect(AsstHandle handle, const char* adb_path, const char* address, const char* config, - bool block) + AsstBool block) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return false; } return handle->async_connect(adb_path, address, config ? config : std::string(), block); @@ -152,33 +154,33 @@ AsstAsyncCallId AsstAsyncConnect(AsstHandle handle, const char* adb_path, const AsstTaskId AsstAppendTask(AsstHandle handle, const char* type, const char* params) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return 0; } return handle->append_task(type, params ? params : ""); } -bool AsstSetTaskParams(AsstHandle handle, AsstTaskId id, const char* params) +AsstBool AsstSetTaskParams(AsstHandle handle, AsstTaskId id, const char* params) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return false; } return handle->set_task_params(id, params ? params : ""); } -AsstAsyncCallId AsstAsyncClick(AsstHandle handle, int x, int y, bool block) +AsstAsyncCallId AsstAsyncClick(AsstHandle handle, int32_t x, int32_t y, AsstBool block) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return false; } return handle->async_click(x, y, block); } -AsstAsyncCallId AsstAsyncScreencap(AsstHandle handle, bool block) +AsstAsyncCallId AsstAsyncScreencap(AsstHandle handle, AsstBool block) { - if (!inited || handle == nullptr) { + if (!inited() || handle == nullptr) { return false; } return handle->async_screencap(block); @@ -186,7 +188,7 @@ AsstAsyncCallId AsstAsyncScreencap(AsstHandle handle, bool block) AsstSize AsstGetImage(AsstHandle handle, void* buff, AsstSize buff_size) { - if (!inited || handle == nullptr || buff == nullptr) { + if (!inited() || handle == nullptr || buff == nullptr) { return NullSize; } auto img_data = handle->get_image(); @@ -200,7 +202,7 @@ AsstSize AsstGetImage(AsstHandle handle, void* buff, AsstSize buff_size) AsstSize AsstGetUUID(AsstHandle handle, char* buff, AsstSize buff_size) { - if (!inited || handle == nullptr || buff == nullptr) { + if (!inited() || handle == nullptr || buff == nullptr) { return NullSize; } auto uuid = handle->get_uuid(); @@ -214,7 +216,7 @@ AsstSize AsstGetUUID(AsstHandle handle, char* buff, AsstSize buff_size) AsstSize AsstGetTasksList(AsstHandle handle, AsstTaskId* buff, AsstSize buff_size) { - if (!inited || handle == nullptr || buff == nullptr) { + if (!inited() || handle == nullptr || buff == nullptr) { return NullSize; } auto tasks = handle->get_tasks_list(); @@ -238,8 +240,8 @@ const char* AsstGetVersion() void AsstLog(const char* level, const char* message) { - if (!inited) { - std::cerr << "Not inited" << std::endl; + if (asst::UserDir.empty()) { + std::cerr << __FUNCTION__ << " | User Dir not set" << std::endl; return; } asst::Log.log(asst::Logger::level(level), message); diff --git a/src/MaaCore/Common/AsstMsg.h b/src/MaaCore/Common/AsstMsg.h index 31a328d855..e4357e68f3 100644 --- a/src/MaaCore/Common/AsstMsg.h +++ b/src/MaaCore/Common/AsstMsg.h @@ -54,14 +54,11 @@ namespace asst return os << _type_name.at(type); } - // AsstCallback 消息回调函数 - // 参数: - // AsstMsg 消息类型 - // const json::value& 消息详情json,每种消息不同,Todo,需要补充个协议文档啥的 - // void* 外部调用者自定义参数,每次回调会带出去,建议传个(void*)this指针进来 - using AsstApiCallback = void (*)(int msg, const char* detail_json, void* custom_arg); + // 对外的回调接口 + using AsstMsgId = int32_t; + using ApiCallback = void (*)(AsstMsgId msg, const char* details_json, void* custom_arg); // 内部使用的回调 class Assistant; - using AsstCallback = std::function; + using AsstCallback = std::function; } diff --git a/src/MaaCore/Config/ResourceLoader.cpp b/src/MaaCore/Config/ResourceLoader.cpp index 7af9e5e6a0..f1e1e4c7ea 100644 --- a/src/MaaCore/Config/ResourceLoader.cpp +++ b/src/MaaCore/Config/ResourceLoader.cpp @@ -26,6 +26,7 @@ bool asst::ResourceLoader::load(const std::filesystem::path& path) auto full_path = path / Filename; \ bool ret = load_resource(full_path); \ if (!ret) { \ + m_loaded = false; \ Log.error(#Config, " load failed, path:", full_path); \ return false; \ } \ @@ -38,6 +39,7 @@ bool asst::ResourceLoader::load(const std::filesystem::path& path) auto full_templ_dir = path / TemplDir; \ bool ret = load_resource_with_templ(full_path, full_templ_dir); \ if (!ret) { \ + m_loaded = false; \ Log.error(#Config, "load failed, path:", full_path, ", templ dir:", full_templ_dir); \ return false; \ } \ @@ -72,3 +74,8 @@ bool asst::ResourceLoader::load(const std::filesystem::path& path) return true; } + +bool asst::ResourceLoader::loaded() const noexcept +{ + return m_loaded; +} diff --git a/src/MaaCore/Config/ResourceLoader.h b/src/MaaCore/Config/ResourceLoader.h index 06e55c6bec..e1b4671027 100644 --- a/src/MaaCore/Config/ResourceLoader.h +++ b/src/MaaCore/Config/ResourceLoader.h @@ -16,6 +16,7 @@ namespace asst virtual ~ResourceLoader() override = default; virtual bool load(const std::filesystem::path& path) override; + bool loaded() const noexcept; private: template diff --git a/src/MaaCore/MaaCore.vcxproj b/src/MaaCore/MaaCore.vcxproj index 8f9bf38e43..a4866360c2 100644 --- a/src/MaaCore/MaaCore.vcxproj +++ b/src/MaaCore/MaaCore.vcxproj @@ -386,4 +386,4 @@ - + \ No newline at end of file