Files
TinfoilVibeServer/TinfoilVibeServer/Utilities/IdHelper.cs
T
ecenshu c260ebd566
Build & Push Docker image / build-and-push (push) Successful in 5m39s
ci / build_linux (push) Successful in 4m36s
If filename can extract to a NcaMetadata entry, don't use nspextractor to pull information (#3)
Scan directories sequentially to reduce memory footprint

Reviewed-on: #3
Co-authored-by: Huy Nguyen <ecenshu@gmail.com>
Co-committed-by: Huy Nguyen <ecenshu@gmail.com>
2025-11-15 06:59:25 +00:00

78 lines
2.3 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using System.Text.RegularExpressions;
namespace TinfoilVibeServer.Utilities;
public static class IdHelper
{
public static string ToStrId(this int num)
{
return ToIdFromConvertedNumBytes(BitConverter.GetBytes(num));
}
public static string ToStrId(this uint num)
{
return ToIdFromConvertedNumBytes(BitConverter.GetBytes(num));
}
public static string ToStrId(this long num)
{
return ToIdFromConvertedNumBytes(BitConverter.GetBytes(num));
}
public static string ToStrId(this ulong num)
{
return ToIdFromConvertedNumBytes(BitConverter.GetBytes(num));
}
public static string ToStrId(this IEnumerable<byte> bytes)
{
return bytes.Aggregate("", (current, b) => current + b.ToString("X2"));
}
public static string ToStrId(this Span<byte> bytes)
{
return bytes.ToArray().ToStrId();
}
private static string ToIdFromConvertedNumBytes(IEnumerable<byte> getBytes)
{
return ToStrId(getBytes.Reverse());
}
public static string GetTitleId(this FileInfo fileInfo)
{
var match = Regex.Match(fileInfo.Name, "^.*\\[(\\w{16})\\].*\\.nsp$");
return match is { Length: > 0, Groups.Count: > 1 }?match.Groups[1].Value:string.Empty;
}
#region 1 From hex string byte[] (bigendian)
/// <summary>
/// Turns an evenlength hex string into a byte array in **bigendian** order
/// (the same order that the original <c>ToStrId</c> creates).
/// </summary>
private static byte[] HexStringToBytes(string hex)
{
if (string.IsNullOrWhiteSpace(hex))
throw new ArgumentException("ID string cannot be empty", nameof(hex));
if (hex.Length % 2 != 0)
throw new ArgumentException("ID string must contain an even number of hex digits", nameof(hex));
var bytes = new byte[hex.Length / 2];
for (var i = 0; i < bytes.Length; i++)
{
var chunk = hex.Substring(i * 2, 2);
bytes[i] = byte.Parse(chunk, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return bytes;
}
#endregion
public static long ToLongFromStrId(this string hex) =>
BitConverter.ToInt64(HexStringToBytes(hex).Reverse().ToArray(), 0);
}