Move SuperDAT out of DatFileTool

This commit is contained in:
Matt Nadareski
2025-02-12 15:36:25 -05:00
parent 262eca5d14
commit 057b0ddd85
3 changed files with 141 additions and 139 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Threading.Tasks;
#endif
@@ -194,142 +193,6 @@ namespace SabreTools.DatFiles
#endregion
#region SuperDAT
/// <summary>
/// Apply SuperDAT naming logic to a merged DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
/// <param name="inputs">List of inputs to use for renaming</param>
public static void ApplySuperDAT(DatFile datFile, List<ParentablePath> inputs)
{
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else
foreach (var key in datFile.Items.SortedKeys)
#endif
{
List<DatItem>? items = datFile.GetItemsForBucket(key);
if (items == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
List<DatItem> newItems = [];
foreach (DatItem item in items)
{
DatItem newItem = item;
var source = newItem.GetFieldValue<Source?>(DatItem.SourceKey);
if (source == null)
continue;
string filename = inputs[source.Index].CurrentPath;
string rootpath = inputs[source.Index].ParentPath ?? string.Empty;
if (rootpath.Length > 0
#if NETFRAMEWORK
&& !rootpath.EndsWith(Path.DirectorySeparatorChar.ToString())
&& !rootpath.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
#else
&& !rootpath.EndsWith(Path.DirectorySeparatorChar)
&& !rootpath.EndsWith(Path.AltDirectorySeparatorChar))
#endif
{
rootpath += Path.DirectorySeparatorChar.ToString();
}
filename = filename.Remove(0, rootpath.Length);
var machine = newItem.GetFieldValue<Machine>(DatItem.MachineKey);
if (machine == null)
continue;
machine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetDirectoryName(filename)
+ Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename)
+ Path.DirectorySeparatorChar
+ machine.GetStringFieldValue(Models.Metadata.Machine.NameKey));
newItems.Add(newItem);
}
datFile.RemoveBucket(key);
newItems.ForEach(item => datFile.AddItem(item, statsOnly: false));
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
/// <summary>
/// Apply SuperDAT naming logic to a merged DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
/// <param name="inputs">List of inputs to use for renaming</param>
public static void ApplySuperDATDB(DatFile datFile, List<ParentablePath> inputs)
{
List<string> keys = [.. datFile.ItemsDB.SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(keys, key =>
#else
foreach (var key in keys)
#endif
{
var items = datFile.GetItemsForBucketDB(key);
if (items == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
foreach (var item in items)
{
var source = datFile.ItemsDB.GetSourceForItem(item.Key);
if (source.Value == null)
continue;
var machine = datFile.ItemsDB.GetMachineForItem(item.Key);
if (machine.Value == null)
continue;
string filename = inputs[source.Value.Index].CurrentPath;
string rootpath = inputs[source.Value.Index].ParentPath ?? string.Empty;
if (rootpath.Length > 0
#if NETFRAMEWORK
&& !rootpath!.EndsWith(Path.DirectorySeparatorChar.ToString())
&& !rootpath!.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
#else
&& !rootpath.EndsWith(Path.DirectorySeparatorChar)
&& !rootpath.EndsWith(Path.AltDirectorySeparatorChar))
#endif
{
rootpath += Path.DirectorySeparatorChar.ToString();
}
filename = filename.Remove(0, rootpath.Length);
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename) + Path.DirectorySeparatorChar
+ machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey));
}
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
#endregion
#region Population
/// <summary>

View File

@@ -1,6 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
#if NET40_OR_GREATER || NETCOREAPP
using System.Threading.Tasks;
#endif
using SabreTools.Core.Tools;
using SabreTools.DatFiles;
using SabreTools.DatItems;
using SabreTools.IO;
using SabreTools.IO.Logging;
namespace SabreTools.DatTools
@@ -86,6 +93,138 @@ namespace SabreTools.DatTools
return true;
}
/// <summary>
/// Apply SuperDAT naming logic to a merged DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
/// <param name="inputs">List of inputs to use for renaming</param>
public static void ApplySuperDAT(DatFile datFile, List<ParentablePath> inputs)
{
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(datFile.Items.SortedKeys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(datFile.Items.SortedKeys, key =>
#else
foreach (var key in datFile.Items.SortedKeys)
#endif
{
List<DatItem>? items = datFile.GetItemsForBucket(key);
if (items == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
List<DatItem> newItems = [];
foreach (DatItem item in items)
{
DatItem newItem = item;
var source = newItem.GetFieldValue<Source?>(DatItem.SourceKey);
if (source == null)
continue;
string filename = inputs[source.Index].CurrentPath;
string rootpath = inputs[source.Index].ParentPath ?? string.Empty;
if (rootpath.Length > 0
#if NETFRAMEWORK
&& !rootpath.EndsWith(Path.DirectorySeparatorChar.ToString())
&& !rootpath.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
#else
&& !rootpath.EndsWith(Path.DirectorySeparatorChar)
&& !rootpath.EndsWith(Path.AltDirectorySeparatorChar))
#endif
{
rootpath += Path.DirectorySeparatorChar.ToString();
}
filename = filename.Remove(0, rootpath.Length);
var machine = newItem.GetFieldValue<Machine>(DatItem.MachineKey);
if (machine == null)
continue;
machine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetDirectoryName(filename)
+ Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename)
+ Path.DirectorySeparatorChar
+ machine.GetStringFieldValue(Models.Metadata.Machine.NameKey));
newItems.Add(newItem);
}
datFile.RemoveBucket(key);
newItems.ForEach(item => datFile.AddItem(item, statsOnly: false));
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
/// <summary>
/// Apply SuperDAT naming logic to a merged DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
/// <param name="inputs">List of inputs to use for renaming</param>
public static void ApplySuperDATDB(DatFile datFile, List<ParentablePath> inputs)
{
List<string> keys = [.. datFile.ItemsDB.SortedKeys];
#if NET452_OR_GREATER || NETCOREAPP
Parallel.ForEach(keys, Core.Globals.ParallelOptions, key =>
#elif NET40_OR_GREATER
Parallel.ForEach(keys, key =>
#else
foreach (var key in keys)
#endif
{
var items = datFile.GetItemsForBucketDB(key);
if (items == null)
#if NET40_OR_GREATER || NETCOREAPP
return;
#else
continue;
#endif
foreach (var item in items)
{
var source = datFile.ItemsDB.GetSourceForItem(item.Key);
if (source.Value == null)
continue;
var machine = datFile.ItemsDB.GetMachineForItem(item.Key);
if (machine.Value == null)
continue;
string filename = inputs[source.Value.Index].CurrentPath;
string rootpath = inputs[source.Value.Index].ParentPath ?? string.Empty;
if (rootpath.Length > 0
#if NETFRAMEWORK
&& !rootpath!.EndsWith(Path.DirectorySeparatorChar.ToString())
&& !rootpath!.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
#else
&& !rootpath.EndsWith(Path.DirectorySeparatorChar)
&& !rootpath.EndsWith(Path.AltDirectorySeparatorChar))
#endif
{
rootpath += Path.DirectorySeparatorChar.ToString();
}
filename = filename.Remove(0, rootpath.Length);
machine.Value.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename) + Path.DirectorySeparatorChar
+ machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey));
}
#if NET40_OR_GREATER || NETCOREAPP
});
#else
}
#endif
}
#endregion
}
}

View File

@@ -373,8 +373,8 @@ namespace SabreTools.Features
// If we're in SuperDAT mode, prefix all games with their respective DATs
if (string.Equals(userInputDat.Header.GetStringFieldValue(Models.Metadata.Header.TypeKey), "SuperDAT", StringComparison.OrdinalIgnoreCase))
{
DatFileTool.ApplySuperDAT(userInputDat, inputPaths);
//DatFileTool.ApplySuperDATDB(userInputDat, inputPaths);
MergeSplit.ApplySuperDAT(userInputDat, inputPaths);
//MergeSplit.ApplySuperDATDB(userInputDat, inputPaths);
}
Writer.Write(userInputDat, OutputDir);