Updated Moonsharp Interpreter, added support for implicit conversions, new types: LuaByte, LuaUShort and LuaFloat added to solve method overload conflict, support for setting UserData metatables, remove LuaUserData.AddCallMetaMethod and moved GUI object to Lua

This commit is contained in:
Evil Factory
2022-03-09 13:35:11 -03:00
parent 92dbca40a8
commit a5228583d0
15 changed files with 252 additions and 304 deletions
@@ -17,7 +17,6 @@ using System.Diagnostics;
namespace Barotrauma
{
partial class LuaSetup
{
partial class LuaUserData
@@ -39,6 +38,12 @@ namespace Barotrauma
{
Type type = GetType(typeName);
if (type == null)
{
GameMain.Lua.HandleLuaException(new Exception($"Tried to register a type that doesn't exist: {typeName}."));
return null;
}
MethodInfo method = typeof(UserData).GetMethod(nameof(UserData.RegisterType), new Type[2] { typeof(InteropAccessMode), typeof(string) });
MethodInfo generic = method.MakeGenericMethod(type);
return (IUserDataDescriptor)generic.Invoke(null, new object[] { null, null });
@@ -48,6 +53,12 @@ namespace Barotrauma
{
Type type = GetType(typeName);
if (type == null)
{
GameMain.Lua.HandleLuaException(new Exception($"Tried to unregister a type that doesn't exist: {typeName}."));
return;
}
MethodInfo method = typeof(UserData).GetMethod(nameof(UserData.UnregisterType), new Type[2] { typeof(InteropAccessMode), typeof(string) });
MethodInfo generic = method.MakeGenericMethod(type);
generic.Invoke(null, new object[] { null, null });
@@ -82,18 +93,17 @@ namespace Barotrauma
{
Type type = GetType(typeName);
if (type == null)
{
GameMain.Lua.HandleLuaException(new Exception($"Tried to create a static userdata of a type that doesn't exist: {typeName}."));
return null;
}
MethodInfo method = typeof(UserData).GetMethod(nameof(UserData.CreateStatic), 1, new Type[0]);
MethodInfo generic = method.MakeGenericMethod(type);
return generic.Invoke(null, null);
}
public static void AddCallMetaMember(IUserDataDescriptor IUDD)
{
var descriptor = (StandardUserDataDescriptor)IUDD;
descriptor.RemoveMetaMember("__call");
descriptor.AddMetaMember("__call", new ObjectCallbackMemberDescriptor("__call", LuaSetup.luaSetup.HandleCall));
}
public static void MakeFieldAccessible(IUserDataDescriptor IUUD, string fieldName)
{
var descriptor = (StandardUserDataDescriptor)IUUD;
@@ -5,56 +5,4 @@ using Barotrauma.Networking;
namespace Barotrauma
{
partial class LuaSetup
{
public class LuaWriteOnlyMessage
{
private WriteOnlyMessage target;
[MoonSharpHidden]
public LuaWriteOnlyMessage(WriteOnlyMessage p)
{
target = p;
}
public void WriteByte(byte v) => target.Write(v);
public void WriteUShort(ushort v) => target.Write(v);
public void Write(bool val) => target.Write(val);
public void WritePadBits() => target.WritePadBits();
public void Write(byte val) => target.Write(val);
public void Write(Int16 val) => target.Write(val);
public void Write(UInt16 val) => target.Write(val);
public void Write(Int32 val) => target.Write(val);
public void Write(UInt32 val) => target.Write(val);
public void Write(Int64 val) => target.Write(val);
public void Write(UInt64 val) => target.Write(val);
public void Write(Single val) => target.Write(val);
public void Write(Double val) => target.Write(val);
public void WriteColorR8G8B8(Microsoft.Xna.Framework.Color val) => target.WriteColorR8G8B8(val);
public void WriteColorR8G8B8A8(Microsoft.Xna.Framework.Color val) => target.WriteColorR8G8B8A8(val);
public void WriteVariableUInt32(UInt32 val) => target.WriteVariableUInt32(val);
public void Write(string val) => target.Write(val);
public void WriteRangedInteger(int val, int min, int max) => target.WriteRangedInteger(val, min, max);
public void WriteRangedSingle(Single val, Single min, Single max, int bitCount) => target.WriteRangedSingle(val, min, max, bitCount);
public void Write(byte[] val, int startIndex, int length) => target.Write(val, startIndex, length);
public void PrepareForSending(ref byte[] outBuf, out bool isCompressed, out int outLength) => target.PrepareForSending(ref outBuf, out isCompressed, out outLength);
public int BitPosition
{
get { return target.BitPosition; }
set { target.BitPosition = value; }
}
public int BytePosition => target.BytePosition;
public byte[] Buffer => target.Buffer;
public int LengthBits
{
get { return target.LengthBits; }
set { target.LengthBits = value; }
}
public int LengthBytes => target.LengthBytes;
}
}
}
@@ -222,41 +222,6 @@ namespace Barotrauma
return value * 2;
}
// messy solution
public object HandleCall(object arg1, ScriptExecutionContext arg2, CallbackArguments arg3)
{
var what = arg3.RawGet(0, true);
var code = "return " + what.UserData.Descriptor.Type.Name + ".__new(";
var tbl = new Table(lua);
tbl[what.UserData.Descriptor.Type.Name] = what;
for(var i=1; i < arg3.Count; i++)
{
if (i == arg3.Count - 1)
code = code + "arg" + i;
else
code = code + "arg" + i + ",";
tbl["arg" + i] = arg3.RawGet(i, true);
}
code = code + ")";
try
{
return lua.DoString(code, tbl);
}
catch(Exception e)
{
HandleLuaException(e);
}
return null;
}
#if SERVER
public static void InstallClientSideLua()
@@ -324,7 +289,7 @@ namespace Barotrauma
LuaCustomConverters.RegisterAll();
lua = new Script(CoreModules.Preset_SoftSandbox);
lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug);
lua.Options.DebugPrint = PrintMessage;
lua.Options.ScriptLoader = luaScriptLoader;
@@ -344,14 +309,6 @@ namespace Barotrauma
UserData.RegisterType<LuaUserData>();
UserData.RegisterType<IUserDataDescriptor>();
UserData.RegisterProxyType<LuaWriteOnlyMessage, WriteOnlyMessage>(r => new LuaWriteOnlyMessage(r));
#if SERVER
#elif CLIENT
UserData.RegisterType<LuaGUI>();
#endif
lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths;
lua.Globals["TestFunction"] = (Func<float, float>)TestFunction;
@@ -364,7 +321,7 @@ namespace Barotrauma
lua.Globals["dostring"] = (Func<string, Table, string, DynValue>)DoString;
lua.Globals["load"] = (Func<string, Table, string, DynValue>)LoadString;
lua.Globals["LuaUserData"] = UserData.CreateStatic<LuaUserData>();
lua.Globals["Game"] = game;
lua.Globals["Hook"] = hook;
@@ -373,12 +330,6 @@ namespace Barotrauma
lua.Globals["File"] = UserData.CreateStatic<LuaFile>();
lua.Globals["Networking"] = networking;
#if SERVER
#elif CLIENT
lua.Globals["GUI"] = new LuaGUI(this);
#endif
bool isServer = true;
#if SERVER
@@ -397,7 +348,7 @@ namespace Barotrauma
else if (File.Exists("Mods/LuaForBarotrauma/Lua/LuaSetup.lua")) // in case its the workshop version
DoFile("Mods/LuaForBarotrauma/Lua/LuaSetup.lua");
else
PrintError("Lua loader not found! Lua/LuaSetup.lua, no lua scripts will be executed or work.");
PrintError("Lua loader not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work.");
}
public LuaSetup()
@@ -0,0 +1,41 @@
using System;
using System.ComponentModel;
namespace Barotrauma
{
public class LuaByte
{
public byte Value;
public LuaByte(byte v)
{
Value = v;
}
public static implicit operator byte(LuaByte lb) => lb.Value;
}
public class LuaUShort
{
public ushort Value;
public LuaUShort(ushort v)
{
Value = v;
}
public static implicit operator ushort(LuaUShort lb) => lb.Value;
}
public class LuaFloat
{
public float Value;
public LuaFloat(float v)
{
Value = v;
}
public static implicit operator float(LuaFloat lb) => lb.Value;
}
}