Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 1a5e843

Browse files
committed
handle ExplorerStandalone update internally
1 parent ade7539 commit 1a5e843

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ Note: You must use version 0.3 of MelonLoader or greater. Version 0.3 is current
6969

7070
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.
7171
1. Create an instance of Unity Explorer with `ExplorerStandalone.CreateInstance();`
72-
2. You will need to call `ExplorerStandalone.Update()` from your Update method.
73-
3. Subscribe to the `ExplorerStandalone.OnLog` event to handle logging if you wish.
72+
2. Optionally subscribe to the `ExplorerStandalone.OnLog` event to handle logging if you wish.
7473

7574
## Logging
7675

@@ -115,6 +114,7 @@ For IL2CPP:
115114
1. Install BepInEx or MelonLoader for your game.
116115
2. Open the `src\UnityExplorer.csproj` file in a text editor.
117116
3. Set `BIECppGameFolder` (for BepInEx) and/or `MLCppGameFolder` (for MelonLoader) so the project can locate the necessary references.
117+
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.
118118

119119
For all builds:
120120
1. Open the `src\UnityExplorer.sln` project.

src/Loader/ExplorerBepIn6Plugin.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ internal void Awake()
3939
Instance = this;
4040

4141
new ExplorerCore();
42-
43-
// HarmonyInstance.PatchAll();
4442
}
4543

4644
internal void Update()
@@ -83,8 +81,6 @@ public override void Load()
8381
GameObject.DontDestroyOnLoad(obj);
8482

8583
new ExplorerCore();
86-
87-
// HarmonyInstance.PatchAll();
8884
}
8985

9086
// BepInEx Il2Cpp mod class doesn't have monobehaviour methods yet, so wrap them in a dummy.

src/Loader/ExplorerStandalone.cs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
using System;
44
using System.IO;
55
using System.Reflection;
6+
using UnityEngine;
7+
#if CPP
8+
using UnhollowerRuntimeLib;
9+
#endif
610

711
namespace UnityExplorer
812
{
913
public class ExplorerStandalone : IExplorerLoader
1014
{
15+
/// <summary>
16+
/// Call this to initialize UnityExplorer. Optionally, also subscribe to the 'OnLog' event to handle logging.
17+
/// </summary>
18+
/// <returns>The new (or active, if one exists) instance of ExplorerStandalone.</returns>
1119
public static ExplorerStandalone CreateInstance()
1220
{
1321
if (Instance != null)
@@ -18,16 +26,15 @@ public static ExplorerStandalone CreateInstance()
1826

1927
private ExplorerStandalone()
2028
{
21-
Instance = this;
22-
new ExplorerCore();
29+
Init();
2330
}
2431

2532
public static ExplorerStandalone Instance { get; private set; }
2633

2734
/// <summary>
2835
/// Invoked whenever Explorer logs something. Subscribe to this to handle logging.
2936
/// </summary>
30-
public static event Action<string, UnityEngine.LogType> OnLog;
37+
public static event Action<string, LogType> OnLog;
3138

3239
public Harmony HarmonyInstance => s_harmony;
3340
public static readonly Harmony s_harmony = new Harmony(ExplorerCore.GUID);
@@ -50,16 +57,42 @@ public string ExplorerFolder
5057

5158
public string ConfigFolder => ExplorerFolder;
5259

53-
Action<object> IExplorerLoader.OnLogMessage => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", UnityEngine.LogType.Log); };
54-
Action<object> IExplorerLoader.OnLogWarning => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", UnityEngine.LogType.Warning); };
55-
Action<object> IExplorerLoader.OnLogError => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", UnityEngine.LogType.Error); };
60+
Action<object> IExplorerLoader.OnLogMessage => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", LogType.Log); };
61+
Action<object> IExplorerLoader.OnLogWarning => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", LogType.Warning); };
62+
Action<object> IExplorerLoader.OnLogError => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", LogType.Error); };
5663

57-
/// <summary>
58-
/// Call this once per frame for Explorer to update.
59-
/// </summary>
60-
public static void Update()
64+
private void Init()
65+
{
66+
Instance = this;
67+
#if CPP
68+
ClassInjector.RegisterTypeInIl2Cpp<ExplorerBehaviour>();
69+
70+
var obj = new GameObject(
71+
"ExplorerBehaviour",
72+
new Il2CppSystem.Type[] { Il2CppType.Of<ExplorerBehaviour>() }
73+
);
74+
#else
75+
var obj = new GameObject(
76+
"ExplorerBehaviour",
77+
new Type[] { typeof(ExplorerBehaviour) }
78+
);
79+
#endif
80+
81+
obj.hideFlags = HideFlags.HideAndDontSave;
82+
GameObject.DontDestroyOnLoad(obj);
83+
84+
new ExplorerCore();
85+
}
86+
87+
public class ExplorerBehaviour : MonoBehaviour
6188
{
62-
ExplorerCore.Update();
89+
#if CPP
90+
public ExplorerBehaviour(IntPtr ptr) : base(ptr) { }
91+
#endif
92+
internal void Update()
93+
{
94+
ExplorerCore.Update();
95+
}
6396
}
6497
}
6598
}

0 commit comments

Comments
 (0)