Skip to content

Commit

Permalink
Don't output invalid CK3 accessory gene templates in DNA (#1682) #patch
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Jan 2, 2024
1 parent 75b52fb commit 758480d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 40 deletions.
43 changes: 26 additions & 17 deletions ImperatorToCK3/CK3/Characters/DNAFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ public DNA GenerateDNA(Imperator.Characters.Character irCharacter, PortraitData
} else {
Logger.Warn("hairstyles not found in CK3 special accessory genes!");
}

var clothesGeneValue = MatchAccessoryGeneValueByTemplate(irCharacter, irPortraitData, "clothes");
if (clothesGeneValue is not null) {
accessoryDNAValues.Add("clothes", clothesGeneValue.Value);

if (ck3GenesDB.SpecialAccessoryGenes.TryGetValue("clothes", out var ck3ClothesGene)) {
var clothesGeneValue = MatchAccessoryGeneValueByTemplate(irCharacter, irPortraitData, "clothes", ck3ClothesGene);
if (clothesGeneValue is not null) {
accessoryDNAValues.Add(ck3ClothesGene.Id, clothesGeneValue.Value);
}
} else {
Logger.Warn("clothes not found in CK3 special accessory genes!");
}

// Convert eye accessories.
Expand Down Expand Up @@ -331,9 +335,9 @@ AccessoryGene ck3Gene
if (!irPortraitData.AccessoryGenesDict.TryGetValue(irGeneName, out var geneInfo)) {
return null;
}

var objectMappings = accessoryGeneMapper.ObjectToObjectMappings[irGeneName];
if (!objectMappings.TryGetValue(geneInfo.ObjectName, out var convertedSetEntry)) {
var convertedSetEntry = accessoryGeneMapper.GetObjectFromObject(irGeneName, geneInfo.ObjectName);
if (convertedSetEntry is null) {
Logger.Warn($"No object mappings found for {geneInfo.ObjectName} in gene {irGeneName}!");
return null;
}
Expand All @@ -343,7 +347,8 @@ AccessoryGene ck3Gene
Logger.Warn($"No template found for {convertedSetEntry} in CK3 gene {ck3Gene.Id}!");
return null;
}
if (!objectMappings.TryGetValue(geneInfo.ObjectNameRecessive, out var convertedSetEntryRecessive)) {
var convertedSetEntryRecessive = accessoryGeneMapper.GetObjectFromObject(irGeneName, geneInfo.ObjectNameRecessive);
if (convertedSetEntryRecessive is null) {
Logger.Warn($"No object mappings found for {geneInfo.ObjectNameRecessive} in gene {irGeneName}!");
return null;
}
Expand Down Expand Up @@ -374,26 +379,30 @@ AccessoryGene ck3Gene
private DNAGeneValue? MatchAccessoryGeneValueByTemplate(
Imperator.Characters.Character irCharacter,
PortraitData irPortraitData,
string imperatorGeneName
string imperatorGeneName,
AccessoryGene ck3Gene
) {
if (!irPortraitData.AccessoryGenesDict.TryGetValue(imperatorGeneName, out var geneInfo)) {
return null;
}

var validCK3TemplateIds = ck3Gene.GeneTemplates
.Select(template => template.Id)
.ToList();

if (!accessoryGeneMapper.TemplateToTemplateMappings.TryGetValue(imperatorGeneName, out var templateMappings)) {
Logger.Warn($"No template-to-template mappings found for gene {imperatorGeneName}!");
return null;
}
if (!templateMappings.TryGetValue(geneInfo.GeneTemplate, out var ck3GeneTemplateName)) {
var ck3GeneTemplateName = accessoryGeneMapper.GetTemplateFromTemplate(imperatorGeneName, geneInfo.GeneTemplate, validCK3TemplateIds);
if (ck3GeneTemplateName is null) {
Logger.Warn($"No template-to-template mapping found for gene {imperatorGeneName} and template {geneInfo.GeneTemplate}!");
// Try to return first found template as a fallback.
if (templateMappings.Count > 0) {
ck3GeneTemplateName = templateMappings.First().Value;
var fallbackTemplateName = accessoryGeneMapper.GetFallbackTemplateForGene(imperatorGeneName, validCK3TemplateIds);
if (fallbackTemplateName is not null) {
ck3GeneTemplateName = fallbackTemplateName;
} else {
return null;
}
}
if (!templateMappings.TryGetValue(geneInfo.GeneTemplateRecessive, out var ck3GeneTemplateNameRecessive)) {
var ck3GeneTemplateNameRecessive = accessoryGeneMapper.GetTemplateFromTemplate(imperatorGeneName, geneInfo.GeneTemplateRecessive, validCK3TemplateIds);
if (ck3GeneTemplateNameRecessive is null) {
Logger.Warn($"No template-to-template mapping found for gene {imperatorGeneName} and recessive template {geneInfo.GeneTemplateRecessive}!");
// Use dominant template as a fallback.
ck3GeneTemplateNameRecessive = ck3GeneTemplateName;
Expand Down
4 changes: 3 additions & 1 deletion ImperatorToCK3/CK3/Titles/LandedTitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,9 @@ public void LoadCulturalNamesFromConfigurables() {

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, titleId) => {
var nameListToLocKeyDict = reader.GetAssignments();
var nameListToLocKeyDict = reader.GetAssignments()
.GroupBy(a => a.Key)
.ToDictionary(g => g.Key, g => g.Last().Value);
if (!TryGetValue(titleId, out var title)) {
return;
Expand Down
4 changes: 3 additions & 1 deletion ImperatorToCK3/CK3/Titles/Title.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,9 @@ private void RegisterKeys(Parser parser) {
parser.RegisterKeyword("de_jure_drift_disabled", reader => DeJureDriftDisabled = reader.GetBool());
parser.RegisterKeyword("can_be_named_after_dynasty", reader => CanBeNamedAfterDynasty = reader.GetBool());
parser.RegisterKeyword("male_names", reader => MaleNames = reader.GetStrings());
parser.RegisterKeyword("cultural_names", reader => CulturalNames = reader.GetAssignments());
parser.RegisterKeyword("cultural_names", reader => CulturalNames = reader.GetAssignments()
.GroupBy(a => a.Key)
.ToDictionary(g => g.Key, g => g.Last().Value));

parser.RegisterRegex(CommonRegexes.Catchall, (reader, token) => {
IgnoredTokens.Add(token);
Expand Down
8 changes: 3 additions & 5 deletions ImperatorToCK3/Imperator/Countries/CountryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ static Country() {
}
});
parser.RegisterKeyword("historical_regnal_numbers", reader => {
parsedCountry.HistoricalRegnalNumbers = new Dictionary<string, int>(
reader.GetAssignments().ToDictionary(
t => t.Key, t => int.Parse(t.Value)
)
);
parsedCountry.HistoricalRegnalNumbers = reader.GetAssignments()
.GroupBy(a => a.Key)
.ToDictionary(g => g.Key, g => int.Parse(g.Last().Value));
});
parser.RegisterKeyword("primary_culture", reader => parsedCountry.PrimaryCulture = reader.GetString());
parser.RegisterKeyword("religion", reader => parsedCountry.Religion = reader.GetString());
Expand Down
5 changes: 4 additions & 1 deletion ImperatorToCK3/Imperator/Religions/ReligionCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public void LoadHolySiteDatabase(BufferedReader deityManagerReader) {
var databaseParser = new Parser();
databaseParser.RegisterRegex(CommonRegexes.Integer, (reader, holySiteIdStr) => {
var holySiteId = ulong.Parse(holySiteIdStr);
if (reader.GetAssignments().TryGetValue("deity", out var deityIdWithQuotes)) {
var assignmentsDict = reader.GetAssignments()
.GroupBy(a => a.Key)
.ToDictionary(g => g.Key, g => g.Last().Value);
if (assignmentsDict.TryGetValue("deity", out var deityIdWithQuotes)) {
holySiteIdToDeityIdDict[holySiteId] = deityIdWithQuotes.RemQuotes();
} else {
Logger.Warn($"Holy site {holySiteId} has no deity!");
Expand Down
6 changes: 3 additions & 3 deletions ImperatorToCK3/ImperatorToCK3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</PackageReference>
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="NAudio.Vorbis" Version="1.5.0" />
<PackageReference Include="PGCG.commonItems" Version="10.1.0" />
<PackageReference Include="PGCG.commonItems" Version="11.0.0" />
<PackageReference Include="PGCG.commonItems.SourceGenerators" Version="1.0.5" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.2" />
</ItemGroup>
Expand Down Expand Up @@ -83,15 +83,15 @@

<Target Name="CopyCustomContent" AfterTargets="Build">
<Copy SourceFiles="@(FronterConfigurationFiles)" DestinationFolder="$(OutDir)/../Configuration" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(Configurables)" DestinationFolder="$(OutDir)/configurables/%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Retries="3" RetryDelayMilliseconds="300" />
<Copy SourceFiles="@(Configurables)" DestinationFolder="$(OutDir)\configurables\%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Retries="3" RetryDelayMilliseconds="300" />
<Copy SourceFiles="@(RakalyBinaries)" DestinationFolder="$(OutDir)\Resources\rakaly\%(RecursiveDir)" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(ConverterGlobals)" DestinationFolder="$(OutDir)/.." SkipUnchangedFiles="true" />
<Copy SourceFiles="@(BlankMod)" DestinationFolder="$(OutDir)\blankMod\%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Retries="3" RetryDelayMilliseconds="300" />
</Target>

<Target Name="CopyCustomContentOnPublish" AfterTargets="Publish">
<Copy SourceFiles="@(FronterConfigurationFiles)" DestinationFolder="$(PublishDir)/../Configuration" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(Configurables)" DestinationFolder="$(OutDir)/configurables/%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Retries="3" RetryDelayMilliseconds="300" />
<Copy SourceFiles="@(Configurables)" DestinationFolder="$(PublishDir)\configurables\%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Retries="3" RetryDelayMilliseconds="300" />
<Copy SourceFiles="@(RakalyBinaries)" DestinationFolder="$(PublishDir)\Resources\rakaly\%(RecursiveDir)" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(ConverterGlobals)" DestinationFolder="$(PublishDir)/.." SkipUnchangedFiles="true" />
<Copy SourceFiles="@(BlankMod)" DestinationFolder="$(PublishDir)\blankMod\%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Retries="3" RetryDelayMilliseconds="300" />
Expand Down
44 changes: 39 additions & 5 deletions ImperatorToCK3/Mappers/Gene/AccessoryGeneMapper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using commonItems;
using System.Collections.Generic;
using System.Linq;
using Assignment = System.Collections.Generic.KeyValuePair<string, string>;

namespace ImperatorToCK3.Mappers.Gene;

public class AccessoryGeneMapper {
public IDictionary<string, IDictionary<string, string>> ObjectToObjectMappings { get; } =
new Dictionary<string, IDictionary<string, string>>();

public IDictionary<string, IDictionary<string, string>> TemplateToTemplateMappings { get; } =
new Dictionary<string, IDictionary<string, string>>();
private Dictionary<string, IList<Assignment>> ObjectToObjectMappings { get; } = [];
private Dictionary<string, IList<Assignment>> TemplateToTemplateMappings { get; } = [];

public AccessoryGeneMapper(string mappingsFilePath) {
var objectToObjectMappingsParser = new Parser();
Expand All @@ -28,4 +27,39 @@ public AccessoryGeneMapper(string mappingsFilePath) {
parser.IgnoreAndLogUnregisteredItems();
parser.ParseFile(mappingsFilePath);
}

public string? GetObjectFromObject(string irGeneName, string irObjectName) {
if (!ObjectToObjectMappings.TryGetValue(irGeneName, out var mappings)) {
return null;
}

return mappings
.Where(mapping => mapping.Key == irObjectName)
.Select(mapping => mapping.Value)
.FirstOrDefault();
}

public string? GetTemplateFromTemplate(string irGeneName, string irTemplateName, IEnumerable<string> validCK3TemplateIds) {
if (!TemplateToTemplateMappings.TryGetValue(irGeneName, out var mappings)) {
Logger.Warn($"No template-to-template mappings found for gene {irGeneName}!");
return null;
}

return mappings
.Where(mapping => mapping.Key == irTemplateName)
.Select(mapping => mapping.Value)
.Intersect(validCK3TemplateIds)
.FirstOrDefault();
}

public string? GetFallbackTemplateForGene(string irGeneName, IEnumerable<string> validCK3TemplateIds) {
if (!TemplateToTemplateMappings.TryGetValue(irGeneName, out var mappings)) {
return null;
}

return mappings
.Select(mapping => mapping.Value)
.Intersect(validCK3TemplateIds)
.FirstOrDefault();
}
}
18 changes: 11 additions & 7 deletions ImperatorToCK3/Mappers/Gene/MorphGeneTemplateMapper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using commonItems;
using System.Collections.Generic;
using System.Linq;
using Assignment = System.Collections.Generic.KeyValuePair<string, string>;

namespace ImperatorToCK3.Mappers.Gene;
namespace ImperatorToCK3.Mappers.Gene;

public class MorphGeneTemplateMapper {
private readonly Dictionary<string, IDictionary<string, string>> templateMappings = new(); // <geneName, <irTemplate, ck3Template>>
private readonly Dictionary<string, IList<Assignment>> templateMappings = []; // <geneName, <irTemplate, ck3Template>>

public MorphGeneTemplateMapper(string mappingsFilePath) {
var parser = new Parser();
Expand All @@ -21,11 +23,13 @@ public MorphGeneTemplateMapper(string mappingsFilePath) {
return null;
}

if (templateMappingsForGene.TryGetValue(irTemplateName, out var ck3TemplateName)) {
return ck3TemplateName;
var ck3TemplateName = templateMappingsForGene
.Where(mapping => mapping.Key == irTemplateName)
.Select(mapping => mapping.Value)
.FirstOrDefault();
if (ck3TemplateName is null) {
Logger.Warn($"I:R template {irTemplateName} not found in morph gene template mappings!");
}

Logger.Warn($"I:R template {irTemplateName} not found in morph gene template mappings!");
return null;
return ck3TemplateName;
}
}

0 comments on commit 758480d

Please sign in to comment.