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

View File

@@ -28,6 +28,8 @@ namespace Barotrauma.LuaCs;
class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
{
public Script? InternalScript => _script;
private Script? _script;
private bool _isRunning;
[MemberNotNullWhen(true, nameof(_script))]
@@ -184,8 +186,10 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
Script.GlobalOptions.ShouldPCallCatchException = (Exception ex) => { return true; };
UserData.RegisterType<ILuaCsHook.HookMethodType>();
UserData.RegisterType(typeof(LuaGame));
UserData.RegisterType(typeof(EventService));
StandardUserDataDescriptor descriptor = (StandardUserDataDescriptor)UserData.RegisterType(typeof(EventService));
descriptor.AddDynValue("HookMethodType", UserData.CreateStatic<ILuaCsHook.HookMethodType>());
UserData.RegisterType(typeof(ILuaCsNetworking));
UserData.RegisterType(typeof(ILuaCsUtility));
UserData.RegisterType(typeof(ILuaCsTimer));
@@ -195,7 +199,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
UserData.RegisterType(typeof(IUserDataDescriptor));
UserData.RegisterType(typeof(INetworkingService));
new LuaConverters(_script).RegisterLuaConverters();
new LuaConverters(this).RegisterLuaConverters();
var luaRequire = new LuaRequire(_script);
@@ -287,7 +291,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
return result;
}
public DynValue? CallFunction(DynValue luaFunction, params object[] args)
public DynValue? CallFunctionSafe(object luaFunction, params object[] args)
{
if (!IsRunning) { return null; }

View File

@@ -14,11 +14,12 @@ namespace Barotrauma.LuaCs;
public interface ILuaScriptManagementService : IReusableService
{
#region Script_Ops
Script? InternalScript { get; }
object? GetGlobalTableValue(string tableName);
FluentResults.Result<DynValue> DoString(string code);
DynValue? CallFunctionSafe(object luaFunction, params object[] args);
/// <summary>
/// Parses and loads script sources (code) into a memory cache without executing it.
/// </summary>
@@ -54,8 +55,5 @@ public interface ILuaScriptManagementService : IReusableService
/// </summary>
/// <returns></returns>
/// <remarks>May be functionally equivalent to <see cref="IReusableService.Reset"/></remarks>
FluentResults.Result DisposeAllPackageResources();
#endregion
FluentResults.Result DisposeAllPackageResources();
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -794,7 +794,6 @@ namespace Barotrauma.LuaCs
}
registeredPatches.Clear();
patchModuleBuilder = null;
compatHookPrefixMethods.Clear();
compatHookPostfixMethods.Clear();

View File

@@ -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);
}
}