mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-06-17 06:28:42 +08:00

* Errors are now logged properly. * Can now define classes, methods, etc - no longer has to be an expression body. * Added `StartCoroutine(IEnumerator routine)` helper method to easily run a Coroutine * Disabling suggestions now properly stops Explorer trying to update suggestion cache instead of just not showing them. In the rare cases that suggestions cause a crash, disabling them will now prevent those crashes. * Various other misc improvements behind the scenes
123 lines
4.0 KiB
C#
123 lines
4.0 KiB
C#
#if CPP
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using BF = System.Reflection.BindingFlags;
|
|
using System.Text;
|
|
using UnhollowerBaseLib;
|
|
using UnhollowerRuntimeLib;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Collections;
|
|
|
|
namespace UnityExplorer.Core.Runtime.Il2Cpp
|
|
{
|
|
public class Il2CppProvider : RuntimeProvider
|
|
{
|
|
public override void Initialize()
|
|
{
|
|
Reflection = new Il2CppReflection();
|
|
TextureUtil = new Il2CppTextureUtil();
|
|
}
|
|
|
|
public override void SetupEvents()
|
|
{
|
|
try
|
|
{
|
|
//Application.add_logMessageReceived(new Action<string, string, LogType>(ExplorerCore.Instance.OnUnityLog));
|
|
|
|
var logType = ReflectionUtility.GetTypeByName("UnityEngine.Application+LogCallback");
|
|
var castMethod = logType.GetMethod("op_Implicit", new[] { typeof(Action<string, string, LogType>) });
|
|
var addMethod = typeof(Application).GetMethod("add_logMessageReceived", BF.Static | BF.Public, null, new[] { logType }, null);
|
|
addMethod.Invoke(null, new[]
|
|
{
|
|
castMethod.Invoke(null, new[] { new Action<string, string, LogType>(ExplorerCore.Instance.OnUnityLog) })
|
|
});
|
|
}
|
|
catch
|
|
{
|
|
ExplorerCore.LogWarning("Exception setting up Unity log listener, make sure Unity libraries have been unstripped!");
|
|
}
|
|
}
|
|
|
|
public override void StartConsoleCoroutine(IEnumerator routine)
|
|
{
|
|
Il2CppCoroutine.Start(routine);
|
|
}
|
|
|
|
internal delegate IntPtr d_LayerToName(int layer);
|
|
|
|
public override string LayerToName(int layer)
|
|
{
|
|
var iCall = ICallManager.GetICall<d_LayerToName>("UnityEngine.LayerMask::LayerToName");
|
|
return IL2CPP.Il2CppStringToManaged(iCall.Invoke(layer));
|
|
}
|
|
|
|
internal delegate IntPtr d_FindObjectsOfTypeAll(IntPtr type);
|
|
|
|
public override UnityEngine.Object[] FindObjectsOfTypeAll(Type type)
|
|
{
|
|
var iCall = ICallManager.GetICall<d_FindObjectsOfTypeAll>("UnityEngine.Resources::FindObjectsOfTypeAll");
|
|
var cppType = Il2CppType.From(type);
|
|
|
|
return new Il2CppReferenceArray<UnityEngine.Object>(iCall.Invoke(cppType.Pointer));
|
|
}
|
|
|
|
public override int GetSceneHandle(Scene scene)
|
|
=> scene.handle;
|
|
|
|
//Scene.GetRootGameObjects();
|
|
|
|
internal delegate void d_GetRootGameObjects(int handle, IntPtr list);
|
|
|
|
public override GameObject[] GetRootGameObjects(Scene scene) => GetRootGameObjects(scene.handle);
|
|
|
|
public static GameObject[] GetRootGameObjects(int handle)
|
|
{
|
|
if (handle == -1)
|
|
return new GameObject[0];
|
|
|
|
int count = GetRootCount(handle);
|
|
|
|
if (count < 1)
|
|
return new GameObject[0];
|
|
|
|
var list = new Il2CppSystem.Collections.Generic.List<GameObject>(count);
|
|
|
|
var iCall = ICallManager.GetICall<d_GetRootGameObjects>("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal");
|
|
|
|
iCall.Invoke(handle, list.Pointer);
|
|
|
|
return list.ToArray();
|
|
}
|
|
|
|
//Scene.rootCount;
|
|
|
|
internal delegate int d_GetRootCountInternal(int handle);
|
|
|
|
public override int GetRootCount(Scene scene) => GetRootCount(scene.handle);
|
|
|
|
public static int GetRootCount(int handle)
|
|
{
|
|
return ICallManager.GetICall<d_GetRootCountInternal>("UnityEngine.SceneManagement.Scene::GetRootCountInternal")
|
|
.Invoke(handle);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class UnityEventExtensions
|
|
{
|
|
public static void AddListener(this UnityEvent action, Action listener)
|
|
{
|
|
action.AddListener(listener);
|
|
}
|
|
|
|
public static void AddListener<T>(this UnityEvent<T> action, Action<T> listener)
|
|
{
|
|
action.AddListener(listener);
|
|
}
|
|
}
|
|
|
|
#endif |