Restore UnlockMouse config, adjust config saving

This commit is contained in:
Sinai 2021-03-30 21:23:45 +11:00
parent 40f698122d
commit 3501a28fd1
14 changed files with 114 additions and 94 deletions

View File

@ -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<T>(ConfigElement<T> element);
public abstract void SetConfigValue<T>(ConfigElement<T> element, T value);
public abstract T GetConfigValue<T>(ConfigElement<T> element);
public abstract void Init();
public abstract void LoadConfig();
public abstract void SaveConfig();
public virtual void OnAnyConfigChanged() { }
}
}

View File

@ -12,15 +12,17 @@ namespace UnityExplorer.Core.Config
{ {
public static class ConfigManager 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. // 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<KeyCode> Main_Menu_Toggle; public static ConfigElement<KeyCode> Main_Menu_Toggle;
public static ConfigElement<bool> Force_Unlock_Mouse;
public static ConfigElement<int> Default_Page_Limit; public static ConfigElement<int> Default_Page_Limit;
public static ConfigElement<string> Default_Output_Path; public static ConfigElement<string> Default_Output_Path;
public static ConfigElement<bool> Log_Unity_Debug; public static ConfigElement<bool> Log_Unity_Debug;
public static ConfigElement<bool> Hide_On_Startup; public static ConfigElement<bool> Hide_On_Startup;
public static ConfigElement<string> Last_Window_Anchors; public static ConfigElement<string> Last_Window_Anchors;
public static ConfigElement<int> Last_Active_Tab; public static ConfigElement<int> Last_Active_Tab;
public static ConfigElement<bool> Last_DebugConsole_State; public static ConfigElement<bool> Last_DebugConsole_State;
@ -28,7 +30,7 @@ namespace UnityExplorer.Core.Config
internal static readonly Dictionary<string, IConfigElement> ConfigElements = new Dictionary<string, IConfigElement>(); internal static readonly Dictionary<string, IConfigElement> ConfigElements = new Dictionary<string, IConfigElement>();
public static void Init(IConfigHandler configHandler) public static void Init(ConfigHandler configHandler)
{ {
Handler = configHandler; Handler = configHandler;
Handler.Init(); Handler.Init();
@ -56,6 +58,11 @@ namespace UnityExplorer.Core.Config
KeyCode.F7, KeyCode.F7,
false); false);
Force_Unlock_Mouse = new ConfigElement<bool>("Force Unlock Mouse",
"Force the Cursor to be unlocked (visible) when the UnityExplorer menu is open.",
true,
false);
Default_Page_Limit = new ConfigElement<int>("Default Page Limit", Default_Page_Limit = new ConfigElement<int>("Default Page Limit",
"The default maximum number of elements per 'page' in UnityExplorer.", "The default maximum number of elements per 'page' in UnityExplorer.",
25, 25,
@ -102,25 +109,25 @@ namespace UnityExplorer.Core.Config
private static void PanelDragger_OnFinishResize(RectTransform rect) private static void PanelDragger_OnFinishResize(RectTransform rect)
{ {
Last_Window_Anchors.Value = RectAnchorsToString(rect); Last_Window_Anchors.Value = RectAnchorsToString(rect);
Handler.SaveConfig(); Handler.OnAnyConfigChanged();
} }
private static void MainMenu_OnActiveTabChanged(int page) private static void MainMenu_OnActiveTabChanged(int page)
{ {
Last_Active_Tab.Value = page; Last_Active_Tab.Value = page;
Handler.SaveConfig(); Handler.OnAnyConfigChanged();
} }
private static void DebugConsole_OnToggleShow(bool showing) private static void DebugConsole_OnToggleShow(bool showing)
{ {
Last_DebugConsole_State.Value = showing; Last_DebugConsole_State.Value = showing;
Handler.SaveConfig(); Handler.OnAnyConfigChanged();
} }
private static void SceneExplorer_OnToggleShow(bool showing) private static void SceneExplorer_OnToggleShow(bool showing)
{ {
Last_SceneExplorer_State.Value = showing; Last_SceneExplorer_State.Value = showing;
Handler.SaveConfig(); Handler.OnAnyConfigChanged();
} }
// Window Anchors helpers // Window Anchors helpers

View File

@ -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<T>(ConfigElement<T> element);
void SetConfigValue<T>(ConfigElement<T> element, T value);
T GetConfigValue<T>(ConfigElement<T> element);
void Init();
void LoadConfig();
void SaveConfig();
}
}

View File

@ -17,20 +17,18 @@ namespace UnityExplorer.Core.Input
{ {
public class CursorUnlocker public class CursorUnlocker
{ {
//public static bool Unlock public static bool Unlock
//{ {
// get => m_forceUnlock; get => m_forceUnlock;
// set => SetForceUnlock(value); set
//} {
//private static bool m_forceUnlock; m_forceUnlock = value;
UpdateCursorControl();
}
}
private static bool m_forceUnlock;
//private static void SetForceUnlock(bool unlock) public static bool ShouldActuallyUnlock => UIManager.ShowMenu && Unlock;
//{
// m_forceUnlock = unlock;
// UpdateCursorControl();
//}
//public static bool ShouldForceMouse => UIManager.ShowMenu && Unlock;
private static CursorLockMode m_lastLockMode; private static CursorLockMode m_lastLockMode;
private static bool m_lastVisibleState; private static bool m_lastVisibleState;
@ -48,7 +46,8 @@ namespace UnityExplorer.Core.Input
UpdateCursorControl(); UpdateCursorControl();
//Unlock = true; Unlock = true;
ConfigManager.Force_Unlock_Mouse.OnValueChanged += (bool val) => { Unlock = val; };
} }
private static void SetupPatches() private static void SetupPatches()
@ -122,7 +121,7 @@ namespace UnityExplorer.Core.Input
try try
{ {
m_currentlySettingCursor = true; m_currentlySettingCursor = true;
if (UIManager.ShowMenu) if (ShouldActuallyUnlock)
{ {
Cursor.lockState = CursorLockMode.None; Cursor.lockState = CursorLockMode.None;
Cursor.visible = true; Cursor.visible = true;
@ -196,12 +195,10 @@ namespace UnityExplorer.Core.Input
m_lastEventSystem = value; m_lastEventSystem = value;
m_lastInputModule = value?.currentInputModule; m_lastInputModule = value?.currentInputModule;
if (UIManager.ShowMenu) if (ShouldActuallyUnlock)
{
value = UIManager.EventSys; value = UIManager.EventSys;
} }
} }
}
// Force mouse to stay unlocked and visible while UnlockMouse and ShowMenu are true. // Force mouse to stay unlocked and visible while UnlockMouse and ShowMenu are true.
// Also keep track of when anything else tries to set Cursor state, this will be the // Also keep track of when anything else tries to set Cursor state, this will be the
@ -214,12 +211,10 @@ namespace UnityExplorer.Core.Input
{ {
m_lastLockMode = value; m_lastLockMode = value;
if (UIManager.ShowMenu) if (ShouldActuallyUnlock)
{
value = CursorLockMode.None; value = CursorLockMode.None;
} }
} }
}
[HarmonyPrefix] [HarmonyPrefix]
public static void Prefix_set_visible(ref bool value) public static void Prefix_set_visible(ref bool value)
@ -228,11 +223,9 @@ namespace UnityExplorer.Core.Input
{ {
m_lastVisibleState = value; m_lastVisibleState = value;
if (UIManager.ShowMenu) if (ShouldActuallyUnlock)
{
value = true; value = true;
} }
} }
} }
}
} }

View File

@ -8,18 +8,18 @@ using UnityExplorer.Core.Config;
namespace UnityExplorer.Loader.BIE namespace UnityExplorer.Loader.BIE
{ {
public class BepInExConfigHandler : IConfigHandler public class BepInExConfigHandler : ConfigHandler
{ {
private ConfigFile Config => ExplorerBepInPlugin.Instance.Config; private ConfigFile Config => ExplorerBepInPlugin.Instance.Config;
private const string CTG_NAME = "UnityExplorer"; private const string CTG_NAME = "UnityExplorer";
public void Init() public override void Init()
{ {
// Not necessary // Not necessary
} }
public void RegisterConfigElement<T>(ConfigElement<T> config) public override void RegisterConfigElement<T>(ConfigElement<T> config)
{ {
var entry = Config.Bind(CTG_NAME, config.Name, config.Value, config.Description); var entry = Config.Bind(CTG_NAME, config.Name, config.Value, config.Description);
@ -29,7 +29,7 @@ namespace UnityExplorer.Loader.BIE
}; };
} }
public T GetConfigValue<T>(ConfigElement<T> element) public override T GetConfigValue<T>(ConfigElement<T> element)
{ {
if (Config.TryGetEntry(CTG_NAME, element.Name, out ConfigEntry<T> configEntry)) if (Config.TryGetEntry(CTG_NAME, element.Name, out ConfigEntry<T> configEntry))
return configEntry.Value; return configEntry.Value;
@ -37,7 +37,7 @@ namespace UnityExplorer.Loader.BIE
throw new Exception("Could not get config entry '" + element.Name + "'"); throw new Exception("Could not get config entry '" + element.Name + "'");
} }
public void SetConfigValue<T>(ConfigElement<T> element, T value) public override void SetConfigValue<T>(ConfigElement<T> element, T value)
{ {
if (Config.TryGetEntry(CTG_NAME, element.Name, out ConfigEntry<T> configEntry)) if (Config.TryGetEntry(CTG_NAME, element.Name, out ConfigEntry<T> configEntry))
configEntry.Value = value; configEntry.Value = value;
@ -45,7 +45,7 @@ namespace UnityExplorer.Loader.BIE
ExplorerCore.Log("Could not get config entry '" + element.Name + "'"); ExplorerCore.Log("Could not get config entry '" + element.Name + "'");
} }
public void LoadConfig() public override void LoadConfig()
{ {
foreach (var entry in ConfigManager.ConfigElements) foreach (var entry in ConfigManager.ConfigElements)
{ {
@ -59,7 +59,7 @@ namespace UnityExplorer.Loader.BIE
} }
} }
public void SaveConfig() public override void SaveConfig()
{ {
// not required // not required
} }

View File

@ -36,7 +36,7 @@ namespace UnityExplorer
=> Log; => Log;
#endif #endif
public IConfigHandler ConfigHandler => _configHandler; public ConfigHandler ConfigHandler => _configHandler;
private BepInExConfigHandler _configHandler; private BepInExConfigHandler _configHandler;
public Harmony HarmonyInstance => s_harmony; public Harmony HarmonyInstance => s_harmony;

View File

@ -11,7 +11,7 @@ namespace UnityExplorer
string ExplorerFolder { get; } string ExplorerFolder { get; }
string ConfigFolder { get; } string ConfigFolder { get; }
IConfigHandler ConfigHandler { get; } ConfigHandler ConfigHandler { get; }
Action<object> OnLogMessage { get; } Action<object> OnLogMessage { get; }
Action<object> OnLogWarning { get; } Action<object> OnLogWarning { get; }

View File

@ -2,6 +2,7 @@
using System; using System;
using System.IO; using System.IO;
using MelonLoader; using MelonLoader;
using UnityEngine;
using UnityExplorer; using UnityExplorer;
using UnityExplorer.Core.Config; using UnityExplorer.Core.Config;
using UnityExplorer.Loader.ML; using UnityExplorer.Loader.ML;
@ -18,7 +19,7 @@ namespace UnityExplorer
public string ExplorerFolder => Path.Combine("Mods", ExplorerCore.NAME); public string ExplorerFolder => Path.Combine("Mods", ExplorerCore.NAME);
public string ConfigFolder => ExplorerFolder; public string ConfigFolder => ExplorerFolder;
public IConfigHandler ConfigHandler => _configHandler; public ConfigHandler ConfigHandler => _configHandler;
public MelonLoaderConfigHandler _configHandler; public MelonLoaderConfigHandler _configHandler;
public Action<object> OnLogMessage => MelonLogger.Msg; public Action<object> OnLogMessage => MelonLogger.Msg;

View File

@ -10,20 +10,20 @@ using UnityExplorer.Core.Config;
namespace UnityExplorer.Loader.ML namespace UnityExplorer.Loader.ML
{ {
public class MelonLoaderConfigHandler : IConfigHandler public class MelonLoaderConfigHandler : ConfigHandler
{ {
internal const string CTG_NAME = "UnityExplorer"; internal const string CTG_NAME = "UnityExplorer";
internal MelonPreferences_Category prefCategory; internal MelonPreferences_Category prefCategory;
public void Init() public override void Init()
{ {
prefCategory = MelonPreferences.CreateCategory(CTG_NAME, $"{CTG_NAME} Settings"); prefCategory = MelonPreferences.CreateCategory(CTG_NAME, $"{CTG_NAME} Settings");
MelonPreferences.Mapper.RegisterMapper(KeycodeReader, KeycodeWriter); MelonPreferences.Mapper.RegisterMapper(KeycodeReader, KeycodeWriter);
} }
public void LoadConfig() public override void LoadConfig()
{ {
foreach (var entry in ConfigManager.ConfigElements) foreach (var entry in ConfigManager.ConfigElements)
{ {
@ -36,7 +36,7 @@ namespace UnityExplorer.Loader.ML
} }
} }
public void RegisterConfigElement<T>(ConfigElement<T> config) public override void RegisterConfigElement<T>(ConfigElement<T> config)
{ {
var entry = prefCategory.CreateEntry(config.Name, config.Value, null, config.IsInternal) as MelonPreferences_Entry<T>; var entry = prefCategory.CreateEntry(config.Name, config.Value, null, config.IsInternal) as MelonPreferences_Entry<T>;
@ -49,17 +49,16 @@ namespace UnityExplorer.Loader.ML
}; };
} }
public void SetConfigValue<T>(ConfigElement<T> config, T value) public override void SetConfigValue<T>(ConfigElement<T> config, T value)
{ {
if (prefCategory.GetEntry<T>(config.Name) is MelonPreferences_Entry<T> entry) if (prefCategory.GetEntry<T>(config.Name) is MelonPreferences_Entry<T> entry)
{ {
entry.Value = value; entry.Value = value;
entry.Save(); entry.Save();
MelonPreferences.Save();
} }
} }
public T GetConfigValue<T>(ConfigElement<T> config) public override T GetConfigValue<T>(ConfigElement<T> config)
{ {
if (prefCategory.GetEntry<T>(config.Name) is MelonPreferences_Entry<T> entry) if (prefCategory.GetEntry<T>(config.Name) is MelonPreferences_Entry<T> entry)
return entry.Value; return entry.Value;
@ -67,9 +66,9 @@ namespace UnityExplorer.Loader.ML
return default; return default;
} }
public void SaveConfig() public override void SaveConfig()
{ {
// Not necessary MelonPreferences.Save();
} }
public static KeyCode KeycodeReader(TomlObject value) public static KeyCode KeycodeReader(TomlObject value)

View File

@ -48,8 +48,8 @@ namespace UnityExplorer
public Harmony HarmonyInstance => s_harmony; public Harmony HarmonyInstance => s_harmony;
public static readonly Harmony s_harmony = new Harmony(ExplorerCore.GUID); public static readonly Harmony s_harmony = new Harmony(ExplorerCore.GUID);
public IConfigHandler ConfigHandler => _configHandler; public ConfigHandler ConfigHandler => _configHandler;
private IConfigHandler _configHandler; private StandaloneConfigHandler _configHandler;
public string ExplorerFolder public string ExplorerFolder
{ {

View File

@ -10,36 +10,36 @@ using UnityEngine;
namespace UnityExplorer.Loader.STANDALONE namespace UnityExplorer.Loader.STANDALONE
{ {
public class StandaloneConfigHandler : IConfigHandler public class StandaloneConfigHandler : ConfigHandler
{ {
internal static IniDataParser _parser; internal static IniDataParser _parser;
internal static string INI_PATH; internal static string INI_PATH;
public void Init() public override void Init()
{ {
INI_PATH = Path.Combine(ExplorerCore.Loader.ConfigFolder, "config.ini"); INI_PATH = Path.Combine(ExplorerCore.Loader.ConfigFolder, "config.ini");
_parser = new IniDataParser(); _parser = new IniDataParser();
_parser.Configuration.CommentString = "#"; _parser.Configuration.CommentString = "#";
} }
public void LoadConfig() public override void LoadConfig()
{ {
if (!TryLoadConfig()) if (!TryLoadConfig())
SaveConfig(); SaveConfig();
} }
public void RegisterConfigElement<T>(ConfigElement<T> element) public override void RegisterConfigElement<T>(ConfigElement<T> element)
{ {
// Not necessary // Not necessary
} }
public void SetConfigValue<T>(ConfigElement<T> element, T value) public override void SetConfigValue<T>(ConfigElement<T> element, T value)
{ {
// Not necessary, just save. // Not necessary, just save.
SaveConfig(); SaveConfig();
} }
public T GetConfigValue<T>(ConfigElement<T> element) public override T GetConfigValue<T>(ConfigElement<T> element)
{ {
// Not necessary, just return the value. // Not necessary, just return the value.
return element.Value; return element.Value;
@ -82,7 +82,12 @@ namespace UnityExplorer.Loader.STANDALONE
return value; return value;
} }
public void SaveConfig() public override void OnAnyConfigChanged()
{
SaveConfig();
}
public override void SaveConfig()
{ {
var data = new IniParser.Model.IniData(); var data = new IniParser.Model.IniData();

View File

@ -49,7 +49,7 @@ namespace UnityExplorer.UI.CacheObject
public override void SetValue() public override void SetValue()
{ {
RefConfig.BoxedValue = IValue.Value; RefConfig.BoxedValue = IValue.Value;
ConfigManager.Handler.SaveConfig(); ConfigManager.Handler.OnAnyConfigChanged();
} }
internal GameObject m_leftGroup; internal GameObject m_leftGroup;
@ -59,7 +59,9 @@ namespace UnityExplorer.UI.CacheObject
{ {
base.ConstructUI(); 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); UIFactory.SetLayoutElement(horiGroup, minHeight: 30, flexibleHeight: 0);
// left group // left group
@ -89,6 +91,12 @@ namespace UnityExplorer.UI.CacheObject
IValue.m_mainContentParent = m_rightGroup; IValue.m_mainContentParent = m_rightGroup;
IValue.m_subContentParent = this.m_subContent; IValue.m_subContentParent = this.m_subContent;
} }
// Config description label
UIFactory.CreateLabel(vertGroup, "Description", $"<i>{RefConfig.Description}</i>", TextAnchor.MiddleLeft, Color.grey);
m_subContent.transform.SetAsLastSibling();
} }
} }
} }

View File

@ -19,13 +19,6 @@ namespace UnityExplorer.UI.Main.Options
{ {
ConstructUI(); 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; return true;
} }
@ -51,14 +44,26 @@ namespace UnityExplorer.UI.Main.Options
var titleLabel = UIFactory.CreateLabel(Content, "Title", "Options", TextAnchor.UpperLeft, default, true, 25); var titleLabel = UIFactory.CreateLabel(Content, "Title", "Options", TextAnchor.UpperLeft, default, true, 25);
UIFactory.SetLayoutElement(titleLabel.gameObject, minHeight: 30, flexibleHeight: 0); 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 ~~~~~ // ~~~~~ Actual options ~~~~~
UIFactory.CreateScrollView(Content, "ConfigList", out m_contentObj, out _, new Color(0.05f, 0.05f, 0.05f)); UIFactory.CreateScrollView(Content, "ConfigList", out m_contentObj, out _, new Color(0.05f, 0.05f, 0.05f));
UIFactory.SetLayoutGroup<VerticalLayoutGroup>(m_contentObj, forceHeight: true, spacing: 3, padLeft: 3, padRight: 3); UIFactory.SetLayoutGroup<VerticalLayoutGroup>(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), _cachedConfigEntries.AddRange(ConfigManager.ConfigElements.Values
// new Color(0.1f, 0.1f, 0.1f)); .Where(it => !it.IsInternal)
//UIFactory.SetLayoutElement(m_contentObj, minHeight: 340, flexibleHeight: 9999); .Select(it => new CacheConfigEntry(it, m_contentObj)));
foreach (var entry in _cachedConfigEntries)
entry.Enable();
} }

View File

@ -218,7 +218,7 @@
<Compile Include="Core\Config\ConfigElement.cs" /> <Compile Include="Core\Config\ConfigElement.cs" />
<Compile Include="Core\Config\ConfigManager.cs" /> <Compile Include="Core\Config\ConfigManager.cs" />
<Compile Include="Core\Config\IConfigElement.cs" /> <Compile Include="Core\Config\IConfigElement.cs" />
<Compile Include="Core\Config\IConfigHandler.cs" /> <Compile Include="Core\Config\ConfigHandler.cs" />
<Compile Include="Core\CSharp\DummyBehaviour.cs" /> <Compile Include="Core\CSharp\DummyBehaviour.cs" />
<Compile Include="Core\CSharp\ScriptEvaluator.cs" /> <Compile Include="Core\CSharp\ScriptEvaluator.cs" />
<Compile Include="Core\CSharp\ScriptInteraction.cs" /> <Compile Include="Core\CSharp\ScriptInteraction.cs" />