Add ScriptUpgradesCache class to enable maps to explicitly declare what upgrades their Lua scripts will use

This commit is contained in:
penev92
2015-05-27 17:00:33 +03:00
parent f1ec71b17d
commit 2ab7abcee4
5 changed files with 67 additions and 5 deletions

View File

@@ -8,6 +8,9 @@
*/
#endregion
using System;
using System.IO;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting;
using OpenRA.Traits;
@@ -17,35 +20,56 @@ namespace OpenRA.Mods.Common.Scripting
[ScriptPropertyGroup("General")]
public class UpgradeProperties : ScriptActorProperties, Requires<UpgradeManagerInfo>
{
UpgradeManager um;
readonly UpgradeManager um;
readonly ScriptUpgradesCache validUpgrades;
public UpgradeProperties(ScriptContext context, Actor self)
: base(context, self)
{
um = self.Trait<UpgradeManager>();
validUpgrades = self.World.WorldActor.TraitOrDefault<ScriptUpgradesCache>();
}
[Desc("Grant an upgrade to this actor.")]
public void GrantUpgrade(string upgrade)
{
um.GrantUpgrade(Self, upgrade, this);
if (validUpgrades == null)
throw new InvalidOperationException("Can not grant upgrades because there is no ScriptUpgradesCache defined!");
if (validUpgrades.Info.Upgrades.Contains(upgrade))
um.GrantUpgrade(Self, upgrade, this);
else
throw new InvalidDataException("The ScriptUpgradesCache does not contain a definition for upgrade `{0}`".F(upgrade));
}
[Desc("Revoke an upgrade that was previously granted using GrantUpgrade.")]
public void RevokeUpgrade(string upgrade)
{
um.RevokeUpgrade(Self, upgrade, this);
if (validUpgrades == null)
throw new InvalidOperationException("Can not grant upgrades because there is no ScriptUpgradesCache defined!");
if (validUpgrades.Info.Upgrades.Contains(upgrade))
um.RevokeUpgrade(Self, upgrade, this);
else
throw new InvalidDataException("The ScriptUpgradesCache does not contain a definition for upgrade `{0}`".F(upgrade));
}
[Desc("Grant a limited-time upgrade to this actor.")]
public void GrantTimedUpgrade(string upgrade, int duration)
{
um.GrantTimedUpgrade(Self, upgrade, duration);
if (validUpgrades == null)
throw new InvalidOperationException("Can not grant upgrades because there is no ScriptUpgradesCache defined!");
if (validUpgrades.Info.Upgrades.Contains(upgrade))
um.GrantTimedUpgrade(Self, upgrade, duration);
else
throw new InvalidDataException("The ScriptUpgradesCache does not contain a definition for upgrade `{0}`".F(upgrade));
}
[Desc("Check whether this actor accepts a specific upgrade.")]
public bool AcceptsUpgrade(string upgrade)
{
return um.AcceptsUpgrade(Self, upgrade);
return validUpgrades != null && validUpgrades.Info.Upgrades.Contains(upgrade) && um.AcceptsUpgrade(Self, upgrade);
}
}
}