Merge pull request #12477 from pchote/conditions-lint

Add a conditions lint rule + other rules fixes.
This commit is contained in:
Oliver Brakmann
2016-12-23 14:27:22 +01:00
committed by GitHub
44 changed files with 325 additions and 161 deletions

View File

@@ -0,0 +1,72 @@
#region Copyright & License Information
/*
* Copyright 2007-2016 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.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Lint
{
public class CheckConditions : ILintRulesPass
{
public void Run(Action<string> emitError, Action<string> emitWarning, Ruleset rules)
{
foreach (var actorInfo in rules.Actors)
{
if (actorInfo.Key.StartsWith("^", StringComparison.Ordinal))
continue;
var granted = new HashSet<string>();
var consumed = new HashSet<string>();
foreach (var trait in actorInfo.Value.TraitInfos<ITraitInfo>())
{
var fieldConsumed = trait.GetType().GetFields()
.Where(x => x.HasAttribute<ConsumedConditionReferenceAttribute>())
.SelectMany(f => LintExts.GetFieldValues(trait, f, emitError));
var propertyConsumed = trait.GetType().GetProperties()
.Where(x => x.HasAttribute<ConsumedConditionReferenceAttribute>())
.SelectMany(p => LintExts.GetPropertyValues(trait, p, emitError));
var fieldGranted = trait.GetType().GetFields()
.Where(x => x.HasAttribute<GrantedConditionReferenceAttribute>())
.SelectMany(f => LintExts.GetFieldValues(trait, f, emitError));
var propertyGranted = trait.GetType().GetProperties()
.Where(x => x.HasAttribute<GrantedConditionReferenceAttribute>())
.SelectMany(f => LintExts.GetPropertyValues(trait, f, emitError));
foreach (var c in fieldConsumed.Concat(propertyConsumed))
if (!string.IsNullOrEmpty(c))
consumed.Add(c);
foreach (var g in fieldGranted.Concat(propertyGranted))
if (!string.IsNullOrEmpty(g))
granted.Add(g);
}
var unconsumed = granted.Except(consumed);
if (unconsumed.Any())
emitWarning("Actor type `{0}` grants conditions that are not consumed: {1}".F(actorInfo.Key, unconsumed.JoinWith(", ")));
var ungranted = consumed.Except(granted);
if (ungranted.Any())
emitError("Actor type `{0}` consumes conditions that are not granted: {1}".F(actorInfo.Key, ungranted.JoinWith(", ")));
if ((consumed.Any() || granted.Any()) && actorInfo.Value.TraitInfoOrDefault<UpgradeManagerInfo>() == null)
emitError("Actor type `{0}` defines conditions but does not include an UpgradeManager".F(actorInfo.Key));
}
}
}
}

View File

@@ -10,6 +10,7 @@
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@@ -24,20 +25,37 @@ namespace OpenRA.Mods.Common.Lint
var type = fieldInfo.FieldType;
if (type == typeof(string))
return new[] { (string)fieldInfo.GetValue(ruleInfo) };
if (type == typeof(string[]))
return (string[])fieldInfo.GetValue(ruleInfo);
if (type == typeof(HashSet<string>))
return (HashSet<string>)fieldInfo.GetValue(ruleInfo);
if (typeof(IEnumerable<string>).IsAssignableFrom(type))
return fieldInfo.GetValue(ruleInfo) as IEnumerable<string>;
if (type == typeof(BooleanExpression))
{
var expr = (BooleanExpression)fieldInfo.GetValue(ruleInfo);
return expr != null ? expr.Variables : Enumerable.Empty<string>();
}
emitError("Bad type for reference on {0}.{1}. Supported types: string, string[], HashSet<string>, BooleanExpression"
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: string, IEnumerable<string>, BooleanExpression"
.F(ruleInfo.GetType().Name, fieldInfo.Name));
}
return new string[] { };
public static IEnumerable<string> GetPropertyValues(object ruleInfo, PropertyInfo propertyInfo, Action<string> emitError)
{
var type = propertyInfo.PropertyType;
if (type == typeof(string))
return new[] { (string)propertyInfo.GetValue(ruleInfo) };
if (typeof(IEnumerable).IsAssignableFrom(type))
return (IEnumerable<string>)propertyInfo.GetValue(ruleInfo);
if (type == typeof(BooleanExpression))
{
var expr = (BooleanExpression)propertyInfo.GetValue(ruleInfo);
return expr != null ? expr.Variables : Enumerable.Empty<string>();
}
throw new InvalidOperationException("Bad type for reference on {0}.{1}. Supported types: string, IEnumerable<string>, BooleanExpression"
.F(ruleInfo.GetType().Name, propertyInfo.Name));
}
}
}

View File

@@ -181,6 +181,7 @@
<Compile Include="Lint\CheckDeathTypes.cs" />
<Compile Include="Lint\CheckRangeLimit.cs" />
<Compile Include="Lint\CheckVoiceReferences.cs" />
<Compile Include="Lint\CheckConditions.cs" />
<Compile Include="Lint\CheckTargetHealthRadius.cs" />
<Compile Include="Lint\LintBuildablePrerequisites.cs" />
<Compile Include="Lint\LintExts.cs" />

View File

@@ -52,11 +52,11 @@ namespace OpenRA.Mods.Common.Traits
[VoiceReference] public readonly string Voice = "Action";
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while airborne.")]
public readonly string AirborneCondition = null;
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while at cruise altitude.")]
public readonly string CruisingCondition = null;

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Used together with ClassicProductionQueue.")]
public class PrimaryBuildingInfo : ITraitInfo
{
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while this is the primary building.")]
public readonly string PrimaryCondition = null;

View File

@@ -55,20 +55,22 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Cursor to display when unable to unload the passengers.")]
public readonly string UnloadBlockedCursor = "deploy-blocked";
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while waiting for cargo to load.")]
public readonly string LoadingCondition = null;
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while passengers are loaded.",
"Condition can stack with multiple passengers.")]
public readonly string LoadedCondition = null;
[UpgradeGrantedReference]
[Desc("Conditions to grant when specified actors are loaded inside the transport.",
"A dictionary of [actor id]: [condition].")]
public readonly Dictionary<string, string> PassengerConditions = new Dictionary<string, string>();
[GrantedConditionReference]
public IEnumerable<string> LinterPassengerConditions { get { return PassengerConditions.Values; } }
public object Create(ActorInitializer init) { return new Cargo(init, this); }
}

View File

@@ -17,11 +17,11 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Can be carried by actors with the `Carryall` trait.")]
public class CarryableInfo : UpgradableTraitInfo
{
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while a carryall has been reserved.")]
public readonly string ReservedCondition = null;
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while being carried.")]
public readonly string CarriedCondition = null;

View File

@@ -53,7 +53,7 @@ namespace OpenRA.Mods.Common.Traits
public readonly HashSet<string> CloakTypes = new HashSet<string> { "Cloak" };
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while cloaked.")]
public readonly string CloakedCondition = null;

View File

@@ -27,6 +27,9 @@ namespace OpenRA.Mods.Common.Traits
"Value is a list of the upgrade types to grant")]
public readonly Dictionary<int, string> Conditions = null;
[GrantedConditionReference]
public IEnumerable<string> LinterConditions { get { return Conditions.Values; } }
[Desc("Palette for the level up sprite.")]
[PaletteReference] public readonly string LevelUpPalette = "effect";

View File

@@ -18,7 +18,7 @@ namespace OpenRA.Mods.Common.Traits
public class GrantConditionOnPrerequisiteInfo : ITraitInfo
{
[FieldLoader.Require]
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant.")]
public readonly string Condition = null;

View File

@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Traits
public readonly int FallRate = 13;
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant to self while parachuting.")]
public readonly string ParachutingCondition = null;

View File

@@ -20,10 +20,12 @@ namespace OpenRA.Mods.Common.Traits
public readonly CVec Offset = CVec.Zero;
[FieldLoader.Require]
[UpgradeGrantedReference]
[Desc("Conditions to grant for each accepted plug type.")]
public readonly Dictionary<string, string> Conditions = null;
[GrantedConditionReference]
public IEnumerable<string> LinterConditions { get { return Conditions.Values; } }
public object Create(ActorInitializer init) { return new Pluggable(init, this); }
}

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.Traits
"Externally granted conditions that aren't explicitly whitelisted will be silently ignored.")]
public class ExternalConditionsInfo : TraitInfo<ExternalConditions>
{
[UpgradeGrantedReference]
[GrantedConditionReference]
public readonly string[] Conditions = { };
}

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.Traits
class GrantConditionInfo : UpgradableTraitInfo
{
[FieldLoader.Require]
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("Condition to grant.")]
public readonly string Condition = null;

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.Traits
public class GrantConditionOnDamageStateInfo : ITraitInfo, Requires<HealthInfo>
{
[FieldLoader.Require]
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("Condition to grant.")]
public readonly string Condition = null;

View File

@@ -21,11 +21,12 @@ namespace OpenRA.Mods.Common.Traits
{
public class GrantConditionOnDeployInfo : ITraitInfo
{
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("The condition to grant while the actor is undeployed.")]
public readonly string UndeployedCondition = null;
[UpgradeGrantedReference, FieldLoader.Require]
[FieldLoader.Require]
[GrantedConditionReference]
[Desc("The condition to grant after deploying and revoke before undeploying.")]
public readonly string DeployedCondition = null;

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.Traits
public class GrantConditionOnMovementInfo : UpgradableTraitInfo, Requires<IMoveInfo>
{
[FieldLoader.Require]
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("Condition to grant.")]
public readonly string Condition = null;

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.Traits
public class GrantConditionOnTerrainInfo : ITraitInfo
{
[FieldLoader.Require]
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("Condition to grant.")]
public readonly string Condition = null;

View File

@@ -17,12 +17,12 @@ namespace OpenRA.Mods.Common.Traits
public class StackedConditionInfo : TraitInfo<StackedCondition>
{
[FieldLoader.Require]
[UpgradeUsedReference]
[ConsumedConditionReference]
[Desc("Condition to monitor.")]
public readonly string Condition = null;
[FieldLoader.Require]
[UpgradeGrantedReference]
[GrantedConditionReference]
[Desc("Conditions to grant when the monitored condition is granted multiple times.",
"The first entry is activated at 2x grants, second entry at 3x grants, and so on.")]
public readonly string[] StackedConditions = { };

View File

@@ -21,7 +21,7 @@ namespace OpenRA.Mods.Common.Traits
{
static readonly IReadOnlyDictionary<string, bool> NoConditions = new ReadOnlyDictionary<string, bool>(new Dictionary<string, bool>());
[UpgradeUsedReference]
[ConsumedConditionReference]
[Desc("Boolean expression defining the condition to enable this trait.")]
public readonly BooleanExpression RequiresCondition = null;