Use Tomlet, simplify panel data saving

This commit is contained in:
Sinai 2022-01-19 17:34:53 +11:00
parent c740c3c54d
commit 8fb7d87ca6
16 changed files with 64 additions and 165 deletions

View File

@ -12,10 +12,14 @@ namespace UnityExplorer.Config
{ {
public static class ConfigManager public static class ConfigManager
{ {
internal static readonly Dictionary<string, IConfigElement> ConfigElements = new();
internal static readonly Dictionary<string, IConfigElement> InternalConfigs = new();
// Each Mod 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 ConfigHandler Handler { get; private set; } public static ConfigHandler Handler { get; private set; }
// Actual UE Settings
public static ConfigElement<KeyCode> Master_Toggle; public static ConfigElement<KeyCode> Master_Toggle;
public static ConfigElement<UIManager.VerticalAnchor> Main_Navbar_Anchor; public static ConfigElement<UIManager.VerticalAnchor> Main_Navbar_Anchor;
public static ConfigElement<bool> Force_Unlock_Mouse; public static ConfigElement<bool> Force_Unlock_Mouse;
@ -26,22 +30,18 @@ namespace UnityExplorer.Config
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<float> Startup_Delay_Time; public static ConfigElement<float> Startup_Delay_Time;
public static ConfigElement<string> Reflection_Signature_Blacklist; public static ConfigElement<string> Reflection_Signature_Blacklist;
// internal configs // internal configs
internal static InternalConfigHandler InternalHandler { get; private set; } internal static InternalConfigHandler InternalHandler { get; private set; }
internal static readonly Dictionary<UIManager.Panels, ConfigElement<string>> PanelSaveData = new();
public static ConfigElement<string> ObjectExplorerData; internal static ConfigElement<string> GetPanelSaveData(UIManager.Panels panel)
public static ConfigElement<string> InspectorData; {
public static ConfigElement<string> CSConsoleData; if (!PanelSaveData.ContainsKey(panel))
public static ConfigElement<string> OptionsPanelData; PanelSaveData.Add(panel, new ConfigElement<string>(panel.ToString(), string.Empty, string.Empty, true));
public static ConfigElement<string> ConsoleLogData; return PanelSaveData[panel];
public static ConfigElement<string> HookManagerData; }
public static ConfigElement<string> ClipboardData;
internal static readonly Dictionary<string, IConfigElement> ConfigElements = new Dictionary<string, IConfigElement>();
internal static readonly Dictionary<string, IConfigElement> InternalConfigs = new Dictionary<string, IConfigElement>();
public static void Init(ConfigHandler configHandler) public static void Init(ConfigHandler configHandler)
{ {
@ -124,16 +124,6 @@ namespace UnityExplorer.Config
"Seperate signatures with a semicolon ';'.\r\n" + "Seperate signatures with a semicolon ';'.\r\n" +
"For example, to blacklist Camera.main, you would add 'UnityEngine.Camera.main;'", "For example, to blacklist Camera.main, you would add 'UnityEngine.Camera.main;'",
""); "");
// Internal configs (panel save data)
ObjectExplorerData = new ConfigElement<string>("ObjectExplorer", "", "", true);
InspectorData = new ConfigElement<string>("Inspector", "", "", true);
CSConsoleData = new ConfigElement<string>("CSConsole", "", "", true);
OptionsPanelData = new ConfigElement<string>("OptionsPanel", "", "", true);
ConsoleLogData = new ConfigElement<string>("ConsoleLog", "", "", true);
HookManagerData = new ConfigElement<string>("HookManager", "", "", true);
ClipboardData = new ConfigElement<string>("Clipboard", "", "", true);
} }
} }
} }

View File

@ -1,24 +1,22 @@
using IniParser.Parser; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using UnityExplorer.UI; using UnityExplorer.UI;
using Tomlet;
using Tomlet.Models;
namespace UnityExplorer.Config namespace UnityExplorer.Config
{ {
public class InternalConfigHandler : ConfigHandler public class InternalConfigHandler : ConfigHandler
{ {
internal static IniDataParser _parser; internal static string CONFIG_PATH;
internal static string INI_PATH;
public override void Init() public override void Init()
{ {
INI_PATH = Path.Combine(ExplorerCore.Loader.ExplorerFolder, "data.ini"); CONFIG_PATH = Path.Combine(ExplorerCore.Loader.ExplorerFolder, "data.cfg");
_parser = new IniDataParser();
_parser.Configuration.CommentString = "#";
} }
public override void LoadConfig() public override void LoadConfig()
@ -37,32 +35,24 @@ namespace UnityExplorer.Config
// Not necessary // Not necessary
} }
public override T GetConfigValue<T>(ConfigElement<T> element) // Not necessary, just return the value.
{ public override T GetConfigValue<T>(ConfigElement<T> element) => element.Value;
// Not necessary, just return the value.
return element.Value;
}
public override void OnAnyConfigChanged() // Always just auto-save.
{ public override void OnAnyConfigChanged() => SaveConfig();
SaveConfig();
}
public bool TryLoadConfig() public bool TryLoadConfig()
{ {
try try
{ {
if (!File.Exists(INI_PATH)) if (!File.Exists(CONFIG_PATH))
return false; return false;
string ini = File.ReadAllText(INI_PATH); TomlDocument document = TomlParser.ParseFile(CONFIG_PATH);
foreach (var key in document.Keys)
var data = _parser.Parse(ini);
foreach (var config in data.Sections["Config"])
{ {
if (ConfigManager.InternalConfigs.TryGetValue(config.KeyName, out IConfigElement configElement)) var panelKey = (UIManager.Panels)Enum.Parse(typeof(UIManager.Panels), key);
configElement.BoxedValue = StringToConfigValue(config.Value, configElement.ElementType); ConfigManager.GetPanelSaveData(panelKey).Value = document.GetString(key);
} }
return true; return true;
@ -79,27 +69,11 @@ namespace UnityExplorer.Config
if (UIManager.Initializing) if (UIManager.Initializing)
return; return;
var data = new IniParser.Model.IniData(); var tomlDocument = TomlDocument.CreateEmpty();
data.Sections.AddSection("Config");
var sec = data.Sections["Config"];
foreach (var entry in ConfigManager.InternalConfigs) foreach (var entry in ConfigManager.InternalConfigs)
sec.AddKey(entry.Key, entry.Value.BoxedValue.ToString()); tomlDocument.Put(entry.Key, entry.Value.BoxedValue as string, false);
File.WriteAllText(INI_PATH, data.ToString()); File.WriteAllText(CONFIG_PATH, tomlDocument.SerializedValue);
}
public object StringToConfigValue(string value, Type elementType)
{
if (elementType.IsEnum)
return Enum.Parse(elementType, value);
else if (elementType == typeof(bool))
return bool.Parse(value);
else if (elementType == typeof(int))
return int.Parse(value);
else
return value;
} }
} }
} }

View File

@ -6,9 +6,9 @@
<ItemGroup> <ItemGroup>
<InputAssemblies Include="$(OutputPath)$(AssemblyName).dll" /> <InputAssemblies Include="$(OutputPath)$(AssemblyName).dll" />
<InputAssemblies Include="..\lib\mcs-unity\mcs\bin\Release\mcs.dll" /> <InputAssemblies Include="..\lib\mcs-unity\mcs\bin\Release\mcs.dll" />
<InputAssemblies Include="packages\ini-parser.2.5.2\lib\net20\INIFileParser.dll" /> <InputAssemblies Include="packages\Samboy063.Tomlet.3.1.3\lib\net35\Tomlet.dll" />
</ItemGroup> </ItemGroup>
<!-- Required references for ILRepack --> <!-- Required references for ILRepack -->
<ItemGroup> <ItemGroup>
<ReferenceFolders Include="packages\HarmonyX.2.5.2\lib\net35\" /> <ReferenceFolders Include="packages\HarmonyX.2.5.2\lib\net35\" />

View File

@ -208,9 +208,5 @@ namespace UnityExplorer.Inspectors
UIRoot.SetActive(false); UIRoot.SetActive(false);
} }
public override void DoSaveToConfigElement() { }
public override string GetSaveDataFromConfigManager() => null;
} }
} }

View File

@ -55,15 +55,6 @@ namespace UnityExplorer.UI.Panels
ConsoleController.Update(); ConsoleController.Update();
} }
// Saving
public override void DoSaveToConfigElement()
{
ConfigManager.CSConsoleData.Value = this.ToSaveData();
}
public override string GetSaveDataFromConfigManager() => ConfigManager.CSConsoleData.Value;
// UI Construction // UI Construction
public override void OnFinishResize(RectTransform panel) public override void OnFinishResize(RectTransform panel)

View File

@ -27,8 +27,6 @@ namespace UnityExplorer.UI.Panels
public override bool NavButtonWanted => true; public override bool NavButtonWanted => true;
public override bool ShouldSaveActiveState => true; public override bool ShouldSaveActiveState => true;
public override bool ShowByDefault => true; public override bool ShowByDefault => true;
public override string GetSaveDataFromConfigManager() => ConfigManager.ClipboardData.Value;
public override void DoSaveToConfigElement() => ConfigManager.ClipboardData.Value = this.ToSaveData();
private static Text CurrentPasteLabel; private static Text CurrentPasteLabel;

View File

@ -46,10 +46,6 @@ namespace UnityExplorer.UI.Panels
public Text EditorInputText { get; private set; } public Text EditorInputText { get; private set; }
public Text EditorHighlightText { get; private set; } public Text EditorHighlightText { get; private set; }
public override string GetSaveDataFromConfigManager() => ConfigManager.HookManagerData.Value;
public override void DoSaveToConfigElement() => ConfigManager.HookManagerData.Value = this.ToSaveData();
private void OnClassInputAddClicked() private void OnClassInputAddClicked()
{ {
HookManager.Instance.OnClassSelectedForHooks(this.classSelectorInputField.Text); HookManager.Instance.OnClassSelectedForHooks(this.classSelectorInputField.Text);

View File

@ -44,10 +44,6 @@ namespace UnityExplorer.UI.Panels
InspectorManager.OnPanelResized(panel.rect.width); InspectorManager.OnPanelResized(panel.rect.width);
} }
public override string GetSaveDataFromConfigManager() => ConfigManager.InspectorData.Value;
public override void DoSaveToConfigElement() => ConfigManager.InspectorData.Value = this.ToSaveData();
protected internal override void DoSetDefaultPosAndAnchors() protected internal override void DoSetDefaultPosAndAnchors()
{ {
Rect.localPosition = Vector2.zero; Rect.localPosition = Vector2.zero;

View File

@ -144,18 +144,6 @@ namespace UnityExplorer.UI.Panels
RuntimeProvider.Instance.SetColorBlock(cell.Input.Component, color); RuntimeProvider.Instance.SetColorBlock(cell.Input.Component, color);
} }
// Panel save data
public override string GetSaveDataFromConfigManager()
{
return ConfigManager.ConsoleLogData.Value;
}
public override void DoSaveToConfigElement()
{
ConfigManager.ConsoleLogData.Value = this.ToSaveData();
}
protected internal override void DoSetDefaultPosAndAnchors() protected internal override void DoSetDefaultPosAndAnchors()
{ {
Rect.localPosition = Vector2.zero; Rect.localPosition = Vector2.zero;

View File

@ -46,7 +46,7 @@ namespace UnityExplorer.UI.Panels
RuntimeProvider.Instance.SetColorBlock(button.Component, UniversalUI.enabledButtonColor, UniversalUI.enabledButtonColor * 1.2f); RuntimeProvider.Instance.SetColorBlock(button.Component, UniversalUI.enabledButtonColor, UniversalUI.enabledButtonColor * 1.2f);
SelectedTab = tabIndex; SelectedTab = tabIndex;
SaveToConfigManager(); SaveInternalData();
} }
private void DisableTab(int tabIndex) private void DisableTab(int tabIndex)
@ -63,21 +63,12 @@ namespace UnityExplorer.UI.Panels
ObjectSearch.Update(); ObjectSearch.Update();
} }
public override string GetSaveDataFromConfigManager() => ConfigManager.ObjectExplorerData.Value;
public override void DoSaveToConfigElement()
{
ConfigManager.ObjectExplorerData.Value = this.ToSaveData();
}
public override string ToSaveData() public override string ToSaveData()
{ {
string ret = base.ToSaveData(); return string.Join("|", new string[] { base.ToSaveData(), SelectedTab.ToString() });
ret += "|" + SelectedTab;
return ret;
} }
public override void ApplySaveData(string data) protected override void ApplySaveData(string data)
{ {
base.ApplySaveData(data); base.ApplySaveData(data);

View File

@ -55,17 +55,7 @@ namespace UnityExplorer.UI.Panels
CacheObjectControllerHelper.SetCell(cell, index, this.configEntries, null); CacheObjectControllerHelper.SetCell(cell, index, this.configEntries, null);
} }
// Panel save data // UI Construction
public override string GetSaveDataFromConfigManager()
{
return ConfigManager.OptionsPanelData.Value;
}
public override void DoSaveToConfigElement()
{
ConfigManager.OptionsPanelData.Value = this.ToSaveData();
}
protected internal override void DoSetDefaultPosAndAnchors() protected internal override void DoSetDefaultPosAndAnchors()
{ {
@ -76,8 +66,6 @@ namespace UnityExplorer.UI.Panels
Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 600f); Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 600f);
} }
// UI Construction
public override void ConstructPanelContent() public override void ConstructPanelContent()
{ {
// Save button // Save button

View File

@ -92,19 +92,16 @@ namespace UnityExplorer.UI.Panels
protected GameObject uiRoot; protected GameObject uiRoot;
public RectTransform Rect; public RectTransform Rect;
public GameObject content; public GameObject content;
public GameObject titleBar; public GameObject titleBar;
public abstract void ConstructPanelContent();
public virtual void OnFinishResize(RectTransform panel) public virtual void OnFinishResize(RectTransform panel)
{ {
SaveToConfigManager(); SaveInternalData();
} }
public virtual void OnFinishDrag(RectTransform panel) public virtual void OnFinishDrag(RectTransform panel)
{ {
SaveToConfigManager(); SaveInternalData();
} }
public override void SetActive(bool active) public override void SetActive(bool active)
@ -115,7 +112,7 @@ namespace UnityExplorer.UI.Panels
base.SetActive(active); base.SetActive(active);
if (!ApplyingSaveData) if (!ApplyingSaveData)
SaveToConfigManager(); SaveInternalData();
if (NavButtonWanted) if (NavButtonWanted)
{ {
@ -162,29 +159,30 @@ namespace UnityExplorer.UI.Panels
panel.localPosition = pos; panel.localPosition = pos;
} }
#region Save Data // Save Data
public abstract void DoSaveToConfigElement(); public bool ApplyingSaveData { get; set; }
public void SaveToConfigManager() public void SaveInternalData()
{ {
if (UIManager.Initializing) if (UIManager.Initializing)
return; return;
DoSaveToConfigElement(); SetSaveDataToConfigValue();
} }
public abstract string GetSaveDataFromConfigManager(); private void SetSaveDataToConfigValue() => ConfigManager.GetPanelSaveData(this.PanelType).Value = this.ToSaveData();
public bool ApplyingSaveData { get; set; }
public virtual string ToSaveData() public virtual string ToSaveData()
{ {
try try
{ {
return $"{ShouldSaveActiveState && Enabled}" + return string.Join("|", new string[]
$"|{Rect.RectAnchorsToString()}" + {
$"|{Rect.RectPositionToString()}"; $"{ShouldSaveActiveState && Enabled}",
Rect.RectAnchorsToString(),
Rect.RectPositionToString()
});
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -193,7 +191,13 @@ namespace UnityExplorer.UI.Panels
} }
} }
public virtual void ApplySaveData(string data) public virtual void ApplySaveData()
{
string data = ConfigManager.GetPanelSaveData(this.PanelType).Value;
ApplySaveData(data);
}
protected virtual void ApplySaveData(string data)
{ {
if (string.IsNullOrEmpty(data)) if (string.IsNullOrEmpty(data))
return; return;
@ -210,17 +214,14 @@ namespace UnityExplorer.UI.Panels
{ {
ExplorerCore.LogWarning("Invalid or corrupt panel save data! Restoring to default."); ExplorerCore.LogWarning("Invalid or corrupt panel save data! Restoring to default.");
SetTransformDefaults(); SetTransformDefaults();
UIManager.Initializing = false; SetSaveDataToConfigValue();
DoSaveToConfigElement();
ConfigManager.InternalHandler.SaveConfig();
UIManager.Initializing = true;
} }
} }
#endregion
// UI Construction // UI Construction
public abstract void ConstructPanelContent();
public void ConstructUI() public void ConstructUI()
{ {
//this.Enabled = true; //this.Enabled = true;
@ -275,7 +276,7 @@ namespace UnityExplorer.UI.Panels
closeBtn.OnClick += () => closeBtn.OnClick += () =>
{ {
UIManager.SetPanelActive(this.PanelType, false); UIManager.SetPanelActive(this.PanelType, false);
SaveToConfigManager(); SaveInternalData();
}; };
if (!CanDragAndResize) if (!CanDragAndResize)
@ -300,7 +301,7 @@ namespace UnityExplorer.UI.Panels
// apply panel save data or revert to default // apply panel save data or revert to default
try try
{ {
ApplySaveData(GetSaveDataFromConfigManager()); ApplySaveData();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -320,7 +321,7 @@ namespace UnityExplorer.UI.Panels
// simple listener for saving enabled state // simple listener for saving enabled state
this.OnToggleEnabled += (bool val) => this.OnToggleEnabled += (bool val) =>
{ {
SaveToConfigManager(); SaveInternalData();
}; };
ApplyingSaveData = false; ApplyingSaveData = false;

View File

@ -72,8 +72,5 @@ namespace UnityExplorer.UI.Panels
this.Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 500f); this.Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 500f);
this.Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 500f); this.Rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 500f);
} }
public override void DoSaveToConfigElement() { }
public override string GetSaveDataFromConfigManager() => null;
} }
} }

View File

@ -315,12 +315,5 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
UIRoot.SetActive(false); UIRoot.SetActive(false);
} }
public override void DoSaveToConfigElement()
{
// not savable
}
public override string GetSaveDataFromConfigManager() => null;
} }
} }

View File

@ -106,8 +106,8 @@
<HintPath>..\lib\mcs-unity\mcs\bin\Release\mcs.dll</HintPath> <HintPath>..\lib\mcs-unity\mcs\bin\Release\mcs.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="INIFileParser, Version=2.5.2.0, Culture=neutral, PublicKeyToken=79af7b307b65cf3c, processorArchitecture=MSIL"> <Reference Include="Tomlet, Version=3.1.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\ini-parser.2.5.2\lib\net20\INIFileParser.dll</HintPath> <HintPath>packages\Samboy063.Tomlet.3.1.3\lib\net35\Tomlet.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
</ItemGroup> </ItemGroup>

View File

@ -2,7 +2,7 @@
<packages> <packages>
<package id="HarmonyX" version="2.5.2" targetFramework="net35" /> <package id="HarmonyX" version="2.5.2" targetFramework="net35" />
<package id="ILRepack.Lib.MSBuild.Task" version="2.0.18.2" targetFramework="net35" /> <package id="ILRepack.Lib.MSBuild.Task" version="2.0.18.2" targetFramework="net35" />
<package id="ini-parser" version="2.5.2" targetFramework="net35" />
<package id="Mono.Cecil" version="0.10.4" targetFramework="net35" /> <package id="Mono.Cecil" version="0.10.4" targetFramework="net35" />
<package id="Samboy063.Tomlet" version="3.1.3" targetFramework="net472" />
<package id="UniverseLib" version="1.0.6" targetFramework="net35" /> <package id="UniverseLib" version="1.0.6" targetFramework="net35" />
</packages> </packages>