2020-08-18 17:11:58 +10:00
|
|
|
|
using System;
|
2020-09-08 17:07:10 +10:00
|
|
|
|
using System.Collections;
|
2020-08-18 17:11:58 +10:00
|
|
|
|
using System.Collections.Generic;
|
2020-09-11 18:53:17 +10:00
|
|
|
|
using System.IO;
|
2020-09-18 18:38:11 +10:00
|
|
|
|
using System.Reflection;
|
2020-08-18 17:11:58 +10:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using BF = System.Reflection.BindingFlags;
|
2020-10-18 21:41:04 +11:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#if CPP
|
2020-09-08 17:07:10 +10:00
|
|
|
|
using ILType = Il2CppSystem.Type;
|
2020-09-27 22:04:23 +10:00
|
|
|
|
using UnhollowerBaseLib;
|
|
|
|
|
using UnhollowerRuntimeLib;
|
2020-10-18 04:39:50 +11:00
|
|
|
|
using System.Runtime.InteropServices;
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#endif
|
2020-08-18 17:11:58 +10:00
|
|
|
|
|
2020-10-16 19:40:01 +11:00
|
|
|
|
namespace Explorer.Helpers
|
2020-08-18 17:11:58 +10:00
|
|
|
|
{
|
2020-10-18 21:41:04 +11:00
|
|
|
|
[SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "External methods")]
|
2020-08-18 17:11:58 +10:00
|
|
|
|
public class ReflectionHelpers
|
|
|
|
|
{
|
|
|
|
|
public static BF CommonFlags = BF.Public | BF.Instance | BF.NonPublic | BF.Static;
|
|
|
|
|
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#if CPP
|
2020-10-08 06:15:42 +11:00
|
|
|
|
public static ILType GameObjectType => Il2CppType.Of<GameObject>();
|
2020-10-18 04:39:50 +11:00
|
|
|
|
public static ILType TransformType => Il2CppType.Of<Transform>();
|
|
|
|
|
public static ILType ObjectType => Il2CppType.Of<UnityEngine.Object>();
|
|
|
|
|
public static ILType ComponentType => Il2CppType.Of<Component>();
|
|
|
|
|
public static ILType BehaviourType => Il2CppType.Of<Behaviour>();
|
|
|
|
|
#else
|
|
|
|
|
public static Type GameObjectType => typeof(GameObject);
|
|
|
|
|
public static Type TransformType => typeof(Transform);
|
|
|
|
|
public static Type ObjectType => typeof(UnityEngine.Object);
|
|
|
|
|
public static Type ComponentType => typeof(Component);
|
|
|
|
|
public static Type BehaviourType => typeof(Behaviour);
|
|
|
|
|
#endif
|
2020-08-18 17:11:58 +10:00
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
#if CPP
|
|
|
|
|
private static readonly Dictionary<Type, IntPtr> ClassPointers = new Dictionary<Type, IntPtr>();
|
2020-08-18 17:11:58 +10:00
|
|
|
|
|
|
|
|
|
public static object Il2CppCast(object obj, Type castTo)
|
|
|
|
|
{
|
2020-10-18 04:39:50 +11:00
|
|
|
|
if (!(obj is Il2CppSystem.Object ilObj))
|
|
|
|
|
return obj;
|
2020-08-22 17:17:11 +10:00
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
if (!typeof(Il2CppSystem.Object).IsAssignableFrom(castTo))
|
|
|
|
|
return obj;
|
2020-09-21 22:45:33 +10:00
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
IntPtr castToPtr;
|
|
|
|
|
if (!ClassPointers.ContainsKey(castTo))
|
2020-09-27 22:04:23 +10:00
|
|
|
|
{
|
2020-10-18 04:39:50 +11:00
|
|
|
|
castToPtr = (IntPtr)typeof(Il2CppClassPointerStore<>)
|
|
|
|
|
.MakeGenericType(new Type[] { castTo })
|
|
|
|
|
.GetField("NativeClassPtr", BF.Public | BF.Static)
|
|
|
|
|
.GetValue(null);
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
ClassPointers.Add(castTo, castToPtr);
|
2020-08-31 18:23:19 +10:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-10-18 04:39:50 +11:00
|
|
|
|
castToPtr = ClassPointers[castTo];
|
2020-08-31 18:23:19 +10:00
|
|
|
|
}
|
2020-10-18 04:39:50 +11:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
if (castToPtr == IntPtr.Zero)
|
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
|
|
var classPtr = il2cpp_object_get_class(ilObj.Pointer);
|
2020-10-18 04:39:50 +11:00
|
|
|
|
|
|
|
|
|
if (!il2cpp_class_is_assignable_from(castToPtr, classPtr))
|
|
|
|
|
return obj;
|
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
if (RuntimeSpecificsStore.IsInjected(castToPtr))
|
|
|
|
|
return UnhollowerBaseLib.Runtime.ClassInjectorBase.GetMonoObjectFromIl2CppPointer(ilObj.Pointer);
|
2020-10-18 04:46:50 +11:00
|
|
|
|
|
2020-10-18 21:41:04 +11:00
|
|
|
|
return Activator.CreateInstance(castTo, ilObj.Pointer);
|
2020-08-18 17:11:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
|
|
|
|
public static extern bool il2cpp_class_is_assignable_from(IntPtr klass, IntPtr oklass);
|
2020-09-07 17:05:37 +10:00
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
[DllImport("GameAssembly", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
|
|
|
|
public static extern IntPtr il2cpp_object_get_class(IntPtr obj);
|
2020-10-18 21:41:04 +11:00
|
|
|
|
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#endif
|
2020-08-29 21:15:54 +10:00
|
|
|
|
|
2020-09-08 06:21:45 +10:00
|
|
|
|
public static Type GetTypeByName(string fullName)
|
2020-08-18 17:11:58 +10:00
|
|
|
|
{
|
|
|
|
|
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
|
|
|
|
|
{
|
2020-09-08 17:07:10 +10:00
|
|
|
|
foreach (var type in asm.TryGetTypes())
|
2020-08-18 17:11:58 +10:00
|
|
|
|
{
|
2020-09-08 06:21:45 +10:00
|
|
|
|
if (type.FullName == fullName)
|
2020-08-18 17:11:58 +10:00
|
|
|
|
{
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-01 18:03:44 +10:00
|
|
|
|
public static Type GetActualType(object obj)
|
2020-08-18 17:11:58 +10:00
|
|
|
|
{
|
2020-09-01 18:03:44 +10:00
|
|
|
|
if (obj == null) return null;
|
2020-08-22 00:16:05 +10:00
|
|
|
|
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#if CPP
|
2020-09-11 00:17:13 +10:00
|
|
|
|
// Need to use GetIl2CppType for Il2CppSystem Objects
|
2020-09-01 18:03:44 +10:00
|
|
|
|
if (obj is Il2CppSystem.Object ilObject)
|
2020-08-18 17:11:58 +10:00
|
|
|
|
{
|
2020-09-11 00:17:13 +10:00
|
|
|
|
// Prevent weird behaviour when inspecting an Il2CppSystem.Type object.
|
|
|
|
|
if (ilObject is ILType)
|
2020-08-30 23:29:37 +10:00
|
|
|
|
{
|
2020-09-11 00:17:13 +10:00
|
|
|
|
return typeof(ILType);
|
2020-08-30 23:29:37 +10:00
|
|
|
|
}
|
2020-09-01 18:03:44 +10:00
|
|
|
|
|
2020-09-11 00:17:13 +10:00
|
|
|
|
return Type.GetType(ilObject.GetIl2CppType().AssemblyQualifiedName) ?? obj.GetType();
|
2020-08-18 17:11:58 +10:00
|
|
|
|
}
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#endif
|
2020-09-01 18:03:44 +10:00
|
|
|
|
|
2020-09-11 00:17:13 +10:00
|
|
|
|
// It's a normal object, this is fine
|
2020-09-01 18:03:44 +10:00
|
|
|
|
return obj.GetType();
|
2020-08-18 17:11:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 06:15:42 +11:00
|
|
|
|
public static Type[] GetAllBaseTypes(object obj) => GetAllBaseTypes(GetActualType(obj));
|
|
|
|
|
|
|
|
|
|
public static Type[] GetAllBaseTypes(Type type)
|
2020-08-18 17:11:58 +10:00
|
|
|
|
{
|
|
|
|
|
var list = new List<Type>();
|
|
|
|
|
|
2020-09-11 00:17:13 +10:00
|
|
|
|
while (type != null)
|
2020-08-18 17:11:58 +10:00
|
|
|
|
{
|
|
|
|
|
list.Add(type);
|
2020-09-11 00:17:13 +10:00
|
|
|
|
type = type.BaseType;
|
2020-08-18 17:11:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list.ToArray();
|
|
|
|
|
}
|
2020-09-08 17:07:10 +10:00
|
|
|
|
|
2020-09-11 18:53:17 +10:00
|
|
|
|
public static bool LoadModule(string module)
|
|
|
|
|
{
|
2020-09-29 05:40:06 +10:00
|
|
|
|
#if CPP
|
|
|
|
|
#if ML
|
|
|
|
|
var path = $@"MelonLoader\Managed\{module}.dll";
|
|
|
|
|
#else
|
|
|
|
|
var path = $@"BepInEx\unhollowed\{module}.dll";
|
|
|
|
|
#endif
|
2020-09-11 18:53:17 +10:00
|
|
|
|
if (!File.Exists(path)) return false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Assembly.Load(File.ReadAllBytes(path));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-09-27 22:04:23 +10:00
|
|
|
|
ExplorerCore.Log(e.GetType() + ", " + e.Message);
|
2020-09-11 18:53:17 +10:00
|
|
|
|
}
|
2020-09-29 05:40:06 +10:00
|
|
|
|
#endif
|
|
|
|
|
return false;
|
2020-09-11 18:53:17 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
public static bool IsEnumerable(Type t)
|
2020-09-08 17:07:10 +10:00
|
|
|
|
{
|
2020-10-18 04:39:50 +11:00
|
|
|
|
if (typeof(IEnumerable).IsAssignableFrom(t))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#if CPP
|
2020-10-18 04:39:50 +11:00
|
|
|
|
if (t.IsGenericType && t.GetGenericTypeDefinition() is Type g)
|
2020-09-08 17:07:10 +10:00
|
|
|
|
{
|
2020-10-18 04:39:50 +11:00
|
|
|
|
return typeof(Il2CppSystem.Collections.Generic.List<>).IsAssignableFrom(g)
|
|
|
|
|
|| typeof(Il2CppSystem.Collections.Generic.IList<>).IsAssignableFrom(g)
|
|
|
|
|
|| typeof(Il2CppSystem.Collections.Generic.HashSet<>).IsAssignableFrom(g);
|
2020-09-08 17:07:10 +10:00
|
|
|
|
}
|
2020-10-18 04:39:50 +11:00
|
|
|
|
else
|
2020-09-08 17:07:10 +10:00
|
|
|
|
{
|
2020-10-18 04:39:50 +11:00
|
|
|
|
return typeof(Il2CppSystem.Collections.IList).IsAssignableFrom(t);
|
2020-09-08 17:07:10 +10:00
|
|
|
|
}
|
2020-10-18 04:39:50 +11:00
|
|
|
|
#else
|
|
|
|
|
return false;
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#endif
|
2020-09-08 17:07:10 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
public static bool IsDictionary(Type t)
|
2020-09-08 17:07:10 +10:00
|
|
|
|
{
|
2020-10-18 04:39:50 +11:00
|
|
|
|
if (typeof(IDictionary).IsAssignableFrom(t))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-09-08 17:07:10 +10:00
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
#if CPP
|
|
|
|
|
if (t.IsGenericType && t.GetGenericTypeDefinition() is Type g)
|
|
|
|
|
{
|
|
|
|
|
return typeof(Il2CppSystem.Collections.Generic.Dictionary<,>).IsAssignableFrom(g)
|
|
|
|
|
|| typeof(Il2CppSystem.Collections.Generic.IDictionary<,>).IsAssignableFrom(g);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return typeof(Il2CppSystem.Collections.IDictionary).IsAssignableFrom(t)
|
|
|
|
|
|| typeof(Il2CppSystem.Collections.Hashtable).IsAssignableFrom(t);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
return false;
|
|
|
|
|
#endif
|
2020-09-08 17:07:10 +10:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
public static string ExceptionToString(Exception e)
|
2020-09-08 17:07:10 +10:00
|
|
|
|
{
|
2020-10-18 04:39:50 +11:00
|
|
|
|
return e.GetType() + ", " + e.Message;
|
2020-09-08 17:07:10 +10:00
|
|
|
|
}
|
2020-08-18 17:11:58 +10:00
|
|
|
|
}
|
|
|
|
|
}
|