mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-01-07 18:13:35 +08:00
Separate internal and public configs
This commit is contained in:
parent
1769a4ed8d
commit
0f69833283
@ -18,6 +18,10 @@ namespace UnityExplorer.Core.Config
|
|||||||
|
|
||||||
public object DefaultValue { get; }
|
public object DefaultValue { get; }
|
||||||
|
|
||||||
|
public ConfigHandler Handler => IsInternal
|
||||||
|
? ConfigManager.InternalHandler
|
||||||
|
: ConfigManager.Handler;
|
||||||
|
|
||||||
public T Value
|
public T Value
|
||||||
{
|
{
|
||||||
get => m_value;
|
get => m_value;
|
||||||
@ -51,19 +55,19 @@ namespace UnityExplorer.Core.Config
|
|||||||
|
|
||||||
m_value = value;
|
m_value = value;
|
||||||
|
|
||||||
ConfigManager.Handler.SetConfigValue(this, value);
|
Handler.SetConfigValue(this, value);
|
||||||
|
|
||||||
OnValueChanged?.Invoke(value);
|
OnValueChanged?.Invoke(value);
|
||||||
OnValueChangedNotify?.Invoke();
|
OnValueChangedNotify?.Invoke();
|
||||||
|
|
||||||
ConfigManager.Handler.OnAnyConfigChanged();
|
Handler.OnAnyConfigChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
object IConfigElement.GetLoaderConfigValue() => GetLoaderConfigValue();
|
object IConfigElement.GetLoaderConfigValue() => GetLoaderConfigValue();
|
||||||
|
|
||||||
public T GetLoaderConfigValue()
|
public T GetLoaderConfigValue()
|
||||||
{
|
{
|
||||||
return ConfigManager.Handler.GetConfigValue(this);
|
return Handler.GetConfigValue(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RevertToDefaultValue()
|
public void RevertToDefaultValue()
|
||||||
|
@ -6,6 +6,7 @@ using System.Linq;
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityExplorer.UI;
|
||||||
|
|
||||||
namespace UnityExplorer.Core.Config
|
namespace UnityExplorer.Core.Config
|
||||||
{
|
{
|
||||||
@ -24,34 +25,45 @@ namespace UnityExplorer.Core.Config
|
|||||||
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> Last_Window_Anchors;
|
// internal configs
|
||||||
public static ConfigElement<string> Last_Window_Position;
|
internal static InternalConfigHandler InternalHandler { get; private set; }
|
||||||
public static ConfigElement<bool> Last_DebugConsole_State;
|
|
||||||
public static ConfigElement<bool> Last_SceneExplorer_State;
|
public static ConfigElement<string> SceneExplorerData;
|
||||||
|
public static ConfigElement<string> GameObjectInspectorData;
|
||||||
|
public static ConfigElement<string> MainWindowData;
|
||||||
|
public static ConfigElement<string> DebugConsoleData;
|
||||||
|
|
||||||
internal static readonly Dictionary<string, IConfigElement> ConfigElements = new Dictionary<string, IConfigElement>();
|
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)
|
||||||
{
|
{
|
||||||
Handler = configHandler;
|
Handler = configHandler;
|
||||||
Handler.Init();
|
Handler.Init();
|
||||||
|
|
||||||
|
InternalHandler = new InternalConfigHandler();
|
||||||
|
InternalHandler.Init();
|
||||||
|
|
||||||
CreateConfigElements();
|
CreateConfigElements();
|
||||||
|
|
||||||
Handler.LoadConfig();
|
Handler.LoadConfig();
|
||||||
|
InternalHandler.LoadConfig();
|
||||||
|
|
||||||
//SceneExplorer.OnToggleShow += SceneExplorer_OnToggleShow;
|
//InitConsoleCallback();
|
||||||
//PanelDragger.OnFinishResize += PanelDragger_OnFinishResize;
|
|
||||||
//PanelDragger.OnFinishDrag += PanelDragger_OnFinishDrag;
|
|
||||||
//DebugConsole.OnToggleShow += DebugConsole_OnToggleShow;
|
|
||||||
|
|
||||||
InitConsoleCallback();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void RegisterConfigElement<T>(ConfigElement<T> configElement)
|
internal static void RegisterConfigElement<T>(ConfigElement<T> configElement)
|
||||||
{
|
{
|
||||||
Handler.RegisterConfigElement(configElement);
|
if (!configElement.IsInternal)
|
||||||
ConfigElements.Add(configElement.Name, configElement);
|
{
|
||||||
|
Handler.RegisterConfigElement(configElement);
|
||||||
|
ConfigElements.Add(configElement.Name, configElement);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
InternalHandler.RegisterConfigElement(configElement);
|
||||||
|
InternalConfigs.Add(configElement.Name, configElement);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CreateConfigElements()
|
private static void CreateConfigElements()
|
||||||
@ -90,154 +102,10 @@ namespace UnityExplorer.Core.Config
|
|||||||
|
|
||||||
// Internal configs
|
// Internal configs
|
||||||
|
|
||||||
Last_Window_Anchors = new ConfigElement<string>("Last_Window_Anchors",
|
SceneExplorerData = new ConfigElement<string>("SceneExplorer", "", "", true);
|
||||||
"For internal use, the last anchors of the UnityExplorer window.",
|
GameObjectInspectorData = new ConfigElement<string>("GameObjectInspector", "", "", true);
|
||||||
DEFAULT_WINDOW_ANCHORS,
|
MainWindowData = new ConfigElement<string>("MainWindow", "", "", true);
|
||||||
true);
|
DebugConsoleData = new ConfigElement<string>("DebugConsole", "", "", true);
|
||||||
|
|
||||||
Last_Window_Position = new ConfigElement<string>("Last_Window_Position",
|
|
||||||
"For internal use, the last position of the UnityExplorer window.",
|
|
||||||
DEFAULT_WINDOW_POSITION,
|
|
||||||
true);
|
|
||||||
|
|
||||||
Last_DebugConsole_State = new ConfigElement<bool>("Last_DebugConsole_State",
|
|
||||||
"For internal use, the collapsed state of the Debug Console.",
|
|
||||||
true,
|
|
||||||
true);
|
|
||||||
|
|
||||||
Last_SceneExplorer_State = new ConfigElement<bool>("Last_SceneExplorer_State",
|
|
||||||
"For internal use, the collapsed state of the Scene Explorer.",
|
|
||||||
true,
|
|
||||||
true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal config callback listeners
|
|
||||||
|
|
||||||
private static void PanelDragger_OnFinishResize(RectTransform rect)
|
|
||||||
{
|
|
||||||
Last_Window_Anchors.Value = rect.RectAnchorsToString();
|
|
||||||
PanelDragger_OnFinishDrag(rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void PanelDragger_OnFinishDrag(RectTransform rect)
|
|
||||||
{
|
|
||||||
Last_Window_Position.Value = rect.RectPositionToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void DebugConsole_OnToggleShow(bool showing)
|
|
||||||
{
|
|
||||||
Last_DebugConsole_State.Value = showing;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void SceneExplorer_OnToggleShow(bool showing)
|
|
||||||
{
|
|
||||||
Last_SceneExplorer_State.Value = showing;
|
|
||||||
}
|
|
||||||
|
|
||||||
#region CONSOLE ONEXIT CALLBACK
|
|
||||||
|
|
||||||
internal static void InitConsoleCallback()
|
|
||||||
{
|
|
||||||
handler = new ConsoleEventDelegate(ConsoleEventCallback);
|
|
||||||
SetConsoleCtrlHandler(handler, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool ConsoleEventCallback(int eventType)
|
|
||||||
{
|
|
||||||
// 2 is Console Quit
|
|
||||||
if (eventType == 2)
|
|
||||||
Handler.SaveConfig();
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static ConsoleEventDelegate handler;
|
|
||||||
private delegate bool ConsoleEventDelegate(int eventType);
|
|
||||||
|
|
||||||
[DllImport("kernel32.dll", SetLastError = true)]
|
|
||||||
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region WINDOW ANCHORS / POSITION HELPERS
|
|
||||||
|
|
||||||
// Window Anchors helpers
|
|
||||||
|
|
||||||
private const string DEFAULT_WINDOW_ANCHORS = "0.25,0.10,0.78,0.95";
|
|
||||||
private const string DEFAULT_WINDOW_POSITION = "0,0";
|
|
||||||
|
|
||||||
internal static CultureInfo _enCulture = new CultureInfo("en-US");
|
|
||||||
|
|
||||||
internal static string RectAnchorsToString(this RectTransform rect)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return string.Format(_enCulture, "{0},{1},{2},{3}", new object[]
|
|
||||||
{
|
|
||||||
rect.anchorMin.x,
|
|
||||||
rect.anchorMin.y,
|
|
||||||
rect.anchorMax.x,
|
|
||||||
rect.anchorMax.y
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return DEFAULT_WINDOW_ANCHORS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void SetAnchorsFromString(this RectTransform panel, string stringAnchors)
|
|
||||||
{
|
|
||||||
Vector4 anchors;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var split = stringAnchors.Split(',');
|
|
||||||
|
|
||||||
if (split.Length != 4)
|
|
||||||
throw new Exception();
|
|
||||||
|
|
||||||
anchors.x = float.Parse(split[0], _enCulture);
|
|
||||||
anchors.y = float.Parse(split[1], _enCulture);
|
|
||||||
anchors.z = float.Parse(split[2], _enCulture);
|
|
||||||
anchors.w = float.Parse(split[3], _enCulture);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
anchors = new Vector4(0.25f, 0.1f, 0.78f, 0.95f);
|
|
||||||
}
|
|
||||||
|
|
||||||
panel.anchorMin = new Vector2(anchors.x, anchors.y);
|
|
||||||
panel.anchorMax = new Vector2(anchors.z, anchors.w);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static string RectPositionToString(this RectTransform rect)
|
|
||||||
{
|
|
||||||
return string.Format(_enCulture, "{0},{1}", new object[]
|
|
||||||
{
|
|
||||||
rect.localPosition.x, rect.localPosition.y
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void SetPositionFromString(this RectTransform rect, string stringPosition)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var split = stringPosition.Split(',');
|
|
||||||
|
|
||||||
if (split.Length != 2)
|
|
||||||
throw new Exception();
|
|
||||||
|
|
||||||
Vector3 vector = rect.localPosition;
|
|
||||||
vector.x = float.Parse(split[0], _enCulture);
|
|
||||||
vector.y = float.Parse(split[1], _enCulture);
|
|
||||||
rect.localPosition = vector;
|
|
||||||
}
|
|
||||||
catch //(Exception ex)
|
|
||||||
{
|
|
||||||
//ExplorerCore.LogWarning("Exception setting window position: " + ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
104
src/Core/Config/InternalConfigHandler.cs
Normal file
104
src/Core/Config/InternalConfigHandler.cs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
using IniParser.Parser;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace UnityExplorer.Core.Config
|
||||||
|
{
|
||||||
|
public class InternalConfigHandler : ConfigHandler
|
||||||
|
{
|
||||||
|
internal static IniDataParser _parser;
|
||||||
|
internal static string INI_PATH;
|
||||||
|
|
||||||
|
public override void Init()
|
||||||
|
{
|
||||||
|
INI_PATH = Path.Combine(ExplorerCore.Loader.ExplorerFolder, "config.ini");
|
||||||
|
_parser = new IniDataParser();
|
||||||
|
_parser.Configuration.CommentString = "#";
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void LoadConfig()
|
||||||
|
{
|
||||||
|
if (!TryLoadConfig())
|
||||||
|
SaveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void RegisterConfigElement<T>(ConfigElement<T> element)
|
||||||
|
{
|
||||||
|
// Not necessary
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void SetConfigValue<T>(ConfigElement<T> element, T value)
|
||||||
|
{
|
||||||
|
// Not necessary, just save.
|
||||||
|
SaveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override T GetConfigValue<T>(ConfigElement<T> element)
|
||||||
|
{
|
||||||
|
// Not necessary, just return the value.
|
||||||
|
return element.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryLoadConfig()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!File.Exists(INI_PATH))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
string ini = File.ReadAllText(INI_PATH);
|
||||||
|
|
||||||
|
var data = _parser.Parse(ini);
|
||||||
|
|
||||||
|
foreach (var config in data.Sections["Config"])
|
||||||
|
{
|
||||||
|
if (ConfigManager.InternalConfigs.TryGetValue(config.KeyName, out IConfigElement configElement))
|
||||||
|
configElement.BoxedValue = StringToConfigValue(config.Value, configElement.ElementType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnAnyConfigChanged()
|
||||||
|
{
|
||||||
|
SaveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void SaveConfig()
|
||||||
|
{
|
||||||
|
var data = new IniParser.Model.IniData();
|
||||||
|
|
||||||
|
data.Sections.AddSection("Config");
|
||||||
|
var sec = data.Sections["Config"];
|
||||||
|
|
||||||
|
foreach (var entry in ConfigManager.InternalConfigs)
|
||||||
|
sec.AddKey(entry.Key, entry.Value.BoxedValue.ToString());
|
||||||
|
|
||||||
|
if (!Directory.Exists(ExplorerCore.Loader.ConfigFolder))
|
||||||
|
Directory.CreateDirectory(ExplorerCore.Loader.ConfigFolder);
|
||||||
|
|
||||||
|
File.WriteAllText(INI_PATH, data.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<InputAssemblies Include="$(OutputPath)$(AssemblyName).dll" />
|
<InputAssemblies Include="$(OutputPath)$(AssemblyName).dll" />
|
||||||
<InputAssemblies Include="..\lib\mcs.dll" />
|
<InputAssemblies Include="..\lib\mcs.dll" />
|
||||||
<InputAssemblies Include="..\lib\INIFileParser.dll" Condition="'$(IsStandalone)' == 'true'"/>
|
<InputAssemblies Include="..\lib\INIFileParser.dll" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ILRepack
|
<ILRepack
|
||||||
Parallel="true"
|
Parallel="true"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user