From 292b54b2098d4a9376fbd76d5b2ec26c9d254205 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 24 Oct 2024 04:21:13 -0400 Subject: [PATCH] Reduce more complexity --- SabreTools.Core/Tools/Utilities.cs | 13 +++++++++ SabreTools.DatFiles/DatFile.cs | 30 ++++++++++++-------- SabreTools.FileTypes/Archives/GZipArchive.cs | 2 +- SabreTools.FileTypes/Archives/XZArchive.cs | 2 +- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/SabreTools.Core/Tools/Utilities.cs b/SabreTools.Core/Tools/Utilities.cs index f959bfe1..41d81aa2 100644 --- a/SabreTools.Core/Tools/Utilities.cs +++ b/SabreTools.Core/Tools/Utilities.cs @@ -2,6 +2,7 @@ using System.IO; using System.Linq; using SabreTools.Hashing; +using SabreTools.IO.Extensions; using SabreTools.Matching; namespace SabreTools.Core.Tools @@ -45,6 +46,18 @@ namespace SabreTools.Core.Tools return string.Equals(firstHash, secondHash, StringComparison.OrdinalIgnoreCase); } + /// + /// Get a proper romba sub path + /// + /// SHA-1 hash to get the path for + /// Positive value representing the depth of the depot + /// Subfolder path for the given hash + public static string? GetDepotPath(byte[]? hash, int depth) + { + string? sha1 = ByteArrayExtensions.ByteArrayToString(hash); + return GetDepotPath(sha1, depth); + } + /// /// Get a proper romba sub path /// diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index 12996112..e5a54186 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -467,27 +467,30 @@ namespace SabreTools.DatFiles if (item is Disk disk) { // We can only write out if there's a SHA-1 - if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key))) + string? sha1 = disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key); + if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key), outputDepot.Depth)?.Replace('\\', '/'); + name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/'); item.SetName($"{pre}{name}{post}"); } } else if (item is Media media) { // We can only write out if there's a SHA-1 - if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key))) + string? sha1 = media.GetStringFieldValue(Models.Metadata.Media.SHA1Key); + if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key), outputDepot.Depth)?.Replace('\\', '/'); + name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/'); item.SetName($"{pre}{name}{post}"); } } else if (item is Rom rom) { // We can only write out if there's a SHA-1 - if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key))) + string? sha1 = rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key); + if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key), outputDepot.Depth)?.Replace('\\', '/'); + name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/'); item.SetName($"{pre}{name}{post}"); } } @@ -567,27 +570,30 @@ namespace SabreTools.DatFiles if (item.Item2 is Disk disk) { // We can only write out if there's a SHA-1 - if (!string.IsNullOrEmpty(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key))) + string? sha1 = disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key); + if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(disk.GetStringFieldValue(Models.Metadata.Disk.SHA1Key), outputDepot.Depth)?.Replace('\\', '/'); + name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/'); item.Item2.SetName($"{pre}{name}{post}"); } } else if (item.Item2 is Media media) { // We can only write out if there's a SHA-1 - if (!string.IsNullOrEmpty(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key))) + string? sha1 = media.GetStringFieldValue(Models.Metadata.Media.SHA1Key); + if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(media.GetStringFieldValue(Models.Metadata.Media.SHA1Key), outputDepot.Depth)?.Replace('\\', '/'); + name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/'); item.Item2.SetName($"{pre}{name}{post}"); } } else if (item.Item2 is Rom rom) { // We can only write out if there's a SHA-1 - if (!string.IsNullOrEmpty(rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key))) + string? sha1 = rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key); + if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(rom.GetStringFieldValue(Models.Metadata.Rom.SHA1Key), outputDepot.Depth)?.Replace('\\', '/'); + name = Utilities.GetDepotPath(sha1, outputDepot.Depth)?.Replace('\\', '/'); item.Item2.SetName($"{pre}{name}{post}"); } } diff --git a/SabreTools.FileTypes/Archives/GZipArchive.cs b/SabreTools.FileTypes/Archives/GZipArchive.cs index 0288c1b9..69878bac 100644 --- a/SabreTools.FileTypes/Archives/GZipArchive.cs +++ b/SabreTools.FileTypes/Archives/GZipArchive.cs @@ -446,7 +446,7 @@ namespace SabreTools.FileTypes.Archives baseFile = GetInfo(inputStream, keepReadOpen: true); // Get the output file name - string outfile = Path.Combine(outDir, Utilities.GetDepotPath(ByteArrayExtensions.ByteArrayToString(baseFile.SHA1), Depth) ?? string.Empty); + string outfile = Path.Combine(outDir, Utilities.GetDepotPath(baseFile.SHA1, Depth) ?? string.Empty); // Check to see if the folder needs to be created if (!Directory.Exists(Path.GetDirectoryName(outfile))) diff --git a/SabreTools.FileTypes/Archives/XZArchive.cs b/SabreTools.FileTypes/Archives/XZArchive.cs index a18d7795..87282737 100644 --- a/SabreTools.FileTypes/Archives/XZArchive.cs +++ b/SabreTools.FileTypes/Archives/XZArchive.cs @@ -334,7 +334,7 @@ namespace SabreTools.FileTypes.Archives baseFile = GetInfo(inputStream, keepReadOpen: true); // Get the output file name - string outfile = Path.Combine(outDir, Utilities.GetDepotPath(ByteArrayExtensions.ByteArrayToString(baseFile.SHA1), Depth)!); + string outfile = Path.Combine(outDir, Utilities.GetDepotPath(baseFile.SHA1, Depth)!); outfile = outfile.Replace(".gz", ".xz"); // Check to see if the folder needs to be created