Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
catornot committed Mar 31, 2024
1 parent 9225a49 commit c106e69
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ targets = ["x86_64-pc-windows-msvc", "x86_64-pc-windows-gnu"] # hopefully docs.r
[features]
# default = ["async_engine"]
async_engine = []

[[example]]
crate-type = ["cdylib"]
name = "squirrel_example"
path = "examples/squirrel_example.rs"

[[example]]
crate-type = ["cdylib"]
name = "cvar_example"
path = "examples/cvar_example.rs"

[[example]]
crate-type = ["cdylib"]
name = "async_engine"
path = "examples/async_engine.rs"
required-features = ["async_engine"]
46 changes: 46 additions & 0 deletions examples/async_engine.rs
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
}
73 changes: 73 additions & 0 deletions examples/cvar_example.rs
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)
);
}
50 changes: 50 additions & 0 deletions examples/squirrel_example.rs
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>()
}
3 changes: 3 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub use crate::{
};
pub use log;

#[cfg(feature = "async_engine")]
pub use crate::high::engine_sync::{async_execute, AsyncEngineMessage};

// consider adding more stuff ^

/// puts a thread on sleep in milliseconds
Expand Down

0 comments on commit c106e69

Please sign in to comment.