Improve Il2Cpp Coroutine support, make universal ExplorerBehaviour class

This commit is contained in:
Sinai
2021-05-01 20:55:14 +10:00
parent d6cde68a44
commit ab8b736f7e
13 changed files with 112 additions and 98 deletions

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
#if CPP
using UnhollowerRuntimeLib;
#endif
namespace UnityExplorer
{
// Handles all Behaviour update calls for UnityExplorer (Update, FixedUpdate, OnPostRender).
// Basically just a wrapper which calls the corresponding methods in ExplorerCore.
public class ExplorerBehaviour : MonoBehaviour
{
internal static ExplorerBehaviour Instance { get; private set; }
internal static void Setup()
{
#if CPP
ClassInjector.RegisterTypeInIl2Cpp<ExplorerBehaviour>();
#endif
var obj = new GameObject("ExplorerBehaviour");
GameObject.DontDestroyOnLoad(obj);
obj.hideFlags |= HideFlags.HideAndDontSave;
Instance = obj.AddComponent<ExplorerBehaviour>();
}
#if CPP
public ExplorerBehaviour(IntPtr ptr) : base(ptr) { }
#endif
internal void Awake()
{
#if CPP
Camera.onPostRender = Camera.onPostRender == null
? new Action<Camera>(OnPostRender)
: Il2CppSystem.Delegate.Combine(Camera.onPostRender, (Camera.CameraCallback)new Action<Camera>(OnPostRender)).Cast<Camera.CameraCallback>();
#else
Camera.onPostRender += OnPostRender;
#endif
}
internal void Update()
{
ExplorerCore.Update();
}
internal void FixedUpdate()
{
ExplorerCore.FixedUpdate();
}
internal static void OnPostRender(Camera camera)
{
ExplorerCore.OnPostRender();
}
}
}

View File

@ -143,6 +143,9 @@ namespace UnityExplorer.Core.Input
public static void ReleaseEventSystem()
{
if (EventSystem.current != UIManager.EventSys)
return;
if (InputManager.CurrentType == InputType.InputSystem)
return;
@ -173,7 +176,7 @@ namespace UnityExplorer.Core.Input
m_lastVisibleState = (bool?)CursorType.GetProperty("visible", BF.Public | BF.Static)?.GetValue(null, null)
?? false;
ExplorerCore.Loader.SetupPatches();
ExplorerCore.Loader.SetupCursorPatches();
}
catch (Exception e)
{

View File

@ -124,8 +124,11 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp
if (nextAsEnumerator != null) // il2cpp IEnumerator also handles CustomYieldInstruction
next = new Il2CppEnumeratorWrapper(nextAsEnumerator);
else
ExplorerCore.LogWarning($"Unknown coroutine yield object of type {il2CppObjectBase} for coroutine {enumerator}");
break;
ExplorerCore.LogWarning($"Unknown coroutine yield object of type '{il2CppObjectBase}' for coroutine '{enumerator}'");
return;
default:
ExplorerCore.LogWarning($"Unknown coroutine yield object of type '{next}' for coroutine '{enumerator}'");
return;
}
ourCoroutinesStore.Add(new CoroTuple { WaitCondition = next, Coroutine = enumerator });

View File

@ -56,6 +56,11 @@ namespace UnityExplorer.Core.Runtime.Il2Cpp
Il2CppCoroutine.Start(routine);
}
internal override void ProcessOnPostRender()
{
Il2CppCoroutine.ProcessWaitForEndOfFrame();
}
public override void Update()
{
Il2CppCoroutine.Process();

View File

@ -1,29 +0,0 @@
#if MONO
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace UnityExplorer.Core.Runtime.Mono
{
public class DummyBehaviour : MonoBehaviour
{
public static DummyBehaviour Instance;
public static void Setup()
{
var obj = new GameObject("Explorer_DummyBehaviour");
DontDestroyOnLoad(obj);
obj.hideFlags |= HideFlags.HideAndDontSave;
obj.AddComponent<DummyBehaviour>();
}
internal void Awake()
{
Instance = this;
}
}
}
#endif

View File

@ -23,8 +23,6 @@ namespace UnityExplorer.Core.Runtime.Mono
ExplorerCore.Context = RuntimeContext.Mono;
Reflection = new MonoReflection();
TextureUtil = new MonoTextureUtil();
DummyBehaviour.Setup();
}
public override void SetupEvents()
@ -39,7 +37,7 @@ namespace UnityExplorer.Core.Runtime.Mono
public override void StartCoroutine(IEnumerator routine)
{
DummyBehaviour.Instance.StartCoroutine(routine);
ExplorerBehaviour.Instance.StartCoroutine(routine);
}
public override void Update()

View File

@ -65,5 +65,13 @@ namespace UnityExplorer
public abstract void SetColorBlock(Selectable selectable, Color? normal = null, Color? highlighted = null, Color? pressed = null,
Color? disabled = null);
internal virtual void ProcessOnPostRender()
{
}
internal virtual void ProcessFixedUpdate()
{
}
}
}