IPrepoService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using MsgPack;
  2. using MsgPack.Serialization;
  3. using Ryujinx.Common;
  4. using Ryujinx.Common.Logging;
  5. using Ryujinx.Common.Utilities;
  6. using Ryujinx.HLE.HOS.Services.Account.Acc;
  7. using Ryujinx.HLE.Utilities;
  8. using System;
  9. using System.Text;
  10. namespace Ryujinx.HLE.HOS.Services.Prepo
  11. {
  12. [Service("prepo:a", PrepoServicePermissionLevel.Admin)] // 1.0.0-5.1.0
  13. [Service("prepo:a2", PrepoServicePermissionLevel.Admin)] // 6.0.0+
  14. [Service("prepo:m", PrepoServicePermissionLevel.Manager)]
  15. [Service("prepo:u", PrepoServicePermissionLevel.User)]
  16. [Service("prepo:s", PrepoServicePermissionLevel.System)]
  17. class IPrepoService : IpcService
  18. {
  19. private PrepoServicePermissionLevel _permission;
  20. private ulong _systemSessionId;
  21. public IPrepoService(ServiceCtx context, PrepoServicePermissionLevel permission)
  22. {
  23. _permission = permission;
  24. }
  25. [Command(10100)] // 1.0.0-5.1.0
  26. [Command(10102)] // 6.0.0-9.2.0
  27. [Command(10104)] // 10.0.0+
  28. // SaveReport(u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  29. public ResultCode SaveReport(ServiceCtx context)
  30. {
  31. if (((int)_permission & 1) == 0)
  32. {
  33. return ResultCode.PermissionDenied;
  34. }
  35. // We don't care about the differences since we don't use the play report.
  36. return ProcessReport(context, withUserID: false);
  37. }
  38. [Command(10101)] // 1.0.0-5.1.0
  39. [Command(10103)] // 6.0.0-9.2.0
  40. [Command(10105)] // 10.0.0+
  41. // SaveReportWithUser(nn::account::Uid, u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  42. public ResultCode SaveReportWithUser(ServiceCtx context)
  43. {
  44. if (((int)_permission & 1) == 0)
  45. {
  46. return ResultCode.PermissionDenied;
  47. }
  48. // We don't care about the differences since we don't use the play report.
  49. return ProcessReport(context, withUserID: true);
  50. }
  51. [Command(10200)]
  52. // RequestImmediateTransmission()
  53. public ResultCode RequestImmediateTransmission(ServiceCtx context)
  54. {
  55. // It signals an event of nn::prepo::detail::service::core::TransmissionStatusManager that requests the transmission of the report.
  56. // Since we don't use reports it's fine to do nothing.
  57. return ResultCode.Success;
  58. }
  59. [Command(10300)]
  60. // GetTransmissionStatus() -> u32
  61. public ResultCode GetTransmissionStatus(ServiceCtx context)
  62. {
  63. // It returns the transmission result of nn::prepo::detail::service::core::TransmissionStatusManager.
  64. // Since we don't use reports it's fine to return ResultCode.Success.
  65. context.ResponseData.Write((int)ResultCode.Success);
  66. return ResultCode.Success;
  67. }
  68. [Command(10400)] // 9.0.0+
  69. // GetSystemSessionId() -> u64
  70. public ResultCode GetSystemSessionId(ServiceCtx context)
  71. {
  72. if (((int)_permission & 1) == 0)
  73. {
  74. return ResultCode.PermissionDenied;
  75. }
  76. if (_systemSessionId == 0)
  77. {
  78. byte[] randomBuffer = new byte[8];
  79. new Random().NextBytes(randomBuffer);
  80. _systemSessionId = BitConverter.ToUInt64(randomBuffer, 0);
  81. }
  82. context.ResponseData.Write(_systemSessionId);
  83. return ResultCode.Success;
  84. }
  85. private ResultCode ProcessReport(ServiceCtx context, bool withUserID)
  86. {
  87. UserId userId = withUserID ? context.RequestData.ReadStruct<UserId>() : new UserId();
  88. string gameRoom = StringUtils.ReadUtf8String(context);
  89. if (withUserID)
  90. {
  91. if (userId.IsNull)
  92. {
  93. return ResultCode.InvalidArgument;
  94. }
  95. }
  96. if (gameRoom == string.Empty)
  97. {
  98. return ResultCode.InvalidState;
  99. }
  100. long inputPosition = context.Request.SendBuff[0].Position;
  101. long inputSize = context.Request.SendBuff[0].Size;
  102. if (inputSize == 0)
  103. {
  104. return ResultCode.InvalidBufferSize;
  105. }
  106. byte[] inputBuffer = new byte[inputSize];
  107. context.Memory.Read((ulong)inputPosition, inputBuffer);
  108. Logger.Info?.Print(LogClass.ServicePrepo, ReadReportBuffer(inputBuffer, gameRoom, userId));
  109. return ResultCode.Success;
  110. }
  111. private string ReadReportBuffer(byte[] buffer, string room, UserId userId)
  112. {
  113. StringBuilder builder = new StringBuilder();
  114. MessagePackObject deserializedReport = MessagePackSerializer.UnpackMessagePackObject(buffer);
  115. builder.AppendLine();
  116. builder.AppendLine("PlayReport log:");
  117. if (!userId.IsNull)
  118. {
  119. builder.AppendLine($" UserId: {userId}");
  120. }
  121. builder.AppendLine($" Room: {room}");
  122. builder.AppendLine($" Report: {MessagePackObjectFormatter.Format(deserializedReport)}");
  123. return builder.ToString();
  124. }
  125. }
  126. }