IFriendService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Common.Memory;
  4. using Ryujinx.Common.Utilities;
  5. using Ryujinx.HLE.HOS.Services.Account.Acc;
  6. using Ryujinx.HLE.HOS.Services.Friend.ServiceCreator.FriendService;
  7. using System;
  8. using System.IO;
  9. using System.Runtime.CompilerServices;
  10. using System.Runtime.InteropServices;
  11. using System.Security.Cryptography;
  12. namespace Ryujinx.HLE.HOS.Services.Friend.ServiceCreator
  13. {
  14. class IFriendService : IpcService
  15. {
  16. private FriendServicePermissionLevel _permissionLevel;
  17. public IFriendService(FriendServicePermissionLevel permissionLevel)
  18. {
  19. _permissionLevel = permissionLevel;
  20. }
  21. [Command(10100)]
  22. // nn::friends::GetFriendListIds(int offset, nn::account::Uid userId, nn::friends::detail::ipc::SizedFriendFilter friendFilter, ulong pidPlaceHolder, pid)
  23. // -> int outCount, array<nn::account::NetworkServiceAccountId, 0xa>
  24. public ResultCode GetFriendListIds(ServiceCtx context)
  25. {
  26. int offset = context.RequestData.ReadInt32();
  27. // Padding
  28. context.RequestData.ReadInt32();
  29. UserId userId = context.RequestData.ReadStruct<UserId>();
  30. FriendFilter filter = context.RequestData.ReadStruct<FriendFilter>();
  31. // Pid placeholder
  32. context.RequestData.ReadInt64();
  33. if (userId.IsNull)
  34. {
  35. return ResultCode.InvalidArgument;
  36. }
  37. // There are no friends online, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  38. context.ResponseData.Write(0);
  39. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new
  40. {
  41. UserId = userId.ToString(),
  42. offset,
  43. filter.PresenceStatus,
  44. filter.IsFavoriteOnly,
  45. filter.IsSameAppPresenceOnly,
  46. filter.IsSameAppPlayedOnly,
  47. filter.IsArbitraryAppPlayedOnly,
  48. filter.PresenceGroupId,
  49. });
  50. return ResultCode.Success;
  51. }
  52. [Command(10101)]
  53. // nn::friends::GetFriendList(int offset, nn::account::Uid userId, nn::friends::detail::ipc::SizedFriendFilter friendFilter, ulong pidPlaceHolder, pid)
  54. // -> int outCount, array<nn::friends::detail::FriendImpl, 0x6>
  55. public ResultCode GetFriendList(ServiceCtx context)
  56. {
  57. int offset = context.RequestData.ReadInt32();
  58. // Padding
  59. context.RequestData.ReadInt32();
  60. UserId userId = context.RequestData.ReadStruct<UserId>();
  61. FriendFilter filter = context.RequestData.ReadStruct<FriendFilter>();
  62. // Pid placeholder
  63. context.RequestData.ReadInt64();
  64. if (userId.IsNull)
  65. {
  66. return ResultCode.InvalidArgument;
  67. }
  68. // There are no friends online, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  69. context.ResponseData.Write(0);
  70. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new {
  71. UserId = userId.ToString(),
  72. offset,
  73. filter.PresenceStatus,
  74. filter.IsFavoriteOnly,
  75. filter.IsSameAppPresenceOnly,
  76. filter.IsSameAppPlayedOnly,
  77. filter.IsArbitraryAppPlayedOnly,
  78. filter.PresenceGroupId,
  79. });
  80. return ResultCode.Success;
  81. }
  82. [Command(10400)]
  83. // nn::friends::GetBlockedUserListIds(int offset, nn::account::Uid userId) -> (u32, buffer<nn::account::NetworkServiceAccountId, 0xa>)
  84. public ResultCode GetBlockedUserListIds(ServiceCtx context)
  85. {
  86. int offset = context.RequestData.ReadInt32();
  87. // Padding
  88. context.RequestData.ReadInt32();
  89. UserId userId = context.RequestData.ReadStruct<UserId>();
  90. // There are no friends blocked, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  91. context.ResponseData.Write(0);
  92. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { offset, UserId = userId.ToString() });
  93. return ResultCode.Success;
  94. }
  95. [Command(10600)]
  96. // nn::friends::DeclareOpenOnlinePlaySession(nn::account::Uid userId)
  97. public ResultCode DeclareOpenOnlinePlaySession(ServiceCtx context)
  98. {
  99. UserId userId = context.RequestData.ReadStruct<UserId>();
  100. if (userId.IsNull)
  101. {
  102. return ResultCode.InvalidArgument;
  103. }
  104. if (context.Device.System.State.Account.TryGetUser(userId, out UserProfile profile))
  105. {
  106. profile.OnlinePlayState = AccountState.Open;
  107. }
  108. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString(), profile.OnlinePlayState });
  109. return ResultCode.Success;
  110. }
  111. [Command(10601)]
  112. // nn::friends::DeclareCloseOnlinePlaySession(nn::account::Uid userId)
  113. public ResultCode DeclareCloseOnlinePlaySession(ServiceCtx context)
  114. {
  115. UserId userId = context.RequestData.ReadStruct<UserId>();
  116. if (userId.IsNull)
  117. {
  118. return ResultCode.InvalidArgument;
  119. }
  120. if (context.Device.System.State.Account.TryGetUser(userId, out UserProfile profile))
  121. {
  122. profile.OnlinePlayState = AccountState.Closed;
  123. }
  124. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString(), profile.OnlinePlayState });
  125. return ResultCode.Success;
  126. }
  127. [Command(10610)]
  128. // nn::friends::UpdateUserPresence(nn::account::Uid, u64, pid, buffer<nn::friends::detail::UserPresenceImpl, 0x19>)
  129. public ResultCode UpdateUserPresence(ServiceCtx context)
  130. {
  131. UserId uuid = context.RequestData.ReadStruct<UserId>();
  132. // Pid placeholder
  133. context.RequestData.ReadInt64();
  134. long position = context.Request.PtrBuff[0].Position;
  135. long size = context.Request.PtrBuff[0].Size;
  136. byte[] bufferContent = new byte[size];
  137. context.Memory.Read((ulong)position, bufferContent);
  138. if (uuid.IsNull)
  139. {
  140. return ResultCode.InvalidArgument;
  141. }
  142. int elementCount = bufferContent.Length / Marshal.SizeOf<UserPresence>();
  143. using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(bufferContent)))
  144. {
  145. UserPresence[] userPresenceInputArray = bufferReader.ReadStructArray<UserPresence>(elementCount);
  146. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = uuid.ToString(), userPresenceInputArray });
  147. }
  148. return ResultCode.Success;
  149. }
  150. [Command(10700)]
  151. // nn::friends::GetPlayHistoryRegistrationKey(b8 unknown, nn::account::Uid) -> buffer<nn::friends::PlayHistoryRegistrationKey, 0x1a>
  152. public ResultCode GetPlayHistoryRegistrationKey(ServiceCtx context)
  153. {
  154. bool unknownBool = context.RequestData.ReadBoolean();
  155. UserId userId = context.RequestData.ReadStruct<UserId>();
  156. long bufferPosition = context.Request.RecvListBuff[0].Position;
  157. if (userId.IsNull)
  158. {
  159. return ResultCode.InvalidArgument;
  160. }
  161. // NOTE: Calls nn::friends::detail::service::core::PlayHistoryManager::GetInstance and stores the instance.
  162. byte[] randomBytes = new byte[8];
  163. Random random = new Random();
  164. random.NextBytes(randomBytes);
  165. // NOTE: Calls nn::friends::detail::service::core::UuidManager::GetInstance and stores the instance.
  166. // Then call nn::friends::detail::service::core::AccountStorageManager::GetInstance and store the instance.
  167. // Then it checks if an Uuid is already stored for the UserId, if not it generates a random Uuid.
  168. // And store it in the savedata 8000000000000080 in the friends:/uid.bin file.
  169. Array16<byte> randomGuid = new Array16<byte>();
  170. Guid.NewGuid().ToByteArray().AsSpan().CopyTo(randomGuid.ToSpan());
  171. PlayHistoryRegistrationKey playHistoryRegistrationKey = new PlayHistoryRegistrationKey
  172. {
  173. Type = 0x101,
  174. KeyIndex = (byte)(randomBytes[0] & 7),
  175. UserIdBool = 0, // TODO: Find it.
  176. UnknownBool = (byte)(unknownBool ? 1 : 0), // TODO: Find it.
  177. Reserved = new Array11<byte>(),
  178. Uuid = randomGuid
  179. };
  180. ReadOnlySpan<byte> playHistoryRegistrationKeyBuffer = SpanHelpers.AsByteSpan(ref playHistoryRegistrationKey);
  181. /*
  182. NOTE: The service uses the KeyIndex to get a random key from a keys buffer (since the key index is stored in the returned buffer).
  183. We currently don't support play history and online services so we can use a blank key for now.
  184. Code for reference:
  185. byte[] hmacKey = new byte[0x20];
  186. HMACSHA256 hmacSha256 = new HMACSHA256(hmacKey);
  187. byte[] hmacHash = hmacSha256.ComputeHash(playHistoryRegistrationKeyBuffer);
  188. */
  189. context.Memory.Write((ulong)bufferPosition, playHistoryRegistrationKeyBuffer);
  190. context.Memory.Write((ulong)bufferPosition + 0x20, new byte[0x20]); // HmacHash
  191. return ResultCode.Success;
  192. }
  193. }
  194. }