Skip to content

Commit 58cd974

Browse files
authored
Tweaks to the province outputting code (#2627) #patch
Fixes Rider's complaining about "Captured variable is disposed in outer scope". Hopefully also fixes Sentry event 20a029d390f146a4baebe76f504024a7.
1 parent c90e5e9 commit 58cd974

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

.github/config/changelog-config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
"title": "## 🐛 Fixes",
99
"labels": ["fix"]
1010
},
11+
{
12+
"title": "## 🧹 Maintenance",
13+
"labels": ["maintenance", "chore", "refactor"]
14+
},
1115
{
1216
"title": "## 🧪 Tests",
1317
"labels": ["test"]

ImperatorToCK3/Outputter/ProvincesOutputter.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public static async Task OutputProvinces(
1717
Title.LandedTitles titles
1818
) {
1919
Logger.Info("Writing provinces...");
20-
20+
2121
FrozenSet<ulong> countyCapitalProvinceIds = titles.Counties.AsValueEnumerable()
2222
.Select(title => title.CapitalBaronyProvinceId)
2323
.Where(id => id is not null)
2424
.Select(id => id!.Value)
2525
.ToFrozenSet();
2626

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

3030
var deJureKingdoms = titles.GetDeJureKingdoms();
3131
Parallel.ForEach(deJureKingdoms, kingdom => {
@@ -36,60 +36,63 @@ Title.LandedTitles titles
3636
}
3737

3838
ProvinceOutputter.WriteProvince(sb, province, countyCapitalProvinceIds.Contains(province.Id));
39-
alreadyOutputtedProvinces.Add(province.Id);
39+
alreadyOutputtedProvIds.Add(province.Id);
4040
}
4141

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

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

5454
foreach (var province in provinces) {
55-
if (alreadyOutputtedProvinces.Contains(province.Id)) {
55+
if (alreadyOutputtedProvIds.Contains(province.Id)) {
5656
continue;
5757
}
5858

5959
if (duchy.DuchyContainsProvince(province.Id)) {
6060
sb.AppendLine($"# {duchy.Id}");
6161
ProvinceOutputter.WriteProvince(sb, province, countyCapitalProvinceIds.Contains(province.Id));
62-
alreadyOutputtedProvinces.Add(province.Id);
62+
alreadyOutputtedProvIds.Add(province.Id);
6363
}
6464
}
6565

6666
if (sb.Length > 0) {
67-
historyOutput.Write(sb.ToString());
67+
await historyOutput.WriteAsync(sb.ToString());
6868
}
69-
});
69+
}
7070
}
7171

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

78-
foreach (var province in provinces) {
79-
if (alreadyOutputtedProvinces.Contains(province.Id)) {
80-
continue;
81-
}
76+
Logger.IncrementProgress();
77+
}
8278

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

88-
await threadSafeWriter.WriteLineAsync($"{province.Id} = {baseProvId}");
89-
alreadyOutputtedProvinces.Add(province.Id);
84+
foreach (var province in provinces) {
85+
if (alreadyOutputtedProvinceIds.Contains(province.Id)) {
86+
continue;
9087
}
91-
}
9288

93-
Logger.IncrementProgress();
89+
var baseProvId = province.BaseProvinceId;
90+
if (baseProvId is null) {
91+
continue;
92+
}
93+
94+
await threadSafeWriter.WriteLineAsync($"{province.Id} = {baseProvId}");
95+
alreadyOutputtedProvinceIds.Add(province.Id);
96+
}
9497
}
9598
}

0 commit comments

Comments
 (0)