From fb34be43c43caab4b0527c3dc01390d85b8e4b1a Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 20 Feb 2024 18:36:32 +0800 Subject: [PATCH 01/10] feat: app_dir() for runtime --- .../LibraryHolder/ControlUnit/ControlUnit.cpp | 63 +++---------------- source/MaaProjectInterfaceCli/main.cpp | 10 +-- source/MaaProjectInterfaceCli/runner.cpp | 3 - source/MaaToolkit/Config/ConfigMgr.cpp | 22 ++++--- source/MaaToolkit/Config/ConfigMgr.h | 10 +-- .../MaaUtils/AppRuntime/AppRuntime_Posix.cpp | 5 ++ source/MaaUtils/AppRuntime/AppRuntime_Win.cpp | 55 ++++++++++++++++ source/include/Utils/AppRuntime.h | 12 ++++ 8 files changed, 105 insertions(+), 75 deletions(-) create mode 100644 source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp create mode 100644 source/MaaUtils/AppRuntime/AppRuntime_Win.cpp create mode 100644 source/include/Utils/AppRuntime.h diff --git a/source/LibraryHolder/ControlUnit/ControlUnit.cpp b/source/LibraryHolder/ControlUnit/ControlUnit.cpp index e7b15a856..73a6c5006 100644 --- a/source/LibraryHolder/ControlUnit/ControlUnit.cpp +++ b/source/LibraryHolder/ControlUnit/ControlUnit.cpp @@ -7,52 +7,7 @@ #include "ControlUnit/ThriftControlUnitAPI.h" #include "ControlUnit/Win32ControlUnitAPI.h" #include "Utils/Logger.h" - -static std::filesystem::path library_dir; - -#ifdef _WIN32 -#include "Utils/SafeWindows.hpp" - -void init_library_path(HINSTANCE hinstDLL) -{ - char buffer[MAX_PATH + 1] = { 0 }; - GetModuleFileName(hinstDLL, buffer, MAX_PATH); - library_dir = MAA_NS::path(buffer).parent_path(); -} - -// https://learn.microsoft.com/zh-cn/windows/win32/dlls/dllmain -BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module - DWORD fdwReason, // reason for calling function - LPVOID lpvReserved) // reserved -{ - // Perform actions based on the reason for calling. - switch (fdwReason) { - case DLL_PROCESS_ATTACH: - // Initialize once for each new process. - // Return FALSE to fail DLL load. - init_library_path(hinstDLL); - break; - - case DLL_THREAD_ATTACH: - // Do thread-specific initialization. - break; - - case DLL_THREAD_DETACH: - // Do thread-specific cleanup. - break; - - case DLL_PROCESS_DETACH: - - if (lpvReserved != nullptr) { - break; // do not do cleanup if process termination scenario - } - - // Perform any necessary cleanup. - break; - } - return TRUE; // Successful DLL_PROCESS_ATTACH. -} -#endif +#include "Utils/AppRuntime.h" MAA_NS_BEGIN @@ -78,8 +33,8 @@ std::shared_ptr AdbControlUnitLibraryHolder::c MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { - if (!load_library(library_dir / libname_)) { - LogError << "Failed to load library" << VAR(library_dir) << VAR(libname_); + if (!load_library(app_dir() / libname_)) { + LogError << "Failed to load library" << VAR(app_dir()) << VAR(libname_); return nullptr; } @@ -117,8 +72,8 @@ std::shared_ptr Win32ControlUnitLibraryHolder: MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { - if (!load_library(library_dir / libname_)) { - LogError << "Failed to load library" << VAR(library_dir) << VAR(libname_); + if (!load_library(app_dir() / libname_)) { + LogError << "Failed to load library" << VAR(app_dir()) << VAR(libname_); return nullptr; } @@ -154,8 +109,8 @@ std::shared_ptr Win32ControlUnitLibraryHolder: std::shared_ptr DbgControlUnitLibraryHolder::create_control_unit( MaaDbgControllerType type, MaaStringView read_path) { - if (!load_library(library_dir / libname_)) { - LogError << "Failed to load library" << VAR(library_dir) << VAR(libname_); + if (!load_library(app_dir() / libname_)) { + LogError << "Failed to load library" << VAR(app_dir()) << VAR(libname_); return nullptr; } @@ -191,8 +146,8 @@ std::shared_ptr DbgControlUnitLibraryHolder::c std::shared_ptr ThriftControlUnitLibraryHolder::create_control_unit( MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config) { - if (!load_library(library_dir / libname_)) { - LogError << "Failed to load library" << VAR(library_dir) << VAR(libname_); + if (!load_library(app_dir() / libname_)) { + LogError << "Failed to load library" << VAR(app_dir()) << VAR(libname_); return nullptr; } diff --git a/source/MaaProjectInterfaceCli/main.cpp b/source/MaaProjectInterfaceCli/main.cpp index e44b6043b..521028933 100644 --- a/source/MaaProjectInterfaceCli/main.cpp +++ b/source/MaaProjectInterfaceCli/main.cpp @@ -1,7 +1,9 @@ #include #include +#include "MaaToolkit/MaaToolkitAPI.h" #include "Utils/Platform.h" +#include "Utils/AppRuntime.h" #include "interactor.h" #include "runner.h" @@ -14,13 +16,11 @@ void mpause() int main(int argc, char** argv) { - std::ignore = argc; - - auto app_path = MaaNS::path(argv[0]); - auto project_dir = app_path.parent_path(); + MaaToolkitInit(); Interactor interactor; - if (!interactor.load(project_dir)) { + + if (!interactor.load(MAA_NS::app_dir())) { mpause(); return -1; } diff --git a/source/MaaProjectInterfaceCli/runner.cpp b/source/MaaProjectInterfaceCli/runner.cpp index c28d107df..ef0b00284 100644 --- a/source/MaaProjectInterfaceCli/runner.cpp +++ b/source/MaaProjectInterfaceCli/runner.cpp @@ -12,8 +12,6 @@ bool Runner::run(const MAA_PROJECT_INTERFACE_NS::RuntimeParam& param) { - MaaToolkitInit(); - auto maa_handle = MaaCreate(&Runner::on_maafw_notify, nullptr); auto controller_handle = MaaAdbControllerCreateV2(param.adb_param.adb_path.c_str(), param.adb_param.address.c_str(), @@ -46,7 +44,6 @@ bool Runner::run(const MAA_PROJECT_INTERFACE_NS::RuntimeParam& param) MaaDestroy(maa_handle); MaaResourceDestroy(resource_handle); MaaControllerDestroy(controller_handle); - MaaToolkitUninit(); }); for (const auto& [name, executor] : param.recognizer) { diff --git a/source/MaaToolkit/Config/ConfigMgr.cpp b/source/MaaToolkit/Config/ConfigMgr.cpp index a6b82990a..999cd2ff2 100644 --- a/source/MaaToolkit/Config/ConfigMgr.cpp +++ b/source/MaaToolkit/Config/ConfigMgr.cpp @@ -8,14 +8,18 @@ #include "MaaFramework/MaaAPI.h" #include "Utils/Logger.h" #include "Utils/Platform.h" +#include "Utils/AppRuntime.h" MAA_TOOLKIT_NS_BEGIN bool ConfigMgr::init() { - LogFunc << VAR(kConfigPath); + LogFunc << VAR(app_dir()); - if (!std::filesystem::exists(kConfigPath)) { + config_path_ = app_dir() / kConfigPath; + debug_dir_ = app_dir() / kDebugDir; + + if (!std::filesystem::exists(config_path_)) { dump(); } @@ -37,11 +41,11 @@ bool ConfigMgr::uninit() bool ConfigMgr::load() { - LogFunc << VAR(kConfigPath); + LogFunc << VAR(config_path_); - auto json_opt = json::open(kConfigPath); + auto json_opt = json::open(config_path_); if (!json_opt) { - LogError << "Failed to open json file:" << kConfigPath; + LogError << "Failed to open json file:" << config_path_; return false; } @@ -65,7 +69,7 @@ bool ConfigMgr::parse_and_apply_policy(const json::value& policy_json) LogFunc << VAR(policy_json); policy_logging_ = policy_json.get(kPolicyLoggging, policy_logging_); - std::string logging_dir = policy_logging_ ? path_to_utf8_string(kDebugDir) : ""; + std::string logging_dir = policy_logging_ ? path_to_utf8_string(debug_dir_) : ""; MaaSetGlobalOption(MaaGlobalOption_LogDir, static_cast(logging_dir.data()), logging_dir.size()); policy_save_draw_ = policy_json.get(kPolicySaveDraw, policy_save_draw_); @@ -104,10 +108,10 @@ bool ConfigMgr::save(const json::value& root) const { LogInfo; - std::filesystem::create_directories(kConfigPath.parent_path()); - std::ofstream ofs(kConfigPath, std::ios::out); + std::filesystem::create_directories(config_path_.parent_path()); + std::ofstream ofs(config_path_, std::ios::out); if (!ofs.is_open()) { - LogError << "Failed to open config file:" << kConfigPath; + LogError << "Failed to open config file:" << config_path_; return false; } ofs << root; diff --git a/source/MaaToolkit/Config/ConfigMgr.h b/source/MaaToolkit/Config/ConfigMgr.h index d938bf251..03e59ad90 100644 --- a/source/MaaToolkit/Config/ConfigMgr.h +++ b/source/MaaToolkit/Config/ConfigMgr.h @@ -19,10 +19,8 @@ class ConfigMgr : public SingletonHolder, public MaaToolkitConfigMgrA friend class SingletonHolder; public: - // TODO: linux 可能要区分下放到家目录之类的 - inline static const std::filesystem::path kUserDir = "."; - inline static const std::filesystem::path kConfigPath = kUserDir / "config" / "maa_toolkit.json"; - inline static const std::filesystem::path kDebugDir = kUserDir / "debug"; + inline static const std::filesystem::path kConfigPath = "config/maa_toolkit.json"; + inline static const std::filesystem::path kDebugDir = "debug"; public: inline static const std::string kPolicyKey = "policy"; @@ -48,6 +46,10 @@ class ConfigMgr : public SingletonHolder, public MaaToolkitConfigMgrA bool dump() const; bool save(const json::value& root) const; +private: + std::filesystem::path config_path_; + std::filesystem::path debug_dir_; + private: bool policy_logging_ = true; bool policy_save_draw_ = false; diff --git a/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp b/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp new file mode 100644 index 000000000..aee7ff57c --- /dev/null +++ b/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp @@ -0,0 +1,5 @@ +#ifndef _WIN32 + +#include "Utils/AppRuntime.h" + +#endif diff --git a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp b/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp new file mode 100644 index 000000000..4b9c8cc72 --- /dev/null +++ b/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp @@ -0,0 +1,55 @@ +#ifdef _WIN32 + +#include "Utils/AppRuntime.h" + +#include "Utils/Platform.h" +#include "Utils/SafeWindows.hpp" + +static std::filesystem::path app_dir_cache; + +const std::filesystem::path& app_dir() +{ + return app_dir_cache; +} + +void init_app_dir(HINSTANCE hinstDLL) +{ + char buffer[MAX_PATH + 1] = { 0 }; + GetModuleFileName(hinstDLL, buffer, MAX_PATH); + app_dir_cache = MAA_NS::path(buffer).parent_path(); +} + +// https://learn.microsoft.com/zh-cn/windows/win32/dlls/dllmain +BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module + DWORD fdwReason, // reason for calling function + LPVOID lpvReserved) // reserved +{ + // Perform actions based on the reason for calling. + switch (fdwReason) { + case DLL_PROCESS_ATTACH: + // Initialize once for each new process. + // Return FALSE to fail DLL load. + init_app_dir(hinstDLL); + break; + + case DLL_THREAD_ATTACH: + // Do thread-specific initialization. + break; + + case DLL_THREAD_DETACH: + // Do thread-specific cleanup. + break; + + case DLL_PROCESS_DETACH: + + if (lpvReserved != nullptr) { + break; // do not do cleanup if process termination scenario + } + + // Perform any necessary cleanup. + break; + } + return TRUE; // Successful DLL_PROCESS_ATTACH. +} + +#endif diff --git a/source/include/Utils/AppRuntime.h b/source/include/Utils/AppRuntime.h new file mode 100644 index 000000000..8ac081588 --- /dev/null +++ b/source/include/Utils/AppRuntime.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include "Conf/Conf.h" +#include "MaaFramework/MaaPort.h" + +MAA_NS_BEGIN + +MAA_UTILS_API const std::filesystem::path& app_dir(); + +MAA_NS_END From e6fc3957e1f955c44edce4baff42652d3e71c40f Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 20 Feb 2024 18:39:20 +0800 Subject: [PATCH 02/10] fix: namespace error --- source/MaaUtils/AppRuntime/AppRuntime_Win.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp b/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp index 4b9c8cc72..b917f53d3 100644 --- a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp +++ b/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp @@ -5,18 +5,23 @@ #include "Utils/Platform.h" #include "Utils/SafeWindows.hpp" -static std::filesystem::path app_dir_cache; +MAA_NS_BEGIN + +static std::filesystem::path s_app_dir_cache; const std::filesystem::path& app_dir() { - return app_dir_cache; + return s_app_dir_cache; } +MAA_NS_END + + void init_app_dir(HINSTANCE hinstDLL) { char buffer[MAX_PATH + 1] = { 0 }; GetModuleFileName(hinstDLL, buffer, MAX_PATH); - app_dir_cache = MAA_NS::path(buffer).parent_path(); + MAA_NS::s_app_dir_cache = MAA_NS::path(buffer).parent_path(); } // https://learn.microsoft.com/zh-cn/windows/win32/dlls/dllmain From 986850b6db871de3b852c884a0602f914d42b921 Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 20 Feb 2024 18:51:51 +0800 Subject: [PATCH 03/10] feat: app_dir impl for posix --- .../MaaUtils/AppRuntime/AppRuntime_Posix.cpp | 32 ++++++++++++++++++- source/MaaUtils/AppRuntime/AppRuntime_Win.cpp | 1 - 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp b/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp index aee7ff57c..4ca889e7f 100644 --- a/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp +++ b/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp @@ -1,5 +1,35 @@ #ifndef _WIN32 - #include "Utils/AppRuntime.h" +#include + +#include "Utils/Platform.h" + +MAA_NS_BEGIN + +void init_app_dir(); + +static std::filesystem::path s_app_dir_cache; + +const std::filesystem::path& app_dir() +{ + if (s_app_dir_cache.empty()) { + init_app_dir(); + } + + return s_app_dir_cache; +} + +void init_app_dir() +{ + Dl_info dl_info{}; + if (dladdr((void*)init_app_dir, &dl_info) == 0) { + return; + } + + s_app_dir_cache = MAA_NS::path(dl_info.dli_fname).parent_path(); +} + +MAA_NS_END + #endif diff --git a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp b/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp index b917f53d3..9cfd8fa17 100644 --- a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp +++ b/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp @@ -1,5 +1,4 @@ #ifdef _WIN32 - #include "Utils/AppRuntime.h" #include "Utils/Platform.h" From 5f7c19168b282f376b7cac1f22333d5a3d1e6205 Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 20 Feb 2024 18:55:44 +0800 Subject: [PATCH 04/10] styles: format --- source/MaaUtils/AppRuntime/AppRuntime_Win.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp b/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp index 9cfd8fa17..91c5472b2 100644 --- a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp +++ b/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp @@ -13,16 +13,15 @@ const std::filesystem::path& app_dir() return s_app_dir_cache; } -MAA_NS_END - - void init_app_dir(HINSTANCE hinstDLL) { char buffer[MAX_PATH + 1] = { 0 }; GetModuleFileName(hinstDLL, buffer, MAX_PATH); - MAA_NS::s_app_dir_cache = MAA_NS::path(buffer).parent_path(); + s_app_dir_cache = MAA_NS::path(buffer).parent_path(); } +MAA_NS_END + // https://learn.microsoft.com/zh-cn/windows/win32/dlls/dllmain BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module DWORD fdwReason, // reason for calling function @@ -33,7 +32,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module case DLL_PROCESS_ATTACH: // Initialize once for each new process. // Return FALSE to fail DLL load. - init_app_dir(hinstDLL); + MAA_NS::init_app_dir(hinstDLL); break; case DLL_THREAD_ATTACH: From b91f616d56c3e98c1e4d635dd15592e1b7acb2d8 Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 21 Feb 2024 00:35:18 +0800 Subject: [PATCH 05/10] ci: add python binding test --- .github/workflows/test.yml | 15 +++++++++++++++ test/python/binding_test.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 test/python/binding_test.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fbc2b5b2b..30fb4f260 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -123,6 +123,11 @@ jobs: run: | ./install/bin/PipelineTesting.exe ./install/test + - name: Run Python testing + shell: bash + run: | + python3 ./test/python/binding_test.py + - uses: actions/upload-artifact@v3 if: always() with: @@ -208,6 +213,11 @@ jobs: run: | ./install/bin/PipelineTesting ./install/test + - name: Run Python testing + shell: bash + run: | + python3 ./test/python/binding_test.py + - uses: actions/upload-artifact@v3 if: always() with: @@ -283,6 +293,11 @@ jobs: run: | ./install/bin/PipelineTesting ./install/test + - name: Run Python testing + shell: bash + run: | + python3 ./test/python/binding_test.py + - uses: actions/upload-artifact@v3 if: always() with: diff --git a/test/python/binding_test.py b/test/python/binding_test.py new file mode 100644 index 000000000..849cbeb42 --- /dev/null +++ b/test/python/binding_test.py @@ -0,0 +1,35 @@ +from pathlib import Path +import sys + +current_dir = Path(__file__).resolve().parent + +binding_dir = current_dir.parent.parent / "source" / "binding" / "Python" +binding_dir = str(binding_dir) +print(binding_dir) +if binding_dir not in sys.path: + sys.path.insert(0, binding_dir) + + +from maa.library import Library +from maa.resource import Resource +from maa.controller import AdbController +from maa.instance import Instance +from maa.toolkit import Toolkit + +if __name__ == "__main__": + version = Library.open(current_dir.parent.parent / "install" / "bin") + print(f"MaaFw Version: {version}") + + Toolkit.init_config() + + resource = Resource() + print(f"resource: {hex(resource._handle)}") + + adb_controller = AdbController("adb", "127.0.0.1:5555") + print(f"adb_controller: {hex(adb_controller._handle)}") + + maa_inst = Instance() + maa_inst.bind(resource, adb_controller) + print(f"maa_inst: {hex(maa_inst._handle)}") + + print("Done.") From 1c47035bb677c489ff4c9ffc3665dd570d9dbfbd Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 21 Feb 2024 00:38:47 +0800 Subject: [PATCH 06/10] chore: rename --- .../LibraryHolder/ControlUnit/ControlUnit.cpp | 18 +++++----- source/MaaProjectInterfaceCli/main.cpp | 4 +-- source/MaaToolkit/Config/ConfigMgr.cpp | 8 ++--- .../MaaUtils/AppRuntime/AppRuntime_Posix.cpp | 35 ------------------- source/MaaUtils/Runtime/Runtime_Posix.cpp | 35 +++++++++++++++++++ .../Runtime_Win.cpp} | 14 ++++---- .../include/Utils/{AppRuntime.h => Runtime.h} | 2 +- 7 files changed, 58 insertions(+), 58 deletions(-) delete mode 100644 source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp create mode 100644 source/MaaUtils/Runtime/Runtime_Posix.cpp rename source/MaaUtils/{AppRuntime/AppRuntime_Win.cpp => Runtime/Runtime_Win.cpp} (78%) rename source/include/Utils/{AppRuntime.h => Runtime.h} (67%) diff --git a/source/LibraryHolder/ControlUnit/ControlUnit.cpp b/source/LibraryHolder/ControlUnit/ControlUnit.cpp index 73a6c5006..d082024cf 100644 --- a/source/LibraryHolder/ControlUnit/ControlUnit.cpp +++ b/source/LibraryHolder/ControlUnit/ControlUnit.cpp @@ -7,7 +7,7 @@ #include "ControlUnit/ThriftControlUnitAPI.h" #include "ControlUnit/Win32ControlUnitAPI.h" #include "Utils/Logger.h" -#include "Utils/AppRuntime.h" +#include "Utils/Runtime.h" MAA_NS_BEGIN @@ -33,8 +33,8 @@ std::shared_ptr AdbControlUnitLibraryHolder::c MaaStringView adb_path, MaaStringView adb_serial, MaaAdbControllerType type, MaaStringView config, MaaStringView agent_path, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { - if (!load_library(app_dir() / libname_)) { - LogError << "Failed to load library" << VAR(app_dir()) << VAR(libname_); + if (!load_library(library_dir() / libname_)) { + LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_); return nullptr; } @@ -72,8 +72,8 @@ std::shared_ptr Win32ControlUnitLibraryHolder: MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { - if (!load_library(app_dir() / libname_)) { - LogError << "Failed to load library" << VAR(app_dir()) << VAR(libname_); + if (!load_library(library_dir() / libname_)) { + LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_); return nullptr; } @@ -109,8 +109,8 @@ std::shared_ptr Win32ControlUnitLibraryHolder: std::shared_ptr DbgControlUnitLibraryHolder::create_control_unit( MaaDbgControllerType type, MaaStringView read_path) { - if (!load_library(app_dir() / libname_)) { - LogError << "Failed to load library" << VAR(app_dir()) << VAR(libname_); + if (!load_library(library_dir() / libname_)) { + LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_); return nullptr; } @@ -146,8 +146,8 @@ std::shared_ptr DbgControlUnitLibraryHolder::c std::shared_ptr ThriftControlUnitLibraryHolder::create_control_unit( MaaThriftControllerType type, MaaStringView host, int32_t port, MaaStringView config) { - if (!load_library(app_dir() / libname_)) { - LogError << "Failed to load library" << VAR(app_dir()) << VAR(libname_); + if (!load_library(library_dir() / libname_)) { + LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_); return nullptr; } diff --git a/source/MaaProjectInterfaceCli/main.cpp b/source/MaaProjectInterfaceCli/main.cpp index 521028933..0e1f52967 100644 --- a/source/MaaProjectInterfaceCli/main.cpp +++ b/source/MaaProjectInterfaceCli/main.cpp @@ -3,7 +3,7 @@ #include "MaaToolkit/MaaToolkitAPI.h" #include "Utils/Platform.h" -#include "Utils/AppRuntime.h" +#include "Utils/Runtime.h" #include "interactor.h" #include "runner.h" @@ -20,7 +20,7 @@ int main(int argc, char** argv) Interactor interactor; - if (!interactor.load(MAA_NS::app_dir())) { + if (!interactor.load(MAA_NS::library_dir())) { mpause(); return -1; } diff --git a/source/MaaToolkit/Config/ConfigMgr.cpp b/source/MaaToolkit/Config/ConfigMgr.cpp index 999cd2ff2..09481a29d 100644 --- a/source/MaaToolkit/Config/ConfigMgr.cpp +++ b/source/MaaToolkit/Config/ConfigMgr.cpp @@ -8,16 +8,16 @@ #include "MaaFramework/MaaAPI.h" #include "Utils/Logger.h" #include "Utils/Platform.h" -#include "Utils/AppRuntime.h" +#include "Utils/Runtime.h" MAA_TOOLKIT_NS_BEGIN bool ConfigMgr::init() { - LogFunc << VAR(app_dir()); + LogFunc << VAR(library_dir()); - config_path_ = app_dir() / kConfigPath; - debug_dir_ = app_dir() / kDebugDir; + config_path_ = library_dir() / kConfigPath; + debug_dir_ = library_dir() / kDebugDir; if (!std::filesystem::exists(config_path_)) { dump(); diff --git a/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp b/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp deleted file mode 100644 index 4ca889e7f..000000000 --- a/source/MaaUtils/AppRuntime/AppRuntime_Posix.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef _WIN32 -#include "Utils/AppRuntime.h" - -#include - -#include "Utils/Platform.h" - -MAA_NS_BEGIN - -void init_app_dir(); - -static std::filesystem::path s_app_dir_cache; - -const std::filesystem::path& app_dir() -{ - if (s_app_dir_cache.empty()) { - init_app_dir(); - } - - return s_app_dir_cache; -} - -void init_app_dir() -{ - Dl_info dl_info{}; - if (dladdr((void*)init_app_dir, &dl_info) == 0) { - return; - } - - s_app_dir_cache = MAA_NS::path(dl_info.dli_fname).parent_path(); -} - -MAA_NS_END - -#endif diff --git a/source/MaaUtils/Runtime/Runtime_Posix.cpp b/source/MaaUtils/Runtime/Runtime_Posix.cpp new file mode 100644 index 000000000..977c396ad --- /dev/null +++ b/source/MaaUtils/Runtime/Runtime_Posix.cpp @@ -0,0 +1,35 @@ +#ifndef _WIN32 +#include "Utils/Runtime.h" + +#include + +#include "Utils/Platform.h" + +MAA_NS_BEGIN + +void init_library_dir(); + +static std::filesystem::path s_library_dir_cache; + +const std::filesystem::path& library_dir() +{ + if (s_library_dir_cache.empty()) { + init_library_dir(); + } + + return s_library_dir_cache; +} + +void init_library_dir() +{ + Dl_info dl_info{}; + if (dladdr((void*)init_library_dir, &dl_info) == 0) { + return; + } + + s_library_dir_cache = MAA_NS::path(dl_info.dli_fname).parent_path(); +} + +MAA_NS_END + +#endif diff --git a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp b/source/MaaUtils/Runtime/Runtime_Win.cpp similarity index 78% rename from source/MaaUtils/AppRuntime/AppRuntime_Win.cpp rename to source/MaaUtils/Runtime/Runtime_Win.cpp index 91c5472b2..b56efd355 100644 --- a/source/MaaUtils/AppRuntime/AppRuntime_Win.cpp +++ b/source/MaaUtils/Runtime/Runtime_Win.cpp @@ -1,23 +1,23 @@ #ifdef _WIN32 -#include "Utils/AppRuntime.h" +#include "Utils/Runtime.h" #include "Utils/Platform.h" #include "Utils/SafeWindows.hpp" MAA_NS_BEGIN -static std::filesystem::path s_app_dir_cache; +static std::filesystem::path s_library_dir_cache; -const std::filesystem::path& app_dir() +const std::filesystem::path& library_dir() { - return s_app_dir_cache; + return s_library_dir_cache; } -void init_app_dir(HINSTANCE hinstDLL) +void init_library_dir(HINSTANCE hinstDLL) { char buffer[MAX_PATH + 1] = { 0 }; GetModuleFileName(hinstDLL, buffer, MAX_PATH); - s_app_dir_cache = MAA_NS::path(buffer).parent_path(); + s_library_dir_cache = MAA_NS::path(buffer).parent_path(); } MAA_NS_END @@ -32,7 +32,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module case DLL_PROCESS_ATTACH: // Initialize once for each new process. // Return FALSE to fail DLL load. - MAA_NS::init_app_dir(hinstDLL); + MAA_NS::init_library_dir(hinstDLL); break; case DLL_THREAD_ATTACH: diff --git a/source/include/Utils/AppRuntime.h b/source/include/Utils/Runtime.h similarity index 67% rename from source/include/Utils/AppRuntime.h rename to source/include/Utils/Runtime.h index 8ac081588..0f29acf8d 100644 --- a/source/include/Utils/AppRuntime.h +++ b/source/include/Utils/Runtime.h @@ -7,6 +7,6 @@ MAA_NS_BEGIN -MAA_UTILS_API const std::filesystem::path& app_dir(); +MAA_UTILS_API const std::filesystem::path& library_dir(); MAA_NS_END From bdc36186a3139dbf58f96dd1436bd2c0ef01dc4d Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 21 Feb 2024 00:49:37 +0800 Subject: [PATCH 07/10] ci: fix python test error --- .github/workflows/test.yml | 3 +++ source/binding/Python/requirements.txt | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 source/binding/Python/requirements.txt diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 30fb4f260..79fe09015 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -126,6 +126,7 @@ jobs: - name: Run Python testing shell: bash run: | + python3 -m pip install -r ./source/binding/Python/requirements.txt python3 ./test/python/binding_test.py - uses: actions/upload-artifact@v3 @@ -216,6 +217,7 @@ jobs: - name: Run Python testing shell: bash run: | + python3 -m pip install -r ./source/binding/Python/requirements.txt python3 ./test/python/binding_test.py - uses: actions/upload-artifact@v3 @@ -296,6 +298,7 @@ jobs: - name: Run Python testing shell: bash run: | + python3 -m pip install -r ./source/binding/Python/requirements.txt python3 ./test/python/binding_test.py - uses: actions/upload-artifact@v3 diff --git a/source/binding/Python/requirements.txt b/source/binding/Python/requirements.txt new file mode 100644 index 000000000..0d59398ea --- /dev/null +++ b/source/binding/Python/requirements.txt @@ -0,0 +1,2 @@ +numpy +Pillow From f20cf578776336de297208ca006c1d33160942dc Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 21 Feb 2024 01:08:54 +0800 Subject: [PATCH 08/10] ci: fix --- .github/workflows/test.yml | 2 +- source/binding/Python/maa/library.py | 29 +++++----------------------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 79fe09015..c38cfb366 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -159,7 +159,7 @@ jobs: - name: Install dep run: | pacman -Sy - pacman -S --noconfirm cmake python ccache ninja git + pacman -S --noconfirm cmake python python-pip ccache ninja git # https://github.com/MaaAssistantArknights/MaaFramework/actions/runs/5643408179/job/15285186255 - uses: actions/checkout@v3 diff --git a/source/binding/Python/maa/library.py b/source/binding/Python/maa/library.py index 25441a2d7..84331e745 100644 --- a/source/binding/Python/maa/library.py +++ b/source/binding/Python/maa/library.py @@ -21,30 +21,12 @@ def open( """ platform_values = { - "windows": { - "framework_libpath": "MaaFramework.dll", - "toolkit_libpath": "MaaToolkit.dll", - "environ_var": "PATH", - }, - "darwin": { - "framework_libpath": "libMaaFramework.dylib", - "toolkit_libpath": "libMaaToolkit.dylib", - "environ_var": "DYLD_LIBRARY_PATH", - }, - "linux": { - "framework_libpath": "libMaaFramework.so", - "toolkit_libpath": "libMaaToolkit.so", - "environ_var": "LD_LIBRARY_PATH", - }, + "windows": ("MaaFramework.dll", "MaaToolkit.dll"), + "darwin": ("libMaaFramework.dylib", "libMaaToolkit.dylib"), + "linux": ("libMaaFramework.so", "libMaaToolkit.so"), } platform_type = platform.system().lower() - environ_var = platform_values[platform_type]["environ_var"] - environ = os.environ[environ_var] - try: - environ += os.pathsep + str(path) - except KeyError: - environ = str(path) if platform_type == "windows": lib_import = ctypes.WinDLL @@ -53,7 +35,7 @@ def open( try: cls.framework_libpath = ( - pathlib.Path(path) / platform_values[platform_type]["framework_libpath"] + pathlib.Path(path) / platform_values[platform_type][0] ) cls.framework = lib_import(str(cls.framework_libpath)) except OSError: @@ -67,8 +49,7 @@ def open( if toolkit: try: cls.toolkit_libpath = ( - pathlib.Path(path) - / platform_values[platform_type]["toolkit_libpath"] + pathlib.Path(path) / platform_values[platform_type][1] ) cls.toolkit = lib_import(str(cls.toolkit_libpath)) except OSError: From bcfc7ddfb25ff19aa141eddf44023bc36b23aee8 Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 21 Feb 2024 09:51:54 +0800 Subject: [PATCH 09/10] ci: python venv --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c38cfb366..00cc8a056 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -298,6 +298,8 @@ jobs: - name: Run Python testing shell: bash run: | + python3 -m venv .venv + source .venv/bin/activate python3 -m pip install -r ./source/binding/Python/requirements.txt python3 ./test/python/binding_test.py From 9189941694c3147e85230cf08cb1e29a9f1f25c2 Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 21 Feb 2024 10:02:45 +0800 Subject: [PATCH 10/10] typo --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 00cc8a056..1336fdd1d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -217,6 +217,8 @@ jobs: - name: Run Python testing shell: bash run: | + python3 -m venv .venv + source .venv/bin/activate python3 -m pip install -r ./source/binding/Python/requirements.txt python3 ./test/python/binding_test.py @@ -298,8 +300,6 @@ jobs: - name: Run Python testing shell: bash run: | - python3 -m venv .venv - source .venv/bin/activate python3 -m pip install -r ./source/binding/Python/requirements.txt python3 ./test/python/binding_test.py