diff --git a/ImperatorToCK3/CK3/Provinces/ProvinceCollection.cs b/ImperatorToCK3/CK3/Provinces/ProvinceCollection.cs index 6c7343f95..f62a980c8 100644 --- a/ImperatorToCK3/CK3/Provinces/ProvinceCollection.cs +++ b/ImperatorToCK3/CK3/Provinces/ProvinceCollection.cs @@ -1,13 +1,12 @@ using commonItems; using commonItems.Collections; using commonItems.Mods; -using CsvHelper; -using CsvHelper.Configuration; using ImperatorToCK3.CK3.Titles; using ImperatorToCK3.Exceptions; using ImperatorToCK3.Mappers.Culture; using ImperatorToCK3.Mappers.Province; using ImperatorToCK3.Mappers.Religion; +using Microsoft.VisualBasic.FileIO; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -30,41 +29,33 @@ private void LoadProvinceDefinitions(ModFilesystem ck3ModFs) { // TODO: get rid if (filePath is null) { throw new ConverterException("Province definitions file not found!"); } + + int count = 0; + using (var parser = new TextFieldParser(filePath)) { + parser.TextFieldType = FieldType.Delimited; + parser.SetDelimiters(";"); + parser.CommentTokens = ["#"]; + parser.TrimWhiteSpace = true; + + while (!parser.EndOfData) { + string[]? fields = parser.ReadFields(); + if (fields is null) { + continue; + } - var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture) { - Delimiter = ";", - HasHeaderRecord = false, - AllowComments = true, - TrimOptions = TrimOptions.Trim, - IgnoreBlankLines = true, - ShouldSkipRecord = (args => { - string? cell = args.Row[0]; - if (cell is null) { - return true; + if (fields.Length < 1) { + continue; + } + + var id = ulong.Parse(fields[0]); + if (id == 0) { + continue; } - cell = cell.Trim(); - return cell.Length == 0 || cell[0] == '#'; - }), - }; - var provinceDefinition = new { - Id = default(ulong), - }; - using var reader = new StreamReader(filePath); - using var csv = new CsvReader(reader, csvConfig); - var records = csv.GetRecords(provinceDefinition); - - var count = 0; - foreach (var record in records) { - var id = record.Id; - if (id == 0) { - continue; + AddOrReplace(new Province(id)); + ++count; } - - AddOrReplace(new Province(id)); - ++count; } - Logger.Debug($"Loaded {count} province definitions."); } diff --git a/ImperatorToCK3/CommonUtils/Map/Adjacency.cs b/ImperatorToCK3/CommonUtils/Map/Adjacency.cs deleted file mode 100644 index 2d9999ed8..000000000 --- a/ImperatorToCK3/CommonUtils/Map/Adjacency.cs +++ /dev/null @@ -1,15 +0,0 @@ -using CsvHelper.Configuration.Attributes; - -namespace ImperatorToCK3.CommonUtils.Map; - -public sealed class Adjacency { - [Index(0)] public long From { get; set; } - [Index(1)] public long To { get; set; } - [Index(2)] public string Type { get; set; } = string.Empty; - [Index(3)] public long Through { get; set; } - [Index(4)] public long StartX { get; set; } - [Index(5)] public long StartY { get; set; } - [Index(6)] public long StopX { get; set; } - [Index(7)] public long StopY { get; set; } - [Index(8)] public string Comment { get; set; } = string.Empty; -} \ No newline at end of file diff --git a/ImperatorToCK3/CommonUtils/Map/MapData.cs b/ImperatorToCK3/CommonUtils/Map/MapData.cs index d18bb4c28..235991fe1 100644 --- a/ImperatorToCK3/CommonUtils/Map/MapData.cs +++ b/ImperatorToCK3/CommonUtils/Map/MapData.cs @@ -1,7 +1,6 @@ using commonItems; using commonItems.Mods; -using CsvHelper; -using CsvHelper.Configuration; +using Microsoft.VisualBasic.FileIO; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using System; @@ -11,8 +10,6 @@ using System.Linq; using System.Runtime.InteropServices; -using Index = CsvHelper.Configuration.Attributes.IndexAttribute; - namespace ImperatorToCK3.CommonUtils.Map; public sealed class MapData { @@ -423,37 +420,39 @@ private void LoadAdjacencies(string adjacenciesFilename, ModFilesystem modFS) { } Logger.Debug($"Loading adjacencies from \"{adjacenciesPath}\"..."); - var reader = new StreamReader(adjacenciesPath); - - var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture) { - Delimiter = ";", - HasHeaderRecord = false, // Ignore header using ShouldSkipRecord instead. - AllowComments = true, - TrimOptions = TrimOptions.Trim, - IgnoreBlankLines = true, - ShouldSkipRecord = (args => { - string? cell = args.Row[0]; - if (cell is null) { - return true; + int count = 0; + using (var parser = new TextFieldParser(adjacenciesPath)) { + parser.TextFieldType = FieldType.Delimited; + parser.SetDelimiters(";"); + parser.CommentTokens = ["#"]; + parser.TrimWhiteSpace = true; + + // Skip the header row. + parser.ReadFields(); + + while (!parser.EndOfData) { + string[]? fields = parser.ReadFields(); + if (fields is null) { + continue; } - cell = cell.Trim(); - return cell.Length == 0 || cell[0] == '#' || !ulong.TryParse(cell, out _); - }), - }; - using CsvReader csv = new(reader, csvConfig); - var records = csv.GetRecords().ToList(); - - int count = 0; - foreach (var record in records) { - if (record.From == -1) { - continue; - } - if (record.To == -1) { - continue; + if (fields.Length < 2) { + continue; + } + + var fromStr = fields[0]; + if (fromStr == "-1") { + continue; + } + + var toStr = fields[1]; + if (toStr == "-1") { + continue; + } + + AddAdjacency(ulong.Parse(fromStr, CultureInfo.InvariantCulture), ulong.Parse(toStr, CultureInfo.InvariantCulture)); + ++count; } - AddAdjacency((ulong)record.From, (ulong)record.To); - ++count; } Logger.Debug($"Loaded {count} province adjacencies."); } diff --git a/ImperatorToCK3/ImperatorToCK3.csproj b/ImperatorToCK3/ImperatorToCK3.csproj index 9d8b63f61..2dd5e4a4e 100644 --- a/ImperatorToCK3/ImperatorToCK3.csproj +++ b/ImperatorToCK3/ImperatorToCK3.csproj @@ -33,7 +33,6 @@ - all @@ -104,7 +103,6 @@ -