Allow lua API to bind a minimal set of properties to dead actors.
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenRA.Scripting
|
||||
@@ -18,15 +19,33 @@ namespace OpenRA.Scripting
|
||||
readonly Actor actor;
|
||||
|
||||
protected override string DuplicateKeyError(string memberName) { return "Actor '{0}' defines the command '{1}' on multiple traits".F(actor.Info.Name, memberName); }
|
||||
protected override string MemberNotFoundError(string memberName) { return "Actor '{0}' does not define a property '{1}'".F(actor.Info.Name, memberName); }
|
||||
protected override string MemberNotFoundError(string memberName)
|
||||
{
|
||||
var actorName = actor.Info.Name;
|
||||
if (actor.IsDead())
|
||||
actorName += " (dead)";
|
||||
|
||||
return "Actor '{0}' does not define a property '{1}'".F(actorName, memberName);
|
||||
}
|
||||
|
||||
public ScriptActorInterface(ScriptContext context, Actor actor)
|
||||
: base(context)
|
||||
{
|
||||
this.actor = actor;
|
||||
|
||||
InitializeBindings();
|
||||
}
|
||||
|
||||
void InitializeBindings()
|
||||
{
|
||||
var commandClasses = context.ActorCommands[actor.Info].AsEnumerable();
|
||||
|
||||
// Destroyed actors cannot have their traits queried
|
||||
if (actor.Destroyed)
|
||||
commandClasses = commandClasses.Where(c => c.HasAttribute<ExposedForDestroyedActors>());
|
||||
|
||||
var args = new object[] { context, actor };
|
||||
var objects = context.ActorCommands[actor.Info].Select(cg =>
|
||||
var objects = commandClasses.Select(cg =>
|
||||
{
|
||||
var groupCtor = cg.GetConstructor(new Type[] { typeof(ScriptContext), typeof(Actor) });
|
||||
return groupCtor.Invoke(args);
|
||||
@@ -34,5 +53,11 @@ namespace OpenRA.Scripting
|
||||
|
||||
Bind(objects);
|
||||
}
|
||||
|
||||
public void OnActorDestroyed()
|
||||
{
|
||||
// Regenerate bindings to remove access to bogus trait state
|
||||
InitializeBindings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,9 @@ namespace OpenRA.Scripting
|
||||
public ScriptPropertyGroupAttribute(string category) { Category = category; }
|
||||
}
|
||||
|
||||
// For property groups that are safe to initialize invoke on destroyed actors
|
||||
public sealed class ExposedForDestroyedActors : Attribute { }
|
||||
|
||||
public sealed class ScriptActorPropertyActivityAttribute : Attribute { }
|
||||
|
||||
public abstract class ScriptActorProperties
|
||||
|
||||
Reference in New Issue
Block a user