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.Linq;
14 using OpenRA.Mods.Common.Traits;
15 using OpenRA.Traits;
16 
17 namespace OpenRA.Mods.Common.Lint
18 {
19 	public class CheckLocomotorReferences : ILintRulesPass
20 	{
Run(Action<string> emitError, Action<string> emitWarning, Ruleset rules)21 		public void Run(Action<string> emitError, Action<string> emitWarning, Ruleset rules)
22 		{
23 			var worldActor = rules.Actors["world"];
24 			var locomotorInfos = worldActor.TraitInfos<LocomotorInfo>().ToArray();
25 			foreach (var li in locomotorInfos)
26 				foreach (var otherLocomotor in locomotorInfos)
27 					if (li != otherLocomotor && li.Name == otherLocomotor.Name)
28 						emitError("There is more than one Locomotor with name {0}!".F(li.Name));
29 
30 			foreach (var actorInfo in rules.Actors)
31 			{
32 				foreach (var traitInfo in actorInfo.Value.TraitInfos<ITraitInfo>())
33 				{
34 					var fields = traitInfo.GetType().GetFields().Where(f => f.HasAttribute<LocomotorReferenceAttribute>());
35 					foreach (var field in fields)
36 					{
37 						var locomotors = LintExts.GetFieldValues(traitInfo, field, emitError);
38 						foreach (var locomotor in locomotors)
39 						{
40 							if (string.IsNullOrEmpty(locomotor))
41 								continue;
42 
43 							CheckLocomotors(actorInfo.Value, emitError, rules, locomotorInfos, locomotor);
44 						}
45 					}
46 				}
47 			}
48 		}
49 
CheckLocomotors(ActorInfo actorInfo, Action<string> emitError, Ruleset rules, LocomotorInfo[] locomotorInfos, string locomotor)50 		void CheckLocomotors(ActorInfo actorInfo, Action<string> emitError, Ruleset rules, LocomotorInfo[] locomotorInfos, string locomotor)
51 		{
52 			if (!locomotorInfos.Any(l => l.Name == locomotor))
53 				emitError("Actor {0} defines Locomotor {1} not found on World actor.".F(actorInfo.Name, locomotor));
54 		}
55 	}
56 }
57