Skip to content

Tweaks to the province outputting code #2627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/config/changelog-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"title": "## 🐛 Fixes",
"labels": ["fix"]
},
{
"title": "## 🧹 Maintenance",
"labels": ["maintenance", "chore", "refactor"]
},
{
"title": "## 🧪 Tests",
"labels": ["test"]
Expand Down
55 changes: 29 additions & 26 deletions ImperatorToCK3/Outputter/ProvincesOutputter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
Title.LandedTitles titles
) {
Logger.Info("Writing provinces...");

FrozenSet<ulong> countyCapitalProvinceIds = titles.Counties.AsValueEnumerable()
.Select(title => title.CapitalBaronyProvinceId)
.Where(id => id is not null)
.Select(id => id!.Value)
.ToFrozenSet();

// Output provinces to files named after their de jure kingdoms.
var alreadyOutputtedProvinces = new ConcurrentHashSet<ulong>();
var alreadyOutputtedProvIds = new ConcurrentHashSet<ulong>();

var deJureKingdoms = titles.GetDeJureKingdoms();
Parallel.ForEach(deJureKingdoms, kingdom => {
Expand All @@ -36,60 +36,63 @@
}

ProvinceOutputter.WriteProvince(sb, province, countyCapitalProvinceIds.Contains(province.Id));
alreadyOutputtedProvinces.Add(province.Id);
alreadyOutputtedProvIds.Add(province.Id);
}

var filePath = $"{outputModPath}/history/provinces/{kingdom.Id}.txt";
using var historyOutput = new StreamWriter(filePath);
historyOutput.Write(sb.ToString());
});

if (alreadyOutputtedProvinces.Count != provinces.Count) {
if (alreadyOutputtedProvIds.Count != provinces.Count) {
var filePath = $"{outputModPath}/history/provinces/onlyDeJureDuchy.txt";
await using var historyOutput = TextWriter.Synchronized(new StreamWriter(filePath));
var deJureDuchies = titles.GetDeJureDuchies();
Parallel.ForEach(deJureDuchies, duchy => {
foreach (var duchy in deJureDuchies) {
var sb = new System.Text.StringBuilder();

foreach (var province in provinces) {
if (alreadyOutputtedProvinces.Contains(province.Id)) {
if (alreadyOutputtedProvIds.Contains(province.Id)) {
continue;
}

if (duchy.DuchyContainsProvince(province.Id)) {
sb.AppendLine($"# {duchy.Id}");
ProvinceOutputter.WriteProvince(sb, province, countyCapitalProvinceIds.Contains(province.Id));
alreadyOutputtedProvinces.Add(province.Id);
alreadyOutputtedProvIds.Add(province.Id);
}
}

if (sb.Length > 0) {
historyOutput.Write(sb.ToString());
await historyOutput.WriteAsync(sb.ToString());
}
});
}
}

// Create province mapping file.
if (alreadyOutputtedProvinces.Count != provinces.Count) {
var mappingsPath = $"{outputModPath}/history/province_mapping/province_mapping.txt";
await using var mappingsWriter = FileHelper.OpenWriteWithRetries(mappingsPath, System.Text.Encoding.UTF8);
await using var threadSafeWriter = TextWriter.Synchronized(mappingsWriter);
if (alreadyOutputtedProvIds.Count != provinces.Count) {

Check notice on line 72 in ImperatorToCK3/Outputter/ProvincesOutputter.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

ImperatorToCK3/Outputter/ProvincesOutputter.cs#L72

This condition was just checked on line 47.
await CreateProvinceMappingFile(outputModPath, provinces, alreadyOutputtedProvIds);
}

foreach (var province in provinces) {
if (alreadyOutputtedProvinces.Contains(province.Id)) {
continue;
}
Logger.IncrementProgress();
}

var baseProvId = province.BaseProvinceId;
if (baseProvId is null) {
continue;
}
private static async Task CreateProvinceMappingFile(string outputModPath, ProvinceCollection provinces, ConcurrentHashSet<ulong> alreadyOutputtedProvinceIds) {
var mappingsPath = $"{outputModPath}/history/province_mapping/province_mapping.txt";
await using var mappingsWriter = FileHelper.OpenWriteWithRetries(mappingsPath, System.Text.Encoding.UTF8);
await using var threadSafeWriter = TextWriter.Synchronized(mappingsWriter);

await threadSafeWriter.WriteLineAsync($"{province.Id} = {baseProvId}");
alreadyOutputtedProvinces.Add(province.Id);
foreach (var province in provinces) {
if (alreadyOutputtedProvinceIds.Contains(province.Id)) {
continue;
}
}

Logger.IncrementProgress();
var baseProvId = province.BaseProvinceId;
if (baseProvId is null) {
continue;
}

await threadSafeWriter.WriteLineAsync($"{province.Id} = {baseProvId}");
alreadyOutputtedProvinceIds.Add(province.Id);
}
}
}
Loading