IFriendService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. [CommandHipc(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. [CommandHipc(1)]
  40. // nn::friends::Cancel()
  41. public ResultCode Cancel(ServiceCtx context)
  42. {
  43. // TODO: Original service sets an internal field to 1 here. Determine usage.
  44. Logger.Stub?.PrintStub(LogClass.ServiceFriend);
  45. return ResultCode.Success;
  46. }
  47. [CommandHipc(10100)]
  48. // nn::friends::GetFriendListIds(int offset, nn::account::Uid userId, nn::friends::detail::ipc::SizedFriendFilter friendFilter, ulong pidPlaceHolder, pid)
  49. // -> int outCount, array<nn::account::NetworkServiceAccountId, 0xa>
  50. public ResultCode GetFriendListIds(ServiceCtx context)
  51. {
  52. int offset = context.RequestData.ReadInt32();
  53. // Padding
  54. context.RequestData.ReadInt32();
  55. UserId userId = context.RequestData.ReadStruct<UserId>();
  56. FriendFilter filter = context.RequestData.ReadStruct<FriendFilter>();
  57. // Pid placeholder
  58. context.RequestData.ReadInt64();
  59. if (userId.IsNull)
  60. {
  61. return ResultCode.InvalidArgument;
  62. }
  63. // There are no friends online, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  64. context.ResponseData.Write(0);
  65. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new
  66. {
  67. UserId = userId.ToString(),
  68. offset,
  69. filter.PresenceStatus,
  70. filter.IsFavoriteOnly,
  71. filter.IsSameAppPresenceOnly,
  72. filter.IsSameAppPlayedOnly,
  73. filter.IsArbitraryAppPlayedOnly,
  74. filter.PresenceGroupId,
  75. });
  76. return ResultCode.Success;
  77. }
  78. [CommandHipc(10101)]
  79. // nn::friends::GetFriendList(int offset, nn::account::Uid userId, nn::friends::detail::ipc::SizedFriendFilter friendFilter, ulong pidPlaceHolder, pid)
  80. // -> int outCount, array<nn::friends::detail::FriendImpl, 0x6>
  81. public ResultCode GetFriendList(ServiceCtx context)
  82. {
  83. int offset = context.RequestData.ReadInt32();
  84. // Padding
  85. context.RequestData.ReadInt32();
  86. UserId userId = context.RequestData.ReadStruct<UserId>();
  87. FriendFilter filter = context.RequestData.ReadStruct<FriendFilter>();
  88. // Pid placeholder
  89. context.RequestData.ReadInt64();
  90. if (userId.IsNull)
  91. {
  92. return ResultCode.InvalidArgument;
  93. }
  94. // There are no friends online, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  95. context.ResponseData.Write(0);
  96. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new {
  97. UserId = userId.ToString(),
  98. offset,
  99. filter.PresenceStatus,
  100. filter.IsFavoriteOnly,
  101. filter.IsSameAppPresenceOnly,
  102. filter.IsSameAppPlayedOnly,
  103. filter.IsArbitraryAppPlayedOnly,
  104. filter.PresenceGroupId,
  105. });
  106. return ResultCode.Success;
  107. }
  108. [CommandHipc(10120)] // 10.0.0+
  109. // nn::friends::IsFriendListCacheAvailable(nn::account::Uid userId) -> bool
  110. public ResultCode IsFriendListCacheAvailable(ServiceCtx context)
  111. {
  112. UserId userId = context.RequestData.ReadStruct<UserId>();
  113. if (userId.IsNull)
  114. {
  115. return ResultCode.InvalidArgument;
  116. }
  117. // TODO: Service mount the friends:/ system savedata and try to load friend.cache file, returns true if exists, false otherwise.
  118. // NOTE: If no cache is available, guest then calls nn::friends::EnsureFriendListAvailable, we can avoid that by faking the cache check.
  119. context.ResponseData.Write(true);
  120. // TODO: Since we don't support friend features, it's fine to stub it for now.
  121. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  122. return ResultCode.Success;
  123. }
  124. [CommandHipc(10121)] // 10.0.0+
  125. // nn::friends::EnsureFriendListAvailable(nn::account::Uid userId)
  126. public ResultCode EnsureFriendListAvailable(ServiceCtx context)
  127. {
  128. UserId userId = context.RequestData.ReadStruct<UserId>();
  129. if (userId.IsNull)
  130. {
  131. return ResultCode.InvalidArgument;
  132. }
  133. // TODO: Service mount the friends:/ system savedata and create a friend.cache file for the given user id.
  134. // Since we don't support friend features, it's fine to stub it for now.
  135. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  136. return ResultCode.Success;
  137. }
  138. [CommandHipc(10400)]
  139. // nn::friends::GetBlockedUserListIds(int offset, nn::account::Uid userId) -> (u32, buffer<nn::account::NetworkServiceAccountId, 0xa>)
  140. public ResultCode GetBlockedUserListIds(ServiceCtx context)
  141. {
  142. int offset = context.RequestData.ReadInt32();
  143. // Padding
  144. context.RequestData.ReadInt32();
  145. UserId userId = context.RequestData.ReadStruct<UserId>();
  146. // There are no friends blocked, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  147. context.ResponseData.Write(0);
  148. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { offset, UserId = userId.ToString() });
  149. return ResultCode.Success;
  150. }
  151. [CommandHipc(10600)]
  152. // nn::friends::DeclareOpenOnlinePlaySession(nn::account::Uid userId)
  153. public ResultCode DeclareOpenOnlinePlaySession(ServiceCtx context)
  154. {
  155. UserId userId = context.RequestData.ReadStruct<UserId>();
  156. if (userId.IsNull)
  157. {
  158. return ResultCode.InvalidArgument;
  159. }
  160. context.Device.System.AccountManager.OpenUserOnlinePlay(userId);
  161. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  162. return ResultCode.Success;
  163. }
  164. [CommandHipc(10601)]
  165. // nn::friends::DeclareCloseOnlinePlaySession(nn::account::Uid userId)
  166. public ResultCode DeclareCloseOnlinePlaySession(ServiceCtx context)
  167. {
  168. UserId userId = context.RequestData.ReadStruct<UserId>();
  169. if (userId.IsNull)
  170. {
  171. return ResultCode.InvalidArgument;
  172. }
  173. context.Device.System.AccountManager.CloseUserOnlinePlay(userId);
  174. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  175. return ResultCode.Success;
  176. }
  177. [CommandHipc(10610)]
  178. // nn::friends::UpdateUserPresence(nn::account::Uid, u64, pid, buffer<nn::friends::detail::UserPresenceImpl, 0x19>)
  179. public ResultCode UpdateUserPresence(ServiceCtx context)
  180. {
  181. UserId uuid = context.RequestData.ReadStruct<UserId>();
  182. // Pid placeholder
  183. context.RequestData.ReadInt64();
  184. ulong position = context.Request.PtrBuff[0].Position;
  185. ulong size = context.Request.PtrBuff[0].Size;
  186. ReadOnlySpan<UserPresence> userPresenceInputArray = MemoryMarshal.Cast<byte, UserPresence>(context.Memory.GetSpan(position, (int)size));
  187. if (uuid.IsNull)
  188. {
  189. return ResultCode.InvalidArgument;
  190. }
  191. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = uuid.ToString(), userPresenceInputArray = userPresenceInputArray.ToArray() });
  192. return ResultCode.Success;
  193. }
  194. [CommandHipc(10700)]
  195. // nn::friends::GetPlayHistoryRegistrationKey(b8 unknown, nn::account::Uid) -> buffer<nn::friends::PlayHistoryRegistrationKey, 0x1a>
  196. public ResultCode GetPlayHistoryRegistrationKey(ServiceCtx context)
  197. {
  198. bool unknownBool = context.RequestData.ReadBoolean();
  199. UserId userId = context.RequestData.ReadStruct<UserId>();
  200. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(0x40UL);
  201. ulong bufferPosition = context.Request.RecvListBuff[0].Position;
  202. if (userId.IsNull)
  203. {
  204. return ResultCode.InvalidArgument;
  205. }
  206. // NOTE: Calls nn::friends::detail::service::core::PlayHistoryManager::GetInstance and stores the instance.
  207. byte[] randomBytes = new byte[8];
  208. Random.Shared.NextBytes(randomBytes);
  209. // NOTE: Calls nn::friends::detail::service::core::UuidManager::GetInstance and stores the instance.
  210. // Then call nn::friends::detail::service::core::AccountStorageManager::GetInstance and store the instance.
  211. // Then it checks if an Uuid is already stored for the UserId, if not it generates a random Uuid.
  212. // And store it in the savedata 8000000000000080 in the friends:/uid.bin file.
  213. Array16<byte> randomGuid = new Array16<byte>();
  214. Guid.NewGuid().ToByteArray().AsSpan().CopyTo(randomGuid.AsSpan());
  215. PlayHistoryRegistrationKey playHistoryRegistrationKey = new PlayHistoryRegistrationKey
  216. {
  217. Type = 0x101,
  218. KeyIndex = (byte)(randomBytes[0] & 7),
  219. UserIdBool = 0, // TODO: Find it.
  220. UnknownBool = (byte)(unknownBool ? 1 : 0), // TODO: Find it.
  221. Reserved = new Array11<byte>(),
  222. Uuid = randomGuid
  223. };
  224. ReadOnlySpan<byte> playHistoryRegistrationKeyBuffer = SpanHelpers.AsByteSpan(ref playHistoryRegistrationKey);
  225. /*
  226. 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).
  227. We currently don't support play history and online services so we can use a blank key for now.
  228. Code for reference:
  229. byte[] hmacKey = new byte[0x20];
  230. HMACSHA256 hmacSha256 = new HMACSHA256(hmacKey);
  231. byte[] hmacHash = hmacSha256.ComputeHash(playHistoryRegistrationKeyBuffer);
  232. */
  233. context.Memory.Write(bufferPosition, playHistoryRegistrationKeyBuffer);
  234. context.Memory.Write(bufferPosition + 0x20, new byte[0x20]); // HmacHash
  235. return ResultCode.Success;
  236. }
  237. [CommandHipc(10702)]
  238. // nn::friends::AddPlayHistory(nn::account::Uid, u64, pid, buffer<nn::friends::PlayHistoryRegistrationKey, 0x19>, buffer<nn::friends::InAppScreenName, 0x19>, buffer<nn::friends::InAppScreenName, 0x19>)
  239. public ResultCode AddPlayHistory(ServiceCtx context)
  240. {
  241. UserId userId = context.RequestData.ReadStruct<UserId>();
  242. // Pid placeholder
  243. context.RequestData.ReadInt64();
  244. ulong pid = context.Request.HandleDesc.PId;
  245. ulong playHistoryRegistrationKeyPosition = context.Request.PtrBuff[0].Position;
  246. ulong PlayHistoryRegistrationKeySize = context.Request.PtrBuff[0].Size;
  247. ulong inAppScreenName1Position = context.Request.PtrBuff[1].Position;
  248. ulong inAppScreenName1Size = context.Request.PtrBuff[1].Size;
  249. ulong inAppScreenName2Position = context.Request.PtrBuff[2].Position;
  250. ulong inAppScreenName2Size = context.Request.PtrBuff[2].Size;
  251. if (userId.IsNull || inAppScreenName1Size > 0x48 || inAppScreenName2Size > 0x48)
  252. {
  253. return ResultCode.InvalidArgument;
  254. }
  255. // TODO: Call nn::arp::GetApplicationControlProperty here when implemented.
  256. ApplicationControlProperty controlProperty = context.Device.Application.ControlData.Value;
  257. /*
  258. NOTE: The service calls nn::friends::detail::service::core::PlayHistoryManager to store informations using the registration key computed in GetPlayHistoryRegistrationKey.
  259. Then calls nn::friends::detail::service::core::FriendListManager to update informations on the friend list.
  260. We currently don't support play history and online services so it's fine to do nothing.
  261. */
  262. Logger.Stub?.PrintStub(LogClass.ServiceFriend);
  263. return ResultCode.Success;
  264. }
  265. }
  266. }