UserId.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Ryujinx.HLE.Utilities;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. namespace Ryujinx.HLE.HOS.SystemState
  6. {
  7. public struct UserId
  8. {
  9. public string UserIdHex { get; private set; }
  10. public byte[] Bytes { get; private set; }
  11. public UserId(long Low, long High)
  12. {
  13. if ((Low | High) == 0)
  14. {
  15. throw new ArgumentException("Zero is not a valid user id!");
  16. }
  17. byte[] Bytes = new byte[16];
  18. int Index = Bytes.Length;
  19. void WriteBytes(long Value)
  20. {
  21. for (int Byte = 0; Byte < 8; Byte++)
  22. {
  23. Bytes[--Index] = (byte)(Value >> Byte * 8);
  24. }
  25. }
  26. WriteBytes(Low);
  27. WriteBytes(High);
  28. UserIdHex = string.Empty;
  29. foreach (byte Byte in Bytes)
  30. {
  31. UserIdHex += Byte.ToString("X2");
  32. }
  33. this.Bytes = Bytes;
  34. }
  35. public UserId(string UserIdHex)
  36. {
  37. if (UserIdHex == null || UserIdHex.Length != 32 || !UserIdHex.All("0123456789abcdefABCDEF".Contains))
  38. {
  39. throw new ArgumentException("Invalid user id!", nameof(UserIdHex));
  40. }
  41. if (UserIdHex == "00000000000000000000000000000000")
  42. {
  43. throw new ArgumentException("Zero is not a valid user id!", nameof(UserIdHex));
  44. }
  45. this.UserIdHex = UserIdHex.ToUpper();
  46. Bytes = StringUtils.HexToBytes(UserIdHex);
  47. }
  48. internal void Write(BinaryWriter Writer)
  49. {
  50. for (int Index = Bytes.Length - 1; Index >= 0; Index--)
  51. {
  52. Writer.Write(Bytes[Index]);
  53. }
  54. }
  55. public override string ToString()
  56. {
  57. return UserIdHex;
  58. }
  59. }
  60. }