Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: library_dir() for runtime #148

Merged
merged 10 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ jobs:
run: |
./install/bin/PipelineTesting.exe ./install/test

- 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
if: always()
with:
Expand Down Expand Up @@ -153,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
Expand Down Expand Up @@ -208,6 +214,14 @@ jobs:
run: |
./install/bin/PipelineTesting ./install/test

- 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

- uses: actions/upload-artifact@v3
if: always()
with:
Expand Down Expand Up @@ -283,6 +297,12 @@ jobs:
run: |
./install/bin/PipelineTesting ./install/test

- 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
if: always()
with:
Expand Down
63 changes: 9 additions & 54 deletions source/LibraryHolder/ControlUnit/ControlUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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/Runtime.h"

MAA_NS_BEGIN

Expand All @@ -78,8 +33,8 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> 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(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}

Expand Down Expand Up @@ -117,8 +72,8 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> 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(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}

Expand Down Expand Up @@ -154,8 +109,8 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> Win32ControlUnitLibraryHolder:
std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> 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(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}

Expand Down Expand Up @@ -191,8 +146,8 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> DbgControlUnitLibraryHolder::c
std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> 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(library_dir() / libname_)) {
LogError << "Failed to load library" << VAR(library_dir()) << VAR(libname_);
return nullptr;
}

Expand Down
10 changes: 5 additions & 5 deletions source/MaaProjectInterfaceCli/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <filesystem>
#include <iostream>

#include "MaaToolkit/MaaToolkitAPI.h"
#include "Utils/Platform.h"
#include "Utils/Runtime.h"

#include "interactor.h"
#include "runner.h"
Expand All @@ -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::library_dir())) {
mpause();
return -1;
}
Expand Down
3 changes: 0 additions & 3 deletions source/MaaProjectInterfaceCli/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 13 additions & 9 deletions source/MaaToolkit/Config/ConfigMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
#include "MaaFramework/MaaAPI.h"
#include "Utils/Logger.h"
#include "Utils/Platform.h"
#include "Utils/Runtime.h"

MAA_TOOLKIT_NS_BEGIN

bool ConfigMgr::init()
{
LogFunc << VAR(kConfigPath);
LogFunc << VAR(library_dir());

if (!std::filesystem::exists(kConfigPath)) {
config_path_ = library_dir() / kConfigPath;
debug_dir_ = library_dir() / kDebugDir;

if (!std::filesystem::exists(config_path_)) {
dump();
}

Expand All @@ -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;
}

Expand All @@ -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<void*>(logging_dir.data()), logging_dir.size());

policy_save_draw_ = policy_json.get(kPolicySaveDraw, policy_save_draw_);
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions source/MaaToolkit/Config/ConfigMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ class ConfigMgr : public SingletonHolder<ConfigMgr>, public MaaToolkitConfigMgrA
friend class SingletonHolder<ConfigMgr>;

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";
Expand All @@ -48,6 +46,10 @@ class ConfigMgr : public SingletonHolder<ConfigMgr>, 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;
Expand Down
35 changes: 35 additions & 0 deletions source/MaaUtils/Runtime/Runtime_Posix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _WIN32
#include "Utils/Runtime.h"

#include <dlfcn.h>

#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
58 changes: 58 additions & 0 deletions source/MaaUtils/Runtime/Runtime_Win.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifdef _WIN32
#include "Utils/Runtime.h"

#include "Utils/Platform.h"
#include "Utils/SafeWindows.hpp"

MAA_NS_BEGIN

static std::filesystem::path s_library_dir_cache;

const std::filesystem::path& library_dir()
{
return s_library_dir_cache;
}

void init_library_dir(HINSTANCE hinstDLL)
{
char buffer[MAX_PATH + 1] = { 0 };
GetModuleFileName(hinstDLL, buffer, MAX_PATH);
s_library_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
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.
MAA_NS::init_library_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
Loading
Loading