-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcvar_example.rs
73 lines (61 loc) · 1.94 KB
/
cvar_example.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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)
);
}