Uid.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Horizon.Sdk.Account
  6. {
  7. [StructLayout(LayoutKind.Sequential)]
  8. readonly record struct Uid
  9. {
  10. public readonly long High;
  11. public readonly long Low;
  12. public bool IsNull => (Low | High) == 0;
  13. public static Uid Null => new(0, 0);
  14. public Uid(long low, long high)
  15. {
  16. Low = low;
  17. High = high;
  18. }
  19. public Uid(byte[] bytes)
  20. {
  21. High = BitConverter.ToInt64(bytes, 0);
  22. Low = BitConverter.ToInt64(bytes, 8);
  23. }
  24. public Uid(string hex)
  25. {
  26. if (hex == null || hex.Length != 32 || !hex.All("0123456789abcdefABCDEF".Contains))
  27. {
  28. throw new ArgumentException("Invalid Hex value!", nameof(hex));
  29. }
  30. Low = Convert.ToInt64(hex[16..], 16);
  31. High = Convert.ToInt64(hex[..16], 16);
  32. }
  33. public void Write(BinaryWriter binaryWriter)
  34. {
  35. binaryWriter.Write(High);
  36. binaryWriter.Write(Low);
  37. }
  38. public override string ToString()
  39. {
  40. return High.ToString("x16") + Low.ToString("x16");
  41. }
  42. public LibHac.Account.Uid ToLibHacUid()
  43. {
  44. return new LibHac.Account.Uid((ulong)High, (ulong)Low);
  45. }
  46. public UInt128 ToUInt128()
  47. {
  48. return new UInt128((ulong)High, (ulong)Low);
  49. }
  50. }
  51. }