Merge pull request #10143 from Phrohdoh/interface-reflection

Implement a utility command to check for explicit interface implementation violations.
This commit is contained in:
Oliver Brakmann
2016-01-18 20:23:43 +01:00
14 changed files with 150 additions and 19 deletions

View File

@@ -726,6 +726,7 @@
<Compile Include="FileFormats\RLEZerosCompression.cs" />
<Compile Include="FileFormats\IniFile.cs" />
<Compile Include="Orders\GuardOrderGenerator.cs" />
<Compile Include="UtilityCommands\CheckExplicitInterfacesCommand.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@@ -78,8 +78,7 @@ namespace OpenRA.Mods.Common.Traits
Color.FromArgb(96, Color.Black));
}
// Selection bar
public float GetValue()
float ISelectionBar.GetValue()
{
// Visible to player and allies
if (!ValidRenderPlayer())
@@ -92,6 +91,6 @@ namespace OpenRA.Mods.Common.Traits
return (float)progress / total;
}
public Color GetColor() { return Color.Purple; }
Color ISelectionBar.GetColor() { return Color.Purple; }
}
}

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Common.Traits
capturable = self.Trait<ExternalCapturable>();
}
public float GetValue()
float ISelectionBar.GetValue()
{
// only show when building is being captured
if (!capturable.CaptureInProgress)
@@ -37,6 +37,6 @@ namespace OpenRA.Mods.Common.Traits
return (float)capturable.CaptureProgressTime / (capturable.Info.CaptureCompleteTime * 25);
}
public Color GetColor() { return Color.Orange; }
Color ISelectionBar.GetColor() { return Color.Orange; }
}
}

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Common.Traits
playerPower = self.Owner.PlayerActor.Trait<PowerManager>();
}
public float GetValue()
float ISelectionBar.GetValue()
{
if (playerPower.PowerOutageRemainingTicks <= 0)
return 0;
@@ -36,7 +36,7 @@ namespace OpenRA.Mods.Common.Traits
return (float)playerPower.PowerOutageRemainingTicks / playerPower.PowerOutageTotalTicks;
}
public Color GetColor()
Color ISelectionBar.GetColor()
{
return Color.Yellow;
}

View File

@@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits
value = current != null ? 1 - (float)current.RemainingCost / current.TotalCost : 0;
}
public float GetValue()
float ISelectionBar.GetValue()
{
// only people we like should see our production status.
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
@@ -79,7 +79,7 @@ namespace OpenRA.Mods.Common.Traits
return value;
}
public Color GetColor() { return info.Color; }
Color ISelectionBar.GetColor() { return info.Color; }
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common.Traits
this.info = info;
}
public float GetValue()
float ISelectionBar.GetValue()
{
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
return 0;
@@ -46,6 +46,6 @@ namespace OpenRA.Mods.Common.Traits
return 1 - (float)power.RemainingTime / power.TotalTime;
}
public Color GetColor() { return info.Color; }
Color ISelectionBar.GetColor() { return info.Color; }
}
}

View File

@@ -47,7 +47,7 @@ namespace OpenRA.Mods.Common.Traits
value = remaining * 1f / duration;
}
public float GetValue()
float ISelectionBar.GetValue()
{
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
return 0;
@@ -55,6 +55,6 @@ namespace OpenRA.Mods.Common.Traits
return value;
}
public Color GetColor() { return info.Color; }
Color ISelectionBar.GetColor() { return info.Color; }
}
}

View File

@@ -0,0 +1,122 @@
#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 System.Linq;
using System.Reflection;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.UtilityCommands
{
public class CheckExplicitInterfacesCommand : IUtilityCommand
{
string IUtilityCommand.Name { get { return "--check-explicit-interfaces"; } }
bool IUtilityCommand.ValidateArguments(string[] args)
{
return args.Length == 1;
}
int violationCount;
[Desc("Check for explicit interface implementation violations in all assemblies referenced by the specified mod.")]
void IUtilityCommand.Run(ModData modData, string[] args)
{
var types = modData.ObjectCreator.GetTypes();
foreach (var implementingType in types.Where(t => !t.IsInterface))
{
if (implementingType.IsEnum)
continue;
var interfaces = implementingType.GetInterfaces();
foreach (var interfaceType in interfaces)
{
if (!interfaceType.HasAttribute<RequireExplicitImplementationAttribute>())
continue;
var interfaceMembers = interfaceType.GetMembers();
foreach (var interfaceMember in interfaceMembers)
{
if (interfaceMember.Name.StartsWith("get_") || interfaceMember.Name.StartsWith("set_"))
continue;
var interfaceMethod = interfaceMember as MethodInfo;
if (interfaceMethod != null)
{
var interfaceMethodParams = interfaceMethod.GetParameters();
foreach (var implementingMethod in implementingType.GetMethods())
{
if (implementingMethod.Name != interfaceMethod.Name
|| implementingMethod.ReturnType != interfaceMethod.ReturnType)
continue;
var implementingMethodParams = implementingMethod.GetParameters();
var lenImpl = implementingMethodParams.Length;
if (lenImpl != interfaceMethodParams.Length)
continue;
var allMatch = true;
for (var i = 0; i < lenImpl; i++)
{
var implementingParam = implementingMethodParams[i];
var interfaceParam = interfaceMethodParams[i];
if (implementingParam.ParameterType != interfaceParam.ParameterType
|| implementingParam.Name != interfaceParam.Name
|| implementingParam.IsOut != interfaceParam.IsOut)
{
allMatch = false;
break;
}
}
// Explicitly implemented methods are never public in C#.
if (allMatch && implementingMethod.IsPublic)
OnViolation(implementingType, interfaceType, implementingMethod);
}
}
var interfaceProperty = interfaceMember as PropertyInfo;
if (interfaceProperty != null)
{
var implementingProperties = implementingType.GetProperties();
foreach (var implementingProperty in implementingProperties)
{
if (implementingProperty.PropertyType != interfaceProperty.PropertyType
|| implementingProperty.Name != interfaceProperty.Name)
continue;
if (!IsExplicitInterfaceProperty(implementingProperty))
OnViolation(implementingType, interfaceType, implementingProperty);
}
}
}
}
}
if (violationCount > 0)
{
Console.WriteLine("Explicit interface violations: {0}", violationCount);
Environment.Exit(1);
}
}
static bool IsExplicitInterfaceProperty(PropertyInfo pi)
{
return pi.Name.Contains(".");
}
void OnViolation(Type implementor, Type interfaceType, MemberInfo violator)
{
Console.WriteLine("{0} must explicitly implement the interface member {1}.{2}", implementor.Name, interfaceType.Name, violator.Name);
violationCount++;
}
}
}