diff --git a/Barotrauma/BarotraumaServer/ServerSource/LuaCs/Services/PackageManagementService.cs b/Barotrauma/BarotraumaServer/ServerSource/LuaCs/Services/PackageManagementService.cs deleted file mode 100644 index ef6ee88a6..000000000 --- a/Barotrauma/BarotraumaServer/ServerSource/LuaCs/Services/PackageManagementService.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Collections.Generic; -using Barotrauma.LuaCs.Data; -using Barotrauma.LuaCs.Services.Processing; - -namespace Barotrauma.LuaCs.Services; - -public partial class PackageManagementService -{ - -} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/IModConfigInfo.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/IModConfigInfo.cs index c8740e51d..70cb2d6d7 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/IModConfigInfo.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Data/IModConfigInfo.cs @@ -4,7 +4,7 @@ using System.Xml.Linq; namespace Barotrauma.LuaCs.Data; -public partial interface IModConfigInfo : IAssembliesResourcesInfo, +public interface IModConfigInfo : IAssembliesResourcesInfo, ILuaScriptsResourcesInfo, IConfigsResourcesInfo { // package info diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PackageManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PackageManagementService.cs index a14ed5784..802b2dc07 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PackageManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PackageManagementService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections.Concurrent; +using System.Collections.Generic; using System.Collections.Immutable; using System.Threading.Tasks; using Barotrauma.LuaCs.Data; @@ -6,105 +7,132 @@ using FluentResults; namespace Barotrauma.LuaCs.Services; -public partial class PackageManagementService : IPackageManagementService +public sealed class PackageManagementService : IPackageManagementService { + // svc + private ILoggerService _logger; + private IModConfigService _modConfigService; + private ILuaScriptManagementService _luaScriptManagementService; + private IPluginManagementService _pluginManagementService; + // state + private readonly ConcurrentDictionary _loadedPackages = new(); + private readonly ConcurrentDictionary _runningPackages = new(); + // control + private readonly AsyncReaderWriterLock _operationsLock = new(); + + public PackageManagementService(ILoggerService logger, IModConfigService modConfigService, ILuaScriptManagementService luaScriptManagementService, IPluginManagementService pluginManagementService) + { + _logger = logger; + _modConfigService = modConfigService; + _luaScriptManagementService = luaScriptManagementService; + _pluginManagementService = pluginManagementService; + } + public void Dispose() { - throw new System.NotImplementedException(); + using var lck = _operationsLock.AcquireWriterLock().ConfigureAwait(false).GetAwaiter().GetResult(); + if (!ModUtils.Threading.CheckIfClearAndSetBool(ref _isDisposed)) + return; + + _logger.LogMessage($"{nameof(PackageManagementService)} is disposing"); + _luaScriptManagementService.Dispose(); + _pluginManagementService.Dispose(); + _modConfigService.Dispose(); + _logger.Dispose(); + + _logger = null; + _luaScriptManagementService = null; + _pluginManagementService = null; + _modConfigService = null; + _loadedPackages.Clear(); + _runningPackages.Clear(); } private int _isDisposed = 0; public bool IsDisposed { get => ModUtils.Threading.GetBool(ref _isDisposed); - private set => ModUtils.Threading.SetBool(ref _isDisposed, value); + set => ModUtils.Threading.SetBool(ref _isDisposed, value); } - public FluentResults.Result Reset() { - throw new System.NotImplementedException(); + using var lck = _operationsLock.AcquireWriterLock().ConfigureAwait(false).GetAwaiter().GetResult(); + if (IsDisposed) + return FluentResults.Result.Fail($"{nameof(PackageManagementService)}failed to reset. Has already been disposed."); + + var operationResult = new FluentResults.Result(); + CombineResultErrors(operationResult, UnsafeStopRunningPackagesInternal()); + CombineResultErrors(operationResult, UnsafeUnloadAllPackagesInternal()); + return operationResult; + + void CombineResultErrors(FluentResults.Result result, + ImmutableArray<(ContentPackage Package, FluentResults.Result OperationResult)> packRes) + { + if (packRes.IsDefaultOrEmpty) + return; + + foreach (var r in packRes) + { + if (r.OperationResult.IsSuccess) + continue; + _logger.LogResults(r.OperationResult); + result.WithErrors(r.OperationResult.Errors); + } + } } - public ImmutableArray Configs { get; } - public ImmutableArray LuaScripts { get; } - public ImmutableArray Assemblies { get; } - public async Task LoadPackageInfosAsync(ContentPackage package) + public FluentResults.Result LoadPackageInfo(ContentPackage package) { throw new System.NotImplementedException(); } - public async Task> LoadPackagesInfosAsync(IReadOnlyList packages) + public ImmutableArray<(ContentPackage Package, FluentResults.Result LoadSuccessResult)> LoadPackagesInfo(IReadOnlyCollection packages) { throw new System.NotImplementedException(); } - public IReadOnlyList GetAllLoadedPackages() + private FluentResults.Result UnsafeLoadPackageInfoInternal(ContentPackage package) { throw new System.NotImplementedException(); } - public bool IsPackageLoaded(ContentPackage package) + public ImmutableArray<(ContentPackage Package, FluentResults.Result ExecutionResult)> ExecuteLoadedPackages() { throw new System.NotImplementedException(); } - public ImmutableArray FilterUnloadableResources(IReadOnlyList resources, bool enabledPackagesOnly = false) where T : IResourceInfo + private ImmutableArray<(ContentPackage Package, FluentResults.Result StopExectionResult)> UnsafeStopRunningPackagesInternal() + { + throw new System.NotImplementedException(); + } + + public ImmutableArray<(ContentPackage Package, FluentResults.Result StopExecutionResult)> StopRunningPackages() { throw new System.NotImplementedException(); } - public void DisposePackageInfos(ContentPackage package) + private FluentResults.Result UnsafeUnloadPackageInternal(ContentPackage package) { throw new System.NotImplementedException(); } - public void DisposePackagesInfos(IReadOnlyList packages) + private ImmutableArray<(ContentPackage Package, FluentResults.Result UnloadSuccessResult)> UnsafeUnloadAllPackagesInternal() + { + throw new System.NotImplementedException(); + } + + public FluentResults.Result UnloadPackage(ContentPackage package) + { + throw new System.NotImplementedException(); + } + + public ImmutableArray<(ContentPackage Package, FluentResults.Result UnloadSuccessResult)> UnloadPackages(IReadOnlyCollection packages) { throw new System.NotImplementedException(); } - public Result GetAssembliesInfos(ContentPackage package, bool onlySupportedResources = true) - { - throw new System.NotImplementedException(); - } - - public Result GetConfigsInfos(ContentPackage package, bool onlySupportedResources = true) - { - throw new System.NotImplementedException(); - } - - public Result GetLuaScriptsInfos(ContentPackage package, bool onlySupportedResources = true) - { - throw new System.NotImplementedException(); - } - - public Result GetAssembliesInfos(IReadOnlyList packages, bool onlySupportedResources = true) - { - throw new System.NotImplementedException(); - } - - public Result GetConfigsInfos(IReadOnlyList packages, bool onlySupportedResources = true) - { - throw new System.NotImplementedException(); - } - - public Result GetLuaScriptsInfos(IReadOnlyList packages, bool onlySupportedResources = true) - { - throw new System.NotImplementedException(); - } - - public async Task> GetAssembliesInfosAsync(IReadOnlyList packages, bool onlySupportedResources = true) - { - throw new System.NotImplementedException(); - } - - public async Task> GetConfigsInfosAsync(IReadOnlyList packages, bool onlySupportedResources = true) - { - throw new System.NotImplementedException(); - } - - public async Task> GetLuaScriptsInfosAsync(IReadOnlyList packages, bool onlySupportedResources = true) + public ImmutableArray<(ContentPackage Package, FluentResults.Result UnloadSuccessResult)> UnloadAllPackages() { throw new System.NotImplementedException(); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/IPackageManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/IPackageManagementService.cs index ae566c125..3f0614154 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/IPackageManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/IPackageManagementService.cs @@ -9,50 +9,13 @@ using Barotrauma.LuaCs.Data; namespace Barotrauma.LuaCs.Services; -public interface IPackageManagementService : IReusableService, IConfigsResourcesInfo, ILuaScriptsResourcesInfo, IAssembliesResourcesInfo +public interface IPackageManagementService : IReusableService { - /// - /// Loads and parses the provided for supported by the current runtime environment. - /// Will overwrite any existing package data. - /// - /// Package to load. - /// - Task LoadPackageInfosAsync(ContentPackage package); - /// - /// Loads and parses the provided collection for supported by the current runtime environment. - /// Will overwrite any existing package data. - /// - /// List of packages to load. - /// - Task> LoadPackagesInfosAsync(IReadOnlyList packages); - IReadOnlyList GetAllLoadedPackages(); - bool IsPackageLoaded(ContentPackage package); - - /// - /// Filters out resources not suitable for the current environment using the following criteria:
- /// - Platform (Operating System)
- /// - Target (Client|Server)
- /// - Null/Invalid
- /// - Dependency Package Registered in PMS
- ///
- /// - /// - /// - ImmutableArray FilterUnloadableResources(IReadOnlyList resources, bool enabledPackagesOnly = false) - where T : IResourceInfo; - void DisposePackageInfos(ContentPackage package); - void DisposePackagesInfos(IReadOnlyList packages); - - // single - FluentResults.Result GetAssembliesInfos(ContentPackage package, bool onlySupportedResources = true); - FluentResults.Result GetConfigsInfos(ContentPackage package, bool onlySupportedResources = true); - FluentResults.Result GetLuaScriptsInfos(ContentPackage package, bool onlySupportedResources = true); - // collection - FluentResults.Result GetAssembliesInfos(IReadOnlyList packages, bool onlySupportedResources = true); - FluentResults.Result GetConfigsInfos(IReadOnlyList packages, bool onlySupportedResources = true); - FluentResults.Result GetLuaScriptsInfos(IReadOnlyList packages, bool onlySupportedResources = true); - - Task> GetAssembliesInfosAsync(IReadOnlyList packages, bool onlySupportedResources = true); - Task> GetConfigsInfosAsync(IReadOnlyList packages, bool onlySupportedResources = true); - Task> GetLuaScriptsInfosAsync(IReadOnlyList packages, bool onlySupportedResources = true); + public FluentResults.Result LoadPackageInfo(ContentPackage package); + public ImmutableArray<(ContentPackage Package, FluentResults.Result LoadSuccessResult)> LoadPackagesInfo(IReadOnlyCollection packages); + public ImmutableArray<(ContentPackage Package, FluentResults.Result ExecutionResult)> ExecuteLoadedPackages(); + public ImmutableArray<(ContentPackage Package, FluentResults.Result StopExecutionResult)> StopRunningPackages(); + public FluentResults.Result UnloadPackage(ContentPackage package); + public ImmutableArray<(ContentPackage Package, FluentResults.Result UnloadSuccessResult)> UnloadPackages(IReadOnlyCollection packages); + public ImmutableArray<(ContentPackage Package, FluentResults.Result UnloadSuccessResult)> UnloadAllPackages(); }