Skip to content

Commit

Permalink
Fix names converter from custom names (#304) #minor
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Sep 5, 2021
1 parent e7af714 commit b486d31
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }" +
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
48 changes: 31 additions & 17 deletions ImperatorToCK3/CK3/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion ImperatorToCK3/Imperator/Characters/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -85,7 +86,9 @@ public void AddYears(int years) {
public static HashSet<string> 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);
Expand Down
13 changes: 4 additions & 9 deletions ImperatorToCK3/Imperator/Characters/CharacterName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

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();
ParseStream(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);
}
}
Expand Down

0 comments on commit b486d31

Please sign in to comment.