IFriendService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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(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. [CommandHipc(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. context.Device.System.AccountManager.OpenUserOnlinePlay(userId);
  123. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  124. return ResultCode.Success;
  125. }
  126. [CommandHipc(10601)]
  127. // nn::friends::DeclareCloseOnlinePlaySession(nn::account::Uid userId)
  128. public ResultCode DeclareCloseOnlinePlaySession(ServiceCtx context)
  129. {
  130. UserId userId = context.RequestData.ReadStruct<UserId>();
  131. if (userId.IsNull)
  132. {
  133. return ResultCode.InvalidArgument;
  134. }
  135. context.Device.System.AccountManager.CloseUserOnlinePlay(userId);
  136. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
  137. return ResultCode.Success;
  138. }
  139. [CommandHipc(10610)]
  140. // nn::friends::UpdateUserPresence(nn::account::Uid, u64, pid, buffer<nn::friends::detail::UserPresenceImpl, 0x19>)
  141. public ResultCode UpdateUserPresence(ServiceCtx context)
  142. {
  143. UserId uuid = context.RequestData.ReadStruct<UserId>();
  144. // Pid placeholder
  145. context.RequestData.ReadInt64();
  146. ulong position = context.Request.PtrBuff[0].Position;
  147. ulong size = context.Request.PtrBuff[0].Size;
  148. byte[] bufferContent = new byte[size];
  149. context.Memory.Read(position, bufferContent);
  150. if (uuid.IsNull)
  151. {
  152. return ResultCode.InvalidArgument;
  153. }
  154. int elementCount = bufferContent.Length / Marshal.SizeOf<UserPresence>();
  155. using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(bufferContent)))
  156. {
  157. UserPresence[] userPresenceInputArray = bufferReader.ReadStructArray<UserPresence>(elementCount);
  158. Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = uuid.ToString(), userPresenceInputArray });
  159. }
  160. return ResultCode.Success;
  161. }
  162. [CommandHipc(10700)]
  163. // nn::friends::GetPlayHistoryRegistrationKey(b8 unknown, nn::account::Uid) -> buffer<nn::friends::PlayHistoryRegistrationKey, 0x1a>
  164. public ResultCode GetPlayHistoryRegistrationKey(ServiceCtx context)
  165. {
  166. bool unknownBool = context.RequestData.ReadBoolean();
  167. UserId userId = context.RequestData.ReadStruct<UserId>();
  168. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize(0x40UL);
  169. ulong bufferPosition = context.Request.RecvListBuff[0].Position;
  170. if (userId.IsNull)
  171. {
  172. return ResultCode.InvalidArgument;
  173. }
  174. // NOTE: Calls nn::friends::detail::service::core::PlayHistoryManager::GetInstance and stores the instance.
  175. byte[] randomBytes = new byte[8];
  176. Random random = new Random();
  177. random.NextBytes(randomBytes);
  178. // NOTE: Calls nn::friends::detail::service::core::UuidManager::GetInstance and stores the instance.
  179. // Then call nn::friends::detail::service::core::AccountStorageManager::GetInstance and store the instance.
  180. // Then it checks if an Uuid is already stored for the UserId, if not it generates a random Uuid.
  181. // And store it in the savedata 8000000000000080 in the friends:/uid.bin file.
  182. Array16<byte> randomGuid = new Array16<byte>();
  183. Guid.NewGuid().ToByteArray().AsSpan().CopyTo(randomGuid.ToSpan());
  184. PlayHistoryRegistrationKey playHistoryRegistrationKey = new PlayHistoryRegistrationKey
  185. {
  186. Type = 0x101,
  187. KeyIndex = (byte)(randomBytes[0] & 7),
  188. UserIdBool = 0, // TODO: Find it.
  189. UnknownBool = (byte)(unknownBool ? 1 : 0), // TODO: Find it.
  190. Reserved = new Array11<byte>(),
  191. Uuid = randomGuid
  192. };
  193. ReadOnlySpan<byte> playHistoryRegistrationKeyBuffer = SpanHelpers.AsByteSpan(ref playHistoryRegistrationKey);
  194. /*
  195. 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).
  196. We currently don't support play history and online services so we can use a blank key for now.
  197. Code for reference:
  198. byte[] hmacKey = new byte[0x20];
  199. HMACSHA256 hmacSha256 = new HMACSHA256(hmacKey);
  200. byte[] hmacHash = hmacSha256.ComputeHash(playHistoryRegistrationKeyBuffer);
  201. */
  202. context.Memory.Write(bufferPosition, playHistoryRegistrationKeyBuffer);
  203. context.Memory.Write(bufferPosition + 0x20, new byte[0x20]); // HmacHash
  204. return ResultCode.Success;
  205. }
  206. [CommandHipc(10702)]
  207. // nn::friends::AddPlayHistory(nn::account::Uid, u64, pid, buffer<nn::friends::PlayHistoryRegistrationKey, 0x19>, buffer<nn::friends::InAppScreenName, 0x19>, buffer<nn::friends::InAppScreenName, 0x19>)
  208. public ResultCode AddPlayHistory(ServiceCtx context)
  209. {
  210. UserId userId = context.RequestData.ReadStruct<UserId>();
  211. // Pid placeholder
  212. context.RequestData.ReadInt64();
  213. long pid = context.Process.Pid;
  214. ulong playHistoryRegistrationKeyPosition = context.Request.PtrBuff[0].Position;
  215. ulong PlayHistoryRegistrationKeySize = context.Request.PtrBuff[0].Size;
  216. ulong inAppScreenName1Position = context.Request.PtrBuff[1].Position;
  217. ulong inAppScreenName1Size = context.Request.PtrBuff[1].Size;
  218. ulong inAppScreenName2Position = context.Request.PtrBuff[2].Position;
  219. ulong inAppScreenName2Size = context.Request.PtrBuff[2].Size;
  220. if (userId.IsNull || inAppScreenName1Size > 0x48 || inAppScreenName2Size > 0x48)
  221. {
  222. return ResultCode.InvalidArgument;
  223. }
  224. // TODO: Call nn::arp::GetApplicationControlProperty here when implemented.
  225. ApplicationControlProperty controlProperty = context.Device.Application.ControlData.Value;
  226. /*
  227. NOTE: The service calls nn::friends::detail::service::core::PlayHistoryManager to store informations using the registration key computed in GetPlayHistoryRegistrationKey.
  228. Then calls nn::friends::detail::service::core::FriendListManager to update informations on the friend list.
  229. We currently don't support play history and online services so it's fine to do nothing.
  230. */
  231. Logger.Stub?.PrintStub(LogClass.ServiceFriend);
  232. return ResultCode.Success;
  233. }
  234. }
  235. }