IpcHandleDesc.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.IO;
  3. namespace Ryujinx.HLE.HOS.Ipc
  4. {
  5. class IpcHandleDesc
  6. {
  7. public bool HasPId { get; private set; }
  8. public long PId { get; private set; }
  9. public int[] ToCopy { get; private set; }
  10. public int[] ToMove { get; private set; }
  11. public IpcHandleDesc(BinaryReader reader)
  12. {
  13. int word = reader.ReadInt32();
  14. HasPId = (word & 1) != 0;
  15. ToCopy = new int[(word >> 1) & 0xf];
  16. ToMove = new int[(word >> 5) & 0xf];
  17. PId = HasPId ? reader.ReadInt64() : 0;
  18. for (int index = 0; index < ToCopy.Length; index++)
  19. {
  20. ToCopy[index] = reader.ReadInt32();
  21. }
  22. for (int index = 0; index < ToMove.Length; index++)
  23. {
  24. ToMove[index] = reader.ReadInt32();
  25. }
  26. }
  27. public IpcHandleDesc(int[] copy, int[] move)
  28. {
  29. ToCopy = copy ?? throw new ArgumentNullException(nameof(copy));
  30. ToMove = move ?? throw new ArgumentNullException(nameof(move));
  31. }
  32. public IpcHandleDesc(int[] copy, int[] move, long pId) : this(copy, move)
  33. {
  34. PId = pId;
  35. HasPId = true;
  36. }
  37. public static IpcHandleDesc MakeCopy(params int[] handles)
  38. {
  39. return new IpcHandleDesc(handles, new int[0]);
  40. }
  41. public static IpcHandleDesc MakeMove(params int[] handles)
  42. {
  43. return new IpcHandleDesc(new int[0], handles);
  44. }
  45. public byte[] GetBytes()
  46. {
  47. using (MemoryStream ms = new MemoryStream())
  48. {
  49. BinaryWriter writer = new BinaryWriter(ms);
  50. int word = HasPId ? 1 : 0;
  51. word |= (ToCopy.Length & 0xf) << 1;
  52. word |= (ToMove.Length & 0xf) << 5;
  53. writer.Write(word);
  54. if (HasPId)
  55. {
  56. writer.Write(PId);
  57. }
  58. foreach (int handle in ToCopy)
  59. {
  60. writer.Write(handle);
  61. }
  62. foreach (int handle in ToMove)
  63. {
  64. writer.Write(handle);
  65. }
  66. return ms.ToArray();
  67. }
  68. }
  69. }
  70. }