UnityExplorer/src/ExplorerCore.cs

155 lines
4.2 KiB
C#
Raw Normal View History

using System;
2020-11-23 18:23:25 +11:00
using System.IO;
using System.Reflection;
2020-11-23 18:23:25 +11:00
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityExplorer.Config;
using UnityExplorer.Helpers;
using UnityExplorer.Input;
2020-11-23 18:23:25 +11:00
using UnityExplorer.Inspectors;
using UnityExplorer.UI;
using UnityExplorer.UI.Modules;
2020-08-07 22:19:03 +10:00
namespace UnityExplorer
2020-08-07 22:19:03 +10:00
{
public class ExplorerCore
2020-08-07 22:19:03 +10:00
{
public const string NAME = "UnityExplorer";
2021-03-11 18:32:57 +11:00
public const string VERSION = "3.2.1";
public const string AUTHOR = "Sinai";
public const string GUID = "com.sinai.unityexplorer";
2021-01-02 19:38:01 +01:00
2021-03-11 17:57:58 +11:00
public static ExplorerCore Instance { get; private set; }
private static IExplorerLoader s_loader;
public static IExplorerLoader Loader => s_loader
2021-01-02 19:38:01 +01:00
#if ML
2021-03-11 17:57:58 +11:00
?? (s_loader = ExplorerMelonMod.Instance);
2021-01-02 19:38:01 +01:00
#elif BIE
2021-03-11 17:57:58 +11:00
?? (s_loader = ExplorerBepInPlugin.Instance);
#elif STANDALONE
2021-03-11 17:57:58 +11:00
?? (s_loader = ExplorerStandalone.Instance);
2021-01-02 19:38:01 +01:00
#endif
2021-03-11 17:57:58 +11:00
public static string ExplorerFolder => Loader.ExplorerFolder;
public ExplorerCore()
2020-08-07 22:19:03 +10:00
{
if (Instance != null)
{
Log("An instance of Explorer is already active!");
return;
}
2020-08-07 22:19:03 +10:00
Instance = this;
#if CPP
ReflectionHelpers.TryLoadGameModules();
#endif
2021-03-11 17:57:58 +11:00
if (!Directory.Exists(ExplorerFolder))
Directory.CreateDirectory(ExplorerFolder);
2021-03-11 17:57:58 +11:00
ExplorerConfig.OnLoad();
InputManager.Init();
2020-10-08 06:15:42 +11:00
ForceUnlockCursor.Init();
SetupEvents();
2021-03-11 17:57:58 +11:00
UIManager.ShowMenu = true;
2020-10-08 06:15:42 +11:00
Log($"{NAME} {VERSION} initialized.");
}
public static void Update()
2020-08-07 22:19:03 +10:00
{
2021-03-11 17:57:58 +11:00
UIManager.CheckUIInit();
if (MouseInspector.Enabled)
MouseInspector.UpdateInspect();
else
2021-03-11 17:57:58 +11:00
UIManager.Update();
}
private void SetupEvents()
{
#if CPP
try
{
Application.add_logMessageReceived(new Action<string, string, LogType>(OnUnityLog));
2021-03-11 17:57:58 +11:00
SceneManager.add_sceneLoaded(new Action<Scene, LoadSceneMode>((Scene a, LoadSceneMode b) => { OnSceneLoaded(); }));
SceneManager.add_activeSceneChanged(new Action<Scene, Scene>((Scene a, Scene b) => { OnSceneLoaded(); }));
}
2021-03-11 17:57:58 +11:00
catch (Exception ex)
{
LogWarning($"Exception setting up Unity event listeners!\r\n{ex}");
}
#else
Application.logMessageReceived += OnUnityLog;
SceneManager.sceneLoaded += (Scene a, LoadSceneMode b) => { OnSceneLoaded(); };
SceneManager.activeSceneChanged += (Scene a, Scene b) => { OnSceneLoaded(); };
#endif
}
internal void OnSceneLoaded()
{
UIManager.OnSceneChange();
}
private void OnUnityLog(string message, string stackTrace, LogType type)
{
if (!DebugConsole.LogUnity)
return;
message = $"[UNITY] {message}";
switch (type)
{
case LogType.Assert:
case LogType.Log:
Log(message, true);
break;
case LogType.Warning:
LogWarning(message, true);
break;
case LogType.Exception:
case LogType.Error:
LogError(message, true);
break;
}
}
public static void Log(object message, bool unity = false)
{
DebugConsole.Log(message?.ToString());
if (unity)
return;
2021-03-11 17:57:58 +11:00
Loader.OnLogMessage(message);
}
public static void LogWarning(object message, bool unity = false)
{
DebugConsole.Log(message?.ToString(), "FFFF00");
if (unity)
return;
2021-03-11 17:57:58 +11:00
Loader.OnLogWarning(message);
}
public static void LogError(object message, bool unity = false)
{
DebugConsole.Log(message?.ToString(), "FF0000");
if (unity)
return;
2021-03-11 17:57:58 +11:00
Loader.OnLogError(message);
}
2020-08-07 22:19:03 +10:00
}
}