Skip to content

Commit

Permalink
load squirrel functions
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Jan 19, 2024
1 parent 3f1252e commit caaa12d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/bindings/squirrelfunctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ pub struct SquirrelFunctions {
pub sq_sealstructslot: sq_sealstructslotType,
}

impl From<ClientSQFunctions> 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,
Expand Down Expand Up @@ -191,8 +191,8 @@ impl From<ClientSQFunctions> for SquirrelFunctions {
}
}

impl From<ServerSQFunctions> 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,
Expand Down
3 changes: 3 additions & 0 deletions src/macros/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 :((((");
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion src/mid/squirrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -23,6 +26,7 @@ use crate::{
squirrel_traits::{GetFromSQObject, PushToSquirrelVm},
vector::Vector3,
},
prelude::DLLPointer,
};

use super::utils::{to_cstring, try_cstring};
Expand Down Expand Up @@ -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 })
}
Expand Down

0 comments on commit caaa12d

Please sign in to comment.