Skip to main content

hao_il2cpp_runtime/
api.rs

1//! The `il2cpp_*` functions exported by the game binary.
2
3use std::ffi::{c_char, c_void};
4
5use libloading::Library;
6
7use crate::error::{Error, Result};
8
9/// An untyped pointer into the Il2Cpp runtime: a domain, image, class,
10/// method, field, type or object, depending on the function.
11pub type Ptr = *mut c_void;
12
13macro_rules! api {
14    ($($name:ident: fn($($arg:ty),*) $(-> $ret:ty)?;)*) => {
15        /// Function pointers into the game's Il2Cpp runtime.
16        #[allow(non_snake_case)]
17        pub struct Api {
18            $(
19                #[doc = concat!("The game's `", stringify!($name), "` export.")]
20                pub $name: unsafe extern "C" fn($($arg),*) $(-> $ret)?,)*
21        }
22
23        impl Api {
24            pub(crate) fn load(library: &Library) -> Result<Self> {
25                unsafe {
26                    Ok(Self {
27                        $($name: *library
28                            .get(concat!(stringify!($name), "\0").as_bytes())
29                            .map_err(|e| Error::MissingExport(stringify!($name), e))?,)*
30                    })
31                }
32            }
33        }
34    };
35}
36
37api! {
38    il2cpp_init: fn(*const c_char) -> i32;
39    il2cpp_set_data_dir: fn(*const c_char);
40    il2cpp_domain_get: fn() -> Ptr;
41    il2cpp_thread_attach: fn(Ptr) -> Ptr;
42    il2cpp_domain_assembly_open: fn(Ptr, *const c_char) -> Ptr;
43    il2cpp_assembly_get_image: fn(Ptr) -> Ptr;
44    il2cpp_class_from_name: fn(Ptr, *const c_char, *const c_char) -> Ptr;
45    il2cpp_class_get_name: fn(Ptr) -> *const c_char;
46    il2cpp_class_get_namespace: fn(Ptr) -> *const c_char;
47    il2cpp_class_get_method_from_name: fn(Ptr, *const c_char, i32) -> Ptr;
48    il2cpp_class_get_field_from_name: fn(Ptr, *const c_char) -> Ptr;
49    il2cpp_class_get_fields: fn(Ptr, *mut Ptr) -> Ptr;
50    il2cpp_field_get_name: fn(Ptr) -> *const c_char;
51    il2cpp_field_get_type: fn(Ptr) -> Ptr;
52    il2cpp_field_get_flags: fn(Ptr) -> i32;
53    il2cpp_field_get_offset: fn(Ptr) -> usize;
54    il2cpp_method_get_name: fn(Ptr) -> *const c_char;
55    il2cpp_method_get_token: fn(Ptr) -> u32;
56    il2cpp_object_get_class: fn(Ptr) -> Ptr;
57    il2cpp_string_new: fn(*const c_char) -> Ptr;
58    il2cpp_string_chars: fn(Ptr) -> *const u16;
59    il2cpp_string_length: fn(Ptr) -> i32;
60    il2cpp_method_get_param_count: fn(Ptr) -> u32;
61    il2cpp_method_get_param: fn(Ptr, u32) -> Ptr;
62    il2cpp_method_get_param_name: fn(Ptr, u32) -> *const c_char;
63    il2cpp_method_get_return_type: fn(Ptr) -> Ptr;
64    il2cpp_method_is_instance: fn(Ptr) -> bool;
65    il2cpp_type_get_type: fn(Ptr) -> i32;
66    il2cpp_type_is_byref: fn(Ptr) -> bool;
67    il2cpp_type_get_name: fn(Ptr) -> *mut c_char;
68    il2cpp_class_get_methods: fn(Ptr, *mut Ptr) -> Ptr;
69    il2cpp_runtime_invoke: fn(Ptr, Ptr, *mut Ptr, *mut Ptr) -> Ptr;
70    il2cpp_type_get_class_or_element_class: fn(Ptr) -> Ptr;
71    il2cpp_class_is_valuetype: fn(Ptr) -> bool;
72    il2cpp_class_is_enum: fn(Ptr) -> bool;
73    il2cpp_class_enum_basetype: fn(Ptr) -> Ptr;
74    il2cpp_class_get_type: fn(Ptr) -> Ptr;
75    il2cpp_type_get_object: fn(Ptr) -> Ptr;
76    il2cpp_object_new: fn(Ptr) -> Ptr;
77    il2cpp_gchandle_new: fn(Ptr, bool) -> Ptr;
78    il2cpp_gchandle_get_target: fn(Ptr) -> Ptr;
79    il2cpp_gchandle_free: fn(Ptr);
80    il2cpp_array_length: fn(Ptr) -> usize;
81    il2cpp_array_new: fn(Ptr, usize) -> Ptr;
82    il2cpp_domain_get_assemblies: fn(Ptr, *mut usize) -> *mut Ptr;
83    il2cpp_image_get_class_count: fn(Ptr) -> usize;
84    il2cpp_image_get_class: fn(Ptr, usize) -> Ptr;
85    il2cpp_image_get_name: fn(Ptr) -> *const c_char;
86    il2cpp_object_unbox: fn(Ptr) -> Ptr;
87}