- Removed all MoveImmute situations.

- Added filtering and ordering functions for package resources.
This commit is contained in:
MapleWheels
2026-01-26 16:37:51 -05:00
committed by Maplewheels
parent 60ed549605
commit f9a467453a
8 changed files with 56 additions and 24 deletions
+1
View File
@@ -15,6 +15,7 @@ bld/
[Rr]eleaseMac/ [Rr]eleaseMac/
[Dd]ebugLinux/ [Dd]ebugLinux/
[Rr]eleaseLinux/ [Rr]eleaseLinux/
LocalMods/
*.o *.o
*/Barotrauma*/doc/ */Barotrauma*/doc/
@@ -187,7 +187,7 @@ public sealed partial class ConfigService : IConfigService
})); }));
} }
var taskResults = await Task.WhenAll(taskBuilder.MoveToImmutable()); var taskResults = await Task.WhenAll(taskBuilder.ToImmutable());
if (toProcessErrors.Count > 0) if (toProcessErrors.Count > 0)
{ {
@@ -354,7 +354,7 @@ public class EventService : IEventService, IEventAssemblyContextUnloading
public FluentResults.Result Reset() public FluentResults.Result Reset()
{ {
((IService)this).CheckDisposed(); IService.CheckDisposed(this);
_subscriptions.Clear(); _subscriptions.Clear();
_luaSubscriptionFactories.Clear(); _luaSubscriptionFactories.Clear();
_eventTypeNameAliases.Clear(); _eventTypeNameAliases.Clear();
@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Barotrauma.Extensions;
using Barotrauma.LuaCs.Data; using Barotrauma.LuaCs.Data;
using FluentResults; using FluentResults;
using Microsoft.Toolkit.Diagnostics; using Microsoft.Toolkit.Diagnostics;
@@ -212,13 +213,19 @@ public sealed class PackageManagementService : IPackageManagementService
// get loading order. Note: packages not in the execution order list will load first. // get loading order. Note: packages not in the execution order list will load first.
var loadingOrderedPackages = _loadedPackages.OrderBy(pkg => executionOrder.IndexOf(pkg.Key)) var loadingOrderedPackages = _loadedPackages.OrderBy(pkg => executionOrder.IndexOf(pkg.Key))
.ToImmutableArray(); .ToImmutableArray();
var loadOrderByPackage = loadingOrderedPackages.Select(p => p.Key).ToImmutableArray();
var toLoadPackagesIndents = loadingOrderedPackages
.SelectMany(p => p.Key.AltNames.Union(new []{ p.Key.Name }).ToIdentifiers())
.ToImmutableHashSet();
// NOTE: Config/Settings are instanced in LoadPackages() // NOTE: Config/Settings are instanced in LoadPackages()
//lua scripts //lua scripts
var luaScripts = loadingOrderedPackages var luaScripts = SelectCompatible(loadingOrderedPackages
.SelectMany(pkg => pkg.Value.LuaScripts.OrderBy(scr => scr.LoadPriority)) .SelectMany(pkg => pkg.Value.LuaScripts)
.ToImmutableArray(); .ToImmutableArray(), toLoadPackagesIndents, loadOrderByPackage);
if (!luaScripts.IsDefaultOrEmpty) if (!luaScripts.IsDefaultOrEmpty)
{ {
result.WithReasons(_luaScriptManagementService.ExecuteLoadedScripts(luaScripts).Reasons); result.WithReasons(_luaScriptManagementService.ExecuteLoadedScripts(luaScripts).Reasons);
@@ -226,9 +233,10 @@ public sealed class PackageManagementService : IPackageManagementService
if (_runConfig.IsCsEnabled) if (_runConfig.IsCsEnabled)
{ {
var plugins = var plugins = SelectCompatible(loadingOrderedPackages
loadingOrderedPackages.SelectMany(pkg => pkg.Value.Assemblies.OrderBy(scr => scr.LoadPriority)) .SelectMany(pkg => pkg.Value.Assemblies)
.ToImmutableArray(); .ToImmutableArray(), toLoadPackagesIndents, loadOrderByPackage);
if (!plugins.IsDefaultOrEmpty) if (!plugins.IsDefaultOrEmpty)
{ {
result.WithReasons(_pluginManagementService.LoadAssemblyResources(plugins).Reasons); result.WithReasons(_pluginManagementService.LoadAssemblyResources(plugins).Reasons);
@@ -239,10 +247,29 @@ public sealed class PackageManagementService : IPackageManagementService
{ {
_runningPackages[package.Key] = package.Value; _runningPackages[package.Key] = package.Value;
} }
if (result.IsFailed)
{
_logger.LogResults(result);
}
return result; return result;
} }
private static ImmutableArray<T> SelectCompatible<T>(ImmutableArray<T> resources, ImmutableHashSet<Identifier> enabledPackagesIdents, ImmutableArray<ContentPackage> loadingOrder)
where T : IBaseResourceInfo
{
return resources
.Where(r => r.SupportedPlatforms.HasFlag(ModUtils.Environment.CurrentPlatform))
.Where(r => r.SupportedTargets.HasFlag(ModUtils.Environment.CurrentTarget))
.Where(r => !r.Optional || (
(r.RequiredPackages.IsDefaultOrEmpty || enabledPackagesIdents.Intersect(r.RequiredPackages).Any())
&& (r.IncompatiblePackages.IsDefaultOrEmpty || enabledPackagesIdents.Intersect(r.IncompatiblePackages).None()))
).OrderBy(r => loadingOrder.IndexOf(r.OwnerPackage))
.ThenBy(r => r.LoadPriority)
.ToImmutableArray();
}
public FluentResults.Result SyncLoadedPackagesList(ImmutableArray<ContentPackage> packages) public FluentResults.Result SyncLoadedPackagesList(ImmutableArray<ContentPackage> packages)
{ {
if (packages.IsDefaultOrEmpty) if (packages.IsDefaultOrEmpty)
@@ -15,6 +15,7 @@ using FluentResults;
using FluentResults.LuaCs; using FluentResults.LuaCs;
using ImpromptuInterface.Build; using ImpromptuInterface.Build;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.Toolkit.Diagnostics;
using OneOf; using OneOf;
namespace Barotrauma.LuaCs.Services; namespace Barotrauma.LuaCs.Services;
@@ -118,8 +119,8 @@ public class PluginManagementService : IPluginManagementService, IAssemblyManage
{ {
if (includeDefaultContext) if (includeDefaultContext)
{ {
var type = Type.GetType(typeName, false); var type = Type.GetType(typeName, false, false);
if (type is not null) if (type is not null && (includeInterfaces || !type.IsInterface))
{ {
if (isByRefType) if (isByRefType)
{ {
@@ -132,7 +133,7 @@ public class PluginManagementService : IPluginManagementService, IAssemblyManage
foreach (var ass in AssemblyLoadContext.All.SelectMany(alc => alc.Assemblies)) foreach (var ass in AssemblyLoadContext.All.SelectMany(alc => alc.Assemblies))
{ {
if (ass.GetType(typeName, false) is not { } type) if (ass.GetType(typeName, false, false) is not {} type || (!includeInterfaces && type.IsInterface))
{ {
continue; continue;
} }
@@ -143,11 +144,14 @@ public class PluginManagementService : IPluginManagementService, IAssemblyManage
return null; return null;
} }
public FluentResults.Result LoadAssemblyResources(ImmutableArray<IAssemblyResourceInfo> resource) public FluentResults.Result LoadAssemblyResources(ImmutableArray<IAssemblyResourceInfo> resources)
{ {
#if DEBUG IService.CheckDisposed(this);
return FluentResults.Result.Fail($"{nameof(LoadAssemblyResources)}: Plugin loading not currently implemented."); if (resources.IsDefaultOrEmpty)
#endif {
ThrowHelper.ThrowArgumentNullException($"{nameof(LoadAssemblyResources)}: The resources list is empty!");
}
throw new NotImplementedException(); throw new NotImplementedException();
} }
@@ -114,7 +114,7 @@ public sealed class SettingsFileParserService :
} }
} }
return FluentResults.Result.Ok(parsedInfo.MoveToImmutable()); return FluentResults.Result.Ok(parsedInfo.ToImmutable());
// Helpers // Helpers
@@ -196,12 +196,12 @@ public sealed class SettingsFileParserService :
{ {
InternalName = profileName, InternalName = profileName,
OwnerPackage = res.path.ContentPackage, OwnerPackage = res.path.ContentPackage,
ProfileValues = profileValuesBuilder.MoveToImmutable() ProfileValues = profileValuesBuilder.ToImmutable()
}); });
} }
} }
return parsedInfo.MoveToImmutable(); return parsedInfo.ToImmutable();
FluentResults.Result ReturnFail(string msg) FluentResults.Result ReturnFail(string msg)
{ {
@@ -251,7 +251,7 @@ public class StorageService : IStorageService
{ {
builder.Add((path, LoadPackageData(path, dataLoader))); builder.Add((path, LoadPackageData(path, dataLoader)));
} }
return builder.MoveToImmutable(); return builder.ToImmutable();
} }
public ImmutableArray<(ContentPath, Result<XDocument>)> LoadPackageXmlFiles(ImmutableArray<ContentPath> filePaths) public ImmutableArray<(ContentPath, Result<XDocument>)> LoadPackageXmlFiles(ImmutableArray<ContentPath> filePaths)
@@ -317,7 +317,7 @@ public class StorageService : IStorageService
{ {
builder.Add((path, await LoadPackageDataAsync(path, dataLoader))); builder.Add((path, await LoadPackageDataAsync(path, dataLoader)));
} }
return builder.MoveToImmutable(); return builder.ToImmutable();
} }
public async Task<ImmutableArray<(ContentPath, Result<XDocument>)>> LoadPackageXmlFilesAsync(ImmutableArray<ContentPath> filePaths) public async Task<ImmutableArray<(ContentPath, Result<XDocument>)>> LoadPackageXmlFilesAsync(ImmutableArray<ContentPath> filePaths)
@@ -35,9 +35,9 @@ public interface IPluginManagementService : IReusableService
/// <summary> /// <summary>
/// Loads the provided assembly resources in the order of their dependencies and intra-mod priority load order. /// Loads the provided assembly resources in the order of their dependencies and intra-mod priority load order.
/// </summary> /// </summary>
/// <param name="resource"></param> /// <param name="resources"></param>
/// <returns>Success/Failure and list of failed resources, if any.</returns> /// <returns>Success/Failure and list of failed resources, if any.</returns>
FluentResults.Result LoadAssemblyResources(ImmutableArray<IAssemblyResourceInfo> resource); FluentResults.Result LoadAssemblyResources(ImmutableArray<IAssemblyResourceInfo> resources);
/// <summary> /// <summary>
/// Creates instances of the given type and provides Property Injection and instance reference caching. Disposes of /// Creates instances of the given type and provides Property Injection and instance reference caching. Disposes of