diff --git a/OpenRA.Mods.RA/Lint/CheckActorReferences.cs b/OpenRA.Mods.RA/Lint/CheckActorReferences.cs new file mode 100644 index 0000000000..6ffe7d77f4 --- /dev/null +++ b/OpenRA.Mods.RA/Lint/CheckActorReferences.cs @@ -0,0 +1,72 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using OpenRA.Traits; + +namespace OpenRA.Mods.RA +{ + public class CheckActorReferences : ILintPass + { + Action EmitError; + + public void Run(Action emitError, Action emitWarning) + { + EmitError = emitError; + + foreach (var actorInfo in Rules.Info) + foreach (var traitInfo in actorInfo.Value.Traits.WithInterface()) + CheckTrait(actorInfo.Value, traitInfo); + } + + void CheckTrait(ActorInfo actorInfo, ITraitInfo traitInfo) + { + var actualType = traitInfo.GetType(); + foreach (var field in actualType.GetFields()) + { + if (field.HasAttribute()) + CheckReference(actorInfo, traitInfo, field, Rules.Info, "actor"); + if (field.HasAttribute()) + CheckReference(actorInfo, traitInfo, field, Rules.Weapons, "weapon"); + if (field.HasAttribute()) + CheckReference(actorInfo, traitInfo, field, Rules.Voices, "voice"); + } + } + + string[] GetFieldValues(ITraitInfo traitInfo, FieldInfo fieldInfo) + { + var type = fieldInfo.FieldType; + if (type == typeof(string)) + return new string[] { (string)fieldInfo.GetValue(traitInfo) }; + if (type == typeof(string[])) + return (string[])fieldInfo.GetValue(traitInfo); + + EmitError("Bad type for reference on {0}.{1}. Supported types: string, string[]" + .F(traitInfo.GetType().Name, fieldInfo.Name)); + + return new string[] { }; + } + + void CheckReference(ActorInfo actorInfo, ITraitInfo traitInfo, FieldInfo fieldInfo, + Dictionary dict, string type) + { + var values = GetFieldValues(traitInfo, fieldInfo); + foreach (var v in values) + if (v != null && !dict.ContainsKey(v.ToLowerInvariant())) + EmitError("{0}.{1}.{2}: Missing {3} `{4}`." + .F(actorInfo.Name, traitInfo.GetType().Name, fieldInfo.Name, type, v)); + } + } +} + diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index b3eb56524b..3f24a1c818 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -355,6 +355,7 @@ + diff --git a/RALint/RALint.cs b/RALint/RALint.cs index f4a09dd9ac..7222d0e2fe 100644 --- a/RALint/RALint.cs +++ b/RALint/RALint.cs @@ -10,8 +10,8 @@ using System; using System.Collections.Generic; -using System.Reflection; using System.Linq; +using System.Reflection; using OpenRA; using OpenRA.FileFormats; using OpenRA.Traits; @@ -45,11 +45,7 @@ namespace RALint Game.modData = new ModData(args); Rules.LoadRules(Game.modData.Manifest, new Map()); - foreach (var actorInfo in Rules.Info) - foreach (var traitInfo in actorInfo.Value.Traits.WithInterface()) - CheckTrait(actorInfo.Value, traitInfo); - - foreach (var customPassType in Game.modData.ObjectCreator + foreach (var customPassType in Game.modData.ObjectCreator .GetTypesImplementing()) { var customPass = (ILintPass)Game.modData.ObjectCreator @@ -74,43 +70,5 @@ namespace RALint return 1; } } - - static void CheckTrait(ActorInfo actorInfo, ITraitInfo traitInfo) - { - var actualType = traitInfo.GetType(); - foreach (var field in actualType.GetFields()) - { - if (field.HasAttribute()) - CheckReference(actorInfo, traitInfo, field, Rules.Info, "actor"); - if (field.HasAttribute()) - CheckReference(actorInfo, traitInfo, field, Rules.Weapons, "weapon"); - if (field.HasAttribute()) - CheckReference(actorInfo, traitInfo, field, Rules.Voices, "voice"); - } - } - - static string[] GetFieldValues(ITraitInfo traitInfo, FieldInfo fieldInfo) - { - var type = fieldInfo.FieldType; - if (type == typeof(string)) - return new string[] { (string)fieldInfo.GetValue(traitInfo) }; - if (type == typeof(string[])) - return (string[])fieldInfo.GetValue(traitInfo); - - EmitError("Bad type for reference on {0}.{1}. Supported types: string, string[]" - .F(traitInfo.GetType().Name, fieldInfo.Name)); - - return new string[] { }; - } - - static void CheckReference(ActorInfo actorInfo, ITraitInfo traitInfo, FieldInfo fieldInfo, - Dictionary dict, string type) - { - var values = GetFieldValues(traitInfo, fieldInfo); - foreach (var v in values) - if (v != null && !dict.ContainsKey(v.ToLowerInvariant())) - EmitError("{0}.{1}.{2}: Missing {3} `{4}`." - .F(actorInfo.Name, traitInfo.GetType().Name, fieldInfo.Name, type, v)); - } } }