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 System.Reflection;
15 using OpenRA.Mods.Common.Traits;
16 using OpenRA.Traits;
17 
18 namespace OpenRA.Mods.Common.UtilityCommands
19 {
20 	public class CheckConditionalTraitInterfaceOverrides : IUtilityCommand
21 	{
22 		string IUtilityCommand.Name { get { return "--check-conditional-trait-interface-overrides"; } }
23 
IUtilityCommand.ValidateArguments(string[] args)24 		bool IUtilityCommand.ValidateArguments(string[] args)
25 		{
26 			return args.Length == 1;
27 		}
28 
29 		int violationCount;
30 
IsConditionalTrait(Type type)31 		static bool IsConditionalTrait(Type type)
32 		{
33 			// Walk up the inheritance chain to check if any parent type is the generic ConditionalTrait type
34 			while (type != null && type != typeof(object))
35 			{
36 				if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ConditionalTrait<>))
37 					return true;
38 
39 				type = type.BaseType;
40 			}
41 
42 			return false;
43 		}
44 
CheckInterfaceViolation(Utility utility, Type interfaceType, string methodName)45 		void CheckInterfaceViolation(Utility utility, Type interfaceType, string methodName)
46 		{
47 			var types = utility.ModData.ObjectCreator.GetTypes()
48 				.Where(t => interfaceType.IsAssignableFrom(t) && !t.IsGenericType);
49 
50 			foreach (var t in types)
51 			{
52 				if (!IsConditionalTrait(t))
53 					continue;
54 
55 				var overridesCreated = t.GetMethod("{0}.{1}".F(interfaceType.FullName, methodName), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly) != null;
56 				if (overridesCreated)
57 				{
58 					Console.WriteLine("{0} must override ConditionalTrait's {1} method instead of implementing {2} directly", t.Name, methodName, interfaceType.Name);
59 					violationCount++;
60 				}
61 			}
62 		}
63 
64 		[Desc("Check for incorrect interface overrides in conditional traits defined in all assemblies referenced by the specified mod.")]
IUtilityCommand.Run(Utility utility, string[] args)65 		void IUtilityCommand.Run(Utility utility, string[] args)
66 		{
67 			CheckInterfaceViolation(utility, typeof(INotifyCreated), "Created");
68 			CheckInterfaceViolation(utility, typeof(IObservesVariables), "GetVariableObservers");
69 
70 			if (violationCount > 0)
71 			{
72 				Console.WriteLine("Interface override violations: {0}", violationCount);
73 				Environment.Exit(1);
74 			}
75 		}
76 	}
77 }
78