diff --git a/SabreTools.Metadata.DatFiles.Test/DepotInformationTests.cs b/SabreTools.Metadata.DatFiles.Test/DepotInformationTests.cs new file mode 100644 index 00000000..1a3ad828 --- /dev/null +++ b/SabreTools.Metadata.DatFiles.Test/DepotInformationTests.cs @@ -0,0 +1,47 @@ +using Xunit; + +namespace SabreTools.Metadata.DatFiles.Test +{ + public class DepotInformationTests + { + #region GetDepotPath + + [Theory] + [InlineData(null, 0, null)] + [InlineData(null, 4, null)] + [InlineData(new byte[] { 0x12, 0x34, 0x56 }, 0, null)] + [InlineData(new byte[] { 0x12, 0x34, 0x56 }, 4, null)] + [InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, -1, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + [InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + [InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, 1, "da\\da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + public void GetDepotPath_Array(byte[]? hash, int depth, string? expected) + { + var depot = new DepotInformation(isActive: true, depth: depth); + string? actual = depot.GetDepotPath(hash); + if (System.IO.Path.DirectorySeparatorChar == '/') + expected = expected?.Replace('\\', '/'); + + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(null, 0, null)] + [InlineData(null, 4, null)] + [InlineData("123456", 0, null)] + [InlineData("123456", 4, null)] + [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", -1, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", 1, "da\\da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] + public void GetDepotPath_String(string? hash, int depth, string? expected) + { + var depot = new DepotInformation(isActive: true, depth: depth); + string? actual = depot.GetDepotPath(hash); + if (System.IO.Path.DirectorySeparatorChar == '/') + expected = expected?.Replace('\\', '/'); + + Assert.Equal(expected, actual); + } + + #endregion + } +} diff --git a/SabreTools.Metadata.DatFiles/DatFile.cs b/SabreTools.Metadata.DatFiles/DatFile.cs index 11818383..1a17abd5 100644 --- a/SabreTools.Metadata.DatFiles/DatFile.cs +++ b/SabreTools.Metadata.DatFiles/DatFile.cs @@ -506,7 +506,7 @@ namespace SabreTools.Metadata.DatFiles string? sha1 = disk.SHA1; if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(sha1, Modifiers.OutputDepot.Depth)?.Replace('\\', '/'); + name = Modifiers.OutputDepot.GetDepotPath(sha1)?.Replace('\\', '/'); disk.Name = $"{pre}{name}{post}"; } } @@ -516,7 +516,7 @@ namespace SabreTools.Metadata.DatFiles string? sha1 = media.SHA1; if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(sha1, Modifiers.OutputDepot.Depth)?.Replace('\\', '/'); + name = Modifiers.OutputDepot.GetDepotPath(sha1)?.Replace('\\', '/'); media.Name = $"{pre}{name}{post}"; } } @@ -526,7 +526,7 @@ namespace SabreTools.Metadata.DatFiles string? sha1 = rom.SHA1; if (!string.IsNullOrEmpty(sha1)) { - name = Utilities.GetDepotPath(sha1, Modifiers.OutputDepot.Depth)?.Replace('\\', '/'); + name = Modifiers.OutputDepot.GetDepotPath(sha1)?.Replace('\\', '/'); rom.Name = $"{pre}{name}{post}"; } } diff --git a/SabreTools.Metadata.DatFiles/DepotInformation.cs b/SabreTools.Metadata.DatFiles/DepotInformation.cs index 219b398d..4188f458 100644 --- a/SabreTools.Metadata.DatFiles/DepotInformation.cs +++ b/SabreTools.Metadata.DatFiles/DepotInformation.cs @@ -1,5 +1,7 @@ using System; +using System.IO; using SabreTools.Hashing; +using SabreTools.Text.Extensions; namespace SabreTools.Metadata.DatFiles { @@ -23,6 +25,11 @@ namespace SabreTools.Metadata.DatFiles /// public int Depth { get; } + /// + /// Maximum depth allowed for path generation + /// + private static readonly int MaximumDepth = HashType.SHA1.ZeroBytes.Length; + /// /// Constructor /// @@ -50,8 +57,8 @@ namespace SabreTools.Metadata.DatFiles Depth = 4; else if (Depth < 0) Depth = 0; - else if (Depth > HashType.SHA1.ZeroBytes.Length) - Depth = HashType.SHA1.ZeroBytes.Length; + else if (Depth > MaximumDepth) + Depth = MaximumDepth; } #region Cloning @@ -62,5 +69,55 @@ namespace SabreTools.Metadata.DatFiles public object Clone() => new DepotInformation(Name, IsActive, Depth); #endregion + + #region Path Generation + + /// + /// Get a proper romba subpath + /// + /// SHA-1 hash to get the path for + /// Positive value representing the depth of the depot + /// Subfolder path for the given hash, including the filename and .gz extension + public string? GetDepotPath(byte[]? hash) + { + string? sha1 = hash.ToHexString(); + return GetDepotPath(sha1); + } + + /// + /// Get a proper romba subpath + /// + /// SHA-1 hash to get the path for + /// Subfolder path for the given hash, including the filename and .gz extension + public string? GetDepotPath(string? hash) + { + // If the hash is null, then we return null + if (hash is null) + return null; + + // If the hash isn't the right size, then we return null + if (hash.Length != HashType.SHA1.ZeroString.Length) + return null; + + // Cap the depth between 0 and 20, for now + int depth = Depth; + if (depth < 0) + depth = 0; + else if (depth > MaximumDepth) + depth = MaximumDepth; + + // Loop through and generate the subdirectory + string path = string.Empty; + for (int i = 0; i < depth; i++) + { + path += hash.Substring(i * 2, 2) + Path.DirectorySeparatorChar; + } + + // Now append the filename + path += $"{hash}.gz"; + return path; + } + + #endregion } } diff --git a/SabreTools.Metadata.Test/UtilitiesTests.cs b/SabreTools.Metadata.Test/UtilitiesTests.cs index fdc34b8d..1344505c 100644 --- a/SabreTools.Metadata.Test/UtilitiesTests.cs +++ b/SabreTools.Metadata.Test/UtilitiesTests.cs @@ -4,44 +4,6 @@ namespace SabreTools.Metadata.Test { public class UtilitiesTests { - #region GetDepotPath - - [Theory] - [InlineData(null, 0, null)] - [InlineData(null, 4, null)] - [InlineData(new byte[] { 0x12, 0x34, 0x56 }, 0, null)] - [InlineData(new byte[] { 0x12, 0x34, 0x56 }, 4, null)] - [InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, -1, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] - [InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] - [InlineData(new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }, 1, "da\\da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] - public void GetDepotPath_Array(byte[]? hash, int depth, string? expected) - { - string? actual = Utilities.GetDepotPath(hash, depth); - if (System.IO.Path.DirectorySeparatorChar == '/') - expected = expected?.Replace('\\', '/'); - - Assert.Equal(expected, actual); - } - - [Theory] - [InlineData(null, 0, null)] - [InlineData(null, 4, null)] - [InlineData("123456", 0, null)] - [InlineData("123456", 4, null)] - [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", -1, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] - [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", 0, "da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] - [InlineData("da39a3ee5e6b4b0d3255bfef95601890afd80709", 1, "da\\da39a3ee5e6b4b0d3255bfef95601890afd80709.gz")] - public void GetDepotPath_String(string? hash, int depth, string? expected) - { - string? actual = Utilities.GetDepotPath(hash, depth); - if (System.IO.Path.DirectorySeparatorChar == '/') - expected = expected?.Replace('\\', '/'); - - Assert.Equal(expected, actual); - } - - #endregion - #region HasValidDatExtension [Theory] diff --git a/SabreTools.Metadata/Utilities.cs b/SabreTools.Metadata/Utilities.cs index 966e60d1..71a9516b 100644 --- a/SabreTools.Metadata/Utilities.cs +++ b/SabreTools.Metadata/Utilities.cs @@ -1,6 +1,4 @@ using System.IO; -using SabreTools.Hashing; -using SabreTools.Text.Extensions; namespace SabreTools.Metadata { @@ -9,53 +7,7 @@ namespace SabreTools.Metadata /// public static class Utilities { - /// - /// 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 = hash.ToHexString(); - return GetDepotPath(sha1, depth); - } - - /// - /// 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(string? hash, int depth) - { - // If the hash is null, then we return null - if (hash is null) - return null; - - // If the hash isn't the right size, then we return null - if (hash.Length != HashType.SHA1.ZeroString.Length) - return null; - - // Cap the depth between 0 and 20, for now - if (depth < 0) - depth = 0; - else if (depth > HashType.SHA1.ZeroBytes.Length) - depth = HashType.SHA1.ZeroBytes.Length; - - // Loop through and generate the subdirectory - string path = string.Empty; - for (int i = 0; i < depth; i++) - { - path += hash.Substring(i * 2, 2) + Path.DirectorySeparatorChar; - } - - // Now append the filename - path += $"{hash}.gz"; - return path; - } - - /// + /// /// Get if the given path has a valid DAT extension /// /// Path to check