2021-03-30 19:50:04 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace UnityExplorer.Core.Config
|
|
|
|
|
{
|
|
|
|
|
public class ConfigElement<T> : IConfigElement
|
|
|
|
|
{
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
public string Description { get; }
|
|
|
|
|
|
|
|
|
|
public bool IsInternal { get; }
|
|
|
|
|
public Type ElementType => typeof(T);
|
|
|
|
|
|
|
|
|
|
public Action<T> OnValueChanged;
|
|
|
|
|
public Action OnValueChangedNotify { get; set; }
|
|
|
|
|
|
2021-03-31 01:42:32 +11:00
|
|
|
|
public object DefaultValue { get; }
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
public T Value
|
|
|
|
|
{
|
|
|
|
|
get => m_value;
|
|
|
|
|
set => SetValue(value);
|
|
|
|
|
}
|
|
|
|
|
private T m_value;
|
|
|
|
|
|
|
|
|
|
object IConfigElement.BoxedValue
|
|
|
|
|
{
|
|
|
|
|
get => m_value;
|
|
|
|
|
set => SetValue((T)value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 01:42:32 +11:00
|
|
|
|
public ConfigElement(string name, string description, T defaultValue, bool isInternal = false)
|
2021-03-30 19:50:04 +11:00
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
Description = description;
|
|
|
|
|
|
|
|
|
|
m_value = defaultValue;
|
2021-03-31 01:42:32 +11:00
|
|
|
|
DefaultValue = defaultValue;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
|
|
|
|
|
IsInternal = isInternal;
|
|
|
|
|
|
|
|
|
|
ConfigManager.RegisterConfigElement(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetValue(T value)
|
|
|
|
|
{
|
2021-04-02 17:06:49 +11:00
|
|
|
|
if ((m_value == null && value == null) || (m_value != null && m_value.Equals(value)))
|
2021-03-30 19:50:04 +11:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_value = value;
|
|
|
|
|
|
|
|
|
|
ConfigManager.Handler.SetConfigValue(this, value);
|
|
|
|
|
|
|
|
|
|
OnValueChanged?.Invoke(value);
|
|
|
|
|
OnValueChangedNotify?.Invoke();
|
2021-03-31 01:42:32 +11:00
|
|
|
|
|
|
|
|
|
ConfigManager.Handler.OnAnyConfigChanged();
|
2021-03-30 19:50:04 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object IConfigElement.GetLoaderConfigValue() => GetLoaderConfigValue();
|
|
|
|
|
|
|
|
|
|
public T GetLoaderConfigValue()
|
|
|
|
|
{
|
|
|
|
|
return ConfigManager.Handler.GetConfigValue(this);
|
|
|
|
|
}
|
2021-03-31 01:42:32 +11:00
|
|
|
|
|
|
|
|
|
public void RevertToDefaultValue()
|
|
|
|
|
{
|
|
|
|
|
Value = (T)DefaultValue;
|
|
|
|
|
}
|
2021-03-30 19:50:04 +11:00
|
|
|
|
}
|
|
|
|
|
}
|