From 0b8d7708efc5cd8e7a94658efa363259e180047b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 16 Aug 2015 09:25:54 +0200 Subject: [PATCH] add more lint interfaces to reduce boiler plate --- OpenRA.Game/Traits/TraitsInterfaces.cs | 4 +- .../Lint/CheckActorReferences.cs | 9 +-- OpenRA.Mods.Common/Lint/CheckActors.cs | 5 +- OpenRA.Mods.Common/Lint/CheckDeathTypes.cs | 9 +-- .../Lint/CheckDefaultVisibility.cs | 9 +-- OpenRA.Mods.Common/Lint/CheckMapCordon.cs | 5 +- OpenRA.Mods.Common/Lint/CheckMapRules.cs | 30 --------- OpenRA.Mods.Common/Lint/CheckPalettes.cs | 9 +-- OpenRA.Mods.Common/Lint/CheckPlayers.cs | 5 +- .../Lint/CheckRevealFootprint.cs | 9 +-- OpenRA.Mods.Common/Lint/CheckSequences.cs | 2 +- .../Lint/CheckSyncAnnotations.cs | 5 +- .../Lint/CheckTraitPrerequisites.cs | 11 ++-- OpenRA.Mods.Common/Lint/CheckUpgrades.cs | 9 +-- .../Lint/CheckVoiceReferences.cs | 9 +-- .../Lint/LintBuildablePrerequisites.cs | 9 +-- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 - .../UtilityCommands/CheckYaml.cs | 64 ++++++++++++++----- 18 files changed, 75 insertions(+), 129 deletions(-) delete mode 100644 OpenRA.Mods.Common/Lint/CheckMapRules.cs diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 00526d6d00..ffd7ebb581 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -327,7 +327,9 @@ namespace OpenRA.Traits Stance oldStance, Stance newStance); } - public interface ILintPass { void Run(Action emitError, Action emitWarning, Map map); } + public interface ILintPass { void Run(Action emitError, Action emitWarning); } + public interface ILintMapPass { void Run(Action emitError, Action emitWarning, Map map); } + public interface ILintRulesPass { void Run(Action emitError, Action emitWarning, Ruleset rules); } public interface IObjectivesPanel { diff --git a/OpenRA.Mods.Common/Lint/CheckActorReferences.cs b/OpenRA.Mods.Common/Lint/CheckActorReferences.cs index d38b518975..49eeea08dc 100644 --- a/OpenRA.Mods.Common/Lint/CheckActorReferences.cs +++ b/OpenRA.Mods.Common/Lint/CheckActorReferences.cs @@ -15,17 +15,12 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - public class CheckActorReferences : ILintPass + public class CheckActorReferences : ILintRulesPass { Action emitError; - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - this.emitError = emitError; foreach (var actorInfo in rules.Actors) diff --git a/OpenRA.Mods.Common/Lint/CheckActors.cs b/OpenRA.Mods.Common/Lint/CheckActors.cs index 3db292eb11..8a21991038 100644 --- a/OpenRA.Mods.Common/Lint/CheckActors.cs +++ b/OpenRA.Mods.Common/Lint/CheckActors.cs @@ -14,13 +14,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - public class CheckActors : ILintPass + public class CheckActors : ILintMapPass { public void Run(Action emitError, Action emitWarning, Map map) { - if (map == null) - return; - var actorTypes = map.ActorDefinitions.Select(a => a.Value.Value); foreach (var actor in actorTypes) if (!map.Rules.Actors.Keys.Contains(actor.ToLowerInvariant())) diff --git a/OpenRA.Mods.Common/Lint/CheckDeathTypes.cs b/OpenRA.Mods.Common/Lint/CheckDeathTypes.cs index 8e6efc7b82..2dde6ecbc1 100644 --- a/OpenRA.Mods.Common/Lint/CheckDeathTypes.cs +++ b/OpenRA.Mods.Common/Lint/CheckDeathTypes.cs @@ -16,15 +16,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - class CheckDeathTypes : ILintPass + class CheckDeathTypes : ILintRulesPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - foreach (var actorInfo in rules.Actors) { var animations = actorInfo.Value.Traits.WithInterface().ToList(); diff --git a/OpenRA.Mods.Common/Lint/CheckDefaultVisibility.cs b/OpenRA.Mods.Common/Lint/CheckDefaultVisibility.cs index 696b2b1cf8..5752f4f019 100644 --- a/OpenRA.Mods.Common/Lint/CheckDefaultVisibility.cs +++ b/OpenRA.Mods.Common/Lint/CheckDefaultVisibility.cs @@ -16,15 +16,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - class CheckDefaultVisibility : ILintPass + class CheckDefaultVisibility : ILintRulesPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - foreach (var actorInfo in rules.Actors) { if (actorInfo.Key.StartsWith("^")) diff --git a/OpenRA.Mods.Common/Lint/CheckMapCordon.cs b/OpenRA.Mods.Common/Lint/CheckMapCordon.cs index 242431b9b4..7cdd24c19b 100644 --- a/OpenRA.Mods.Common/Lint/CheckMapCordon.cs +++ b/OpenRA.Mods.Common/Lint/CheckMapCordon.cs @@ -14,13 +14,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - public class CheckMapCordon : ILintPass + public class CheckMapCordon : ILintMapPass { public void Run(Action emitError, Action emitWarning, Map map) { - if (map == null) - return; - if (map.Bounds.Left == 0 || map.Bounds.Top == 0 || map.Bounds.Right == map.MapSize.X || map.Bounds.Bottom == map.MapSize.Y) emitError("This map does not define a valid cordon.\n" diff --git a/OpenRA.Mods.Common/Lint/CheckMapRules.cs b/OpenRA.Mods.Common/Lint/CheckMapRules.cs deleted file mode 100644 index 563992dddf..0000000000 --- a/OpenRA.Mods.Common/Lint/CheckMapRules.cs +++ /dev/null @@ -1,30 +0,0 @@ -#region Copyright & License Information -/* - * Copyright 2007-2015 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 OpenRA.Traits; - -namespace OpenRA.Mods.Common.Lint -{ - public class CheckMapRules : ILintPass - { - public void Run(Action emitError, Action emitWarning, Map map) - { - try - { - Game.ModData.RulesetCache.Load(map); - } - catch (Exception e) - { - emitError(e.Message); - } - } - } -} \ No newline at end of file diff --git a/OpenRA.Mods.Common/Lint/CheckPalettes.cs b/OpenRA.Mods.Common/Lint/CheckPalettes.cs index bc32cf45c9..47f8835112 100644 --- a/OpenRA.Mods.Common/Lint/CheckPalettes.cs +++ b/OpenRA.Mods.Common/Lint/CheckPalettes.cs @@ -16,15 +16,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - class CheckPalettes : ILintPass + class CheckPalettes : ILintRulesPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - var palettes = GetPalettes(emitError, rules).ToList(); foreach (var actorInfo in rules.Actors) diff --git a/OpenRA.Mods.Common/Lint/CheckPlayers.cs b/OpenRA.Mods.Common/Lint/CheckPlayers.cs index ba118cad44..3968812ddf 100644 --- a/OpenRA.Mods.Common/Lint/CheckPlayers.cs +++ b/OpenRA.Mods.Common/Lint/CheckPlayers.cs @@ -15,13 +15,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - public class CheckPlayers : ILintPass + public class CheckPlayers : ILintMapPass { public void Run(Action emitError, Action emitWarning, Map map) { - if (map == null) - return; - var players = new MapPlayers(map.PlayerDefinitions).Players; var playerNames = players.Values.Select(p => p.Name).ToHashSet(); diff --git a/OpenRA.Mods.Common/Lint/CheckRevealFootprint.cs b/OpenRA.Mods.Common/Lint/CheckRevealFootprint.cs index 2973a16cf5..16147b33c4 100644 --- a/OpenRA.Mods.Common/Lint/CheckRevealFootprint.cs +++ b/OpenRA.Mods.Common/Lint/CheckRevealFootprint.cs @@ -16,15 +16,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - class CheckRevealFootprint : ILintPass + class CheckRevealFootprint : ILintRulesPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - foreach (var actorInfo in rules.Actors) { if (actorInfo.Key.StartsWith("^")) diff --git a/OpenRA.Mods.Common/Lint/CheckSequences.cs b/OpenRA.Mods.Common/Lint/CheckSequences.cs index aaadeb79b3..f163833ef4 100644 --- a/OpenRA.Mods.Common/Lint/CheckSequences.cs +++ b/OpenRA.Mods.Common/Lint/CheckSequences.cs @@ -18,7 +18,7 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - class CheckSequences : ILintPass + class CheckSequences : ILintMapPass { Action emitError; diff --git a/OpenRA.Mods.Common/Lint/CheckSyncAnnotations.cs b/OpenRA.Mods.Common/Lint/CheckSyncAnnotations.cs index 9802106bb1..9215590ec2 100644 --- a/OpenRA.Mods.Common/Lint/CheckSyncAnnotations.cs +++ b/OpenRA.Mods.Common/Lint/CheckSyncAnnotations.cs @@ -17,11 +17,8 @@ namespace OpenRA.Mods.Common.Lint { class CheckSyncAnnotations : ILintPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning) { - if (map != null) - return; - /* first, check all the types implementing ISync */ foreach (var t in Game.ModData.ObjectCreator.GetTypesImplementing()) if (!HasAnySyncFields(t)) diff --git a/OpenRA.Mods.Common/Lint/CheckTraitPrerequisites.cs b/OpenRA.Mods.Common/Lint/CheckTraitPrerequisites.cs index 2a7c9db1f0..3e7917e9f9 100644 --- a/OpenRA.Mods.Common/Lint/CheckTraitPrerequisites.cs +++ b/OpenRA.Mods.Common/Lint/CheckTraitPrerequisites.cs @@ -14,16 +14,12 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - public class CheckTraitPrerequisites : ILintPass + public class CheckTraitPrerequisites : ILintRulesPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - foreach (var actorInfo in rules.Actors.Where(a => !a.Key.StartsWith("^"))) + { try { var hasTraits = actorInfo.Value.TraitsInConstructOrder().Any(); @@ -34,6 +30,7 @@ namespace OpenRA.Mods.Common.Lint { emitError("Actor {0} is not constructible; failure: {1}".F(actorInfo.Key, e.Message)); } + } } } } \ No newline at end of file diff --git a/OpenRA.Mods.Common/Lint/CheckUpgrades.cs b/OpenRA.Mods.Common/Lint/CheckUpgrades.cs index f01c365f99..73006c958b 100644 --- a/OpenRA.Mods.Common/Lint/CheckUpgrades.cs +++ b/OpenRA.Mods.Common/Lint/CheckUpgrades.cs @@ -16,15 +16,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - public class CheckUpgrades : ILintPass + public class CheckUpgrades : ILintRulesPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - CheckUpgradesValidity(emitError, rules); CheckUpgradesUsage(emitError, emitWarning, rules); } diff --git a/OpenRA.Mods.Common/Lint/CheckVoiceReferences.cs b/OpenRA.Mods.Common/Lint/CheckVoiceReferences.cs index 6a40db2483..4dc86ce289 100644 --- a/OpenRA.Mods.Common/Lint/CheckVoiceReferences.cs +++ b/OpenRA.Mods.Common/Lint/CheckVoiceReferences.cs @@ -15,15 +15,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - public class CheckVoiceReferences : ILintPass + public class CheckVoiceReferences : ILintRulesPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any() && !map.VoiceDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - foreach (var actorInfo in rules.Actors) { foreach (var traitInfo in actorInfo.Value.Traits.WithInterface()) diff --git a/OpenRA.Mods.Common/Lint/LintBuildablePrerequisites.cs b/OpenRA.Mods.Common/Lint/LintBuildablePrerequisites.cs index 9f1a50db46..a2e15a0d88 100644 --- a/OpenRA.Mods.Common/Lint/LintBuildablePrerequisites.cs +++ b/OpenRA.Mods.Common/Lint/LintBuildablePrerequisites.cs @@ -15,15 +15,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Lint { - class LintBuildablePrerequisites : ILintPass + class LintBuildablePrerequisites : ILintRulesPass { - public void Run(Action emitError, Action emitWarning, Map map) + public void Run(Action emitError, Action emitWarning, Ruleset rules) { - if (map != null && !map.RuleDefinitions.Any()) - return; - - var rules = map == null ? Game.ModData.DefaultRules : map.Rules; - // ProvidesPrerequisite allows arbitrary prereq definitions var customPrereqs = rules.Actors.SelectMany(a => a.Value.Traits .WithInterface().Select(p => p.Prerequisite ?? a.Value.Name)); diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 374ce1713f..7198c4d69a 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -183,7 +183,6 @@ - diff --git a/OpenRA.Mods.Common/UtilityCommands/CheckYaml.cs b/OpenRA.Mods.Common/UtilityCommands/CheckYaml.cs index 34d7a4cb21..0e3cadc59e 100644 --- a/OpenRA.Mods.Common/UtilityCommands/CheckYaml.cs +++ b/OpenRA.Mods.Common/UtilityCommands/CheckYaml.cs @@ -52,7 +52,25 @@ namespace OpenRA.Mods.Common.UtilityCommands var maps = new List(); if (args.Length < 2) { - maps.Add(null); + Console.WriteLine("Testing mod: {0}".F(Game.ModData.Manifest.Mod.Title)); + + // Run all rule checks on the default mod rules. + CheckRules(Game.ModData.DefaultRules); + + // Run all generic (not mod-level) checks here. + foreach (var customPassType in Game.ModData.ObjectCreator.GetTypesImplementing()) + { + try + { + var customPass = (ILintPass)Game.ModData.ObjectCreator.CreateBasic(customPassType); + customPass.Run(EmitError, EmitWarning); + } + catch (Exception e) + { + EmitError("{0} failed with exception: {1}".F(customPassType, e)); + } + } + Game.ModData.MapCache.LoadMaps(); maps.AddRange(Game.ModData.MapCache .Where(m => m.Status == MapStatus.Available) @@ -63,29 +81,24 @@ namespace OpenRA.Mods.Common.UtilityCommands foreach (var testMap in maps) { - if (testMap != null) - { - Console.WriteLine("Testing map: {0}".F(testMap.Title)); - testMap.PreloadRules(); - } - else - { - Console.WriteLine("Testing mod: {0}".F(Game.ModData.Manifest.Mod.Title)); - } + Console.WriteLine("Testing map: {0}".F(testMap.Title)); + testMap.PreloadRules(); - foreach (var customPassType in Game.ModData.ObjectCreator - .GetTypesImplementing()) + // Run all rule checks on the map if it defines custom rules. + if (testMap.RuleDefinitions.Any() || testMap.VoiceDefinitions.Any() || testMap.WeaponDefinitions.Any()) + CheckRules(testMap.Rules, testMap); + + // Run all map-level checks here. + foreach (var customMapPassType in Game.ModData.ObjectCreator.GetTypesImplementing()) { try { - var customPass = (ILintPass)Game.ModData.ObjectCreator - .CreateBasic(customPassType); - - customPass.Run(EmitError, EmitWarning, testMap); + var customMapPass = (ILintMapPass)Game.ModData.ObjectCreator.CreateBasic(customMapPassType); + customMapPass.Run(EmitError, EmitWarning, testMap); } catch (Exception e) { - EmitError("{0} failed with exception: {1}".F(customPassType, e)); + EmitError("{0} failed with exception: {1}".F(customMapPassType, e)); } } } @@ -102,5 +115,22 @@ namespace OpenRA.Mods.Common.UtilityCommands Environment.Exit(1); } } + + void CheckRules(Ruleset rules, Map map = null) + { + foreach (var customRulesPassType in Game.ModData.ObjectCreator.GetTypesImplementing()) + { + try + { + Game.ModData.RulesetCache.Load(map); + var customRulesPass = (ILintRulesPass)Game.ModData.ObjectCreator.CreateBasic(customRulesPassType); + customRulesPass.Run(EmitError, EmitWarning, rules); + } + catch (Exception e) + { + EmitError("{0} failed with exception: {1}".F(customRulesPassType, e)); + } + } + } } }