Misc Lua fixes

This commit is contained in:
Evil Factory
2026-02-07 21:53:30 -03:00
committed by Maplewheels
parent ba10d9d031
commit 422e8a6185
6 changed files with 24 additions and 53 deletions
@@ -39,12 +39,13 @@ public class DefaultLuaRegistrar : IDefaultLuaRegistrar
_userDataService.RegisterType("Barotrauma.Success`2");
_userDataService.RegisterType("Barotrauma.Failure`2");
_userDataService.RegisterType("Barotrauma.Range`1");
_userDataService.RegisterType("Barotrauma.ItemPrefab");
List<Assembly> assembliesToScan = [typeof(DefaultLuaRegistrar).Assembly, typeof(Identifier).Assembly, typeof(Microsoft.Xna.Framework.Vector2).Assembly];
foreach (var type in assembliesToScan.SelectMany(a => a.GetTypes()))
{
if (type.IsEnum || type.IsDefined(typeof(CompilerGeneratedAttribute)) || !_safeUserDataService.IsAllowed(type.FullName))
if (type.IsEnum || type.Name.StartsWith("<") || type.IsDefined(typeof(CompilerGeneratedAttribute)) || !_safeUserDataService.IsAllowed(type.FullName))
{
continue;
}
@@ -11,14 +11,14 @@ namespace Barotrauma
{
public class LuaConverters
{
private readonly Script _script;
private readonly ILuaScriptManagementService _luaScriptManagementService;
public LuaConverters(Script script)
public LuaConverters(ILuaScriptManagementService luaScriptManagementService)
{
_script = script;
_luaScriptManagementService = luaScriptManagementService;
}
private DynValue Call(object function, params object[] arguments) => _script.Call(function, arguments);
private DynValue Call(object function, params object[] arguments) => _luaScriptManagementService.CallFunctionSafe(function, arguments);
public void RegisterLuaConverters()
{
@@ -34,7 +34,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsAction), v => (LuaCsAction)(args =>
{
if (v.Function.OwnerScript == _script)
if (v.Function.OwnerScript == _luaScriptManagementService)
{
Call(v.Function, args);
}
@@ -42,7 +42,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsFunc), v => (LuaCsFunc)(args =>
{
if (v.Function.OwnerScript == _script)
if (v.Function.OwnerScript == _luaScriptManagementService.InternalScript)
{
return Call(v.Function, args);
}
@@ -51,7 +51,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsCompatPatchFunc), v => (LuaCsCompatPatchFunc)((self, args) =>
{
if (v.Function.OwnerScript == _script)
if (v.Function.OwnerScript == _luaScriptManagementService.InternalScript)
{
return Call(v.Function, self, args);
}
@@ -60,7 +60,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsPatchFunc), v => (LuaCsPatchFunc)((self, args) =>
{
if (v.Function.OwnerScript == _script)
if (v.Function.OwnerScript == _luaScriptManagementService.InternalScript)
{
return Call(v.Function, self, args);
}
@@ -137,7 +137,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(NetMessageReceived), v => (NetMessageReceived)((arg1) =>
{
if (v.Function.OwnerScript == _script)
if (v.Function.OwnerScript == _luaScriptManagementService.InternalScript)
{
Call(v.Function, arg1);
}
@@ -145,7 +145,7 @@ namespace Barotrauma
#elif SERVER
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(NetMessageReceived), v => (NetMessageReceived)((arg1, arg2) =>
{
if (v.Function.OwnerScript == _script)
if (v.Function.OwnerScript == _luaScriptManagementService.InternalScript)
{
Call(v.Function, arg1, arg2);
}
@@ -794,7 +794,6 @@ namespace Barotrauma.LuaCs
}
registeredPatches.Clear();
patchModuleBuilder = null;
compatHookPrefixMethods.Clear();
compatHookPostfixMethods.Clear();
@@ -87,7 +87,7 @@ public class LuaUserDataService : ILuaUserDataService
throw new ScriptRuntimeException($"tried to register a type that doesn't exist: {typeName}.");
}
return UserData.RegisterType(type, new CallableUserDataDescriptor(type));
return UserData.RegisterType(type);
}
public void RegisterExtensionType(string typeName)
@@ -407,34 +407,3 @@ public class LuaUserDataService : ILuaUserDataService
return FluentResults.Result.Ok();
}
}
sealed class CallableUserDataDescriptor : StandardUserDataDescriptor
{
public CallableUserDataDescriptor(Type type)
: base(type, InteropAccessMode.Default)
{
}
public override DynValue MetaIndex(Script script, object obj, string metaname)
{
if (metaname == "__call")
{
return DynValue.NewCallback((ctx, args) =>
{
var self = args[0];
var ctor = base.Index(script, obj, DynValue.NewString("__new"), true);
if (ctor == null || ctor.IsNil())
{
throw new ScriptRuntimeException("Attempted to call userdata without __new.");
}
var callArgs = args.GetArray().Skip(1).ToArray();
return script.Call(ctor, callArgs);
});
}
return base.MetaIndex(script, obj, metaname);
}
}