UnityExplorer/src/ExplorerCore.cs
2022-03-05 07:32:32 +11:00

135 lines
3.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.Config;
using UnityExplorer.UI;
using UnityExplorer.ObjectExplorer;
using UnityExplorer.UI.Panels;
using UnityExplorer.Runtime;
using UniverseLib.Input;
using UniverseLib.UI;
using System.Reflection;
namespace UnityExplorer
{
public static class ExplorerCore
{
public const string NAME = "UnityExplorer";
public const string VERSION = "4.5.8";
public const string AUTHOR = "Sinai";
public const string GUID = "com.sinai.unityexplorer";
public static IExplorerLoader Loader { get; private set; }
public static HarmonyLib.Harmony Harmony { get; } = new HarmonyLib.Harmony(GUID);
/// <summary>
/// Initialize UnityExplorer with the provided Loader implementation.
/// </summary>
public static void Init(IExplorerLoader loader)
{
if (Loader != null)
{
LogWarning("UnityExplorer is already loaded!");
return;
}
Loader = loader;
Log($"{NAME} {VERSION} initializing...");
if (!Directory.Exists(Loader.ExplorerFolder))
Directory.CreateDirectory(Loader.ExplorerFolder);
ConfigManager.Init(Loader.ConfigHandler);
UERuntimeHelper.Init();
ExplorerBehaviour.Setup();
UnityCrashPrevention.Init();
UniverseLib.Universe.Init(ConfigManager.Startup_Delay_Time.Value, LateInit, Log, new()
{
Disable_EventSystem_Override = ConfigManager.Disable_EventSystem_Override.Value,
Force_Unlock_Mouse = ConfigManager.Force_Unlock_Mouse.Value,
Unhollowed_Modules_Folder = loader.UnhollowedModulesFolder
});
}
// Do a delayed setup so that objects aren't destroyed instantly.
// This can happen for a multitude of reasons.
// Default delay is 1 second which is usually enough.
private static void LateInit()
{
Log($"Setting up late core features...");
SceneHandler.Init();
Log($"Creating UI...");
UIManager.InitUI();
Log($"{NAME} {VERSION} initialized for {UniverseLib.Universe.Context}.");
//InspectorManager.Inspect(typeof(Tests.TestClass));
}
/// <summary>
/// Should be called once per frame.
/// </summary>
public static void Update()
{
UIManager.Update();
// check master toggle
if (InputManager.GetKeyDown(ConfigManager.Master_Toggle.Value))
UIManager.ShowMenu = !UIManager.ShowMenu;
}
#region LOGGING
public static void Log(object message)
=> Log(message, LogType.Log);
public static void LogWarning(object message)
=> Log(message, LogType.Warning);
public static void LogError(object message)
=> Log(message, LogType.Error);
public static void LogUnity(object message, LogType logType)
{
if (!ConfigManager.Log_Unity_Debug.Value)
return;
Log($"[Unity] {message}", logType);
}
private static void Log(object message, LogType logType)
{
string log = message?.ToString() ?? "";
LogPanel.Log(log, logType);
switch (logType)
{
case LogType.Assert:
case LogType.Log:
Loader.OnLogMessage(log);
break;
case LogType.Warning:
Loader.OnLogWarning(log);
break;
case LogType.Error:
case LogType.Exception:
Loader.OnLogError(log);
break;
}
}
#endregion
}
}