Reduce more complexity

This commit is contained in:
Matt Nadareski
2024-10-24 04:21:13 -04:00
parent 759a771f4b
commit 292b54b209
4 changed files with 33 additions and 14 deletions

View File

@@ -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);
}
/// <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 = ByteArrayExtensions.ByteArrayToString(hash);
return GetDepotPath(sha1, depth);
}
/// <summary>
/// Get a proper romba sub path
/// </summary>

View File

@@ -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}");
}
}

View File

@@ -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)))

View File

@@ -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