RinLovesYou / unity-rs

Apache License 2.0
6 stars 1 forks source link

Path does not support non-ASCII characters #4

Open nil-ref opened 1 year ago

nil-ref commented 1 year ago

After tracing, I found that use LoadLibraryA instead of LoadLibraryW in the code. This will cause this problem under Windows.

Here is my fixed code(src\libs.rs):

macro_rules! str_to_utf16 {
    ($s:expr) => {{
        let mut v: Vec<u16> = $s.encode_utf16().collect();
        v.push(0);
        v
    }};
}

#[cfg(target_os = "windows")]
pub fn load_lib<P: AsRef<Path>>(path: P) -> Result<NativeLibrary, LibError> {
    let path = path.as_ref();

    use winapi::um::libloaderapi::LoadLibraryW;

    let path_string = path.to_str().ok_or_else(|| LibError::FailedToGetLibPath)?;
    let win_path = str_to_utf16!(path_string);

    let lib = unsafe { LoadLibraryW(win_path.as_ptr()) };

    if lib.is_null() {
        return Err(LibError::FailedToLoadLib);
    }

    let lib_name = path
        .file_name()
        .ok_or(LibError::FailedToGetLibName)?
        .to_str()
        .ok_or(LibError::FailedToGetLibName)?
        .to_string();

    Ok(NativeLibrary {
        name: lib_name,
        path: path.to_path_buf(),
        handle: lib.cast(),
    })
}
nil-ref commented 1 year ago

Windows does not enable a unified utf8 environment by default for compatibility. A large number of users still use the Code Page method. This is the main reason for the problem.