Skip to content

Commit

Permalink
find function
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson committed Sep 12, 2024
1 parent 74afe94 commit b91a608
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
8 changes: 1 addition & 7 deletions arma-rs-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ pub fn arma(_attr: TokenStream, item: TokenStream) -> TokenStream {

let ext_init = quote! {
if RV_EXTENSION.is_none() {
let mut ext = #init();
ext.register_request_context_proc(RVExtensionRequestContextProc);
RV_EXTENSION = Some(ext);
RV_EXTENSION = Some(#init());
}
};

Expand Down Expand Up @@ -47,10 +45,6 @@ pub fn arma(_attr: TokenStream, item: TokenStream) -> TokenStream {
#[no_mangle]
pub static RVExtensionFeatureFlags: u64 = arma_rs::flags::RV_CONTEXT_NO_DEFAULT_CALL;

extern "C" {
fn RVExtensionRequestContextProc();
}

#[cfg(all(target_os="windows", target_arch="x86"))]
arma_rs::link_args::windows! {
unsafe {
Expand Down
8 changes: 7 additions & 1 deletion arma-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ uuid = { version = "1.4.1", optional = true }
[target.'cfg(all(target_os="windows", target_arch="x86"))'.dependencies]
link_args = "0.6.0"

[target.'cfg(windows)'.dependencies]
winapi = "0.3.9"

[target.'cfg(windows)'.dependencies.windows]
version = "0.51.1"
version = "0.58.0"
features = ["Win32_Foundation", "Win32_System_Console"]

[target.'cfg(not(windows))'.dependencies]
libc = "0.2.158"

[dev-dependencies]
trybuild = "1.0.8"

Expand Down
7 changes: 0 additions & 7 deletions arma-rs/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ impl Context {
&self.group
}

// TODO implement
// #[must_use]
// /// Call context with stack trace, fetched from Arma on first use.
// pub fn call_context_and_stack(&self) -> &ArmaCallContext {
// todo!()
// }

#[must_use]
/// Returns the length in bytes of the output buffer.
/// This is the maximum size of the data that can be returned by the extension.
Expand Down
52 changes: 51 additions & 1 deletion arma-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,64 @@ impl ExtensionBuilder {
#[must_use]
/// Builds the extension.
pub fn finish(self) -> Extension {
// super convenient
#[cfg(all(windows, not(debug_assertions)))]
let request_context = {
let module = unsafe {
winapi::um::libloaderapi::GetModuleHandleW(std::ptr::null());
};
if module.is_null() {
panic!("GetModuleHandleW failed");
}
let function_name = CString::new("RVExtensionRequestContextProc").expect("CString::new failed");

let func_address = unsafe {
GetProcAddress(module_handle, function_name.as_ptr());
};

if func_address.is_null() {
panic!("Failed to get function address");
}

unsafe {
std::mem::transmute(func_address)
}
};

#[cfg(all(not(windows), not(debug_assertions)))]
let request_context = {
let c_name = std::ffi::CString::new("RVExtensionRequestContextProc").expect("CString::new failed");

let handle = unsafe { libc::dlopen(std::ptr::null_mut(), libc::RTLD_LAZY | libc::RTLD_NOLOAD) };

if handle.is_null() {
panic!("Failed to open handle to current process");
}

let result = unsafe { libc::dlsym(handle, c_name.as_ptr()) };

unsafe { libc::dlclose(handle) };

if result.is_null() {
panic!("Failed to get function address");
}

unsafe {
std::mem::transmute::<*mut libc::c_void, unsafe extern "C" fn()>(result)
}
};

#[cfg(debug_assertions)]
let request_context = empty_request_context;

Extension {
version: self.version,
group: self.group.into(),
allow_no_args: self.allow_no_args,
callback: None,
callback_channel: unbounded(),
callback_thread: None,
context_manager: Rc::new(ArmaContextManager::new(empty_request_context)),
context_manager: Rc::new(ArmaContextManager::new(request_context)),
}
}
}
Expand Down

0 comments on commit b91a608

Please sign in to comment.