From e6b6fc34ecedffae35afa5ac7f99cbcb18ada482 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 20 Oct 2024 15:11:59 +0000 Subject: [PATCH 1/3] Bump PGCG.commonItems from 14.1.4 to 15.0.0 in /ImperatorToCK3 Bumps [PGCG.commonItems](https://github.com/ParadoxGameConverters/commonItems.NET) from 14.1.4 to 15.0.0. - [Commits](https://github.com/ParadoxGameConverters/commonItems.NET/compare/14.1.4...15.0.0) --- updated-dependencies: - dependency-name: PGCG.commonItems dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- ImperatorToCK3/ImperatorToCK3.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ImperatorToCK3/ImperatorToCK3.csproj b/ImperatorToCK3/ImperatorToCK3.csproj index d95ef4a4d..81fd3f4a9 100644 --- a/ImperatorToCK3/ImperatorToCK3.csproj +++ b/ImperatorToCK3/ImperatorToCK3.csproj @@ -42,7 +42,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + From db956db9386f49aba687c2a86cc93d1c04ad1769 Mon Sep 17 00:00:00 2001 From: iht Date: Sun, 20 Oct 2024 17:31:18 +0200 Subject: [PATCH 2/3] Use Defines class from commonItems --- .../Imperator/Armies/UnitCollectionTests.cs | 2 +- .../Imperator/Armies/UnitTests.cs | 9 +++--- .../Imperator/DefinesTests.cs | 3 +- .../Imperator/Religions/DeityTests.cs | 2 +- .../Religions/ReligionCollectionTests.cs | 6 ++-- ImperatorToCK3/CK3/World.cs | 5 +++- ImperatorToCK3/Imperator/Armies/Unit.cs | 4 +-- .../Imperator/Armies/UnitCollection.cs | 2 +- ImperatorToCK3/Imperator/Defines.cs | 29 +++++-------------- ImperatorToCK3/Imperator/World.cs | 6 ++-- 10 files changed, 29 insertions(+), 39 deletions(-) diff --git a/ImperatorToCK3.UnitTests/Imperator/Armies/UnitCollectionTests.cs b/ImperatorToCK3.UnitTests/Imperator/Armies/UnitCollectionTests.cs index fa0892e49..83475e37f 100644 --- a/ImperatorToCK3.UnitTests/Imperator/Armies/UnitCollectionTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/Armies/UnitCollectionTests.cs @@ -26,7 +26,7 @@ public void UnitsCanBeLoaded() { var unitCollection = new UnitCollection(); var reader = new BufferedReader(@"1={} 2={} 3=none 1040187400={}"); - unitCollection.LoadUnits(reader, new LocDB("english"), new Defines()); + unitCollection.LoadUnits(reader, new LocDB("english"), new ImperatorDefines()); unitCollection .Select(unit => unit.Id) diff --git a/ImperatorToCK3.UnitTests/Imperator/Armies/UnitTests.cs b/ImperatorToCK3.UnitTests/Imperator/Armies/UnitTests.cs index c9cea19a9..a75d1bb74 100644 --- a/ImperatorToCK3.UnitTests/Imperator/Armies/UnitTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/Armies/UnitTests.cs @@ -32,7 +32,7 @@ public void LocalizedNameIsCorrectlyGenerated() { } } }"); - var unit = new Unit(1, unitReader, units, locDB, new Defines()); + var unit = new Unit(1, unitReader, units, locDB, new ImperatorDefines()); Assert.NotNull(unit.LocalizedName); Assert.Equal("Cohors V Legio Italia", unit.LocalizedName["english"]); @@ -40,9 +40,6 @@ public void LocalizedNameIsCorrectlyGenerated() { [Fact] public void UnitStrengthIsCorrectlyCalculated() { - var defines = new Defines(); - Assert.Equal(500, defines.CohortSize); - var subunitsReader = new BufferedReader(@" 1 = { strength = 0.5 type=""archers"" } # 250 men 2 = { strength = 1 type=""archers"" } # 500 men @@ -50,9 +47,11 @@ public void UnitStrengthIsCorrectlyCalculated() { var unitCollection = new UnitCollection(); unitCollection.LoadSubunits(subunitsReader); + + // Default cohort size is 500. var unitReader = new BufferedReader("cohort=1 cohort=2"); - var unit = new Unit(1, unitReader, unitCollection, new LocDB("english"), defines); + var unit = new Unit(1, unitReader, unitCollection, new LocDB("english"), new ImperatorDefines()); Assert.Equal(750, unit.MenPerUnitType["archers"]); // 250 + 500 } diff --git a/ImperatorToCK3.UnitTests/Imperator/DefinesTests.cs b/ImperatorToCK3.UnitTests/Imperator/DefinesTests.cs index d59b7aa3e..6121dcfae 100644 --- a/ImperatorToCK3.UnitTests/Imperator/DefinesTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/DefinesTests.cs @@ -1,3 +1,4 @@ +using commonItems; using commonItems.Mods; using ImperatorToCK3.Imperator; using Xunit; @@ -12,6 +13,6 @@ public class DefinesTests { public void CohortSizeCanBeRead() { var defines = new Defines(); defines.LoadDefines(imperatorModFS); - Assert.Equal(601, defines.CohortSize); + Assert.Equal(601, int.Parse(defines.GetValue("NUnit", "COHORT_SIZE")!)); } } \ No newline at end of file diff --git a/ImperatorToCK3.UnitTests/Imperator/Religions/DeityTests.cs b/ImperatorToCK3.UnitTests/Imperator/Religions/DeityTests.cs index 96ee0f2a1..74c0deeb4 100644 --- a/ImperatorToCK3.UnitTests/Imperator/Religions/DeityTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/Religions/DeityTests.cs @@ -21,7 +21,7 @@ public void ConstructedDeityHasCorrectId() { [Fact] public void ModifiersAreRead() { var scriptValues = new ScriptValueCollection(); - scriptValues.LoadScriptValues(imperatorModFS); + scriptValues.LoadScriptValues(imperatorModFS, new Defines()); var deityReader = new BufferedReader(@" passive_modifier = { diff --git a/ImperatorToCK3.UnitTests/Imperator/Religions/ReligionCollectionTests.cs b/ImperatorToCK3.UnitTests/Imperator/Religions/ReligionCollectionTests.cs index 4e2a6b433..44323e780 100644 --- a/ImperatorToCK3.UnitTests/Imperator/Religions/ReligionCollectionTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/Religions/ReligionCollectionTests.cs @@ -16,7 +16,7 @@ public class ReligionCollectionTests { [Fact] public void ReligionsAreLoadedFromGameAndMods() { var scriptValues = new ScriptValueCollection(); - scriptValues.LoadScriptValues(imperatorModFS); + scriptValues.LoadScriptValues(imperatorModFS, new Defines()); var religions = new ReligionCollection(scriptValues); religions.LoadReligions(imperatorModFS); @@ -47,7 +47,7 @@ public void ReligionsAreLoadedFromGameAndMods() { [Fact] public void DeitiesAreLoadedFromGameAndMods() { var scriptValues = new ScriptValueCollection(); - scriptValues.LoadScriptValues(imperatorModFS); + scriptValues.LoadScriptValues(imperatorModFS, new Defines()); var religions = new ReligionCollection(scriptValues); religions.LoadDeities(imperatorModFS); @@ -70,7 +70,7 @@ public void DeitiesAreLoadedFromGameAndMods() { [Fact] public void GetDeityForHolySiteIdReturnsCorrectDeityOrNullWhenIdIsNotFoundOrWhenDeityIsNotFound() { var scriptValues = new ScriptValueCollection(); - scriptValues.LoadScriptValues(imperatorModFS); + scriptValues.LoadScriptValues(imperatorModFS, new Defines()); var religions = new ReligionCollection(scriptValues); religions.LoadDeities(imperatorModFS); diff --git a/ImperatorToCK3/CK3/World.cs b/ImperatorToCK3/CK3/World.cs index 305460796..5f65d4440 100644 --- a/ImperatorToCK3/CK3/World.cs +++ b/ImperatorToCK3/CK3/World.cs @@ -111,6 +111,9 @@ public World(Imperator.World impWorld, Configuration config, Thread? irCoaExtrac // Include a fake mod pointing to blankMod in the output folder. LoadedMods.Add(new Mod("blankMod", outputModPath)); ModFS = new ModFilesystem(Path.Combine(config.CK3Path, "game"), LoadedMods); + + var ck3Defines = new Defines(); + ck3Defines.LoadDefines(ModFS); ColorFactory ck3ColorFactory = new(); // Now that we have the mod filesystem, we can initialize the localization database. @@ -120,7 +123,7 @@ public World(Imperator.World impWorld, Configuration config, Thread? irCoaExtrac LocDB.LoadLocFromModFS(ModFS, config.GetActiveCK3ModFlags()); Logger.IncrementProgress(); }, - () => ScriptValues.LoadScriptValues(ModFS), + () => ScriptValues.LoadScriptValues(ModFS, ck3Defines), () => { NamedColors.LoadNamedColors("common/named_colors", ModFS); ck3ColorFactory.AddNamedColorDict(NamedColors); diff --git a/ImperatorToCK3/Imperator/Armies/Unit.cs b/ImperatorToCK3/Imperator/Armies/Unit.cs index 4f832bdbd..e2a7656e8 100644 --- a/ImperatorToCK3/Imperator/Armies/Unit.cs +++ b/ImperatorToCK3/Imperator/Armies/Unit.cs @@ -19,7 +19,7 @@ public sealed class Unit : IIdentifiable { public LocBlock? LocalizedName { get; private set; } public IDictionary MenPerUnitType { get; } - public Unit(ulong id, BufferedReader legionReader, UnitCollection unitCollection, LocDB irLocDB, Defines defines) { + public Unit(ulong id, BufferedReader legionReader, UnitCollection unitCollection, LocDB irLocDB, ImperatorDefines defines) { Id = id; var parser = new Parser(); @@ -83,7 +83,7 @@ public Unit(ulong id, BufferedReader legionReader, UnitCollection unitCollection return nameLocBlock; } - private Dictionary GetMenPerUnitType(UnitCollection unitCollection, Defines defines) { + private Dictionary GetMenPerUnitType(UnitCollection unitCollection, ImperatorDefines defines) { var cohortSize = defines.CohortSize; return unitCollection.Subunits.Where(s => CohortIds.Contains(s.Id)) diff --git a/ImperatorToCK3/Imperator/Armies/UnitCollection.cs b/ImperatorToCK3/Imperator/Armies/UnitCollection.cs index ffec9089b..2fec0f4af 100644 --- a/ImperatorToCK3/Imperator/Armies/UnitCollection.cs +++ b/ImperatorToCK3/Imperator/Armies/UnitCollection.cs @@ -28,7 +28,7 @@ public void LoadSubunits(BufferedReader subunitsReader) { } Logger.IncrementProgress(); } - public void LoadUnits(BufferedReader unitsReader, LocDB irLocDB, Defines defines) { + public void LoadUnits(BufferedReader unitsReader, LocDB irLocDB, ImperatorDefines defines) { Logger.Info("Loading units..."); var parser = new Parser(); diff --git a/ImperatorToCK3/Imperator/Defines.cs b/ImperatorToCK3/Imperator/Defines.cs index 940e1a85e..05507b4f2 100644 --- a/ImperatorToCK3/Imperator/Defines.cs +++ b/ImperatorToCK3/Imperator/Defines.cs @@ -1,32 +1,19 @@ using commonItems; using commonItems.Mods; -using ImperatorToCK3.Helpers; -using System; -using System.Text.Json; - namespace ImperatorToCK3.Imperator; -public sealed class Defines { +public sealed class ImperatorDefines : Defines { public int CohortSize { get; private set; } = 500; - public void LoadDefines(ModFilesystem imperatorModFs) { + public new void LoadDefines(ModFilesystem modFS) { Logger.Info("Loading Imperator defines..."); - - var definesFiles = imperatorModFs.GetAllFilesInFolderRecursive("common/defines"); - foreach (var fileInfo in definesFiles) { - string jsonString = string.Empty; - try { - jsonString = RakalyCaller.GetJson(fileInfo.AbsolutePath); - var jsonRoot = JsonDocument.Parse(jsonString).RootElement; - - if (jsonRoot.TryGetProperty("NUnit", out var unitProp) && unitProp.TryGetProperty("COHORT_SIZE", out var cohortSizeProp)) { - CohortSize = cohortSizeProp.GetInt32(); - } - } catch (Exception e) { - Logger.Warn($"Failed to read defines from {fileInfo.AbsolutePath}:\n\tJSON string: {jsonString}\n\texception: {e}"); - } + base.LoadDefines(modFS); + + var cohortSizeStr = GetValue("NUnit", "COHORT_SIZE"); + if (cohortSizeStr is not null) { + CohortSize = int.Parse(cohortSizeStr); } - + Logger.IncrementProgress(); } } \ No newline at end of file diff --git a/ImperatorToCK3/Imperator/World.cs b/ImperatorToCK3/Imperator/World.cs index 58a635afa..e72445b6e 100644 --- a/ImperatorToCK3/Imperator/World.cs +++ b/ImperatorToCK3/Imperator/World.cs @@ -43,7 +43,7 @@ public partial class World { private readonly SortedSet dlcs = []; public IReadOnlySet GlobalFlags { get; private set; } = ImmutableHashSet.Empty; private readonly ScriptValueCollection scriptValues = new(); - public Defines Defines { get; } = new(); + public ImperatorDefines Defines { get; } = new(); public LocDB LocDB { get; } = new(ConverterGlobals.PrimaryLanguage, ConverterGlobals.SecondaryLanguages); public NamedColorCollection NamedColors { get; } = []; @@ -629,12 +629,12 @@ private void LoadModFilesystemDependentData() { Areas.LoadAreas(ModFS, Provinces); ImperatorRegionMapper = new ImperatorRegionMapper(Areas, MapData); }, - () => Defines.LoadDefines(ModFS), () => InventionsDB.LoadInventions(ModFS), () => Country.LoadGovernments(ModFS), ParseGenes, () => { - scriptValues.LoadScriptValues(ModFS); + Defines.LoadDefines(ModFS); + scriptValues.LoadScriptValues(ModFS, Defines); Logger.IncrementProgress(); }, () => { From 9f7d534632b517ccf9fdba9510533fc31afe14df Mon Sep 17 00:00:00 2001 From: iht Date: Sun, 20 Oct 2024 17:35:05 +0200 Subject: [PATCH 3/3] Rename DefinesTests to ImperatorDefinesTests --- .../{DefinesTests.cs => ImperatorDefinesTests.cs} | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) rename ImperatorToCK3.UnitTests/Imperator/{DefinesTests.cs => ImperatorDefinesTests.cs} (70%) diff --git a/ImperatorToCK3.UnitTests/Imperator/DefinesTests.cs b/ImperatorToCK3.UnitTests/Imperator/ImperatorDefinesTests.cs similarity index 70% rename from ImperatorToCK3.UnitTests/Imperator/DefinesTests.cs rename to ImperatorToCK3.UnitTests/Imperator/ImperatorDefinesTests.cs index 6121dcfae..48e4a546c 100644 --- a/ImperatorToCK3.UnitTests/Imperator/DefinesTests.cs +++ b/ImperatorToCK3.UnitTests/Imperator/ImperatorDefinesTests.cs @@ -1,18 +1,17 @@ -using commonItems; using commonItems.Mods; using ImperatorToCK3.Imperator; using Xunit; namespace ImperatorToCK3.UnitTests.Imperator; -public class DefinesTests { +public class ImperatorDefinesTests { private const string ImperatorRoot = "TestFiles/Imperator/game"; private readonly ModFilesystem imperatorModFS = new(ImperatorRoot, System.Array.Empty()); [Fact] public void CohortSizeCanBeRead() { - var defines = new Defines(); + var defines = new ImperatorDefines(); defines.LoadDefines(imperatorModFS); - Assert.Equal(601, int.Parse(defines.GetValue("NUnit", "COHORT_SIZE")!)); + Assert.Equal(601, defines.CohortSize); } } \ No newline at end of file