Add lua types for C# primitives
This commit is contained in:
@@ -6,9 +6,21 @@ local CreateEnum = LuaSetup.LuaUserData.CreateEnumTable
|
||||
|
||||
require("DefaultLib/Utils/SteamApi")
|
||||
|
||||
defaultLib["SByte"] = CreateStatic("Barotrauma.LuaSByte", true)
|
||||
defaultLib["Byte"] = CreateStatic("Barotrauma.LuaByte", true)
|
||||
defaultLib["UShort"] = CreateStatic("Barotrauma.LuaUShort", true)
|
||||
defaultLib["Float"] = CreateStatic("Barotrauma.LuaFloat", true)
|
||||
defaultLib["Int16"] = CreateStatic("Barotrauma.LuaInt16", true)
|
||||
defaultLib["UInt16"] = CreateStatic("Barotrauma.LuaUInt16", true)
|
||||
defaultLib["Int32"] = CreateStatic("Barotrauma.LuaInt32", true)
|
||||
defaultLib["UInt32"] = CreateStatic("Barotrauma.LuaUInt32", true)
|
||||
defaultLib["Int64"] = CreateStatic("Barotrauma.LuaInt64", true)
|
||||
defaultLib["UInt64"] = CreateStatic("Barotrauma.LuaUInt64", true)
|
||||
defaultLib["Single"] = CreateStatic("Barotrauma.LuaSingle", true)
|
||||
defaultLib["Double"] = CreateStatic("Barotrauma.LuaDouble", true)
|
||||
|
||||
-- Backward compatibility
|
||||
defaultLib["Float"] = CreateStatic("Barotrauma.LuaSingle", true)
|
||||
defaultLib["Short"] = CreateStatic("Barotrauma.LuaInt16", true)
|
||||
defaultLib["UShort"] = CreateStatic("Barotrauma.LuaUInt16", true)
|
||||
|
||||
defaultLib["SpawnType"] = CreateEnum("Barotrauma.SpawnType")
|
||||
defaultLib["ChatMessageType"] = CreateEnum("Barotrauma.Networking.ChatMessageType")
|
||||
|
||||
@@ -4,9 +4,16 @@ local RegisterBarotrauma = LuaSetup.LuaUserData.RegisterTypeBarotrauma
|
||||
Register("System.TimeSpan")
|
||||
Register("System.Exception")
|
||||
|
||||
RegisterBarotrauma("LuaSByte")
|
||||
RegisterBarotrauma("LuaByte")
|
||||
RegisterBarotrauma("LuaUShort")
|
||||
RegisterBarotrauma("LuaFloat")
|
||||
RegisterBarotrauma("LuaInt16")
|
||||
RegisterBarotrauma("LuaUInt16")
|
||||
RegisterBarotrauma("LuaInt32")
|
||||
RegisterBarotrauma("LuaUInt32")
|
||||
RegisterBarotrauma("LuaInt64")
|
||||
RegisterBarotrauma("LuaUInt64")
|
||||
RegisterBarotrauma("LuaSingle")
|
||||
RegisterBarotrauma("LuaDouble")
|
||||
|
||||
RegisterBarotrauma("Range`1[System.Single]")
|
||||
RegisterBarotrauma("Range`1[System.Int32]")
|
||||
|
||||
@@ -1,41 +1,184 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class LuaByte
|
||||
{
|
||||
public byte Value;
|
||||
public struct LuaSByte
|
||||
{
|
||||
private readonly sbyte value;
|
||||
|
||||
public LuaByte(byte v)
|
||||
{
|
||||
Value = v;
|
||||
}
|
||||
public LuaSByte(double v)
|
||||
{
|
||||
value = (sbyte)v;
|
||||
}
|
||||
|
||||
public static implicit operator byte(LuaByte lb) => lb.Value;
|
||||
}
|
||||
public LuaSByte(string v, int radix = 10)
|
||||
{
|
||||
value = Convert.ToSByte(v, radix);
|
||||
}
|
||||
|
||||
public class LuaUShort
|
||||
{
|
||||
public ushort Value;
|
||||
public static implicit operator sbyte(LuaSByte luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public LuaUShort(ushort v)
|
||||
{
|
||||
Value = v;
|
||||
}
|
||||
public struct LuaByte
|
||||
{
|
||||
private readonly byte value;
|
||||
|
||||
public static implicit operator ushort(LuaUShort lb) => lb.Value;
|
||||
}
|
||||
public LuaByte(double v)
|
||||
{
|
||||
value = (byte)v;
|
||||
}
|
||||
|
||||
public class LuaFloat
|
||||
{
|
||||
public float Value;
|
||||
public LuaByte(string v, int radix = 10)
|
||||
{
|
||||
value = Convert.ToByte(v, radix);
|
||||
}
|
||||
|
||||
public LuaFloat(float v)
|
||||
{
|
||||
Value = v;
|
||||
}
|
||||
public static implicit operator byte(LuaByte luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public static implicit operator float(LuaFloat lb) => lb.Value;
|
||||
}
|
||||
public struct LuaInt16
|
||||
{
|
||||
private readonly short value;
|
||||
|
||||
public LuaInt16(double v)
|
||||
{
|
||||
value = (short)v;
|
||||
}
|
||||
|
||||
public LuaInt16(string v, int radix = 10)
|
||||
{
|
||||
value = Convert.ToInt16(v, radix);
|
||||
}
|
||||
|
||||
public static implicit operator short(LuaInt16 luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public struct LuaUInt16
|
||||
{
|
||||
private readonly ushort value;
|
||||
|
||||
public LuaUInt16(double v)
|
||||
{
|
||||
value = (ushort)v;
|
||||
}
|
||||
|
||||
public LuaUInt16(string v, int radix = 10)
|
||||
{
|
||||
value = Convert.ToUInt16(v, radix);
|
||||
}
|
||||
|
||||
public static implicit operator ushort(LuaUInt16 luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public struct LuaInt32
|
||||
{
|
||||
private readonly int value;
|
||||
|
||||
public LuaInt32(double v)
|
||||
{
|
||||
value = (int)v;
|
||||
}
|
||||
|
||||
public LuaInt32(string v, int radix = 10)
|
||||
{
|
||||
value = Convert.ToInt32(v, radix);
|
||||
}
|
||||
|
||||
public static implicit operator int(LuaInt32 luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public struct LuaUInt32
|
||||
{
|
||||
private readonly uint value;
|
||||
|
||||
public LuaUInt32(double v)
|
||||
{
|
||||
value = (uint)v;
|
||||
}
|
||||
|
||||
public LuaUInt32(string v, int radix = 10)
|
||||
{
|
||||
value = Convert.ToUInt32(v, radix);
|
||||
}
|
||||
|
||||
public static implicit operator uint(LuaUInt32 luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public struct LuaInt64
|
||||
{
|
||||
private readonly long value;
|
||||
|
||||
public LuaInt64(double v)
|
||||
{
|
||||
value = (long)v;
|
||||
}
|
||||
|
||||
public LuaInt64(double lo, double hi)
|
||||
{
|
||||
value = Convert.ToUInt32(lo) | (long)Convert.ToInt32(hi) << 32;
|
||||
}
|
||||
|
||||
public LuaInt64(string v, int radix = 10)
|
||||
{
|
||||
value = Convert.ToInt64(v, radix);
|
||||
}
|
||||
|
||||
public static implicit operator long(LuaInt64 luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public struct LuaUInt64
|
||||
{
|
||||
private readonly ulong value;
|
||||
|
||||
public LuaUInt64(double v)
|
||||
{
|
||||
value = (ulong)v;
|
||||
}
|
||||
|
||||
public LuaUInt64(double lo, double hi)
|
||||
{
|
||||
value = Convert.ToUInt32(lo) | (ulong)Convert.ToUInt32(hi) << 32;
|
||||
}
|
||||
|
||||
public LuaUInt64(string v, int radix = 10)
|
||||
{
|
||||
value = Convert.ToUInt64(v, radix);
|
||||
}
|
||||
|
||||
public static implicit operator ulong(LuaUInt64 luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public struct LuaSingle
|
||||
{
|
||||
private readonly float value;
|
||||
|
||||
public LuaSingle(double v)
|
||||
{
|
||||
value = (float)v;
|
||||
}
|
||||
|
||||
public LuaSingle(string v)
|
||||
{
|
||||
value = float.Parse(v);
|
||||
}
|
||||
|
||||
public static implicit operator float(LuaSingle luaValue) => luaValue.value;
|
||||
}
|
||||
|
||||
public struct LuaDouble
|
||||
{
|
||||
private readonly double value;
|
||||
|
||||
public LuaDouble(double v)
|
||||
{
|
||||
value = v;
|
||||
}
|
||||
|
||||
public LuaDouble(string v)
|
||||
{
|
||||
value = double.Parse(v);
|
||||
}
|
||||
|
||||
public static implicit operator double(LuaDouble luaValue) => luaValue.value;
|
||||
}
|
||||
}
|
||||
@@ -118,22 +118,66 @@ namespace Barotrauma
|
||||
return UInt64.Parse(v.String);
|
||||
});
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.UserData, typeof(object), v =>
|
||||
{
|
||||
if (v.UserData.Object is LuaByte lbyte)
|
||||
{
|
||||
return lbyte.Value;
|
||||
}
|
||||
else if (v.UserData.Object is LuaUShort lushort)
|
||||
{
|
||||
return lushort.Value;
|
||||
}
|
||||
else if (v.UserData.Object is LuaFloat lfloat)
|
||||
{
|
||||
return lfloat.Value;
|
||||
}
|
||||
return v.UserData.Object;
|
||||
});
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(sbyte),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaSByte v
|
||||
? (sbyte)v
|
||||
: throw new ScriptRuntimeException("use SByte(value) to pass primitive type 'sbyte' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(byte),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaByte v
|
||||
? (byte)v
|
||||
: throw new ScriptRuntimeException("use Byte(value) to pass primitive type 'byte' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(short),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaInt16 v
|
||||
? (short)v
|
||||
: throw new ScriptRuntimeException("use Int16(value) to pass primitive type 'short' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(ushort),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaUInt16 v
|
||||
? (ushort)v
|
||||
: throw new ScriptRuntimeException("use UInt16(value) to pass primitive type 'ushort' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(int),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaInt32 v
|
||||
? (int)v
|
||||
: throw new ScriptRuntimeException("use Int32(value) to pass primitive type 'int' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(uint),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaUInt32 v
|
||||
? (uint)v
|
||||
: throw new ScriptRuntimeException("use UInt32(value) to pass primitive type 'uint' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(long),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaInt64 v
|
||||
? (long)v
|
||||
: throw new ScriptRuntimeException("use Int64(value) to pass primitive type 'long' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(ulong),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaUInt64 v
|
||||
? (ulong)v
|
||||
: throw new ScriptRuntimeException("use UInt64(value) to pass primitive type 'ulong' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(float),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaSingle v
|
||||
? (float)v
|
||||
: throw new ScriptRuntimeException("use Single(value) to pass primitive type 'float' to C#"));
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||
scriptDataType: DataType.UserData,
|
||||
clrDataType: typeof(double),
|
||||
converter: luaValue => luaValue.UserData.Object is LuaDouble v
|
||||
? (double)v
|
||||
: throw new ScriptRuntimeException("use Double(value) to pass primitive type 'double' to C#"));
|
||||
}
|
||||
|
||||
public static void RegisterAction<T>()
|
||||
|
||||
Reference in New Issue
Block a user