Fix ParentablePath issues; fix parse logging

This commit is contained in:
Matt Nadareski
2020-09-21 13:04:11 -07:00
parent 07066c2299
commit a04a3485ef
20 changed files with 517 additions and 388 deletions

View File

@@ -59,53 +59,57 @@ namespace SabreTools.Library.DatFiles
{ {
// Get the current line, split and parse // Get the current line, split and parse
svr.ReadNextLine(); svr.ReadNextLine();
}
catch (InvalidDataException ex)
{
Globals.Logger.Error(ex, $"Malformed line found in '{filename}' at line {svr.LineNumber}");
if (throwOnError) throw ex;
continue;
}
Rom rom = new Rom Rom rom = new Rom
{
Name = "-",
Size = Constants.SizeZero,
CRC = Constants.CRCZero,
MD5 = Constants.MD5Zero,
SHA1 = Constants.SHA1Zero,
ItemStatus = ItemStatus.None,
Machine = new Machine
{ {
Name = svr.Line[0], // #Name Name = "-",
Description = svr.Line[1], // Title Size = Constants.SizeZero,
CloneOf = svr.Line[3], // CloneOf CRC = Constants.CRCZero,
Year = svr.Line[4], // Year MD5 = Constants.MD5Zero,
Manufacturer = svr.Line[5], // Manufacturer SHA1 = Constants.SHA1Zero,
Category = svr.Line[6], // Category ItemStatus = ItemStatus.None,
Players = svr.Line[7], // Players
Rotation = svr.Line[8], // Rotation
Control = svr.Line[9], // Control
Status = svr.Line[10], // Status
DisplayCount = svr.Line[11], // DisplayCount
DisplayType = svr.Line[12], // DisplayType
Comment = svr.Line[15], // Extra
Buttons = svr.Line[16], // Buttons
},
AltName = svr.Line[13], // AltRomname Machine = new Machine
AltTitle = svr.Line[14], // AltTitle {
Name = svr.Line[0], // #Name
Description = svr.Line[1], // Title
CloneOf = svr.Line[3], // CloneOf
Year = svr.Line[4], // Year
Manufacturer = svr.Line[5], // Manufacturer
Category = svr.Line[6], // Category
Players = svr.Line[7], // Players
Rotation = svr.Line[8], // Rotation
Control = svr.Line[9], // Control
Status = svr.Line[10], // Status
DisplayCount = svr.Line[11], // DisplayCount
DisplayType = svr.Line[12], // DisplayType
Comment = svr.Line[15], // Extra
Buttons = svr.Line[16], // Buttons
},
Source = new Source AltName = svr.Line[13], // AltRomname
AltTitle = svr.Line[14], // AltTitle
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now process and add the rom
ParseAddHelper(rom);
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
Globals.Logger.Error(ex, message);
if (throwOnError)
{ {
Index = indexId, svr.Dispose();
Name = filename, throw new Exception(message, ex);
}, }
}; }
// Now process and add the rom
ParseAddHelper(rom);
} }
svr.Dispose(); svr.Dispose();

View File

@@ -54,33 +54,46 @@ namespace SabreTools.Library.DatFiles
while (!cmpr.EndOfStream) while (!cmpr.EndOfStream)
{ {
cmpr.ReadNextLine(); try
// Ignore everything not top-level
if (cmpr.RowType != CmpRowType.TopLevel)
continue;
// Switch on the top-level name
switch (cmpr.TopLevel.ToLowerInvariant())
{ {
// Header values cmpr.ReadNextLine();
case "clrmamepro":
case "romvault":
ReadHeader(cmpr, keep);
break;
// Sets // Ignore everything not top-level
case "set": // Used by the most ancient DATs if (cmpr.RowType != CmpRowType.TopLevel)
case "game": // Used by most CMP DATs continue;
case "machine": // Possibly used by MAME CMP DATs
ReadSet(cmpr, false, filename, indexId);
break;
case "resource": // Used by some other DATs to denote a BIOS set
ReadSet(cmpr, true, filename, indexId);
break;
default: // Switch on the top-level name
break; switch (cmpr.TopLevel.ToLowerInvariant())
{
// Header values
case "clrmamepro":
case "romvault":
ReadHeader(cmpr, keep);
break;
// Sets
case "set": // Used by the most ancient DATs
case "game": // Used by most CMP DATs
case "machine": // Possibly used by MAME CMP DATs
ReadSet(cmpr, false, filename, indexId);
break;
case "resource": // Used by some other DATs to denote a BIOS set
ReadSet(cmpr, true, filename, indexId);
break;
default:
break;
}
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing line {cmpr.LineNumber} '{cmpr.CurrentLine}'";
Globals.Logger.Error(ex, message);
if (throwOnError)
{
cmpr.Dispose();
throw new Exception(message, ex);
}
} }
} }

View File

@@ -1802,10 +1802,11 @@ namespace SabreTools.Library.DatFiles
/// Create a DatFile and parse a file into it /// Create a DatFile and parse a file into it
/// </summary> /// </summary>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="filename">Name of the file to be parsed</param>
public static DatFile CreateAndParse(string filename) /// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
public static DatFile CreateAndParse(string filename, bool throwOnError = false)
{ {
DatFile datFile = Create(); DatFile datFile = Create();
datFile.Parse(new ParentablePath(filename)); datFile.Parse(new ParentablePath(filename), throwOnError: throwOnError);
return datFile; return datFile;
} }
@@ -1833,22 +1834,22 @@ namespace SabreTools.Library.DatFiles
/// <summary> /// <summary>
/// Parse a DAT and return all found games and roms within /// Parse a DAT and return all found games and roms within
/// </summary> /// </summary>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="input">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param> /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
/// <param name="keepext">True if original extension should be kept, false otherwise (default)</param> /// <param name="keepext">True if original extension should be kept, false otherwise (default)</param>
/// <param name="quotes">True if quotes are assumed in supported types (default), false otherwise</param> /// <param name="quotes">True if quotes are assumed in supported types (default), false otherwise</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param> /// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
public void Parse( public void Parse(
ParentablePath filename, ParentablePath input,
int indexId = 0, int indexId = 0,
bool keep = false, bool keep = false,
bool keepext = false, bool keepext = false,
bool quotes = true, bool quotes = true,
bool throwOnError = false) bool throwOnError = true)
{ {
// Get the current path from the filename // Get the current path from the filename
string currentPath = filename.CurrentPath; string currentPath = input.CurrentPath;
// Check the file extension first as a safeguard // Check the file extension first as a safeguard
if (!PathExtensions.HasValidDatExtension(currentPath)) if (!PathExtensions.HasValidDatExtension(currentPath))
@@ -1868,7 +1869,7 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) catch (Exception ex)
{ {
Globals.Logger.Error(ex, $"Error with file '{filename}'"); Globals.Logger.Error(ex, $"Error with file '{currentPath}'");
if (throwOnError) throw ex; if (throwOnError) throw ex;
} }
} }

View File

@@ -43,27 +43,40 @@ namespace SabreTools.Library.DatFiles
while (!cmpr.EndOfStream) while (!cmpr.EndOfStream)
{ {
cmpr.ReadNextLine(); try
// Ignore everything not top-level
if (cmpr.RowType != CmpRowType.TopLevel)
continue;
// Switch on the top-level name
switch (cmpr.TopLevel.ToLowerInvariant())
{ {
// Header values cmpr.ReadNextLine();
case "doscenter":
ReadHeader(cmpr);
break;
// Sets // Ignore everything not top-level
case "game": if (cmpr.RowType != CmpRowType.TopLevel)
ReadGame(cmpr, filename, indexId); continue;
break;
default: // Switch on the top-level name
break; switch (cmpr.TopLevel.ToLowerInvariant())
{
// Header values
case "doscenter":
ReadHeader(cmpr);
break;
// Sets
case "game":
ReadGame(cmpr, filename, indexId);
break;
default:
break;
}
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing line {cmpr.LineNumber} '{cmpr.CurrentLine}'";
Globals.Logger.Error(ex, message);
if (throwOnError)
{
cmpr.Dispose();
throw new Exception(message, ex);
}
} }
} }

View File

@@ -33,51 +33,65 @@ namespace SabreTools.Library.DatFiles
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param> /// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false) protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
{ {
// TODO: Use SeparatedValueReader
// Open a file reader // Open a file reader
Encoding enc = FileExtensions.GetEncoding(filename); Encoding enc = FileExtensions.GetEncoding(filename);
StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), enc); StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), enc);
while (!sr.EndOfStream) while (!sr.EndOfStream)
{ {
string line = sr.ReadLine(); try
/*
The gameinfo order is as follows
0 - SHA-256
1 - Machine Name/Filename
2 - SHA-1
3 - MD5
4 - CRC32
*/
string[] gameinfo = line.Split('\t');
string[] fullname = gameinfo[1].Split('/');
Rom rom = new Rom
{ {
Name = gameinfo[1].Substring(fullname[0].Length + 1), string line = sr.ReadLine();
Size = null, // No size provided, but we don't want the size being 0
CRC = gameinfo[4],
MD5 = gameinfo[3],
SHA1 = gameinfo[2],
SHA256 = gameinfo[0],
ItemStatus = ItemStatus.None,
Machine = new Machine /*
The gameinfo order is as follows
0 - SHA-256
1 - Machine Name/Filename
2 - SHA-1
3 - MD5
4 - CRC32
*/
string[] gameinfo = line.Split('\t');
string[] fullname = gameinfo[1].Split('/');
Rom rom = new Rom
{ {
Name = fullname[0], Name = gameinfo[1].Substring(fullname[0].Length + 1),
Description = fullname[0], Size = null, // No size provided, but we don't want the size being 0
}, CRC = gameinfo[4],
MD5 = gameinfo[3],
SHA1 = gameinfo[2],
SHA256 = gameinfo[0],
ItemStatus = ItemStatus.None,
Source = new Source Machine = new Machine
{
Name = fullname[0],
Description = fullname[0],
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now process and add the rom
ParseAddHelper(rom);
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing at position {sr.BaseStream.Position}";
Globals.Logger.Error(ex, message);
if (throwOnError)
{ {
Index = indexId, sr.Dispose();
Name = filename, throw new Exception(message, ex);
}, }
}; }
// Now process and add the rom
ParseAddHelper(rom);
} }
sr.Dispose(); sr.Dispose();

View File

@@ -43,56 +43,69 @@ namespace SabreTools.Library.DatFiles
while (!sr.EndOfStream) while (!sr.EndOfStream)
{ {
string line = sr.ReadLine(); try
// Split the line and get the name and hash
string[] split = line.Split(' ');
string name = string.Empty;
string hash = string.Empty;
// If we have CRC, then it's an SFV file and the name is first are
if (_hash.HasFlag(Hash.CRC))
{ {
name = split[0].Replace("*", String.Empty); string line = sr.ReadLine();
hash = split[1];
}
// Otherwise, the name is second
else
{
name = split[1].Replace("*", String.Empty);
hash = split[0];
}
Rom rom = new Rom // Split the line and get the name and hash
{ string[] split = line.Split(' ');
Name = name, string name = string.Empty;
Size = null, string hash = string.Empty;
CRC = (_hash.HasFlag(Hash.CRC) ? hash : null),
MD5 = (_hash.HasFlag(Hash.MD5) ? hash : null), // If we have CRC, then it's an SFV file and the name is first are
if (_hash.HasFlag(Hash.CRC))
{
name = split[0].Replace("*", String.Empty);
hash = split[1];
}
// Otherwise, the name is second
else
{
name = split[1].Replace("*", String.Empty);
hash = split[0];
}
Rom rom = new Rom
{
Name = name,
Size = null,
CRC = (_hash.HasFlag(Hash.CRC) ? hash : null),
MD5 = (_hash.HasFlag(Hash.MD5) ? hash : null),
#if NET_FRAMEWORK #if NET_FRAMEWORK
RIPEMD160 = (_hash.HasFlag(Hash.RIPEMD160) ? hash : null), RIPEMD160 = (_hash.HasFlag(Hash.RIPEMD160) ? hash : null),
#endif #endif
SHA1 = (_hash.HasFlag(Hash.SHA1) ? hash : null), SHA1 = (_hash.HasFlag(Hash.SHA1) ? hash : null),
SHA256 = (_hash.HasFlag(Hash.SHA256) ? hash : null), SHA256 = (_hash.HasFlag(Hash.SHA256) ? hash : null),
SHA384 = (_hash.HasFlag(Hash.SHA384) ? hash : null), SHA384 = (_hash.HasFlag(Hash.SHA384) ? hash : null),
SHA512 = (_hash.HasFlag(Hash.SHA512) ? hash : null), SHA512 = (_hash.HasFlag(Hash.SHA512) ? hash : null),
SpamSum = (_hash.HasFlag(Hash.SpamSum) ? hash : null), SpamSum = (_hash.HasFlag(Hash.SpamSum) ? hash : null),
ItemStatus = ItemStatus.None, ItemStatus = ItemStatus.None,
Machine = new Machine Machine = new Machine
{
Name = Path.GetFileNameWithoutExtension(filename),
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now process and add the rom
ParseAddHelper(rom);
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing at position {sr.BaseStream.Position}";
Globals.Logger.Error(ex, message);
if (throwOnError)
{ {
Name = Path.GetFileNameWithoutExtension(filename), sr.Dispose();
}, throw new Exception(message, ex);
}
Source = new Source }
{
Index = indexId,
Name = filename,
},
};
// Now process and add the rom
ParseAddHelper(rom);
} }
sr.Dispose(); sr.Dispose();

View File

@@ -1410,13 +1410,13 @@ namespace SabreTools.Library.DatFiles
dirStats.ResetStatistics(); dirStats.ResetStatistics();
} }
Globals.Logger.Verbose($"Beginning stat collection for '{file}'", false); Globals.Logger.Verbose($"Beginning stat collection for '{file.CurrentPath}'", false);
List<string> games = new List<string>(); List<string> games = new List<string>();
DatFile datdata = DatFile.CreateAndParse(file.CurrentPath); DatFile datdata = DatFile.CreateAndParse(file.CurrentPath);
datdata.Items.BucketBy(Field.Machine_Name, DedupeType.None, norename: true); datdata.Items.BucketBy(Field.Machine_Name, DedupeType.None, norename: true);
// Output single DAT stats (if asked) // Output single DAT stats (if asked)
Globals.Logger.User($"Adding stats for file '{file}'\n", false); Globals.Logger.User($"Adding stats for file '{file.CurrentPath}'\n", false);
if (single) if (single)
{ {
reports.ForEach(report => report.ReplaceStatistics(datdata.Header.FileName, datdata.Items.Keys.Count, datdata.Items)); reports.ForEach(report => report.ReplaceStatistics(datdata.Header.FileName, datdata.Items.Keys.Count, datdata.Items));

View File

@@ -50,201 +50,214 @@ namespace SabreTools.Library.DatFiles
string gamename = string.Empty; string gamename = string.Empty;
while (!sr.EndOfStream) while (!sr.EndOfStream)
{ {
string line = sr.ReadLine().Trim(); try
// If we have a blank line, we just skip it
if (string.IsNullOrWhiteSpace(line))
{ {
continue; string line = sr.ReadLine().Trim();
}
// If we have the descriptor line, ignore it // If we have a blank line, we just skip it
else if (line == "Name Size Checksum") if (string.IsNullOrWhiteSpace(line))
{
continue;
}
// If we have the beginning of a game, set the name of the game
else if (line.StartsWith("ROMs required for"))
{
gamename = Regex.Match(line, @"^ROMs required for \S*? string.Empty(.*?)string.Empty\.").Groups[1].Value;
}
// If we have a machine with no required roms (usually internal devices), skip it
else if (line.StartsWith("No ROMs required for"))
{
continue;
}
// Otherwise, we assume we have a rom that we need to add
else
{
// First, we preprocess the line so that the rom name is consistently correct
string[] split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
// If the line doesn't have the 4 spaces of padding, check for 3
if (split.Length == 1)
split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
// If the split is still unsuccessful, log it and skip
if (split.Length == 1)
Globals.Logger.Warning($"Possibly malformed line: '{line}'");
string romname = split[0];
line = line.Substring(romname.Length);
// Next we separate the ROM into pieces
split = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
// Standard Disks have 2 pieces (name, sha1)
if (split.Length == 1)
{ {
Disk disk = new Disk() continue;
{
Name = romname,
SHA1 = Sanitizer.CleanListromHashData(split[0]),
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
} }
// Baddump Disks have 4 pieces (name, BAD, sha1, BAD_DUMP) // If we have the descriptor line, ignore it
else if (split.Length == 3 && line.EndsWith("BAD_DUMP")) else if (line == "Name Size Checksum")
{ {
Disk disk = new Disk() continue;
{
Name = romname,
SHA1 = Sanitizer.CleanListromHashData(split[1]),
ItemStatus = ItemStatus.BadDump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
} }
// Standard ROMs have 4 pieces (name, size, crc, sha1) // If we have the beginning of a game, set the name of the game
else if (split.Length == 3) else if (line.StartsWith("ROMs required for"))
{ {
Rom rom = new Rom() gamename = Regex.Match(line, @"^ROMs required for \S*? string.Empty(.*?)string.Empty\.").Groups[1].Value;
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
CRC = Sanitizer.CleanListromHashData(split[1]),
SHA1 = Sanitizer.CleanListromHashData(split[2]),
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
} }
// Nodump Disks have 5 pieces (name, NO, GOOD, DUMP, KNOWN) // If we have a machine with no required roms (usually internal devices), skip it
else if (split.Length == 4 && line.EndsWith("NO GOOD DUMP KNOWN")) else if (line.StartsWith("No ROMs required for"))
{ {
Disk disk = new Disk() continue;
{
Name = romname,
ItemStatus = ItemStatus.Nodump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
} }
// Baddump ROMs have 6 pieces (name, size, BAD, crc, sha1, BAD_DUMP) // Otherwise, we assume we have a rom that we need to add
else if (split.Length == 5 && line.EndsWith("BAD_DUMP"))
{
Rom rom = new Rom()
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
CRC = Sanitizer.CleanListromHashData(split[2]),
SHA1 = Sanitizer.CleanListromHashData(split[3]),
ItemStatus = ItemStatus.BadDump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
}
// Nodump ROMs have 6 pieces (name, size, NO, GOOD, DUMP, KNOWN)
else if (split.Length == 5 && line.EndsWith("NO GOOD DUMP KNOWN"))
{
Rom rom = new Rom()
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
ItemStatus = ItemStatus.Nodump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
}
// If we have something else, it's invalid
else else
{ {
Globals.Logger.Warning($"Invalid line detected: '{romname} {line}'"); // First, we preprocess the line so that the rom name is consistently correct
string[] split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
// If the line doesn't have the 4 spaces of padding, check for 3
if (split.Length == 1)
split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
// If the split is still unsuccessful, log it and skip
if (split.Length == 1)
Globals.Logger.Warning($"Possibly malformed line: '{line}'");
string romname = split[0];
line = line.Substring(romname.Length);
// Next we separate the ROM into pieces
split = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
// Standard Disks have 2 pieces (name, sha1)
if (split.Length == 1)
{
Disk disk = new Disk()
{
Name = romname,
SHA1 = Sanitizer.CleanListromHashData(split[0]),
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
}
// Baddump Disks have 4 pieces (name, BAD, sha1, BAD_DUMP)
else if (split.Length == 3 && line.EndsWith("BAD_DUMP"))
{
Disk disk = new Disk()
{
Name = romname,
SHA1 = Sanitizer.CleanListromHashData(split[1]),
ItemStatus = ItemStatus.BadDump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
}
// Standard ROMs have 4 pieces (name, size, crc, sha1)
else if (split.Length == 3)
{
Rom rom = new Rom()
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
CRC = Sanitizer.CleanListromHashData(split[1]),
SHA1 = Sanitizer.CleanListromHashData(split[2]),
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
}
// Nodump Disks have 5 pieces (name, NO, GOOD, DUMP, KNOWN)
else if (split.Length == 4 && line.EndsWith("NO GOOD DUMP KNOWN"))
{
Disk disk = new Disk()
{
Name = romname,
ItemStatus = ItemStatus.Nodump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(disk);
}
// Baddump ROMs have 6 pieces (name, size, BAD, crc, sha1, BAD_DUMP)
else if (split.Length == 5 && line.EndsWith("BAD_DUMP"))
{
Rom rom = new Rom()
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
CRC = Sanitizer.CleanListromHashData(split[2]),
SHA1 = Sanitizer.CleanListromHashData(split[3]),
ItemStatus = ItemStatus.BadDump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
}
// Nodump ROMs have 6 pieces (name, size, NO, GOOD, DUMP, KNOWN)
else if (split.Length == 5 && line.EndsWith("NO GOOD DUMP KNOWN"))
{
Rom rom = new Rom()
{
Name = romname,
Size = Sanitizer.CleanLong(split[0]),
ItemStatus = ItemStatus.Nodump,
Machine = new Machine
{
Name = gamename,
},
Source = new Source
{
Index = indexId,
Name = filename,
},
};
ParseAddHelper(rom);
}
// If we have something else, it's invalid
else
{
Globals.Logger.Warning($"Invalid line detected: '{romname} {line}'");
}
}
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing at position {sr.BaseStream.Position}";
Globals.Logger.Error(ex, message);
if (throwOnError)
{
sr.Dispose();
throw new Exception(message, ex);
} }
} }
} }

View File

@@ -90,8 +90,12 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) catch (Exception ex)
{ {
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); Globals.Logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError) throw ex; if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node // For XML errors, just skip the affected node
xtr?.Read(); xtr?.Read();

View File

@@ -107,8 +107,12 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) catch (Exception ex)
{ {
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); Globals.Logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError) throw ex; if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node // For XML errors, just skip the affected node
xtr?.Read(); xtr?.Read();

View File

@@ -78,8 +78,12 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) catch (Exception ex)
{ {
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); Globals.Logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError) throw ex; if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node // For XML errors, just skip the affected node
xtr?.Read(); xtr?.Read();

View File

@@ -80,8 +80,12 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) catch (Exception ex)
{ {
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); Globals.Logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError) throw ex; if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node // For XML errors, just skip the affected node
xtr?.Read(); xtr?.Read();

View File

@@ -84,8 +84,13 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) catch (Exception ex)
{ {
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); string message = $"'{filename}' - There was an error parsing line {ir.LineNumber} '{ir.CurrentLine}'";
if (throwOnError) throw ex; Globals.Logger.Error(ex, message);
if (throwOnError)
{
ir.Dispose();
throw new Exception(message, ex);
}
} }
ir.Dispose(); ir.Dispose();
@@ -304,7 +309,7 @@ namespace SabreTools.Library.DatFiles
} }
// Roms are not valid row formats, usually // Roms are not valid row formats, usually
string line = reader.Line; string line = reader.CurrentLine;
// If we don't have a valid game, keep reading // If we don't have a valid game, keep reading
if (!line.StartsWith("¬")) if (!line.StartsWith("¬"))

View File

@@ -77,8 +77,12 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) catch (Exception ex)
{ {
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); Globals.Logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError) throw ex; if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node // For XML errors, just skip the affected node
xtr?.Read(); xtr?.Read();

View File

@@ -65,8 +65,14 @@ namespace SabreTools.Library.DatFiles
} }
catch (InvalidDataException ex) catch (InvalidDataException ex)
{ {
Globals.Logger.Warning($"Malformed line found in '{filename}' at line {svr.LineNumber}"); string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
if (throwOnError) throw ex; Globals.Logger.Error(ex, message);
if (throwOnError)
{
svr.Dispose();
throw new Exception(message, ex);
}
continue; continue;
} }

View File

@@ -81,8 +81,12 @@ namespace SabreTools.Library.DatFiles
} }
catch (Exception ex) catch (Exception ex)
{ {
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}"); Globals.Logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError) throw ex; if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node // For XML errors, just skip the affected node
xtr?.Read(); xtr?.Read();

View File

@@ -74,7 +74,7 @@ namespace SabreTools.Library.Filtering
{ {
// Get the key and value // Get the key and value
string key = ir.Section; string key = ir.Section;
string value = ir.Line.Trim(); string value = ir.CurrentLine.Trim();
// If the section is "ROOT_FOLDER", then we use the value "true" instead. // If the section is "ROOT_FOLDER", then we use the value "true" instead.
// This is done because some INI files use the name of the file as the // This is done because some INI files use the name of the file as the

View File

@@ -16,6 +16,16 @@ namespace SabreTools.Library.IO
/// </summary> /// </summary>
private StreamReader sr; private StreamReader sr;
/// <summary>
/// Contents of the current line, unprocessed
/// </summary>
public string CurrentLine { get; private set; } = string.Empty;
/// <summary>
/// Get the current line number
/// </summary>
public long LineNumber { get; private set; } = 0;
/// <summary> /// <summary>
/// Get if at end of stream /// Get if at end of stream
/// </summary> /// </summary>
@@ -74,7 +84,6 @@ namespace SabreTools.Library.IO
public ClrMameProReader(string filename) public ClrMameProReader(string filename)
{ {
sr = new StreamReader(filename); sr = new StreamReader(filename);
DosCenter = true;
} }
/// <summary> /// <summary>
@@ -83,7 +92,6 @@ namespace SabreTools.Library.IO
public ClrMameProReader(Stream stream, Encoding encoding) public ClrMameProReader(Stream stream, Encoding encoding)
{ {
sr = new StreamReader(stream, encoding); sr = new StreamReader(stream, encoding);
DosCenter = true;
} }
/// <summary> /// <summary>
@@ -94,8 +102,11 @@ namespace SabreTools.Library.IO
if (!(sr.BaseStream?.CanRead ?? false) || sr.EndOfStream) if (!(sr.BaseStream?.CanRead ?? false) || sr.EndOfStream)
return false; return false;
string line = sr.ReadLine().Trim(); CurrentLine = sr.ReadLine().Trim();
ProcessLine(line); LineNumber++;
// TODO: Act like IniReader here
ProcessLine(CurrentLine);
return true; return true;
} }

View File

@@ -30,9 +30,14 @@ namespace SabreTools.Library.IO
public KeyValuePair<string, string>? KeyValuePair { get; private set; } = null; public KeyValuePair<string, string>? KeyValuePair { get; private set; } = null;
/// <summary> /// <summary>
/// Contents of the currently read line /// Contents of the current line, unprocessed
/// </summary> /// </summary>
public string Line { get; private set; } = string.Empty; public string CurrentLine { get; private set; } = string.Empty;
/// <summary>
/// Get the current line number
/// </summary>
public long LineNumber { get; private set; } = 0;
/// <summary> /// <summary>
/// Current row type /// Current row type
@@ -73,7 +78,8 @@ namespace SabreTools.Library.IO
if (!(sr.BaseStream?.CanRead ?? false) || sr.EndOfStream) if (!(sr.BaseStream?.CanRead ?? false) || sr.EndOfStream)
return false; return false;
Line = sr.ReadLine().Trim(); CurrentLine = sr.ReadLine().Trim();
LineNumber++;
ProcessLine(); ProcessLine();
return true; return true;
} }
@@ -84,25 +90,25 @@ namespace SabreTools.Library.IO
private void ProcessLine() private void ProcessLine()
{ {
// Comment // Comment
if (Line.StartsWith(";")) if (CurrentLine.StartsWith(";"))
{ {
KeyValuePair = null; KeyValuePair = null;
RowType = IniRowType.Comment; RowType = IniRowType.Comment;
} }
// Section // Section
else if (Line.StartsWith("[") && Line.EndsWith("]")) else if (CurrentLine.StartsWith("[") && CurrentLine.EndsWith("]"))
{ {
KeyValuePair = null; KeyValuePair = null;
RowType = IniRowType.SectionHeader; RowType = IniRowType.SectionHeader;
Section = Line.TrimStart('[').TrimEnd(']'); Section = CurrentLine.TrimStart('[').TrimEnd(']');
} }
// KeyValuePair // KeyValuePair
else if (Line.Contains("=")) else if (CurrentLine.Contains("="))
{ {
// Split the line by '=' for key-value pairs // Split the line by '=' for key-value pairs
string[] data = Line.Split('='); string[] data = CurrentLine.Split('=');
// If the value field contains an '=', we need to put them back in // If the value field contains an '=', we need to put them back in
string key = data[0].Trim(); string key = data[0].Trim();
@@ -113,10 +119,10 @@ namespace SabreTools.Library.IO
} }
// Empty // Empty
else if (string.IsNullOrEmpty(Line)) else if (string.IsNullOrEmpty(CurrentLine))
{ {
KeyValuePair = null; KeyValuePair = null;
Line = string.Empty; CurrentLine = string.Empty;
RowType = IniRowType.None; RowType = IniRowType.None;
} }
@@ -127,7 +133,7 @@ namespace SabreTools.Library.IO
RowType = IniRowType.Invalid; RowType = IniRowType.Invalid;
if (ValidateRows) if (ValidateRows)
throw new InvalidDataException($"Invalid INI row found, cannot continue: {Line}"); throw new InvalidDataException($"Invalid INI row found, cannot continue: {CurrentLine}");
} }
} }

View File

@@ -30,6 +30,16 @@ namespace SabreTools.Library.IO
} }
} }
/// <summary>
/// Contents of the current line, unprocessed
/// </summary>
public string CurrentLine { get; private set; } = string.Empty;
/// <summary>
/// Get the current line number
/// </summary>
public long LineNumber { get; private set; } = 0;
/// <summary> /// <summary>
/// Assume the first row is a header /// Assume the first row is a header
/// </summary> /// </summary>
@@ -45,11 +55,6 @@ namespace SabreTools.Library.IO
/// </summary> /// </summary>
public List<string> Line { get; private set; } = null; public List<string> Line { get; private set; } = null;
/// <summary>
/// Get the current line number
/// </summary>
public long LineNumber { get; private set; } = -1;
/// <summary> /// <summary>
/// Assume that values are wrapped in quotes /// Assume that values are wrapped in quotes
/// </summary> /// </summary>
@@ -104,6 +109,7 @@ namespace SabreTools.Library.IO
return false; return false;
string fullLine = sr.ReadLine(); string fullLine = sr.ReadLine();
CurrentLine = fullLine;
LineNumber++; LineNumber++;
// If we have quotes, we need to split specially // If we have quotes, we need to split specially