Skip to content

Commit

Permalink
Replace the CsvHelper library, which uses reflection (#1846) #patch
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains authored Mar 16, 2024
1 parent 7a41344 commit 68d4e2d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 81 deletions.
55 changes: 23 additions & 32 deletions ImperatorToCK3/CK3/Provinces/ProvinceCollection.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.");
}

Expand Down
15 changes: 0 additions & 15 deletions ImperatorToCK3/CommonUtils/Map/Adjacency.cs

This file was deleted.

63 changes: 31 additions & 32 deletions ImperatorToCK3/CommonUtils/Map/MapData.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Adjacency>().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.");
}
Expand Down
2 changes: 0 additions & 2 deletions ImperatorToCK3/ImperatorToCK3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

<ItemGroup>
<PackageReference Include="ColorHashSharp" Version="1.0.0" />
<PackageReference Include="CsvHelper" Version="31.0.2" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="13.6.0" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.146">
<PrivateAssets>all</PrivateAssets>
Expand Down Expand Up @@ -104,7 +103,6 @@

<ItemGroup>
<!-- root descriptors for trimming -->
<TrimmerRootAssembly Include="CsvHelper" />
<TrimmerRootAssembly Include="log4net" />
<TrimmerRootAssembly Include="commonItems" />
<TrimmerRootAssembly Include="System.Configuration.ConfigurationManager" />
Expand Down

0 comments on commit 68d4e2d

Please sign in to comment.