Skip to content

Commit

Permalink
fixes + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Feb 8, 2024
1 parent 22a619a commit 1a4b6bd
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/bindings/squirrelfunctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ offset_functions! {
sq_get_entity_constant_cbase_entity = sq_GetEntityConstantType where offset(0x3E49B0);
sq_getentityfrominstance = sq_getentityfrominstanceType where offset(0x114F0);

sq_getfunction = sq_getfunctionType where offset(0x6CB0);
sq_getfunction = sq_getfunctionType where offset(0x572FB0);
sq_stackinfos = sq_stackinfosType where offset(0x35970);

sq_pushnewstructinstance = sq_pushnewstructinstanceType where offset(0x5400);
Expand Down
18 changes: 14 additions & 4 deletions src/high/squirrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ impl SQHandle<SQClosure> {
/// Adds a sqfunction to the registration list
///
/// The sqfunction will be registered when its vm is loaded
///
/// # Example
/// ```
/// # use rrplug::prelude::*;
/// # use rrplug::high::squirrel::call_sq_function;
/// # use rrplug::bindings::class_types::cplayer::CPlayer;
/// #[rrplug::sqfunction(VM="Server")]
/// fn simple_example(name: String, player: Option<&mut CPlayer>) -> Result<(),String> {
/// let _player = player.ok_or("Not a Player!".to_string())?;
///
/// log::info!("the name is {name}");
/// Ok(())
/// }
/// ```
pub fn register_sq_functions(get_info_func: FuncSQFuncInfo) {
FUNCTION_SQ_REGISTER.lock().push(get_info_func());
}
Expand All @@ -193,7 +207,6 @@ pub fn register_sq_functions(get_info_func: FuncSQFuncInfo) {
/// # use rrplug::prelude::*;
/// # use rrplug::high::squirrel::call_sq_function;
/// # use rrplug::{high::squirrel::SQHandle,bindings::squirreldatatypes::SQClosure};
///
/// #[rrplug::sqfunction(VM="Server")]
/// fn test_call_sq_object_function() -> Result<(),String> {
/// call_sq_function(sqvm, sq_functions, "someFunction").map_err(|err| err.to_string())?;
Expand Down Expand Up @@ -244,7 +257,6 @@ pub fn call_sq_function(
/// # use rrplug::prelude::*;
/// # use rrplug::high::squirrel::call_sq_object_function;
/// # use rrplug::{high::squirrel::SQHandle,bindings::squirreldatatypes::SQClosure};
///
/// #[rrplug::sqfunction(VM="Server")]
/// fn call_sqvm_function(mut func: SQHandle<SQClosure>) -> Result<(),String>{
/// call_sq_object_function(sqvm, sq_functions, func).map_err(|err| err.to_string())?;
Expand Down Expand Up @@ -290,14 +302,12 @@ fn _call_sq_object_function(
/// ```
/// # use rrplug::prelude::*;
/// # use rrplug::high::squirrel::compile_string;
///
/// #[rrplug::sqfunction(VM="Server")]
/// fn compile_string_test() -> Result<(),String> {
/// compile_string(sqvm, sq_functions, true, "print(\"helloworld\")").map_err(|err| err.to_string())?;
///
/// Ok(())
/// }
///
/// ```
pub fn compile_string(
sqvm: *mut HSquirrelVM,
Expand Down
41 changes: 32 additions & 9 deletions src/high/squirrel_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ use crate::{
squirrelfunctions::SquirrelFunctions,
},
call_sq_object_function,
mid::squirrel::{
get_sq_array, get_sq_bool, get_sq_float, get_sq_int, get_sq_object, get_sq_string,
get_sq_vector, push_sq_array, push_sq_bool, push_sq_float, push_sq_int, push_sq_object,
push_sq_string, push_sq_vector,
mid::{
server::cplayer::CPLAYER_VTABLE,
squirrel::{
get_sq_array, get_sq_bool, get_sq_float, get_sq_int, get_sq_object, get_sq_string,
get_sq_vector, push_sq_array, push_sq_bool, push_sq_float, push_sq_int, push_sq_object,
push_sq_string, push_sq_vector, sqvm_to_context,
},
},
prelude::ScriptContext,
};

// Push Trait
Expand Down Expand Up @@ -218,13 +222,20 @@ where
}
}

impl GetFromSquirrelVm for &mut CPlayer {
impl GetFromSquirrelVm for Option<&mut CPlayer> {
fn get_from_sqvm(
sqvm: *mut HSquirrelVM,
sqfunctions: &SquirrelFunctions,
stack_pos: i32,
) -> Self {
unsafe {
#[cfg(debug_assertions)]
assert_eq!(
sqvm_to_context(sqvm),
ScriptContext::SERVER,
"CPlayer only exists on server vm use C_Player for CLIENT and UI"
);

let sqvm = sqvm.as_mut().expect("the sqvm was invalid");
let cs_sqvm = sqvm
.sharedState
Expand All @@ -235,13 +246,25 @@ impl GetFromSquirrelVm for &mut CPlayer {
let mut obj = MaybeUninit::<SQObject>::uninit();
(sqfunctions.sq_getobject)(sqvm, stack_pos, obj.as_mut_ptr());

(sqfunctions.sq_getentityfrominstance)(
let ent = (sqfunctions.sq_getentityfrominstance)(
cs_sqvm,
obj.as_mut_ptr(),
(sqfunctions.sq_get_entity_constant_cbase_entity)(),
)
.as_mut()
.expect("entity was supposed to be valid")
.cast::<CPlayer>()
.as_mut()?;

if ent.vtable.copy_inner()
!= CPLAYER_VTABLE
.get()
.expect("CPlayer vtable is missing wtf?")
.vtable
.cast::<usize>()
{
return None;
}

Some(ent)
}
}
}
Expand Down Expand Up @@ -401,7 +424,7 @@ sqvm_name! {
f32 = "float";
bool = "bool";
Vector3 = "vector";
&mut CPlayer = "entity";
Option<&mut CPlayer> = "entity";
SQHandle<SQClosure> = "var";
() = "void";
}
Expand Down
4 changes: 4 additions & 0 deletions src/macros/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ macro_rules! entry {
&dll_ptr,
&mid::engine::concommands::REGISTER_CONCOMNMADS,
);
mid::server::cplayer::CPlayerVtable::try_init(
&dll_ptr,
&mid::server::cplayer::CPLAYER_VTABLE,
);
}
mid::squirrel::SQFUNCTIONS.fetch_functions(&dll_ptr);

Expand Down
10 changes: 10 additions & 0 deletions src/mid/squirrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ impl SQFunctionContext {
}
}

/// returns the context of the sqvm
///
/// # Safety
/// assumes the sqvm is valid and does a bunch of derefs
#[inline]
pub unsafe fn sqvm_to_context(sqvm: *mut HSquirrelVM) -> ScriptContext {
ScriptContext::try_from(unsafe { (*(*(*sqvm).sharedState).cSquirrelVM).vmContext })
.expect("sqvm should have a valid vmcontext")
}

/// pushes a `Vec<T>` to the sqvm
#[inline]
pub fn push_sq_array<T>(sqvm: *mut HSquirrelVM, sqfunctions: &SquirrelFunctions, arguments: Vec<T>)
Expand Down
2 changes: 0 additions & 2 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub trait Plugin: Any + Sync {
const PLUGIN_INFO: PluginInfo;

/// init function
///
/// TODO: redo docs for this about registering sq functions
fn new(reloaded: bool) -> Self;

/// called when a dll is loaded with winapi functions by the game (full paths are not provided)
Expand Down

0 comments on commit 1a4b6bd

Please sign in to comment.