-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use rrplug::{bindings::cvar::convar::FCVAR_GAMEDLL, prelude::*}; | ||
|
||
pub struct ExamplePlugin; | ||
|
||
impl Plugin for ExamplePlugin { | ||
const PLUGIN_INFO: PluginInfo = | ||
PluginInfo::new(c"example", c"EXAMPLLEE", c"EXAMPLE", PluginContext::all()); | ||
|
||
fn new(_reloaded: bool) -> Self { | ||
Self {} | ||
} | ||
|
||
fn on_dll_load( | ||
&self, | ||
engine_data: Option<&EngineData>, | ||
_dll_ptr: &DLLPointer, | ||
engine_token: EngineToken, | ||
) { | ||
let Some(engine_data) = engine_data else { | ||
return; | ||
}; | ||
|
||
engine_data | ||
.register_concommand( | ||
"set_max_score", | ||
set_max_score, | ||
"this will set the game's max score", | ||
FCVAR_GAMEDLL as i32, | ||
engine_token, | ||
) | ||
.expect("could not create set_max_score concommand"); | ||
} | ||
} | ||
|
||
entry!(ExamplePlugin); | ||
|
||
#[rrplug::concommand] | ||
fn set_max_score(command: CCommandResult) -> Option<()> { | ||
_ = async_execute(AsyncEngineMessage::run_squirrel_func( | ||
"SetScoreLimit", | ||
ScriptContext::SERVER, | ||
command.get_arg(0)?.parse::<i32>().ok()?, | ||
)); | ||
|
||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use rrplug::{bindings::cvar::convar::FCVAR_GAMEDLL, prelude::*}; | ||
use std::cell::RefCell; | ||
|
||
static HELLO_COUNT: EngineGlobal<RefCell<Option<ConVarStruct>>> = | ||
EngineGlobal::new(RefCell::new(None)); | ||
|
||
pub struct ExamplePlugin; | ||
|
||
impl Plugin for ExamplePlugin { | ||
const PLUGIN_INFO: PluginInfo = | ||
PluginInfo::new(c"example", c"EXAMPLLEE", c"EXAMPLE", PluginContext::all()); | ||
|
||
fn new(_reloaded: bool) -> Self { | ||
Self {} | ||
} | ||
|
||
fn on_dll_load( | ||
&self, | ||
engine_data: Option<&EngineData>, | ||
_dll_ptr: &DLLPointer, | ||
engine_token: EngineToken, | ||
) { | ||
let Some(engine_data) = engine_data else { | ||
return; | ||
}; | ||
|
||
engine_data | ||
.register_concommand( | ||
"hello_world", | ||
hello_world, | ||
"this will set the game's max score", | ||
FCVAR_GAMEDLL as i32, | ||
engine_token, | ||
) | ||
.expect("could not create set_max_score concommand"); | ||
|
||
let convar = ConVarStruct::try_new( | ||
&ConVarRegister { | ||
callback: Some(hello_count), // prints the amount of hellos on each update | ||
..ConVarRegister::mandatory("hello_count", "0", FCVAR_GAMEDLL as i32, "todo") | ||
}, | ||
engine_token, | ||
) | ||
.expect("could not create hello_count convar"); | ||
|
||
_ = HELLO_COUNT.get(engine_token).borrow_mut().replace(convar); | ||
} | ||
} | ||
|
||
entry!(ExamplePlugin); | ||
|
||
#[rrplug::concommand] | ||
fn hello_world() { | ||
log::info!("hello world"); | ||
|
||
let mut convar = HELLO_COUNT.get(engine_token).borrow_mut(); | ||
if let Some(convar) = convar.as_mut() { | ||
convar.set_value_i32(convar.get_value_i32() + 1, engine_token); | ||
} | ||
} | ||
|
||
#[rrplug::convar] | ||
fn hello_count() { | ||
log::info!( | ||
"hellos {}", | ||
HELLO_COUNT | ||
.get(engine_token) | ||
.borrow() | ||
.as_ref() | ||
.map(|convar| convar.get_value_i32()) | ||
.unwrap_or(0) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use rrplug::{ | ||
bindings::squirreldatatypes::SQClosure, | ||
high::squirrel::{call_sq_object_function, SQHandle}, | ||
prelude::*, | ||
}; | ||
|
||
pub struct ExamplePlugin; | ||
|
||
impl Plugin for ExamplePlugin { | ||
const PLUGIN_INFO: PluginInfo = | ||
PluginInfo::new(c"example", c"EXAMPLLEE", c"EXAMPLE", PluginContext::all()); | ||
|
||
fn new(_reloaded: bool) -> Self { | ||
register_sq_functions(great_person); | ||
register_sq_functions(call_with_random_number); | ||
register_sq_functions(sum); | ||
|
||
Self {} | ||
} | ||
} | ||
|
||
entry!(ExamplePlugin); | ||
|
||
// if it returns an error the function will throw a error in the sqvm which can be caught at the call site | ||
#[rrplug::sqfunction(VM = "SERVER", ExportName = "GreatPerson")] | ||
fn great_person(function: SQHandle<SQClosure>) -> Result<String, rrplug::errors::CallError> { | ||
// non type safe way of getting a return from a function | ||
// this could be changed if the crate gets some attention | ||
let name = call_sq_object_function(sqvm, sq_functions, function)?; | ||
|
||
log::info!("hello, {}", name); | ||
|
||
Ok(name) | ||
} | ||
|
||
#[rrplug::sqfunction(VM = "SERVER", ExportName = "CallWithRandomNumber")] | ||
fn call_with_random_number(mut function: SquirrelFn<(i32, String)>) { | ||
const TOTALY_RANDOM_NUMBER: i32 = 37; | ||
|
||
_ = function.call( | ||
sqvm, | ||
sq_functions, | ||
(TOTALY_RANDOM_NUMBER, TOTALY_RANDOM_NUMBER.to_string()), | ||
); | ||
} | ||
|
||
#[rrplug::sqfunction(VM = "SERVER", ExportName = "Sum")] | ||
fn sum(vec: Vector3, numbers: Vec<f32>) -> f32 { | ||
vec.x + vec.y + vec.z + numbers.into_iter().sum::<f32>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters