Rewrite sequence linting / missing file handling.

- Distinguish between missing sequences and missing sprites
- Lint default sequences as well as maps
- Improved performance
- Correctly handle null images
This commit is contained in:
Paul Chote
2020-08-12 23:53:22 +01:00
committed by abcdefg30
parent b8e60ca8ec
commit 7803686aec
21 changed files with 177 additions and 180 deletions

View File

@@ -12,172 +12,136 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Lint
{
class CheckSequences : ILintMapPass
class CheckSequences : ILintRulesPass
{
Action<string> emitError;
List<MiniYamlNode> sequenceDefinitions;
public void Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Map map)
void ILintRulesPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Ruleset rules)
{
if (map.SequenceDefinitions == null)
return;
// Custom maps define rules.Sequences, default mod rules leave it null.
if (rules.Sequences == null)
{
foreach (var kv in modData.DefaultSequences)
{
Console.WriteLine("Testing default sequences for {0}", kv.Key);
Run(emitError, emitWarning, rules, kv.Value);
}
}
else if (!modData.DefaultSequences.Values.Contains(rules.Sequences))
Run(emitError, emitWarning, rules, rules.Sequences);
}
this.emitError = emitError;
sequenceDefinitions = MiniYaml.Load(map, modData.Manifest.Sequences, map.SequenceDefinitions);
var rules = map.Rules;
void Run(Action<string> emitError, Action<string> emitWarning, Ruleset rules, SequenceProvider sequences)
{
var factions = rules.Actors["world"].TraitInfos<FactionInfo>().Select(f => f.InternalName).ToArray();
var sequenceProviders = new[] { rules.Sequences };
foreach (var actorInfo in rules.Actors)
{
foreach (var renderInfo in actorInfo.Value.TraitInfos<RenderSpritesInfo>())
// Actors may have 0 or 1 RenderSprites traits
var renderInfo = actorInfo.Value.TraitInfoOrDefault<RenderSpritesInfo>();
if (renderInfo == null)
continue;
var images = new HashSet<string>()
{
foreach (var faction in factions)
{
foreach (var sequenceProvider in sequenceProviders)
{
var image = renderInfo.GetImage(actorInfo.Value, sequenceProvider, faction);
if (sequenceDefinitions.All(s => s.Key != image.ToLowerInvariant()))
emitError("Sprite image {0} from actor {1} using faction {2} has no sequence definition."
.F(image, actorInfo.Value.Name, faction));
}
}
}
renderInfo.GetImage(actorInfo.Value, sequences, null).ToLowerInvariant()
};
// Some actors define faction-specific artwork
foreach (var faction in factions)
images.Add(renderInfo.GetImage(actorInfo.Value, sequences, faction).ToLowerInvariant());
foreach (var traitInfo in actorInfo.Value.TraitInfos<TraitInfo>())
{
// Remove the "Info" suffix
var traitName = traitInfo.GetType().Name;
traitName = traitName.Remove(traitName.Length - 4);
var fields = traitInfo.GetType().GetFields();
foreach (var field in fields)
{
if (field.HasAttribute<SequenceReferenceAttribute>())
var sequenceReference = field.GetCustomAttributes<SequenceReferenceAttribute>(true).FirstOrDefault();
if (sequenceReference == null)
continue;
// Some sequences may specify their own Image override
IEnumerable<string> sequenceImages = images;
if (!string.IsNullOrEmpty(sequenceReference.ImageReference))
{
var sequences = LintExts.GetFieldValues(traitInfo, field, emitError);
foreach (var sequence in sequences)
var imageField = fields.First(f => f.Name == sequenceReference.ImageReference);
var imageOverride = (string)imageField.GetValue(traitInfo);
if (string.IsNullOrEmpty(imageOverride))
{
if (string.IsNullOrEmpty(sequence))
continue;
var renderInfo = actorInfo.Value.TraitInfos<RenderSpritesInfo>().FirstOrDefault();
if (renderInfo == null)
continue;
foreach (var faction in factions)
{
var sequenceReference = field.GetCustomAttributes<SequenceReferenceAttribute>(true).FirstOrDefault();
if (sequenceReference != null && !string.IsNullOrEmpty(sequenceReference.ImageReference))
{
var imageField = fields.FirstOrDefault(f => f.Name == sequenceReference.ImageReference);
if (imageField != null)
{
foreach (var imageOverride in LintExts.GetFieldValues(traitInfo, imageField, emitError))
{
if (string.IsNullOrEmpty(imageOverride))
{
if (!sequenceReference.ActorNameFallback)
{
emitWarning("Custom sprite image of actor {0} is null and there is no fallback.".F(actorInfo.Value.Name));
continue;
}
foreach (var sequenceProvider in sequenceProviders)
{
var image = renderInfo.GetImage(actorInfo.Value, sequenceProvider, faction);
CheckDefinitions(image, sequenceReference, actorInfo, sequence, faction, field, traitInfo);
}
continue;
}
if (sequenceDefinitions.All(s => s.Key != imageOverride.ToLowerInvariant()))
emitError("Custom sprite image {0} from actor {1} has no sequence definition.".F(imageOverride, actorInfo.Value.Name));
else
CheckDefinitions(imageOverride, sequenceReference, actorInfo, sequence, faction, field, traitInfo);
}
}
}
else
{
foreach (var sequenceProvider in sequenceProviders)
{
var image = renderInfo.GetImage(actorInfo.Value, sequenceProvider, faction);
CheckDefinitions(image, sequenceReference, actorInfo, sequence, faction, field, traitInfo);
}
}
}
if (!sequenceReference.AllowNullImage)
emitError("Actor type `{0}` trait `{1}` must define a value for `{2}`".F(actorInfo.Value.Name, traitName, sequenceReference.ImageReference));
continue;
}
sequenceImages = new[] { imageOverride.ToLowerInvariant() };
}
}
}
foreach (var weaponInfo in rules.Weapons)
{
var projectileInfo = weaponInfo.Value.Projectile;
if (projectileInfo == null)
continue;
var fields = projectileInfo.GetType().GetFields();
foreach (var field in fields)
{
if (field.HasAttribute<SequenceReferenceAttribute>())
foreach (var sequence in LintExts.GetFieldValues(traitInfo, field, emitError))
{
var sequences = LintExts.GetFieldValues(projectileInfo, field, emitError);
foreach (var sequence in sequences)
{
if (string.IsNullOrEmpty(sequence))
continue;
if (string.IsNullOrEmpty(sequence))
continue;
var sequenceReference = field.GetCustomAttributes<SequenceReferenceAttribute>(true).FirstOrDefault();
if (sequenceReference != null && !string.IsNullOrEmpty(sequenceReference.ImageReference))
foreach (var i in sequenceImages)
{
if (sequenceReference.Prefix)
{
var imageField = fields.FirstOrDefault(f => f.Name == sequenceReference.ImageReference);
if (imageField != null)
{
foreach (var imageOverride in LintExts.GetFieldValues(projectileInfo, imageField, emitError))
{
if (!string.IsNullOrEmpty(imageOverride))
{
var definitions = sequenceDefinitions.FirstOrDefault(n => n.Key == imageOverride.ToLowerInvariant());
if (definitions == null)
emitError("Can't find sequence definition for projectile image {0} at weapon {1}.".F(imageOverride, weaponInfo.Key));
else if (!definitions.Value.Nodes.Any(n => n.Key == sequence))
emitError("Projectile sprite image {0} from weapon {1} does not define sequence {2} from field {3} of {4}"
.F(imageOverride, weaponInfo.Key, sequence, field.Name, projectileInfo));
}
}
}
// TODO: Remove prefixed sequence references and instead use explicit lists of lintable references
if (!sequences.Sequences(i).Any(s => s.StartsWith(sequence)))
emitWarning("Actor type `{0}` trait `{1}` field `{2}` defines a prefix `{3}` that does not match any sequences on image `{4}`.".F(actorInfo.Value.Name, traitName, field.Name, sequence, i));
}
else if (!sequences.HasSequence(i, sequence))
emitError("Actor type `{0}` trait `{1}` field `{2}` references an undefined sequence `{3}` on image `{4}`.".F(actorInfo.Value.Name, traitName, field.Name, sequence, i));
}
}
}
}
}
}
void CheckDefinitions(string image, SequenceReferenceAttribute sequenceReference,
KeyValuePair<string, ActorInfo> actorInfo, string sequence, string faction, FieldInfo field, TraitInfo traitInfo)
{
var definitions = sequenceDefinitions.FirstOrDefault(n => n.Key == image.ToLowerInvariant());
if (definitions != null)
foreach (var weaponInfo in rules.Weapons)
{
if (sequenceReference != null && sequenceReference.Prefix)
var projectileInfo = weaponInfo.Value.Projectile;
if (projectileInfo == null)
continue;
var fields = projectileInfo.GetType().GetFields();
foreach (var field in fields)
{
if (!definitions.Value.Nodes.Any(n => n.Key.StartsWith(sequence)))
emitError("Sprite image {0} from actor {1} of faction {2} does not define sequence prefix {3} from field {4} of {5}"
.F(image, actorInfo.Value.Name, faction, sequence, field.Name, traitInfo));
}
else if (definitions.Value.Nodes.All(n => n.Key != sequence))
{
emitError("Sprite image {0} from actor {1} of faction {2} does not define sequence {3} from field {4} of {5}"
.F(image, actorInfo.Value.Name, faction, sequence, field.Name, traitInfo));
var sequenceReference = field.GetCustomAttributes<SequenceReferenceAttribute>(true).FirstOrDefault();
if (sequenceReference == null)
continue;
// All weapon sequences must specify their corresponding image
var image = ((string)fields.First(f => f.Name == sequenceReference.ImageReference).GetValue(projectileInfo));
if (string.IsNullOrEmpty(image))
{
if (!sequenceReference.AllowNullImage)
emitError("Weapon type `{0}` projectile field `{1}` must define a value".F(weaponInfo.Key, sequenceReference.ImageReference));
continue;
}
image = image.ToLowerInvariant();
foreach (var sequence in LintExts.GetFieldValues(projectileInfo, field, emitError))
{
if (string.IsNullOrEmpty(sequence))
continue;
if (sequenceReference.Prefix)
{
// TODO: Remove prefixed sequence references and instead use explicit lists of lintable references
if (!sequences.Sequences(image).Any(s => s.StartsWith(sequence)))
emitWarning("Weapon type `{0}` projectile field `{1}` defines a prefix `{2}` that does not match any sequences on image `{3}`.".F(weaponInfo.Key, field.Name, sequence, image));
}
else if (!sequences.HasSequence(image, sequence))
emitError("Weapon type `{0}` projectile field `{1}` references an undefined sequence `{2}` on image `{3}`.".F(weaponInfo.Key, field.Name, sequence, image));
}
}
}
}