Add --check-conditional-trait-interface-overrides utility command.
This command is used by `make check` to detect traits that incorrectly override interface methods that are required for conditions to work correctly.
This commit is contained in:
3
Makefile
3
Makefile
@@ -108,6 +108,9 @@ check: dependencies
|
|||||||
@echo
|
@echo
|
||||||
@echo "Checking for explicit interface violations..."
|
@echo "Checking for explicit interface violations..."
|
||||||
@mono --debug OpenRA.Utility.exe all --check-explicit-interfaces
|
@mono --debug OpenRA.Utility.exe all --check-explicit-interfaces
|
||||||
|
@echo
|
||||||
|
@echo "Checking for incorrect conditional trait interface overrides..."
|
||||||
|
@mono --debug OpenRA.Utility.exe all --check-conditional-trait-interface-overrides
|
||||||
|
|
||||||
|
|
||||||
NUNIT_CONSOLE := $(shell test -f thirdparty/download/nunit3-console.exe && echo mono thirdparty/download/nunit3-console.exe || \
|
NUNIT_CONSOLE := $(shell test -f thirdparty/download/nunit3-console.exe && echo mono thirdparty/download/nunit3-console.exe || \
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2019 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, either version 3 of
|
||||||
|
* the License, or (at your option) any later version. For more
|
||||||
|
* information, see COPYING.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using OpenRA.Mods.Common.Traits;
|
||||||
|
using OpenRA.Traits;
|
||||||
|
|
||||||
|
namespace OpenRA.Mods.Common.UtilityCommands
|
||||||
|
{
|
||||||
|
public class CheckConditionalTraitInterfaceOverrides : IUtilityCommand
|
||||||
|
{
|
||||||
|
string IUtilityCommand.Name { get { return "--check-conditional-trait-interface-overrides"; } }
|
||||||
|
|
||||||
|
bool IUtilityCommand.ValidateArguments(string[] args)
|
||||||
|
{
|
||||||
|
return args.Length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int violationCount;
|
||||||
|
|
||||||
|
static bool IsConditionalTrait(Type type)
|
||||||
|
{
|
||||||
|
// Walk up the inheritance chain to check if any parent type is the generic ConditionalTrait type
|
||||||
|
while (type != null && type != typeof(object))
|
||||||
|
{
|
||||||
|
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ConditionalTrait<>))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
type = type.BaseType;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckInterfaceViolation(Utility utility, Type interfaceType, string methodName)
|
||||||
|
{
|
||||||
|
var types = utility.ModData.ObjectCreator.GetTypes()
|
||||||
|
.Where(t => interfaceType.IsAssignableFrom(t) && !t.IsGenericType);
|
||||||
|
|
||||||
|
foreach (var t in types)
|
||||||
|
{
|
||||||
|
if (!IsConditionalTrait(t))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var overridesCreated = t.GetMethod("{0}.{1}".F(interfaceType.FullName, methodName), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly) != null;
|
||||||
|
if (overridesCreated)
|
||||||
|
{
|
||||||
|
Console.WriteLine("{0} must override ConditionalTrait's {1} method instead of implementing {2} directly", t.Name, methodName, interfaceType.Name);
|
||||||
|
violationCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Desc("Check for incorrect interface overrides in conditional traits defined in all assemblies referenced by the specified mod.")]
|
||||||
|
void IUtilityCommand.Run(Utility utility, string[] args)
|
||||||
|
{
|
||||||
|
CheckInterfaceViolation(utility, typeof(INotifyCreated), "Created");
|
||||||
|
CheckInterfaceViolation(utility, typeof(IObservesVariables), "GetVariableObservers");
|
||||||
|
|
||||||
|
if (violationCount > 0)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Interface override violations: {0}", violationCount);
|
||||||
|
Environment.Exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
make.ps1
3
make.ps1
@@ -147,6 +147,9 @@ function Check-Command
|
|||||||
{
|
{
|
||||||
Write-Host "Checking for explicit interface violations..." -ForegroundColor Cyan
|
Write-Host "Checking for explicit interface violations..." -ForegroundColor Cyan
|
||||||
./OpenRA.Utility.exe all --check-explicit-interfaces
|
./OpenRA.Utility.exe all --check-explicit-interfaces
|
||||||
|
|
||||||
|
Write-Host "Checking for incorrect conditional trait interface overrides..." -ForegroundColor Cyan
|
||||||
|
./OpenRA.Utility.exe all --check-conditional-trait-interface-overrides
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user