diff --git a/src/bindings/squirrelfunctions.rs b/src/bindings/squirrelfunctions.rs index cda0a78..5b22c42 100644 --- a/src/bindings/squirrelfunctions.rs +++ b/src/bindings/squirrelfunctions.rs @@ -148,8 +148,8 @@ pub struct SquirrelFunctions { pub sq_sealstructslot: sq_sealstructslotType, } -impl From for SquirrelFunctions { - fn from(val: ClientSQFunctions) -> Self { +impl From<&ClientSQFunctions> for SquirrelFunctions { + fn from(val: &ClientSQFunctions) -> Self { SquirrelFunctions { register_squirrel_func: val.register_squirrel_func, sq_defconst: val.sq_defconst, @@ -191,8 +191,8 @@ impl From for SquirrelFunctions { } } -impl From for SquirrelFunctions { - fn from(val: ServerSQFunctions) -> Self { +impl From<&ServerSQFunctions> for SquirrelFunctions { + fn from(val: &ServerSQFunctions) -> Self { SquirrelFunctions { register_squirrel_func: val.register_squirrel_func, sq_defconst: val.sq_defconst, diff --git a/src/macros/entry.rs b/src/macros/entry.rs index 40fc71a..dde33b2 100644 --- a/src/macros/entry.rs +++ b/src/macros/entry.rs @@ -118,6 +118,8 @@ macro_rules! entry { false // TODO: add this to Plugin } fn OnSqvmCreated(&self, sqvm: *mut squirreldatatypes::CSquirrelVM) { + _ = mid::squirrel::SQFUNCTIONS.try_init(); + let context: squirrelclasstypes::ScriptContext = unsafe { (*sqvm).vmContext } .try_into() .expect("sqvm was not valid :(((("); @@ -230,6 +232,7 @@ macro_rules! entry { &mid::concommands::REGISTER_CONCOMNMADS, ); } + mid::squirrel::SQFUNCTIONS.fetch_functions(&dll_ptr); let engine_data = if dll_string == "engine.dll" { unsafe { diff --git a/src/mid/squirrel.rs b/src/mid/squirrel.rs index b20e2b9..3aeb4a0 100644 --- a/src/mid/squirrel.rs +++ b/src/mid/squirrel.rs @@ -14,7 +14,10 @@ use crate::{ bindings::{ squirrelclasstypes::{SQFunction, ScriptContext}, squirreldatatypes::{CSquirrelVM, HSquirrelVM, SQClosure, SQObject}, - squirrelfunctions::SquirrelFunctions, + squirrelfunctions::{ + ClientSQFunctions, ServerSQFunctions, SquirrelFunctions, SQUIRREL_CLIENT_FUNCS, + SQUIRREL_SERVER_FUNCS, + }, }, errors::CallError, high::{ @@ -23,6 +26,7 @@ use crate::{ squirrel_traits::{GetFromSQObject, PushToSquirrelVm}, vector::Vector3, }, + prelude::DLLPointer, }; use super::utils::{to_cstring, try_cstring}; @@ -54,6 +58,20 @@ pub struct SqFunctions { } impl SqFunctions { + #[doc(hidden)] + pub fn fetch_functions(&self, dll: &DLLPointer) { + unsafe { ClientSQFunctions::try_init(dll, &SQUIRREL_CLIENT_FUNCS) }; + unsafe { ServerSQFunctions::try_init(dll, &SQUIRREL_SERVER_FUNCS) }; + } + + #[doc(hidden)] + pub fn try_init(&self) -> Option<()> { + self.client.set(SQUIRREL_CLIENT_FUNCS.wait().into()).ok()?; + self.server.set(SQUIRREL_SERVER_FUNCS.wait().into()).ok()?; + + None + } + pub fn from_sqvm(&'static self, sqvm: *mut HSquirrelVM) -> &'static SquirrelFunctions { self.from_cssqvm(unsafe { (*(*sqvm).sharedState).cSquirrelVM }) }