Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 3501a28

Browse files
committed
Restore UnlockMouse config, adjust config saving
1 parent 40f6981 commit 3501a28

14 files changed

+114
-94
lines changed

src/Core/Config/ConfigHandler.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace UnityExplorer.Core.Config
7+
{
8+
public abstract class ConfigHandler
9+
{
10+
public abstract void RegisterConfigElement<T>(ConfigElement<T> element);
11+
12+
public abstract void SetConfigValue<T>(ConfigElement<T> element, T value);
13+
14+
public abstract T GetConfigValue<T>(ConfigElement<T> element);
15+
16+
public abstract void Init();
17+
18+
public abstract void LoadConfig();
19+
20+
public abstract void SaveConfig();
21+
22+
public virtual void OnAnyConfigChanged() { }
23+
}
24+
}

src/Core/Config/ConfigManager.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@ namespace UnityExplorer.Core.Config
1212
{
1313
public static class ConfigManager
1414
{
15-
// Each Loader has its own ConfigHandler.
15+
// Each Mod Loader has its own ConfigHandler.
1616
// See the UnityExplorer.Loader namespace for the implementations.
17-
public static IConfigHandler Handler { get; private set; }
17+
public static ConfigHandler Handler { get; private set; }
1818

1919
public static ConfigElement<KeyCode> Main_Menu_Toggle;
20+
public static ConfigElement<bool> Force_Unlock_Mouse;
2021
public static ConfigElement<int> Default_Page_Limit;
2122
public static ConfigElement<string> Default_Output_Path;
2223
public static ConfigElement<bool> Log_Unity_Debug;
2324
public static ConfigElement<bool> Hide_On_Startup;
25+
2426
public static ConfigElement<string> Last_Window_Anchors;
2527
public static ConfigElement<int> Last_Active_Tab;
2628
public static ConfigElement<bool> Last_DebugConsole_State;
2729
public static ConfigElement<bool> Last_SceneExplorer_State;
2830

2931
internal static readonly Dictionary<string, IConfigElement> ConfigElements = new Dictionary<string, IConfigElement>();
3032

31-
public static void Init(IConfigHandler configHandler)
33+
public static void Init(ConfigHandler configHandler)
3234
{
3335
Handler = configHandler;
3436
Handler.Init();
@@ -56,6 +58,11 @@ private static void CreateConfigElements()
5658
KeyCode.F7,
5759
false);
5860

61+
Force_Unlock_Mouse = new ConfigElement<bool>("Force Unlock Mouse",
62+
"Force the Cursor to be unlocked (visible) when the UnityExplorer menu is open.",
63+
true,
64+
false);
65+
5966
Default_Page_Limit = new ConfigElement<int>("Default Page Limit",
6067
"The default maximum number of elements per 'page' in UnityExplorer.",
6168
25,
@@ -102,25 +109,25 @@ private static void CreateConfigElements()
102109
private static void PanelDragger_OnFinishResize(RectTransform rect)
103110
{
104111
Last_Window_Anchors.Value = RectAnchorsToString(rect);
105-
Handler.SaveConfig();
112+
Handler.OnAnyConfigChanged();
106113
}
107114

108115
private static void MainMenu_OnActiveTabChanged(int page)
109116
{
110117
Last_Active_Tab.Value = page;
111-
Handler.SaveConfig();
118+
Handler.OnAnyConfigChanged();
112119
}
113120

114121
private static void DebugConsole_OnToggleShow(bool showing)
115122
{
116123
Last_DebugConsole_State.Value = showing;
117-
Handler.SaveConfig();
124+
Handler.OnAnyConfigChanged();
118125
}
119126

120127
private static void SceneExplorer_OnToggleShow(bool showing)
121128
{
122129
Last_SceneExplorer_State.Value = showing;
123-
Handler.SaveConfig();
130+
Handler.OnAnyConfigChanged();
124131
}
125132

126133
// Window Anchors helpers

src/Core/Config/IConfigHandler.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Core/Input/CursorUnlocker.cs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,18 @@ namespace UnityExplorer.Core.Input
1717
{
1818
public class CursorUnlocker
1919
{
20-
//public static bool Unlock
21-
//{
22-
// get => m_forceUnlock;
23-
// set => SetForceUnlock(value);
24-
//}
25-
//private static bool m_forceUnlock;
26-
27-
//private static void SetForceUnlock(bool unlock)
28-
//{
29-
// m_forceUnlock = unlock;
30-
// UpdateCursorControl();
31-
//}
20+
public static bool Unlock
21+
{
22+
get => m_forceUnlock;
23+
set
24+
{
25+
m_forceUnlock = value;
26+
UpdateCursorControl();
27+
}
28+
}
29+
private static bool m_forceUnlock;
3230

33-
//public static bool ShouldForceMouse => UIManager.ShowMenu && Unlock;
31+
public static bool ShouldActuallyUnlock => UIManager.ShowMenu && Unlock;
3432

3533
private static CursorLockMode m_lastLockMode;
3634
private static bool m_lastVisibleState;
@@ -48,7 +46,8 @@ public static void Init()
4846

4947
UpdateCursorControl();
5048

51-
//Unlock = true;
49+
Unlock = true;
50+
ConfigManager.Force_Unlock_Mouse.OnValueChanged += (bool val) => { Unlock = val; };
5251
}
5352

5453
private static void SetupPatches()
@@ -122,7 +121,7 @@ public static void UpdateCursorControl()
122121
try
123122
{
124123
m_currentlySettingCursor = true;
125-
if (UIManager.ShowMenu)
124+
if (ShouldActuallyUnlock)
126125
{
127126
Cursor.lockState = CursorLockMode.None;
128127
Cursor.visible = true;
@@ -196,10 +195,8 @@ public static void Prefix_EventSystem_set_current(ref EventSystem value)
196195
m_lastEventSystem = value;
197196
m_lastInputModule = value?.currentInputModule;
198197

199-
if (UIManager.ShowMenu)
200-
{
198+
if (ShouldActuallyUnlock)
201199
value = UIManager.EventSys;
202-
}
203200
}
204201
}
205202

@@ -214,10 +211,8 @@ public static void Prefix_set_lockState(ref CursorLockMode value)
214211
{
215212
m_lastLockMode = value;
216213

217-
if (UIManager.ShowMenu)
218-
{
214+
if (ShouldActuallyUnlock)
219215
value = CursorLockMode.None;
220-
}
221216
}
222217
}
223218

@@ -228,10 +223,8 @@ public static void Prefix_set_visible(ref bool value)
228223
{
229224
m_lastVisibleState = value;
230225

231-
if (UIManager.ShowMenu)
232-
{
226+
if (ShouldActuallyUnlock)
233227
value = true;
234-
}
235228
}
236229
}
237230
}

src/Loader/BIE/BepInExConfigHandler.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88

99
namespace UnityExplorer.Loader.BIE
1010
{
11-
public class BepInExConfigHandler : IConfigHandler
11+
public class BepInExConfigHandler : ConfigHandler
1212
{
1313
private ConfigFile Config => ExplorerBepInPlugin.Instance.Config;
1414

1515
private const string CTG_NAME = "UnityExplorer";
1616

17-
public void Init()
17+
public override void Init()
1818
{
1919
// Not necessary
2020
}
2121

22-
public void RegisterConfigElement<T>(ConfigElement<T> config)
22+
public override void RegisterConfigElement<T>(ConfigElement<T> config)
2323
{
2424
var entry = Config.Bind(CTG_NAME, config.Name, config.Value, config.Description);
2525

@@ -29,23 +29,23 @@ public void RegisterConfigElement<T>(ConfigElement<T> config)
2929
};
3030
}
3131

32-
public T GetConfigValue<T>(ConfigElement<T> element)
32+
public override T GetConfigValue<T>(ConfigElement<T> element)
3333
{
3434
if (Config.TryGetEntry(CTG_NAME, element.Name, out ConfigEntry<T> configEntry))
3535
return configEntry.Value;
3636
else
3737
throw new Exception("Could not get config entry '" + element.Name + "'");
3838
}
3939

40-
public void SetConfigValue<T>(ConfigElement<T> element, T value)
40+
public override void SetConfigValue<T>(ConfigElement<T> element, T value)
4141
{
4242
if (Config.TryGetEntry(CTG_NAME, element.Name, out ConfigEntry<T> configEntry))
4343
configEntry.Value = value;
4444
else
4545
ExplorerCore.Log("Could not get config entry '" + element.Name + "'");
4646
}
4747

48-
public void LoadConfig()
48+
public override void LoadConfig()
4949
{
5050
foreach (var entry in ConfigManager.ConfigElements)
5151
{
@@ -59,7 +59,7 @@ public void LoadConfig()
5959
}
6060
}
6161

62-
public void SaveConfig()
62+
public override void SaveConfig()
6363
{
6464
// not required
6565
}

src/Loader/BIE/ExplorerBepInPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ManualLogSource LogSource
3636
=> Log;
3737
#endif
3838

39-
public IConfigHandler ConfigHandler => _configHandler;
39+
public ConfigHandler ConfigHandler => _configHandler;
4040
private BepInExConfigHandler _configHandler;
4141

4242
public Harmony HarmonyInstance => s_harmony;

src/Loader/IExplorerLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IExplorerLoader
1111
string ExplorerFolder { get; }
1212

1313
string ConfigFolder { get; }
14-
IConfigHandler ConfigHandler { get; }
14+
ConfigHandler ConfigHandler { get; }
1515

1616
Action<object> OnLogMessage { get; }
1717
Action<object> OnLogWarning { get; }

src/Loader/ML/ExplorerMelonMod.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.IO;
44
using MelonLoader;
5+
using UnityEngine;
56
using UnityExplorer;
67
using UnityExplorer.Core.Config;
78
using UnityExplorer.Loader.ML;
@@ -18,7 +19,7 @@ public class ExplorerMelonMod : MelonMod, IExplorerLoader
1819
public string ExplorerFolder => Path.Combine("Mods", ExplorerCore.NAME);
1920
public string ConfigFolder => ExplorerFolder;
2021

21-
public IConfigHandler ConfigHandler => _configHandler;
22+
public ConfigHandler ConfigHandler => _configHandler;
2223
public MelonLoaderConfigHandler _configHandler;
2324

2425
public Action<object> OnLogMessage => MelonLogger.Msg;

src/Loader/ML/MelonLoaderConfigHandler.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010

1111
namespace UnityExplorer.Loader.ML
1212
{
13-
public class MelonLoaderConfigHandler : IConfigHandler
13+
public class MelonLoaderConfigHandler : ConfigHandler
1414
{
1515
internal const string CTG_NAME = "UnityExplorer";
1616

1717
internal MelonPreferences_Category prefCategory;
1818

19-
public void Init()
19+
public override void Init()
2020
{
2121
prefCategory = MelonPreferences.CreateCategory(CTG_NAME, $"{CTG_NAME} Settings");
2222

2323
MelonPreferences.Mapper.RegisterMapper(KeycodeReader, KeycodeWriter);
2424
}
2525

26-
public void LoadConfig()
26+
public override void LoadConfig()
2727
{
2828
foreach (var entry in ConfigManager.ConfigElements)
2929
{
@@ -36,7 +36,7 @@ public void LoadConfig()
3636
}
3737
}
3838

39-
public void RegisterConfigElement<T>(ConfigElement<T> config)
39+
public override void RegisterConfigElement<T>(ConfigElement<T> config)
4040
{
4141
var entry = prefCategory.CreateEntry(config.Name, config.Value, null, config.IsInternal) as MelonPreferences_Entry<T>;
4242

@@ -49,27 +49,26 @@ public void RegisterConfigElement<T>(ConfigElement<T> config)
4949
};
5050
}
5151

52-
public void SetConfigValue<T>(ConfigElement<T> config, T value)
52+
public override void SetConfigValue<T>(ConfigElement<T> config, T value)
5353
{
5454
if (prefCategory.GetEntry<T>(config.Name) is MelonPreferences_Entry<T> entry)
5555
{
5656
entry.Value = value;
5757
entry.Save();
58-
MelonPreferences.Save();
5958
}
6059
}
6160

62-
public T GetConfigValue<T>(ConfigElement<T> config)
61+
public override T GetConfigValue<T>(ConfigElement<T> config)
6362
{
6463
if (prefCategory.GetEntry<T>(config.Name) is MelonPreferences_Entry<T> entry)
6564
return entry.Value;
6665

6766
return default;
6867
}
6968

70-
public void SaveConfig()
69+
public override void SaveConfig()
7170
{
72-
// Not necessary
71+
MelonPreferences.Save();
7372
}
7473

7574
public static KeyCode KeycodeReader(TomlObject value)

src/Loader/STANDALONE/ExplorerStandalone.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public static ExplorerStandalone CreateInstance(Action<string, LogType> logListe
4848
public Harmony HarmonyInstance => s_harmony;
4949
public static readonly Harmony s_harmony = new Harmony(ExplorerCore.GUID);
5050

51-
public IConfigHandler ConfigHandler => _configHandler;
52-
private IConfigHandler _configHandler;
51+
public ConfigHandler ConfigHandler => _configHandler;
52+
private StandaloneConfigHandler _configHandler;
5353

5454
public string ExplorerFolder
5555
{

0 commit comments

Comments
 (0)