using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Threading.Tasks; using Barotrauma.LuaCs.Data; using Barotrauma.LuaCs.Services.Processing; using FluentResults; // ReSharper disable UseCollectionExpression namespace Barotrauma.LuaCs.Services; public partial class PackageManagementService : IPackageManagementService { public PackageManagementService( IConverterServiceAsync modConfigParserService, IProcessorService, IAssembliesResourcesInfo> assemblyInfoConverter, IProcessorService, IConfigsResourcesInfo> configsInfoConverter, IProcessorService, IConfigProfilesResourcesInfo> configProfilesConverter, IProcessorService, ILocalizationsResourcesInfo> localizationsConverter, IProcessorService, ILuaScriptsResourcesInfo> luaScriptsConverter, IPackageInfoLookupService packageInfoLookupService, Func, IStylesResourcesInfo> stylesInfoConverter) { _stylesInfoConverter = stylesInfoConverter; _modConfigParserService = modConfigParserService; _assemblyInfoConverter = assemblyInfoConverter; _configsInfoConverter = configsInfoConverter; _configProfilesConverter = configProfilesConverter; _localizationsConverter = localizationsConverter; _luaScriptsConverter = luaScriptsConverter; _packageInfoLookupService = packageInfoLookupService; } private readonly Func, IStylesResourcesInfo> _stylesInfoConverter; public ImmutableArray Styles => _modInfos.IsEmpty ? ImmutableArray.Empty : _modInfos.SelectMany(kvp => kvp.Value.Styles).ToImmutableArray(); public Result GetStylesInfos(ContentPackage package, bool onlySupportedResources = true) { ((IService)this).CheckDisposed(); if (package is null) return FluentResults.Result.Fail($"{nameof(GetStylesInfos)}: ContentPackage is null."); if (_modInfos.TryGetValue(package, out var result)) return FluentResults.Result.Ok(_stylesInfoConverter(onlySupportedResources? result.Styles.Where(r => (r.SupportedPlatforms & ModUtils.Environment.CurrentPlatform) > 0 && (r.SupportedTargets & ModUtils.Environment.CurrentTarget) > 0).ToImmutableArray() : result.Styles )); return FluentResults.Result.Fail( $"{nameof(GetStylesInfos)}: ContentPackage {package.Name} is not registered."); } public Result GetStylesInfos(IReadOnlyList packages, bool onlySupportedResources = true) { ((IService)this).CheckDisposed(); if (packages is null || packages.Count == 0) return FluentResults.Result.Fail($"{nameof(GetStylesInfos)}: ContentPackage list is null or empty."); var builder = ImmutableArray.CreateBuilder(); foreach (var package in packages) { if (_modInfos.TryGetValue(package, out var result) && result.Styles is { IsEmpty: false }) { builder.AddRange(onlySupportedResources? result.Styles.Where(r => (r.SupportedPlatforms & ModUtils.Environment.CurrentPlatform) > 0 && (r.SupportedTargets & ModUtils.Environment.CurrentTarget) > 0).ToImmutableArray() : result.Styles); } } return FluentResults.Result.Ok(_stylesInfoConverter(builder.MoveToImmutable())); } public async Task> GetStylesInfosAsync(IReadOnlyList packages, bool onlySupportedResources = true) { return await Task.Run(() => GetStylesInfos(packages, onlySupportedResources)); } }