ScriptActorInterfaces, unbind on actor destroy.

This commit is contained in:
Vapre
2022-07-26 08:00:00 +02:00
committed by Matthias Mailänder
parent fc1d8d2355
commit 215898c7ec
5 changed files with 63 additions and 54 deletions

View File

@@ -9,7 +9,6 @@
*/ */
#endregion #endregion
using System;
using System.Linq; using System.Linq;
namespace OpenRA.Scripting namespace OpenRA.Scripting
@@ -38,26 +37,21 @@ namespace OpenRA.Scripting
void InitializeBindings() void InitializeBindings()
{ {
var commandClasses = Context.ActorCommands[actor.Info].AsEnumerable(); var commandClasses = Context.ActorCommands[actor.Info];
// Destroyed actors cannot have their traits queried // Destroyed actors cannot have their traits queried. In rare cases the actor may have already been destroyed.
if (actor.Disposed) if (actor.Disposed)
commandClasses = commandClasses.Where(c => c.HasAttribute<ExposedForDestroyedActors>()); commandClasses = commandClasses.Where(c => c.HasAttribute<ExposedForDestroyedActors>()).ToArray();
var args = new object[] { Context, actor }; Bind(CreateObjects(commandClasses, new object[] { Context, actor }));
var objects = commandClasses.Select(cg =>
{
var groupCtor = cg.GetConstructor(new Type[] { typeof(ScriptContext), typeof(Actor) });
return groupCtor.Invoke(args);
});
Bind(objects);
} }
public void OnActorDestroyed() public void OnActorDestroyed()
{ {
// Regenerate bindings to remove access to bogus trait state // Remove bindings not available to destroyed actors.
InitializeBindings(); foreach (var commandClass in Context.ActorCommands[actor.Info])
if (!commandClass.HasAttribute<ExposedForDestroyedActors>())
Unbind(commandClass);
} }
} }
} }

View File

@@ -126,20 +126,19 @@ namespace OpenRA.Scripting
{ {
// Only expose defined public non-static methods that were explicitly declared by the author // Only expose defined public non-static methods that were explicitly declared by the author
var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly; var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
return t.GetMembers(flags).Where(mi => foreach (var mi in t.GetMembers(flags))
{ {
// Properties are always wrappable // Properties are always wrappable
if (mi is PropertyInfo) if (mi is PropertyInfo)
return true; yield return mi;
// Methods are allowed if they aren't generic, and aren't generated by the compiler // Methods are allowed if they aren't generic, and aren't generated by the compiler
var method = mi as MethodInfo; var method = mi as MethodInfo;
if (method != null && !method.IsGenericMethodDefinition && !method.IsSpecialName) if (method != null && !method.IsGenericMethodDefinition && !method.IsSpecialName)
return true; yield return mi;
// Fields aren't allowed // Fields aren't allowed
return false; }
});
} }
public static string[] RequiredTraitNames(Type t) public static string[] RequiredTraitNames(Type t)

View File

@@ -9,6 +9,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Eluant; using Eluant;
using Eluant.ObjectBinding; using Eluant.ObjectBinding;
@@ -21,16 +22,32 @@ namespace OpenRA.Scripting
protected abstract string MemberNotFoundError(string memberName); protected abstract string MemberNotFoundError(string memberName);
protected readonly ScriptContext Context; protected readonly ScriptContext Context;
Dictionary<string, ScriptMemberWrapper> members; readonly Dictionary<string, ScriptMemberWrapper> members = new Dictionary<string, ScriptMemberWrapper>();
public ScriptObjectWrapper(ScriptContext context) public ScriptObjectWrapper(ScriptContext context)
{ {
Context = context; Context = context;
} }
protected void Bind(IEnumerable<object> clrObjects) protected static object[] CreateObjects(Type[] types, object[] constructorArgs)
{ {
members = new Dictionary<string, ScriptMemberWrapper>(); var i = 0;
var argTypes = new Type[constructorArgs.Length];
foreach (var ca in constructorArgs)
argTypes[i++] = ca.GetType();
var objects = new object[types.Length];
i = 0;
foreach (var type in types)
objects[i++] = type.GetConstructor(argTypes).Invoke(constructorArgs);
return objects;
}
protected void Bind(object[] clrObjects)
{
members.Clear();
foreach (var obj in clrObjects) foreach (var obj in clrObjects)
{ {
var wrappable = ScriptMemberWrapper.WrappableMembers(obj.GetType()); var wrappable = ScriptMemberWrapper.WrappableMembers(obj.GetType());
@@ -44,6 +61,13 @@ namespace OpenRA.Scripting
} }
} }
protected void Unbind(Type targetType)
{
foreach (var m in members)
if (targetType == m.Value.Target.GetType())
members.Remove(m.Key);
}
public bool ContainsKey(string key) { return members.ContainsKey(key); } public bool ContainsKey(string key) { return members.ContainsKey(key); }
public LuaValue this[LuaRuntime runtime, LuaValue keyValue] public LuaValue this[LuaRuntime runtime, LuaValue keyValue]

View File

@@ -9,9 +9,6 @@
*/ */
#endregion #endregion
using System;
using System.Linq;
namespace OpenRA.Scripting namespace OpenRA.Scripting
{ {
public class ScriptPlayerInterface : ScriptObjectWrapper public class ScriptPlayerInterface : ScriptObjectWrapper
@@ -25,15 +22,7 @@ namespace OpenRA.Scripting
: base(context) : base(context)
{ {
this.player = player; this.player = player;
Bind(CreateObjects(context.PlayerCommands, new object[] { context, player }));
var args = new object[] { context, player };
var objects = context.PlayerCommands.Select(cg =>
{
var groupCtor = cg.GetConstructor(new Type[] { typeof(ScriptContext), typeof(Player) });
return groupCtor.Invoke(args);
});
Bind(objects);
} }
} }
} }

View File

@@ -61,7 +61,9 @@ namespace OpenRA.Scripting
return true; return true;
} }
if (value is LuaNumber && t.IsAssignableFrom(typeof(double))) if (value is LuaNumber)
{
if (t.IsAssignableFrom(typeof(double)))
{ {
clrObject = value.ToNumber().Value; clrObject = value.ToNumber().Value;
return true; return true;
@@ -69,23 +71,24 @@ namespace OpenRA.Scripting
// Need an explicit test for double -> int // Need an explicit test for double -> int
// TODO: Lua 5.3 will introduce an integer type, so this will be able to go away // TODO: Lua 5.3 will introduce an integer type, so this will be able to go away
if (value is LuaNumber && t.IsAssignableFrom(typeof(int))) if (t.IsAssignableFrom(typeof(int)))
{ {
clrObject = (int)value.ToNumber().Value; clrObject = (int)value.ToNumber().Value;
return true; return true;
} }
if (value is LuaNumber && t.IsAssignableFrom(typeof(short))) if (t.IsAssignableFrom(typeof(short)))
{ {
clrObject = (short)value.ToNumber().Value; clrObject = (short)value.ToNumber().Value;
return true; return true;
} }
if (value is LuaNumber && t.IsAssignableFrom(typeof(byte))) if (t.IsAssignableFrom(typeof(byte)))
{ {
clrObject = (byte)value.ToNumber().Value; clrObject = (byte)value.ToNumber().Value;
return true; return true;
} }
}
if (value is LuaString && t.IsAssignableFrom(typeof(string))) if (value is LuaString && t.IsAssignableFrom(typeof(string)))
{ {