IAccountServiceForApplication.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using ARMeilleure.Memory;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.HLE.HOS.Services.Arp;
  4. using Ryujinx.HLE.Utilities;
  5. using System.Collections.Generic;
  6. namespace Ryujinx.HLE.HOS.Services.Account.Acc
  7. {
  8. [Service("acc:u0")]
  9. class IAccountServiceForApplication : IpcService
  10. {
  11. private bool _userRegistrationRequestPermitted = false;
  12. private ApplicationLaunchProperty _applicationLaunchProperty;
  13. public IAccountServiceForApplication(ServiceCtx context) { }
  14. [Command(0)]
  15. // GetUserCount() -> i32
  16. public ResultCode GetUserCount(ServiceCtx context)
  17. {
  18. context.ResponseData.Write(context.Device.System.State.Account.GetUserCount());
  19. return ResultCode.Success;
  20. }
  21. [Command(1)]
  22. // GetUserExistence(nn::account::Uid) -> bool
  23. public ResultCode GetUserExistence(ServiceCtx context)
  24. {
  25. UInt128 userId = new UInt128(context.RequestData.ReadBytes(0x10));
  26. if (userId.IsNull)
  27. {
  28. return ResultCode.NullArgument;
  29. }
  30. context.ResponseData.Write(context.Device.System.State.Account.TryGetUser(userId, out _));
  31. return ResultCode.Success;
  32. }
  33. [Command(2)]
  34. // ListAllUsers() -> array<nn::account::Uid, 0xa>
  35. public ResultCode ListAllUsers(ServiceCtx context)
  36. {
  37. return WriteUserList(context, context.Device.System.State.Account.GetAllUsers());
  38. }
  39. [Command(3)]
  40. // ListOpenUsers() -> array<nn::account::Uid, 0xa>
  41. public ResultCode ListOpenUsers(ServiceCtx context)
  42. {
  43. return WriteUserList(context, context.Device.System.State.Account.GetOpenedUsers());
  44. }
  45. private ResultCode WriteUserList(ServiceCtx context, IEnumerable<UserProfile> profiles)
  46. {
  47. if (context.Request.RecvListBuff.Count == 0)
  48. {
  49. return ResultCode.InvalidInputBuffer;
  50. }
  51. long outputPosition = context.Request.RecvListBuff[0].Position;
  52. long outputSize = context.Request.RecvListBuff[0].Size;
  53. MemoryHelper.FillWithZeros(context.Memory, outputPosition, (int)outputSize);
  54. ulong offset = 0;
  55. foreach (UserProfile userProfile in profiles)
  56. {
  57. if (offset + 0x10 > (ulong)outputSize)
  58. {
  59. break;
  60. }
  61. context.Memory.WriteInt64(outputPosition + (long)offset, userProfile.UserId.Low);
  62. context.Memory.WriteInt64(outputPosition + (long)offset + 8, userProfile.UserId.High);
  63. offset += 0x10;
  64. }
  65. return ResultCode.Success;
  66. }
  67. [Command(4)]
  68. // GetLastOpenedUser() -> nn::account::Uid
  69. public ResultCode GetLastOpenedUser(ServiceCtx context)
  70. {
  71. context.Device.System.State.Account.LastOpenedUser.UserId.Write(context.ResponseData);
  72. return ResultCode.Success;
  73. }
  74. [Command(5)]
  75. // GetProfile(nn::account::Uid) -> object<nn::account::profile::IProfile>
  76. public ResultCode GetProfile(ServiceCtx context)
  77. {
  78. UInt128 userId = new UInt128(context.RequestData.ReadBytes(0x10));
  79. if (!context.Device.System.State.Account.TryGetUser(userId, out UserProfile userProfile))
  80. {
  81. Logger.PrintWarning(LogClass.ServiceAcc, $"User 0x{userId} not found!");
  82. return ResultCode.UserNotFound;
  83. }
  84. MakeObject(context, new IProfile(userProfile));
  85. // Doesn't occur in our case.
  86. // return ResultCode.NullObject;
  87. return ResultCode.Success;
  88. }
  89. [Command(50)]
  90. // IsUserRegistrationRequestPermitted(u64, pid) -> bool
  91. public ResultCode IsUserRegistrationRequestPermitted(ServiceCtx context)
  92. {
  93. // The u64 argument seems to be unused by account.
  94. context.ResponseData.Write(_userRegistrationRequestPermitted);
  95. return ResultCode.Success;
  96. }
  97. [Command(51)]
  98. // TrySelectUserWithoutInteraction(bool) -> nn::account::Uid
  99. public ResultCode TrySelectUserWithoutInteraction(ServiceCtx context)
  100. {
  101. if (context.Device.System.State.Account.GetUserCount() != 1)
  102. {
  103. // Invalid UserId.
  104. new UInt128(0, 0).Write(context.ResponseData);
  105. return 0;
  106. }
  107. bool baasCheck = context.RequestData.ReadBoolean();
  108. if (baasCheck)
  109. {
  110. // This checks something related to baas (online), and then return an invalid UserId if the check in baas returns an error code.
  111. // In our case, we can just log it for now.
  112. Logger.PrintStub(LogClass.ServiceAcc, new { baasCheck });
  113. }
  114. // As we returned an invalid UserId if there is more than one user earlier, now we can return only the first one.
  115. context.Device.System.State.Account.GetFirst().UserId.Write(context.ResponseData);
  116. return ResultCode.Success;
  117. }
  118. [Command(100)]
  119. [Command(140)] // 6.0.0+
  120. // InitializeApplicationInfo(u64, pid)
  121. // Both calls (100, 140) use the same submethod, maybe there's something different further along when arp:r is called?
  122. public ResultCode InitializeApplicationInfo(ServiceCtx context)
  123. {
  124. if (_applicationLaunchProperty != null)
  125. {
  126. return ResultCode.ApplicationLaunchPropertyAlreadyInit;
  127. }
  128. // The u64 argument seems to be unused by account.
  129. long unknown = context.RequestData.ReadInt64();
  130. // TODO: Account actually calls nn::arp::detail::IReader::GetApplicationLaunchProperty() with the current PID and store the result (ApplicationLaunchProperty) internally.
  131. // For now we can hardcode values, and fix it after GetApplicationLaunchProperty is implemented.
  132. /*
  133. if (nn::arp::detail::IReader::GetApplicationLaunchProperty() == 0xCC9D) // InvalidProcessId
  134. {
  135. _applicationLaunchProperty = ApplicationLaunchProperty.Default;
  136. return ResultCode.InvalidArgument;
  137. }
  138. else
  139. */
  140. {
  141. _applicationLaunchProperty = ApplicationLaunchProperty.GetByPid(context);
  142. }
  143. Logger.PrintStub(LogClass.ServiceAcc, new { unknown });
  144. return ResultCode.Success;
  145. }
  146. [Command(101)]
  147. // GetBaasAccountManagerForApplication(nn::account::Uid) -> object<nn::account::baas::IManagerForApplication>
  148. public ResultCode GetBaasAccountManagerForApplication(ServiceCtx context)
  149. {
  150. UInt128 userId = new UInt128(context.RequestData.ReadBytes(0x10));
  151. if (userId.IsNull)
  152. {
  153. return ResultCode.NullArgument;
  154. }
  155. if (_applicationLaunchProperty == null)
  156. {
  157. return ResultCode.InvalidArgument;
  158. }
  159. MakeObject(context, new IManagerForApplication(userId, _applicationLaunchProperty));
  160. // Doesn't occur in our case.
  161. // return ResultCode.NullObject;
  162. return ResultCode.Success;
  163. }
  164. [Command(110)]
  165. // StoreSaveDataThumbnail(nn::account::Uid, buffer<bytes, 5>)
  166. public ResultCode StoreSaveDataThumbnail(ServiceCtx context)
  167. {
  168. if (_applicationLaunchProperty == null)
  169. {
  170. return ResultCode.InvalidArgument;
  171. }
  172. UInt128 userId = new UInt128(context.RequestData.ReadBytes(0x10));
  173. if (userId.IsNull)
  174. {
  175. return ResultCode.NullArgument;
  176. }
  177. if (context.Request.SendBuff.Count == 0)
  178. {
  179. return ResultCode.InvalidInputBuffer;
  180. }
  181. long inputPosition = context.Request.SendBuff[0].Position;
  182. long inputSize = context.Request.SendBuff[0].Size;
  183. if (inputSize != 0x24000)
  184. {
  185. return ResultCode.InvalidInputBufferSize;
  186. }
  187. byte[] thumbnailBuffer = context.Memory.ReadBytes(inputPosition, inputSize);
  188. // TODO: Store thumbnailBuffer somewhere, in save data 0x8000000000000010 ?
  189. Logger.PrintStub(LogClass.ServiceAcc);
  190. return ResultCode.Success;
  191. }
  192. [Command(111)]
  193. // ClearSaveDataThumbnail(nn::account::Uid)
  194. public ResultCode ClearSaveDataThumbnail(ServiceCtx context)
  195. {
  196. if (_applicationLaunchProperty == null)
  197. {
  198. return ResultCode.InvalidArgument;
  199. }
  200. UInt128 userId = new UInt128(context.RequestData.ReadBytes(0x10));
  201. if (userId.IsNull)
  202. {
  203. return ResultCode.NullArgument;
  204. }
  205. // TODO: Clear the Thumbnail somewhere, in save data 0x8000000000000010 ?
  206. Logger.PrintStub(LogClass.ServiceAcc);
  207. return ResultCode.Success;
  208. }
  209. [Command(150)] // 6.0.0+
  210. // IsUserAccountSwitchLocked() -> bool
  211. public ResultCode IsUserAccountSwitchLocked(ServiceCtx context)
  212. {
  213. // TODO : Validate the following check.
  214. /*
  215. if (_applicationLaunchProperty != null)
  216. {
  217. return ResultCode.ApplicationLaunchPropertyAlreadyInit;
  218. }
  219. */
  220. // Account actually calls nn::arp::detail::IReader::GetApplicationControlProperty() with the current PID and store the result (NACP File) internally.
  221. // But since we use LibHac and we load one Application at a time, it's not necessary.
  222. context.ResponseData.Write(context.Device.System.ControlData.Value.UserAccountSwitchLock);
  223. Logger.PrintStub(LogClass.ServiceAcc);
  224. return ResultCode.Success;
  225. }
  226. }
  227. }