- Added legacy LuaCsPerformanceCounter.cs

- Added legacy redirects.
- Added IConsoleCommandsService injection to plugins.
This commit is contained in:
MapleWheels
2026-03-31 10:48:44 -04:00
parent f1808d9231
commit 4f2da55a8e
4 changed files with 68 additions and 5 deletions
@@ -364,10 +364,15 @@ namespace Barotrauma
CoroutineManager.Update(paused: false, (float)Timing.Step); CoroutineManager.Update(paused: false, (float)Timing.Step);
performanceCounterTimer.Stop(); performanceCounterTimer.Stop();
if (LuaCsSetup.Instance.PerformanceCounterService.EnablePerformanceCounter)
{
LuaCsSetup.Instance.PerformanceCounterService.AddElapsedTicks(new SimplePerformanceData("Update", performanceCounterTimer.ElapsedTicks));
}
if (LuaCsSetup.Instance.PerformanceCounter.EnablePerformanceCounter) 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(); performanceCounterTimer.Reset();
Timing.Accumulator -= Timing.Step; Timing.Accumulator -= Timing.Step;
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Barotrauma
{
/// <summary>
/// <b>[Obsolete]</b> Legacy compatibility only.
/// </summary>
[Obsolete("Deprecated.")]
public class LuaCsPerformanceCounter
{
public bool EnablePerformanceCounter = false;
public double UpdateElapsedTime;
public Dictionary<string, Dictionary<string, double>> HookElapsedTime = new Dictionary<string, Dictionary<string, double>>();
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<string, double>();
}
HookElapsedTime[eventName][hookName] = (double)ticks / Stopwatch.Frequency;
}
}
}
@@ -69,7 +69,8 @@ namespace Barotrauma
private readonly IServicesProvider _servicesProvider; private readonly IServicesProvider _servicesProvider;
public PerformanceCounterService PerformanceCounter => _servicesProvider.GetService<PerformanceCounterService>(); private PerformanceCounterService _performanceCounterService;
public PerformanceCounterService PerformanceCounterService => _performanceCounterService ??= _servicesProvider.GetService<PerformanceCounterService>();
public ILoggerService Logger => _servicesProvider.GetService<ILoggerService>(); public ILoggerService Logger => _servicesProvider.GetService<ILoggerService>();
public IConfigService ConfigService => _servicesProvider.GetService<IConfigService>(); public IConfigService ConfigService => _servicesProvider.GetService<IConfigService>();
public IPackageManagementService PackageManagementService => _servicesProvider.GetService<IPackageManagementService>(); public IPackageManagementService PackageManagementService => _servicesProvider.GetService<IPackageManagementService>();
@@ -381,6 +382,17 @@ namespace Barotrauma
#region LegacyRedirects #region LegacyRedirects
// --- Compatibility
/// <summary>
/// <b>[Obsolete]</b> Legacy support only.
/// </summary>
[Obsolete]
public LuaCsPerformanceCounter PerformanceCounter { get; private set; } = new LuaCsPerformanceCounter();
/// <summary>
/// <b>[Obsolete] Use <see cref="IPluginManagementService"/> instead.</b>
/// </summary>
[Obsolete($"Use {nameof(PluginManagementService)} instead.")]
public IPluginManagementService PluginPackageManager => this.PluginManagementService;
public ILuaCsHook Hook => this.EventService; public ILuaCsHook Hook => this.EventService;
public INetworkingService Networking => this.NetworkingService; public INetworkingService Networking => this.NetworkingService;
public ILuaCsTimer Timer => _servicesProvider.GetService<ILuaCsTimer>(); public ILuaCsTimer Timer => _servicesProvider.GetService<ILuaCsTimer>();
@@ -413,6 +425,7 @@ namespace Barotrauma
_eventService = null; _eventService = null;
_game = null; _game = null;
PerformanceCounter = null;
_servicesProvider.DisposeAndReset(); _servicesProvider.DisposeAndReset();
} }
catch (Exception e) catch (Exception e)
@@ -198,6 +198,7 @@ public class PluginManagementService : IAssemblyManagementService
private Lazy<ILuaScriptManagementService> _luaScriptManagementService; private Lazy<ILuaScriptManagementService> _luaScriptManagementService;
private IEventService _pluginEventService; private IEventService _pluginEventService;
private Lazy<ILuaPatcher> _pluginLuaPatcherService; private Lazy<ILuaPatcher> _pluginLuaPatcherService;
private Func<IConsoleCommandsService> _consoleCommandServiceFactory;
private readonly ConcurrentDictionary<ContentPackage, IAssemblyLoaderService> _assemblyLoaders = new(); private readonly ConcurrentDictionary<ContentPackage, IAssemblyLoaderService> _assemblyLoaders = new();
private readonly ConcurrentDictionary<Type, ContentPackage> _pluginPackageLookup = new(); private readonly ConcurrentDictionary<Type, ContentPackage> _pluginPackageLookup = new();
private readonly ConcurrentDictionary<ContentPackage, ImmutableArray<IAssemblyPlugin>> _pluginInstances = new(); private readonly ConcurrentDictionary<ContentPackage, ImmutableArray<IAssemblyPlugin>> _pluginInstances = new();
@@ -213,7 +214,8 @@ public class PluginManagementService : IAssemblyManagementService
Lazy<IEventService> eventService, Lazy<IEventService> eventService,
Lazy<ILuaScriptManagementService> luaScriptManagementService, Lazy<ILuaScriptManagementService> luaScriptManagementService,
Lazy<IConfigService> configService, Lazy<IConfigService> configService,
Lazy<ILuaPatcher> pluginLuaPatcherService) Lazy<ILuaPatcher> pluginLuaPatcherService,
Func<IConsoleCommandsService> consoleCommandServiceFactory)
{ {
_assemblyLoaderFactory = assemblyLoaderFactory; _assemblyLoaderFactory = assemblyLoaderFactory;
_storageService = storageService; _storageService = storageService;
@@ -222,6 +224,7 @@ public class PluginManagementService : IAssemblyManagementService
_luaScriptManagementService = luaScriptManagementService; _luaScriptManagementService = luaScriptManagementService;
_configService = configService; _configService = configService;
_pluginLuaPatcherService = pluginLuaPatcherService; _pluginLuaPatcherService = pluginLuaPatcherService;
_consoleCommandServiceFactory = consoleCommandServiceFactory;
} }
private ServiceContainer CreatePluginServiceContainer() private ServiceContainer CreatePluginServiceContainer()
@@ -240,6 +243,7 @@ public class PluginManagementService : IAssemblyManagementService
container.Register<IPluginManagementService>(fac => this); container.Register<IPluginManagementService>(fac => this);
container.Register<ILuaScriptManagementService>(fac => _luaScriptManagementService.Value); container.Register<ILuaScriptManagementService>(fac => _luaScriptManagementService.Value);
container.Register<IConfigService>(fac => _configService.Value); container.Register<IConfigService>(fac => _configService.Value);
container.Register<IConsoleCommandsService>(fac => _consoleCommandServiceFactory?.Invoke());
return container; return container;
} }