Files
UnityExplorer/src/Loader/Standalone/ExplorerStandalone.cs
2022-04-12 00:26:01 +10:00

106 lines
4.3 KiB
C#

#if STANDALONE
using HarmonyLib;
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityExplorer.Config;
using UnityEngine.EventSystems;
using UniverseLib.Input;
using UnityExplorer.Loader.Standalone;
#if CPP
using UnhollowerRuntimeLib;
#endif
namespace UnityExplorer
{
public class ExplorerStandalone : IExplorerLoader
{
public static ExplorerStandalone Instance { get; protected set; }
/// <summary>
/// Invoked whenever Explorer logs something. Subscribe to this to handle logging.
/// </summary>
public static event Action<string, LogType> OnLog;
public string UnhollowedModulesFolder => unhollowedPath;
private string unhollowedPath;
public ConfigHandler ConfigHandler => configHandler;
internal StandaloneConfigHandler configHandler;
public string ExplorerFolderName => ExplorerCore.DEFAULT_EXPLORER_FOLDER_NAME;
public string ExplorerFolderDestination
{
get
{
CheckExplorerFolder();
return explorerFolderDest;
}
}
protected static string explorerFolderDest;
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 to initialize UnityExplorer without adding a log listener or Unhollowed modules path.
/// The default Unhollowed path "sinai-dev-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 "sinai-dev-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)
{
if (Instance != null)
return Instance;
var instance = new ExplorerStandalone();
instance.Init();
instance.CheckExplorerFolder();
if (logListener != null)
OnLog += logListener;
if (string.IsNullOrEmpty(unhollowedModulesPath) || !Directory.Exists(unhollowedModulesPath))
instance.unhollowedPath = Path.Combine(ExplorerCore.ExplorerFolder, "Modules");
else
instance.unhollowedPath = unhollowedModulesPath;
return instance;
}
internal void Init()
{
Instance = this;
configHandler = new StandaloneConfigHandler();
ExplorerCore.Init(this);
}
protected virtual void CheckExplorerFolder()
{
if (explorerFolderDest == null)
{
string assemblyLocation = Uri.UnescapeDataString(new Uri(typeof(ExplorerCore).Assembly.CodeBase).AbsolutePath);
explorerFolderDest = Path.GetDirectoryName(assemblyLocation);
}
}
}
}
#endif