Initial version
This commit is contained in:
1 parent
2cff35f215
commit
fe8e2cb5e7
360 files changed
+253989
-74
No files matched your search
@@ -0,0 +1,61 @@
|
||||
|
||||
|
||||
use std::{ffi::{c_char, c_void}, fs::File, os::windows::{ffi::OsStrExt, io::AsRawHandle}, sync::OnceLock};
|
||||
|
||||
type Open = unsafe extern "system" fn(*mut c_void, *mut *mut c_void) -> i32;
|
||||
type Read = unsafe extern "system" fn(*mut c_void, *mut c_void, u32, u64) -> i32;
|
||||
type Close = unsafe extern "system" fn(*mut c_void);
|
||||
type Limits = unsafe extern "system" fn(*mut u32, *mut u32, *mut u64);
|
||||
struct Api { open: Open, read: Read, close: Close, limits: Option<Limits> }
|
||||
#[link(name="kernel32")]
|
||||
unsafe extern "system" {
|
||||
fn LoadLibraryExW(path: *const u16, file: *mut c_void, flags: u32) -> *mut c_void;
|
||||
fn GetProcAddress(module: *mut c_void, name: *const c_char) -> *const c_void;
|
||||
}
|
||||
fn api() -> Option<&'static Api> {
|
||||
static API: OnceLock<Option<Api>> = OnceLock::new();
|
||||
API.get_or_init(|| {
|
||||
let path = std::env::current_exe().ok()?.parent()?.join("eonacat-xs-dstorage-bridge.dll");
|
||||
let wide: Vec<u16> = path.as_os_str().encode_wide().chain(Some(0)).collect();
|
||||
|
||||
let lib = unsafe { LoadLibraryExW(wide.as_ptr(), std::ptr::null_mut(), 0x100 | 0x1000) };
|
||||
if lib.is_null() { return None; }
|
||||
let open = unsafe { GetProcAddress(lib, c"eonacat_xs_ds_open".as_ptr()) };
|
||||
let read = unsafe { GetProcAddress(lib, c"eonacat_xs_ds_read".as_ptr()) };
|
||||
let close = unsafe { GetProcAddress(lib, c"eonacat_xs_ds_close".as_ptr()) };
|
||||
if open.is_null() || read.is_null() || close.is_null() { return None; }
|
||||
let limits = unsafe { GetProcAddress(lib, c"eonacat_xs_ds_get_limits".as_ptr()) };
|
||||
Some(Api {
|
||||
open: unsafe { std::mem::transmute::<*const c_void, Open>(open) },
|
||||
read: unsafe { std::mem::transmute::<*const c_void, Read>(read) },
|
||||
close: unsafe { std::mem::transmute::<*const c_void, Close>(close) },
|
||||
limits: if limits.is_null() { None } else { Some(unsafe { std::mem::transmute::<*const c_void, Limits>(limits) }) },
|
||||
})
|
||||
}).as_ref()
|
||||
}
|
||||
|
||||
pub(super) struct NativeFile { context: *mut c_void, api: &'static Api }
|
||||
|
||||
unsafe impl Send for NativeFile {}
|
||||
unsafe impl Sync for NativeFile {}
|
||||
impl NativeFile {
|
||||
pub(super) fn open(file: &File) -> Option<Self> {
|
||||
let api=api()?; let mut context=std::ptr::null_mut();
|
||||
let result=unsafe { (api.open)(file.as_raw_handle().cast(), &mut context) };
|
||||
(result >= 0 && !context.is_null()).then_some(Self { context, api })
|
||||
}
|
||||
pub(super) fn limits(&self) -> Option<(u32, u32, u64)> {
|
||||
let limits = self.api.limits?;
|
||||
let (mut slots, mut wait_ms, mut staging_bytes) = (0, 0, 0);
|
||||
unsafe { limits(&mut slots, &mut wait_ms, &mut staging_bytes) };
|
||||
Some((slots, wait_ms, staging_bytes))
|
||||
}
|
||||
pub(super) fn read(&self, output: &mut [u8], offset: u64) -> Result<usize,i32> {
|
||||
let len=u32::try_from(output.len()).map_err(|_| -1)?;
|
||||
|
||||
let hr=unsafe { (self.api.read)(self.context, output.as_mut_ptr().cast(), len, offset) };
|
||||
if hr < 0 { Err(hr) } else { Ok(output.len()) }
|
||||
}
|
||||
}
|
||||
impl Drop for NativeFile { fn drop(&mut self) { unsafe { (self.api.close)(self.context) } } }
|
||||
|
||||
Reference in new issue
Block a user