use core::ffi::c_void;
use core::ptr;
use std::panic;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, Ordering};
use crate::{Config, Unload, UnloadReason, WindowHandle, host, runtime, sys, worker};
static LOADED: AtomicBool = AtomicBool::new(false);
const CONFIG_EXPORT: &core::ffi::CStr = c"__mirust_config";
#[unsafe(no_mangle)]
unsafe extern "system" fn LoadDll(info: *mut sys::LoadInfo) {
LOADED.store(true, Ordering::SeqCst);
let config = config();
if config.pin_module {
worker::pin_module();
}
let host = unsafe { host::init(info, config.keep_loaded, config.unicode) };
worker::loaded(host.main_window(), config.exit_grace);
if let Some(hook) = config.on_load {
let _ = panic::catch_unwind(|| hook(host));
}
}
#[unsafe(no_mangle)]
extern "system" fn UnloadDll(raw_reason: i32) -> i32 {
unload(UnloadReason::from_raw(raw_reason))
}
fn unload(reason: UnloadReason) -> i32 {
let config = config();
let decision = match config.on_unload {
Some(hook) => panic::catch_unwind(|| hook(reason)).unwrap_or(Unload::Allow),
None if config.keep_loaded => Unload::Keep,
None => Unload::Allow,
};
if let (UnloadReason::Idle, Unload::Keep) = (reason, decision) {
return 0;
}
worker::unloading(reason == UnloadReason::Exit);
1
}
pub(crate) fn begin_legacy_call(main_window: WindowHandle) -> Option<LegacyCall> {
if LOADED.load(Ordering::SeqCst) {
return None;
}
let config = config();
if config.pin_module {
worker::pin_module();
}
let host = host::init_legacy(main_window);
worker::loaded(host.main_window(), config.exit_grace);
if let Some(hook) = config.on_load {
let _ = panic::catch_unwind(|| hook(host));
}
Some(LegacyCall)
}
pub(crate) struct LegacyCall;
impl Drop for LegacyCall {
fn drop(&mut self) {
unload(UnloadReason::Manual);
}
}
#[unsafe(no_mangle)]
unsafe extern "system" fn __mirust_deliver(
main_window: *mut c_void,
active_window: *mut c_void,
data: *mut c_void,
parms: *mut c_void,
show: i32,
no_pause: i32,
) -> i32 {
unsafe {
runtime::dispatch(
main_window,
active_window,
data,
parms,
show,
no_pause,
runtime::deliver,
)
}
}
#[unsafe(no_mangle)]
unsafe extern "system" fn __mirust_run(
main_window: *mut c_void,
active_window: *mut c_void,
data: *mut c_void,
parms: *mut c_void,
show: i32,
no_pause: i32,
) -> i32 {
unsafe {
runtime::dispatch(
main_window,
active_window,
data,
parms,
show,
no_pause,
runtime::run_next,
)
}
}
fn config() -> &'static Config {
static DEFAULT: Config = Config::new();
static RESOLVED: OnceLock<&'static Config> = OnceLock::new();
RESOLVED.get_or_init(|| find_user_config().unwrap_or(&DEFAULT))
}
fn find_user_config() -> Option<&'static Config> {
let module = own_module()?;
let export = unsafe { sys::GetProcAddress(module, CONFIG_EXPORT.as_ptr().cast()) };
if export.is_null() {
return None;
}
let export: runtime::ExportFn = unsafe { core::mem::transmute(export) };
let mut config: *const Config = ptr::null();
let answer = unsafe {
export(
ptr::null_mut(),
ptr::null_mut(),
(&raw mut config).cast(),
ptr::null_mut(),
runtime::CONFIG_MARKER,
0,
)
};
(answer == runtime::CONFIG_MARKER && !config.is_null()).then(|| unsafe { &*config })
}
pub(crate) fn own_module() -> Option<*mut c_void> {
let mut module = ptr::null_mut();
let found = unsafe {
sys::GetModuleHandleExW(
sys::GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
| sys::GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
own_module as *const u16,
&mut module,
)
};
(found != 0).then_some(module)
}