1#[cfg(target_os = "windows")]
2use windows::{
3 core::{PCSTR, PCWSTR},
4 Win32::System::LibraryLoader::{GetModuleHandleW, GetProcAddress},
5};
6
7#[cfg(target_os = "linux")]
8use std::os::raw;
9
10#[cfg(target_os = "linux")]
11#[link(name = "dl")]
12extern "C" {
13 fn dlsym(handle: *mut raw::c_void, symbol: *const raw::c_char) -> *mut raw::c_void;
14}
15
16macro_rules! load_function {
17 ($name: expr) => {
18 paste::paste! {
19 let prefixed = stringify!($name);
20 let address = crate::helper::get_module_symbol_address("$CAPI", &prefixed)
21 .expect(&format!("could not find '{prefixed}' address"));
22 unsafe {
23 [<OMPRS_ $name>] = Some(std::mem::transmute(address));
24 }
25 }
26 };
27}
28
29#[cfg(target_os = "windows")]
30pub fn get_module_symbol_address(module: &str, symbol: &str) -> Option<usize> {
31 use std::{ffi::CString, iter};
32
33 let module = module
34 .encode_utf16()
35 .chain(iter::once(0))
36 .collect::<Vec<u16>>();
37 let symbol = CString::new(symbol).unwrap();
38
39 unsafe {
40 let handle = GetModuleHandleW(PCWSTR(module.as_ptr() as _)).unwrap();
41 GetProcAddress(handle, PCSTR(symbol.as_ptr() as _)).map(|func| func as usize)
42 }
43}
44
45#[cfg(target_os = "linux")]
46pub fn get_module_symbol_address(_module: &str, symbol: &str) -> Option<usize> {
47 use std::ffi::CString;
48 let symbol = CString::new(symbol).unwrap();
49
50 unsafe { Some(dlsym(std::ptr::null_mut(), symbol.as_ptr()) as usize) }
51}
52
53macro_rules! add_handler {
54 ($name:expr) => {
55 paste::paste! {
56 let cstring = std::ffi::CString::new(stringify!($name)).unwrap();
57
58 OMPRS_Event_AddHandler.unwrap()(
59 cstring.as_ptr(),
60 0,
61 [<OMPRS_ $name:camel>] as *const std::ffi::c_void,
62 );
63 }
64 };
65}
66
67pub fn gen_uid() -> u64 {
68 uuid::Uuid::new_v4().as_u64_pair().0
69}