Skip to content

Commit

Permalink
Move converter's cultural pillars to configurables and prevent them f…
Browse files Browse the repository at this point in the history
…rom overwriting the Fallen Eagle ones (#1680) #minor
  • Loading branch information
IhateTrains authored Jan 2, 2024
1 parent 38fc37a commit 75b52fb
Show file tree
Hide file tree
Showing 27 changed files with 352 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static CharacterCollectionTests() {
irRegionMapper = new ImperatorRegionMapper(areas);
irRegionMapper.LoadRegions(irModFS, colorFactory);

cultures = new CultureCollection(colorFactory, new PillarCollection());
cultures = new CultureCollection(colorFactory, new PillarCollection(colorFactory));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CultureCollectionTests {
private static readonly ColorFactory ColorFactory = new();

static CultureCollectionTests() {
Pillars = new PillarCollection { new("test_heritage", new BufferedReader("type = heritage")) };
Pillars = new PillarCollection(ColorFactory) { new("test_heritage", new PillarData { Type = "heritage" }) };
}

[Fact]
Expand Down
30 changes: 30 additions & 0 deletions ImperatorToCK3.UnitTests/CK3/Cultures/PillarCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using commonItems.Mods;
using ImperatorToCK3.CK3.Cultures;
using System;
using System.IO;
using Xunit;

namespace ImperatorToCK3.UnitTests.CK3.Cultures;

[Collection("Sequential")]
[CollectionDefinition("Sequential", DisableParallelization = true)]
public class PillarCollectionTests {
[Fact]
public void WarningIsLoggedWhenPillarDataIsMissingType() {
Directory.CreateDirectory("pillars_test");
Directory.CreateDirectory("pillars_test/common");
Directory.CreateDirectory("pillars_test/common/culture");
Directory.CreateDirectory("pillars_test/common/culture/pillars");
var pillarsFile = File.CreateText("pillars_test/common/culture/pillars/test_pillars.txt");
pillarsFile.WriteLine("pillar_without_type = {}");
pillarsFile.Close();

var modFS = new ModFilesystem("pillars_test", Array.Empty<Mod>());
var collection = new PillarCollection(new commonItems.Colors.ColorFactory());

var consoleOut = new StringWriter();
Console.SetOut(consoleOut);
collection.LoadPillars(modFS);
Assert.Contains("[WARN] Pillar pillar_without_type has no type defined! Skipping.", consoleOut.ToString());
}
}
12 changes: 1 addition & 11 deletions ImperatorToCK3.UnitTests/CK3/Cultures/PillarTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
using commonItems;
using ImperatorToCK3.CK3.Cultures;
using ImperatorToCK3.Exceptions;
using Xunit;

namespace ImperatorToCK3.UnitTests.CK3.Cultures;

public class PillarTests {
[Fact]
public void ExceptionIsThrownOnMissingType() {
var reader = new BufferedReader("= {}");
var exception = Assert.Throws<ConverterException>(() => new Pillar("test_pillar", reader));
Assert.Equal("Cultural pillar test_pillar has no type defined!", exception.Message);
}

[Fact]
public void PillarIsCorrectlyInitialized() {
var reader = new BufferedReader("= { type = test_type }");
var pillar = new Pillar("test_pillar", reader);
var pillar = new Pillar("test_pillar", new PillarData { Type = "test_type" });
Assert.Equal("test_pillar", pillar.Id);
Assert.Equal("test_type", pillar.Type);
}
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3.UnitTests/CK3/Provinces/ProvincesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void PrimaryImperatorProvinceIsProperlyDeterminedForCK3Province() {
areas.LoadAreas(irModFS, irWorld.Provinces);
var irRegionMapper = new ImperatorRegionMapper(areas);
irRegionMapper.LoadRegions(irModFS, new ColorFactory());
var cultures = new CultureCollection(new ColorFactory(), new PillarCollection());
var cultures = new CultureCollection(new ColorFactory(), new PillarCollection(new ColorFactory()));
var cultureMapper = new CultureMapper(irRegionMapper, ck3RegionMapper, cultures);
var religions = new ReligionCollection(titles);
var religionMapper = new ReligionMapper(religions, irRegionMapper, ck3RegionMapper);
Expand Down
5 changes: 3 additions & 2 deletions ImperatorToCK3.UnitTests/CK3/Titles/LandedTitlesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ static LandedTitlesTests() {
}

public LandedTitlesTests() {
PillarCollection pillars = new();
cultures = new CultureCollection(new ColorFactory(), pillars);
var colorFactory = new ColorFactory();
PillarCollection pillars = new(colorFactory);
cultures = new CultureCollection(colorFactory, pillars);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3.UnitTests/CK3/Titles/RulerTermTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void ImperatorRulerTermIsCorrectlyConverted() {
govMapper,
new LocDB("english"),
new ReligionMapper(ck3Religions, irRegionMapper, ck3RegionMapper),
new CultureMapper(irRegionMapper, ck3RegionMapper, new CultureCollection(new ColorFactory(), new PillarCollection())),
new CultureMapper(irRegionMapper, ck3RegionMapper, new CultureCollection(new ColorFactory(), new PillarCollection(new ColorFactory()))),
new NicknameMapper("TestFiles/configurables/nickname_map.txt"),
new ProvinceMapper(),
new Configuration()
Expand Down
2 changes: 1 addition & 1 deletion ImperatorToCK3.UnitTests/CK3/Titles/TitleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private class TitleBuilder {
private DefiniteFormMapper definiteFormMapper = new("TestFiles/configurables/definite_form_names.txt");

private readonly ReligionMapper religionMapper;
private readonly CultureMapper cultureMapper = new(irRegionMapper, new CK3RegionMapper(), new CultureCollection(new ColorFactory(), new PillarCollection()));
private readonly CultureMapper cultureMapper = new(irRegionMapper, new CK3RegionMapper(), new CultureCollection(new ColorFactory(), new PillarCollection(new ColorFactory())));
private readonly NicknameMapper nicknameMapper = new("TestFiles/configurables/nickname_map.txt");
private readonly Date ck3BookmarkDate = new(867, 1, 1);
private readonly CharacterCollection characters = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ public void GetNameLocBlockCorrectlyHandlesCompositeNames() {
provLocBlock["german"] = "Hormirzad";

var nameLocBlock = countryName.GetNameLocBlock(locDB, []);
Assert.Equal("Memphite Hormirzad", nameLocBlock!["english"]);
Assert.Equal("Memphit Hormirzad", nameLocBlock!["german"]);
Assert.NotNull(nameLocBlock);
Assert.Equal("Memphite Hormirzad", nameLocBlock["english"]);
Assert.Equal("Memphit Hormirzad", nameLocBlock["german"]);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class TagTitleMapperTests {
private static readonly ColorFactory ColorFactory = new();

static TagTitleMapperTests() {
var pillars = new PillarCollection();
var pillars = new PillarCollection(ColorFactory);
cultures = new CultureCollection(ColorFactory, pillars);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void CoaIsOutputtedForCountryWithFlagSet() {
new SuccessionLawMapper(),
new DefiniteFormMapper(),
new ReligionMapper(ck3Religions, irRegionMapper, ck3RegionMapper),
new CultureMapper(irRegionMapper, ck3RegionMapper, new CultureCollection(new ColorFactory(), new PillarCollection())),
new CultureMapper(irRegionMapper, ck3RegionMapper, new CultureCollection(new ColorFactory(), new PillarCollection(new ColorFactory()))),
new NicknameMapper(),
new CharacterCollection(),
new Date(400, 1, 1),
Expand Down Expand Up @@ -102,7 +102,7 @@ public void CoaIsNotOutputtedForCountryWithoutFlagSet() {
new SuccessionLawMapper(),
new DefiniteFormMapper(),
new ReligionMapper(ck3Religions, irRegionMapper, ck3RegionMapper),
new CultureMapper(irRegionMapper, ck3RegionMapper, new CultureCollection(new ColorFactory(), new PillarCollection())),
new CultureMapper(irRegionMapper, ck3RegionMapper, new CultureCollection(new ColorFactory(), new PillarCollection(new ColorFactory()))),
new NicknameMapper(),
new CharacterCollection(),
new Date(400, 1, 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public void DynastiesAreOutputted() {
AreaCollection areas = new();
ImperatorRegionMapper irRegionMapper = new(areas);
irRegionMapper.LoadRegions(irModFS, new ColorFactory());
CultureMapper cultureMapper = new(irRegionMapper, new CK3RegionMapper(), new CultureCollection(new ColorFactory(), new PillarCollection()));
var colorFactory = new ColorFactory();
CultureMapper cultureMapper = new(irRegionMapper, new CK3RegionMapper(), new CultureCollection(colorFactory, new PillarCollection(colorFactory)));

var characters = new CharacterCollection();
var dynasties = new DynastyCollection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
namespace ImperatorToCK3.UnitTests.TestHelpers;

public class TestCK3CultureCollection() : CultureCollection(new ColorFactory(), TestCulturalPillars) {
private static readonly PillarCollection TestCulturalPillars = new();
private static readonly PillarCollection TestCulturalPillars = new(new ColorFactory());

static TestCK3CultureCollection() {
TestCulturalPillars.Add(new Pillar("test_heritage", new BufferedReader("type = heritage")));
TestCulturalPillars.Add(new Pillar("test_heritage", new PillarData { Type = "heritage" }));
}

public void GenerateTestCulture(string id) {
Expand Down
47 changes: 35 additions & 12 deletions ImperatorToCK3/CK3/Cultures/Pillar.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
using commonItems;
using commonItems.Collections;
using ImperatorToCK3.Exceptions;
using commonItems.Colors;
using commonItems.Serialization;
using System.Collections.Generic;
using System.Text;

namespace ImperatorToCK3.CK3.Cultures;

public class Pillar : IIdentifiable<string> {
public class Pillar : IIdentifiable<string>, IPDXSerializable {
public string Id { get; }
public string Type { get; private set; }
public string Type { get; }
public Color? Color { get; }
private readonly List<KeyValuePair<string, StringOfItem>> attributes;
public IReadOnlyCollection<KeyValuePair<string, StringOfItem>> Attributes => attributes;

public Pillar(string id, BufferedReader pillarReader) {
public Pillar(string id, PillarData pillarData) {
Id = id;

var parser = new Parser();
parser.RegisterKeyword("type", reader => {
Type = reader.GetString();
});
parser.IgnoreUnregisteredItems();
parser.ParseStream(pillarReader);
Type = pillarData.Type!;
Color = pillarData.Color;
attributes = new List<KeyValuePair<string, StringOfItem>>(pillarData.Attributes);
}

public string Serialize(string indent, bool withBraces) {
var contentIndent = indent;
if (withBraces) {
contentIndent += '\t';
}

var sb = new StringBuilder();
if (withBraces) {
sb.AppendLine("{");
}

if (string.IsNullOrEmpty(Type)) {
throw new ConverterException($"Cultural pillar {id} has no type defined!");
sb.Append(contentIndent).AppendLine($"type={Type}");
if (Color is not null) {
sb.Append(contentIndent).AppendLine($"color={Color}");
}
sb.AppendLine(PDXSerializer.Serialize(Attributes, indent: contentIndent, withBraces: false));

if (withBraces) {
sb.Append(indent).Append('}');
}

return sb.ToString();
}
}
63 changes: 60 additions & 3 deletions ImperatorToCK3/CK3/Cultures/PillarCollection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using commonItems;
using commonItems.Collections;
using commonItems.Colors;
using commonItems.Mods;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand All @@ -10,12 +12,67 @@ namespace ImperatorToCK3.CK3.Cultures;
public class PillarCollection : IdObjectCollection<string, Pillar> {
public IEnumerable<Pillar> Heritages => this.Where(p => p.Type == "heritage").ToImmutableList();

public PillarCollection(ColorFactory colorFactory) {
InitPillarDataParser(colorFactory);
}

public void LoadPillars(ModFilesystem ck3ModFS) {
var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, pillarId) => {
AddOrReplace(new Pillar(pillarId, reader));
});
parser.RegisterRegex(CommonRegexes.String, (reader, pillarId) => LoadPillar(pillarId, reader));
parser.IgnoreAndLogUnregisteredItems();
parser.ParseGameFolder("common/culture/pillars", ck3ModFS, "txt", true);
}

public void LoadConverterPillars(string converterCulturesPath){
var parser = new Parser();
parser.RegisterRegex(CommonRegexes.String, (reader, pillarId) => LoadPillar(pillarId, reader));
parser.IgnoreAndLogUnregisteredItems();
parser.ParseFolder(converterCulturesPath, "txt", true, logFilePaths: true);
}

private void LoadPillar(string pillarId, BufferedReader pillarReader) {
pillarDataParser.ParseStream(pillarReader);

if (pillarData.InvalidatingPillarIds.Any()) {
foreach (var existingPillar in this) {
if (!pillarData.InvalidatingPillarIds.Contains(existingPillar.Id)) {
continue;
}
Logger.Debug($"Pillar {pillarId} is invalidated by existing {existingPillar.Id}.");
return;
}
Logger.Debug($"Loading optional pillar {pillarId}...");
}
if (pillarData.Type is null) {
Logger.Warn($"Pillar {pillarId} has no type defined! Skipping.");
return;
}
AddOrReplace(new Pillar(pillarId, pillarData));

// Reset pillar data for the next pillar.
pillarData = new PillarData();
}

private void InitPillarDataParser(ColorFactory colorFactory) {
pillarDataParser.RegisterKeyword("INVALIDATED_BY", reader => {
pillarData.InvalidatingPillarIds = reader.GetStrings();
});
pillarDataParser.RegisterKeyword("type", reader => {
pillarData.Type = reader.GetString();
});
pillarDataParser.RegisterKeyword("color", reader => {
try {
pillarData.Color = colorFactory.GetColor(reader);
} catch (Exception e) {
Logger.Warn($"Found invalid color when parsing pillar! {e.Message}");
}
});
pillarDataParser.RegisterRegex(CommonRegexes.String, (reader, keyword) => {
pillarData.Attributes.Add(new KeyValuePair<string, StringOfItem>(keyword, reader.GetStringOfItem()));
});
pillarDataParser.IgnoreAndLogUnregisteredItems();
}

private PillarData pillarData = new();
private readonly Parser pillarDataParser = new();
}
13 changes: 13 additions & 0 deletions ImperatorToCK3/CK3/Cultures/PillarData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using commonItems;
using commonItems.Colors;
using System.Collections.Generic;

namespace ImperatorToCK3.CK3.Cultures;

public record PillarData {
public IEnumerable<string> InvalidatingPillarIds { get; set; } = new List<string>();
public string? Type { get; set; }
public Color? Color { get; set; }

public IList<KeyValuePair<string, StringOfItem>> Attributes { get; } = new List<KeyValuePair<string, StringOfItem>>();
}
9 changes: 6 additions & 3 deletions ImperatorToCK3/CK3/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class World {
public DynastyCollection Dynasties { get; } = new();
public ProvinceCollection Provinces { get; } = new();
public Title.LandedTitles LandedTitles { get; } = new();
public PillarCollection CulturalPillars { get; }
public CultureCollection Cultures { get; }
public ReligionCollection Religions { get; }
public IdObjectCollection<string, MenAtArmsType> MenAtArmsTypes { get; } = new();
Expand Down Expand Up @@ -104,9 +105,11 @@ public World(Imperator.World impWorld, Configuration config) {

// Load CK3 cultures from CK3 mod filesystem.
Logger.Info("Loading cultural pillars...");
var culturalPillars = new PillarCollection();
culturalPillars.LoadPillars(ModFS);
Cultures = new CultureCollection(ck3ColorFactory, culturalPillars);
CulturalPillars = new(ck3ColorFactory);
CulturalPillars.LoadPillars(ModFS);
Logger.Info("Loading converter cultural pillars...");
CulturalPillars.LoadConverterPillars("configurables/cultural_pillars");
Cultures = new CultureCollection(ck3ColorFactory, CulturalPillars);
Logger.Info("Loading name lists...");
Cultures.LoadNameLists(ModFS);
Logger.Info("Loading cultures...");
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 75b52fb

Please sign in to comment.