CommandSerialization.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Ryujinx.Horizon.Sdk.Sf.Cmif;
  2. using Ryujinx.Horizon.Sdk.Sf.Hipc;
  3. using Ryujinx.Memory;
  4. using System;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Horizon.Sdk.Sf
  8. {
  9. static class CommandSerialization
  10. {
  11. public static ReadOnlySpan<byte> GetReadOnlySpan(PointerAndSize bufferRange)
  12. {
  13. return HorizonStatic.AddressSpace.GetSpan(bufferRange.Address, checked((int)bufferRange.Size));
  14. }
  15. public static WritableRegion GetWritableRegion(PointerAndSize bufferRange)
  16. {
  17. return HorizonStatic.AddressSpace.GetWritableRegion(bufferRange.Address, checked((int)bufferRange.Size));
  18. }
  19. public static ref T GetRef<T>(PointerAndSize bufferRange) where T : unmanaged
  20. {
  21. var writableRegion = GetWritableRegion(bufferRange);
  22. return ref MemoryMarshal.Cast<byte, T>(writableRegion.Memory.Span)[0];
  23. }
  24. public static object DeserializeArg<T>(ref ServiceDispatchContext context, ReadOnlySpan<byte> inRawData, int offset) where T : unmanaged
  25. {
  26. return MemoryMarshal.Cast<byte, T>(inRawData.Slice(offset, Unsafe.SizeOf<T>()))[0];
  27. }
  28. public static T DeserializeArg<T>(ReadOnlySpan<byte> inRawData, int offset) where T : unmanaged
  29. {
  30. return MemoryMarshal.Cast<byte, T>(inRawData.Slice(offset, Unsafe.SizeOf<T>()))[0];
  31. }
  32. public static ulong DeserializeClientProcessId(ref ServiceDispatchContext context)
  33. {
  34. return context.Request.Pid;
  35. }
  36. public static int DeserializeCopyHandle(ref ServiceDispatchContext context, int index)
  37. {
  38. return context.Request.Data.CopyHandles[index];
  39. }
  40. public static int DeserializeMoveHandle(ref ServiceDispatchContext context, int index)
  41. {
  42. return context.Request.Data.MoveHandles[index];
  43. }
  44. public static void SerializeArg<T>(Span<byte> outRawData, int offset, T value) where T : unmanaged
  45. {
  46. MemoryMarshal.Cast<byte, T>(outRawData.Slice(offset, Unsafe.SizeOf<T>()))[0] = value;
  47. }
  48. public static void SerializeCopyHandle(HipcMessageData response, int index, int value)
  49. {
  50. response.CopyHandles[index] = value;
  51. }
  52. public static void SerializeMoveHandle(HipcMessageData response, int index, int value)
  53. {
  54. response.MoveHandles[index] = value;
  55. }
  56. }
  57. }