handle ExplorerStandalone update internally

This commit is contained in:
Sinai 2021-03-11 18:16:52 +11:00
parent ade7539fde
commit 1a5e843070
3 changed files with 46 additions and 17 deletions

View File

@ -69,8 +69,7 @@ Note: You must use version 0.3 of MelonLoader or greater. Version 0.3 is current
0. Load the DLL from your mod or inject it. You must also make sure that the required libraries (Harmony, Unhollower for Il2Cpp, etc) are loaded.
1. Create an instance of Unity Explorer with `ExplorerStandalone.CreateInstance();`
2. You will need to call `ExplorerStandalone.Update()` from your Update method.
3. Subscribe to the `ExplorerStandalone.OnLog` event to handle logging if you wish.
2. Optionally subscribe to the `ExplorerStandalone.OnLog` event to handle logging if you wish.
## Logging
@ -115,6 +114,7 @@ For IL2CPP:
1. Install BepInEx or MelonLoader for your game.
2. Open the `src\UnityExplorer.csproj` file in a text editor.
3. Set `BIECppGameFolder` (for BepInEx) and/or `MLCppGameFolder` (for MelonLoader) so the project can locate the necessary references.
4. For Standalone builds, you can either install BepInEx for the game to build, or just change the .csproj file and set the Unhollower reference manually.
For all builds:
1. Open the `src\UnityExplorer.sln` project.

View File

@ -39,8 +39,6 @@ namespace UnityExplorer
Instance = this;
new ExplorerCore();
// HarmonyInstance.PatchAll();
}
internal void Update()
@ -83,8 +81,6 @@ namespace UnityExplorer
GameObject.DontDestroyOnLoad(obj);
new ExplorerCore();
// HarmonyInstance.PatchAll();
}
// BepInEx Il2Cpp mod class doesn't have monobehaviour methods yet, so wrap them in a dummy.

View File

@ -3,11 +3,19 @@ using HarmonyLib;
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
#if CPP
using UnhollowerRuntimeLib;
#endif
namespace UnityExplorer
{
public class ExplorerStandalone : IExplorerLoader
{
/// <summary>
/// Call this to initialize UnityExplorer. Optionally, also subscribe to the 'OnLog' event to handle logging.
/// </summary>
/// <returns>The new (or active, if one exists) instance of ExplorerStandalone.</returns>
public static ExplorerStandalone CreateInstance()
{
if (Instance != null)
@ -18,8 +26,7 @@ namespace UnityExplorer
private ExplorerStandalone()
{
Instance = this;
new ExplorerCore();
Init();
}
public static ExplorerStandalone Instance { get; private set; }
@ -27,7 +34,7 @@ namespace UnityExplorer
/// <summary>
/// Invoked whenever Explorer logs something. Subscribe to this to handle logging.
/// </summary>
public static event Action<string, UnityEngine.LogType> OnLog;
public static event Action<string, LogType> OnLog;
public Harmony HarmonyInstance => s_harmony;
public static readonly Harmony s_harmony = new Harmony(ExplorerCore.GUID);
@ -50,16 +57,42 @@ namespace UnityExplorer
public string ConfigFolder => ExplorerFolder;
Action<object> IExplorerLoader.OnLogMessage => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", UnityEngine.LogType.Log); };
Action<object> IExplorerLoader.OnLogWarning => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", UnityEngine.LogType.Warning); };
Action<object> IExplorerLoader.OnLogError => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", UnityEngine.LogType.Error); };
Action<object> IExplorerLoader.OnLogMessage => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", LogType.Log); };
Action<object> IExplorerLoader.OnLogWarning => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", LogType.Warning); };
Action<object> IExplorerLoader.OnLogError => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", LogType.Error); };
/// <summary>
/// Call this once per frame for Explorer to update.
/// </summary>
public static void Update()
private void Init()
{
ExplorerCore.Update();
Instance = this;
#if CPP
ClassInjector.RegisterTypeInIl2Cpp<ExplorerBehaviour>();
var obj = new GameObject(
"ExplorerBehaviour",
new Il2CppSystem.Type[] { Il2CppType.Of<ExplorerBehaviour>() }
);
#else
var obj = new GameObject(
"ExplorerBehaviour",
new Type[] { typeof(ExplorerBehaviour) }
);
#endif
obj.hideFlags = HideFlags.HideAndDontSave;
GameObject.DontDestroyOnLoad(obj);
new ExplorerCore();
}
public class ExplorerBehaviour : MonoBehaviour
{
#if CPP
public ExplorerBehaviour(IntPtr ptr) : base(ptr) { }
#endif
internal void Update()
{
ExplorerCore.Update();
}
}
}
}