Skip to content

Commit

Permalink
ScriptActorInterfaces, unbind on actor destroy.
Browse files Browse the repository at this point in the history
  • Loading branch information
anvilvapre authored and Mailaender committed Aug 30, 2022
1 parent fc1d8d2 commit 215898c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 54 deletions.
22 changes: 8 additions & 14 deletions OpenRA.Game/Scripting/ScriptActorInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
#endregion

using System;
using System.Linq;

namespace OpenRA.Scripting
Expand Down Expand Up @@ -38,26 +37,21 @@ public ScriptActorInterface(ScriptContext context, Actor actor)

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)
commandClasses = commandClasses.Where(c => c.HasAttribute<ExposedForDestroyedActors>());
commandClasses = commandClasses.Where(c => c.HasAttribute<ExposedForDestroyedActors>()).ToArray();

var args = 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);
Bind(CreateObjects(commandClasses, new object[] { Context, actor }));
}

public void OnActorDestroyed()
{
// Regenerate bindings to remove access to bogus trait state
InitializeBindings();
// Remove bindings not available to destroyed actors.
foreach (var commandClass in Context.ActorCommands[actor.Info])
if (!commandClass.HasAttribute<ExposedForDestroyedActors>())
Unbind(commandClass);
}
}
}
9 changes: 4 additions & 5 deletions OpenRA.Game/Scripting/ScriptMemberWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,19 @@ public static IEnumerable<MemberInfo> WrappableMembers(Type t)
{
// Only expose defined public non-static methods that were explicitly declared by the author
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
if (mi is PropertyInfo)
return true;
yield return mi;

// Methods are allowed if they aren't generic, and aren't generated by the compiler
var method = mi as MethodInfo;
if (method != null && !method.IsGenericMethodDefinition && !method.IsSpecialName)
return true;
yield return mi;

// Fields aren't allowed
return false;
});
}
}

public static string[] RequiredTraitNames(Type t)
Expand Down
30 changes: 27 additions & 3 deletions OpenRA.Game/Scripting/ScriptObjectWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
#endregion

using System;
using System.Collections.Generic;
using Eluant;
using Eluant.ObjectBinding;
Expand All @@ -21,16 +22,32 @@ public abstract class ScriptObjectWrapper : IScriptBindable, ILuaTableBinding
protected abstract string MemberNotFoundError(string memberName);

protected readonly ScriptContext Context;
Dictionary<string, ScriptMemberWrapper> members;
readonly Dictionary<string, ScriptMemberWrapper> members = new Dictionary<string, ScriptMemberWrapper>();

public ScriptObjectWrapper(ScriptContext 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)
{
var wrappable = ScriptMemberWrapper.WrappableMembers(obj.GetType());
Expand All @@ -44,6 +61,13 @@ protected void Bind(IEnumerable<object> clrObjects)
}
}

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 LuaValue this[LuaRuntime runtime, LuaValue keyValue]
Expand Down
13 changes: 1 addition & 12 deletions OpenRA.Game/Scripting/ScriptPlayerInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
*/
#endregion

using System;
using System.Linq;

namespace OpenRA.Scripting
{
public class ScriptPlayerInterface : ScriptObjectWrapper
Expand All @@ -25,15 +22,7 @@ public ScriptPlayerInterface(ScriptContext context, Player player)
: base(context)
{
this.player = 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);
Bind(CreateObjects(context.PlayerCommands, new object[] { context, player }));
}
}
}
43 changes: 23 additions & 20 deletions OpenRA.Game/Scripting/ScriptTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,33 @@ public static bool TryGetClrValue(this LuaValue value, Type t, out object clrObj
return true;
}

if (value is LuaNumber && t.IsAssignableFrom(typeof(double)))
if (value is LuaNumber)
{
clrObject = value.ToNumber().Value;
return true;
}
if (t.IsAssignableFrom(typeof(double)))
{
clrObject = value.ToNumber().Value;
return true;
}

// Need an explicit test for double -> int
// 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)))
{
clrObject = (int)value.ToNumber().Value;
return true;
}
// Need an explicit test for double -> int
// TODO: Lua 5.3 will introduce an integer type, so this will be able to go away
if (t.IsAssignableFrom(typeof(int)))
{
clrObject = (int)value.ToNumber().Value;
return true;
}

if (value is LuaNumber && t.IsAssignableFrom(typeof(short)))
{
clrObject = (short)value.ToNumber().Value;
return true;
}
if (t.IsAssignableFrom(typeof(short)))
{
clrObject = (short)value.ToNumber().Value;
return true;
}

if (value is LuaNumber && t.IsAssignableFrom(typeof(byte)))
{
clrObject = (byte)value.ToNumber().Value;
return true;
if (t.IsAssignableFrom(typeof(byte)))
{
clrObject = (byte)value.ToNumber().Value;
return true;
}
}

if (value is LuaString && t.IsAssignableFrom(typeof(string)))
Expand Down

0 comments on commit 215898c

Please sign in to comment.