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(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. [CommandHipc(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. [CommandHipc(10120)] // 10.0.0+
  101. // nn::friends::IsFriendListCacheAvailable(nn::account::Uid userId) -> bool
  102. public ResultCode IsFriendListCacheAvailable(ServiceCtx context)
  103. {
  104. UserId userId = context.RequestData.ReadStruct<UserId>();
  105. if (userId.IsNull)
  106. {
  107. return ResultCode.InvalidArgument;
  108. }
  109. // TODO: Service mount the friends:/ system savedata and try to load friend.cache file, returns true if exists, false otherwise.
  110. // NOTE: If no cache is available, guest then calls nn::friends::EnsureFriendListAvailable, we can avoid that by faking the cache check.
  111. context.ResponseData.Write(true);
  112. // TODO: Since we don't support friend features, it's fine to stub it for now.
  113. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  114. return ResultCode.Success;
  115. }
  116. [CommandHipc(10121)] // 10.0.0+
  117. // nn::friends::EnsureFriendListAvailable(nn::account::Uid userId)
  118. public ResultCode EnsureFriendListAvailable(ServiceCtx context)
  119. {
  120. UserId userId = context.RequestData.ReadStruct<UserId>();
  121. if (userId.IsNull)
  122. {
  123. return ResultCode.InvalidArgument;
  124. }
  125. // TODO: Service mount the friends:/ system savedata and create a friend.cache file for the given user id.
  126. // Since we don't support friend features, it's fine to stub it for now.
  127. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  128. return ResultCode.Success;
  129. }
  130. [CommandHipc(10400)]
  131. // nn::friends::GetBlockedUserListIds(int offset, nn::account::Uid userId) -> (u32, buffer<nn::account::NetworkServiceAccountId, 0xa>)
  132. public ResultCode GetBlockedUserListIds(ServiceCtx context)
  133. {
  134. int offset = context.RequestData.ReadInt32();
  135. // Padding
  136. context.RequestData.ReadInt32();
  137. UserId userId = context.RequestData.ReadStruct<UserId>();
  138. // There are no friends blocked, so we return 0 because the nn::account::NetworkServiceAccountId array is empty.
  139. context.ResponseData.Write(0);
  140. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { offset, UserId = userId.ToString() });
  141. return ResultCode.Success;
  142. }
  143. [CommandHipc(10600)]
  144. // nn::friends::DeclareOpenOnlinePlaySession(nn::account::Uid userId)
  145. public ResultCode DeclareOpenOnlinePlaySession(ServiceCtx context)
  146. {
  147. UserId userId = context.RequestData.ReadStruct<UserId>();
  148. if (userId.IsNull)
  149. {
  150. return ResultCode.InvalidArgument;
  151. }
  152. context.Device.System.AccountManager.OpenUserOnlinePlay(userId);
  153. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  154. return ResultCode.Success;
  155. }
  156. [CommandHipc(10601)]
  157. // nn::friends::DeclareCloseOnlinePlaySession(nn::account::Uid userId)
  158. public ResultCode DeclareCloseOnlinePlaySession(ServiceCtx context)
  159. {
  160. UserId userId = context.RequestData.ReadStruct<UserId>();
  161. if (userId.IsNull)
  162. {
  163. return ResultCode.InvalidArgument;
  164. }
  165. context.Device.System.AccountManager.CloseUserOnlinePlay(userId);
  166. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  167. return ResultCode.Success;
  168. }
  169. [CommandHipc(10610)]
  170. // nn::friends::UpdateUserPresence(nn::account::Uid, u64, pid, buffer<nn::friends::detail::UserPresenceImpl, 0x19>)
  171. public ResultCode UpdateUserPresence(ServiceCtx context)
  172. {
  173. UserId uuid = context.RequestData.ReadStruct<UserId>();
  174. // Pid placeholder
  175. context.RequestData.ReadInt64();
  176. ulong position = context.Request.PtrBuff[0].Position;
  177. ulong size = context.Request.PtrBuff[0].Size;
  178. byte[] bufferContent = new byte[size];
  179. context.Memory.Read(position, bufferContent);
  180. if (uuid.IsNull)
  181. {
  182. return ResultCode.InvalidArgument;
  183. }
  184. int elementCount = bufferContent.Length / Marshal.SizeOf<UserPresence>();
  185. using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(bufferContent)))
  186. {
  187. UserPresence[] userPresenceInputArray = bufferReader.ReadStructArray<UserPresence>(elementCount);
  188. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = uuid.ToString(), userPresenceInputArray });
  189. }
  190. return ResultCode.Success;
  191. }
  192. [CommandHipc(10700)]
  193. // nn::friends::GetPlayHistoryRegistrationKey(b8 unknown, nn::account::Uid) -> buffer<nn::friends::PlayHistoryRegistrationKey, 0x1a>
  194. public ResultCode GetPlayHistoryRegistrationKey(ServiceCtx context)
  195. {
  196. bool unknownBool = context.RequestData.ReadBoolean();
  197. UserId userId = context.RequestData.ReadStruct<UserId>();
  198. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(0x40UL);
  199. ulong bufferPosition = context.Request.RecvListBuff[0].Position;
  200. if (userId.IsNull)
  201. {
  202. return ResultCode.InvalidArgument;
  203. }
  204. // NOTE: Calls nn::friends::detail::service::core::PlayHistoryManager::GetInstance and stores the instance.
  205. byte[] randomBytes = new byte[8];
  206. Random random = new Random();
  207. random.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. }