diff --git a/ImperatorToCK3/CK3/Cultures/CultureCollection.cs b/ImperatorToCK3/CK3/Cultures/CultureCollection.cs index eced0e93a..96eacc0a3 100644 --- a/ImperatorToCK3/CK3/Cultures/CultureCollection.cs +++ b/ImperatorToCK3/CK3/Cultures/CultureCollection.cs @@ -86,6 +86,8 @@ private void LoadInvalidatingCultureIds(ICollection ck3ModFlags, Buffere } public void LoadCultures(ModFilesystem ck3ModFS) { + Logger.Info("Loading cultures..."); + var parser = new Parser(); parser.RegisterRegex(CommonRegexes.String, (reader, cultureId) => LoadCulture(cultureId, reader)); parser.IgnoreAndLogUnregisteredItems(); @@ -95,6 +97,8 @@ public void LoadCultures(ModFilesystem ck3ModFS) { } public void LoadConverterCultures(string converterCulturesPath) { + Logger.Info("Loading converter cultures..."); + var parser = new Parser(); parser.RegisterRegex(CommonRegexes.String, (reader, cultureId) => LoadCulture(cultureId, reader)); parser.IgnoreAndLogUnregisteredItems(); @@ -149,6 +153,8 @@ private void ReplaceInvalidatedParents() { } public void LoadNameLists(ModFilesystem ck3ModFS) { + Logger.Info("Loading name lists..."); + var parser = new Parser(); parser.RegisterRegex(CommonRegexes.String, (reader, nameListId) => { NameListCollection.AddOrReplace(new NameList(nameListId, reader)); @@ -156,6 +162,18 @@ public void LoadNameLists(ModFilesystem ck3ModFS) { parser.IgnoreAndLogUnregisteredItems(); parser.ParseGameFolder("common/culture/name_lists", ck3ModFS, "txt", recursive: true, logFilePaths: true); } + + public void LoadInnovationIds(ModFilesystem ck3ModFS) { + Logger.Info("Loading CK3 innovation IDs..."); + + var parser = new Parser(); + parser.RegisterRegex(CommonRegexes.String, (reader, innovationId) => { + InnovationIds.Add(innovationId); + ParserHelpers.IgnoreItem(reader); + }); + parser.IgnoreAndLogUnregisteredItems(); + parser.ParseGameFolder("common/culture/innovations", ck3ModFS, "txt", recursive: true, logFilePaths: true); + } private string? GetCK3CultureIdForImperatorCountry(Country country, CultureMapper cultureMapper, ProvinceMapper provinceMapper) { var irCulture = country.PrimaryCulture ?? country.Monarch?.Culture; @@ -182,6 +200,7 @@ public void LoadNameLists(ModFilesystem ck3ModFS) { var innovationMapper = new InnovationMapper(); innovationMapper.LoadLinksAndBonuses("configurables/inventions_to_innovations_map.txt"); innovationMapper.LogUnmappedInventions(inventionsDB, irLocDB); + innovationMapper.RemoveMappingsWithInvalidInnovations(InnovationIds); // Group I:R countries by corresponding CK3 culture. var countriesByCulture = countries.Select(c => new { @@ -207,6 +226,7 @@ public void LoadNameLists(ModFilesystem ck3ModFS) { protected readonly PillarCollection PillarCollection; protected readonly IdObjectCollection NameListCollection = new(); + protected readonly HashSet InnovationIds = []; private CultureData cultureData = new(); private readonly Parser cultureDataParser = new(); diff --git a/ImperatorToCK3/CK3/World.cs b/ImperatorToCK3/CK3/World.cs index 60f9d1e35..9766d6d2a 100644 --- a/ImperatorToCK3/CK3/World.cs +++ b/ImperatorToCK3/CK3/World.cs @@ -115,11 +115,9 @@ public World(Imperator.World impWorld, Configuration config) { Logger.Info("Loading converter cultural pillars..."); CulturalPillars.LoadConverterPillars("configurables/cultural_pillars"); Cultures = new CultureCollection(ck3ColorFactory, CulturalPillars, config.GetCK3ModFlags()); - Logger.Info("Loading name lists..."); Cultures.LoadNameLists(ModFS); - Logger.Info("Loading cultures..."); + Cultures.LoadInnovationIds(ModFS); Cultures.LoadCultures(ModFS); - Logger.Info("Loading converter cultures..."); Cultures.LoadConverterCultures("configurables/converter_cultures.txt"); Logger.IncrementProgress(); diff --git a/ImperatorToCK3/Mappers/Technology/InnovationBonus.cs b/ImperatorToCK3/Mappers/Technology/InnovationBonus.cs index f6f4052f9..b6e34d2d4 100644 --- a/ImperatorToCK3/Mappers/Technology/InnovationBonus.cs +++ b/ImperatorToCK3/Mappers/Technology/InnovationBonus.cs @@ -6,22 +6,22 @@ namespace ImperatorToCK3.Mappers.Technology; public sealed class InnovationBonus { // TODO: add tests private readonly HashSet imperatorInventions = []; - private string? ck3Innovation; + public string? CK3InnovationId { get; private set; } public InnovationBonus(BufferedReader bonusReader) { var parser = new Parser(); parser.RegisterKeyword("ir", reader => imperatorInventions.Add(reader.GetString())); - parser.RegisterKeyword("ck3", reader => ck3Innovation = reader.GetString()); + parser.RegisterKeyword("ck3", reader => CK3InnovationId = reader.GetString()); parser.IgnoreAndLogUnregisteredItems(); parser.ParseStream(bonusReader); - if (ck3Innovation is null) { + if (CK3InnovationId is null) { Logger.Warn($"Innovation bonus from {string.Join(", ", imperatorInventions)} has no CK3 innovation."); } } public KeyValuePair? GetProgress(IEnumerable activeInventions) { - if (ck3Innovation is null) { + if (CK3InnovationId is null) { return null; } @@ -33,6 +33,6 @@ public InnovationBonus(BufferedReader bonusReader) { return null; } - return new(ck3Innovation, (ushort)progress); + return new(CK3InnovationId, (ushort)progress); } } \ No newline at end of file diff --git a/ImperatorToCK3/Mappers/Technology/InnovationLink.cs b/ImperatorToCK3/Mappers/Technology/InnovationLink.cs index bc44bca75..54443868a 100644 --- a/ImperatorToCK3/Mappers/Technology/InnovationLink.cs +++ b/ImperatorToCK3/Mappers/Technology/InnovationLink.cs @@ -4,21 +4,21 @@ namespace ImperatorToCK3.Mappers.Technology; public sealed class InnovationLink { // TODO: ADD TESTS private string? imperatorInvention; - private string? ck3Innovation; + public string? CK3InnovationId { get; private set; } public InnovationLink(BufferedReader linkReader) { var parser = new Parser(); parser.RegisterKeyword("ir", reader => imperatorInvention = reader.GetString()); - parser.RegisterKeyword("ck3", reader => ck3Innovation = reader.GetString()); + parser.RegisterKeyword("ck3", reader => CK3InnovationId = reader.GetString()); parser.IgnoreAndLogUnregisteredItems(); parser.ParseStream(linkReader); - if (ck3Innovation is null) { + if (CK3InnovationId is null) { Logger.Warn($"Innovation link from {imperatorInvention} has no CK3 innovation."); } if (imperatorInvention is null) { - Logger.Warn($"Innovation link to {ck3Innovation} has no Imperator invention."); + Logger.Warn($"Innovation link to {CK3InnovationId} has no Imperator invention."); } } @@ -26,6 +26,6 @@ public InnovationLink(BufferedReader linkReader) { if (imperatorInvention is null) { return null; } - return imperatorInvention == irInvention ? ck3Innovation : null; + return imperatorInvention == irInvention ? CK3InnovationId : null; } } \ No newline at end of file diff --git a/ImperatorToCK3/Mappers/Technology/InnovationMapper.cs b/ImperatorToCK3/Mappers/Technology/InnovationMapper.cs index a67a7b603..5bf8ab052 100644 --- a/ImperatorToCK3/Mappers/Technology/InnovationMapper.cs +++ b/ImperatorToCK3/Mappers/Technology/InnovationMapper.cs @@ -70,4 +70,15 @@ public void LogUnmappedInventions(InventionsDB inventionsDB, LocDB irLocDB) { } // TODO: ALSO LOG UNMAPPED CK3 MARTIAL AND CIVIC INNOVATIONS + + public void RemoveMappingsWithInvalidInnovations(ISet innovationIds) { + int removedCount = 0; + + removedCount += innovationLinks + .RemoveAll(link => link.CK3InnovationId is null || !innovationIds.Contains(link.CK3InnovationId)); + removedCount += innovationBonuses + .RemoveAll(bonus => bonus.CK3InnovationId is null || !innovationIds.Contains(bonus.CK3InnovationId)); + + Logger.Debug($"Removed {removedCount} technology mappings with invalid CK3 innovations."); + } }