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

Commit 8fb7d87

Browse files
committed
Use Tomlet, simplify panel data saving
1 parent c740c3c commit 8fb7d87

16 files changed

+64
-165
lines changed

src/Config/ConfigManager.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ namespace UnityExplorer.Config
1212
{
1313
public static class ConfigManager
1414
{
15+
internal static readonly Dictionary<string, IConfigElement> ConfigElements = new();
16+
internal static readonly Dictionary<string, IConfigElement> InternalConfigs = new();
17+
1518
// Each Mod Loader has its own ConfigHandler.
1619
// See the UnityExplorer.Loader namespace for the implementations.
1720
public static ConfigHandler Handler { get; private set; }
1821

22+
// Actual UE Settings
1923
public static ConfigElement<KeyCode> Master_Toggle;
2024
public static ConfigElement<UIManager.VerticalAnchor> Main_Navbar_Anchor;
2125
public static ConfigElement<bool> Force_Unlock_Mouse;
@@ -26,22 +30,18 @@ public static class ConfigManager
2630
public static ConfigElement<bool> Log_Unity_Debug;
2731
public static ConfigElement<bool> Hide_On_Startup;
2832
public static ConfigElement<float> Startup_Delay_Time;
29-
3033
public static ConfigElement<string> Reflection_Signature_Blacklist;
3134

3235
// internal configs
3336
internal static InternalConfigHandler InternalHandler { get; private set; }
37+
internal static readonly Dictionary<UIManager.Panels, ConfigElement<string>> PanelSaveData = new();
3438

35-
public static ConfigElement<string> ObjectExplorerData;
36-
public static ConfigElement<string> InspectorData;
37-
public static ConfigElement<string> CSConsoleData;
38-
public static ConfigElement<string> OptionsPanelData;
39-
public static ConfigElement<string> ConsoleLogData;
40-
public static ConfigElement<string> HookManagerData;
41-
public static ConfigElement<string> ClipboardData;
42-
43-
internal static readonly Dictionary<string, IConfigElement> ConfigElements = new Dictionary<string, IConfigElement>();
44-
internal static readonly Dictionary<string, IConfigElement> InternalConfigs = new Dictionary<string, IConfigElement>();
39+
internal static ConfigElement<string> GetPanelSaveData(UIManager.Panels panel)
40+
{
41+
if (!PanelSaveData.ContainsKey(panel))
42+
PanelSaveData.Add(panel, new ConfigElement<string>(panel.ToString(), string.Empty, string.Empty, true));
43+
return PanelSaveData[panel];
44+
}
4545

4646
public static void Init(ConfigHandler configHandler)
4747
{
@@ -124,16 +124,6 @@ private static void CreateConfigElements()
124124
"Seperate signatures with a semicolon ';'.\r\n" +
125125
"For example, to blacklist Camera.main, you would add 'UnityEngine.Camera.main;'",
126126
"");
127-
128-
// Internal configs (panel save data)
129-
130-
ObjectExplorerData = new ConfigElement<string>("ObjectExplorer", "", "", true);
131-
InspectorData = new ConfigElement<string>("Inspector", "", "", true);
132-
CSConsoleData = new ConfigElement<string>("CSConsole", "", "", true);
133-
OptionsPanelData = new ConfigElement<string>("OptionsPanel", "", "", true);
134-
ConsoleLogData = new ConfigElement<string>("ConsoleLog", "", "", true);
135-
HookManagerData = new ConfigElement<string>("HookManager", "", "", true);
136-
ClipboardData = new ConfigElement<string>("Clipboard", "", "", true);
137127
}
138128
}
139129
}
Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
using IniParser.Parser;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.IO;
54
using System.Linq;
65
using System.Text;
76
using UnityEngine;
87
using UnityExplorer.UI;
8+
using Tomlet;
9+
using Tomlet.Models;
910

1011
namespace UnityExplorer.Config
1112
{
1213
public class InternalConfigHandler : ConfigHandler
1314
{
14-
internal static IniDataParser _parser;
15-
internal static string INI_PATH;
15+
internal static string CONFIG_PATH;
1616

1717
public override void Init()
1818
{
19-
INI_PATH = Path.Combine(ExplorerCore.Loader.ExplorerFolder, "data.ini");
20-
_parser = new IniDataParser();
21-
_parser.Configuration.CommentString = "#";
19+
CONFIG_PATH = Path.Combine(ExplorerCore.Loader.ExplorerFolder, "data.cfg");
2220
}
2321

2422
public override void LoadConfig()
@@ -37,32 +35,24 @@ public override void SetConfigValue<T>(ConfigElement<T> element, T value)
3735
// Not necessary
3836
}
3937

40-
public override T GetConfigValue<T>(ConfigElement<T> element)
41-
{
42-
// Not necessary, just return the value.
43-
return element.Value;
44-
}
38+
// Not necessary, just return the value.
39+
public override T GetConfigValue<T>(ConfigElement<T> element) => element.Value;
4540

46-
public override void OnAnyConfigChanged()
47-
{
48-
SaveConfig();
49-
}
41+
// Always just auto-save.
42+
public override void OnAnyConfigChanged() => SaveConfig();
5043

5144
public bool TryLoadConfig()
5245
{
5346
try
5447
{
55-
if (!File.Exists(INI_PATH))
48+
if (!File.Exists(CONFIG_PATH))
5649
return false;
5750

58-
string ini = File.ReadAllText(INI_PATH);
59-
60-
var data = _parser.Parse(ini);
61-
62-
foreach (var config in data.Sections["Config"])
51+
TomlDocument document = TomlParser.ParseFile(CONFIG_PATH);
52+
foreach (var key in document.Keys)
6353
{
64-
if (ConfigManager.InternalConfigs.TryGetValue(config.KeyName, out IConfigElement configElement))
65-
configElement.BoxedValue = StringToConfigValue(config.Value, configElement.ElementType);
54+
var panelKey = (UIManager.Panels)Enum.Parse(typeof(UIManager.Panels), key);
55+
ConfigManager.GetPanelSaveData(panelKey).Value = document.GetString(key);
6656
}
6757

6858
return true;
@@ -79,27 +69,11 @@ public override void SaveConfig()
7969
if (UIManager.Initializing)
8070
return;
8171

82-
var data = new IniParser.Model.IniData();
83-
84-
data.Sections.AddSection("Config");
85-
var sec = data.Sections["Config"];
86-
72+
var tomlDocument = TomlDocument.CreateEmpty();
8773
foreach (var entry in ConfigManager.InternalConfigs)
88-
sec.AddKey(entry.Key, entry.Value.BoxedValue.ToString());
74+
tomlDocument.Put(entry.Key, entry.Value.BoxedValue as string, false);
8975

90-
File.WriteAllText(INI_PATH, data.ToString());
91-
}
92-
93-
public object StringToConfigValue(string value, Type elementType)
94-
{
95-
if (elementType.IsEnum)
96-
return Enum.Parse(elementType, value);
97-
else if (elementType == typeof(bool))
98-
return bool.Parse(value);
99-
else if (elementType == typeof(int))
100-
return int.Parse(value);
101-
else
102-
return value;
76+
File.WriteAllText(CONFIG_PATH, tomlDocument.SerializedValue);
10377
}
10478
}
10579
}

src/ILRepack.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<ItemGroup>
77
<InputAssemblies Include="$(OutputPath)$(AssemblyName).dll" />
88
<InputAssemblies Include="..\lib\mcs-unity\mcs\bin\Release\mcs.dll" />
9-
<InputAssemblies Include="packages\ini-parser.2.5.2\lib\net20\INIFileParser.dll" />
9+
<InputAssemblies Include="packages\Samboy063.Tomlet.3.1.3\lib\net35\Tomlet.dll" />
1010
</ItemGroup>
11-
11+
1212
<!-- Required references for ILRepack -->
1313
<ItemGroup>
1414
<ReferenceFolders Include="packages\HarmonyX.2.5.2\lib\net35\" />

src/Inspectors/InspectUnderMouse.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,5 @@ public override void ConstructPanelContent()
208208

209209
UIRoot.SetActive(false);
210210
}
211-
212-
public override void DoSaveToConfigElement() { }
213-
214-
public override string GetSaveDataFromConfigManager() => null;
215211
}
216212
}

src/UI/Panels/CSConsolePanel.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ public override void Update()
5555
ConsoleController.Update();
5656
}
5757

58-
// Saving
59-
60-
public override void DoSaveToConfigElement()
61-
{
62-
ConfigManager.CSConsoleData.Value = this.ToSaveData();
63-
}
64-
65-
public override string GetSaveDataFromConfigManager() => ConfigManager.CSConsoleData.Value;
66-
6758
// UI Construction
6859

6960
public override void OnFinishResize(RectTransform panel)

src/UI/Panels/ClipboardPanel.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public class ClipboardPanel : UIPanel
2727
public override bool NavButtonWanted => true;
2828
public override bool ShouldSaveActiveState => true;
2929
public override bool ShowByDefault => true;
30-
public override string GetSaveDataFromConfigManager() => ConfigManager.ClipboardData.Value;
31-
public override void DoSaveToConfigElement() => ConfigManager.ClipboardData.Value = this.ToSaveData();
3230

3331
private static Text CurrentPasteLabel;
3432

src/UI/Panels/HookManagerPanel.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ public enum Pages
4646
public Text EditorInputText { get; private set; }
4747
public Text EditorHighlightText { get; private set; }
4848

49-
public override string GetSaveDataFromConfigManager() => ConfigManager.HookManagerData.Value;
50-
51-
public override void DoSaveToConfigElement() => ConfigManager.HookManagerData.Value = this.ToSaveData();
52-
5349
private void OnClassInputAddClicked()
5450
{
5551
HookManager.Instance.OnClassSelectedForHooks(this.classSelectorInputField.Text);

src/UI/Panels/InspectorPanel.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public override void OnFinishResize(RectTransform panel)
4444
InspectorManager.OnPanelResized(panel.rect.width);
4545
}
4646

47-
public override string GetSaveDataFromConfigManager() => ConfigManager.InspectorData.Value;
48-
49-
public override void DoSaveToConfigElement() => ConfigManager.InspectorData.Value = this.ToSaveData();
50-
5147
protected internal override void DoSetDefaultPosAndAnchors()
5248
{
5349
Rect.localPosition = Vector2.zero;

src/UI/Panels/LogPanel.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,6 @@ public void SetCell(ConsoleLogCell cell, int index)
144144
RuntimeProvider.Instance.SetColorBlock(cell.Input.Component, color);
145145
}
146146

147-
// Panel save data
148-
149-
public override string GetSaveDataFromConfigManager()
150-
{
151-
return ConfigManager.ConsoleLogData.Value;
152-
}
153-
154-
public override void DoSaveToConfigElement()
155-
{
156-
ConfigManager.ConsoleLogData.Value = this.ToSaveData();
157-
}
158-
159147
protected internal override void DoSetDefaultPosAndAnchors()
160148
{
161149
Rect.localPosition = Vector2.zero;

src/UI/Panels/ObjectExplorerPanel.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void SetTab(int tabIndex)
4646
RuntimeProvider.Instance.SetColorBlock(button.Component, UniversalUI.enabledButtonColor, UniversalUI.enabledButtonColor * 1.2f);
4747

4848
SelectedTab = tabIndex;
49-
SaveToConfigManager();
49+
SaveInternalData();
5050
}
5151

5252
private void DisableTab(int tabIndex)
@@ -63,21 +63,12 @@ public override void Update()
6363
ObjectSearch.Update();
6464
}
6565

66-
public override string GetSaveDataFromConfigManager() => ConfigManager.ObjectExplorerData.Value;
67-
68-
public override void DoSaveToConfigElement()
69-
{
70-
ConfigManager.ObjectExplorerData.Value = this.ToSaveData();
71-
}
72-
7366
public override string ToSaveData()
7467
{
75-
string ret = base.ToSaveData();
76-
ret += "|" + SelectedTab;
77-
return ret;
68+
return string.Join("|", new string[] { base.ToSaveData(), SelectedTab.ToString() });
7869
}
7970

80-
public override void ApplySaveData(string data)
71+
protected override void ApplySaveData(string data)
8172
{
8273
base.ApplySaveData(data);
8374

0 commit comments

Comments
 (0)