Skip to content

Commit

Permalink
Prevent multiple building_gfx entries for a culture
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Jul 6, 2024
1 parent ef481f1 commit da9f83c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
19 changes: 13 additions & 6 deletions ImperatorToCK3/CK3/Cultures/Culture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.Text;
using System.Threading.Tasks;

namespace ImperatorToCK3.CK3.Cultures;
namespace ImperatorToCK3.CK3.Cultures;

public sealed class Culture : IIdentifiable<string>, IPDXSerializable {
public string Id { get; }
Expand All @@ -27,7 +27,7 @@ public sealed class Culture : IIdentifiable<string>, IPDXSerializable {

private readonly OrderedSet<string> innovationsFromImperator = [];
private readonly Dictionary<string, ushort> innovationProgressesFromImperator = [];

public Culture(string id, CultureData cultureData) {
Id = id;

Expand All @@ -38,8 +38,15 @@ public Culture(string id, CultureData cultureData) {
traditionIds = cultureData.TraditionIds;
nameLists = cultureData.NameLists;
attributes = new List<KeyValuePair<string, StringOfItem>>(cultureData.Attributes);

// Avoid multiple "building_gfx" attributes in the same culture by keeping only the last one.
var buildingGfxAttributes = attributes.Where(pair => pair.Key == "building_gfx").ToList();
if (buildingGfxAttributes.Count > 1) {
attributes.RemoveAll(pair => pair.Key == "building_gfx");
attributes.Add(buildingGfxAttributes[^1]);
}
}

public string Serialize(string indent, bool withBraces) {
var contentIndent = indent;
if (withBraces) {
Expand Down Expand Up @@ -75,7 +82,7 @@ public async Task OutputHistory(string outputModPath, Date date) {
// Nothing to output.
return;
}

var historyStrBuilder = new StringBuilder();
historyStrBuilder.AppendLine("# This file was generated by the IRToCK3 converter.");

Expand All @@ -90,15 +97,15 @@ public async Task OutputHistory(string outputModPath, Date date) {
historyStrBuilder.AppendLine("\t}");
}
historyStrBuilder.AppendLine("}");

var historyPath = Path.Combine(outputModPath, "history/cultures", Id + ".txt");
await using var historyWriter = File.CreateText(historyPath);
await historyWriter.WriteAsync(historyStrBuilder.ToString());
}

public void ImportInnovationsFromImperator(ISet<string> irInventions, InnovationMapper innovationMapper) {
innovationsFromImperator.AddRange(innovationMapper.GetInnovations(irInventions));

var progresses = innovationMapper.GetInnovationProgresses(irInventions);
foreach (var (innovationId, progress) in progresses) {
// If progress is 100 or more, the innovation can be considered discovered.
Expand Down
46 changes: 23 additions & 23 deletions ImperatorToCK3/CK3/Cultures/CultureCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using System.Collections.Generic;
using System.Linq;

namespace ImperatorToCK3.CK3.Cultures;
namespace ImperatorToCK3.CK3.Cultures;

public class CultureCollection : IdObjectCollection<string, Culture> {
public CultureCollection(ColorFactory colorFactory, PillarCollection pillarCollection, ICollection<string> ck3ModFlags) {
Expand Down Expand Up @@ -64,10 +64,10 @@ private void InitCultureDataParser(ColorFactory colorFactory, ICollection<string
});
cultureDataParser.IgnoreAndLogUnregisteredItems();
}

private void LoadInvalidatingCultureIds(ICollection<string> ck3ModFlags, BufferedReader reader) {
var cultureIdsPerModFlagParser = new Parser();

if (ck3ModFlags.Count == 0) {
cultureIdsPerModFlagParser.RegisterKeyword("vanilla", modCultureIdsReader => {
cultureData.InvalidatingCultureIds = modCultureIdsReader.GetStrings();
Expand All @@ -79,37 +79,37 @@ private void LoadInvalidatingCultureIds(ICollection<string> ck3ModFlags, Buffere
});
}
}

// Ignore culture IDs from mods that haven't been selected.
cultureIdsPerModFlagParser.IgnoreAndStoreUnregisteredItems(ignoredModFlags);
cultureIdsPerModFlagParser.ParseStream(reader);
}

public void LoadCultures(ModFilesystem ck3ModFS) {
Logger.Info("Loading cultures...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, cultureId) => LoadCulture(cultureId, reader));
parser.IgnoreAndLogUnregisteredItems();
parser.ParseGameFolder("common/culture/cultures", ck3ModFS, "txt", true, logFilePaths: true);
parser.ParseGameFolder("common/culture/cultures", ck3ModFS, "txt", recursive: true, logFilePaths: true);

ReplaceInvalidatedParents();
}

public void LoadConverterCultures(string converterCulturesPath) {
Logger.Info("Loading converter cultures...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, cultureId) => LoadCulture(cultureId, reader));
parser.IgnoreAndLogUnregisteredItems();
parser.ParseFile(converterCulturesPath);

ReplaceInvalidatedParents();
}

private void LoadCulture(string cultureId, BufferedReader cultureReader) {
cultureData = new CultureData();

cultureDataParser.ParseStream(cultureReader);

if (cultureData.InvalidatingCultureIds.Any()) {
Expand Down Expand Up @@ -154,18 +154,18 @@ private void ReplaceInvalidatedParents() {

public void LoadNameLists(ModFilesystem ck3ModFS) {
Logger.Info("Loading name lists...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, nameListId) => {
NameListCollection.AddOrReplace(new NameList(nameListId, reader));
});
parser.IgnoreAndLogUnregisteredItems();
parser.ParseGameFolder("common/culture/name_lists", ck3ModFS, "txt", recursive: true, logFilePaths: true);
}

public void LoadInnovationIds(ModFilesystem ck3ModFS) {
Logger.Info("Loading CK3 innovation IDs...");

var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, innovationId) => {
InnovationIds.Add(innovationId);
Expand All @@ -181,7 +181,7 @@ public void LoadInnovationIds(ModFilesystem ck3ModFS) {
if (country.CountryType == CountryType.real) {
Logger.Warn($"Failed to get primary or monarch culture for Imperator country {country.Tag}!");
}

return null;
}

Expand All @@ -190,25 +190,25 @@ public void LoadInnovationIds(ModFilesystem ck3ModFS) {
if (irProvinceId.HasValue) {
ck3ProvinceId = provinceMapper.GetCK3ProvinceNumbers(irProvinceId.Value).FirstOrDefault();
}

return cultureMapper.Match(irCulture, ck3ProvinceId, irProvinceId, country.HistoricalTag);
}

public void ImportTechnology(CountryCollection countries, CultureMapper cultureMapper, ProvinceMapper provinceMapper, InventionsDB inventionsDB, LocDB irLocDB) { // TODO: add tests for this

Check warning on line 197 in ImperatorToCK3/CK3/Cultures/CultureCollection.cs

View workflow job for this annotation

GitHub Actions / build (self-hosted, windows)

Check warning on line 197 in ImperatorToCK3/CK3/Cultures/CultureCollection.cs

View workflow job for this annotation

GitHub Actions / build (self-hosted, linux)

Check warning on line 197 in ImperatorToCK3/CK3/Cultures/CultureCollection.cs

View workflow job for this annotation

GitHub Actions / build (macos-14)

Check warning on line 197 in ImperatorToCK3/CK3/Cultures/CultureCollection.cs

View workflow job for this annotation

GitHub Actions / test_and_check_coverage

Check warning on line 197 in ImperatorToCK3/CK3/Cultures/CultureCollection.cs

View workflow job for this annotation

GitHub Actions / test (macos-14)

Check warning on line 197 in ImperatorToCK3/CK3/Cultures/CultureCollection.cs

View workflow job for this annotation

GitHub Actions / test (self-hosted, windows)

Logger.Info("Converting Imperator inventions to CK3 innovations...");

var innovationMapper = new InnovationMapper();
innovationMapper.LoadLinksAndBonuses("configurables/inventions_to_innovations_map.txt");
innovationMapper.LogUnmappedInventions(inventionsDB, irLocDB);
innovationMapper.RemoveMappingsWithInvalidInnovations(InnovationIds);

// Group I:R countries by corresponding CK3 culture.
var countriesByCulture = countries.Select(c => new {
Country = c, CK3CultureId = GetCK3CultureIdForImperatorCountry(c, cultureMapper, provinceMapper),
})
.Where(c => c.CK3CultureId is not null)
.GroupBy(c => c.CK3CultureId);

foreach (var grouping in countriesByCulture) {
if (!TryGetValue(grouping.Key!, out var culture)) {
Logger.Warn($"Can't import technology for culture {grouping.Key}: culture not found in CK3 cultures!");
Expand All @@ -223,11 +223,11 @@ public void LoadInnovationIds(ModFilesystem ck3ModFS) {
}

private readonly IDictionary<string, string> cultureReplacements = new Dictionary<string, string>(); // replaced culture -> replacing culture

protected readonly PillarCollection PillarCollection;
protected readonly IdObjectCollection<string, NameList> NameListCollection = new();
protected readonly IdObjectCollection<string, NameList> NameListCollection = [];
protected readonly HashSet<string> InnovationIds = [];

private CultureData cultureData = new();
private readonly Parser cultureDataParser = new();
private readonly IgnoredKeywordsSet ignoredModFlags = [];
Expand Down

0 comments on commit da9f83c

Please sign in to comment.