|
| 1 | +use std::marker::PhantomData; |
| 2 | +use crate::il2cpp::object::Array; |
| 3 | +use crate::macro_context::MethodInfo; |
| 4 | +use crate::prelude::{Il2CppClass, Il2CppClassData, OptionalMethod}; |
| 5 | + |
| 6 | +#[crate::class("System", "Delegate")] |
| 7 | +pub struct Delegate<T: Sized + 'static> { |
| 8 | + pub method_ptr: *const u8, |
| 9 | + invoke_impl: *const u8, |
| 10 | + target: Option<&'static T>, |
| 11 | + method: *const MethodInfo, |
| 12 | + __: [u8; 0x38], |
| 13 | +} |
| 14 | + |
| 15 | +#[crate::class("System", "MulticastDelegate")] |
| 16 | +pub struct MulticastDelegate<T: Sized + 'static> { |
| 17 | + pub parent: DelegateFields<T>, |
| 18 | + pub delegates: &'static Array<&'static mut Delegate<T>>, |
| 19 | +} |
| 20 | + |
| 21 | +/// Action that takes no arguments |
| 22 | +/// Performs methods of fn(&Obj) |
| 23 | +#[crate::class("System", "Action")] |
| 24 | +pub struct Action{ |
| 25 | + pub method_ptr: *const u8, |
| 26 | + invoke_impl: *const u8, |
| 27 | + pub target_obj: *const u8, // Reference to Obj |
| 28 | + pub method: Option<&'static MethodInfo>, |
| 29 | +} |
| 30 | + |
| 31 | +impl Action{ |
| 32 | + pub fn new<T: Il2CppClassData>(target: Option<&T>, method: fn(&T, OptionalMethod)) -> &'static mut Self { |
| 33 | + let mut method_info = MethodInfo::new(); |
| 34 | + method_info.method_ptr = method as _; |
| 35 | + method_info.parameters_count = 0; |
| 36 | + let action = Action::instantiate().unwrap(); |
| 37 | + action.ctor(target, &method_info); |
| 38 | + action |
| 39 | + } |
| 40 | + pub fn new_from_method_info<T>(target: Option<&T>, method_info: &MethodInfo) -> &'static mut Self { |
| 41 | + let action = Action::instantiate().unwrap(); |
| 42 | + action.ctor(target, method_info); |
| 43 | + action |
| 44 | + } |
| 45 | + #[crate::class_method(1)] pub fn invoke(&self); // Offset: 0x33F42B0 Flags: 0 |
| 46 | +} |
| 47 | + |
| 48 | +/// Action that makes one argument |
| 49 | +/// Performs method of fn(&Obj, Arg) |
| 50 | +#[crate::class("System", "Action`1")] |
| 51 | +pub struct Action1<A>{ |
| 52 | + pub method_ptr: *const u8, |
| 53 | + invoke_impl: *const u8, |
| 54 | + pub target_obj: *const u8, // Reference to Obj |
| 55 | + method: *const MethodInfo, |
| 56 | + __: [u8; 0x38], |
| 57 | + // delegates: &'static Array<&'static mut Delegate<T>>, |
| 58 | + phantom: PhantomData<A> |
| 59 | +} |
| 60 | + |
| 61 | +impl<A> Action1<A> |
| 62 | +where |
| 63 | + A: Il2CppClassData + Sized, |
| 64 | +{ |
| 65 | + pub fn new_with_method<T: Il2CppClassData>(object: Option<&T>, method: fn(&T, OptionalMethod)) -> &'static mut Self { |
| 66 | + let action_t_class = Il2CppClass::from_name("System", "Action`1").unwrap(); |
| 67 | + let klass = crate::il2cpp::class::make_generic(action_t_class, &[A::class()]) |
| 68 | + .expect(format!("Expect Action<{}>", A::class().get_name()).as_str()); |
| 69 | + let action = klass.instantiate_as::<Action1<A>>().unwrap(); |
| 70 | + let mut method_info = MethodInfo::new(); |
| 71 | + method_info.method_ptr = method as _; |
| 72 | + method_info.parameters_count = 1; |
| 73 | + action.ctor(object, &method_info); |
| 74 | + action |
| 75 | + } |
| 76 | + pub fn new<T: Il2CppClassData>(object: Option<&T>, method_info: &MethodInfo) -> &'static mut Self { |
| 77 | + let action_t_class = Il2CppClass::from_name("System", "Action`1").unwrap(); |
| 78 | + let klass = crate::il2cpp::class::make_generic(action_t_class, &[A::class()]) |
| 79 | + .expect(format!("Expect Action<{}>", A::class().get_name()).as_str()); |
| 80 | + |
| 81 | + let action = klass.instantiate_as::<Action1<A>>().unwrap(); |
| 82 | + action.ctor(object, method_info); |
| 83 | + action |
| 84 | + } |
| 85 | + pub fn invoke(&self, arg: A){ |
| 86 | + let invoke = unsafe { std::mem::transmute::<_, fn(&Self, A, OptionalMethod)>(self.klass.get_methods()[1].method_ptr) }; |
| 87 | + invoke(self, arg, None); |
| 88 | + } |
| 89 | +} |
| 90 | +/// Action that takes no arguments |
| 91 | +/// Performs methods of fn(&Obj) -> R |
| 92 | +#[crate::class("System", "Func`1")] |
| 93 | +pub struct Func<T: 'static , R>{ |
| 94 | + pub method_ptr: *const u8, |
| 95 | + invoke_impl: *const u8, |
| 96 | + pub target: Option<&'static T>, |
| 97 | + method: *const MethodInfo, |
| 98 | + __: [u8; 0x38], |
| 99 | + delegates: &'static Array<&'static mut Delegate<T>>, |
| 100 | + phantom: PhantomData<R>, |
| 101 | +} |
| 102 | +impl<T, R> Func<T, R> |
| 103 | +where |
| 104 | + T: Il2CppClassData + Sized, |
| 105 | + R: Il2CppClassData + Sized + 'static, |
| 106 | +{ |
| 107 | + pub fn new(object: Option<&T>, method_info: &MethodInfo) -> &'static mut Self { |
| 108 | + let func_r_class = Il2CppClass::from_name("System", "Func`1").unwrap(); |
| 109 | + let klass = crate::il2cpp::class::make_generic(func_r_class, &[R::class()]) |
| 110 | + .expect(format!("Expect Func<{}>", R::class().get_name()).as_str()); |
| 111 | + |
| 112 | + let action = klass.instantiate_as::<Func<T, R>>().unwrap(); |
| 113 | + action.ctor(object, method_info); |
| 114 | + action |
| 115 | + } |
| 116 | + pub fn invoke(&self) -> R { |
| 117 | + let invoke = unsafe { std::mem::transmute::<_, fn(&Self, OptionalMethod) -> R >(self.klass.get_methods()[1].method_ptr) }; |
| 118 | + invoke(self, None) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/// Action that takes no arguments |
| 123 | +/// Performs methods of fn(&Obj, A) -> R |
| 124 | +#[crate::class("System", "Func`2")] |
| 125 | +pub struct Func1<T: 'static, A, R>{ |
| 126 | + pub method_ptr: *const u8, |
| 127 | + invoke_impl: *const u8, |
| 128 | + pub target: Option<&'static T>, |
| 129 | + method: *const MethodInfo, |
| 130 | + __: [u8; 0x38], |
| 131 | + delegates: &'static Array<&'static mut Delegate<T>>, |
| 132 | + phantom1: PhantomData<A>, |
| 133 | + phantom2: PhantomData<R>, |
| 134 | +} |
| 135 | + |
| 136 | +/// Trait to use the .ctor for all delegate classes |
| 137 | +pub trait SystemDelegate: Il2CppClassData + Sized { |
| 138 | + fn ctor<T>(&self, object: Option<&T>, method_info: &MethodInfo) { |
| 139 | + unsafe { system_action_ctor(self, object, method_info); } |
| 140 | + } |
| 141 | +} |
| 142 | +impl<T> SystemDelegate for Delegate<T> {} |
| 143 | +impl<T> SystemDelegate for MulticastDelegate<T> {} |
| 144 | +impl SystemDelegate for Action {} |
| 145 | +impl<A> SystemDelegate for Action1<A> {} |
| 146 | +impl<T,R> SystemDelegate for Func<T,R> {} |
| 147 | + |
| 148 | +#[skyline::from_offset(0x33f4290)] |
| 149 | +fn system_action_ctor<D, T>(this: &D, obj: Option<&T>, method_info: &MethodInfo) |
| 150 | +where D: SystemDelegate; |
0 commit comments