Skip to content

Commit

Permalink
Stellaris CS is now respecting authority merging
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Oct 8, 2024
1 parent f2b8f0e commit 735d6c8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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
// ***********************************************************************
// <copyright file="OverwrittenObjectSingleFileParser.cs" company="Mario">
// Mario
Expand All @@ -25,7 +24,6 @@

namespace IronyModManager.Parser.Games.Stellaris
{

/// <summary>
/// Class OverwrittenParser.
/// Implements the <see cref="IronyModManager.Parser.Common.Parsers.BaseParser" />
Expand All @@ -37,19 +35,22 @@ public class OverwrittenObjectSingleFileParser : BaseParser, IGameParser
{
#region Fields

/// <summary>
/// The allow duplicate code block
/// </summary>
private static readonly Dictionary<string, string[]> allowDuplicateCodeBlock = new() { { Common.Constants.Stellaris.GovernmentAuthorities.ToLowerInvariant(), ["advanced_authority_swap"] } };

/// <summary>
/// The merge type ids
/// </summary>
private static readonly Dictionary<string, string[]> allowDuplicateIds = new() { { Common.Constants.Stellaris.Ethics.ToLowerInvariant(), new string[] { "ethic_categories" } } };
private static readonly Dictionary<string, string[]> allowDuplicateIds = new() { { Common.Constants.Stellaris.Ethics.ToLowerInvariant(), ["ethic_categories"] } };

/// <summary>
/// The starts with checks
/// </summary>
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
Expand Down Expand Up @@ -111,17 +112,29 @@ public override IEnumerable<IDefinition> 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;
}

Expand All @@ -132,8 +145,8 @@ public override IEnumerable<IDefinition> Parse(ParserArgs args)
/// <returns><c>true</c> if this instance [can parse starts with] the specified arguments; otherwise, <c>false</c>.</returns>
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
Expand Down
15 changes: 15 additions & 0 deletions src/IronyModManager.Services/ModPatchCollectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,20 @@ public virtual async Task<IIndexedDefinitions> GetModObjectsAsync(IGame game, IE
await messageBus.PublishAsync(new ModDefinitionInvalidReplaceEvent(99.9));
var indexed = DIResolver.Get<IIndexedDefinitions>();
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;
}
Expand Down Expand Up @@ -1275,6 +1289,7 @@ async void processedSearchItemHandler(object sender, ProcessedArgs args)
previousProgress = perc;
}

// ReSharper disable once DisposeOnUsingVariable
mutex.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 02-12-2020
//
// Last Modified By : Mario
// Last Modified On : 09-10-2024
// Last Modified On : 10-08-2024
// ***********************************************************************
// <copyright file="GameRegistration.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -44,7 +44,7 @@ public class GameRegistration : PostStartup
/// <summary>
/// The stellaris cache version
/// </summary>
private const int StellarisCacheVersion = 23;
private const int StellarisCacheVersion = 24;

/// <summary>
/// The path resolver
Expand Down

0 comments on commit 735d6c8

Please sign in to comment.