UnityExplorer/src/Loader/Standalone/ExplorerStandalone.cs

111 lines
4.3 KiB
C#
Raw Normal View History

2021-03-11 17:57:58 +11:00
#if STANDALONE
using HarmonyLib;
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityExplorer.Config;
using UnityExplorer.Loader.STANDALONE;
using UnityEngine.EventSystems;
2021-12-02 18:35:46 +11:00
using UniverseLib.Input;
#if CPP
using UnhollowerRuntimeLib;
#endif
2021-03-11 17:57:58 +11:00
namespace UnityExplorer
{
public class ExplorerStandalone : IExplorerLoader
{
/// <summary>
/// Call this to initialize UnityExplorer without adding a log listener or Unhollowed modules path.
/// The default Unhollowed path "UnityExplorer\Modules\" will be used.
/// </summary>
/// <returns>The new (or active, if one exists) instance of ExplorerStandalone.</returns>
public static ExplorerStandalone CreateInstance() => CreateInstance(null, null);
/// <summary>
/// Call this to initialize UnityExplorer and add a listener for UnityExplorer's log messages, without specifying an Unhollowed modules path.
/// The default Unhollowed path "UnityExplorer\Modules\" will be used.
/// </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) => CreateInstance(logListener, null);
/// <summary>
/// Call this to initialize UnityExplorer with the provided log listener and Unhollowed modules path.
/// </summary>
/// <param name="logListener">Your log listener to handle UnityExplorer logs.</param>
/// <param name="unhollowedModulesPath">The path of the Unhollowed modules, either relative or absolute.</param>
/// <returns>The new (or active, if one exists) instance of ExplorerStandalone.</returns>
public static ExplorerStandalone CreateInstance(Action<string, LogType> logListener, string unhollowedModulesPath)
2021-03-11 17:57:58 +11:00
{
if (Instance != null)
return Instance;
var instance = new ExplorerStandalone();
instance.Init();
instance.CheckExplorerFolder();
2021-07-21 04:13:42 +10:00
if (logListener != null)
OnLog += logListener;
2021-03-11 17:57:58 +11:00
if (string.IsNullOrEmpty(unhollowedModulesPath) || !Directory.Exists(unhollowedModulesPath))
instance._unhollowedPath = Path.Combine(instance.ExplorerFolder, "Modules");
else
instance._unhollowedPath = unhollowedModulesPath;
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>
public static event Action<string, LogType> OnLog;
2021-03-11 17:57:58 +11:00
public string UnhollowedModulesFolder => _unhollowedPath;
private string _unhollowedPath;
public ConfigHandler ConfigHandler => _configHandler;
private StandaloneConfigHandler _configHandler;
2021-03-11 17:57:58 +11:00
public string ExplorerFolder
{
get
{
2021-07-21 04:13:42 +10:00
CheckExplorerFolder();
2021-03-11 17:57:58 +11:00
return s_explorerFolder;
}
}
private static string s_explorerFolder;
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
private void Init()
{
Instance = this;
_configHandler = new StandaloneConfigHandler();
ExplorerCore.Init(this);
}
2021-07-21 04:13:42 +10:00
private void CheckExplorerFolder()
{
2021-07-21 04:13:42 +10:00
if (s_explorerFolder == null)
{
2021-07-21 04:13:42 +10:00
s_explorerFolder =
Path.Combine(
Path.GetDirectoryName(
Uri.UnescapeDataString(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath)),
"UnityExplorer");
2021-07-21 04:13:42 +10:00
if (!Directory.Exists(s_explorerFolder))
Directory.CreateDirectory(s_explorerFolder);
}
}
2021-03-11 17:57:58 +11:00
}
}
#endif