Fix bullshit Lua issues

This commit is contained in:
Evil Factory
2026-02-12 21:50:13 -03:00
parent 07d838eee0
commit 5f38b4a43a
5 changed files with 22 additions and 14 deletions
@@ -2,7 +2,7 @@
namespace Barotrauma.LuaCs.Compatibility; namespace Barotrauma.LuaCs.Compatibility;
internal partial interface ILuaCsTimer : ILuaCsShim internal partial interface ILuaCsTimer : IReusableService, ILuaCsShim
{ {
public static double Time => Timing.TotalTime; public static double Time => Timing.TotalTime;
public static double GetTime() => Time; public static double GetTime() => Time;
@@ -25,10 +25,7 @@ internal class HarmonyEventPatchesService : IService
Harmony.PatchAll(typeof(HarmonyEventPatchesService)); Harmony.PatchAll(typeof(HarmonyEventPatchesService));
} }
// TODO: This causes like hell in Debug.
#if !DEBUG
[HarmonyPatch(typeof(CoroutineManager), nameof(CoroutineManager.Update)), HarmonyPostfix] [HarmonyPatch(typeof(CoroutineManager), nameof(CoroutineManager.Update)), HarmonyPostfix]
#endif
public static void CoroutineManager_Update_Post() public static void CoroutineManager_Update_Post()
{ {
_eventService.PublishEvent<IEventUpdate>(x => x.OnUpdate(Timing.TotalTime)); _eventService.PublishEvent<IEventUpdate>(x => x.OnUpdate(Timing.TotalTime));
@@ -174,9 +174,8 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
_eventService.RegisterLuaEventAlias<IEventUpdate>("think", "OnUpdate"); _eventService.RegisterLuaEventAlias<IEventUpdate>("think", "OnUpdate");
_eventService.RegisterLuaEventAlias<IEventKeyUpdate>("keyUpdate", "OnKeyUpdate"); _eventService.RegisterLuaEventAlias<IEventKeyUpdate>("keyUpdate", "OnKeyUpdate");
_eventService.RegisterLuaEventAlias<IEventCharacterCreated>("character.created", "OnCharacterCreated"); _eventService.RegisterLuaEventAlias<IEventCharacterCreated>("character.created", "OnCharacterCreated");
_eventService.RegisterLuaEventAlias<IEventCharacterCreated>("character.giveJobItems", "OnGiveCharacterJobItems"); _eventService.RegisterLuaEventAlias<IEventGiveCharacterJobItems>("character.giveJobItems", "OnGiveCharacterJobItems");
_eventService.RegisterLuaEventAlias<IEventCharacterCreated>("afflictionUpdate", "OnAfflictionUpdate"); _eventService.RegisterLuaEventAlias<IEventAfflictionUpdate>("afflictionUpdate", "OnAfflictionUpdate");
} }
private void SetupEnvironment(bool enableSandbox) private void SetupEnvironment(bool enableSandbox)
@@ -357,6 +356,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
{ {
_luaScriptLoader.ClearCaches(); _luaScriptLoader.ClearCaches();
_userDataService.Reset(); _userDataService.Reset();
_luaCsTimer.Reset();
RegisterLuaEvents(); RegisterLuaEvents();
return DisposeAllPackageResources(); return DisposeAllPackageResources();
} }
@@ -34,7 +34,7 @@ namespace Barotrauma
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsAction), v => (LuaCsAction)(args => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsAction), v => (LuaCsAction)(args =>
{ {
if (v.Function.OwnerScript == _luaScriptManagementService) if (v.Function.OwnerScript == _luaScriptManagementService.InternalScript)
{ {
Call(v.Function, args); Call(v.Function, args);
} }
@@ -61,7 +61,7 @@ namespace Barotrauma
public LuaCsTimer(IEventService eventService) public LuaCsTimer(IEventService eventService)
{ {
_eventService = eventService; _eventService = eventService;
_eventService.Subscribe<IEventUpdate>(this); SubscribeToEvents();
} }
private void AddTimer(TimedAction timedAction) private void AddTimer(TimedAction timedAction)
@@ -101,11 +101,6 @@ namespace Barotrauma
AddTimer(timedAction); AddTimer(timedAction);
} }
public void Dispose()
{
_eventService.Unsubscribe<IEventUpdate>(this);
}
public void OnUpdate(double fixedDeltaTime) public void OnUpdate(double fixedDeltaTime)
{ {
lock (timedActions) lock (timedActions)
@@ -135,6 +130,22 @@ namespace Barotrauma
} }
} }
private void SubscribeToEvents()
{
_eventService.Subscribe<IEventUpdate>(this);
}
public FluentResults.Result Reset()
{
SubscribeToEvents();
return FluentResults.Result.Ok();
}
public void Dispose()
{
_eventService.Unsubscribe<IEventUpdate>(this);
}
public bool IsDisposed => false; public bool IsDisposed => false;
} }
} }