2021-03-11 17:57:58 +11:00
|
|
|
|
#if STANDALONE
|
|
|
|
|
using HarmonyLib;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
2021-03-11 18:16:52 +11:00
|
|
|
|
using UnityEngine;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
using UnityExplorer.Core.Config;
|
|
|
|
|
using UnityExplorer.Loader.STANDALONE;
|
2021-04-02 17:06:49 +11:00
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityExplorer.Core.Input;
|
|
|
|
|
using UnityExplorer.Core;
|
2021-03-11 18:16:52 +11:00
|
|
|
|
#if CPP
|
|
|
|
|
using UnhollowerRuntimeLib;
|
|
|
|
|
#endif
|
2021-03-11 17:57:58 +11:00
|
|
|
|
|
|
|
|
|
namespace UnityExplorer
|
|
|
|
|
{
|
|
|
|
|
public class ExplorerStandalone : IExplorerLoader
|
|
|
|
|
{
|
2021-03-11 18:16:52 +11:00
|
|
|
|
/// <summary>
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// Call this to initialize UnityExplorer without adding a log listener.
|
2021-03-11 18:16:52 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The new (or active, if one exists) instance of ExplorerStandalone.</returns>
|
2021-03-11 17:57:58 +11:00
|
|
|
|
public static ExplorerStandalone CreateInstance()
|
2021-03-30 19:50:04 +11:00
|
|
|
|
=> CreateInstance(null);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Call this to initialize UnityExplorer and add a listener for UnityExplorer's log messages.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logListener">Your log listener to handle UnityExplorer logs.</param>
|
|
|
|
|
/// <returns>The new (or active, if one exists) instance of ExplorerStandalone.</returns>
|
|
|
|
|
public static ExplorerStandalone CreateInstance(Action<string, LogType> logListener)
|
2021-03-11 17:57:58 +11:00
|
|
|
|
{
|
|
|
|
|
if (Instance != null)
|
|
|
|
|
return Instance;
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
OnLog += logListener;
|
2021-03-11 17:57:58 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var instance = new ExplorerStandalone();
|
|
|
|
|
instance.Init();
|
|
|
|
|
return instance;
|
2021-03-11 17:57:58 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ExplorerStandalone Instance { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked whenever Explorer logs something. Subscribe to this to handle logging.
|
|
|
|
|
/// </summary>
|
2021-03-11 18:16:52 +11:00
|
|
|
|
public static event Action<string, LogType> OnLog;
|
2021-03-11 17:57:58 +11:00
|
|
|
|
|
|
|
|
|
public Harmony HarmonyInstance => s_harmony;
|
|
|
|
|
public static readonly Harmony s_harmony = new Harmony(ExplorerCore.GUID);
|
|
|
|
|
|
2021-03-30 21:23:45 +11:00
|
|
|
|
public ConfigHandler ConfigHandler => _configHandler;
|
|
|
|
|
private StandaloneConfigHandler _configHandler;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
|
2021-03-11 17:57:58 +11:00
|
|
|
|
public string ExplorerFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (s_explorerFolder == null)
|
|
|
|
|
{
|
2021-03-11 18:40:04 +11:00
|
|
|
|
s_explorerFolder =
|
|
|
|
|
Path.Combine(
|
|
|
|
|
Path.GetDirectoryName(
|
|
|
|
|
Uri.UnescapeDataString(new Uri(Assembly.GetExecutingAssembly().CodeBase)
|
|
|
|
|
.AbsolutePath)),
|
|
|
|
|
"UnityExplorer");
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(s_explorerFolder))
|
|
|
|
|
Directory.CreateDirectory(s_explorerFolder);
|
2021-03-11 17:57:58 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s_explorerFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static string s_explorerFolder;
|
|
|
|
|
|
|
|
|
|
public string ConfigFolder => ExplorerFolder;
|
|
|
|
|
|
2021-03-11 18:16:52 +11:00
|
|
|
|
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); };
|
2021-03-11 17:57:58 +11:00
|
|
|
|
|
2021-03-11 18:16:52 +11:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
_configHandler = new StandaloneConfigHandler();
|
|
|
|
|
|
2021-03-11 18:16:52 +11:00
|
|
|
|
#if CPP
|
|
|
|
|
ClassInjector.RegisterTypeInIl2Cpp<ExplorerBehaviour>();
|
|
|
|
|
#endif
|
2021-04-02 17:06:49 +11:00
|
|
|
|
var obj = new GameObject("ExplorerBehaviour");
|
|
|
|
|
obj.AddComponent<ExplorerBehaviour>();
|
2021-03-11 18:16:52 +11:00
|
|
|
|
|
|
|
|
|
GameObject.DontDestroyOnLoad(obj);
|
2021-03-30 19:50:04 +11:00
|
|
|
|
obj.hideFlags = HideFlags.HideAndDontSave;
|
2021-03-11 18:16:52 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
ExplorerCore.Init(this);
|
2021-03-11 18:16:52 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ExplorerBehaviour : MonoBehaviour
|
2021-03-11 17:57:58 +11:00
|
|
|
|
{
|
2021-03-11 18:16:52 +11:00
|
|
|
|
#if CPP
|
|
|
|
|
public ExplorerBehaviour(IntPtr ptr) : base(ptr) { }
|
|
|
|
|
#endif
|
|
|
|
|
internal void Update()
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.Update();
|
|
|
|
|
}
|
2021-03-11 17:57:58 +11:00
|
|
|
|
}
|
2021-04-02 17:06:49 +11:00
|
|
|
|
|
|
|
|
|
public void SetupPatches()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this.HarmonyInstance.PatchAll();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.Log($"Exception setting up Harmony patches:\r\n{ex.ReflectionExToString()}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HarmonyPatch(typeof(EventSystem), "current", MethodType.Setter)]
|
|
|
|
|
public class PATCH_EventSystem_current
|
|
|
|
|
{
|
|
|
|
|
[HarmonyPrefix]
|
|
|
|
|
public static void Prefix_EventSystem_set_current(ref EventSystem value)
|
|
|
|
|
{
|
|
|
|
|
CursorUnlocker.Prefix_EventSystem_set_current(ref value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HarmonyPatch(typeof(Cursor), "lockState", MethodType.Setter)]
|
|
|
|
|
public class PATCH_Cursor_lockState
|
|
|
|
|
{
|
|
|
|
|
[HarmonyPrefix]
|
|
|
|
|
public static void Prefix_set_lockState(ref CursorLockMode value)
|
|
|
|
|
{
|
|
|
|
|
CursorUnlocker.Prefix_set_lockState(ref value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HarmonyPatch(typeof(Cursor), "visible", MethodType.Setter)]
|
|
|
|
|
public class PATCH_Cursor_visible
|
|
|
|
|
{
|
|
|
|
|
[HarmonyPrefix]
|
|
|
|
|
public static void Prefix_set_visible(ref bool value)
|
|
|
|
|
{
|
|
|
|
|
CursorUnlocker.Prefix_set_visible(ref value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 17:57:58 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|