From 4f2da55a8e95cd1482b0c299f20b37d1bce23701 Mon Sep 17 00:00:00 2001 From: MapleWheels Date: Tue, 31 Mar 2026 10:48:44 -0400 Subject: [PATCH] - Added legacy LuaCsPerformanceCounter.cs - Added legacy redirects. - Added IConsoleCommandsService injection to plugins. --- .../BarotraumaServer/ServerSource/GameMain.cs | 7 +++- .../Compatibility/LuaCsPerformanceCounter.cs | 41 +++++++++++++++++++ .../SharedSource/LuaCs/LuaCsSetup.cs | 19 +++++++-- .../_Services/PluginManagementService.cs | 6 ++- 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 Barotrauma/BarotraumaShared/SharedSource/LuaCs/Compatibility/LuaCsPerformanceCounter.cs diff --git a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs index 2fc1138e4..bb4c4f628 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/GameMain.cs @@ -364,10 +364,15 @@ namespace Barotrauma CoroutineManager.Update(paused: false, (float)Timing.Step); performanceCounterTimer.Stop(); + if (LuaCsSetup.Instance.PerformanceCounterService.EnablePerformanceCounter) + { + LuaCsSetup.Instance.PerformanceCounterService.AddElapsedTicks(new SimplePerformanceData("Update", performanceCounterTimer.ElapsedTicks)); + } if (LuaCsSetup.Instance.PerformanceCounter.EnablePerformanceCounter) { - LuaCsSetup.Instance.PerformanceCounter.AddElapsedTicks(new SimplePerformanceData("Update", performanceCounterTimer.ElapsedTicks)); + LuaCsSetup.Instance.PerformanceCounter.UpdateElapsedTime = (double)performanceCounterTimer.ElapsedTicks / Stopwatch.Frequency; } + performanceCounterTimer.Reset(); Timing.Accumulator -= Timing.Step; diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Compatibility/LuaCsPerformanceCounter.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Compatibility/LuaCsPerformanceCounter.cs new file mode 100644 index 000000000..807b30ecc --- /dev/null +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Compatibility/LuaCsPerformanceCounter.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; + + +namespace Barotrauma +{ + /// + /// [Obsolete] Legacy compatibility only. + /// + [Obsolete("Deprecated.")] + public class LuaCsPerformanceCounter + { + public bool EnablePerformanceCounter = false; + + public double UpdateElapsedTime; + public Dictionary> HookElapsedTime = new Dictionary>(); + + public static float MemoryUsage + { + get + { + Process proc = Process.GetCurrentProcess(); + float memory = MathF.Round(proc.PrivateMemorySize64 / (1024 * 1024), 2); + proc.Dispose(); + + return memory; + } + } + + public void SetHookElapsedTicks(string eventName, string hookName, long ticks) + { + if (!HookElapsedTime.ContainsKey(eventName)) + { + HookElapsedTime[eventName] = new Dictionary(); + } + + HookElapsedTime[eventName][hookName] = (double)ticks / Stopwatch.Frequency; + } + } +} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs index 0e8dcb1bc..21cf10aa5 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/LuaCsSetup.cs @@ -68,8 +68,9 @@ namespace Barotrauma */ private readonly IServicesProvider _servicesProvider; - - public PerformanceCounterService PerformanceCounter => _servicesProvider.GetService(); + + private PerformanceCounterService _performanceCounterService; + public PerformanceCounterService PerformanceCounterService => _performanceCounterService ??= _servicesProvider.GetService(); public ILoggerService Logger => _servicesProvider.GetService(); public IConfigService ConfigService => _servicesProvider.GetService(); public IPackageManagementService PackageManagementService => _servicesProvider.GetService(); @@ -82,8 +83,8 @@ namespace Barotrauma // hotpath performance ref cache private LuaGame _game; public LuaGame Game => _game ??= _servicesProvider.GetService(); - + /// /// Whether C# plugin code is enabled. /// @@ -381,6 +382,17 @@ namespace Barotrauma #region LegacyRedirects + // --- Compatibility + /// + /// [Obsolete] Legacy support only. + /// + [Obsolete] + public LuaCsPerformanceCounter PerformanceCounter { get; private set; } = new LuaCsPerformanceCounter(); + /// + /// [Obsolete] Use instead. + /// + [Obsolete($"Use {nameof(PluginManagementService)} instead.")] + public IPluginManagementService PluginPackageManager => this.PluginManagementService; public ILuaCsHook Hook => this.EventService; public INetworkingService Networking => this.NetworkingService; public ILuaCsTimer Timer => _servicesProvider.GetService(); @@ -413,6 +425,7 @@ namespace Barotrauma _eventService = null; _game = null; + PerformanceCounter = null; _servicesProvider.DisposeAndReset(); } catch (Exception e) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs index a8cf9ec23..6c23761dd 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs @@ -198,6 +198,7 @@ public class PluginManagementService : IAssemblyManagementService private Lazy _luaScriptManagementService; private IEventService _pluginEventService; private Lazy _pluginLuaPatcherService; + private Func _consoleCommandServiceFactory; private readonly ConcurrentDictionary _assemblyLoaders = new(); private readonly ConcurrentDictionary _pluginPackageLookup = new(); private readonly ConcurrentDictionary> _pluginInstances = new(); @@ -213,7 +214,8 @@ public class PluginManagementService : IAssemblyManagementService Lazy eventService, Lazy luaScriptManagementService, Lazy configService, - Lazy pluginLuaPatcherService) + Lazy pluginLuaPatcherService, + Func consoleCommandServiceFactory) { _assemblyLoaderFactory = assemblyLoaderFactory; _storageService = storageService; @@ -222,6 +224,7 @@ public class PluginManagementService : IAssemblyManagementService _luaScriptManagementService = luaScriptManagementService; _configService = configService; _pluginLuaPatcherService = pluginLuaPatcherService; + _consoleCommandServiceFactory = consoleCommandServiceFactory; } private ServiceContainer CreatePluginServiceContainer() @@ -240,6 +243,7 @@ public class PluginManagementService : IAssemblyManagementService container.Register(fac => this); container.Register(fac => _luaScriptManagementService.Value); container.Register(fac => _configService.Value); + container.Register(fac => _consoleCommandServiceFactory?.Invoke()); return container; }