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(); }, () => {