Reimplementation of DoString with lua/cl_lua commands and fix Lua scripts not being loaded properly

This commit is contained in:
Evil Factory
2026-01-28 23:41:23 -03:00
committed by Maplewheels
parent f0f09c20fa
commit 67d3d5f587
6 changed files with 28 additions and 34 deletions

View File

@@ -4225,27 +4225,20 @@ namespace Barotrauma
commands.Add(new Command("cl_lua", $"cl_lua: Runs a string on the client.", (string[] args) =>
{
throw new NotImplementedException();
/*if (GameMain.Client != null && !GameMain.Client.HasPermission(ClientPermissions.ConsoleCommands))
if (GameMain.Client != null && !GameMain.Client.HasPermission(ClientPermissions.ConsoleCommands))
{
ThrowError("Command not permitted.");
return;
}
if (GameMain.LuaCs.Lua == null)
if (GameMain.LuaCs.CurrentRunState != RunState.Running)
{
ThrowError("LuaCs not initialized, use the console command cl_reloadluacs to force initialization.");
return;
}
try
{
GameMain.LuaCs.Lua.DoString(string.Join(" ", args));
}
catch(Exception ex)
{
LuaCsLogger.HandleException(ex, LuaCsMessageOrigin.LuaMod);
}*/
var result = GameMain.LuaCs.LuaScriptManagementService.DoString(string.Join(" ", args));
GameMain.LuaCs.Logger.LogResults(result.ToResult());
}));
commands.Add(new Command("cl_reloadlua|cl_reloadcs|cl_reloadluacs", "Re-initializes the LuaCs environment.", (string[] args) =>

View File

@@ -1291,15 +1291,8 @@ namespace Barotrauma
commands.Add(new Command("lua", "lua: Runs a string.", (string[] args) =>
{
try
{
throw new NotImplementedException();
//GameMain.LuaCs.Lua.DoString(string.Join(" ", args));
}
catch (Exception ex)
{
LuaCsLogger.HandleException(ex, LuaCsMessageOrigin.LuaMod);
}
var result = GameMain.LuaCs.LuaScriptManagementService.DoString(string.Join(" ", args));
GameMain.LuaCs.Logger.LogResults(result.ToResult());
}));
commands.Add(new Command("reloadlua|reloadcs|reloadluacs", "Re-initializes the LuaCs environment.", (string[] args) =>

View File

@@ -1,6 +1,6 @@
LuaSetup = {}
local path, resourcesToExecute = ...
local path = ...
package.path = {path .. "/Lua/?.lua"}
@@ -42,10 +42,4 @@ require("DefaultLib/Utils/SteamApi")
require("PostSetup")
LuaSetup = nil
for resource in resourcesToExecute do
for path in resource.FilePaths do
dofile(path)
end
end
LuaSetup = nil

View File

@@ -11,7 +11,7 @@ namespace Barotrauma
internal class LuaUserData
{
[Obsolete("Use IPluginManagementService::GetTypesByName()")]
public static Type GetType(string typeName) => GameMain.LuaCs.PluginManagementService.GetType(typeName); //LuaCsSetup.GetType(typeName);
public static Type GetType(string typeName) => GameMain.LuaCs.PluginManagementService.GetType(typeName, includeInterfaces: true); //LuaCsSetup.GetType(typeName);
public static IUserDataDescriptor RegisterType(string typeName)
{

View File

@@ -99,6 +99,22 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
return new FluentResults.Result().WithReasons(cacheRes.Value.SelectMany(cr => cr.Item2.Reasons));
}
public FluentResults.Result<DynValue> DoString(string code)
{
IService.CheckDisposed(this);
if (_script == null || !IsRunning) { throw new Exception("Disposed"); }
try
{
var result = _script.DoString(code);
return FluentResults.Result.Ok(result);
}
catch (Exception ex)
{
return FluentResults.Result.Fail(new ExceptionalError(ex));
}
}
private DynValue DoFile(string file, Table? globalContext = null, string? codeStringFriendly = null)
{
if (_script == null)
@@ -205,16 +221,13 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
var result = FluentResults.Result.Ok();
List<ILuaScriptResourceInfo> initializationScripts = executionOrder.Where(x => x.OwnerPackage.Name == "LuaCsForBarotrauma").ToList();
List<ILuaScriptResourceInfo> otherScripts = executionOrder.Except(initializationScripts).ToList();
foreach (ILuaScriptResourceInfo resource in initializationScripts)
foreach (ILuaScriptResourceInfo resource in executionOrder.Where(l => l.IsAutorun))
{
foreach (ContentPath filePath in resource.FilePaths)
{
try
{
_script?.Call(_script.LoadFile(filePath.FullPath), Path.GetDirectoryName(resource.OwnerPackage.Path), otherScripts.ToList());
_script?.Call(_script.LoadFile(filePath.FullPath), Path.GetDirectoryName(resource.OwnerPackage.Path));
}
catch(Exception e)
{

View File

@@ -17,6 +17,7 @@ public interface ILuaScriptManagementService : IReusableService
#region Script_Ops
object? GetGlobalTableValue(string tableName);
FluentResults.Result<DynValue> DoString(string code);
/// <summary>
/// Parses and loads script sources (code) into a memory cache without executing it.