From 3501a28fd16f0c76b589b0f4a1754442799d2d5c Mon Sep 17 00:00:00 2001 From: Sinai Date: Tue, 30 Mar 2021 21:23:45 +1100 Subject: [PATCH] Restore UnlockMouse config, adjust config saving --- src/Core/Config/ConfigHandler.cs | 24 +++++++++++ src/Core/Config/ConfigManager.cs | 21 ++++++---- src/Core/Config/IConfigHandler.cs | 22 ---------- src/Core/Input/CursorUnlocker.cs | 41 ++++++++----------- src/Loader/BIE/BepInExConfigHandler.cs | 14 +++---- src/Loader/BIE/ExplorerBepInPlugin.cs | 2 +- src/Loader/IExplorerLoader.cs | 2 +- src/Loader/ML/ExplorerMelonMod.cs | 3 +- src/Loader/ML/MelonLoaderConfigHandler.cs | 17 ++++---- src/Loader/STANDALONE/ExplorerStandalone.cs | 4 +- .../STANDALONE/StandaloneConfigHandler.cs | 19 +++++---- src/UI/CacheObject/CacheConfigEntry.cs | 12 +++++- src/UI/Main/Options/OptionsPage.cs | 25 ++++++----- src/UnityExplorer.csproj | 2 +- 14 files changed, 114 insertions(+), 94 deletions(-) create mode 100644 src/Core/Config/ConfigHandler.cs delete mode 100644 src/Core/Config/IConfigHandler.cs diff --git a/src/Core/Config/ConfigHandler.cs b/src/Core/Config/ConfigHandler.cs new file mode 100644 index 0000000..70c79ee --- /dev/null +++ b/src/Core/Config/ConfigHandler.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UnityExplorer.Core.Config +{ + public abstract class ConfigHandler + { + public abstract void RegisterConfigElement(ConfigElement element); + + public abstract void SetConfigValue(ConfigElement element, T value); + + public abstract T GetConfigValue(ConfigElement element); + + public abstract void Init(); + + public abstract void LoadConfig(); + + public abstract void SaveConfig(); + + public virtual void OnAnyConfigChanged() { } + } +} diff --git a/src/Core/Config/ConfigManager.cs b/src/Core/Config/ConfigManager.cs index 4ee253a..bcedc34 100644 --- a/src/Core/Config/ConfigManager.cs +++ b/src/Core/Config/ConfigManager.cs @@ -12,15 +12,17 @@ namespace UnityExplorer.Core.Config { public static class ConfigManager { - // Each Loader has its own ConfigHandler. + // Each Mod Loader has its own ConfigHandler. // See the UnityExplorer.Loader namespace for the implementations. - public static IConfigHandler Handler { get; private set; } + public static ConfigHandler Handler { get; private set; } public static ConfigElement Main_Menu_Toggle; + public static ConfigElement Force_Unlock_Mouse; public static ConfigElement Default_Page_Limit; public static ConfigElement Default_Output_Path; public static ConfigElement Log_Unity_Debug; public static ConfigElement Hide_On_Startup; + public static ConfigElement Last_Window_Anchors; public static ConfigElement Last_Active_Tab; public static ConfigElement Last_DebugConsole_State; @@ -28,7 +30,7 @@ namespace UnityExplorer.Core.Config internal static readonly Dictionary ConfigElements = new Dictionary(); - public static void Init(IConfigHandler configHandler) + public static void Init(ConfigHandler configHandler) { Handler = configHandler; Handler.Init(); @@ -56,6 +58,11 @@ namespace UnityExplorer.Core.Config KeyCode.F7, false); + Force_Unlock_Mouse = new ConfigElement("Force Unlock Mouse", + "Force the Cursor to be unlocked (visible) when the UnityExplorer menu is open.", + true, + false); + Default_Page_Limit = new ConfigElement("Default Page Limit", "The default maximum number of elements per 'page' in UnityExplorer.", 25, @@ -102,25 +109,25 @@ namespace UnityExplorer.Core.Config private static void PanelDragger_OnFinishResize(RectTransform rect) { Last_Window_Anchors.Value = RectAnchorsToString(rect); - Handler.SaveConfig(); + Handler.OnAnyConfigChanged(); } private static void MainMenu_OnActiveTabChanged(int page) { Last_Active_Tab.Value = page; - Handler.SaveConfig(); + Handler.OnAnyConfigChanged(); } private static void DebugConsole_OnToggleShow(bool showing) { Last_DebugConsole_State.Value = showing; - Handler.SaveConfig(); + Handler.OnAnyConfigChanged(); } private static void SceneExplorer_OnToggleShow(bool showing) { Last_SceneExplorer_State.Value = showing; - Handler.SaveConfig(); + Handler.OnAnyConfigChanged(); } // Window Anchors helpers diff --git a/src/Core/Config/IConfigHandler.cs b/src/Core/Config/IConfigHandler.cs deleted file mode 100644 index 667034f..0000000 --- a/src/Core/Config/IConfigHandler.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace UnityExplorer.Core.Config -{ - public interface IConfigHandler - { - void RegisterConfigElement(ConfigElement element); - - void SetConfigValue(ConfigElement element, T value); - - T GetConfigValue(ConfigElement element); - - void Init(); - - void LoadConfig(); - - void SaveConfig(); - } -} diff --git a/src/Core/Input/CursorUnlocker.cs b/src/Core/Input/CursorUnlocker.cs index a3de30a..bfc6bf1 100644 --- a/src/Core/Input/CursorUnlocker.cs +++ b/src/Core/Input/CursorUnlocker.cs @@ -17,20 +17,18 @@ namespace UnityExplorer.Core.Input { public class CursorUnlocker { - //public static bool Unlock - //{ - // get => m_forceUnlock; - // set => SetForceUnlock(value); - //} - //private static bool m_forceUnlock; + public static bool Unlock + { + get => m_forceUnlock; + set + { + m_forceUnlock = value; + UpdateCursorControl(); + } + } + private static bool m_forceUnlock; - //private static void SetForceUnlock(bool unlock) - //{ - // m_forceUnlock = unlock; - // UpdateCursorControl(); - //} - - //public static bool ShouldForceMouse => UIManager.ShowMenu && Unlock; + public static bool ShouldActuallyUnlock => UIManager.ShowMenu && Unlock; private static CursorLockMode m_lastLockMode; private static bool m_lastVisibleState; @@ -48,7 +46,8 @@ namespace UnityExplorer.Core.Input UpdateCursorControl(); - //Unlock = true; + Unlock = true; + ConfigManager.Force_Unlock_Mouse.OnValueChanged += (bool val) => { Unlock = val; }; } private static void SetupPatches() @@ -122,7 +121,7 @@ namespace UnityExplorer.Core.Input try { m_currentlySettingCursor = true; - if (UIManager.ShowMenu) + if (ShouldActuallyUnlock) { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; @@ -196,10 +195,8 @@ namespace UnityExplorer.Core.Input m_lastEventSystem = value; m_lastInputModule = value?.currentInputModule; - if (UIManager.ShowMenu) - { + if (ShouldActuallyUnlock) value = UIManager.EventSys; - } } } @@ -214,10 +211,8 @@ namespace UnityExplorer.Core.Input { m_lastLockMode = value; - if (UIManager.ShowMenu) - { + if (ShouldActuallyUnlock) value = CursorLockMode.None; - } } } @@ -228,10 +223,8 @@ namespace UnityExplorer.Core.Input { m_lastVisibleState = value; - if (UIManager.ShowMenu) - { + if (ShouldActuallyUnlock) value = true; - } } } } diff --git a/src/Loader/BIE/BepInExConfigHandler.cs b/src/Loader/BIE/BepInExConfigHandler.cs index a47399f..0072817 100644 --- a/src/Loader/BIE/BepInExConfigHandler.cs +++ b/src/Loader/BIE/BepInExConfigHandler.cs @@ -8,18 +8,18 @@ using UnityExplorer.Core.Config; namespace UnityExplorer.Loader.BIE { - public class BepInExConfigHandler : IConfigHandler + public class BepInExConfigHandler : ConfigHandler { private ConfigFile Config => ExplorerBepInPlugin.Instance.Config; private const string CTG_NAME = "UnityExplorer"; - public void Init() + public override void Init() { // Not necessary } - public void RegisterConfigElement(ConfigElement config) + public override void RegisterConfigElement(ConfigElement config) { var entry = Config.Bind(CTG_NAME, config.Name, config.Value, config.Description); @@ -29,7 +29,7 @@ namespace UnityExplorer.Loader.BIE }; } - public T GetConfigValue(ConfigElement element) + public override T GetConfigValue(ConfigElement element) { if (Config.TryGetEntry(CTG_NAME, element.Name, out ConfigEntry configEntry)) return configEntry.Value; @@ -37,7 +37,7 @@ namespace UnityExplorer.Loader.BIE throw new Exception("Could not get config entry '" + element.Name + "'"); } - public void SetConfigValue(ConfigElement element, T value) + public override void SetConfigValue(ConfigElement element, T value) { if (Config.TryGetEntry(CTG_NAME, element.Name, out ConfigEntry configEntry)) configEntry.Value = value; @@ -45,7 +45,7 @@ namespace UnityExplorer.Loader.BIE ExplorerCore.Log("Could not get config entry '" + element.Name + "'"); } - public void LoadConfig() + public override void LoadConfig() { foreach (var entry in ConfigManager.ConfigElements) { @@ -59,7 +59,7 @@ namespace UnityExplorer.Loader.BIE } } - public void SaveConfig() + public override void SaveConfig() { // not required } diff --git a/src/Loader/BIE/ExplorerBepInPlugin.cs b/src/Loader/BIE/ExplorerBepInPlugin.cs index 0a8127f..cc56a8f 100644 --- a/src/Loader/BIE/ExplorerBepInPlugin.cs +++ b/src/Loader/BIE/ExplorerBepInPlugin.cs @@ -36,7 +36,7 @@ namespace UnityExplorer => Log; #endif - public IConfigHandler ConfigHandler => _configHandler; + public ConfigHandler ConfigHandler => _configHandler; private BepInExConfigHandler _configHandler; public Harmony HarmonyInstance => s_harmony; diff --git a/src/Loader/IExplorerLoader.cs b/src/Loader/IExplorerLoader.cs index 9b7b406..f8b8e18 100644 --- a/src/Loader/IExplorerLoader.cs +++ b/src/Loader/IExplorerLoader.cs @@ -11,7 +11,7 @@ namespace UnityExplorer string ExplorerFolder { get; } string ConfigFolder { get; } - IConfigHandler ConfigHandler { get; } + ConfigHandler ConfigHandler { get; } Action OnLogMessage { get; } Action OnLogWarning { get; } diff --git a/src/Loader/ML/ExplorerMelonMod.cs b/src/Loader/ML/ExplorerMelonMod.cs index 5599ef1..ae40b77 100644 --- a/src/Loader/ML/ExplorerMelonMod.cs +++ b/src/Loader/ML/ExplorerMelonMod.cs @@ -2,6 +2,7 @@ using System; using System.IO; using MelonLoader; +using UnityEngine; using UnityExplorer; using UnityExplorer.Core.Config; using UnityExplorer.Loader.ML; @@ -18,7 +19,7 @@ namespace UnityExplorer public string ExplorerFolder => Path.Combine("Mods", ExplorerCore.NAME); public string ConfigFolder => ExplorerFolder; - public IConfigHandler ConfigHandler => _configHandler; + public ConfigHandler ConfigHandler => _configHandler; public MelonLoaderConfigHandler _configHandler; public Action OnLogMessage => MelonLogger.Msg; diff --git a/src/Loader/ML/MelonLoaderConfigHandler.cs b/src/Loader/ML/MelonLoaderConfigHandler.cs index d00a81a..73bc632 100644 --- a/src/Loader/ML/MelonLoaderConfigHandler.cs +++ b/src/Loader/ML/MelonLoaderConfigHandler.cs @@ -10,20 +10,20 @@ using UnityExplorer.Core.Config; namespace UnityExplorer.Loader.ML { - public class MelonLoaderConfigHandler : IConfigHandler + public class MelonLoaderConfigHandler : ConfigHandler { internal const string CTG_NAME = "UnityExplorer"; internal MelonPreferences_Category prefCategory; - public void Init() + public override void Init() { prefCategory = MelonPreferences.CreateCategory(CTG_NAME, $"{CTG_NAME} Settings"); MelonPreferences.Mapper.RegisterMapper(KeycodeReader, KeycodeWriter); } - public void LoadConfig() + public override void LoadConfig() { foreach (var entry in ConfigManager.ConfigElements) { @@ -36,7 +36,7 @@ namespace UnityExplorer.Loader.ML } } - public void RegisterConfigElement(ConfigElement config) + public override void RegisterConfigElement(ConfigElement config) { var entry = prefCategory.CreateEntry(config.Name, config.Value, null, config.IsInternal) as MelonPreferences_Entry; @@ -49,17 +49,16 @@ namespace UnityExplorer.Loader.ML }; } - public void SetConfigValue(ConfigElement config, T value) + public override void SetConfigValue(ConfigElement config, T value) { if (prefCategory.GetEntry(config.Name) is MelonPreferences_Entry entry) { entry.Value = value; entry.Save(); - MelonPreferences.Save(); } } - public T GetConfigValue(ConfigElement config) + public override T GetConfigValue(ConfigElement config) { if (prefCategory.GetEntry(config.Name) is MelonPreferences_Entry entry) return entry.Value; @@ -67,9 +66,9 @@ namespace UnityExplorer.Loader.ML return default; } - public void SaveConfig() + public override void SaveConfig() { - // Not necessary + MelonPreferences.Save(); } public static KeyCode KeycodeReader(TomlObject value) diff --git a/src/Loader/STANDALONE/ExplorerStandalone.cs b/src/Loader/STANDALONE/ExplorerStandalone.cs index 48a495e..1657040 100644 --- a/src/Loader/STANDALONE/ExplorerStandalone.cs +++ b/src/Loader/STANDALONE/ExplorerStandalone.cs @@ -48,8 +48,8 @@ namespace UnityExplorer public Harmony HarmonyInstance => s_harmony; public static readonly Harmony s_harmony = new Harmony(ExplorerCore.GUID); - public IConfigHandler ConfigHandler => _configHandler; - private IConfigHandler _configHandler; + public ConfigHandler ConfigHandler => _configHandler; + private StandaloneConfigHandler _configHandler; public string ExplorerFolder { diff --git a/src/Loader/STANDALONE/StandaloneConfigHandler.cs b/src/Loader/STANDALONE/StandaloneConfigHandler.cs index 5c9159d..e96a480 100644 --- a/src/Loader/STANDALONE/StandaloneConfigHandler.cs +++ b/src/Loader/STANDALONE/StandaloneConfigHandler.cs @@ -10,36 +10,36 @@ using UnityEngine; namespace UnityExplorer.Loader.STANDALONE { - public class StandaloneConfigHandler : IConfigHandler + public class StandaloneConfigHandler : ConfigHandler { internal static IniDataParser _parser; internal static string INI_PATH; - public void Init() + public override void Init() { INI_PATH = Path.Combine(ExplorerCore.Loader.ConfigFolder, "config.ini"); _parser = new IniDataParser(); _parser.Configuration.CommentString = "#"; } - public void LoadConfig() + public override void LoadConfig() { if (!TryLoadConfig()) SaveConfig(); } - public void RegisterConfigElement(ConfigElement element) + public override void RegisterConfigElement(ConfigElement element) { // Not necessary } - public void SetConfigValue(ConfigElement element, T value) + public override void SetConfigValue(ConfigElement element, T value) { // Not necessary, just save. SaveConfig(); } - public T GetConfigValue(ConfigElement element) + public override T GetConfigValue(ConfigElement element) { // Not necessary, just return the value. return element.Value; @@ -82,7 +82,12 @@ namespace UnityExplorer.Loader.STANDALONE return value; } - public void SaveConfig() + public override void OnAnyConfigChanged() + { + SaveConfig(); + } + + public override void SaveConfig() { var data = new IniParser.Model.IniData(); diff --git a/src/UI/CacheObject/CacheConfigEntry.cs b/src/UI/CacheObject/CacheConfigEntry.cs index 518daf6..afdd753 100644 --- a/src/UI/CacheObject/CacheConfigEntry.cs +++ b/src/UI/CacheObject/CacheConfigEntry.cs @@ -49,7 +49,7 @@ namespace UnityExplorer.UI.CacheObject public override void SetValue() { RefConfig.BoxedValue = IValue.Value; - ConfigManager.Handler.SaveConfig(); + ConfigManager.Handler.OnAnyConfigChanged(); } internal GameObject m_leftGroup; @@ -59,7 +59,9 @@ namespace UnityExplorer.UI.CacheObject { base.ConstructUI(); - var horiGroup = UIFactory.CreateHorizontalGroup(m_mainContent, "ConfigEntryHolder", true, false, true, true, 5, new Vector4(2,2,2,2)); + var vertGroup = UIFactory.CreateVerticalGroup(m_mainContent, "ConfigHolder", true, false, true, true, 5, new Vector4(2, 2, 2, 2)); + + var horiGroup = UIFactory.CreateHorizontalGroup(vertGroup, "ConfigEntryHolder", true, false, true, true); UIFactory.SetLayoutElement(horiGroup, minHeight: 30, flexibleHeight: 0); // left group @@ -89,6 +91,12 @@ namespace UnityExplorer.UI.CacheObject IValue.m_mainContentParent = m_rightGroup; IValue.m_subContentParent = this.m_subContent; } + + // Config description label + + UIFactory.CreateLabel(vertGroup, "Description", $"{RefConfig.Description}", TextAnchor.MiddleLeft, Color.grey); + + m_subContent.transform.SetAsLastSibling(); } } } diff --git a/src/UI/Main/Options/OptionsPage.cs b/src/UI/Main/Options/OptionsPage.cs index ee49591..3378920 100644 --- a/src/UI/Main/Options/OptionsPage.cs +++ b/src/UI/Main/Options/OptionsPage.cs @@ -19,13 +19,6 @@ namespace UnityExplorer.UI.Main.Options { ConstructUI(); - _cachedConfigEntries.AddRange(ConfigManager.ConfigElements.Values - .Where(it => !it.IsInternal) - .Select(it => new CacheConfigEntry(it, m_contentObj))); - - foreach (var entry in _cachedConfigEntries) - entry.Enable(); - return true; } @@ -51,14 +44,26 @@ namespace UnityExplorer.UI.Main.Options var titleLabel = UIFactory.CreateLabel(Content, "Title", "Options", TextAnchor.UpperLeft, default, true, 25); UIFactory.SetLayoutElement(titleLabel.gameObject, minHeight: 30, flexibleHeight: 0); + // Save button + + var btn = UIFactory.CreateButton(Content, + "SaveButton", + "Save Config File", + () => { ConfigManager.Handler.SaveConfig(); }, + new Color(0.25f, 0.6f, 0.25f)); + UIFactory.SetLayoutElement(btn.gameObject, flexibleWidth: 9999, minHeight: 30, flexibleHeight: 0); + // ~~~~~ Actual options ~~~~~ UIFactory.CreateScrollView(Content, "ConfigList", out m_contentObj, out _, new Color(0.05f, 0.05f, 0.05f)); UIFactory.SetLayoutGroup(m_contentObj, forceHeight: true, spacing: 3, padLeft: 3, padRight: 3); - //m_contentObj = UIFactory.CreateVerticalGroup(Content, "OptionsGroup", true, false, true, false, 5, new Vector4(5,5,5,5), - // new Color(0.1f, 0.1f, 0.1f)); - //UIFactory.SetLayoutElement(m_contentObj, minHeight: 340, flexibleHeight: 9999); + _cachedConfigEntries.AddRange(ConfigManager.ConfigElements.Values + .Where(it => !it.IsInternal) + .Select(it => new CacheConfigEntry(it, m_contentObj))); + + foreach (var entry in _cachedConfigEntries) + entry.Enable(); } diff --git a/src/UnityExplorer.csproj b/src/UnityExplorer.csproj index 99ebc23..e82f549 100644 --- a/src/UnityExplorer.csproj +++ b/src/UnityExplorer.csproj @@ -218,7 +218,7 @@ - +