1 #region Copyright & License Information 2 /* 3 * Copyright 2007-2020 The OpenRA Developers (see AUTHORS) 4 * This file is part of OpenRA, which is free software. It is made 5 * available to you under the terms of the GNU General Public License 6 * as published by the Free Software Foundation, either version 3 of 7 * the License, or (at your option) any later version. For more 8 * information, see COPYING. 9 */ 10 #endregion 11 12 using System; 13 using System.Collections.Generic; 14 using System.Linq; 15 16 namespace OpenRA.Mods.Common.Lint 17 { 18 class CheckUnknownTraitFields : ILintPass, ILintMapPass 19 { NormalizeName(string key)20 string NormalizeName(string key) 21 { 22 var name = key.Split('@')[0]; 23 if (name.StartsWith("-", StringComparison.Ordinal)) 24 return name.Substring(1); 25 26 return name; 27 } 28 CheckActors(IEnumerable<MiniYamlNode> actors, Action<string> emitError, ModData modData)29 void CheckActors(IEnumerable<MiniYamlNode> actors, Action<string> emitError, ModData modData) 30 { 31 foreach (var actor in actors) 32 { 33 foreach (var t in actor.Value.Nodes) 34 { 35 // Removals can never define children or values 36 if (t.Key.StartsWith("-", StringComparison.Ordinal)) 37 { 38 if (t.Value.Nodes.Any()) 39 emitError("{0} {1} defines child nodes, which are not valid for removals.".F(t.Location, t.Key)); 40 41 if (!string.IsNullOrEmpty(t.Value.Value)) 42 emitError("{0} {1} defines a value, which is not valid for removals.".F(t.Location, t.Key)); 43 44 continue; 45 } 46 47 var traitName = NormalizeName(t.Key); 48 49 // Inherits can never define children 50 if (traitName == "Inherits" && t.Value.Nodes.Any()) 51 { 52 emitError("{0} defines child nodes, which are not valid for Inherits.".F(t.Location)); 53 continue; 54 } 55 56 var traitInfo = modData.ObjectCreator.FindType(traitName + "Info"); 57 foreach (var field in t.Value.Nodes) 58 { 59 var fieldName = NormalizeName(field.Key); 60 if (traitInfo.GetField(fieldName) == null) 61 emitError("{0} refers to a trait field `{1}` that does not exist on `{2}`.".F(field.Location, fieldName, traitName)); 62 } 63 } 64 } 65 } 66 ILintPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData)67 void ILintPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData) 68 { 69 foreach (var f in modData.Manifest.Rules) 70 CheckActors(MiniYaml.FromStream(modData.DefaultFileSystem.Open(f), f), emitError, modData); 71 } 72 ILintMapPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Map map)73 void ILintMapPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData, Map map) 74 { 75 if (map.RuleDefinitions != null && map.RuleDefinitions.Value != null) 76 { 77 var mapFiles = FieldLoader.GetValue<string[]>("value", map.RuleDefinitions.Value); 78 foreach (var f in mapFiles) 79 CheckActors(MiniYaml.FromStream(map.Open(f), f), emitError, modData); 80 81 if (map.RuleDefinitions.Nodes.Any()) 82 CheckActors(map.RuleDefinitions.Nodes, emitError, modData); 83 } 84 } 85 } 86 } 87