From 96451477ee3eb23464aabed08484ccccbc2cfad4 Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Tue, 12 Apr 2022 00:26:01 +1000 Subject: [PATCH] Change ExplorerFolder path and refactor --- src/Config/ConfigManager.cs | 2 +- src/Config/InternalConfigHandler.cs | 2 +- src/ExplorerCore.cs | 66 +++++++++++++++++-- src/Loader/BepInEx/ExplorerBepInPlugin.cs | 7 +- src/Loader/IExplorerLoader.cs | 3 +- src/Loader/MelonLoader/ExplorerMelonMod.cs | 7 +- .../Standalone/Editor/ExplorerEditorLoader.cs | 6 +- src/Loader/Standalone/ExplorerStandalone.cs | 25 +++---- .../Standalone/StandaloneConfigHandler.cs | 6 +- src/UI/Panels/LogPanel.cs | 2 +- 10 files changed, 90 insertions(+), 36 deletions(-) diff --git a/src/Config/ConfigManager.cs b/src/Config/ConfigManager.cs index ecbc1e7..63ddd83 100644 --- a/src/Config/ConfigManager.cs +++ b/src/Config/ConfigManager.cs @@ -123,7 +123,7 @@ namespace UnityExplorer.Config Default_Output_Path = new ConfigElement("Default Output Path", "The default output path when exporting things from UnityExplorer.", - Path.Combine(ExplorerCore.Loader.ExplorerFolder, "Output")); + Path.Combine(ExplorerCore.ExplorerFolder, "Output")); Startup_Delay_Time = new ConfigElement("Startup Delay Time", "The delay on startup before the UI is created.", diff --git a/src/Config/InternalConfigHandler.cs b/src/Config/InternalConfigHandler.cs index 86cc527..e98fe1f 100644 --- a/src/Config/InternalConfigHandler.cs +++ b/src/Config/InternalConfigHandler.cs @@ -16,7 +16,7 @@ namespace UnityExplorer.Config public override void Init() { - CONFIG_PATH = Path.Combine(ExplorerCore.Loader.ExplorerFolder, "data.cfg"); + CONFIG_PATH = Path.Combine(ExplorerCore.ExplorerFolder, "data.cfg"); } public override void LoadConfig() diff --git a/src/ExplorerCore.cs b/src/ExplorerCore.cs index 87b9c9b..287cd8f 100644 --- a/src/ExplorerCore.cs +++ b/src/ExplorerCore.cs @@ -15,11 +15,13 @@ namespace UnityExplorer public static class ExplorerCore { public const string NAME = "UnityExplorer"; - public const string VERSION = "4.6.4"; + public const string VERSION = "4.7.0"; public const string AUTHOR = "Sinai"; public const string GUID = "com.sinai.unityexplorer"; public static IExplorerLoader Loader { get; private set; } + public static string ExplorerFolder => Path.Combine(Loader.ExplorerFolderDestination, Loader.ExplorerFolderName); + public const string DEFAULT_EXPLORER_FOLDER_NAME = "sinai-dev-UnityExplorer"; public static HarmonyLib.Harmony Harmony { get; } = new HarmonyLib.Harmony(GUID); @@ -35,7 +37,8 @@ namespace UnityExplorer Log($"{NAME} {VERSION} initializing..."); - Directory.CreateDirectory(Loader.ExplorerFolder); + CheckLegacyExplorerFolder(); + Directory.CreateDirectory(ExplorerFolder); ConfigManager.Init(Loader.ConfigHandler); UERuntimeHelper.Init(); @@ -64,8 +67,6 @@ namespace UnityExplorer UIManager.InitUI(); Log($"{NAME} {VERSION} ({Universe.Context}) initialized."); - - //InspectorManager.Inspect(typeof(Tests.TestClass)); } internal static void Update() @@ -73,6 +74,63 @@ namespace UnityExplorer // check master toggle if (InputManager.GetKeyDown(ConfigManager.Master_Toggle.Value)) UIManager.ShowMenu = !UIManager.ShowMenu; + + UIManager.Update(); + } + + // Can be removed eventually. For migration from <4.7.0 + static void CheckLegacyExplorerFolder() + { + string legacyPath = Path.Combine(Loader.ExplorerFolderDestination, "UnityExplorer"); + if (Directory.Exists(legacyPath)) + { + LogWarning($"Attempting to migrate old 'UnityExplorer/' folder to 'sinai-dev-UnityExplorer/'..."); + + // If new folder doesn't exist yet, let's just use Move(). + if (!Directory.Exists(ExplorerFolder)) + { + try + { + Directory.Move(legacyPath, ExplorerFolder); + Log("Migrated successfully."); + } + catch (Exception ex) + { + LogWarning($"Exception migrating folder: {ex}"); + } + } + else // We have to merge + { + try + { + CopyAll(new(legacyPath), new(ExplorerFolder)); + Directory.Delete(legacyPath, true); + Log("Migrated successfully."); + } + catch (Exception ex) + { + LogWarning($"Exception migrating folder: {ex}"); + } + } + } + } + + public static void CopyAll(DirectoryInfo source, DirectoryInfo target) + { + // Check if the target directory exists, if not, create it. + if (!Directory.Exists(target.FullName)) + Directory.CreateDirectory(target.FullName); + + // Copy each file into it's new directory. + foreach (FileInfo fi in source.GetFiles()) + fi.MoveTo(Path.Combine(target.ToString(), fi.Name)); + + // Copy each subdirectory using recursion. + foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) + { + DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); + CopyAll(diSourceSubDir, nextTargetSubDir); + } } #region LOGGING diff --git a/src/Loader/BepInEx/ExplorerBepInPlugin.cs b/src/Loader/BepInEx/ExplorerBepInPlugin.cs index 0c0ae6e..2cc1b26 100644 --- a/src/Loader/BepInEx/ExplorerBepInPlugin.cs +++ b/src/Loader/BepInEx/ExplorerBepInPlugin.cs @@ -44,9 +44,10 @@ namespace UnityExplorer private BepInExConfigHandler _configHandler; public Harmony HarmonyInstance => s_harmony; - private static readonly Harmony s_harmony = new Harmony(ExplorerCore.GUID); - - public string ExplorerFolder => Path.Combine(Paths.PluginPath, ExplorerCore.NAME); + private static readonly Harmony s_harmony = new(ExplorerCore.GUID); + + public string ExplorerFolderName => ExplorerCore.DEFAULT_EXPLORER_FOLDER_NAME; + public string ExplorerFolderDestination => Paths.PluginPath; public Action OnLogMessage => LogSource.LogMessage; public Action OnLogWarning => LogSource.LogWarning; diff --git a/src/Loader/IExplorerLoader.cs b/src/Loader/IExplorerLoader.cs index cc7c741..12fd357 100644 --- a/src/Loader/IExplorerLoader.cs +++ b/src/Loader/IExplorerLoader.cs @@ -8,7 +8,8 @@ namespace UnityExplorer { public interface IExplorerLoader { - string ExplorerFolder { get; } + string ExplorerFolderDestination { get; } + string ExplorerFolderName { get; } string UnhollowedModulesFolder { get; } ConfigHandler ConfigHandler { get; } diff --git a/src/Loader/MelonLoader/ExplorerMelonMod.cs b/src/Loader/MelonLoader/ExplorerMelonMod.cs index 87c1673..48862e2 100644 --- a/src/Loader/MelonLoader/ExplorerMelonMod.cs +++ b/src/Loader/MelonLoader/ExplorerMelonMod.cs @@ -20,9 +20,8 @@ namespace UnityExplorer { public class ExplorerMelonMod : MelonMod, IExplorerLoader { - public static ExplorerMelonMod Instance; - - public string ExplorerFolder => Path.Combine(MelonHandler.ModsDirectory, ExplorerCore.NAME); + public string ExplorerFolderName => ExplorerCore.DEFAULT_EXPLORER_FOLDER_NAME; + public string ExplorerFolderDestination => MelonHandler.ModsDirectory; public string UnhollowedModulesFolder => Path.Combine( Path.GetDirectoryName(MelonHandler.ModsDirectory), @@ -37,9 +36,7 @@ namespace UnityExplorer public override void OnApplicationStart() { - Instance = this; _configHandler = new MelonLoaderConfigHandler(); - ExplorerCore.Init(this); } } diff --git a/src/Loader/Standalone/Editor/ExplorerEditorLoader.cs b/src/Loader/Standalone/Editor/ExplorerEditorLoader.cs index 6ffe190..574729c 100644 --- a/src/Loader/Standalone/Editor/ExplorerEditorLoader.cs +++ b/src/Loader/Standalone/Editor/ExplorerEditorLoader.cs @@ -10,6 +10,8 @@ namespace UnityExplorer.Loader.Standalone { public class ExplorerEditorLoader : ExplorerStandalone { + public new string ExplorerFolderName => $"{ExplorerCore.DEFAULT_EXPLORER_FOLDER_NAME}~"; + public static void Initialize() { Instance = new ExplorerEditorLoader(); @@ -33,8 +35,8 @@ namespace UnityExplorer.Loader.Standalone protected override void CheckExplorerFolder() { - if (explorerFolder == null) - explorerFolder = Path.Combine(Application.dataPath, "UnityExplorer~"); + if (explorerFolderDest == null) + explorerFolderDest = Application.dataPath; } } } diff --git a/src/Loader/Standalone/ExplorerStandalone.cs b/src/Loader/Standalone/ExplorerStandalone.cs index cbe2951..e69d541 100644 --- a/src/Loader/Standalone/ExplorerStandalone.cs +++ b/src/Loader/Standalone/ExplorerStandalone.cs @@ -29,15 +29,16 @@ namespace UnityExplorer public ConfigHandler ConfigHandler => configHandler; internal StandaloneConfigHandler configHandler; - public string ExplorerFolder + public string ExplorerFolderName => ExplorerCore.DEFAULT_EXPLORER_FOLDER_NAME; + public string ExplorerFolderDestination { get { CheckExplorerFolder(); - return explorerFolder; + return explorerFolderDest; } } - protected static string explorerFolder; + protected static string explorerFolderDest; Action IExplorerLoader.OnLogMessage => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", LogType.Log); }; Action IExplorerLoader.OnLogWarning => (object log) => { OnLog?.Invoke(log?.ToString() ?? "", LogType.Warning); }; @@ -45,14 +46,14 @@ namespace UnityExplorer /// /// Call this to initialize UnityExplorer without adding a log listener or Unhollowed modules path. - /// The default Unhollowed path "UnityExplorer\Modules\" will be used. + /// The default Unhollowed path "sinai-dev-UnityExplorer\Modules\" will be used. /// /// The new (or active, if one exists) instance of ExplorerStandalone. public static ExplorerStandalone CreateInstance() => CreateInstance(null, null); /// /// 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. + /// The default Unhollowed path "sinai-dev-UnityExplorer\Modules\" will be used. /// /// Your log listener to handle UnityExplorer logs. /// The new (or active, if one exists) instance of ExplorerStandalone. @@ -77,7 +78,7 @@ namespace UnityExplorer OnLog += logListener; if (string.IsNullOrEmpty(unhollowedModulesPath) || !Directory.Exists(unhollowedModulesPath)) - instance.unhollowedPath = Path.Combine(instance.ExplorerFolder, "Modules"); + instance.unhollowedPath = Path.Combine(ExplorerCore.ExplorerFolder, "Modules"); else instance.unhollowedPath = unhollowedModulesPath; @@ -94,16 +95,10 @@ namespace UnityExplorer protected virtual void CheckExplorerFolder() { - if (explorerFolder == null) + if (explorerFolderDest == null) { - explorerFolder = - Path.Combine( - Path.GetDirectoryName( - Uri.UnescapeDataString(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath)), - "UnityExplorer"); - - if (!Directory.Exists(explorerFolder)) - Directory.CreateDirectory(explorerFolder); + string assemblyLocation = Uri.UnescapeDataString(new Uri(typeof(ExplorerCore).Assembly.CodeBase).AbsolutePath); + explorerFolderDest = Path.GetDirectoryName(assemblyLocation); } } } diff --git a/src/Loader/Standalone/StandaloneConfigHandler.cs b/src/Loader/Standalone/StandaloneConfigHandler.cs index cd00147..1e11d7d 100644 --- a/src/Loader/Standalone/StandaloneConfigHandler.cs +++ b/src/Loader/Standalone/StandaloneConfigHandler.cs @@ -17,7 +17,7 @@ namespace UnityExplorer.Loader.Standalone public override void Init() { - CONFIG_PATH = Path.Combine(ExplorerCore.Loader.ExplorerFolder, "config.cfg"); + CONFIG_PATH = Path.Combine(ExplorerCore.ExplorerFolder, "config.cfg"); } public override void LoadConfig() @@ -92,8 +92,8 @@ namespace UnityExplorer.Loader.Standalone foreach (var config in ConfigManager.ConfigElements) document.Put(config.Key, config.Value.BoxedValue.ToString()); - if (!Directory.Exists(ExplorerCore.Loader.ExplorerFolder)) - Directory.CreateDirectory(ExplorerCore.Loader.ExplorerFolder); + if (!Directory.Exists(ExplorerCore.ExplorerFolder)) + Directory.CreateDirectory(ExplorerCore.ExplorerFolder); File.WriteAllText(CONFIG_PATH, document.SerializedValue); } diff --git a/src/UI/Panels/LogPanel.cs b/src/UI/Panels/LogPanel.cs index 2b68374..e11bfe5 100644 --- a/src/UI/Panels/LogPanel.cs +++ b/src/UI/Panels/LogPanel.cs @@ -69,7 +69,7 @@ namespace UnityExplorer.UI.Panels { var fileName = $"UnityExplorer {DateTime.Now:u}.txt"; fileName = IOUtility.EnsureValidFilename(fileName); - var path = Path.Combine(ExplorerCore.Loader.ExplorerFolder, "Logs"); + var path = Path.Combine(ExplorerCore.ExplorerFolder, "Logs"); CurrentStreamPath = IOUtility.EnsureValidFilePath(Path.Combine(path, fileName)); // clean old log(s)