Add an external condition whitelist.
This commit is contained in:
@@ -775,6 +775,7 @@
|
||||
<Compile Include="Traits\AutoCarryall.cs" />
|
||||
<Compile Include="Traits\World\CliffBackImpassabilityLayer.cs" />
|
||||
<Compile Include="Traits\Upgrades\GrantCondition.cs" />
|
||||
<Compile Include="Traits\Upgrades\ExternalConditions.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Target Name="AfterBuild">
|
||||
|
||||
25
OpenRA.Mods.Common/Traits/Upgrades/ExternalConditions.cs
Normal file
25
OpenRA.Mods.Common/Traits/Upgrades/ExternalConditions.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
#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 OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Lists conditions that are accepted from external sources (Lua, warheads, etc).",
|
||||
"Externally granted conditions that aren't explicitly whitelisted will be silently ignored.")]
|
||||
public class ExternalConditionsInfo : TraitInfo<ExternalConditions>
|
||||
{
|
||||
[UpgradeGrantedReference]
|
||||
public readonly string[] Conditions = { };
|
||||
}
|
||||
|
||||
public class ExternalConditions { }
|
||||
}
|
||||
@@ -24,6 +24,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
/// <summary>Value used to represent an invalid token.</summary>
|
||||
public static readonly int InvalidConditionToken = -1;
|
||||
string[] externalConditions = { };
|
||||
|
||||
class TimedCondition
|
||||
{
|
||||
@@ -120,6 +121,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
// Update all traits with their initial condition state
|
||||
foreach (var consumer in allConsumers)
|
||||
consumer.ConditionsChanged(self, readOnlyConditionCache);
|
||||
|
||||
// Build external condition whitelist
|
||||
externalConditions = self.Info.TraitInfos<ExternalConditionsInfo>()
|
||||
.SelectMany(t => t.Conditions)
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
void UpdateConditionState(Actor self, string condition, int token, bool isRevoke)
|
||||
@@ -141,8 +148,12 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
/// <summary>Grants a specified condition.</summary>
|
||||
/// <returns>The token that is used to revoke this condition.</returns>
|
||||
public int GrantCondition(Actor self, string condition)
|
||||
/// <param name="external">Validate against the external condition whitelist.</param>
|
||||
public int GrantCondition(Actor self, string condition, bool external = false)
|
||||
{
|
||||
if (external && !externalConditions.Contains(condition))
|
||||
return InvalidConditionToken;
|
||||
|
||||
var token = nextToken++;
|
||||
tokens.Add(token, condition);
|
||||
|
||||
@@ -155,7 +166,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
}
|
||||
|
||||
/// <summary>Revokes a previously granted condition.</summary>
|
||||
/// <returns>The invalid token ID</returns>
|
||||
/// <returns>The invalid token ID.</returns>
|
||||
/// <param name="token">The token ID returned by GrantCondition.</param>
|
||||
public int RevokeCondition(Actor self, int token)
|
||||
{
|
||||
@@ -172,6 +183,15 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return InvalidConditionToken;
|
||||
}
|
||||
|
||||
/// <summary>Returns true if the given external condition will have an effect on this actor.</summary>
|
||||
public bool AcceptsExternalCondition(Actor self, string condition)
|
||||
{
|
||||
if (state == null)
|
||||
throw new InvalidOperationException("AcceptsExternalCondition cannot be queried before the actor has been fully created.");
|
||||
|
||||
return externalConditions.Contains(condition) && !conditionCache[condition];
|
||||
}
|
||||
|
||||
#region Shim methods for legacy upgrade granting code
|
||||
|
||||
void CheckCanManageConditions()
|
||||
|
||||
Reference in New Issue
Block a user