Skip to content

Commit 48cda0b

Browse files
authored
Minor optimizations and cleanup (#2708)
Some of the issues fixed were found by Codacy.
1 parent ca83af1 commit 48cda0b

File tree

11 files changed

+86
-71
lines changed

11 files changed

+86
-71
lines changed

ImperatorToCK3.UnitTests/CK3/Cultures/CultureCollectionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static CultureCollectionTests() {
2929
public void ColorIsLoadedIfDefinedOrGeneratedIfMissing() {
3030
var cultures = new CultureCollection(colorFactory, pillars, ck3ModFlags);
3131
cultures.LoadNameLists(ck3ModFS);
32-
cultures.LoadCultures(ck3ModFS, new Configuration());
32+
cultures.LoadCultures(ck3ModFS);
3333

3434
var cultureWithColor = cultures["culture_with_color"];
3535
Assert.Equal(new Color(10, 20, 30), cultureWithColor.Color);
@@ -54,7 +54,7 @@ public void ConverterHeritageCanBeMergedIntoExistingHeritage() {
5454

5555
cultures.AddNameList(new NameList("name_list_albanian", new BufferedReader()));
5656
cultures.LoadConverterPillars("TestFiles/CK3/CultureCollectionTests/configurables/converter_pillars", ck3ModFlags);
57-
cultures.LoadConverterCultures("TestFiles/CK3/CultureCollectionTests/configurables/converter_cultures.txt", new Configuration());
57+
cultures.LoadConverterCultures("TestFiles/CK3/CultureCollectionTests/configurables/converter_cultures.txt");
5858

5959
Assert.Equal(2, cultures.Count);
6060
Assert.Equal("heritage_arberian", cultures["arberian"].Heritage.Id);
@@ -74,7 +74,7 @@ public void ConverterLanguageCanBeMergedIntoExistingLanguage() {
7474

7575
cultures.AddNameList(new NameList("name_list_albanian", new BufferedReader()));
7676
cultures.LoadConverterPillars("TestFiles/CK3/CultureCollectionTests/configurables/converter_pillars", ck3ModFlags);
77-
cultures.LoadConverterCultures("TestFiles/CK3/CultureCollectionTests/configurables/converter_cultures.txt", new Configuration());
77+
cultures.LoadConverterCultures("TestFiles/CK3/CultureCollectionTests/configurables/converter_cultures.txt");
7878

7979
Assert.Equal(2, cultures.Count);
8080
Assert.Equal("language_illyrian", cultures["albanian"].Language.Id);

ImperatorToCK3.UnitTests/CK3/Dynasties/DynastyTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class DynastyTests {
3939
private static readonly MapData irMapData = new(IRModFS);
4040
private static readonly ImperatorRegionMapper IRRegionMapper;
4141
private static readonly CultureMapper CultureMapper;
42-
private static readonly TestCK3CultureCollection Cultures = new();
42+
private static readonly TestCK3CultureCollection Cultures = [];
4343

4444
static DynastyTests() {
4545
var irProvinces = new ImperatorToCK3.Imperator.Provinces.ProvinceCollection {new(1), new(2), new(3)};
@@ -66,7 +66,7 @@ private class CK3CharacterBuilder {
6666
private static readonly ModFilesystem ck3ModFS = new(CK3Root, Array.Empty<Mod>());
6767

6868
private ImperatorToCK3.Imperator.Characters.Character imperatorCharacter = new(0);
69-
private ImperatorToCK3.CK3.Characters.CharacterCollection characters = new();
69+
private readonly ImperatorToCK3.CK3.Characters.CharacterCollection characters = [];
7070
private ReligionMapper religionMapper = new(new ReligionCollection(new Title.LandedTitles()), IRRegionMapper, new CK3RegionMapper());
7171
private CultureMapper cultureMapper = new(IRRegionMapper, new CK3RegionMapper(), Cultures);
7272
private TraitMapper traitMapper = new("TestFiles/configurables/trait_map.txt", ck3ModFS);
@@ -144,7 +144,7 @@ public void IdAndNameAreProperlyConverted() {
144144
var family = Family.Parse(reader, 45);
145145

146146
var irLocDB = new LocDB("english");
147-
var dynasty = new Dynasty(family, characters, new CulturesDB(), CultureMapper, irLocDB, new TestCK3LocDB(), BookmarkDate);
147+
var dynasty = new Dynasty(family, characters, [], CultureMapper, irLocDB, new TestCK3LocDB(), BookmarkDate);
148148

149149
Assert.Equal("dynn_irtock3_45", dynasty.Id);
150150
Assert.Equal("dynn_irtock3_45", dynasty.Name);
@@ -174,7 +174,7 @@ public void LocalizationDefaultsToUnlocalizedKey() {
174174

175175
var irLocDB = new LocDB("english");
176176
var ck3LocDB = new TestCK3LocDB();
177-
var dynasty = new Dynasty(family, characters, new CulturesDB(), CultureMapper, irLocDB, ck3LocDB, BookmarkDate);
177+
var dynasty = new Dynasty(family, characters, [], CultureMapper, irLocDB, ck3LocDB, BookmarkDate);
178178

179179
Assert.Equal("cornelii", ck3LocDB.GetLocBlockForKey(dynasty.Id)!["english"]);
180180
}
@@ -208,15 +208,15 @@ public void CultureIsBasedOnFirstImperatorMember() {
208208
Cultures
209209
);
210210
var irLocDB = new LocDB("english");
211-
var ck3Member1 = new CK3CharacterBuilder()
211+
new CK3CharacterBuilder()
212212
.WithCultureMapper(cultureMapper)
213213
.WithImperatorCharacter(member1)
214214
.Build();
215-
var ck3Member2 = new CK3CharacterBuilder()
215+
new CK3CharacterBuilder()
216216
.WithCultureMapper(cultureMapper)
217217
.WithImperatorCharacter(member2)
218218
.Build();
219-
var ck3Member3 = new CK3CharacterBuilder()
219+
new CK3CharacterBuilder()
220220
.WithCultureMapper(cultureMapper)
221221
.WithImperatorCharacter(member3)
222222
.Build();

ImperatorToCK3.UnitTests/CK3/Provinces/ProvincesTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void PrimaryImperatorProvinceIsProperlyDeterminedForCK3Province() {
113113
var religions = new ReligionCollection(titles);
114114
var religionMapper = new ReligionMapper(religions, irRegionMapper, ck3RegionMapper);
115115
var ck3MapData = new MapData(ck3ModFs);
116-
ck3MapData.ProvinceDefinitions.Add(new ProvinceDefinition(1) {});
116+
ck3MapData.ProvinceDefinitions.Add(new ProvinceDefinition(1));
117117
ck3Provinces.ImportImperatorProvinces(irWorld, ck3MapData, titles, cultureMapper, religionMapper, provinceMapper, conversionDate, config);
118118

119119
var targetProvince = ck3Provinces[1];

ImperatorToCK3/CK3/Characters/DNAAccessoryGeneValue.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace ImperatorToCK3.CK3.Characters;
66

7-
public readonly struct DNAAccessoryGeneValue(
7+
internal readonly struct DNAAccessoryGeneValue(
88
string templateName,
99
string objectName,
1010
WeightBlock weightBlock,
1111
string templateRecessiveName,
1212
string objectRecessiveName,
1313
WeightBlock weightBlockRecessive
14-
) {
14+
) : IEquatable<DNAAccessoryGeneValue> {
1515
public DNAAccessoryGeneValue(
1616
string templateName,
1717
string objectName,
@@ -57,12 +57,12 @@ public override string ToString() {
5757
return $"\"{TemplateName}\" {IntSliderValue} \"{TemplateRecessiveName}\" {IntSliderValueRecessive}";
5858
}
5959

60-
public bool Equals(DNAGeneValue other) {
60+
public bool Equals(DNAAccessoryGeneValue other) {
6161
return TemplateName == other.TemplateName && IntSliderValue == other.IntSliderValue && TemplateRecessiveName == other.TemplateRecessiveName && IntSliderValueRecessive == other.IntSliderValueRecessive;
6262
}
6363

6464
public override bool Equals(object? obj) {
65-
return obj is DNAGeneValue other && Equals(other);
65+
return obj is DNAAccessoryGeneValue other && Equals(other);
6666
}
6767

6868
public override int GetHashCode() {

ImperatorToCK3/CK3/Cultures/CultureCollection.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,34 @@ private void LoadInvalidatingCultureIds(OrderedDictionary<string, bool> ck3ModFl
9292
cultureIdsPerModFlagParser.ParseStream(reader);
9393
}
9494

95-
public void LoadCultures(ModFilesystem ck3ModFS, Configuration config) {
95+
public void LoadCultures(ModFilesystem ck3ModFS) {
9696
Logger.Info("Loading cultures...");
9797

98-
OrderedDictionary<string, CultureData> culturesData = new(); // Preserves order of insertion.
98+
OrderedDictionary<string, CultureData> culturesData = []; // Preserves order of insertion.
9999

100100
var parser = new Parser();
101101
parser.RegisterRegex(CommonRegexes.String, (reader, cultureId) => culturesData[cultureId] = LoadCultureData(reader));
102102
parser.IgnoreAndLogUnregisteredItems();
103103
parser.ParseGameFolder("common/culture/cultures", ck3ModFS, "txt", recursive: true, logFilePaths: true);
104104

105105
// After we've load all cultures data, we can validate it and create cultures.
106-
ValidateAndLoadCultures(culturesData, config);
106+
ValidateAndLoadCultures(culturesData);
107107

108108
ReplaceInvalidatedParents();
109109
}
110110

111-
public void LoadConverterCultures(string converterCulturesPath, Configuration config) {
111+
public void LoadConverterCultures(string converterCulturesPath) {
112112
Logger.Info("Loading converter cultures...");
113113

114-
OrderedDictionary<string, CultureData> culturesData = new(); // Preserves order of insertion.
114+
OrderedDictionary<string, CultureData> culturesData = []; // Preserves order of insertion.
115115

116116
var parser = new Parser();
117117
parser.RegisterRegex(CommonRegexes.String, (reader, cultureId) => culturesData[cultureId] = LoadCultureData(reader));
118118
parser.IgnoreAndLogUnregisteredItems();
119119
parser.ParseFile(converterCulturesPath);
120120

121121
// After we've load all cultures data, we can validate it and create cultures.
122-
ValidateAndLoadCultures(culturesData, config);
122+
ValidateAndLoadCultures(culturesData);
123123

124124
ReplaceInvalidatedParents();
125125
}
@@ -131,7 +131,7 @@ private CultureData LoadCultureData(BufferedReader cultureReader) {
131131
return cultureData;
132132
}
133133

134-
private void ValidateAndLoadCultures(OrderedDictionary<string, CultureData> culturesData, Configuration config) {
134+
private void ValidateAndLoadCultures(OrderedDictionary<string, CultureData> culturesData) {
135135
foreach (var (cultureId, data) in culturesData) {
136136
if (data.InvalidatingCultureIds.Count != 0) {
137137
bool isInvalidated = false;

ImperatorToCK3/CK3/Titles/LandedTitles.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ internal Title Add(
249249
LocDB irLocDB,
250250
CK3LocDB ck3LocDB,
251251
ProvinceMapper provinceMapper,
252-
CoaMapper coaMapper,
253252
DefiniteFormMapper definiteFormMapper,
254253
ImperatorRegionMapper imperatorRegionMapper,
255254
Configuration config
@@ -264,7 +263,6 @@ Configuration config
264263
irLocDB,
265264
ck3LocDB,
266265
provinceMapper,
267-
coaMapper,
268266
definiteFormMapper,
269267
imperatorRegionMapper,
270268
config
@@ -753,7 +751,6 @@ Configuration config
753751
irLocDB,
754752
ck3LocDB,
755753
provinceMapper,
756-
coaMapper,
757754
definiteFormMapper,
758755
imperatorRegionMapper,
759756
config

ImperatorToCK3/CK3/Titles/Title.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ IReadOnlyCollection<string> enabledCK3Dlcs
102102
/// <param name="irLocDB">I:R localization database.</param>
103103
/// <param name="ck3LocDB">CK3 localization database.</param>
104104
/// <param name="provinceMapper">Province mapper to be used.</param>
105-
/// <param name="coaMapper">CoA mapper to be used.</param>
106105
/// <param name="definiteFormMapper">Definite form mapper to be used.</param>
107106
/// <param name="imperatorRegionMapper">I:R region mapper to be used.</param>
108107
/// <param name="config">Configuration object.</param>
@@ -116,7 +115,6 @@ private Title(LandedTitles parentCollection,
116115
LocDB irLocDB,
117116
CK3LocDB ck3LocDB,
118117
ProvinceMapper provinceMapper,
119-
CoaMapper coaMapper,
120118
DefiniteFormMapper definiteFormMapper,
121119
ImperatorRegionMapper imperatorRegionMapper,
122120
Configuration config

ImperatorToCK3/CK3/World.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public World(Imperator.World impWorld, Configuration config, Thread? irCoaExtrac
135135
Cultures = new CultureCollection(ck3ColorFactory, CulturalPillars, ck3ModFlags);
136136
Cultures.LoadNameLists(ModFS);
137137
Cultures.LoadInnovationIds(ModFS);
138-
Cultures.LoadCultures(ModFS, config);
139-
Cultures.LoadConverterCultures("configurables/converter_cultures.txt", config);
138+
Cultures.LoadCultures(ModFS);
139+
Cultures.LoadConverterCultures("configurables/converter_cultures.txt");
140140
Cultures.WarnAboutCircularParents();
141141
Logger.IncrementProgress();
142142
},

ImperatorToCK3/Outputter/CoatOfArmsEmblemsOutputter.cs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,60 @@
22
using commonItems.Mods;
33
using ImageMagick;
44
using System;
5-
using System.Collections.Generic;
5+
using System.Collections.Frozen;
66
using System.IO;
77
using System.Threading.Tasks;
88

99
namespace ImperatorToCK3.Outputter;
1010

11-
public static class CoatOfArmsEmblemsOutputter {
11+
internal static class CoatOfArmsEmblemsOutputter {
12+
private static readonly FrozenSet<string> acceptedEmblemExtensions = new[] {"dds", "tga", "png"}.ToFrozenSet();
13+
1214
private static void ConvertColoredEmblems(string outputModPath, ModFilesystem imperatorModFS) {
1315
Logger.Info("Converting colored emblems...");
1416
const string coloredEmblemsFolder = "gfx/coat_of_arms/colored_emblems";
15-
var acceptedExtensions = new HashSet<string>{ "dds", "tga", "png" };
1617

1718
var emblemFiles = imperatorModFS.GetAllFilesInFolderRecursive(coloredEmblemsFolder);
1819
Parallel.ForEach(emblemFiles, fileInfo => {
19-
if (!acceptedExtensions.Contains(CommonFunctions.GetExtension(fileInfo.RelativePath))) {
20+
if (!acceptedEmblemExtensions.Contains(CommonFunctions.GetExtension(fileInfo.RelativePath))) {
2021
return;
2122
}
22-
CopyEmblem(fileInfo.AbsolutePath);
23+
CopyEmblem(outputModPath, fileInfo.AbsolutePath);
2324
});
2425
Logger.IncrementProgress();
25-
return;
26+
}
2627

27-
void CopyEmblem(string emblemFilePath) {
28-
var fileName = CommonFunctions.TrimPath(emblemFilePath);
28+
private static void CopyEmblem(string outputModPath, string emblemFilePath) {
29+
var fileName = CommonFunctions.TrimPath(emblemFilePath);
2930

30-
// Load an image.
31-
MagickImage image;
32-
try {
33-
image = new(emblemFilePath);
34-
image.Negate(channels: Channels.Red);
35-
} catch (Exception ex) {
36-
Logger.Debug($"Exception occurred while loading {emblemFilePath}: {ex}");
37-
Logger.Warn($"Failed to load colored emblem {fileName}. CoAs using this emblem will be broken.");
38-
return;
39-
}
40-
41-
// Write the image to new file.
42-
var outputPath = Path.Combine(outputModPath, "gfx/coat_of_arms/colored_emblems", fileName);
43-
try {
44-
image.Write(outputPath);
45-
} catch (Exception ex) {
46-
Logger.Debug($"Exception occurred while writing {outputPath}: {ex}");
47-
Logger.Warn($"Failed to write colored emblem {fileName}. CoAs using this emblem will be broken.");
48-
}
31+
// Load an image.
32+
MagickImage image;
33+
try {
34+
image = new(emblemFilePath);
35+
image.Negate(channels: Channels.Red);
36+
} catch (Exception ex) {
37+
Logger.Debug($"Exception occurred while loading {emblemFilePath}: {ex}");
38+
Logger.Warn($"Failed to load colored emblem {fileName}. CoAs using this emblem will be broken.");
39+
return;
40+
}
41+
42+
// Write the image to new file.
43+
var outputPath = Path.Combine(outputModPath, "gfx/coat_of_arms/colored_emblems", fileName);
44+
try {
45+
image.Write(outputPath);
46+
} catch (Exception ex) {
47+
Logger.Debug($"Exception occurred while writing {outputPath}: {ex}");
48+
Logger.Warn($"Failed to write colored emblem {fileName}. CoAs using this emblem will be broken.");
4949
}
5050
}
51-
51+
5252
private static void CopyTexturedEmblems(string outputModPath, ModFilesystem imperatorModFS) {
5353
Logger.Info("Copying textured emblems...");
5454
const string texturedEmblemsFolder = "gfx/coat_of_arms/textured_emblems";
55-
var acceptedExtensions = new HashSet<string>{ "dds", "tga", "png" };
5655

5756
var emblemFiles = imperatorModFS.GetAllFilesInFolderRecursive(texturedEmblemsFolder);
5857
foreach (var fileInfo in emblemFiles) {
59-
if (!acceptedExtensions.Contains(CommonFunctions.GetExtension(fileInfo.RelativePath))) {
58+
if (!acceptedEmblemExtensions.Contains(CommonFunctions.GetExtension(fileInfo.RelativePath))) {
6059
continue;
6160
}
6261

ImperatorToCK3/Outputter/FileTweaker.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@
99

1010
namespace ImperatorToCK3.Outputter;
1111

12-
internal readonly struct PartOfFileToModify(string textBefore, string textAfter, bool warnIfNotFound = true) {
13-
internal readonly string TextBefore = textBefore;
14-
internal readonly string TextAfter = textAfter;
15-
internal readonly bool WarnIfNotFound = warnIfNotFound;
16-
17-
public void Deconstruct(out string textBefore, out string textAfter, out bool warnIfNotFound) {
18-
textBefore = TextBefore;
19-
textAfter = TextAfter;
20-
warnIfNotFound = WarnIfNotFound;
21-
}
22-
}
23-
2412
internal enum LineEnding {
2513
CRLF,
2614
LF,

0 commit comments

Comments
 (0)