mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-07-08 18:06:41 +00:00
DepotInformation should be responsible for path generation
This commit is contained in:
47
SabreTools.Metadata.DatFiles.Test/DepotInformationTests.cs
Normal file
47
SabreTools.Metadata.DatFiles.Test/DepotInformationTests.cs
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
public int Depth { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Maximum depth allowed for path generation
|
||||
/// </summary>
|
||||
private static readonly int MaximumDepth = HashType.SHA1.ZeroBytes.Length;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Get a proper romba subpath
|
||||
/// </summary>
|
||||
/// <param name="hash">SHA-1 hash to get the path for</param>
|
||||
/// <param name="depth">Positive value representing the depth of the depot</param>
|
||||
/// <returns>Subfolder path for the given hash, including the filename and .gz extension</returns>
|
||||
public string? GetDepotPath(byte[]? hash)
|
||||
{
|
||||
string? sha1 = hash.ToHexString();
|
||||
return GetDepotPath(sha1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a proper romba subpath
|
||||
/// </summary>
|
||||
/// <param name="hash">SHA-1 hash to get the path for</param>
|
||||
/// <returns>Subfolder path for the given hash, including the filename and .gz extension</returns>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.IO;
|
||||
using SabreTools.Hashing;
|
||||
using SabreTools.Text.Extensions;
|
||||
|
||||
namespace SabreTools.Metadata
|
||||
{
|
||||
@@ -9,53 +7,7 @@ namespace SabreTools.Metadata
|
||||
/// </summary>
|
||||
public static class Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Get a proper romba sub path
|
||||
/// </summary>
|
||||
/// <param name="hash">SHA-1 hash to get the path for</param>
|
||||
/// <param name="depth">Positive value representing the depth of the depot</param>
|
||||
/// <returns>Subfolder path for the given hash</returns>
|
||||
public static string? GetDepotPath(byte[]? hash, int depth)
|
||||
{
|
||||
string? sha1 = hash.ToHexString();
|
||||
return GetDepotPath(sha1, depth);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a proper romba sub path
|
||||
/// </summary>
|
||||
/// <param name="hash">SHA-1 hash to get the path for</param>
|
||||
/// <param name="depth">Positive value representing the depth of the depot</param>
|
||||
/// <returns>Subfolder path for the given hash</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// Get if the given path has a valid DAT extension
|
||||
/// </summary>
|
||||
/// <param name="path">Path to check</param>
|
||||
|
||||
Reference in New Issue
Block a user