IFriendService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using LibHac.Ns;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Common.Memory;
  5. using Ryujinx.Common.Utilities;
  6. using Ryujinx.HLE.HOS.Ipc;
  7. using Ryujinx.HLE.HOS.Kernel.Common;
  8. using Ryujinx.HLE.HOS.Kernel.Threading;
  9. using Ryujinx.HLE.HOS.Services.Account.Acc;
  10. using Ryujinx.HLE.HOS.Services.Friend.ServiceCreator.FriendService;
  11. using System;
  12. using System.IO;
  13. using System.Runtime.InteropServices;
  14. namespace Ryujinx.HLE.HOS.Services.Friend.ServiceCreator
  15. {
  16. class IFriendService : IpcService
  17. {
  18. private FriendServicePermissionLevel _permissionLevel;
  19. private KEvent _completionEvent;
  20. public IFriendService(FriendServicePermissionLevel permissionLevel)
  21. {
  22. _permissionLevel = permissionLevel;
  23. }
  24. [Command(0)]
  25. // GetCompletionEvent() -> handle<copy>
  26. public ResultCode GetCompletionEvent(ServiceCtx context)
  27. {
  28. if (_completionEvent == null)
  29. {
  30. _completionEvent = new KEvent(context.Device.System.KernelContext);
  31. }
  32. if (context.Process.HandleTable.GenerateHandle(_completionEvent.ReadableEvent, out int completionEventHandle) != KernelResult.Success)
  33. {
  34. throw new InvalidOperationException("Out of handles!");
  35. }
  36. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(completionEventHandle);
  37. return ResultCode.Success;
  38. }
  39. [Command(10100)]
  40. // nn::friends::GetFriendListIds(int offset, nn::account::Uid userId, nn::friends::detail::ipc::SizedFriendFilter friendFilter, ulong pidPlaceHolder, pid)
  41. // -> int outCount, array<nn::account::NetworkServiceAccountId, 0xa>
  42. public ResultCode GetFriendListIds(ServiceCtx context)
  43. {
  44. int offset = context.RequestData.ReadInt32();
  45. // Padding
  46. context.RequestData.ReadInt32();
  47. UserId userId = context.RequestData.ReadStruct<UserId>();
  48. FriendFilter filter = context.RequestData.ReadStruct<FriendFilter>();
  49. // Pid placeholder
  50. context.RequestData.ReadInt64();
  51. if (userId.IsNull)
  52. {
  53. return ResultCode.InvalidArgument;
  54. }
  55. // There are no friends online, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  56. context.ResponseData.Write(0);
  57. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new
  58. {
  59. UserId = userId.ToString(),
  60. offset,
  61. filter.PresenceStatus,
  62. filter.IsFavoriteOnly,
  63. filter.IsSameAppPresenceOnly,
  64. filter.IsSameAppPlayedOnly,
  65. filter.IsArbitraryAppPlayedOnly,
  66. filter.PresenceGroupId,
  67. });
  68. return ResultCode.Success;
  69. }
  70. [Command(10101)]
  71. // nn::friends::GetFriendList(int offset, nn::account::Uid userId, nn::friends::detail::ipc::SizedFriendFilter friendFilter, ulong pidPlaceHolder, pid)
  72. // -> int outCount, array<nn::friends::detail::FriendImpl, 0x6>
  73. public ResultCode GetFriendList(ServiceCtx context)
  74. {
  75. int offset = context.RequestData.ReadInt32();
  76. // Padding
  77. context.RequestData.ReadInt32();
  78. UserId userId = context.RequestData.ReadStruct<UserId>();
  79. FriendFilter filter = context.RequestData.ReadStruct<FriendFilter>();
  80. // Pid placeholder
  81. context.RequestData.ReadInt64();
  82. if (userId.IsNull)
  83. {
  84. return ResultCode.InvalidArgument;
  85. }
  86. // There are no friends online, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  87. context.ResponseData.Write(0);
  88. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new {
  89. UserId = userId.ToString(),
  90. offset,
  91. filter.PresenceStatus,
  92. filter.IsFavoriteOnly,
  93. filter.IsSameAppPresenceOnly,
  94. filter.IsSameAppPlayedOnly,
  95. filter.IsArbitraryAppPlayedOnly,
  96. filter.PresenceGroupId,
  97. });
  98. return ResultCode.Success;
  99. }
  100. [Command(10400)]
  101. // nn::friends::GetBlockedUserListIds(int offset, nn::account::Uid userId) -> (u32, buffer<nn::account::NetworkServiceAccountId, 0xa>)
  102. public ResultCode GetBlockedUserListIds(ServiceCtx context)
  103. {
  104. int offset = context.RequestData.ReadInt32();
  105. // Padding
  106. context.RequestData.ReadInt32();
  107. UserId userId = context.RequestData.ReadStruct<UserId>();
  108. // There are no friends blocked, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  109. context.ResponseData.Write(0);
  110. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { offset, UserId = userId.ToString() });
  111. return ResultCode.Success;
  112. }
  113. [Command(10600)]
  114. // nn::friends::DeclareOpenOnlinePlaySession(nn::account::Uid userId)
  115. public ResultCode DeclareOpenOnlinePlaySession(ServiceCtx context)
  116. {
  117. UserId userId = context.RequestData.ReadStruct<UserId>();
  118. if (userId.IsNull)
  119. {
  120. return ResultCode.InvalidArgument;
  121. }
  122. if (context.Device.System.State.Account.TryGetUser(userId, out UserProfile profile))
  123. {
  124. profile.OnlinePlayState = AccountState.Open;
  125. }
  126. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString(), profile.OnlinePlayState });
  127. return ResultCode.Success;
  128. }
  129. [Command(10601)]
  130. // nn::friends::DeclareCloseOnlinePlaySession(nn::account::Uid userId)
  131. public ResultCode DeclareCloseOnlinePlaySession(ServiceCtx context)
  132. {
  133. UserId userId = context.RequestData.ReadStruct<UserId>();
  134. if (userId.IsNull)
  135. {
  136. return ResultCode.InvalidArgument;
  137. }
  138. if (context.Device.System.State.Account.TryGetUser(userId, out UserProfile profile))
  139. {
  140. profile.OnlinePlayState = AccountState.Closed;
  141. }
  142. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString(), profile.OnlinePlayState });
  143. return ResultCode.Success;
  144. }
  145. [Command(10610)]
  146. // nn::friends::UpdateUserPresence(nn::account::Uid, u64, pid, buffer<nn::friends::detail::UserPresenceImpl, 0x19>)
  147. public ResultCode UpdateUserPresence(ServiceCtx context)
  148. {
  149. UserId uuid = context.RequestData.ReadStruct<UserId>();
  150. // Pid placeholder
  151. context.RequestData.ReadInt64();
  152. long position = context.Request.PtrBuff[0].Position;
  153. long size = context.Request.PtrBuff[0].Size;
  154. byte[] bufferContent = new byte[size];
  155. context.Memory.Read((ulong)position, bufferContent);
  156. if (uuid.IsNull)
  157. {
  158. return ResultCode.InvalidArgument;
  159. }
  160. int elementCount = bufferContent.Length / Marshal.SizeOf<UserPresence>();
  161. using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(bufferContent)))
  162. {
  163. UserPresence[] userPresenceInputArray = bufferReader.ReadStructArray<UserPresence>(elementCount);
  164. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = uuid.ToString(), userPresenceInputArray });
  165. }
  166. return ResultCode.Success;
  167. }
  168. [Command(10700)]
  169. // nn::friends::GetPlayHistoryRegistrationKey(b8 unknown, nn::account::Uid) -> buffer<nn::friends::PlayHistoryRegistrationKey, 0x1a>
  170. public ResultCode GetPlayHistoryRegistrationKey(ServiceCtx context)
  171. {
  172. bool unknownBool = context.RequestData.ReadBoolean();
  173. UserId userId = context.RequestData.ReadStruct<UserId>();
  174. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(0x40L);
  175. long bufferPosition = context.Request.RecvListBuff[0].Position;
  176. if (userId.IsNull)
  177. {
  178. return ResultCode.InvalidArgument;
  179. }
  180. // NOTE: Calls nn::friends::detail::service::core::PlayHistoryManager::GetInstance and stores the instance.
  181. byte[] randomBytes = new byte[8];
  182. Random random = new Random();
  183. random.NextBytes(randomBytes);
  184. // NOTE: Calls nn::friends::detail::service::core::UuidManager::GetInstance and stores the instance.
  185. // Then call nn::friends::detail::service::core::AccountStorageManager::GetInstance and store the instance.
  186. // Then it checks if an Uuid is already stored for the UserId, if not it generates a random Uuid.
  187. // And store it in the savedata 8000000000000080 in the friends:/uid.bin file.
  188. Array16<byte> randomGuid = new Array16<byte>();
  189. Guid.NewGuid().ToByteArray().AsSpan().CopyTo(randomGuid.ToSpan());
  190. PlayHistoryRegistrationKey playHistoryRegistrationKey = new PlayHistoryRegistrationKey
  191. {
  192. Type = 0x101,
  193. KeyIndex = (byte)(randomBytes[0] & 7),
  194. UserIdBool = 0, // TODO: Find it.
  195. UnknownBool = (byte)(unknownBool ? 1 : 0), // TODO: Find it.
  196. Reserved = new Array11<byte>(),
  197. Uuid = randomGuid
  198. };
  199. ReadOnlySpan<byte> playHistoryRegistrationKeyBuffer = SpanHelpers.AsByteSpan(ref playHistoryRegistrationKey);
  200. /*
  201. 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).
  202. We currently don't support play history and online services so we can use a blank key for now.
  203. Code for reference:
  204. byte[] hmacKey = new byte[0x20];
  205. HMACSHA256 hmacSha256 = new HMACSHA256(hmacKey);
  206. byte[] hmacHash = hmacSha256.ComputeHash(playHistoryRegistrationKeyBuffer);
  207. */
  208. context.Memory.Write((ulong)bufferPosition, playHistoryRegistrationKeyBuffer);
  209. context.Memory.Write((ulong)bufferPosition + 0x20, new byte[0x20]); // HmacHash
  210. return ResultCode.Success;
  211. }
  212. [Command(10702)]
  213. // nn::friends::AddPlayHistory(nn::account::Uid, u64, pid, buffer<nn::friends::PlayHistoryRegistrationKey, 0x19>, buffer<nn::friends::InAppScreenName, 0x19>, buffer<nn::friends::InAppScreenName, 0x19>)
  214. public ResultCode AddPlayHistory(ServiceCtx context)
  215. {
  216. UserId userId = context.RequestData.ReadStruct<UserId>();
  217. // Pid placeholder
  218. context.RequestData.ReadInt64();
  219. long pid = context.Process.Pid;
  220. long playHistoryRegistrationKeyPosition = context.Request.PtrBuff[0].Position;
  221. long PlayHistoryRegistrationKeySize = context.Request.PtrBuff[0].Size;
  222. long inAppScreenName1Position = context.Request.PtrBuff[1].Position;
  223. long inAppScreenName1Size = context.Request.PtrBuff[1].Size;
  224. long inAppScreenName2Position = context.Request.PtrBuff[2].Position;
  225. long inAppScreenName2Size = context.Request.PtrBuff[2].Size;
  226. if (userId.IsNull || inAppScreenName1Size > 0x48 || inAppScreenName2Size > 0x48)
  227. {
  228. return ResultCode.InvalidArgument;
  229. }
  230. // TODO: Call nn::arp::GetApplicationControlProperty here when implemented.
  231. ApplicationControlProperty controlProperty = context.Device.Application.ControlData.Value;
  232. /*
  233. NOTE: The service calls nn::friends::detail::service::core::PlayHistoryManager to store informations using the registration key computed in GetPlayHistoryRegistrationKey.
  234. Then calls nn::friends::detail::service::core::FriendListManager to update informations on the friend list.
  235. We currently don't support play history and online services so it's fine to do nothing.
  236. */
  237. Logger.Stub?.PrintStub(LogClass.ServiceFriend);
  238. return ResultCode.Success;
  239. }
  240. }
  241. }