diff --git a/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterNameTests.cs b/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterNameTests.cs index 4d91f1910..ccd55e16b 100644 --- a/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterNameTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterNameTests.cs @@ -4,10 +4,18 @@ namespace ImperatorToCK3.UnitTests.Imperator.Characters { public class CharacterNameTests { - [Fact] public void CustomNameOverridesName() { + [Fact] + public void NameAndCustomNameDefaultToCorrectValues() { + var reader = new BufferedReader(string.Empty); + var characterName = new CharacterName(reader); + Assert.Equal(string.Empty, characterName.Name); + Assert.Null(characterName.CustomName); + } + [Fact] public void NameAndCustomNameCanBeRead() { var reader = new BufferedReader("name=a custom_name=b"); var characterName = new CharacterName(reader); - Assert.Equal("b", characterName.Name); + Assert.Equal("a", characterName.Name); + Assert.Equal("b", characterName.CustomName); } } } diff --git a/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs b/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs index 167f0f607..9e24d1114 100644 --- a/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/Characters/CharacterTests.cs @@ -26,7 +26,8 @@ public void FieldsCanBeSet() { "\tfamily=125" + "\twealth=\"420.5\"" + "\tfirst_name_loc = {\n" + - "\t\tname=\"Biggus Dickus\"\n" + + "\t\tname=\"Biggus_Dickus\"\n" + + "\t\tcustom_name=\"CUSTOM NAME\"\n" + "\t}\n" + "\tnickname = \"the Great\"\n" + "\tattributes={ martial=1 finesse=2 charisma=3 zeal=4 }" + @@ -78,7 +79,8 @@ public void FieldsCanBeSet() { Assert.Equal((ulong)124, character.Father.Key); Assert.Equal((ulong)125, character.Family.Key); Assert.Equal(420.5, character.Wealth); - Assert.Equal("Biggus Dickus", character.Name); + Assert.Equal("Biggus_Dickus", character.Name); + Assert.Equal("CUSTOM NAME", character.CustomName); Assert.Equal("the Great", character.Nickname); Assert.Equal(1, character.Attributes.Martial); Assert.Equal(2, character.Attributes.Finesse); @@ -107,6 +109,7 @@ public void FieldsDefaultToCorrectValues() { Assert.Equal((ulong)0, character.Family.Key); Assert.Equal(0, character.Wealth); Assert.Equal(string.Empty, character.Name); + Assert.Null(character.CustomName); Assert.Equal(string.Empty, character.Nickname); Assert.Equal(0, character.Attributes.Martial); Assert.Equal(0, character.Attributes.Finesse); diff --git a/ImperatorToCK3/CK3/Characters/Character.cs b/ImperatorToCK3/CK3/Characters/Character.cs index e55141678..75529fba4 100644 --- a/ImperatorToCK3/CK3/Characters/Character.cs +++ b/ImperatorToCK3/CK3/Characters/Character.cs @@ -57,7 +57,37 @@ Date dateOnConversion ImperatorCharacter = impCharacter; ImperatorCharacter.CK3Character = this; ID = "imperator" + ImperatorCharacter.ID.ToString(); - Name = ImperatorCharacter.Name; + + if (!string.IsNullOrEmpty(ImperatorCharacter.CustomName)) { + var loc = ImperatorCharacter.CustomName; + Name = "IMPTOCK3_CUSTOM_NAME_" + loc.Replace(' ', '_'); + Localizations.Add(Name, new LocBlock { + english = loc, + french = loc, + german = loc, + russian = loc, + simp_chinese = loc, + spanish = loc + }); + } else { + Name = ImperatorCharacter.Name; + if (!string.IsNullOrEmpty(Name)) { + var impNameLoc = localizationMapper.GetLocBlockForKey(Name); + if (impNameLoc is not null) { + Localizations.Add(Name, impNameLoc); + } else { // fallback: use unlocalized name as displayed name + Localizations.Add(Name, new LocBlock { + english = Name, + french = Name, + german = Name, + russian = Name, + simp_chinese = Name, + spanish = Name + }); + } + } + } + Female = ImperatorCharacter.Female; Age = ImperatorCharacter.Age; @@ -109,22 +139,6 @@ Date dateOnConversion Culture = match; } - if (!string.IsNullOrEmpty(Name)) { - var impNameLoc = localizationMapper.GetLocBlockForKey(Name); - if (impNameLoc is not null) { - Localizations.Add(Name, impNameLoc); - } else { // fallback: use unlocalized name as displayed name - Localizations.Add(Name, new LocBlock { - english = Name, - french = Name, - german = Name, - russian = Name, - simp_chinese = Name, - spanish = Name - }); - } - } - foreach (var impTrait in ImperatorCharacter.Traits) { var traitMatch = traitMapper.GetCK3TraitForImperatorTrait(impTrait); if (traitMatch is not null) { diff --git a/ImperatorToCK3/Imperator/Characters/Character.cs b/ImperatorToCK3/Imperator/Characters/Character.cs index 0891e6117..fc538ae0f 100644 --- a/ImperatorToCK3/Imperator/Characters/Character.cs +++ b/ImperatorToCK3/Imperator/Characters/Character.cs @@ -26,6 +26,7 @@ public string Culture { } public string Religion { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; + public string? CustomName { get; set; } public string Nickname { get; set; } = string.Empty; public ulong ProvinceID { get; private set; } = 0; public Date BirthDate { get; private set; } = new Date(1, 1, 1); @@ -85,7 +86,9 @@ public void AddYears(int years) { public static HashSet IgnoredTokens { get; } = new(); static Character() { parser.RegisterKeyword("first_name_loc", reader => { - parsedCharacter.Name = new CharacterName(reader).Name; + var characterName = new CharacterName(reader); + parsedCharacter.Name = characterName.Name; + parsedCharacter.CustomName = characterName.CustomName; }); parser.RegisterKeyword("country", reader => { parsedCharacter.Country = new(ParserHelpers.GetULong(reader), null); diff --git a/ImperatorToCK3/Imperator/Characters/CharacterName.cs b/ImperatorToCK3/Imperator/Characters/CharacterName.cs index 1f77acf17..60edff5f4 100644 --- a/ImperatorToCK3/Imperator/Characters/CharacterName.cs +++ b/ImperatorToCK3/Imperator/Characters/CharacterName.cs @@ -2,13 +2,8 @@ namespace ImperatorToCK3.Imperator.Characters { public class CharacterName : Parser { - private string name = string.Empty; - private string? customName; - public string Name { - get { - return customName ?? name; - } - } + public string Name { get; private set; } = string.Empty; // key for localization + public string? CustomName { get; private set; } // localized public CharacterName(BufferedReader reader) { RegisterKeys(); @@ -16,8 +11,8 @@ public CharacterName(BufferedReader reader) { ClearRegisteredRules(); } private void RegisterKeys() { - RegisterKeyword("name", reader => name = ParserHelpers.GetString(reader)); - RegisterKeyword("custom_name", reader => customName = ParserHelpers.GetString(reader)); + RegisterKeyword("name", reader => Name = ParserHelpers.GetString(reader)); + RegisterKeyword("custom_name", reader => CustomName = ParserHelpers.GetString(reader)); RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreAndLogItem); } }