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" {
    /// since v1
    ///
    /// Returns the current SDK version as an integer number.
    fn GetSDKVersion() -> i32;
    /// since v1
    ///
    /// Returns the current host (game) id
    fn GetHostId() -> HostId;
    /// since v1
    ///
    /// 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)
    fn ResolvePath(src: *const c_char, dest: *mut c_char);
    /// since v1
    ///
    /// Returns the absolute path to the CLEO directory
    fn GetCLEOFolder(dest: *mut c_char);
    /// since v1
    ///
    /// Returns the absolute path to the current working directory (normally the game directory)
    fn GetCwd(dest: *mut c_char);
    /// since v1
    ///
    /// Prints a new entry to the cleo_redux.log
    fn Log(text: *const c_char);
    /// since v1
    ///
    /// 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)
    fn RegisterCommand(name: *const c_char, cb: CustomCommand, permission: *const c_char);
    /// since v1
    ///
    /// Reads an integer argument (32 or 64 bit depending on target platform)
    fn GetIntParam(ctx: Context) -> isize;
    /// since v1
    ///
    /// Reads a floating-point argument
    fn GetFloatParam(ctx: Context) -> f32;
    /// since v1
    ///
    /// Reads a string argument
    fn GetStringParam(ctx: Context, dest: *mut c_char, maxlen: u8);
    /// since v1
    ///
    /// Writes the integer value (32 or 64 bit depending on target platform)
    fn SetIntParam(ctx: Context, value: isize);
    /// since v1
    ///
    /// Writes the floating-point value
    fn SetFloatParam(ctx: Context, value: f32);
    /// since v1
    ///
    /// Writes the string value
    fn SetStringParam(ctx: Context, src: *const c_char);
    /// since v1
    ///
    /// Sets the status of the current condition
    fn UpdateCompareFlag(ctx: Context, result: bool);
}

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

/// since v1
///
/// 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)
#[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()),
        };
    }
}

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

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

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

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

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

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

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

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

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

/// since v1
///
/// 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)
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()
}

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

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

/// since v1
///
/// Sets the status of the current condition
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()
    }
}