From 8ebe6d7979930b349c7dc8139b2624eb266ab4c5 Mon Sep 17 00:00:00 2001 From: Alexandr Korsak Date: Mon, 20 Jan 2020 13:16:21 +0100 Subject: [PATCH 1/3] add ability to pass custom getter, setter function to use own variables outside of CVar --- .../LunarConsole/Scripts/Actions/CVar.cs | 228 ++++++++++++++---- 1 file changed, 183 insertions(+), 45 deletions(-) diff --git a/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs b/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs index 13c77a58..e4caa96f 100644 --- a/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs +++ b/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs @@ -19,7 +19,8 @@ // limitations under the License. // -using System; +using System; +using System.CodeDom; using System.Collections; using System.Collections.Generic; using System.Reflection; @@ -39,7 +40,7 @@ public enum CVarType String } - struct CValue + public struct CValue { public string stringValue; public int intValue; @@ -73,7 +74,7 @@ public bool IsValid } public enum CFlags - { + { /// /// No flags (default value) /// @@ -90,6 +91,100 @@ public enum CFlags NoArchive = 1 << 2 } + public interface ICVarProxy + { + CValue Value { get; set; } + } + + public class CVarProxy : ICVarProxy + { + private Func GetFunc { get; } + private Func SetFunc { get; } + private CValue _value; + + public CVarProxy() + { + GetFunc = () => + { + if (typeof(T) == typeof(int)) + { + return (T) Convert.ChangeType(_value.intValue, typeof(T)); + } + + if (typeof(T) == typeof(float)) + { + return (T) Convert.ChangeType(_value.floatValue, typeof(T)); + } + + if (typeof(T) == typeof(string)) + { + return (T) Convert.ChangeType(_value.stringValue, typeof(T)); + } + + throw new ArgumentException(); + }; + + SetFunc = value => value; + } + + public CVarProxy(Func getFunc, Func setFunc) + { + GetFunc = getFunc; + SetFunc = setFunc; + } + + public CValue Value + { + get + { + var value = GetFunc.Invoke(); + + if (typeof(T) == typeof(int)) + { + _value.intValue = (int) Convert.ChangeType(value, typeof(int)); + } + else if (typeof(T) == typeof(float)) + { + _value.floatValue = (float) Convert.ChangeType(value, typeof(float)); + } + else if (typeof(T) == typeof(string)) + { + _value.stringValue = (string) Convert.ChangeType(value, typeof(string)); + } + else + { + throw new ArgumentException(); + } + + return _value; + } + set + { + _value = value; + + if (typeof(T) == typeof(int)) + { + SetFunc.Invoke((T) Convert.ChangeType(_value.intValue, typeof(T))); + return; + } + + if (typeof(T) == typeof(float)) + { + SetFunc.Invoke((T) Convert.ChangeType(_value.floatValue, typeof(T))); + return; + } + + if (typeof(T) == typeof(string)) + { + SetFunc.Invoke((T) Convert.ChangeType(_value.stringValue, typeof(T))); + return; + } + + throw new ArgumentException(); + } + } + } + public class CVar : IEquatable, IComparable { private static int s_nextId; @@ -99,38 +194,57 @@ public class CVar : IEquatable, IComparable private readonly CVarType m_type; private readonly CFlags m_flags; - private CValue m_value; private CValue m_defaultValue; private CVarValueRange m_range = CVarValueRange.Undefined; private CVarChangedDelegateList m_delegateList; - public CVar(string name, bool defaultValue, CFlags flags = CFlags.None) + private CVarProxy _intProxy; + private CVarProxy _floatProxy; + private CVarProxy _stringProxy; + + private ICVarProxy Proxy + { + get + { + if (_intProxy != null) return _intProxy; + if (_floatProxy != null) return _floatProxy; + if (_stringProxy != null) return _stringProxy; + + throw new ArgumentException(); + } + } + + public CVar(string name, bool defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) : this(name, CVarType.Boolean, flags) { - this.IntValue = defaultValue ? 1 : 0; - m_defaultValue = m_value; + _intProxy = proxy ?? new CVarProxy(); + IntValue = defaultValue ? 1 : 0; + m_defaultValue = new CValue { intValue = defaultValue ? 1 : 0 }; } - public CVar(string name, int defaultValue, CFlags flags = CFlags.None) + public CVar(string name, int defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) : this(name, CVarType.Integer, flags) { - this.IntValue = defaultValue; - m_defaultValue = m_value; + _intProxy = proxy ?? new CVarProxy(); + IntValue = defaultValue; + m_defaultValue = new CValue { intValue = defaultValue }; } - public CVar(string name, float defaultValue, CFlags flags = CFlags.None) + public CVar(string name, float defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) : this(name, CVarType.Float, flags) { - this.FloatValue = defaultValue; - m_defaultValue = m_value; + _floatProxy = proxy ?? new CVarProxy(); + FloatValue = defaultValue; + m_defaultValue = new CValue { floatValue = defaultValue }; } - public CVar(string name, string defaultValue, CFlags flags = CFlags.None) + public CVar(string name, string defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) : this(name, CVarType.String, flags) { - this.Value = defaultValue; - m_defaultValue = m_value; + _stringProxy = proxy ?? new CVarProxy(); + Value = defaultValue; + m_defaultValue = new CValue { stringValue = defaultValue }; } private CVar(string name, CVarType type, CFlags flags) @@ -219,7 +333,7 @@ public bool Equals(CVar other) { return other != null && other.m_name == m_name && - other.m_value.Equals(ref m_value) && + other.Proxy.Value.Equals(Proxy.Value) && other.m_defaultValue.Equals(ref m_defaultValue) && other.m_type == m_type; } @@ -268,14 +382,17 @@ public bool IsString public string Value { - get { return m_value.stringValue; } + get { return Proxy.Value.stringValue; } set { - bool changed = m_value.stringValue != value; + var changed = Proxy.Value.stringValue != value; - m_value.stringValue = value; - m_value.floatValue = IsInt || IsFloat ? StringUtils.ParseFloat(value, 0.0f) : 0.0f; - m_value.intValue = IsInt || IsFloat ? (int)FloatValue : 0; + Proxy.Value = new CValue + { + stringValue = value, + floatValue = IsInt || IsFloat ? StringUtils.ParseFloat(value, 0.0f) : 0.0f, + intValue = IsInt || IsFloat ? (int)FloatValue : 0 + }; if (changed) { @@ -302,14 +419,17 @@ public bool IsInt public int IntValue { - get { return m_value.intValue; } + get => Proxy.Value.intValue; set { - bool changed = m_value.intValue != value; + var changed = Proxy.Value.intValue != value; - m_value.stringValue = StringUtils.ToString(value); - m_value.intValue = value; - m_value.floatValue = (float)value; + Proxy.Value = new CValue + { + stringValue = StringUtils.ToString(value), + intValue = value, + floatValue = value + }; if (changed) { @@ -325,16 +445,19 @@ public bool IsFloat public float FloatValue { - get { return m_value.floatValue; } + get { return Proxy.Value.floatValue; } set { - float oldValue = m_value.floatValue; + var changed = Proxy.Value.floatValue != value; - m_value.stringValue = StringUtils.ToString(value); - m_value.intValue = (int)value; - m_value.floatValue = value; + Proxy.Value = new CValue + { + stringValue = StringUtils.ToString(value), + intValue = (int) value, + floatValue = value + }; - if (oldValue != value) + if (changed) { NotifyValueChanged(); } @@ -348,17 +471,23 @@ public bool IsBool public bool BoolValue { - get { return m_value.intValue != 0; } - set { this.IntValue = value ? 1 : 0; } + get { return Proxy.Value.intValue != 0; } + set + { + Proxy.Value = new CValue + { + intValue = value ? 1 : 0 + }; + } } public bool IsDefault { - get { return m_value.Equals(m_defaultValue); } + get { return Proxy.Value.Equals(m_defaultValue); } set { - bool changed = this.IsDefault ^ value; - m_value = m_defaultValue; + bool changed = IsDefault ^ value; + Proxy.Value = m_defaultValue; if (changed) { @@ -383,22 +512,22 @@ public CFlags Flags public static implicit operator string(CVar cvar) { - return cvar.m_value.stringValue; + return cvar.Proxy.Value.stringValue; } public static implicit operator int(CVar cvar) { - return cvar.m_value.intValue; + return cvar.Proxy.Value.intValue; } public static implicit operator float(CVar cvar) { - return cvar.m_value.floatValue; + return cvar.Proxy.Value.floatValue; } public static implicit operator bool(CVar cvar) { - return cvar.m_value.intValue != 0; + return cvar.Proxy.Value.intValue != 0; } #endregion @@ -470,7 +599,7 @@ public IEnumerator GetEnumerator() #region IEnumerable implementation - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + IEnumerator IEnumerable.GetEnumerator() { return m_variables.GetEnumerator(); } @@ -483,7 +612,7 @@ public int Count } } - [AttributeUsage (AttributeTargets.Field, Inherited = true, AllowMultiple = false)] + [AttributeUsage (AttributeTargets.Field)] public sealed class CVarRangeAttribute : Attribute { public readonly float min; @@ -497,6 +626,15 @@ public CVarRangeAttribute(float min, float max) } } + [AttributeUsage (AttributeTargets.Field)] + public sealed class CVarProxyAttribute : Attribute + { + public CVarProxyAttribute(CVar _var) + { + + } + } + [AttributeUsage(AttributeTargets.Class)] public class CVarContainerAttribute : Attribute { @@ -538,4 +676,4 @@ static void NullCVarChangedDelegate(CVar cvar) { } } -} \ No newline at end of file +} From daa5eaecbce207d75c5f3722e3b9d9ccbe0e4c60 Mon Sep 17 00:00:00 2001 From: Alexandr Korsak Date: Mon, 20 Jan 2020 13:18:30 +0100 Subject: [PATCH 2/3] remove unused attribute --- Project/Assets/LunarConsole/Scripts/Actions/CVar.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs b/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs index e4caa96f..ec40a55e 100644 --- a/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs +++ b/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs @@ -626,15 +626,6 @@ public CVarRangeAttribute(float min, float max) } } - [AttributeUsage (AttributeTargets.Field)] - public sealed class CVarProxyAttribute : Attribute - { - public CVarProxyAttribute(CVar _var) - { - - } - } - [AttributeUsage(AttributeTargets.Class)] public class CVarContainerAttribute : Attribute { From 82b7072f8bc4358a0da080eb505c27145e528ca2 Mon Sep 17 00:00:00 2001 From: Alexandr Korsak Date: Mon, 20 Jan 2020 15:05:34 +0100 Subject: [PATCH 3/3] move converts to CValue class --- .../LunarConsole/Scripts/Actions/CVar.cs | 155 ++++++++++++------ 1 file changed, 108 insertions(+), 47 deletions(-) diff --git a/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs b/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs index ec40a55e..72ce6577 100644 --- a/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs +++ b/Project/Assets/LunarConsole/Scripts/Actions/CVar.cs @@ -40,17 +40,79 @@ public enum CVarType String } - public struct CValue + public class CValue { public string stringValue; public int intValue; public float floatValue; + public bool boolValue; + + public CVarType Type { get; } + + public CValue(CVarType type, string value) + { + Type = type; + + stringValue = value; + floatValue = IsInt || IsFloat ? StringUtils.ParseFloat(value, 0.0f) : 0.0f; + intValue = IsInt || IsFloat ? (int) floatValue : 0; + } + + public CValue(CVarType type, int value) + { + Type = type; + + stringValue = StringUtils.ToString(value); + intValue = value; + floatValue = value; + } + + public CValue(CVarType type, float value) + { + Type = type; + + stringValue = StringUtils.ToString(value); + intValue = (int) value; + floatValue = value; + } + + + public CValue(CVarType type, bool value) + { + Type = type; + + boolValue = value; + intValue = value ? 1 : 0; + floatValue = intValue; + stringValue = intValue.ToString(); + } public bool Equals(ref CValue other) { return other.intValue == intValue && other.floatValue == floatValue && - other.stringValue == stringValue; + other.stringValue == stringValue && + other.boolValue == boolValue; + } + + public bool IsInt + { + get { return Type == CVarType.Integer || Type == CVarType.Boolean; } + } + + public bool IsString + { + get { return Type == CVarType.String; } + } + + public bool IsFloat + { + get { return Type == CVarType.Float; } + } + + public bool IsBool + { + get { return Type == CVarType.Boolean; } } } @@ -121,6 +183,11 @@ public CVarProxy() return (T) Convert.ChangeType(_value.stringValue, typeof(T)); } + if (typeof(T) == typeof(bool)) + { + return (T) Convert.ChangeType(_value.boolValue, typeof(T)); + } + throw new ArgumentException(); }; @@ -141,15 +208,19 @@ public CValue Value if (typeof(T) == typeof(int)) { - _value.intValue = (int) Convert.ChangeType(value, typeof(int)); + _value = new CValue(CVarType.Integer, (int) Convert.ChangeType(value, typeof(int))); } else if (typeof(T) == typeof(float)) { - _value.floatValue = (float) Convert.ChangeType(value, typeof(float)); + _value = new CValue(CVarType.Float, (float) Convert.ChangeType(value, typeof(float))); } else if (typeof(T) == typeof(string)) { - _value.stringValue = (string) Convert.ChangeType(value, typeof(string)); + _value = new CValue(CVarType.String, (string) Convert.ChangeType(value, typeof(string))); + } + else if (typeof(T) == typeof(bool)) + { + _value = new CValue(CVarType.Boolean, (bool) Convert.ChangeType(value, typeof(bool))); } else { @@ -180,6 +251,12 @@ public CValue Value return; } + if (typeof(T) == typeof(bool)) + { + SetFunc.Invoke((T) Convert.ChangeType(_value.boolValue, typeof(T))); + return; + } + throw new ArgumentException(); } } @@ -199,52 +276,54 @@ public class CVar : IEquatable, IComparable private CVarChangedDelegateList m_delegateList; - private CVarProxy _intProxy; - private CVarProxy _floatProxy; - private CVarProxy _stringProxy; + private CVarProxy m_intProxy; + private CVarProxy m_boolProxy; + private CVarProxy m_floatProxy; + private CVarProxy m_stringProxy; private ICVarProxy Proxy { get { - if (_intProxy != null) return _intProxy; - if (_floatProxy != null) return _floatProxy; - if (_stringProxy != null) return _stringProxy; + if (m_intProxy != null) return m_intProxy; + if (m_boolProxy != null) return m_boolProxy; + if (m_floatProxy != null) return m_floatProxy; + if (m_stringProxy != null) return m_stringProxy; throw new ArgumentException(); } } - public CVar(string name, bool defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) + public CVar(string name, bool defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) : this(name, CVarType.Boolean, flags) { - _intProxy = proxy ?? new CVarProxy(); - IntValue = defaultValue ? 1 : 0; - m_defaultValue = new CValue { intValue = defaultValue ? 1 : 0 }; + m_boolProxy = proxy ?? new CVarProxy(); + BoolValue = defaultValue; + m_defaultValue = new CValue(m_type, defaultValue); } public CVar(string name, int defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) : this(name, CVarType.Integer, flags) { - _intProxy = proxy ?? new CVarProxy(); + m_intProxy = proxy ?? new CVarProxy(); IntValue = defaultValue; - m_defaultValue = new CValue { intValue = defaultValue }; + m_defaultValue = new CValue(m_type, defaultValue); } public CVar(string name, float defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) : this(name, CVarType.Float, flags) { - _floatProxy = proxy ?? new CVarProxy(); + m_floatProxy = proxy ?? new CVarProxy(); FloatValue = defaultValue; - m_defaultValue = new CValue { floatValue = defaultValue }; + m_defaultValue = new CValue(m_type, defaultValue); } public CVar(string name, string defaultValue, CFlags flags = CFlags.None, CVarProxy proxy = null) : this(name, CVarType.String, flags) { - _stringProxy = proxy ?? new CVarProxy(); + m_stringProxy = proxy ?? new CVarProxy(); Value = defaultValue; - m_defaultValue = new CValue { stringValue = defaultValue }; + m_defaultValue = new CValue(m_type, defaultValue); } private CVar(string name, CVarType type, CFlags flags) @@ -377,7 +456,7 @@ public string DefaultValue public bool IsString { - get { return m_type == CVarType.String; } + get { return Proxy.Value.IsString; } } public string Value @@ -387,12 +466,7 @@ public string Value { var changed = Proxy.Value.stringValue != value; - Proxy.Value = new CValue - { - stringValue = value, - floatValue = IsInt || IsFloat ? StringUtils.ParseFloat(value, 0.0f) : 0.0f, - intValue = IsInt || IsFloat ? (int)FloatValue : 0 - }; + Proxy.Value = new CValue(m_type, value); if (changed) { @@ -414,7 +488,7 @@ public bool HasRange public bool IsInt { - get { return m_type == CVarType.Integer || m_type == CVarType.Boolean; } + get { return Proxy.Value.IsInt; } } public int IntValue @@ -424,12 +498,7 @@ public int IntValue { var changed = Proxy.Value.intValue != value; - Proxy.Value = new CValue - { - stringValue = StringUtils.ToString(value), - intValue = value, - floatValue = value - }; + Proxy.Value = new CValue(m_type, value); if (changed) { @@ -440,7 +509,7 @@ public int IntValue public bool IsFloat { - get { return m_type == CVarType.Float; } + get { return Proxy.Value.Type == CVarType.Float; } } public float FloatValue @@ -450,12 +519,7 @@ public float FloatValue { var changed = Proxy.Value.floatValue != value; - Proxy.Value = new CValue - { - stringValue = StringUtils.ToString(value), - intValue = (int) value, - floatValue = value - }; + Proxy.Value = new CValue(m_type, value); if (changed) { @@ -466,7 +530,7 @@ public float FloatValue public bool IsBool { - get { return m_type == CVarType.Boolean; } + get { return Proxy.Value.IsBool; } } public bool BoolValue @@ -474,10 +538,7 @@ public bool BoolValue get { return Proxy.Value.intValue != 0; } set { - Proxy.Value = new CValue - { - intValue = value ? 1 : 0 - }; + Proxy.Value = new CValue(m_type, value); } }