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
9pub type Ptr = *mut c_void;
10
11macro_rules! api {
12    ($($name:ident: fn($($arg:ty),*) $(-> $ret:ty)?;)*) => {
13        /// Function pointers into the game's Il2Cpp runtime.
14        #[allow(non_snake_case)]
15        pub struct Api {
16            $(pub $name: unsafe extern "C" fn($($arg),*) $(-> $ret)?,)*
17        }
18
19        impl Api {
20            pub(crate) fn load(library: &Library) -> Result<Self> {
21                unsafe {
22                    Ok(Self {
23                        $($name: *library
24                            .get(concat!(stringify!($name), "\0").as_bytes())
25                            .map_err(|e| Error::MissingExport(stringify!($name), e))?,)*
26                    })
27                }
28            }
29        }
30    };
31}
32
33api! {
34    il2cpp_init: fn(*const c_char) -> i32;
35    il2cpp_set_data_dir: fn(*const c_char);
36    il2cpp_domain_get: fn() -> Ptr;
37    il2cpp_thread_attach: fn(Ptr) -> Ptr;
38    il2cpp_domain_assembly_open: fn(Ptr, *const c_char) -> Ptr;
39    il2cpp_assembly_get_image: fn(Ptr) -> Ptr;
40    il2cpp_class_from_name: fn(Ptr, *const c_char, *const c_char) -> Ptr;
41    il2cpp_class_get_name: fn(Ptr) -> *const c_char;
42    il2cpp_class_get_namespace: fn(Ptr) -> *const c_char;
43    il2cpp_class_get_method_from_name: fn(Ptr, *const c_char, i32) -> Ptr;
44    il2cpp_class_get_field_from_name: fn(Ptr, *const c_char) -> Ptr;
45    il2cpp_class_get_fields: fn(Ptr, *mut Ptr) -> Ptr;
46    il2cpp_field_get_name: fn(Ptr) -> *const c_char;
47    il2cpp_field_get_type: fn(Ptr) -> Ptr;
48    il2cpp_field_get_flags: fn(Ptr) -> i32;
49    il2cpp_field_get_offset: fn(Ptr) -> usize;
50    il2cpp_method_get_name: fn(Ptr) -> *const c_char;
51    il2cpp_object_get_class: fn(Ptr) -> Ptr;
52    il2cpp_string_new: fn(*const c_char) -> Ptr;
53    il2cpp_string_chars: fn(Ptr) -> *const u16;
54    il2cpp_string_length: fn(Ptr) -> i32;
55    il2cpp_method_get_param_count: fn(Ptr) -> u32;
56    il2cpp_method_get_param: fn(Ptr, u32) -> Ptr;
57    il2cpp_method_get_param_name: fn(Ptr, u32) -> *const c_char;
58    il2cpp_method_get_return_type: fn(Ptr) -> Ptr;
59    il2cpp_method_is_instance: fn(Ptr) -> bool;
60    il2cpp_type_get_type: fn(Ptr) -> i32;
61    il2cpp_type_is_byref: fn(Ptr) -> bool;
62    il2cpp_type_get_name: fn(Ptr) -> *mut c_char;
63    il2cpp_class_get_methods: fn(Ptr, *mut Ptr) -> Ptr;
64    il2cpp_runtime_invoke: fn(Ptr, Ptr, *mut Ptr, *mut Ptr) -> Ptr;
65    il2cpp_type_get_class_or_element_class: fn(Ptr) -> Ptr;
66    il2cpp_class_is_valuetype: fn(Ptr) -> bool;
67    il2cpp_class_is_enum: fn(Ptr) -> bool;
68    il2cpp_class_enum_basetype: fn(Ptr) -> Ptr;
69    il2cpp_class_get_type: fn(Ptr) -> Ptr;
70    il2cpp_type_get_object: fn(Ptr) -> Ptr;
71    il2cpp_object_new: fn(Ptr) -> Ptr;
72    il2cpp_gchandle_new: fn(Ptr, bool) -> Ptr;
73    il2cpp_gchandle_get_target: fn(Ptr) -> Ptr;
74    il2cpp_gchandle_free: fn(Ptr);
75    il2cpp_array_length: fn(Ptr) -> usize;
76    il2cpp_array_new: fn(Ptr, usize) -> Ptr;
77}