IFriendService.cs 14 KB

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