diff --git a/.gitignore b/.gitignore index b861f38bc..8a9348a93 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ bld/ [Rr]eleaseMac/ [Dd]ebugLinux/ [Rr]eleaseLinux/ +LocalMods/ *.o */Barotrauma*/doc/ diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/ConfigService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/ConfigService.cs index 9ee3c2062..9aea4a8bc 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/ConfigService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/ConfigService.cs @@ -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) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/EventService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/EventService.cs index c3d362960..db42a1194 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/EventService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/EventService.cs @@ -354,7 +354,7 @@ public class EventService : IEventService, IEventAssemblyContextUnloading public FluentResults.Result Reset() { - ((IService)this).CheckDisposed(); + IService.CheckDisposed(this); _subscriptions.Clear(); _luaSubscriptionFactories.Clear(); _eventTypeNameAliases.Clear(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PackageManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PackageManagementService.cs index 126c85a24..286becd0d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PackageManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PackageManagementService.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Threading.Tasks; +using Barotrauma.Extensions; using Barotrauma.LuaCs.Data; using FluentResults; 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. var loadingOrderedPackages = _loadedPackages.OrderBy(pkg => executionOrder.IndexOf(pkg.Key)) .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() //lua scripts - var luaScripts = loadingOrderedPackages - .SelectMany(pkg => pkg.Value.LuaScripts.OrderBy(scr => scr.LoadPriority)) - .ToImmutableArray(); + var luaScripts = SelectCompatible(loadingOrderedPackages + .SelectMany(pkg => pkg.Value.LuaScripts) + .ToImmutableArray(), toLoadPackagesIndents, loadOrderByPackage); + if (!luaScripts.IsDefaultOrEmpty) { result.WithReasons(_luaScriptManagementService.ExecuteLoadedScripts(luaScripts).Reasons); @@ -226,9 +233,10 @@ public sealed class PackageManagementService : IPackageManagementService if (_runConfig.IsCsEnabled) { - var plugins = - loadingOrderedPackages.SelectMany(pkg => pkg.Value.Assemblies.OrderBy(scr => scr.LoadPriority)) - .ToImmutableArray(); + var plugins = SelectCompatible(loadingOrderedPackages + .SelectMany(pkg => pkg.Value.Assemblies) + .ToImmutableArray(), toLoadPackagesIndents, loadOrderByPackage); + if (!plugins.IsDefaultOrEmpty) { result.WithReasons(_pluginManagementService.LoadAssemblyResources(plugins).Reasons); @@ -239,10 +247,29 @@ public sealed class PackageManagementService : IPackageManagementService { _runningPackages[package.Key] = package.Value; } - + + if (result.IsFailed) + { + _logger.LogResults(result); + } return result; } - + + private static ImmutableArray SelectCompatible(ImmutableArray resources, ImmutableHashSet enabledPackagesIdents, ImmutableArray 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 packages) { if (packages.IsDefaultOrEmpty) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PluginManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PluginManagementService.cs index 04a180a40..1ecdb3b8b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PluginManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/PluginManagementService.cs @@ -15,6 +15,7 @@ using FluentResults; using FluentResults.LuaCs; using ImpromptuInterface.Build; using Microsoft.CodeAnalysis; +using Microsoft.Toolkit.Diagnostics; using OneOf; namespace Barotrauma.LuaCs.Services; @@ -118,8 +119,8 @@ public class PluginManagementService : IPluginManagementService, IAssemblyManage { if (includeDefaultContext) { - var type = Type.GetType(typeName, false); - if (type is not null) + var type = Type.GetType(typeName, false, false); + if (type is not null && (includeInterfaces || !type.IsInterface)) { if (isByRefType) { @@ -132,7 +133,7 @@ public class PluginManagementService : IPluginManagementService, IAssemblyManage 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; } @@ -143,11 +144,14 @@ public class PluginManagementService : IPluginManagementService, IAssemblyManage return null; } - public FluentResults.Result LoadAssemblyResources(ImmutableArray resource) + public FluentResults.Result LoadAssemblyResources(ImmutableArray resources) { -#if DEBUG - return FluentResults.Result.Fail($"{nameof(LoadAssemblyResources)}: Plugin loading not currently implemented."); -#endif + IService.CheckDisposed(this); + if (resources.IsDefaultOrEmpty) + { + ThrowHelper.ThrowArgumentNullException($"{nameof(LoadAssemblyResources)}: The resources list is empty!"); + } + throw new NotImplementedException(); } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/Processing/SettingsFileParserService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/Processing/SettingsFileParserService.cs index 3a3f631d0..a44cb285e 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/Processing/SettingsFileParserService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/Processing/SettingsFileParserService.cs @@ -114,7 +114,7 @@ public sealed class SettingsFileParserService : } } - return FluentResults.Result.Ok(parsedInfo.MoveToImmutable()); + return FluentResults.Result.Ok(parsedInfo.ToImmutable()); // Helpers @@ -196,12 +196,12 @@ public sealed class SettingsFileParserService : { InternalName = profileName, OwnerPackage = res.path.ContentPackage, - ProfileValues = profileValuesBuilder.MoveToImmutable() + ProfileValues = profileValuesBuilder.ToImmutable() }); } } - return parsedInfo.MoveToImmutable(); + return parsedInfo.ToImmutable(); FluentResults.Result ReturnFail(string msg) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/StorageService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/StorageService.cs index 5af427abf..cf590839b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/StorageService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/StorageService.cs @@ -251,7 +251,7 @@ public class StorageService : IStorageService { builder.Add((path, LoadPackageData(path, dataLoader))); } - return builder.MoveToImmutable(); + return builder.ToImmutable(); } public ImmutableArray<(ContentPath, Result)> LoadPackageXmlFiles(ImmutableArray filePaths) @@ -317,7 +317,7 @@ public class StorageService : IStorageService { builder.Add((path, await LoadPackageDataAsync(path, dataLoader))); } - return builder.MoveToImmutable(); + return builder.ToImmutable(); } public async Task)>> LoadPackageXmlFilesAsync(ImmutableArray filePaths) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/IPluginManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/IPluginManagementService.cs index 81ebe8f0d..700418bfb 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/IPluginManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/_Interfaces/IPluginManagementService.cs @@ -35,9 +35,9 @@ public interface IPluginManagementService : IReusableService /// /// Loads the provided assembly resources in the order of their dependencies and intra-mod priority load order. /// - /// + /// /// Success/Failure and list of failed resources, if any. - FluentResults.Result LoadAssemblyResources(ImmutableArray resource); + FluentResults.Result LoadAssemblyResources(ImmutableArray resources); /// /// Creates instances of the given type and provides Property Injection and instance reference caching. Disposes of