Add SequenceReference support for dictionaries.

This commit is contained in:
Paul Chote
2020-08-12 23:22:38 +01:00
committed by abcdefg30
parent 7803686aec
commit 94180f6a0a
6 changed files with 80 additions and 13 deletions

View File

@@ -13,6 +13,13 @@ using System;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
public enum LintDictionaryReference
{
None = 0,
Keys = 1,
Values = 2
}
[AttributeUsage(AttributeTargets.Field)] [AttributeUsage(AttributeTargets.Field)]
public sealed class ActorReferenceAttribute : Attribute public sealed class ActorReferenceAttribute : Attribute
{ {
@@ -29,15 +36,19 @@ namespace OpenRA.Traits
[AttributeUsage(AttributeTargets.Field)] [AttributeUsage(AttributeTargets.Field)]
public sealed class SequenceReferenceAttribute : Attribute public sealed class SequenceReferenceAttribute : Attribute
{ {
public readonly string ImageReference; // The field name in the same trait info that contains the image name. // The field name in the same trait info that contains the image name.
public readonly string ImageReference;
public readonly bool Prefix; public readonly bool Prefix;
public readonly bool AllowNullImage; public readonly bool AllowNullImage;
public readonly LintDictionaryReference DictionaryReference;
public SequenceReferenceAttribute(string imageReference = null, bool prefix = false, bool allowNullImage = false) public SequenceReferenceAttribute(string imageReference = null, bool prefix = false, bool allowNullImage = false,
LintDictionaryReference dictionaryReference = LintDictionaryReference.None)
{ {
ImageReference = imageReference; ImageReference = imageReference;
Prefix = prefix; Prefix = prefix;
AllowNullImage = allowNullImage; AllowNullImage = allowNullImage;
DictionaryReference = dictionaryReference;
} }
} }

View File

@@ -12,6 +12,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render; using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits; using OpenRA.Traits;
@@ -83,7 +84,7 @@ namespace OpenRA.Mods.Common.Lint
sequenceImages = new[] { imageOverride.ToLowerInvariant() }; sequenceImages = new[] { imageOverride.ToLowerInvariant() };
} }
foreach (var sequence in LintExts.GetFieldValues(traitInfo, field, emitError)) foreach (var sequence in LintExts.GetFieldValues(traitInfo, field, emitError, sequenceReference.DictionaryReference))
{ {
if (string.IsNullOrEmpty(sequence)) if (string.IsNullOrEmpty(sequence))
continue; continue;
@@ -128,7 +129,7 @@ namespace OpenRA.Mods.Common.Lint
} }
image = image.ToLowerInvariant(); image = image.ToLowerInvariant();
foreach (var sequence in LintExts.GetFieldValues(projectileInfo, field, emitError)) foreach (var sequence in LintExts.GetFieldValues(projectileInfo, field, emitError, sequenceReference.DictionaryReference))
{ {
if (string.IsNullOrEmpty(sequence)) if (string.IsNullOrEmpty(sequence))
continue; continue;

View File

@@ -15,12 +15,14 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using OpenRA.Support; using OpenRA.Support;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Lint namespace OpenRA.Mods.Common.Lint
{ {
public class LintExts public class LintExts
{ {
public static IEnumerable<string> GetFieldValues(object ruleInfo, FieldInfo fieldInfo, Action<string> emitError) public static IEnumerable<string> GetFieldValues(object ruleInfo, FieldInfo fieldInfo, Action<string> emitError,
LintDictionaryReference dictionaryReference = LintDictionaryReference.None)
{ {
var type = fieldInfo.FieldType; var type = fieldInfo.FieldType;
if (type == typeof(string)) if (type == typeof(string))
@@ -35,11 +37,38 @@ namespace OpenRA.Mods.Common.Lint
return expr != null ? expr.Variables : Enumerable.Empty<string>(); return expr != null ? expr.Variables : Enumerable.Empty<string>();
} }
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: string, IEnumerable<string>, BooleanExpression, IntegerExpression" if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
.F(ruleInfo.GetType().Name, fieldInfo.Name)); {
// Use an intermediate list to cover the unlikely case where both keys and values are lintable
var dictionaryValues = new List<string>();
if (dictionaryReference.HasFlag(LintDictionaryReference.Keys) && type.GenericTypeArguments[0] == typeof(string))
dictionaryValues.AddRange((IEnumerable<string>)((IDictionary)fieldInfo.GetValue(ruleInfo)).Keys);
if (dictionaryReference.HasFlag(LintDictionaryReference.Values) && type.GenericTypeArguments[1] == typeof(string))
dictionaryValues.AddRange((IEnumerable<string>)((IDictionary)fieldInfo.GetValue(ruleInfo)).Values);
if (dictionaryReference.HasFlag(LintDictionaryReference.Values) && type.GenericTypeArguments[1] == typeof(IEnumerable<string>))
foreach (var row in (IEnumerable<IEnumerable<string>>)((IDictionary)fieldInfo.GetValue(ruleInfo)).Values)
dictionaryValues.AddRange(row);
return dictionaryValues;
}
var supportedTypes = new[]
{
"string", "IEnumerable<string>",
"Dictionary<string, T> (LintDictionaryReference.Keys)",
"Dictionary<T, string> (LintDictionaryReference.Values)",
"Dictionary<T, IEnumerable<string>> (LintDictionaryReference.Values)",
"BooleanExpression", "IntegerExpression"
};
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: {2}"
.F(ruleInfo.GetType().Name, fieldInfo.Name, supportedTypes.JoinWith(", ")));
} }
public static IEnumerable<string> GetPropertyValues(object ruleInfo, PropertyInfo propertyInfo, Action<string> emitError) public static IEnumerable<string> GetPropertyValues(object ruleInfo, PropertyInfo propertyInfo, Action<string> emitError,
LintDictionaryReference dictionaryReference = LintDictionaryReference.None)
{ {
var type = propertyInfo.PropertyType; var type = propertyInfo.PropertyType;
if (type == typeof(string)) if (type == typeof(string))
@@ -54,8 +83,34 @@ namespace OpenRA.Mods.Common.Lint
return expr != null ? expr.Variables : Enumerable.Empty<string>(); return expr != null ? expr.Variables : Enumerable.Empty<string>();
} }
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: string, IEnumerable<string>, BooleanExpression, IntegerExpression" if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
.F(ruleInfo.GetType().Name, propertyInfo.Name)); {
// Use an intermediate list to cover the unlikely case where both keys and values are lintable
var dictionaryValues = new List<string>();
if (dictionaryReference.HasFlag(LintDictionaryReference.Keys) && type.GenericTypeArguments[0] == typeof(string))
dictionaryValues.AddRange((IEnumerable<string>)((IDictionary)propertyInfo.GetValue(ruleInfo)).Keys);
if (dictionaryReference.HasFlag(LintDictionaryReference.Values) && type.GenericTypeArguments[1] == typeof(string))
dictionaryValues.AddRange((IEnumerable<string>)((IDictionary)propertyInfo.GetValue(ruleInfo)).Values);
if (dictionaryReference.HasFlag(LintDictionaryReference.Values) && type.GenericTypeArguments[1] == typeof(IEnumerable<string>))
foreach (var row in (IEnumerable<IEnumerable<string>>)((IDictionary)propertyInfo.GetValue(ruleInfo)).Values)
dictionaryValues.AddRange(row);
return dictionaryValues;
}
var supportedTypes = new[]
{
"string", "IEnumerable<string>",
"Dictionary<string, T> (LintDictionaryReference.Keys)",
"Dictionary<T, string> (LintDictionaryReference.Values)",
"Dictionary<T, IEnumerable<string>> (LintDictionaryReference.Values)",
"BooleanExpression", "IntegerExpression"
};
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: {2}"
.F(ruleInfo.GetType().Name, propertyInfo.Name, supportedTypes.JoinWith(", ")));
} }
} }
} }

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.Common.Traits.Render
[Desc("Sequence used for full pips that aren't defined in CustomPipSequences.")] [Desc("Sequence used for full pips that aren't defined in CustomPipSequences.")]
public readonly string FullSequence = "pip-green"; public readonly string FullSequence = "pip-green";
// TODO: [SequenceReference] isn't smart enough to use Dictionaries. [SequenceReference("Image", dictionaryReference: LintDictionaryReference.Values)]
[Desc("Pip sequence to use for specific passenger actors.")] [Desc("Pip sequence to use for specific passenger actors.")]
public readonly Dictionary<string, string> CustomPipSequences = new Dictionary<string, string>(); public readonly Dictionary<string, string> CustomPipSequences = new Dictionary<string, string>();

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.Common.Traits.Render
[Desc("Sequence used for full pips that aren't defined in ResourceSequences.")] [Desc("Sequence used for full pips that aren't defined in ResourceSequences.")]
public readonly string FullSequence = "pip-green"; public readonly string FullSequence = "pip-green";
// TODO: [SequenceReference] isn't smart enough to use Dictionaries. [SequenceReference("Image", dictionaryReference: LintDictionaryReference.Values)]
[Desc("Pip sequence to use for specific resource types.")] [Desc("Pip sequence to use for specific resource types.")]
public readonly Dictionary<string, string> ResourceSequences = new Dictionary<string, string>(); public readonly Dictionary<string, string> ResourceSequences = new Dictionary<string, string>();

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Common.Traits.Render
[SequenceReference] [SequenceReference]
public readonly string DefaultAttackSequence = null; public readonly string DefaultAttackSequence = null;
// TODO: [SequenceReference] isn't smart enough to use Dictionaries. [SequenceReference(dictionaryReference: LintDictionaryReference.Values)]
[Desc("Attack sequence to use for each armament.")] [Desc("Attack sequence to use for each armament.")]
public readonly Dictionary<string, string> AttackSequences = new Dictionary<string, string>(); public readonly Dictionary<string, string> AttackSequences = new Dictionary<string, string>();