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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
pub const SDK_STRING_MAX_LEN: usize = 128;
#[allow(non_camel_case_types)]
#[repr(C)]
pub enum HandlerResult {
CONTINUE = 0,
TERMINATE = 1,
ERR = -1,
}
#[allow(non_camel_case_types)]
#[repr(C)]
pub enum HostId {
RE3 = 1,
REVC = 2,
GTA3 = 3,
VC = 4,
SA = 5,
GTA3_UNREAL = 6,
VC_UNREAL = 7,
SA_UNREAL = 8,
}
#[allow(non_camel_case_types)]
type c_char = i8;
pub type Context = *const std::ffi::c_void;
pub type CustomCommand = extern "C" fn(Context) -> HandlerResult;
#[cfg_attr(target_arch = "x86", link(name = "cleo_redux"))]
#[cfg_attr(target_arch = "x86_64", link(name = "cleo_redux64"))]
extern "C" {
fn GetSDKVersion() -> i32;
fn GetHostId() -> HostId;
fn ResolvePath(src: *const c_char, dest: *mut c_char);
fn GetCLEOFolder(dest: *mut c_char);
fn GetCwd(dest: *mut c_char);
fn Log(text: *const c_char);
fn RegisterCommand(name: *const c_char, cb: CustomCommand, permission: *const c_char);
fn GetIntParam(ctx: Context) -> isize;
fn GetFloatParam(ctx: Context) -> f32;
fn GetStringParam(ctx: Context, dest: *mut c_char, maxlen: u8);
fn SetIntParam(ctx: Context, value: isize);
fn SetFloatParam(ctx: Context, value: f32);
fn SetStringParam(ctx: Context, src: *const c_char);
fn UpdateCompareFlag(ctx: Context, result: bool);
}
macro_rules! sz {
($name:expr) => {
std::ffi::CString::new($name).unwrap().as_ptr()
};
}
#[allow(dead_code)]
pub fn register_command(name: &str, cb: CustomCommand, permission: Option<&str>) {
unsafe {
match permission {
Some(token) => RegisterCommand(sz!(name), cb, sz!(token)),
None => RegisterCommand(sz!(name), cb, std::ptr::null()),
};
}
}
pub fn log<T: Into<Vec<u8>>>(text: T) {
unsafe { Log(sz!(text)) };
}
pub fn get_sdk_version() -> i32 {
unsafe { GetSDKVersion() }
}
pub fn get_host_id() -> HostId {
unsafe { GetHostId() }
}
pub fn get_string_param(ctx: Context) -> String {
let mut buf = [0i8; SDK_STRING_MAX_LEN];
unsafe { GetStringParam(ctx, buf.as_mut_ptr(), SDK_STRING_MAX_LEN as _) };
to_rust_string(buf.as_ptr())
}
pub fn set_string_param(ctx: Context, value: String) {
unsafe { SetStringParam(ctx, sz!(value)) };
}
pub fn get_int_param(ctx: Context) -> isize {
unsafe { GetIntParam(ctx) }
}
pub fn set_int_param(ctx: Context, value: isize) {
unsafe { SetIntParam(ctx, value) };
}
pub fn get_cleo_folder() -> std::path::PathBuf {
let mut buf = [0i8; 256];
unsafe { GetCLEOFolder(buf.as_mut_ptr()) };
std::path::Path::new(&to_rust_string(buf.as_ptr())).into()
}
pub fn get_cwd() -> std::path::PathBuf {
let mut buf = [0i8; 256];
unsafe { GetCwd(buf.as_mut_ptr()) };
std::path::Path::new(&to_rust_string(buf.as_ptr())).into()
}
pub fn resolve_path(path: &str) -> std::path::PathBuf {
let mut dest = [0i8; 256];
unsafe { ResolvePath(sz!(path), dest.as_mut_ptr()) };
std::path::Path::new(&to_rust_string(dest.as_ptr())).into()
}
pub fn get_float_param(ctx: Context) -> f32 {
unsafe { GetFloatParam(ctx) }
}
pub fn set_float_param(ctx: Context, value: f32) {
unsafe { SetFloatParam(ctx, value) };
}
pub fn update_compare_flag(ctx: Context, value: bool) {
unsafe { UpdateCompareFlag(ctx, value) }
}
fn to_rust_string(addr: *const i8) -> String {
unsafe {
std::ffi::CStr::from_ptr(addr)
.to_owned()
.into_string()
.unwrap_or_default()
}
}