Skip to content

Commit e99f730

Browse files
author
Blair Williams
committed
Replace CSV loading with another library
1 parent fa0f0a7 commit e99f730

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

src/LuminaSupplemental.Excel/LuminaSupplemental.Excel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<ItemGroup>
5656
<PackageReference Include="CSVFile" Version="3.2.0" />
5757
<PackageReference Include="CsvHelper" Version="30.0.1" />
58+
<PackageReference Include="Sylvan.Data.Csv" Version="1.3.9" />
5859
</ItemGroup>
5960

6061
<ItemGroup>

src/LuminaSupplemental.Excel/Services/CsvLoader.cs

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,52 @@
66
using System.Reflection;
77
using System.Text;
88

9-
using CSVFile;
10-
119
using CsvHelper;
1210

1311
using Lumina;
1412
using Lumina.Data;
1513

1614
using LuminaSupplemental.Excel.Model;
15+
using Sylvan.Data.Csv;
16+
17+
using CsvDataReader = Sylvan.Data.Csv.CsvDataReader;
1718

1819
namespace LuminaSupplemental.Excel.Services;
1920

2021
public static class CsvLoader
2122
{
22-
public static List< T > LoadCsv<T>(string filePath, out List<string> failedLines, out List<Exception> exceptions, GameData? gameData = null, Language? language = null) where T : ICsv, new()
23+
public static List< T > LoadCsv<T>(string filePath, bool includesHeaders, out List<string> failedLines, out List<Exception> exceptions, GameData? gameData = null, Language? language = null) where T : ICsv, new()
2324
{
24-
using var fileStream = new FileStream( filePath, FileMode.Open );
25-
using( StreamReader reader = new StreamReader( fileStream ) )
26-
{
27-
failedLines = new List< string >();
28-
exceptions = new List< Exception >();
29-
var items = new List< T >();
25+
failedLines = new List< string >();
26+
exceptions = new List< Exception >();
27+
var items = new List< T >();
3028

31-
//Loading an empty file
32-
if( reader.EndOfStream )
29+
using CsvDataReader dr = CsvDataReader.Create(filePath, new CsvDataReaderOptions(){HasHeaders = includesHeaders});
30+
while (dr.Read())
31+
{
32+
string[] fields = new string[dr.FieldCount];
33+
34+
for(int i = 0; i < dr.FieldCount; i++)
3335
{
34-
return items;
36+
fields[i] = dr.GetString(i);
3537
}
36-
37-
FileInfo f = new FileInfo(filePath);
38-
var fileContents = reader.ReadToEnd();
39-
fileContents = fileContents.ReplaceLineEndings("\n"); // Works around the fact that apparently CI can change line endings
40-
var csvReader = CSVFile.CSVReader.FromString( fileContents, new CSVSettings { Encoding = Encoding.UTF8, LineSeparator = "\n", BufferSize = (int)f.Length} ); //BufferSize fixes a infinite loop
41-
foreach( var line in csvReader.Lines() )
38+
T item = new T();
39+
try
4240
{
43-
T item = new T();
44-
try
41+
item.FromCsv( fields );
42+
if( gameData != null && language != null )
4543
{
46-
item.FromCsv( line );
47-
if( gameData != null && language != null )
48-
{
49-
item.PopulateData( gameData.Excel, language.Value );
50-
}
51-
items.Add( item );
52-
}
53-
catch( Exception e )
54-
{
55-
exceptions.Add(e);
56-
failedLines.Add( String.Join( ",",line ) );
44+
item.PopulateData( gameData.Excel, language.Value );
5745
}
46+
items.Add( item );
47+
}
48+
catch( Exception e )
49+
{
50+
exceptions.Add(e);
51+
failedLines.Add( String.Join( ",",fields ) );
5852
}
59-
return items;
6053
}
54+
return items;
6155
}
6256

6357
public const string DungeonBossResourceName = "LuminaSupplemental.Excel.Generated.DungeonBoss.csv";
@@ -83,7 +77,7 @@ public static class CsvLoader
8377
public const string FateItemResourceName = "LuminaSupplemental.Excel.Generated.FateItem.csv";
8478
public const string GardeningCrossbreedResourceName = "LuminaSupplemental.Excel.Generated.GardeningCrossbreed.csv";
8579

86-
public static List< T > LoadResource<T>(string resourceName, out List<string> failedLines, out List<Exception> exceptions, GameData? gameData = null, Language? language = null) where T : ICsv, new()
80+
public static List< T > LoadResource<T>(string resourceName, bool includesHeaders, out List<string> failedLines, out List<Exception> exceptions, GameData? gameData = null, Language? language = null) where T : ICsv, new()
8781
{
8882
var assembly = Assembly.GetExecutingAssembly();
8983
using( Stream? stream = assembly.GetManifestResourceStream( resourceName ) )
@@ -96,16 +90,21 @@ public static class CsvLoader
9690
}
9791
using( StreamReader reader = new StreamReader( stream ) )
9892
{
99-
var file = reader.ReadToEnd();
100-
file = file.ReplaceLineEndings("\n"); // Works around the fact that apparently CI can change line endings
101-
var csvReader = CSVFile.CSVReader.FromString( file, new CSVSettings { Encoding = Encoding.UTF8, LineSeparator = "\n" } );
10293
var items = new List< T >();
103-
foreach( var line in csvReader.Lines() )
94+
95+
using CsvDataReader dr = CsvDataReader.Create(reader, new CsvDataReaderOptions(){HasHeaders = includesHeaders});
96+
while (dr.Read())
10497
{
98+
string[] fields = new string[dr.FieldCount];
99+
100+
for(int i = 0; i < dr.FieldCount; i++)
101+
{
102+
fields[i] = dr.GetString(i);
103+
}
105104
T item = new T();
106105
try
107106
{
108-
item.FromCsv( line );
107+
item.FromCsv( fields );
109108

110109
if( gameData != null && language != null )
111110
{
@@ -117,7 +116,7 @@ public static class CsvLoader
117116
catch( Exception e )
118117
{
119118
exceptions.Add(e);
120-
failedLines.Add( String.Join( ",",line ) );
119+
failedLines.Add( String.Join( ",",fields ) );
121120
}
122121
}
123122

0 commit comments

Comments
 (0)