Skip to content

Commit

Permalink
Fix dangling references to removed CK3 spouse characters (#2141)
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Sep 5, 2024
1 parent a23b6eb commit 9eb870e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
33 changes: 32 additions & 1 deletion ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,38 @@ public string? DeathReason {
.WithLiteralField("if", "if")
.WithSimpleField("sexuality", "sexuality", null)
.Build();
public History History { get; } = historyFactory.GetHistory();

private readonly History history = historyFactory.GetHistory();
public History History {
get {
return history;
}
private init {
history = value;

// Build spouses cache.
var spousesHistoryField = history.Fields["spouses"];
foreach (var spouseId in spousesHistoryField.InitialEntries.Select(kvp => kvp.Value.ToString())) {
if (spouseId is null) {
continue;
}
if (characters.TryGetValue(spouseId, out var spouse)) {
spousesCache.Add(spouse);
}
}
foreach (var entriesList in spousesHistoryField.DateToEntriesDict.Values) {
foreach (var entry in entriesList) {
var spouseId = entry.Value.ToString();
if (spouseId is null) {
continue;
}
if (characters.TryGetValue(spouseId, out var spouse)) {
spousesCache.Add(spouse);
}
}
}
}
}

public Character(string id, BufferedReader reader, CharacterCollection characters) {
this.characters = characters;
Expand Down
1 change: 0 additions & 1 deletion ImperatorToCK3/Outputter/CharacterOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using commonItems.Serialization;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using Character = ImperatorToCK3.CK3.Characters.Character;

namespace ImperatorToCK3.Outputter;
Expand Down

0 comments on commit 9eb870e

Please sign in to comment.