Simplify ParseInto invocations

This commit is contained in:
Matt Nadareski
2025-02-12 11:32:22 -05:00
parent 716bc1f883
commit 6509a99d1e
7 changed files with 18 additions and 44 deletions

View File

@@ -1486,7 +1486,7 @@ namespace SabreTools.DatFiles
var input = inputs[i]; var input = inputs[i];
_staticLogger.User($"Adding DAT: {input.CurrentPath}"); _staticLogger.User($"Adding DAT: {input.CurrentPath}");
datFiles[i] = CreateDatFile(datFile.Header.CloneFormat(), datFile.Modifiers); datFiles[i] = CreateDatFile(datFile.Header.CloneFormat(), datFile.Modifiers);
Parser.ParseInto(datFiles[i], input, i, keep: true); Parser.ParseInto(datFiles[i], input.CurrentPath, i, keep: true);
#if NET40_OR_GREATER || NETCOREAPP #if NET40_OR_GREATER || NETCOREAPP
}); });
#else #else

View File

@@ -42,61 +42,35 @@ namespace SabreTools.DatFiles
bool statsOnly = false, bool statsOnly = false,
bool throwOnError = false) bool throwOnError = false)
{ {
var path = new ParentablePath(filename.Trim('"'));
ParseInto(datFile, path, indexId, keep, keepext, statsOnly, throwOnError);
}
/// <summary>
/// Parse a DAT and return all found games and roms within
/// </summary>
/// <param name="datFile">Current DatFile object to add to</param>
/// <param name="input">Name of the file to be parsed</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="keepext">True if original extension should be kept, false otherwise (default)</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
public static void ParseInto(
DatFile datFile,
ParentablePath input,
int indexId = 0,
bool keep = false,
bool keepext = false,
bool statsOnly = false,
bool throwOnError = false)
{
// Get the current path from the filename
string currentPath = input.CurrentPath;
// Check the file extension first as a safeguard // Check the file extension first as a safeguard
if (!Utilities.HasValidDatExtension(currentPath)) if (!Utilities.HasValidDatExtension(filename))
return; return;
// If the output filename isn't set already, get the internal filename // If the output filename isn't set already, get the internal filename
datFile.Header.SetFieldValue<string?>(DatHeader.FileNameKey, string.IsNullOrEmpty(datFile.Header.GetStringFieldValue(DatHeader.FileNameKey)) datFile.Header.SetFieldValue<string?>(DatHeader.FileNameKey, string.IsNullOrEmpty(datFile.Header.GetStringFieldValue(DatHeader.FileNameKey))
? (keepext ? (keepext
? Path.GetFileName(currentPath) ? Path.GetFileName(filename)
: Path.GetFileNameWithoutExtension(currentPath)) : Path.GetFileNameWithoutExtension(filename))
: datFile.Header.GetStringFieldValue(DatHeader.FileNameKey)); : datFile.Header.GetStringFieldValue(DatHeader.FileNameKey));
// If the output type isn't set already, get the internal output type // If the output type isn't set already, get the internal output type
DatFormat datFormat = GetDatFormat(currentPath); DatFormat datFormat = GetDatFormat(filename);
datFile.Header.SetFieldValue<DatFormat>(DatHeader.DatFormatKey, datFile.Header.GetFieldValue<DatFormat>(DatHeader.DatFormatKey) == 0 datFile.Header.SetFieldValue<DatFormat>(DatHeader.DatFormatKey, datFile.Header.GetFieldValue<DatFormat>(DatHeader.DatFormatKey) == 0
? datFormat ? datFormat
: datFile.Header.GetFieldValue<DatFormat>(DatHeader.DatFormatKey)); : datFile.Header.GetFieldValue<DatFormat>(DatHeader.DatFormatKey));
datFile.Items.SetBucketedBy(ItemKey.CRC); // Setting this because it can reduce issues later datFile.Items.SetBucketedBy(ItemKey.CRC); // Setting this because it can reduce issues later
InternalStopwatch watch = new($"Parsing '{currentPath}' into internal DAT"); InternalStopwatch watch = new($"Parsing '{filename}' into internal DAT");
// Now parse the correct type of DAT // Now parse the correct type of DAT
try try
{ {
DatFile parsingDatFile = DatFileTool.CreateDatFile(datFormat, datFile); DatFile parsingDatFile = DatFileTool.CreateDatFile(datFormat, datFile);
parsingDatFile.ParseFile(currentPath, indexId, keep, statsOnly: statsOnly, throwOnError: throwOnError); parsingDatFile.ParseFile(filename, indexId, keep, statsOnly: statsOnly, throwOnError: throwOnError);
} }
catch (Exception ex) when (!throwOnError) catch (Exception ex) when (!throwOnError)
{ {
_staticLogger.Error(ex, $"Error with file '{currentPath}'"); _staticLogger.Error(ex, $"Error with file '{filename}'");
} }
watch.Stop(); watch.Stop();
@@ -114,7 +88,7 @@ namespace SabreTools.DatFiles
return DatFileTool.CreateDatFile(); return DatFileTool.CreateDatFile();
DatFile datFile = DatFileTool.CreateDatFile(); DatFile datFile = DatFileTool.CreateDatFile();
ParseInto(datFile, new ParentablePath(filename), statsOnly: true, throwOnError: throwOnError); ParseInto(datFile, filename, statsOnly: true, throwOnError: throwOnError);
return datFile; return datFile;
} }

View File

@@ -525,7 +525,7 @@ Reset the internal state: reset();";
// Assume there could be multiple // Assume there could be multiple
foreach (ParentablePath datFilePath in datFilePaths) foreach (ParentablePath datFilePath in datFilePaths)
{ {
Parser.ParseInto(batchState.DatFile, datFilePath, batchState.Index++); Parser.ParseInto(batchState.DatFile, datFilePath.CurrentPath, batchState.Index++);
} }
} }
} }

View File

@@ -94,7 +94,7 @@ namespace SabreTools.Features
foreach (ParentablePath datfile in datfilePaths) foreach (ParentablePath datfile in datfilePaths)
{ {
DatFile datdata = DatFileTool.CreateDatFile(); DatFile datdata = DatFileTool.CreateDatFile();
Parser.ParseInto(datdata, datfile, int.MaxValue, keep: true); Parser.ParseInto(datdata, datfile.CurrentPath, int.MaxValue, keep: true);
// Skip if nothing was parsed // Skip if nothing was parsed
if (datdata.DatStatistics.TotalCount == 0) // datdata.ItemsDB.SortedKeys.Length == 0 if (datdata.DatStatistics.TotalCount == 0) // datdata.ItemsDB.SortedKeys.Length == 0
@@ -136,7 +136,7 @@ namespace SabreTools.Features
DatFile datdata = DatFileTool.CreateDatFile(); DatFile datdata = DatFileTool.CreateDatFile();
foreach (ParentablePath datfile in datfilePaths) foreach (ParentablePath datfile in datfilePaths)
{ {
Parser.ParseInto(datdata, datfile, int.MaxValue, keep: true); Parser.ParseInto(datdata, datfile.CurrentPath, int.MaxValue, keep: true);
} }
// Set depot information // Set depot information

View File

@@ -65,7 +65,7 @@ namespace SabreTools.Features
{ {
// Create and fill the new DAT // Create and fill the new DAT
DatFile internalDat = DatFileTool.CreateDatFile(Header!, Modifiers!); DatFile internalDat = DatFileTool.CreateDatFile(Header!, Modifiers!);
Parser.ParseInto(internalDat, file); Parser.ParseInto(internalDat, file.CurrentPath);
// Get the output directory // Get the output directory
OutputDir = OutputDir.Ensure(); OutputDir = OutputDir.Ensure();

View File

@@ -183,7 +183,7 @@ namespace SabreTools.Features
|| currentFormat.HasFlag(DatFormat.TSV); || currentFormat.HasFlag(DatFormat.TSV);
#endif #endif
Parser.ParseInto(datFile, inputPath, keep: true, keepext: isSeparatedFile); Parser.ParseInto(datFile, inputPath.CurrentPath, keep: true, keepext: isSeparatedFile);
// Perform additional processing steps // Perform additional processing steps
Extras!.ApplyExtras(datFile); Extras!.ApplyExtras(datFile);
@@ -392,7 +392,7 @@ namespace SabreTools.Features
{ {
// Parse the path to a new DatFile // Parse the path to a new DatFile
DatFile repDat = DatFileTool.CreateDatFile(Header!, Modifiers); DatFile repDat = DatFileTool.CreateDatFile(Header!, Modifiers);
Parser.ParseInto(repDat, inputPath, indexId: 1, keep: true); Parser.ParseInto(repDat, inputPath.CurrentPath, indexId: 1, keep: true);
// Perform additional processing steps // Perform additional processing steps
Extras.ApplyExtras(repDat); Extras.ApplyExtras(repDat);
@@ -433,7 +433,7 @@ namespace SabreTools.Features
{ {
// Parse the path to a new DatFile // Parse the path to a new DatFile
DatFile repDat = DatFileTool.CreateDatFile(Header!, Modifiers); DatFile repDat = DatFileTool.CreateDatFile(Header!, Modifiers);
Parser.ParseInto(repDat, inputPath, indexId: 1, keep: true); Parser.ParseInto(repDat, inputPath.CurrentPath, indexId: 1, keep: true);
// Perform additional processing steps // Perform additional processing steps
Extras.ApplyExtras(repDat); Extras.ApplyExtras(repDat);

View File

@@ -63,7 +63,7 @@ namespace SabreTools.Features
{ {
// Parse in from the file // Parse in from the file
DatFile datdata = DatFileTool.CreateDatFile(); DatFile datdata = DatFileTool.CreateDatFile();
Parser.ParseInto(datdata, datfile, int.MaxValue, keep: true); Parser.ParseInto(datdata, datfile.CurrentPath, int.MaxValue, keep: true);
// Perform additional processing steps // Perform additional processing steps
Extras!.ApplyExtras(datdata); Extras!.ApplyExtras(datdata);
@@ -114,7 +114,7 @@ namespace SabreTools.Features
DatFile datdata = DatFileTool.CreateDatFile(); DatFile datdata = DatFileTool.CreateDatFile();
foreach (ParentablePath datfile in datfilePaths) foreach (ParentablePath datfile in datfilePaths)
{ {
Parser.ParseInto(datdata, datfile, int.MaxValue, keep: true); Parser.ParseInto(datdata, datfile.CurrentPath, int.MaxValue, keep: true);
} }
// Perform additional processing steps // Perform additional processing steps