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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
pub const SDK_STRING_MAX_LEN: usize = 128;

#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum HandlerResult {
    /// Proceed to the next command
    CONTINUE = 0,
    /// Pause the script and continue on the next game loop iteration
    BREAK = 1,
    /// End the script gracefully
    TERMINATE = 2,
    /// End the script and throw an error
    ERR = -1,
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum HostId {
    RE3 = 1,
    REVC = 2,
    GTA3 = 3,
    VC = 4,
    SA = 5,
    GTA3_UNREAL = 6,
    VC_UNREAL = 7,
    SA_UNREAL = 8,
    IV = 9,
    BULLY = 10,
    MANIFEST = 254,
    UNKNOWN = 255,
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub enum Directory {
    /// /CLEO directory
    CLEO = 0,
    /// /CLEO/.config
    CONFIG = 1,
    /// /CLEO/CLEO_TEXT
    TEXT = 2,
    /// /CLEO/CLEO_PLUGINS
    PLUGINS = 3,
    /// Current working directory
    CWD = 4,
    /// Host root directory
    HOST = 5,
}

#[allow(non_camel_case_types)]
pub type c_char = i8;
#[allow(non_camel_case_types)]
pub type c_void = std::ffi::c_void;

pub type Context = *const c_void;
pub type CustomCommand = extern "C" fn(Context) -> HandlerResult;
pub type CustomLoader = extern "C" fn(*const c_char) -> *mut c_void;
pub type OnTickCallback = extern "C" fn(current_time: u32, time_step: i32);
pub type OnRuntimeInitCallback = extern "C" fn();
pub type OnShowTextBoxCallback = extern "C" fn(*const c_char);

#[cfg_attr(target_arch = "x86", link(name = "cleo_redux"))]
#[cfg_attr(target_arch = "x86_64", link(name = "cleo_redux64"))]
extern "C" {
    /// Returns the current SDK version as an integer number.
    ///
    /// since v1
    fn GetSDKVersion() -> i32;
    /// Returns the current host (game) id
    ///
    /// since v1
    fn GetHostId() -> HostId;
    /// Resolves a path to the absolute path
    ///     - absolute path gets resolved as is
    /// - path starting with "CLEO/" gets resolved relative to the CLEO directory
    ///    * either {game}\CLEO or
    ///    * {user}\AppData\Roaming\CLEO Redux\CLEO
    ///  - all other paths get resolved relative to the cwd (game directory)
    ///
    /// since v1
    fn ResolvePath(src: *const c_char, dest: *mut c_char);
    /// Returns the absolute path to the CLEO directory
    ///
    /// since v1
    /// deprecated: use GetDirectoryPath
    fn GetCLEOFolder(dest: *mut c_char);
    /// Returns the absolute path to the current working directory (normally the game directory)
    ///
    /// since v1
    fn GetCwd(dest: *mut c_char);
    /// Prints a new entry to the cleo_redux.log
    ///
    /// since v1
    fn Log(text: *const c_char);
    /// Registers a new callback handler for the command with the given name. Permission token is required for unsafe operations interacting with the user environment (e.g. mem, fs, net)
    ///
    /// since v1
    fn RegisterCommand(name: *const c_char, cb: CustomCommand, permission: *const c_char);
    /// Reads an integer argument (either 32 or 64 bit depending on the target platform) from the script input
    ///
    /// since v1
    fn GetIntParam(ctx: Context) -> isize;
    /// Reads a floating-point argument from the script input
    ///
    /// since v1
    fn GetFloatParam(ctx: Context) -> f32;
    /// Copies atmost {maxlen} bytes of a UTF-8 encoded character sequence in the script input to {dest}
    ///
    /// since v1
    fn GetStringParam(ctx: Context, dest: *mut c_char, maxlen: u8);
    /// Writes the integer {value} (either 32 or 64 bit depending on the target platform) to the script output
    ///
    /// since v1
    fn SetIntParam(ctx: Context, value: isize);
    /// Writes the floating-point {value} to the script output
    ///
    /// since v1
    fn SetFloatParam(ctx: Context, value: f32);
    /// Copies a null-terminated UTF-8 encoded character sequence from {src} to the script output
    ///
    /// since v1
    fn SetStringParam(ctx: Context, src: *const c_char);
    /// Sets the status of the current condition
    ///
    /// since v1
    fn UpdateCompareFlag(ctx: Context, result: bool);
    /// Copies atmost {maxlen} bytes of a UTF-8 encoded host name to {dest}
    ///
    /// since v2
    fn GetHostName(dest: *mut c_char, maxlen: u8);
    /// Sets the new host name (available in scripts as the HOST constant)
    ///
    /// since v2
    fn SetHostName(src: *const c_char);
    /// Initializes or reloads CLEO runtime
    ///
    /// since v2
    fn RuntimeInit();
    /// Iterates the main loop
    ///
    /// since v2
    fn RuntimeNextTick(current_time: u32, time_step: i32);
    /// Registers a new loader for files matching a glob pattern
    ///
    /// since v3
    fn RegisterLoader(glob: *const c_char, cb: CustomLoader);
    /// Allocates a memory chunk with size in bytes. Memory is guaranteed to be zero initialized
    ///
    /// since v3
    fn AllocMem(size: usize) -> *mut c_void;
    /// Frees up the memory chunk allocated with AllocMem
    ///
    /// since v3
    fn FreeMem(ptr: *mut c_void);
    /// Registers a new callback invoked on each main loop iteration (before scripts are executed)
    ///
    /// since v4
    fn OnBeforeScripts(cb: OnTickCallback);
    /// Registers a new callback invoked on each main loop iteration (after scripts are executed)
    ///
    /// since v4
    fn OnAfterScripts(cb: OnTickCallback);
    /// Registers a new callback invoked on each runtime init event (new game, saved game load, or SDK's RuntimeInit)
    ///
    /// since v4
    fn OnRuntimeInit(cb: OnRuntimeInitCallback);
    /// Registers a new callback invoked on a ShowTextBox function call. Providing a callback shadows built-in ShowTextBox implementation.
    ///
    /// since v5
    fn OnShowTextBox(cb: OnShowTextBoxCallback);
    /// Returns the absolute path to the CLEO root directory or one of its sub-directories
    ///
    /// since v6
    fn GetDirectoryPath(dir: Directory, dest: *mut c_char);
    /// Returns CLEO Redux version as a string
    ///
    /// since v6
    fn GetCLEOVersion(dest: *mut c_char);
    /// Returns a memory address for the given symbol, or 0 if not found
    ///
    /// since v6
    fn GetSymbolAddress(symbol: *const c_char) -> usize;
    /// Returns number of active CS scripts
    ///
    /// since v6
    fn GetNumberOfActiveCSScripts() -> usize;
    /// Returns number of active JS scripts
    ///
    /// since v6
    fn GetNumberOfActiveJSScripts() -> usize;
    /// Returns true if end of arguments reached
    ///
    /// since v6
    fn IsEndOfArguments(ctx: Context) -> bool;
    /// Trigger an event with the given name and data
    ///
    /// since v7
    fn TriggerEvent(event: *const c_char, data: *const c_char);
}

macro_rules! sz {
    ($name:expr) => {
        std::ffi::CString::new($name).unwrap().as_ptr()
    };
}

/// Registers a new callback handler for the command with the given name. Permission token is required for unsafe operations interacting with the user environment (e.g. mem, fs, net)
///
/// since v1
#[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()),
        };
    }
}

/// Prints a new entry to the cleo_redux.log
///
/// since v1
pub fn log<T: Into<Vec<u8>>>(text: T) {
    unsafe { Log(sz!(text)) };
}

/// Returns the current SDK version as an integer number.
///
/// since v1
pub fn get_sdk_version() -> i32 {
    unsafe { GetSDKVersion() }
}

/// Returns the current host (game) id
///
/// since v1
pub fn get_host_id() -> HostId {
    unsafe { GetHostId() }
}

/// Reads a string argument
///
/// since v1
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())
}

/// Writes the string value
///
/// since v1
pub fn set_string_param(ctx: Context, value: String) {
    unsafe { SetStringParam(ctx, sz!(value)) };
}

/// Reads an integer argument (32 or 64 bit depending on target platform)
///
/// since v1
pub fn get_int_param(ctx: Context) -> isize {
    unsafe { GetIntParam(ctx) }
}

/// Writes the integer value (32 or 64 bit depending on target platform)
///
/// since v1
pub fn set_int_param(ctx: Context, value: isize) {
    unsafe { SetIntParam(ctx, value) };
}

/// Returns the absolute path to the CLEO directory
///
/// since v1
///
/// deprecated: use get_directory_path()
pub fn get_cleo_folder() -> std::path::PathBuf {
    get_directory_path(Directory::CLEO)
}

/// Returns the absolute path to the current working directory (normally the game directory)
///
/// since v1
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()
}

/// Resolves a path to the absolute path
///     - absolute path gets resolved as is
/// - path starting with "CLEO/" gets resolved relative to the CLEO directory
///    * either {game}\CLEO or
///    * {user}\AppData\Roaming\CLEO Redux\CLEO
///  - all other paths get resolved relative to the cwd (game directory)
///
/// since v1
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()
}

/// Reads a floating-point argument
///
/// since v1
pub fn get_float_param(ctx: Context) -> f32 {
    unsafe { GetFloatParam(ctx) }
}

/// Writes the floating-point value
///
/// since v1
pub fn set_float_param(ctx: Context, value: f32) {
    unsafe { SetFloatParam(ctx, value) };
}

/// Sets the status of the current condition
///
/// since v1
pub fn update_compare_flag(ctx: Context, value: bool) {
    unsafe { UpdateCompareFlag(ctx, value) }
}

/// Returns a host name
/// The default value matches https://re.cleo.li/docs/en/api.html#host
/// Can be changed with SetHostName
///
/// since v2
pub fn get_host_name() -> String {
    let mut buf = [0i8; SDK_STRING_MAX_LEN];
    unsafe { GetHostName(buf.as_mut_ptr(), SDK_STRING_MAX_LEN as _) };
    to_rust_string(buf.as_ptr())
}

/// Sets the new host name (accessible in scripts via the HOST constant)
///
/// since v2
pub fn set_host_name(value: String) {
    unsafe { SetHostName(sz!(value)) };
}

/// Initializes or reloads CLEO runtime
///
/// since v2
pub fn runtime_init() {
    unsafe { RuntimeInit() }
}

/// Iterates the main loop
///
/// since v2
pub fn runtime_next_tick(current_time: u32, time_step: i32) {
    unsafe { RuntimeNextTick(current_time, time_step) }
}

pub fn to_rust_string(addr: *const i8) -> String {
    unsafe {
        std::ffi::CStr::from_ptr(addr)
            .to_owned()
            .into_string()
            .unwrap_or_default()
    }
}

/// Registers a new loader for files matching a glob pattern
///
/// since v3
#[allow(dead_code)]
pub fn register_loader(glob: &str, cb: CustomLoader) {
    unsafe {
        RegisterLoader(sz!(glob), cb);
    }
}

/// Allocates a memory chunk with size in bytes. Memory is guaranteed to be zero initialized
///
/// since v3
#[allow(dead_code)]
pub fn alloc_mem(size: usize) -> *mut c_void {
    unsafe { AllocMem(size) }
}

/// Frees up the memory chunk allocated with AllocMem
///
/// since v3
#[allow(dead_code)]
pub fn free_mem(ptr: *mut c_void) {
    unsafe { FreeMem(ptr) }
}

/// Registers a new callback invoked on each main loop iteration (before scripts are executed)
///
/// since v4
#[allow(dead_code)]
pub fn on_before_scripts(cb: OnTickCallback) {
    unsafe {
        OnBeforeScripts(cb);
    }
}

/// Registers a new callback invoked on each main loop iteration (after scripts are executed)
///
/// since v4
#[allow(dead_code)]
pub fn on_after_scripts(cb: OnTickCallback) {
    unsafe {
        OnAfterScripts(cb);
    }
}

/// Registers a new callback invoked on each runtime init event (new game, saved game load, or SDK's RuntimeInit)
///
/// since v4
#[allow(dead_code)]
pub fn on_runtime_init(cb: OnRuntimeInitCallback) {
    unsafe {
        OnRuntimeInit(cb);
    }
}

/// Registers a new callback invoked on a ShowTextBox function call. Providing a callback shadows built-in ShowTextBox implementation.
///
/// since v5
#[allow(dead_code)]
pub fn on_show_text_box(cb: OnShowTextBoxCallback) {
    unsafe {
        OnShowTextBox(cb);
    }
}

/// Returns the absolute path to the CLEO root directory or one of its sub-directories
///
/// since v6
#[allow(dead_code)]
pub fn get_directory_path(dir: Directory) -> std::path::PathBuf {
    let mut buf = [0i8; 256];
    unsafe { GetDirectoryPath(dir, buf.as_mut_ptr()) };
    std::path::Path::new(&to_rust_string(buf.as_ptr())).into()
}

/// Returns CLEO Redux version as a string
///
/// since v6
#[allow(dead_code)]
pub fn get_cleo_version() -> String {
    let mut buf = [0i8; 256];
    unsafe { GetCLEOVersion(buf.as_mut_ptr()) };
    to_rust_string(buf.as_ptr())
}

/// Returns a memory address for the given symbol, or 0 if not found
///
/// since v6
#[allow(dead_code)]
pub fn get_symbol_address(symbol: &str) -> usize {
    unsafe { GetSymbolAddress(sz!(symbol)) }
}

/// Get number of active CS scripts
///
/// since v6
#[allow(dead_code)]
pub fn get_number_of_active_cs_scripts() -> usize {
    unsafe { GetNumberOfActiveCSScripts() }
}

/// Get number of active JS scripts
///
/// since v6
#[allow(dead_code)]
pub fn get_number_of_active_js_scripts() -> usize {
    unsafe { GetNumberOfActiveJSScripts() }
}

/// Is end of arguments reached
///
/// since v6
#[allow(dead_code)]
pub fn is_end_of_arguments(ctx: Context) -> bool {
    unsafe { IsEndOfArguments(ctx) }
}

/// Triggers an event with the given name and data
///
/// since v7
#[allow(dead_code)]
pub fn trigger_event(name: &str, data: &str) {
    unsafe { TriggerEvent(sz!(name), sz!(data)) }
}