ServiceName.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Ryujinx.Horizon.Sdk.Sm
  4. {
  5. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  6. struct ServiceName
  7. {
  8. public static ServiceName Invalid { get; } = new ServiceName(0);
  9. public bool IsValid => Packed != 0;
  10. public int Length => sizeof(ulong);
  11. public ulong Packed { get; }
  12. public byte this[int index]
  13. {
  14. get
  15. {
  16. if ((uint)index >= sizeof(ulong))
  17. {
  18. throw new IndexOutOfRangeException();
  19. }
  20. return (byte)(Packed >> (index * 8));
  21. }
  22. }
  23. private ServiceName(ulong packed)
  24. {
  25. Packed = packed;
  26. }
  27. public static ServiceName Encode(string name)
  28. {
  29. ulong packed = 0;
  30. for (int index = 0; index < sizeof(ulong); index++)
  31. {
  32. if (index < name.Length)
  33. {
  34. packed |= (ulong)(byte)name[index] << (index * 8);
  35. }
  36. else
  37. {
  38. break;
  39. }
  40. }
  41. return new ServiceName(packed);
  42. }
  43. public override bool Equals(object obj)
  44. {
  45. return obj is ServiceName serviceName && serviceName.Equals(this);
  46. }
  47. public bool Equals(ServiceName other)
  48. {
  49. return other.Packed == Packed;
  50. }
  51. public override int GetHashCode()
  52. {
  53. return Packed.GetHashCode();
  54. }
  55. public static bool operator ==(ServiceName lhs, ServiceName rhs)
  56. {
  57. return lhs.Equals(rhs);
  58. }
  59. public static bool operator !=(ServiceName lhs, ServiceName rhs)
  60. {
  61. return !lhs.Equals(rhs);
  62. }
  63. public override string ToString()
  64. {
  65. string name = string.Empty;
  66. for (int index = 0; index < sizeof(ulong); index++)
  67. {
  68. byte character = (byte)(Packed >> (index * 8));
  69. if (character == 0)
  70. {
  71. break;
  72. }
  73. name += (char)character;
  74. }
  75. return name;
  76. }
  77. }
  78. }