Some extra logging and bring back LuaCsTimer

This commit is contained in:
Evil Factory
2026-01-29 20:26:25 -03:00
committed by Maplewheels
parent 13a9bc443e
commit 708fe93efe
4 changed files with 46 additions and 32 deletions

View File

@@ -1,4 +1,5 @@
using Barotrauma.LuaCs.Services;
using Barotrauma.LuaCs.Events;
using Barotrauma.LuaCs.Services;
using Barotrauma.LuaCs.Services.Compatibility;
using System;
using System.Collections.Generic;
@@ -6,7 +7,7 @@ using System.Diagnostics;
namespace Barotrauma
{
public class LuaCsTimer : ILuaCsTimer
public class LuaCsTimer : ILuaCsTimer, IEventUpdate
{
public static double Time => Timing.TotalTime;
public static double GetTime() => Time;
@@ -55,6 +56,14 @@ namespace Barotrauma
private List<TimedAction> timedActions = new List<TimedAction>();
private readonly IEventService _eventService;
public LuaCsTimer(IEventService eventService)
{
_eventService = eventService;
_eventService.Subscribe<IEventUpdate>(this);
}
private void AddTimer(TimedAction timedAction)
{
if (timedAction == null)
@@ -75,7 +84,29 @@ namespace Barotrauma
}
}
public void Update()
public void Clear()
{
timedActions = new List<TimedAction>();
}
public void Wait(LuaCsAction action, int millisecondDelay)
{
TimedAction timedAction = new TimedAction(action, millisecondDelay);
AddTimer(timedAction);
}
public void NextFrame(LuaCsAction action)
{
TimedAction timedAction = new TimedAction(action, 0);
AddTimer(timedAction);
}
public void Dispose()
{
_eventService.Unsubscribe<IEventUpdate>(this);
}
public void OnUpdate(double fixedDeltaTime)
{
lock (timedActions)
{
@@ -104,28 +135,6 @@ namespace Barotrauma
}
}
public void Clear()
{
timedActions = new List<TimedAction>();
}
public void Wait(LuaCsAction action, int millisecondDelay)
{
TimedAction timedAction = new TimedAction(action, millisecondDelay);
AddTimer(timedAction);
}
public void NextFrame(LuaCsAction action)
{
TimedAction timedAction = new TimedAction(action, 0);
AddTimer(timedAction);
}
public void Dispose()
{
// ignored
}
public bool IsDisposed => false;
}
}

View File

@@ -191,6 +191,7 @@ namespace Barotrauma
servicesProvider.RegisterServiceType<ILuaScriptManagementService, LuaScriptManagementService>(ServiceLifetime.Singleton);
servicesProvider.RegisterServiceType<ILuaScriptLoader, LuaScriptLoader>(ServiceLifetime.Transient);
servicesProvider.RegisterServiceType<LuaGame, LuaGame>(ServiceLifetime.Singleton);
servicesProvider.RegisterServiceType<ILuaCsTimer, LuaCsTimer>(ServiceLifetime.Singleton);
// TODO: INetworkingService
servicesProvider.RegisterServiceType<IConfigService, ConfigService>(ServiceLifetime.Singleton);
servicesProvider.RegisterServiceType<IModConfigService, ModConfigService>(ServiceLifetime.Transient);

View File

@@ -45,6 +45,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
private readonly ILoggerService _loggerService;
private readonly LuaGame _luaGame;
private readonly ILuaCsHook _luaCsHook;
private readonly ILuaCsTimer _luaCsTimer;
//private readonly ILuaCsNetworking _luaCsNetworking;
//private readonly ILuaCsUtility _luaCsUtility;
//private readonly ILuaCsTimer _luaCsTimer;
@@ -54,10 +55,10 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
ILuaScriptLoader loader,
ILuaScriptServicesConfig luaScriptServicesConfig,
LuaGame luaGame,
ILuaCsHook luaCsHook
ILuaCsHook luaCsHook,
//ILuaCsNetworking luaCsNetworking,
//ILuaCsUtility luaCsUtility,
//ILuaCsTimer luaCsTimer
ILuaCsTimer luaCsTimer
)
{
_luaScriptLoader = loader;
@@ -68,7 +69,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
_luaCsHook = luaCsHook;
//_luaCsNetworking = luaCsNetworking;
//_luaCsUtility = luaCsUtility;
//_luaCsTimer = luaCsTimer;
_luaCsTimer = luaCsTimer;
}
public bool IsDisposed { get; private set; }
@@ -195,7 +196,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
_script.Globals["Game"] = _luaGame;
_script.Globals["Hook"] = _luaCsHook;
//_script.Globals["Timer"] = _luaCsTimer;
_script.Globals["Timer"] = _luaCsTimer;
_script.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
//_script.Globals["Networking"] = _luaCsNetworking;
//_script.Globals["Steam"] = Steam;
@@ -215,18 +216,21 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
return FluentResults.Result.Fail("Tried to execute Lua scripts without unloading first.");
}
_loggerService.LogMessage("Executing Lua scripts");
SetupEnvironment();
_isRunning = true;
var result = FluentResults.Result.Ok();
_isRunning = true;
foreach (ILuaScriptResourceInfo resource in executionOrder.Where(l => l.IsAutorun))
{
foreach (ContentPath filePath in resource.FilePaths)
{
try
{
_loggerService.LogMessage($"Run {filePath.Value}");
_script?.Call(_script.LoadFile(filePath.FullPath), Path.GetDirectoryName(resource.OwnerPackage.Path));
}
catch(Exception e)

View File

@@ -93,7 +93,7 @@ public class PluginManagementService : IAssemblyManagementService
public bool IsDisposed { get; }
public FluentResults.Result Reset()
{
throw new NotImplementedException();
return FluentResults.Result.Fail("Not implemented");
}
#endregion