diff --git a/SabreTools.DatFiles/DatFileTool.cs b/SabreTools.DatFiles/DatFileTool.cs index 167a3ea2..c5982354 100644 --- a/SabreTools.DatFiles/DatFileTool.cs +++ b/SabreTools.DatFiles/DatFileTool.cs @@ -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 - - /// - /// Apply SuperDAT naming logic to a merged DatFile - /// - /// Current DatFile object to run operations on - /// List of inputs to use for renaming - public static void ApplySuperDAT(DatFile datFile, List 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? items = datFile.GetItemsForBucket(key); - if (items == null) -#if NET40_OR_GREATER || NETCOREAPP - return; -#else - continue; -#endif - - List newItems = []; - foreach (DatItem item in items) - { - DatItem newItem = item; - var source = newItem.GetFieldValue(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(DatItem.MachineKey); - if (machine == null) - continue; - - machine.SetFieldValue(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 - } - - /// - /// Apply SuperDAT naming logic to a merged DatFile - /// - /// Current DatFile object to run operations on - /// List of inputs to use for renaming - public static void ApplySuperDATDB(DatFile datFile, List inputs) - { - List 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(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 /// diff --git a/SabreTools.DatTools/MergeSplit.cs b/SabreTools.DatTools/MergeSplit.cs index ee4d0b54..06420f3f 100644 --- a/SabreTools.DatTools/MergeSplit.cs +++ b/SabreTools.DatTools/MergeSplit.cs @@ -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; } + /// + /// Apply SuperDAT naming logic to a merged DatFile + /// + /// Current DatFile object to run operations on + /// List of inputs to use for renaming + public static void ApplySuperDAT(DatFile datFile, List 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? items = datFile.GetItemsForBucket(key); + if (items == null) +#if NET40_OR_GREATER || NETCOREAPP + return; +#else + continue; +#endif + + List newItems = []; + foreach (DatItem item in items) + { + DatItem newItem = item; + var source = newItem.GetFieldValue(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(DatItem.MachineKey); + if (machine == null) + continue; + + machine.SetFieldValue(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 + } + + /// + /// Apply SuperDAT naming logic to a merged DatFile + /// + /// Current DatFile object to run operations on + /// List of inputs to use for renaming + public static void ApplySuperDATDB(DatFile datFile, List inputs) + { + List 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(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 } } diff --git a/SabreTools/Features/Update.cs b/SabreTools/Features/Update.cs index 7cf94608..d3597d48 100644 --- a/SabreTools/Features/Update.cs +++ b/SabreTools/Features/Update.cs @@ -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);