Files
SabreTools/SabreTools.Core/Tools/Utilities.cs

116 lines
4.0 KiB
C#
Raw Normal View History

2023-08-14 18:43:56 -04:00
using System;
using System.IO;
using System.Linq;
2019-02-08 15:31:44 -08:00
namespace SabreTools.Core.Tools
{
/// <summary>
/// Static utility functions used throughout the library
/// </summary>
public static class Utilities
{
2023-08-14 18:43:56 -04:00
/// <summary>
/// Determine if two hashes are equal for the purposes of merging
/// </summary>
public static bool ConditionalHashEquals(byte[]? firstHash, byte[]? secondHash)
{
// If either hash is empty, we say they're equal for merging
if (firstHash.IsNullOrEmpty() || secondHash.IsNullOrEmpty())
return true;
// If they're different sizes, they can't match
if (firstHash!.Length != secondHash!.Length)
return false;
// Otherwise, they need to match exactly
return Enumerable.SequenceEqual(firstHash, secondHash);
}
/// <summary>
/// Determine if two hashes are equal for the purposes of merging
/// </summary>
public static bool ConditionalHashEquals(string? firstHash, string? secondHash)
{
// If either hash is empty, we say they're equal for merging
if (string.IsNullOrWhiteSpace(firstHash) || string.IsNullOrWhiteSpace(secondHash))
return true;
// If they're different sizes, they can't match
if (firstHash!.Length != secondHash!.Length)
return false;
// Otherwise, they need to match exactly
return string.Equals(firstHash, secondHash, StringComparison.OrdinalIgnoreCase);
}
2020-12-10 22:16:53 -08:00
/// <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)
2020-12-10 22:16:53 -08:00
{
2020-12-18 14:22:56 -08:00
// If the hash is null or empty, then we return null
if (string.IsNullOrEmpty(hash))
return null;
2020-12-10 22:16:53 -08:00
// If the hash isn't the right size, then we return null
if (hash.Length != Constants.SHA1Length)
return null;
// Cap the depth between 0 and 20, for now
if (depth < 0)
depth = 0;
else if (depth > Constants.SHA1ZeroBytes.Length)
depth = Constants.SHA1ZeroBytes.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;
}
2020-12-10 23:24:09 -08:00
/// <summary>
/// Get if the given path has a valid DAT extension
/// </summary>
/// <param name="path">Path to check</param>
/// <returns>True if the extension is valid, false otherwise</returns>
public static bool HasValidDatExtension(string? path)
2020-12-10 23:24:09 -08:00
{
2020-12-18 14:22:56 -08:00
// If the path is null or empty, then we return false
if (string.IsNullOrEmpty(path))
return false;
2020-12-10 23:24:09 -08:00
// Get the extension from the path, if possible
2020-12-18 14:22:56 -08:00
string ext = Path.GetExtension(path).TrimStart('.').ToLowerInvariant();
2020-12-10 23:24:09 -08:00
// Check against the list of known DAT extensions
return ext switch
2020-12-10 23:24:09 -08:00
{
"csv" => true,
"dat" => true,
"json" => true,
"md5" => true,
"sfv" => true,
"sha1" => true,
"sha256" => true,
"sha384" => true,
"sha512" => true,
"spamsum" => true,
"ssv" => true,
"tsv" => true,
"txt" => true,
"xml" => true,
_ => false,
};
2020-12-10 23:24:09 -08:00
}
2019-02-08 15:31:44 -08:00
}
}