hao-il2cpp-runtime 0.0.1

Hook and call into Unity Il2Cpp games from inside the game process
Documentation
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Typed values, hooks and calls, checked against the game's method signatures.

use std::{any::Any, ffi::c_void, marker::PhantomData, sync::RwLock};

use crate::{Error, Il2Cpp, Method, MethodInfo, Ptr, Result};

/// Managed element types (`Il2CppTypeEnum`).
pub mod element {
    pub const VOID: i32 = 0x01;
    pub const BOOLEAN: i32 = 0x02;
    pub const CHAR: i32 = 0x03;
    pub const I1: i32 = 0x04;
    pub const U1: i32 = 0x05;
    pub const I2: i32 = 0x06;
    pub const U2: i32 = 0x07;
    pub const I4: i32 = 0x08;
    pub const U4: i32 = 0x09;
    pub const I8: i32 = 0x0A;
    pub const U8: i32 = 0x0B;
    pub const R4: i32 = 0x0C;
    pub const R8: i32 = 0x0D;
    pub const STRING: i32 = 0x0E;
    pub const PTR: i32 = 0x0F;
    pub const VALUETYPE: i32 = 0x11;
    pub const CLASS: i32 = 0x12;
    pub const VAR: i32 = 0x13;
    pub const ARRAY: i32 = 0x14;
    pub const GENERICINST: i32 = 0x15;
    pub const I: i32 = 0x18;
    pub const U: i32 = 0x19;
    pub const OBJECT: i32 = 0x1C;
    pub const SZARRAY: i32 = 0x1D;
    pub const MVAR: i32 = 0x1E;
}

/// The managed type of a parameter or return value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TypeInfo {
    /// The `Il2CppTypeEnum` value. For enums, the element type of the underlying integer.
    pub element: i32,
    pub value_type: bool,
    pub is_enum: bool,
    pub byref: bool,
    pub namespace: &'static str,
    pub name: &'static str,
}

impl TypeInfo {
    /// Whether this is the value type `namespace.name`, passed by value. For
    /// [`Il2CppValue`] impls of `#[repr(C)]` structs mirroring a managed struct.
    pub fn is_value_type(&self, namespace: &str, name: &str) -> bool {
        !self.byref
            && self.value_type
            && !self.is_enum
            && self.namespace == namespace
            && self.name == name
    }
}

/// A method parameter's name and managed type name, from [`Method::parameters`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Parameter {
    /// The declared name, e.g. `amount` (often `arg0` in release builds).
    pub name: String,
    /// The managed type name, e.g. `System.Single`.
    pub type_name: String,
}

/// A managed object reference (`Il2CppObject*`), possibly null.
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Object(pub Ptr);

unsafe impl Send for Object {}
unsafe impl Sync for Object {}

impl Object {
    pub const NULL: Object = Object(std::ptr::null_mut());

    pub fn is_null(&self) -> bool {
        self.0.is_null()
    }

    pub fn as_ptr(&self) -> Ptr {
        self.0
    }
}

/// A managed string (`Il2CppString*`), possibly null.
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Il2CppString(pub Object);

impl Il2CppString {
    pub fn new(il2cpp: &Il2Cpp, value: &str) -> Result<Self> {
        il2cpp.string(value)
    }

    pub fn is_null(&self) -> bool {
        self.0.is_null()
    }

    /// The string's contents, or `None` for null.
    pub fn to_rust(&self) -> Option<String> {
        let il2cpp = Il2Cpp::get()?;
        // Values of this type only come from string typed arguments and returns.
        unsafe { il2cpp.read_string(self.0) }
    }
}

/// A Rust type that has the same representation as some managed types when
/// passed to and from compiled Il2Cpp code.
///
/// # Safety
/// `matches` may only accept managed types whose values are passed exactly like `Self`.
pub unsafe trait Il2CppValue: Copy + 'static {
    fn matches(ty: TypeInfo) -> bool;

    fn describe() -> &'static str {
        std::any::type_name::<Self>()
    }

    /// Whether this is a pointer to an object, so it can be the instance (`this`).
    fn is_reference() -> bool {
        false
    }
}

macro_rules! primitive {
    ($($t:ty => [$($e:ident),+];)*) => {$(
        unsafe impl Il2CppValue for $t {
            fn matches(ty: TypeInfo) -> bool {
                !ty.byref && [$(element::$e),+].contains(&ty.element)
            }
        }
    )*};
}

primitive! {
    () => [VOID];
    bool => [BOOLEAN];
    u16 => [CHAR, U2];
    i8 => [I1];
    u8 => [U1];
    i16 => [I2];
    i32 => [I4];
    u32 => [U4];
    i64 => [I8];
    u64 => [U8];
    f32 => [R4];
    f64 => [R8];
    isize => [I];
    usize => [U];
}

unsafe impl Il2CppValue for Object {
    fn is_reference() -> bool {
        true
    }

    fn matches(ty: TypeInfo) -> bool {
        !ty.byref
            && !ty.value_type
            && [
                element::STRING,
                element::CLASS,
                element::OBJECT,
                element::SZARRAY,
                element::ARRAY,
                element::GENERICINST,
                element::VAR,
                element::MVAR,
            ]
            .contains(&ty.element)
    }
}

unsafe impl Il2CppValue for Il2CppString {
    fn is_reference() -> bool {
        true
    }

    fn matches(ty: TypeInfo) -> bool {
        !ty.byref && ty.element == element::STRING
    }
}

/// `ref`/`out` parameters and pointers.
unsafe impl Il2CppValue for *mut c_void {
    fn is_reference() -> bool {
        true
    }

    fn matches(ty: TypeInfo) -> bool {
        ty.byref || ty.element == element::PTR
    }
}

/// Calls the original code of a hooked method.
pub struct Original<Args, R> {
    code: *const c_void,
    method: MethodInfo,
    _marker: PhantomData<fn(Args) -> R>,
}

/// How a Rust type is checked against a managed parameter or return type.
#[derive(Clone, Copy)]
pub struct ValueCheck {
    pub matches: fn(TypeInfo) -> bool,
    pub name: &'static str,
    pub reference: bool,
}

impl ValueCheck {
    pub fn of<T: Il2CppValue>() -> Self {
        Self {
            matches: T::matches,
            name: T::describe(),
            reference: T::is_reference(),
        }
    }
}

/// The Rust types of a hook or call, checked against the method before use.
pub struct Signature {
    pub parameters: Vec<ValueCheck>,
    pub ret: ValueCheck,
}

impl<Args: MethodArgs, R: Il2CppValue> Original<Args, R> {
    /// Calls the original code with `args`, a tuple like the callback's arguments.
    pub fn call(&self, args: Args) -> R {
        unsafe { args.invoke(self.code, self.method) }
    }
}

/// A closure usable as a hook for a method with parameters `Args` returning `R`.
/// Implemented for `Fn(args..., Original<(args...), R>) -> R`.
pub trait HookCallback<Args, R>: Send + Sync + 'static {
    #[doc(hidden)]
    fn trampoline() -> *mut u8;
    #[doc(hidden)]
    fn signature() -> Signature;
}

/// Argument tuples for [`Method::call`].
pub trait MethodArgs {
    #[doc(hidden)]
    fn parameters() -> Vec<ValueCheck>;
    #[doc(hidden)]
    unsafe fn invoke<R: Il2CppValue>(self, code: *const c_void, method: MethodInfo) -> R;
}

type Callbacks = RwLock<Vec<(usize, &'static (dyn Any + Send + Sync))>>;

/// Hook closures by the address of their hook's original-function stub.
static CALLBACKS: Callbacks = RwLock::new(Vec::new());

pub(crate) fn register<F: Any + Send + Sync>(stub: usize, callback: F) {
    let callback: &'static (dyn Any + Send + Sync) = Box::leak(Box::new(callback));
    CALLBACKS
        .write()
        .unwrap_or_else(|e| e.into_inner())
        .push((stub, callback));
}

fn callback<F: Any>(stub: usize) -> &'static F {
    let callbacks = CALLBACKS.read().unwrap_or_else(|e| e.into_inner());
    callbacks
        .iter()
        .rev()
        .find(|(s, _)| *s == stub)
        .and_then(|(_, c)| c.downcast_ref::<F>())
        .unwrap_or_else(|| {
            tracing::error!("no callback registered for hook {:#x}, aborting", stub);
            eprintln!("[hao] no callback registered for hook {:#x}", stub);
            std::process::abort()
        })
}

macro_rules! arity {
    ($trampoline:ident; $($t:ident $v:ident),*) => {
        impl<F, $($t: Il2CppValue,)* R: Il2CppValue> HookCallback<($($t,)*), R> for F
        where
            F: Fn($($t,)* Original<($($t,)*), R>) -> R + Send + Sync + 'static,
        {
            fn trampoline() -> *mut u8 {
                $trampoline::<F, $($t,)* R> as *mut u8
            }

            fn signature() -> Signature {
                Signature {
                    parameters: vec![$(ValueCheck::of::<$t>(),)*],
                    ret: ValueCheck::of::<R>(),
                }
            }
        }

        #[allow(clippy::too_many_arguments)]
        extern "C-unwind" fn $trampoline<F, $($t,)* R>($($v: $t,)* method: MethodInfo) -> R
        where
            F: Fn($($t,)* Original<($($t,)*), R>) -> R + Send + Sync + 'static,
        {
            // Must come first: the hook passes the original code in registers.
            let code = hooking::original_function_ptr().map_or(std::ptr::null_mut(), |p| p.as_ptr());
            let callback = callback::<F>(code as usize);
            callback($($v,)* Original { code, method, _marker: PhantomData })
        }

        impl<$($t: Il2CppValue,)*> MethodArgs for ($($t,)*) {
            fn parameters() -> Vec<ValueCheck> {
                vec![$(ValueCheck::of::<$t>(),)*]
            }

            #[allow(clippy::unused_unit)]
            unsafe fn invoke<R: Il2CppValue>(self, code: *const c_void, method: MethodInfo) -> R {
                let ($($v,)*) = self;
                let f: extern "C-unwind" fn($($t,)* MethodInfo) -> R = unsafe { std::mem::transmute(code) };
                f($($v,)* method)
            }
        }
    };
}

arity!(trampoline0;);
arity!(trampoline1; A a);
arity!(trampoline2; A a, B b);
arity!(trampoline3; A a, B b, C c);
arity!(trampoline4; A a, B b, C c, D d);
arity!(trampoline5; A a, B b, C c, D d, E e);
arity!(trampoline6; A a, B b, C c, D d, E e, G g);
arity!(trampoline7; A a, B b, C c, D d, E e, G g, H h);
arity!(trampoline8; A a, B b, C c, D d, E e, G g, H h, I i);

impl Method {
    fn type_info(&self, ty: Ptr) -> TypeInfo {
        let api = &self.il2cpp.api;
        unsafe {
            let mut element = (api.il2cpp_type_get_type)(ty);
            let byref = (api.il2cpp_type_is_byref)(ty);
            let class = (api.il2cpp_type_get_class_or_element_class)(ty);
            let value_type = match element {
                element::VALUETYPE => true,
                element::GENERICINST => !class.is_null() && (api.il2cpp_class_is_valuetype)(class),
                _ => false,
            };
            let is_enum = element == element::VALUETYPE
                && !class.is_null()
                && (api.il2cpp_class_is_enum)(class);
            if is_enum {
                element = (api.il2cpp_type_get_type)((api.il2cpp_class_enum_basetype)(class));
            }
            let (namespace, name) = if class.is_null() {
                ("", "")
            } else {
                (
                    crate::c_str((api.il2cpp_class_get_namespace)(class)),
                    crate::c_str((api.il2cpp_class_get_name)(class)),
                )
            };
            TypeInfo {
                element,
                value_type,
                is_enum,
                byref,
                namespace,
                name,
            }
        }
    }

    fn type_name(&self, ty: Ptr) -> String {
        unsafe {
            let name = (self.il2cpp.api.il2cpp_type_get_name)(ty);
            if name.is_null() {
                return "?".into();
            }
            std::ffi::CStr::from_ptr(name)
                .to_string_lossy()
                .into_owned()
        }
    }

    pub fn is_instance(&self) -> bool {
        unsafe { (self.il2cpp.api.il2cpp_method_is_instance)(self.ptr) }
    }

    pub fn parameter_count(&self) -> u32 {
        unsafe { (self.il2cpp.api.il2cpp_method_get_param_count)(self.ptr) }
    }

    /// The managed name of the return type, e.g. `System.Int32`.
    pub fn return_type_name(&self) -> String {
        self.type_name(unsafe { (self.il2cpp.api.il2cpp_method_get_return_type)(self.ptr) })
    }

    /// The managed type name of parameter `index`, e.g. `System.Single`.
    pub fn parameter_type_name(&self, index: u32) -> String {
        self.type_name(unsafe { (self.il2cpp.api.il2cpp_method_get_param)(self.ptr, index) })
    }

    /// The declared name of parameter `index`, e.g. `amount`. Release builds
    /// often strip these to `arg0`, `arg1`, ...
    pub fn parameter_name(&self, index: u32) -> String {
        let name = unsafe { (self.il2cpp.api.il2cpp_method_get_param_name)(self.ptr, index) };
        unsafe { crate::c_str(name) }.to_owned()
    }

    /// The parameters, each with its name and type name.
    pub fn parameters(&self) -> impl Iterator<Item = Parameter> + '_ {
        (0..self.parameter_count()).map(move |i| Parameter {
            name: self.parameter_name(i),
            type_name: self.parameter_type_name(i),
        })
    }

    /// The managed signature, e.g. `System.Int32 Heal(System.Single, System.Boolean)`,
    /// built from [`Method::return_type_name`], [`Method::name`] and [`Method::parameters`].
    pub fn signature(&self) -> String {
        let params: Vec<String> = self.parameters().map(|p| p.type_name).collect();
        format!(
            "{}{} {}({})",
            if self.is_instance() { "" } else { "static " },
            self.return_type_name(),
            self.name(),
            params.join(", ")
        )
    }

    /// Checks `params` (with the instance first for instance methods) and `ret` against the method.
    fn check(&self, params: &[ValueCheck], ret: ValueCheck) -> Result<()> {
        let api = &self.il2cpp.api;
        let instance = self.is_instance() as usize;
        let count = self.parameter_count() as usize;
        let mismatch =
            |detail: String| Error::Signature(format!("{}: {}", self.signature(), detail));
        if params.len() != count + instance {
            return Err(mismatch(format!(
                "expected {} arguments{}, got {}",
                count + instance,
                if instance == 1 {
                    " including the instance"
                } else {
                    ""
                },
                params.len()
            )));
        }
        if instance == 1 && !params[0].reference {
            return Err(mismatch(format!(
                "the instance can't be passed as {}",
                params[0].name
            )));
        }
        for (i, check) in params[instance..].iter().enumerate() {
            let ty = unsafe { (api.il2cpp_method_get_param)(self.ptr, i as u32) };
            if !(check.matches)(self.type_info(ty)) {
                return Err(mismatch(format!(
                    "parameter {} is {}, not {}",
                    i,
                    self.type_name(ty),
                    check.name
                )));
            }
        }
        let ty = unsafe { (api.il2cpp_method_get_return_type)(self.ptr) };
        if !(ret.matches)(self.type_info(ty)) {
            return Err(mismatch(format!(
                "returns {}, not {}",
                self.type_name(ty),
                ret.name
            )));
        }
        Ok(())
    }

    /// Checks argument types `Args` (a tuple, with the instance first for
    /// instance methods) and return type `R` against the method's signature, as
    /// [`Method::hook`] and [`Method::call`] do.
    pub fn check_types<Args: MethodArgs, R: Il2CppValue>(&self) -> Result<()> {
        self.check(&Args::parameters(), ValueCheck::of::<R>())
    }

    /// Redirects calls of this method to `callback`, after checking its types
    /// against the method's signature. The callback gets the arguments (the
    /// instance first for instance methods) and an [`Original`] to call the
    /// original code with.
    ///
    /// ```no_run
    /// # use hao_il2cpp_runtime::{Il2Cpp, Object, Original};
    /// # fn f(il2cpp: &'static Il2Cpp) -> hao_il2cpp_runtime::Result<()> {
    /// let heal = il2cpp.class("Assembly-CSharp", "Game", "PlayerHealth")?.method("Heal", 2)?;
    /// heal.hook(|this: Object, amount: f32, overheal: bool, original: Original<_, i32>| {
    ///     original.call((this, amount * 2.0, overheal))
    /// })?;
    /// # Ok(()) }
    /// ```
    pub fn hook<Args, R, F: HookCallback<Args, R>>(
        &self,
        callback: F,
    ) -> Result<crate::MethodHook> {
        let signature = F::signature();
        self.check(&signature.parameters, signature.ret)?;
        let mut hook = unsafe { hooking::Hook::create(self.code() as *mut u8, F::trampoline()) }
            .map_err(Error::Hook)?;
        register(
            hook.data.original_fn_call_stub_data.as_ptr() as usize,
            callback,
        );
        unsafe { hook.apply_hook() }.map_err(Error::Hook)?;
        tracing::debug!(method = %self.signature(), code = ?self.code(), "hooked");
        Ok(crate::MethodHook(hook))
    }

    /// Calls the method's compiled code after checking the argument and return
    /// types. `args` is a tuple, with the instance first for instance methods.
    ///
    /// Managed exceptions thrown by the method unwind through the caller.
    pub fn call<Args: MethodArgs, R: Il2CppValue>(&self, args: Args) -> Result<R> {
        self.check(&Args::parameters(), ValueCheck::of::<R>())?;
        Ok(unsafe { args.invoke(self.code(), self.ptr) })
    }

    /// [`Method::call`] without the type check, for calls made often after
    /// [`Method::check_types`] has passed once.
    ///
    /// # Safety
    /// `Args` and `R` must pass [`Method::check_types`] for this method.
    pub unsafe fn call_unchecked<Args: MethodArgs, R: Il2CppValue>(&self, args: Args) -> R {
        unsafe { args.invoke(self.code(), self.ptr) }
    }
}