Support ancient .NET in DatFiles

This commit is contained in:
Matt Nadareski
2024-02-28 22:54:56 -05:00
parent e7c45c1f50
commit 2145245c31
38 changed files with 780 additions and 258 deletions

View File

@@ -33,7 +33,13 @@ namespace SabreTools.DatTools
public static void ApplySuperDAT(DatFile datFile, List<ParentablePath> inputs)
{
List<string> keys = [.. datFile.Items.Keys];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(keys, key =>
#else
foreach (var key in keys)
#endif
{
ConcurrentList<DatItem>? items = datFile.Items[key];
if (items == null)
@@ -98,7 +104,13 @@ namespace SabreTools.DatTools
intDat.Items.BucketBy(ItemKey.CRC, DedupeType.None);
// Then we do a hashwise comparison against the base DAT
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(intDat.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(intDat.Items.Keys, key =>
#else
foreach (var key in intDat.Items.Keys)
#endif
{
ConcurrentList<DatItem>? datItems = intDat.Items[key];
if (datItems == null)
@@ -136,7 +148,13 @@ namespace SabreTools.DatTools
intDat.Items.BucketBy(ItemKey.Machine, DedupeType.None);
// Then we do a namewise comparison against the base DAT
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(intDat.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(intDat.Items.Keys, key =>
#else
foreach (var key in intDat.Items.Keys)
#endif
{
ConcurrentList<DatItem>? datItems = intDat.Items[key];
if (datItems == null)
@@ -194,7 +212,13 @@ namespace SabreTools.DatTools
// Then we compare against the base DAT
List<string> keys = [.. intDat.Items.Keys];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(keys, key =>
#else
foreach (var key in keys)
#endif
{
// Game Against uses game names
if (useGames)
@@ -274,13 +298,23 @@ namespace SabreTools.DatTools
// Create the DatFiles from the set of headers
DatFile[] outDatsArray = new DatFile[datHeaders.Count];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.For(0, datHeaders.Count, Globals.ParallelOptions, j =>
#elif NET40_OR_GREATER
Parallel.For(0, datHeaders.Count, j =>
#else
for (int j = 0; j < datHeaders.Count; j++)
#endif
{
DatFile diffData = DatFile.Create(datHeaders[j]);
diffData.Items = [];
FillWithSourceIndex(datFile, diffData, j);
outDatsArray[j] = diffData;
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
outDats = [.. outDatsArray];
watch.Stop();
@@ -330,7 +364,13 @@ namespace SabreTools.DatTools
// Now, loop through the dictionary and populate the correct DATs
watch.Start("Populating duplicate DAT");
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key =>
#else
foreach (var key in datFile.Items.Keys)
#endif
{
ConcurrentList<DatItem> items = DatItem.Merge(datFile.Items[key]);
@@ -341,7 +381,11 @@ namespace SabreTools.DatTools
// Loop through and add the items correctly
foreach (DatItem item in items)
{
#if NETFRAMEWORK
if ((item.DupeType & DupeType.External) != 0)
#else
if (item.DupeType.HasFlag(DupeType.External))
#endif
{
if (item.Clone() is not DatItem newrom)
continue;
@@ -396,7 +440,13 @@ namespace SabreTools.DatTools
// Loop through each of the inputs and get or create a new DatData object
DatFile[] outDatsArray = new DatFile[inputs.Count];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.For(0, inputs.Count, Globals.ParallelOptions, j =>
#elif NET40_OR_GREATER
Parallel.For(0, inputs.Count, j =>
#else
for (int j = 0; j < inputs.Count; j++)
#endif
{
string innerpost = $" ({j} - {inputs[j].GetNormalizedFileName(true)} Only)";
DatFile diffData = DatFile.Create(datFile.Header);
@@ -405,7 +455,11 @@ namespace SabreTools.DatTools
diffData.Header.Description += innerpost;
diffData.Items = [];
outDatsArray[j] = diffData;
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
// Create a list of DatData objects representing individual output files
List<DatFile> outDats = [.. outDatsArray];
@@ -415,7 +469,13 @@ namespace SabreTools.DatTools
// Now, loop through the dictionary and populate the correct DATs
watch.Start("Populating all individual DATs");
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key =>
#else
foreach (var key in datFile.Items.Keys)
#endif
{
ConcurrentList<DatItem> items = DatItem.Merge(datFile.Items[key]);
@@ -429,7 +489,11 @@ namespace SabreTools.DatTools
if (item.Source == null)
continue;
#if NETFRAMEWORK
if ((item.DupeType & DupeType.Internal) != 0 || item.DupeType == 0x00)
#else
if (item.DupeType.HasFlag(DupeType.Internal) || item.DupeType == 0x00)
#endif
outDats[item.Source.Index].Items.Add(key, item);
}
#if NET40_OR_GREATER || NETCOREAPP
@@ -485,7 +549,13 @@ namespace SabreTools.DatTools
// Now, loop through the dictionary and populate the correct DATs
watch.Start("Populating no duplicate DAT");
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key =>
#else
foreach (var key in datFile.Items.Keys)
#endif
{
ConcurrentList<DatItem> items = DatItem.Merge(datFile.Items[key]);
@@ -496,7 +566,11 @@ namespace SabreTools.DatTools
// Loop through and add the items correctly
foreach (DatItem item in items)
{
#if NETFRAMEWORK
if ((item.DupeType & DupeType.Internal) != 0 || item.DupeType == 0x00)
#else
if (item.DupeType.HasFlag(DupeType.Internal) || item.DupeType == 0x00)
#endif
{
if (item.Clone() is not DatItem newrom || newrom.Source == null)
continue;
@@ -540,13 +614,23 @@ namespace SabreTools.DatTools
InternalStopwatch watch = new("Processing individual DATs");
// Parse all of the DATs into their own DatFiles in the array
#if NET452_OR_GREATER || NETCOREAPP
Parallel.For(0, inputs.Count, Globals.ParallelOptions, i =>
#elif NET40_OR_GREATER
Parallel.For(0, inputs.Count, i =>
#else
for (int i = 0; i < inputs.Count; i++)
#endif
{
var input = inputs[i];
logger.User($"Adding DAT: {input.CurrentPath}");
datFiles[i] = DatFile.Create(datFile.Header.CloneFiltering());
Parser.ParseInto(datFiles[i], input, i, keep: true);
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
watch.Stop();
@@ -560,7 +644,7 @@ namespace SabreTools.DatTools
return datFiles.Select(d => d.Header).ToList();
}
/// <summary>
/// Add items from another DatFile to the existing DatFile
/// </summary>
@@ -596,7 +680,13 @@ namespace SabreTools.DatTools
private static void FillWithSourceIndex(DatFile datFile, DatFile indexDat, int index)
{
// Loop through and add the items for this index to the output
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key =>
#else
foreach (var key in datFile.Items.Keys)
#endif
{
ConcurrentList<DatItem> items = DatItem.Merge(datFile.Items[key]);

View File

@@ -61,7 +61,13 @@ namespace SabreTools.DatTools
List<string> files = Directory.EnumerateFiles(basePath, "*", SearchOption.AllDirectories).ToList();
// Loop through and add the file sizes
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(files, Globals.ParallelOptions, item =>
#elif NET40_OR_GREATER
Parallel.ForEach(files, item =>
#else
foreach (var item in files)
#endif
{
Interlocked.Add(ref totalSize, new FileInfo(item).Length);
#if NET40_OR_GREATER || NETCOREAPP
@@ -132,7 +138,11 @@ namespace SabreTools.DatTools
archive.AvailableHashes = hashes;
// Skip if we're treating archives as files and skipping files
#if NETFRAMEWORK
if ((asFiles & TreatAsFile.Archive) != 0 && skipFileType == SkipFileType.File)
#else
if (asFiles.HasFlag(TreatAsFile.Archive) && skipFileType == SkipFileType.File)
#endif
{
return;
}
@@ -144,7 +154,11 @@ namespace SabreTools.DatTools
}
// Process as archive if we're not treating archives as files
#if NETFRAMEWORK
else if ((!asFiles & TreatAsFile.Archive) != 0)
#else
else if (!asFiles.HasFlag(TreatAsFile.Archive))
#endif
{
var extracted = archive.GetChildren();
@@ -223,7 +237,13 @@ namespace SabreTools.DatTools
string parent = (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, basePath?.Length ?? 0) + Path.GetFileNameWithoutExtension(item);
// First take care of the found items
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(extracted, Globals.ParallelOptions, baseFile =>
#elif NET40_OR_GREATER
Parallel.ForEach(extracted, baseFile =>
#else
foreach (var baseFile in extracted)
#endif
{
DatItem? datItem = DatItem.Create(baseFile);
if (datItem == null)
@@ -256,7 +276,13 @@ namespace SabreTools.DatTools
empties = archive.GetEmptyFolders();
// Add add all of the found empties to the DAT
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(empties, Globals.ParallelOptions, empty =>
#elif NET40_OR_GREATER
Parallel.ForEach(empties, empty =>
#else
foreach (var empty in empties)
#endif
{
Rom emptyRom = new(Path.Combine(empty, "_"), item);
ProcessFileHelper(datFile, item, emptyRom, basePath, parent);
@@ -279,7 +305,13 @@ namespace SabreTools.DatTools
return;
List<string> empties = basePath.ListEmpty() ?? [];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(empties, Globals.ParallelOptions, dir =>
#elif NET40_OR_GREATER
Parallel.ForEach(empties, dir =>
#else
foreach (var dir in empties)
#endif
{
// Get the full path for the directory
string fulldir = Path.GetFullPath(dir);

View File

@@ -76,7 +76,13 @@ namespace SabreTools.DatTools
// Now loop through and get only directories from the input paths
List<string> directories = [];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(inputs, Globals.ParallelOptions, input =>
#elif NET40_OR_GREATER
Parallel.ForEach(inputs, input =>
#else
foreach (var input in inputs)
#endif
{
// Add to the list if the input is a directory
if (Directory.Exists(input))
@@ -316,9 +322,17 @@ namespace SabreTools.DatTools
DatItem? internalDatItem;
if (internalFileInfo == null)
internalDatItem = null;
#if NETFRAMEWORK
else if (internalFileInfo.Type == FileType.AaruFormat && (asFiles & TreatAsFile.AaruFormat) == 0)
#else
else if (internalFileInfo.Type == FileType.AaruFormat && !asFiles.HasFlag(TreatAsFile.AaruFormat))
#endif
internalDatItem = new Media(internalFileInfo);
#if NETFRAMEWORK
else if (internalFileInfo.Type == FileType.CHD && (asFiles & TreatAsFile.CHD) == 0)
#else
else if (internalFileInfo.Type == FileType.CHD && !asFiles.HasFlag(TreatAsFile.CHD))
#endif
internalDatItem = new Disk(internalFileInfo);
else
internalDatItem = new Rom(internalFileInfo);
@@ -713,7 +727,7 @@ namespace SabreTools.DatTools
return outputArchive;
}
/// <summary>
/// Get string value from input OutputFormat
/// </summary>

View File

@@ -63,7 +63,13 @@ namespace SabreTools.DatTools
extBDat.Header.Description += $" ({newExtBString})";
// Now separate the roms accordingly
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key =>
#else
foreach (var key in datFile.Items.Keys)
#endif
{
ConcurrentList<DatItem>? items = datFile.Items[key];
if (items == null)
@@ -151,7 +157,13 @@ namespace SabreTools.DatTools
fieldDats[DatItemField.NULL].Header.Description += " (Other)";
// Now populate each of the DAT objects in turn
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key =>
#else
foreach (var key in datFile.Items.Keys)
#endif
{
ConcurrentList<DatItem>? items = datFile.Items[key];
if (items == null)
@@ -250,7 +262,13 @@ namespace SabreTools.DatTools
keys.Sort(SplitByLevelSort);
// Then, we loop over the games
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(keys, key =>
#else
foreach (var key in keys)
#endif
{
// Here, the key is the name of the game to be used for comparison
if (tempDat.Header.Name != null && tempDat.Header.Name != Path.GetDirectoryName(key))
@@ -354,7 +372,13 @@ namespace SabreTools.DatTools
greaterThan.Header.Description += $" (equal-greater than {radix})";
// Now populate each of the DAT objects in turn
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key =>
#else
foreach (var key in datFile.Items.Keys)
#endif
{
ConcurrentList<DatItem>? items = datFile.Items[key];
if (items == null)
@@ -510,7 +534,13 @@ namespace SabreTools.DatTools
}
// Now populate each of the DAT objects in turn
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(outputTypes, Globals.ParallelOptions, itemType =>
#elif NET40_OR_GREATER
Parallel.ForEach(outputTypes, itemType =>
#else
foreach (var itemType in outputTypes)
#endif
{
FillWithItemType(datFile, typeDats[itemType], itemType);
#if NET40_OR_GREATER || NETCOREAPP
@@ -533,7 +563,13 @@ namespace SabreTools.DatTools
private static void FillWithItemType(DatFile datFile, DatFile indexDat, ItemType itemType)
{
// Loop through and add the items for this index to the output
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.Keys, Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.Keys, key =>
#else
foreach (var key in datFile.Items.Keys)
#endif
{
ConcurrentList<DatItem> items = DatItem.Merge(datFile.Items[key]);

View File

@@ -171,7 +171,13 @@ namespace SabreTools.DatTools
try
{
// Write out all required formats
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(outfiles.Keys, Globals.ParallelOptions, reportFormat =>
#elif NET40_OR_GREATER
Parallel.ForEach(outfiles.Keys, reportFormat =>
#else
foreach (var reportFormat in outfiles.Keys)
#endif
{
string outfile = outfiles[reportFormat];
try
@@ -223,19 +229,39 @@ namespace SabreTools.DatTools
// For each output format, get the appropriate stream writer
output.Add(StatReportFormat.None, CreateOutStatsNamesHelper(outDir, ".null", reportName, overwrite));
#if NETFRAMEWORK
if ((statDatFormat & StatReportFormat.Textfile) != 0)
#else
if (statDatFormat.HasFlag(StatReportFormat.Textfile))
#endif
output.Add(StatReportFormat.Textfile, CreateOutStatsNamesHelper(outDir, ".txt", reportName, overwrite));
#if NETFRAMEWORK
if ((statDatFormat & StatReportFormat.CSV) != 0)
#else
if (statDatFormat.HasFlag(StatReportFormat.CSV))
#endif
output.Add(StatReportFormat.CSV, CreateOutStatsNamesHelper(outDir, ".csv", reportName, overwrite));
#if NETFRAMEWORK
if ((statDatFormat & StatReportFormat.HTML) != 0)
#else
if (statDatFormat.HasFlag(StatReportFormat.HTML))
#endif
output.Add(StatReportFormat.HTML, CreateOutStatsNamesHelper(outDir, ".html", reportName, overwrite));
#if NETFRAMEWORK
if ((statDatFormat & StatReportFormat.SSV) != 0)
#else
if (statDatFormat.HasFlag(StatReportFormat.SSV))
#endif
output.Add(StatReportFormat.SSV, CreateOutStatsNamesHelper(outDir, ".ssv", reportName, overwrite));
#if NETFRAMEWORK
if ((statDatFormat & StatReportFormat.TSV) != 0)
#else
if (statDatFormat.HasFlag(StatReportFormat.TSV))
#endif
output.Add(StatReportFormat.TSV, CreateOutStatsNamesHelper(outDir, ".tsv", reportName, overwrite));
return output;

View File

@@ -78,8 +78,13 @@ namespace SabreTools.DatTools
try
{
// Write out all required formats
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(outfiles.Keys, Globals.ParallelOptions, datFormat =>
#elif NET40_OR_GREATER
Parallel.ForEach(outfiles.Keys, datFormat =>
#else
foreach (var datFormat in outfiles.Keys)
#endif
{
string outfile = outfiles[datFormat];
try
@@ -122,7 +127,7 @@ namespace SabreTools.DatTools
var statsList = new List<DatStatistics>
{
new()
new()
{
Statistics = datFile.Items,
DisplayName = datFile.Header.FileName,
@@ -189,6 +194,6 @@ namespace SabreTools.DatTools
return false;
return true;
}
}
}
}