/// <summary> /// Version of TryCast without the generic restriction /// </summary> private static bool TryCast <T>(Il2CppObjectBase obj, out T t) { t = default; var nativeClassPtr = Il2CppClassPointerStore <T> .NativeClassPtr; if (nativeClassPtr == IntPtr.Zero) { MelonLogger.Warning($"{typeof(T)} is not an Il2Cpp reference type"); return(false); } var num = IL2CPP.il2cpp_object_get_class(obj.Pointer); if (!IL2CPP.il2cpp_class_is_assignable_from(nativeClassPtr, num)) { MelonLogger.Warning($"{obj.GetType()} is not a {typeof(T)}"); return(false); } if (RuntimeSpecificsStore.IsInjected(num)) { t = (T)ClassInjectorBase.GetMonoObjectFromIl2CppPointer(obj.Pointer); return(true); } var type = Il2CppClassPointerStore <T> .CreatedTypeRedirect; if ((object)type == null) { type = typeof(T); } t = (T)Activator.CreateInstance(type, obj.Pointer); return(true); }
public static object TryCast(this Il2CppObjectBase self, Type type) { return(AccessTools.Method(self.GetType(), nameof(Il2CppObjectBase.TryCast)).MakeGenericMethod(type).Invoke(self, Array.Empty <object>())); }
static IntPtr getFieldAddress(Il2CppObjectBase obj, string fieldName) => IL2CPP.Il2CppObjectBaseToPtrNotNull(obj) + (int)IL2CPP.il2cpp_field_get_offset((IntPtr)obj.GetType().field("NativeFieldInfoPtr_" + fieldName).GetValue(null));