IPrepoService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. [CommandHipc(10100)] // 1.0.0-5.1.0
  26. [CommandHipc(10102)] // 6.0.0-9.2.0
  27. [CommandHipc(10104)] // 10.0.0+
  28. // SaveReport(u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  29. public ResultCode SaveReport(ServiceCtx context)
  30. {
  31. if ((_permission & PrepoServicePermissionLevel.User) == 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. [CommandHipc(10101)] // 1.0.0-5.1.0
  39. [CommandHipc(10103)] // 6.0.0-9.2.0
  40. [CommandHipc(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 ((_permission & PrepoServicePermissionLevel.User) == 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. [CommandHipc(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. [CommandHipc(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. [CommandHipc(10400)] // 9.0.0+
  69. // GetSystemSessionId() -> u64
  70. public ResultCode GetSystemSessionId(ServiceCtx context)
  71. {
  72. if ((_permission & PrepoServicePermissionLevel.User) == 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. [CommandHipc(20100)]
  86. // SaveSystemReport(u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  87. public ResultCode SaveSystemReport(ServiceCtx context)
  88. {
  89. if ((_permission & PrepoServicePermissionLevel.System) != 0)
  90. {
  91. return ResultCode.PermissionDenied;
  92. }
  93. // We don't care about the differences since we don't use the play report.
  94. return ProcessReport(context, withUserID: false);
  95. }
  96. [CommandHipc(20101)]
  97. // SaveSystemReportWithUser(nn::account::Uid, u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  98. public ResultCode SaveSystemReportWithUser(ServiceCtx context)
  99. {
  100. if ((_permission & PrepoServicePermissionLevel.System) != 0)
  101. {
  102. return ResultCode.PermissionDenied;
  103. }
  104. // We don't care about the differences since we don't use the play report.
  105. return ProcessReport(context, withUserID: true);
  106. }
  107. private ResultCode ProcessReport(ServiceCtx context, bool withUserID)
  108. {
  109. UserId userId = withUserID ? context.RequestData.ReadStruct<UserId>() : new UserId();
  110. string gameRoom = StringUtils.ReadUtf8String(context);
  111. if (withUserID)
  112. {
  113. if (userId.IsNull)
  114. {
  115. return ResultCode.InvalidArgument;
  116. }
  117. }
  118. if (gameRoom == string.Empty)
  119. {
  120. return ResultCode.InvalidState;
  121. }
  122. ulong inputPosition = context.Request.SendBuff[0].Position;
  123. ulong inputSize = context.Request.SendBuff[0].Size;
  124. if (inputSize == 0)
  125. {
  126. return ResultCode.InvalidBufferSize;
  127. }
  128. byte[] inputBuffer = new byte[inputSize];
  129. context.Memory.Read(inputPosition, inputBuffer);
  130. Logger.Info?.Print(LogClass.ServicePrepo, ReadReportBuffer(inputBuffer, gameRoom, userId));
  131. return ResultCode.Success;
  132. }
  133. private string ReadReportBuffer(byte[] buffer, string room, UserId userId)
  134. {
  135. StringBuilder builder = new StringBuilder();
  136. MessagePackObject deserializedReport = MessagePackSerializer.UnpackMessagePackObject(buffer);
  137. builder.AppendLine();
  138. builder.AppendLine("PlayReport log:");
  139. if (!userId.IsNull)
  140. {
  141. builder.AppendLine($" UserId: {userId}");
  142. }
  143. builder.AppendLine($" Room: {room}");
  144. builder.AppendLine($" Report: {MessagePackObjectFormatter.Format(deserializedReport)}");
  145. return builder.ToString();
  146. }
  147. }
  148. }