unity/
system.rs

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
use crate::prelude::{Il2CppArray, Il2CppClassData, MethodInfo};
use std::{marker::PhantomData, ops::{Deref, DerefMut}};

pub mod string;
pub use string::Il2CppString;

#[repr(C)]
#[crate::class("System", "Type")]
pub struct SystemType { }

#[repr(C)]
#[crate::class("System", "Byte")]
pub struct SystemByte { }


#[crate::from_offset("System", "RuntimeType", "MakeGenericType")]
pub fn runtime_type_make_generic_type(gt: *const u8, ty: *const u8);

#[repr(C)]
#[crate::class("System.Collections.Generic", "List`1")]
pub struct SystemList {}

/// The Il2Cpp equivalent of a C# List, similar to a Rust Vec.
/// 
/// Internally backed by a [`Il2CppArray`](crate::il2cpp::object::Il2CppArray), this class keeps track of how many entries are in the array.  
/// This means you do not want to directly edit the array unless you also increase the size field.
#[repr(C)]
#[crate::class("System.Collections.Generic", "List`1")]
pub struct List<T: 'static> {
    pub items: &'static mut Il2CppArray<&'static mut T>,
    pub size: u32,
    version: u32,
    sync_root: *const u8,
}

impl<T: 'static> Deref for ListFields<T> {
    type Target = [&'static mut T];

    fn deref(&self) -> &Self::Target {
        unsafe { std::slice::from_raw_parts(self.items.m_items.as_ptr(), self.size as usize) }
    }
}

impl<T: 'static> DerefMut for ListFields<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { std::slice::from_raw_parts_mut(self.items.m_items.as_mut_ptr(), self.size as usize) }

    }
}

impl<T: crate::il2cpp::class::Il2CppClassData> List<T> {
    pub fn with_capacity(capacity: i32) -> Result<&'static mut Self, crate::Il2CppError> {
        let list_class = crate::il2cpp::class::make_generic(&SystemList::class(), &[T::class()])?;
        //let list_class = crate::get_generic_class!(SystemList<T>).unwrap();
        let list = crate::il2cpp::instantiate_class::<Self>(&list_class)?;

        // Get constructor that expects a capacity
        let method = list.get_class()
            .get_methods()
            .iter()
            .find(|method| method.get_name() == Some(String::from(".ctor")) && method.parameters_count == 1)
            .unwrap();

        let ctor = unsafe {
            std::mem::transmute::<_, extern "C" fn(&Self, i32, &MethodInfo)>(
                method.method_ptr,
            )
        };

        ctor(list, capacity, method);

        Ok(list)
    }
}

impl<T> List<T> {
    pub fn resize(&mut self, length: usize) {
        if self.items.len() != length {
            let new_array = crate::il2cpp::object::Il2CppArray::new_specific(self.items.get_class(), length as _).unwrap();
            new_array[..self.items.len()].swap_with_slice(self.items);
            self.items = new_array;
        }
    }

    pub fn add(&mut self, element: &'static T) {
        let method = self.get_class()
            .get_methods()
            .iter()
            .find(|method| method.get_name() == Some(String::from("Add")))
            .unwrap();
        
        let add = unsafe {
            std::mem::transmute::<_, extern "C" fn(&mut Self, &'static T, &MethodInfo)>(
                method.method_ptr,
            )
        };

        add(self, element, method);
    }

    pub fn insert(&mut self, index: i32, element: &'static mut T) {
        let method = self.get_class()
            .get_methods()
            .iter()
            .find(|method| method.get_name() == Some(String::from("Insert")))
            .unwrap();
        
        let insert = unsafe {
            std::mem::transmute::<_, extern "C" fn(&mut Self, i32, &'static mut T, &MethodInfo)>(
                method.method_ptr,
            )
        };

        insert(self, index, element, method);
    }
    pub fn len(&self) -> usize {
        self.size as _
    }

    pub fn capacity(&self) -> usize {
        self.items.len() as _
    }

    pub fn clear(&mut self) {
        self.get_class().get_virtual_method("Clear").map(|method| {
            let clear = unsafe { std::mem::transmute::<_, extern "C" fn(&List<T>, &MethodInfo)>(method.method_info.method_ptr) };
            clear(&self, method.method_info);
        }).unwrap();
    }
}

pub trait ListVirtual<T>: Il2CppClassData {
    fn add(&mut self, element: &'static mut T) {
        let method = Self::class().get_virtual_method("Add").unwrap();
        
        let add = unsafe {
            std::mem::transmute::<_, extern "C" fn(&mut Self, &'static mut T, &MethodInfo)>(
                method.method_info.method_ptr,
            )
        };

        add(self, element, method.method_info);
    }
}

#[repr(C)]
#[crate::class("System.Collections.Generic", "Stack`1")]
pub struct Stack<T: 'static> {
    pub items: &'static mut Il2CppArray<&'static mut T>,
    pub size: u32,
    version: u32,
    sync_root: *const u8,
}

impl<T: 'static> Deref for StackFields<T> {
    type Target = [&'static mut T];

    fn deref(&self) -> &Self::Target {
        unsafe { std::slice::from_raw_parts(self.items.m_items.as_ptr(), self.size as usize) }
    }
}

impl<T: 'static> DerefMut for StackFields<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { std::slice::from_raw_parts_mut(self.items.m_items.as_mut_ptr(), self.size as usize) }

    }
}

impl<T> Stack<T> {
    pub fn pop(&mut self) -> Option<&'static mut T> {
        let method = self.get_class()
            .get_methods()
            .iter()
            .find(|method| method.get_name() == Some(String::from("Pop")))
            .unwrap();
        
        let pop = unsafe {
            std::mem::transmute::<_, extern "C" fn(&mut Self, &MethodInfo) -> Option<&'static mut T>>(
                method.method_ptr,
            )
        };

        pop(self, method)
    }

    pub fn len(&self) -> usize {
      self.size as _
    }

    pub fn capacity(&self) -> usize {
        self.items.len() as _
    }
}

#[crate::class("System.Collections.Generic", "Dictionary`1")]
pub struct Dictionary<'a, TKey, TValue> {
    array: &'a mut Il2CppArray<i32>,
    pub entries: &'a Il2CppArray<DictionaryEntry<TKey, TValue>>,
}
#[repr(C)]
pub struct DictionaryEntry<TKey, TValue> {
    hash: i32,
    next: i32,
    pub key: Option<TKey>,
    pub value: TValue,
}

impl<'a, TKey, TValue> Dictionary<'a, TKey, TValue> {
    pub fn add(&self, key: TKey, value: TValue) {
        let method = self.get_class()
            .get_virtual_method("Add")
            .unwrap();

        let add = unsafe {
            std::mem::transmute::<_, extern "C" fn(&Self, TKey, TValue, &MethodInfo)>(
                method.method_info.method_ptr,
            )
        };

        add(self, key, value, method.method_info);
    }
    pub fn remove(&self, key: TKey) {
        let method = self.get_class()
            .get_virtual_method("Remove")
            .unwrap();

        let remove = unsafe {
            std::mem::transmute::<_, extern "C" fn(&Self, TKey, &MethodInfo)>(
                method.method_info.method_ptr,
            )
        };
        remove(self, key, method.method_info);
    }
    pub fn get_count(&self) -> i32 {
        let method = self.get_class()
            .get_virtual_method("get_Count")
            .unwrap();

        let count = unsafe {
            std::mem::transmute::<_, extern "C" fn(&Self) -> i32>(
                method.method_info.method_ptr,
            )
        };
        count(self)
    }
    pub fn try_get_value(&self, key: TKey, value: &'a mut TValue) -> bool {
        let method = self.get_class()
            .get_virtual_method("TryGetValue")
            .unwrap();

        let try_get_value = unsafe {
            std::mem::transmute::<_, extern "C" fn(&Self, TKey, &'a mut TValue, &MethodInfo) -> bool>(
                method.method_info.method_ptr,
            )
        };

        try_get_value(self, key, value, method.method_info)
    }
}