From 27cd31879f2967a0b911d7109dda14ddd1fa987f Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 30 Jan 2017 18:22:22 +0000 Subject: [PATCH 1/2] Add FieldLoader.AllowEmptyEntries attribute. --- OpenRA.Game/FieldLoader.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/OpenRA.Game/FieldLoader.cs b/OpenRA.Game/FieldLoader.cs index 0cb095b299..fad93c4462 100644 --- a/OpenRA.Game/FieldLoader.cs +++ b/OpenRA.Game/FieldLoader.cs @@ -455,7 +455,9 @@ namespace OpenRA if (value == null) return Array.CreateInstance(fieldType.GetElementType(), 0); - var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + var options = field != null && field.HasAttribute() ? + StringSplitOptions.None : StringSplitOptions.RemoveEmptyEntries; + var parts = value.Split(new char[] { ',' }, options); var ret = Array.CreateInstance(fieldType.GetElementType(), parts.Length); for (var i = 0; i < parts.Length; i++) @@ -679,6 +681,13 @@ namespace OpenRA : base(true, true) { } } + [AttributeUsage(AttributeTargets.Field)] + public sealed class AllowEmptyEntriesAttribute : SerializeAttribute + { + public AllowEmptyEntriesAttribute() + : base(allowEmptyEntries: true) { } + } + [AttributeUsage(AttributeTargets.Field)] public sealed class LoadUsingAttribute : SerializeAttribute { @@ -702,11 +711,13 @@ namespace OpenRA public bool FromYamlKey; public bool DictionaryFromYamlKey; public bool Required; + public bool AllowEmptyEntries; - public SerializeAttribute(bool serialize = true, bool required = false) + public SerializeAttribute(bool serialize = true, bool required = false, bool allowEmptyEntries = false) { Serialize = serialize; Required = required; + AllowEmptyEntries = allowEmptyEntries; } internal Func GetLoader(Type type) From 47a013e6c2c29715eabcb31d3d1fce70386a008b Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 30 Jan 2017 17:57:44 +0000 Subject: [PATCH 2/2] Support empty strings to skip condition levels. --- .../Traits/Conditions/ConditionManager.cs | 12 ++++++++++-- .../Traits/Conditions/StackedCondition.cs | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Conditions/ConditionManager.cs b/OpenRA.Mods.Common/Traits/Conditions/ConditionManager.cs index 875733a518..805756aaf5 100644 --- a/OpenRA.Mods.Common/Traits/Conditions/ConditionManager.cs +++ b/OpenRA.Mods.Common/Traits/Conditions/ConditionManager.cs @@ -153,10 +153,18 @@ namespace OpenRA.Mods.Common.Traits var target = (conditionState.Tokens.Count - 1).Clamp(0, sc.Length); var st = stackedTokens[condition]; for (var i = st.Count; i < target; i++) - st.Push(GrantCondition(self, sc[i])); + { + // Empty strings are used to skip unwanted levels + var t = !string.IsNullOrEmpty(sc[i]) ? GrantCondition(self, sc[i]) : InvalidConditionToken; + st.Push(t); + } for (var i = st.Count; i > target; i--) - RevokeCondition(self, st.Pop()); + { + var t = st.Pop(); + if (t != InvalidConditionToken) + RevokeCondition(self, t); + } } } diff --git a/OpenRA.Mods.Common/Traits/Conditions/StackedCondition.cs b/OpenRA.Mods.Common/Traits/Conditions/StackedCondition.cs index dc24965f7b..c2cd3574a8 100644 --- a/OpenRA.Mods.Common/Traits/Conditions/StackedCondition.cs +++ b/OpenRA.Mods.Common/Traits/Conditions/StackedCondition.cs @@ -22,9 +22,11 @@ namespace OpenRA.Mods.Common.Traits public readonly string Condition = null; [FieldLoader.Require] + [FieldLoader.AllowEmptyEntries] [GrantedConditionReference] [Desc("Conditions to grant when the monitored condition is granted multiple times.", - "The first entry is activated at 2x grants, second entry at 3x grants, and so on.")] + "The first entry is activated at 2x grants, second entry at 3x grants, and so on.", + "Use empty entries to skip levels.")] public readonly string[] StackedConditions = { }; }