Add actor conditions lint rule.
This commit is contained in:
72
OpenRA.Mods.Common/Lint/CheckConditions.cs
Normal file
72
OpenRA.Mods.Common/Lint/CheckConditions.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user