From 735d6c8ddc76f8a92fba7de08a56a9c0bacf2035 Mon Sep 17 00:00:00 2001 From: bcssov Date: Tue, 8 Oct 2024 09:18:18 +0200 Subject: [PATCH] Stellaris CS is now respecting authority merging --- .../OverwrittenObjectSingleFileParser.cs | 39 ++++++++++++------- .../ModPatchCollectionService.cs | 15 +++++++ .../Registrations/GameRegistration.cs | 4 +- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/IronyModManager.Parser/Games/Stellaris/OverwrittenObjectSingleFileParser.cs b/src/IronyModManager.Parser/Games/Stellaris/OverwrittenObjectSingleFileParser.cs index 3c680bda..60dafe5b 100644 --- a/src/IronyModManager.Parser/Games/Stellaris/OverwrittenObjectSingleFileParser.cs +++ b/src/IronyModManager.Parser/Games/Stellaris/OverwrittenObjectSingleFileParser.cs @@ -1,11 +1,10 @@ - -// *********************************************************************** +// *********************************************************************** // Assembly : IronyModManager.Parser // Author : Mario // Created : 10-25-2021 // // Last Modified By : Mario -// Last Modified On : 06-25-2023 +// Last Modified On : 10-08-2024 // *********************************************************************** // // Mario @@ -25,7 +24,6 @@ namespace IronyModManager.Parser.Games.Stellaris { - /// /// Class OverwrittenParser. /// Implements the @@ -37,19 +35,22 @@ public class OverwrittenObjectSingleFileParser : BaseParser, IGameParser { #region Fields + /// + /// The allow duplicate code block + /// + private static readonly Dictionary allowDuplicateCodeBlock = new() { { Common.Constants.Stellaris.GovernmentAuthorities.ToLowerInvariant(), ["advanced_authority_swap"] } }; + /// /// The merge type ids /// - private static readonly Dictionary allowDuplicateIds = new() { { Common.Constants.Stellaris.Ethics.ToLowerInvariant(), new string[] { "ethic_categories" } } }; + private static readonly Dictionary allowDuplicateIds = new() { { Common.Constants.Stellaris.Ethics.ToLowerInvariant(), ["ethic_categories"] } }; /// /// The starts with checks /// - private static readonly string[] directoryNames = new string[] + private static readonly string[] directoryNames = { - Common.Constants.Stellaris.Ethics, Common.Constants.Stellaris.StarbaseModules, - Common.Constants.Stellaris.ShipSizes, Common.Constants.Stellaris.StrategicResources, - Common.Constants.Stellaris.GovernmentAuthorities + Common.Constants.Stellaris.Ethics, Common.Constants.Stellaris.StarbaseModules, Common.Constants.Stellaris.ShipSizes, Common.Constants.Stellaris.StrategicResources, Common.Constants.Stellaris.GovernmentAuthorities }; #endregion Fields @@ -111,17 +112,29 @@ public override IEnumerable Parse(ParserArgs args) if (item.ValueType == ValueType.Object) { item.ValueType = ValueType.OverwrittenObjectSingleFile; - if (allowDuplicateIds.ContainsKey(Path.GetDirectoryName(args.File))) + if (allowDuplicateIds.ContainsKey(Path.GetDirectoryName(args.File)!)) { - var items = allowDuplicateIds[Path.GetDirectoryName(args.File)]; + var items = allowDuplicateIds[Path.GetDirectoryName(args.File)!]; if (items.Any(p => p.Equals(item.Id, StringComparison.OrdinalIgnoreCase))) { item.AllowDuplicate = true; } } + else if (allowDuplicateCodeBlock.ContainsKey(Path.GetDirectoryName(args.File)!)) + { + var items = allowDuplicateCodeBlock[Path.GetDirectoryName(args.File)!]; + foreach (var block in items) + { + if (item.Code.Contains(block, StringComparison.OrdinalIgnoreCase)) + { + item.AllowDuplicate = true; + } + } + } } } } + return results; } @@ -132,8 +145,8 @@ public override IEnumerable Parse(ParserArgs args) /// true if this instance [can parse starts with] the specified arguments; otherwise, false. protected virtual bool CanParseStartsWith(CanParseArgs args) { - var directoryName = System.IO.Path.GetDirectoryName(args.File); - return directoryNames.Any(s => directoryName.Equals(s, StringComparison.OrdinalIgnoreCase)); + var directoryName = Path.GetDirectoryName(args.File); + return directoryNames.Any(s => directoryName != null && directoryName.Equals(s, StringComparison.OrdinalIgnoreCase)); } #endregion Methods diff --git a/src/IronyModManager.Services/ModPatchCollectionService.cs b/src/IronyModManager.Services/ModPatchCollectionService.cs index 7cfcf218..6c3d1ff5 100644 --- a/src/IronyModManager.Services/ModPatchCollectionService.cs +++ b/src/IronyModManager.Services/ModPatchCollectionService.cs @@ -1144,6 +1144,20 @@ public virtual async Task GetModObjectsAsync(IGame game, IE await messageBus.PublishAsync(new ModDefinitionInvalidReplaceEvent(99.9)); var indexed = DIResolver.Get(); await indexed.InitMapAsync(prunedDefinitions); + + // Mark all labeled duplicate defs with same id as allowed duplicates + if (prunedDefinitions.Count > 0) + { + foreach (var item in prunedDefinitions.Where(p => p.AllowDuplicate)) + { + var duplicateDefs = await indexed.GetByTypeAndIdAsync(item.TypeAndId); + foreach (var def in duplicateDefs) + { + def.AllowDuplicate = true; + } + } + } + await messageBus.PublishAsync(new ModDefinitionInvalidReplaceEvent(100)); return indexed; } @@ -1275,6 +1289,7 @@ async void processedSearchItemHandler(object sender, ProcessedArgs args) previousProgress = perc; } + // ReSharper disable once DisposeOnUsingVariable mutex.Dispose(); } diff --git a/src/IronyModManager.Services/Registrations/GameRegistration.cs b/src/IronyModManager.Services/Registrations/GameRegistration.cs index dbae64d5..5d0bc7b9 100644 --- a/src/IronyModManager.Services/Registrations/GameRegistration.cs +++ b/src/IronyModManager.Services/Registrations/GameRegistration.cs @@ -4,7 +4,7 @@ // Created : 02-12-2020 // // Last Modified By : Mario -// Last Modified On : 09-10-2024 +// Last Modified On : 10-08-2024 // *********************************************************************** // // Mario @@ -44,7 +44,7 @@ public class GameRegistration : PostStartup /// /// The stellaris cache version /// - private const int StellarisCacheVersion = 23; + private const int StellarisCacheVersion = 24; /// /// The path resolver