From 61b1b8d875060872b32895954554f98caef1739c Mon Sep 17 00:00:00 2001 From: IhateTrains Date: Sun, 5 Jan 2025 12:22:40 +0000 Subject: [PATCH] Fix exception if the `special_headgear_blindfold` gene is not found (#2397) #patch Sentry event ID: 2454b92b077e42088ba9b3e3e777f5b3 Issue seems to occur when EPE is included in the CK3 mods. --- ImperatorToCK3/CK3/Characters/DNAFactory.cs | 27 +++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/ImperatorToCK3/CK3/Characters/DNAFactory.cs b/ImperatorToCK3/CK3/Characters/DNAFactory.cs index fa714974f..3648748f3 100644 --- a/ImperatorToCK3/CK3/Characters/DNAFactory.cs +++ b/ImperatorToCK3/CK3/Characters/DNAFactory.cs @@ -156,6 +156,8 @@ internal DNA GenerateDNA(Imperator.Characters.Character irCharacter, PortraitDat } // Convert eye accessories. + const string blindfoldGeneId = "special_headgear_blindfold"; + const string blindfoldTemplateId = "blindfold"; var irEyeAccessoryGeneTemplateName = irPortraitData.AccessoryGenesDict["eye_accessory"].GeneTemplate; switch (irEyeAccessoryGeneTemplateName) { case "normal_eyes": @@ -171,11 +173,18 @@ internal DNA GenerateDNA(Imperator.Characters.Character irCharacter, PortraitDat break; case "blindfold_1": // TODO: check if this is correctly added to portrait modifiers if needed - var blindfoldTemplate = ck3GenesDB.SpecialAccessoryGenes["special_headgear_blindfold"] - .GeneTemplates["blindfold"]; + if (!ck3GenesDB.SpecialAccessoryGenes.TryGetValue(blindfoldGeneId, out var blindfoldGene)) { + Logger.Warn($"{blindfoldGeneId} not found in CK3 special accessory genes!"); + break; + } + if (!blindfoldGene.GeneTemplates.TryGetValue(blindfoldTemplateId, out var blindfoldTemplate)) { + Logger.Warn($"{blindfoldTemplateId} not found in CK3 special accessory genes!"); + break; + } + if (blindfoldTemplate.AgeSexWeightBlocks.TryGetValue(irCharacter.AgeSex, out WeightBlock? blindfoldWeightBlock)) { var blindfoldObjectName = blindfoldWeightBlock.GetMatchingObject(1) ?? blindfoldWeightBlock.ObjectNames.Last(); - accessoryDNAValues["special_headgear_blindfold"] = new(blindfoldTemplate.Id, blindfoldObjectName, blindfoldWeightBlock); + accessoryDNAValues[blindfoldGeneId] = new(blindfoldTemplate.Id, blindfoldObjectName, blindfoldWeightBlock); } break; @@ -206,12 +215,14 @@ internal DNA GenerateDNA(Imperator.Characters.Character irCharacter, PortraitDat var blindEyesObjectName = blindEyesWeighBlock.GetMatchingObject(1) ?? blindEyesWeighBlock.ObjectNames.Last(); accessoryDNAValues["eye_accessory"] = new(blindEyesTemplate.Id, blindEyesObjectName, blindEyesWeighBlock); // TODO: check if this is correctly added to portrait modifiers if needed } - - var blindfoldTemplate = ck3GenesDB.SpecialAccessoryGenes["special_headgear_blindfold"] - .GeneTemplates["blindfold"]; - if (blindfoldTemplate.AgeSexWeightBlocks.TryGetValue(irCharacter.AgeSex, out WeightBlock? blindfoldWeighBlock)) { + + if (!ck3GenesDB.SpecialAccessoryGenes.TryGetValue(blindfoldGeneId, out var blindfoldGene)) { + Logger.Warn($"{blindfoldGeneId} not found in CK3 special accessory genes!"); + } else if (!blindfoldGene.GeneTemplates.TryGetValue(blindfoldTemplateId, out var blindfoldTemplate)) { + Logger.Warn($"{blindfoldTemplateId} not found in CK3 special accessory genes!"); + } else if (blindfoldTemplate.AgeSexWeightBlocks.TryGetValue(irCharacter.AgeSex, out WeightBlock? blindfoldWeighBlock)) { var blindfoldObjectName = blindfoldWeighBlock.GetMatchingObject(1) ?? blindfoldWeighBlock.ObjectNames.Last(); - accessoryDNAValues["special_headgear_blindfold"] = new(blindfoldTemplate.Id, blindfoldObjectName, blindfoldWeighBlock); // TODO: check if this is correctly added to portrait modifiers if needed + accessoryDNAValues[blindfoldGeneId] = new(blindfoldTemplate.Id, blindfoldObjectName, blindfoldWeighBlock); // TODO: check if this is correctly added to portrait modifiers if needed } } else if (irCharacter.Traits.Contains("one_eyed")) { var eyePatchTemplate = ck3GenesDB.SpecialAccessoryGenes["special_headgear_eye_patch"]