Skip to content

Commit

Permalink
Use Defines class from commonItems
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Oct 20, 2024
1 parent e6b6fc3 commit db956db
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions ImperatorToCK3.UnitTests/Imperator/Armies/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,26 @@ 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"]);
}

[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
");

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
}
Expand Down
3 changes: 2 additions & 1 deletion ImperatorToCK3.UnitTests/Imperator/DefinesTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using commonItems;
using commonItems.Mods;
using ImperatorToCK3.Imperator;
using Xunit;
Expand All @@ -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")!));
}
}
2 changes: 1 addition & 1 deletion ImperatorToCK3.UnitTests/Imperator/Religions/DeityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions ImperatorToCK3/Imperator/Armies/Unit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class Unit : IIdentifiable<ulong> {
public LocBlock? LocalizedName { get; private set; }
public IDictionary<string, int> 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();
Expand Down Expand Up @@ -83,7 +83,7 @@ public Unit(ulong id, BufferedReader legionReader, UnitCollection unitCollection
return nameLocBlock;
}

private Dictionary<string, int> GetMenPerUnitType(UnitCollection unitCollection, Defines defines) {
private Dictionary<string, int> GetMenPerUnitType(UnitCollection unitCollection, ImperatorDefines defines) {
var cohortSize = defines.CohortSize;

return unitCollection.Subunits.Where(s => CohortIds.Contains(s.Id))
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3/Imperator/Armies/UnitCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
29 changes: 8 additions & 21 deletions ImperatorToCK3/Imperator/Defines.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
6 changes: 3 additions & 3 deletions ImperatorToCK3/Imperator/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public partial class World {
private readonly SortedSet<string> dlcs = [];
public IReadOnlySet<string> GlobalFlags { get; private set; } = ImmutableHashSet<string>.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; } = [];
Expand Down Expand Up @@ -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();
},
() => {
Expand Down

0 comments on commit db956db

Please sign in to comment.