IPrepoService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using MsgPack.Serialization;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.HLE.HOS.Services.Account.Acc;
  5. using Ryujinx.HLE.Utilities;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Text;
  9. namespace Ryujinx.HLE.HOS.Services.Prepo
  10. {
  11. [Service("prepo:a")]
  12. [Service("prepo:a2")]
  13. [Service("prepo:u")]
  14. class IPrepoService : IpcService
  15. {
  16. public IPrepoService(ServiceCtx context) { }
  17. [Command(10100)] // 1.0.0-5.1.0
  18. // SaveReport(u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  19. public ResultCode SaveReportOld(ServiceCtx context)
  20. {
  21. // We don't care about the differences since we don't use the play report.
  22. return ProcessReport(context, withUserID: false);
  23. }
  24. [Command(10101)] // 1.0.0-5.1.0
  25. // SaveReportWithUserOld(nn::account::Uid, u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  26. public ResultCode SaveReportWithUserOld(ServiceCtx context)
  27. {
  28. // We don't care about the differences since we don't use the play report.
  29. return ProcessReport(context, withUserID: true);
  30. }
  31. [Command(10102)] // 6.0.0+
  32. // SaveReport(u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  33. public ResultCode SaveReport(ServiceCtx context)
  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(10103)] // 6.0.0+
  39. // SaveReportWithUser(nn::account::Uid, u64, pid, buffer<u8, 9>, buffer<bytes, 5>)
  40. public ResultCode SaveReportWithUser(ServiceCtx context)
  41. {
  42. // We don't care about the differences since we don't use the play report.
  43. return ProcessReport(context, withUserID: true);
  44. }
  45. private ResultCode ProcessReport(ServiceCtx context, bool withUserID)
  46. {
  47. UserId userId = withUserID ? context.RequestData.ReadStruct<UserId>() : new UserId();
  48. string gameRoom = StringUtils.ReadUtf8String(context);
  49. if (withUserID)
  50. {
  51. if (userId.IsNull)
  52. {
  53. return ResultCode.InvalidArgument;
  54. }
  55. }
  56. if (gameRoom == string.Empty)
  57. {
  58. return ResultCode.InvalidState;
  59. }
  60. long inputPosition = context.Request.SendBuff[0].Position;
  61. long inputSize = context.Request.SendBuff[0].Size;
  62. if (inputSize == 0)
  63. {
  64. return ResultCode.InvalidBufferSize;
  65. }
  66. byte[] inputBuffer = context.Memory.ReadBytes(inputPosition, inputSize);
  67. Logger.PrintInfo(LogClass.ServicePrepo, ReadReportBuffer(inputBuffer, gameRoom, userId));
  68. return ResultCode.Success;
  69. }
  70. private string ReadReportBuffer(byte[] buffer, string room, UserId userId)
  71. {
  72. StringBuilder sb = new StringBuilder();
  73. sb.AppendLine();
  74. sb.AppendLine("PlayReport log:");
  75. if (!userId.IsNull)
  76. {
  77. sb.AppendLine($" UserId: {userId.ToString()}");
  78. }
  79. sb.AppendLine($" Room: {room}");
  80. var payload = Deserialize<IDictionary<string, object>>(buffer);
  81. foreach (var field in payload)
  82. {
  83. sb.AppendLine($" Key: {field.Key}, Value: {field.Value}");
  84. }
  85. return sb.ToString();
  86. }
  87. private static T Deserialize<T>(byte[] bytes)
  88. {
  89. MessagePackSerializer serializer = MessagePackSerializer.Get<T>();
  90. using (MemoryStream byteStream = new MemoryStream(bytes))
  91. {
  92. return (T)serializer.Unpack(byteStream);
  93. }
  94. }
  95. }
  96. }