Skip to content

Commit

Permalink
set DLL search path in maa-cli instead of maa-sys
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc committed Oct 13, 2023
1 parent 4a2151e commit bea75e2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions maa-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ features = ["rustls-tls", "blocking", "stream", "json"]

[dev-dependencies]
serde_test = "1.0.176"

[target.windows.dependencies]
windows-sys = { version = "0.48.0", features = ["Win32_System_LibraryLoader"] }
5 changes: 0 additions & 5 deletions maa-cli/src/installer/maa_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,6 @@ pub fn find_lib_dir(dirs: &Dirs) -> Option<PathBuf> {
None
}

pub fn find_maa_core(dirs: &Dirs) -> Option<PathBuf> {
let lib_dir = find_lib_dir(dirs)?;
Some(lib_dir.join(MAA_CORE_NAME))
}

pub fn find_resource(dirs: &Dirs) -> Option<PathBuf> {
let resource_dir = dirs.resource();
if resource_dir.exists() {
Expand Down
15 changes: 12 additions & 3 deletions maa-cli/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
Error as ConfigError, FindFile,
},
dirs::{Dirs, Ensure},
installer::maa_core::{find_maa_core, find_resource, MAA_CORE_NAME},
installer::maa_core::{find_lib_dir, find_resource, MAA_CORE_NAME},
log::{set_level, LogLevel},
{debug, normal, warning},
};
Expand Down Expand Up @@ -458,8 +458,17 @@ fn process_resource_dir(path: PathBuf) -> Option<PathBuf> {
}

fn load_core(dirs: &Dirs) {
if let Some(core_path) = find_maa_core(dirs) {
maa_sys::binding::load(core_path);
if let Some(lib_dir) = find_lib_dir(dirs) {
// Set DLL directory on Windows
#[cfg(target_os = "windows")]
{
use std::os::windows::ffi::OsStrExt;
use windows_sys::windows::Win32::System::LibraryLoader::SetDllDirectoryW;

let lib_dir_w: Vec<u16> = lib_dir.as_ref().encode_wide().chain(Some(0)).collect();
unsafe { SetDllDirectoryW(lib_dir_w.as_ptr()) };
}
maa_sys::binding::load(lib_dir.join(MAA_CORE_NAME));
} else {
maa_sys::binding::load(MAA_CORE_NAME);
}
Expand Down
3 changes: 0 additions & 3 deletions maa-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ libloading = { version = "0.8.0", optional = true }

[build-dependencies]
directories = "5"

[target.windows.dependencies]
windows-sys = { version = "0.48.0", features = ["Win32_System_LibraryLoader"] }
14 changes: 0 additions & 14 deletions maa-sys/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ macro_rules! link {
}

pub fn load(path: impl AsRef<std::ffi::OsStr>) {
// Set DLL search path to the directory of the shared library.
#[cfg(target_os = "windows")]
{
use windows_sys::windows::Win32::System::LibraryLoader::SetDllDirectoryW;
use std::os::windows::ffi::OsStrExt;

let pathbuf = std::path::PathBuf::from(path.as_ref());

if let Some(dll_dir) = pathbuf.parent() {
let wine_dll_dir: Vec<u16> = path.as_ref().encode_wide().chain(Some(0)).collect();
unsafe { SetDllDirectoryW(path.as_ptr()) };
}
}

SHARED_LIBRARY.with(|lib| {
*lib.borrow_mut() = Some(Arc::new(SharedLibrary::new(path).unwrap()));
});
Expand Down

0 comments on commit bea75e2

Please sign in to comment.