IPrepoService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.Utilities;
  3. using System;
  4. using System.Buffers.Binary;
  5. using System.IO;
  6. using System.Text;
  7. namespace Ryujinx.HLE.HOS.Services.Prepo
  8. {
  9. [Service("prepo:a")]
  10. [Service("prepo:a2")]
  11. [Service("prepo:u")]
  12. class IPrepoService : IpcService
  13. {
  14. public IPrepoService(ServiceCtx context) { }
  15. [Command(10100)] // 1.0.0-5.1.0
  16. // SaveReport(u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  17. public ResultCode SaveReportOld(ServiceCtx context)
  18. {
  19. // We don't care about the differences since we don't use the play report.
  20. return ProcessReport(context, withUserID: false);
  21. }
  22. [Command(10101)] // 1.0.0-5.1.0
  23. // SaveReportWithUserOld(nn::account::Uid, u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  24. public ResultCode SaveReportWithUserOld(ServiceCtx context)
  25. {
  26. // We don't care about the differences since we don't use the play report.
  27. return ProcessReport(context, withUserID: true);
  28. }
  29. [Command(10102)] // 6.0.0+
  30. // SaveReport(u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  31. public ResultCode SaveReport(ServiceCtx context)
  32. {
  33. // We don't care about the differences since we don't use the play report.
  34. return ProcessReport(context, withUserID: false);
  35. }
  36. [Command(10103)] // 6.0.0+
  37. // SaveReportWithUser(nn::account::Uid, u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  38. public ResultCode SaveReportWithUser(ServiceCtx context)
  39. {
  40. // We don't care about the differences since we don't use the play report.
  41. return ProcessReport(context, withUserID: true);
  42. }
  43. private ResultCode ProcessReport(ServiceCtx context, bool withUserID)
  44. {
  45. UInt128 userId = withUserID ? new UInt128(context.RequestData.ReadBytes(0x10)) : new UInt128();
  46. string gameRoom = StringUtils.ReadUtf8String(context);
  47. if (withUserID)
  48. {
  49. if (userId.IsNull)
  50. {
  51. return ResultCode.InvalidArgument;
  52. }
  53. }
  54. if (gameRoom == string.Empty)
  55. {
  56. return ResultCode.InvalidState;
  57. }
  58. long inputPosition = context.Request.SendBuff[0].Position;
  59. long inputSize = context.Request.SendBuff[0].Size;
  60. if (inputSize == 0)
  61. {
  62. return ResultCode.InvalidBufferSize;
  63. }
  64. byte[] inputBuffer = context.Memory.ReadBytes(inputPosition, inputSize);
  65. Logger.PrintInfo(LogClass.ServicePrepo, ReadReportBuffer(inputBuffer, gameRoom, userId));
  66. return ResultCode.Success;
  67. }
  68. public string ReadReportBuffer(byte[] buffer, string room, UInt128 userId)
  69. {
  70. StringBuilder sb = new StringBuilder();
  71. sb.AppendLine();
  72. sb.AppendLine("PlayReport log:");
  73. if (!userId.IsNull)
  74. {
  75. sb.AppendLine($" UserId: {userId.ToString()}");
  76. }
  77. sb.AppendLine($" Room: {room}");
  78. try
  79. {
  80. using (MemoryStream stream = new MemoryStream(buffer))
  81. using (BinaryReader reader = new BinaryReader(stream))
  82. {
  83. byte unknown1 = reader.ReadByte(); // Version ?
  84. short unknown2 = reader.ReadInt16(); // Size ?
  85. bool isValue = false;
  86. string fieldStr = string.Empty;
  87. while (stream.Position != stream.Length)
  88. {
  89. byte descriptor = reader.ReadByte();
  90. if (!isValue)
  91. {
  92. byte[] key = reader.ReadBytes(descriptor - 0xA0);
  93. fieldStr = $" Key: {Encoding.ASCII.GetString(key)}";
  94. isValue = true;
  95. }
  96. else
  97. {
  98. if (descriptor > 0xD0) // Int value.
  99. {
  100. if (descriptor - 0xD0 == 1)
  101. {
  102. fieldStr += $", Value: {BinaryPrimitives.ReverseEndianness(reader.ReadUInt16())}";
  103. }
  104. else if (descriptor - 0xD0 == 2)
  105. {
  106. fieldStr += $", Value: {BinaryPrimitives.ReverseEndianness(reader.ReadInt32())}";
  107. }
  108. else if (descriptor - 0xD0 == 4)
  109. {
  110. fieldStr += $", Value: {BinaryPrimitives.ReverseEndianness(reader.ReadInt64())}";
  111. }
  112. else
  113. {
  114. // Unknown.
  115. break;
  116. }
  117. }
  118. else if (descriptor > 0xA0 && descriptor < 0xD0) // String value, max size = 0x20 bytes ?
  119. {
  120. int size = descriptor - 0xA0;
  121. string value = string.Empty;
  122. byte[] rawValues = new byte[0];
  123. for (int i = 0; i < size; i++)
  124. {
  125. byte chr = reader.ReadByte();
  126. if (chr >= 0x20 && chr < 0x7f)
  127. {
  128. value += (char)chr;
  129. }
  130. else
  131. {
  132. Array.Resize(ref rawValues, rawValues.Length + 1);
  133. rawValues[rawValues.Length - 1] = chr;
  134. }
  135. }
  136. if (value != string.Empty)
  137. {
  138. fieldStr += $", Value: {value}";
  139. }
  140. // TODO(Ac_K): Determine why there are non-alphanumeric values sometimes.
  141. if (rawValues.Length > 0)
  142. {
  143. fieldStr += $", RawValue: 0x{BitConverter.ToString(rawValues).Replace("-", "")}";
  144. }
  145. }
  146. else // Byte value.
  147. {
  148. fieldStr += $", Value: {descriptor}";
  149. }
  150. sb.AppendLine(fieldStr);
  151. isValue = false;
  152. }
  153. }
  154. }
  155. }
  156. catch (Exception)
  157. {
  158. sb.AppendLine(" Error while parsing the report buffer.");
  159. }
  160. return sb.ToString();
  161. }
  162. }
  163. }