From 64b00e2081b85a56d92e6fb9a5ecdd7ad080694b Mon Sep 17 00:00:00 2001 From: iht Date: Sun, 5 Jan 2025 17:21:51 +0100 Subject: [PATCH] Remove undefined traits from CK3 character history --- .../CK3/Characters/CharacterCollection.cs | 19 +++++++++++++++++++ ImperatorToCK3/CK3/World.cs | 3 +++ ImperatorToCK3/Mappers/Trait/TraitMapper.cs | 2 ++ 3 files changed, 24 insertions(+) diff --git a/ImperatorToCK3/CK3/Characters/CharacterCollection.cs b/ImperatorToCK3/CK3/Characters/CharacterCollection.cs index f51610ec8..72a178415 100644 --- a/ImperatorToCK3/CK3/Characters/CharacterCollection.cs +++ b/ImperatorToCK3/CK3/Characters/CharacterCollection.cs @@ -824,4 +824,23 @@ internal void ConvertImperatorCharacterDNA(DNAFactory dnaFactory) { } } } + + public void RemoveUndefinedTraits(TraitMapper traitMapper) { + Logger.Info("Removing undefined traits from CK3 character history..."); + + var definedTraits = traitMapper.ValidCK3TraitIDs.ToHashSet(); + + foreach (var character in this) { + if (character.FromImperator) { + continue; + } + + var traitsField = character.History.Fields["traits"]; + int removedCount = traitsField.InitialEntries.RemoveAll( + kvp => !definedTraits.Contains(kvp.Value.ToString() ?? string.Empty)); + if (removedCount > 0) { + Logger.Debug($"Removed {removedCount} undefined traits from character {character.Id}."); + } + } + } } \ No newline at end of file diff --git a/ImperatorToCK3/CK3/World.cs b/ImperatorToCK3/CK3/World.cs index 7d21ea6dd..a164e4237 100644 --- a/ImperatorToCK3/CK3/World.cs +++ b/ImperatorToCK3/CK3/World.cs @@ -243,6 +243,9 @@ public World(Imperator.World impWorld, Configuration config, Thread? irCoaExtrac Logger.Warn($"No base mapping found for I:R culture {cultureStr}!"); } } + }, + () => { // depends on TraitMapper and CK3 characters being loaded + Characters.RemoveUndefinedTraits(traitMapper); } ); diff --git a/ImperatorToCK3/Mappers/Trait/TraitMapper.cs b/ImperatorToCK3/Mappers/Trait/TraitMapper.cs index 263de70d4..a6fdeb2da 100644 --- a/ImperatorToCK3/Mappers/Trait/TraitMapper.cs +++ b/ImperatorToCK3/Mappers/Trait/TraitMapper.cs @@ -10,6 +10,8 @@ internal class TraitMapper { protected IDictionary ImperatorToCK3TraitMap = new Dictionary(); protected IdObjectCollection CK3Traits = []; + public IEnumerable ValidCK3TraitIDs => CK3Traits.Select(t => t.Id); + // TODO: add a method for logging all unmapped I:R traits public TraitMapper() { }