added support for None<T> and Some<T> and added some missing types from last update
This commit is contained in:
@@ -16,6 +16,7 @@ defaultLib["Int64"] = CreateStatic("Barotrauma.LuaInt64", true)
|
||||
defaultLib["UInt64"] = CreateStatic("Barotrauma.LuaUInt64", true)
|
||||
defaultLib["Single"] = CreateStatic("Barotrauma.LuaSingle", true)
|
||||
defaultLib["Double"] = CreateStatic("Barotrauma.LuaDouble", true)
|
||||
defaultLib["none"] = CreateStatic("Barotrauma.LuaNone", true)()
|
||||
|
||||
-- Backward compatibility
|
||||
defaultLib["Float"] = CreateStatic("Barotrauma.LuaSingle", true)
|
||||
@@ -112,6 +113,9 @@ defaultLib["XDocument"] = CreateStatic("System.Xml.Linq.XDocument", true)
|
||||
defaultLib["XNode"] = CreateStatic("System.Xml.Linq.XNode", true)
|
||||
defaultLib["SoundsFile"] = CreateStatic("Barotrauma.SoundsFile", true)
|
||||
|
||||
defaultLib["ContentPackageId"] = CreateStatic("Barotrauma.ContentPackageId")
|
||||
defaultLib["IPAddress"] = CreateStatic("System.Net.IPAddress")
|
||||
defaultLib["Address"] = CreateStatic("Barotrauma.Networking.Address")
|
||||
|
||||
defaultLib["Explosion"] = CreateStatic("Barotrauma.Explosion", true)
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ RegisterBarotrauma("LuaInt64")
|
||||
RegisterBarotrauma("LuaUInt64")
|
||||
RegisterBarotrauma("LuaSingle")
|
||||
RegisterBarotrauma("LuaDouble")
|
||||
RegisterBarotrauma("LuaNone")
|
||||
|
||||
RegisterBarotrauma("GameMain")
|
||||
RegisterBarotrauma("Networking.BanList")
|
||||
@@ -77,10 +78,20 @@ RegisterBarotrauma("FabricationRecipe+RequiredItemByIdentifier")
|
||||
RegisterBarotrauma("FabricationRecipe+RequiredItemByTag")
|
||||
RegisterBarotrauma("Submarine")
|
||||
|
||||
RegisterBarotrauma("INetSerializableStruct")
|
||||
RegisterBarotrauma("Networking.AccountInfo")
|
||||
RegisterBarotrauma("Networking.AccountId")
|
||||
RegisterBarotrauma("Networking.SteamId")
|
||||
RegisterBarotrauma("Networking.Address")
|
||||
RegisterBarotrauma("Networking.UnknownAddress")
|
||||
RegisterBarotrauma("Networking.SteamP2PAddress")
|
||||
RegisterBarotrauma("Networking.PipeAddress")
|
||||
RegisterBarotrauma("Networking.LidgrenAddress")
|
||||
RegisterBarotrauma("Networking.Endpoint")
|
||||
RegisterBarotrauma("Networking.SteamP2PEndpoint")
|
||||
RegisterBarotrauma("Networking.PipeEndpoint")
|
||||
RegisterBarotrauma("Networking.LidgrenEndpoint")
|
||||
|
||||
RegisterBarotrauma("INetSerializableStruct")
|
||||
RegisterBarotrauma("Networking.Client")
|
||||
RegisterBarotrauma("Networking.TempClient")
|
||||
RegisterBarotrauma("Networking.NetworkConnection")
|
||||
@@ -131,6 +142,8 @@ RegisterBarotrauma("RegularPackage")
|
||||
RegisterBarotrauma("CorePackage")
|
||||
RegisterBarotrauma("ContentXElement")
|
||||
RegisterBarotrauma("ContentPath")
|
||||
RegisterBarotrauma("ContentPackageId")
|
||||
RegisterBarotrauma("SteamWorkshopId")
|
||||
|
||||
RegisterBarotrauma("SoundsFile")
|
||||
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
using System;
|
||||
using System;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public struct LuaNone
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public struct LuaSByte
|
||||
{
|
||||
private readonly sbyte value;
|
||||
@@ -181,4 +186,4 @@ namespace Barotrauma
|
||||
|
||||
public static implicit operator double(LuaDouble luaValue) => luaValue.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using MoonSharp.Interpreter;
|
||||
using Microsoft.Xna.Framework;
|
||||
using FarseerPhysics.Dynamics;
|
||||
using LuaCsCompatPatchFunc = Barotrauma.LuaCsPatch;
|
||||
using Barotrauma.Networking;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
@@ -187,12 +188,57 @@ namespace Barotrauma
|
||||
? (double)v
|
||||
: throw new ScriptRuntimeException("use Double(value) to pass primitive type 'double' to C#"));
|
||||
|
||||
RegisterOption<Character>();
|
||||
RegisterOption<Barotrauma.Networking.AccountId>();
|
||||
RegisterOption<int>();
|
||||
RegisterOption<Character>(DataType.UserData);
|
||||
RegisterOption<AccountId>(DataType.UserData);
|
||||
RegisterOption<ContentPackageId>(DataType.UserData);
|
||||
RegisterOption<SteamId>(DataType.UserData);
|
||||
RegisterOption<DateTime>(DataType.UserData);
|
||||
RegisterOption<BannedPlayer>(DataType.UserData);
|
||||
|
||||
RegisterOption<int>(DataType.Number);
|
||||
|
||||
RegisterEither<Address, AccountId>();
|
||||
}
|
||||
|
||||
private void RegisterOption<T>()
|
||||
private void RegisterEither<T1, T2>()
|
||||
{
|
||||
DynValue convertEitherIntoDynValue(Either<T1, T2> either)
|
||||
{
|
||||
if (either.TryGet(out T1 value1))
|
||||
{
|
||||
return UserData.Create(value1);
|
||||
}
|
||||
|
||||
if (either.TryGet(out T2 value2))
|
||||
{
|
||||
return UserData.Create(value2);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(EitherT<T1, T2>), (Script v, object obj) =>
|
||||
{
|
||||
if (obj is EitherT<T1, T2> either)
|
||||
{
|
||||
return convertEitherIntoDynValue(either);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(EitherU<T1, T2>), (Script v, object obj) =>
|
||||
{
|
||||
if (obj is EitherU<T1, T2> either)
|
||||
{
|
||||
return convertEitherIntoDynValue(either);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private void RegisterOption<T>(DataType dataType)
|
||||
{
|
||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(Option<T>), (Script v, object obj) =>
|
||||
{
|
||||
@@ -200,16 +246,40 @@ namespace Barotrauma
|
||||
{
|
||||
if (option.TryUnwrap(out T outValue))
|
||||
{
|
||||
return UserData.Create(outValue);
|
||||
return outValue == null ? DynValue.Nil : UserData.Create(outValue);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.UserData, typeof(Option<T>), v =>
|
||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(None<T>), (Script v, object obj) =>
|
||||
{
|
||||
return Option<T>.Some(v.ToObject<T>());
|
||||
return UserData.Create(default(LuaNone));
|
||||
});
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(Some<T>), (Script v, object obj) =>
|
||||
{
|
||||
if (obj is Some<T> some)
|
||||
{
|
||||
return some.Value == null ? DynValue.Nil : UserData.Create(some.Value);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(dataType, typeof(Option<T>), v =>
|
||||
{
|
||||
if (v.UserData.Object is LuaNone)
|
||||
{
|
||||
return Option<T>.None();
|
||||
}
|
||||
else
|
||||
{
|
||||
Option<T>.Some(v.ToObject<T>());
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user