AccountManager.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using LibHac;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.Fs.Shim;
  5. using Ryujinx.Common;
  6. using Ryujinx.Common.Logging;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. namespace Ryujinx.HLE.HOS.Services.Account.Acc
  12. {
  13. public class AccountManager
  14. {
  15. public static readonly UserId DefaultUserId = new UserId("00000000000000010000000000000000");
  16. private readonly AccountSaveDataManager _accountSaveDataManager;
  17. // Todo: The account service doesn't have the permissions to delete save data. Qlaunch takes care of deleting
  18. // save data, so we're currently passing a client with full permissions. Consider moving save data deletion
  19. // outside of the AccountManager.
  20. private readonly HorizonClient _horizonClient;
  21. private readonly ConcurrentDictionary<string, UserProfile> _profiles;
  22. private UserProfile[] _storedOpenedUsers;
  23. public UserProfile LastOpenedUser { get; private set; }
  24. public AccountManager(HorizonClient horizonClient, string initialProfileName = null)
  25. {
  26. _horizonClient = horizonClient;
  27. _profiles = new ConcurrentDictionary<string, UserProfile>();
  28. _storedOpenedUsers = Array.Empty<UserProfile>();
  29. _accountSaveDataManager = new AccountSaveDataManager(_profiles);
  30. if (!_profiles.TryGetValue(DefaultUserId.ToString(), out _))
  31. {
  32. byte[] defaultUserImage = EmbeddedResources.Read("Ryujinx.HLE/HOS/Services/Account/Acc/DefaultUserImage.jpg");
  33. AddUser("RyuPlayer", defaultUserImage, DefaultUserId);
  34. OpenUser(DefaultUserId);
  35. }
  36. else
  37. {
  38. UserId commandLineUserProfileOverride = default;
  39. if (!string.IsNullOrEmpty(initialProfileName))
  40. {
  41. commandLineUserProfileOverride = _profiles.Values.FirstOrDefault(x => x.Name == initialProfileName)?.UserId ?? default;
  42. if (commandLineUserProfileOverride.IsNull)
  43. Logger.Warning?.Print(LogClass.Application, $"The command line specified profile named '{initialProfileName}' was not found");
  44. }
  45. OpenUser(commandLineUserProfileOverride.IsNull ? _accountSaveDataManager.LastOpened : commandLineUserProfileOverride);
  46. }
  47. }
  48. public void AddUser(string name, byte[] image, UserId userId = new UserId())
  49. {
  50. if (userId.IsNull)
  51. {
  52. userId = new UserId(Guid.NewGuid().ToString().Replace("-", ""));
  53. }
  54. UserProfile profile = new UserProfile(userId, name, image);
  55. _profiles.AddOrUpdate(userId.ToString(), profile, (key, old) => profile);
  56. _accountSaveDataManager.Save(_profiles);
  57. }
  58. public void OpenUser(UserId userId)
  59. {
  60. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  61. {
  62. // TODO: Support multiple open users ?
  63. foreach (UserProfile userProfile in GetAllUsers())
  64. {
  65. if (userProfile == LastOpenedUser)
  66. {
  67. userProfile.AccountState = AccountState.Closed;
  68. break;
  69. }
  70. }
  71. (LastOpenedUser = profile).AccountState = AccountState.Open;
  72. _accountSaveDataManager.LastOpened = userId;
  73. }
  74. _accountSaveDataManager.Save(_profiles);
  75. }
  76. public void CloseUser(UserId userId)
  77. {
  78. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  79. {
  80. profile.AccountState = AccountState.Closed;
  81. }
  82. _accountSaveDataManager.Save(_profiles);
  83. }
  84. public void OpenUserOnlinePlay(UserId userId)
  85. {
  86. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  87. {
  88. // TODO: Support multiple open online users ?
  89. foreach (UserProfile userProfile in GetAllUsers())
  90. {
  91. if (userProfile == LastOpenedUser)
  92. {
  93. userProfile.OnlinePlayState = AccountState.Closed;
  94. break;
  95. }
  96. }
  97. profile.OnlinePlayState = AccountState.Open;
  98. }
  99. _accountSaveDataManager.Save(_profiles);
  100. }
  101. public void CloseUserOnlinePlay(UserId userId)
  102. {
  103. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  104. {
  105. profile.OnlinePlayState = AccountState.Closed;
  106. }
  107. _accountSaveDataManager.Save(_profiles);
  108. }
  109. public void SetUserImage(UserId userId, byte[] image)
  110. {
  111. foreach (UserProfile userProfile in GetAllUsers())
  112. {
  113. if (userProfile.UserId == userId)
  114. {
  115. userProfile.Image = image;
  116. break;
  117. }
  118. }
  119. _accountSaveDataManager.Save(_profiles);
  120. }
  121. public void SetUserName(UserId userId, string name)
  122. {
  123. foreach (UserProfile userProfile in GetAllUsers())
  124. {
  125. if (userProfile.UserId == userId)
  126. {
  127. userProfile.Name = name;
  128. break;
  129. }
  130. }
  131. _accountSaveDataManager.Save(_profiles);
  132. }
  133. public void DeleteUser(UserId userId)
  134. {
  135. DeleteSaveData(userId);
  136. _profiles.Remove(userId.ToString(), out _);
  137. OpenUser(DefaultUserId);
  138. _accountSaveDataManager.Save(_profiles);
  139. }
  140. private void DeleteSaveData(UserId userId)
  141. {
  142. var saveDataFilter = SaveDataFilter.Make(programId: default, saveType: default,
  143. new LibHac.Fs.UserId((ulong)userId.High, (ulong)userId.Low), saveDataId: default, index: default);
  144. using var saveDataIterator = new UniqueRef<SaveDataIterator>();
  145. _horizonClient.Fs.OpenSaveDataIterator(ref saveDataIterator.Ref(), SaveDataSpaceId.User, in saveDataFilter).ThrowIfFailure();
  146. Span<SaveDataInfo> saveDataInfo = stackalloc SaveDataInfo[10];
  147. while (true)
  148. {
  149. saveDataIterator.Get.ReadSaveDataInfo(out long readCount, saveDataInfo).ThrowIfFailure();
  150. if (readCount == 0)
  151. {
  152. break;
  153. }
  154. for (int i = 0; i < readCount; i++)
  155. {
  156. _horizonClient.Fs.DeleteSaveData(SaveDataSpaceId.User, saveDataInfo[i].SaveDataId).ThrowIfFailure();
  157. }
  158. }
  159. }
  160. internal int GetUserCount()
  161. {
  162. return _profiles.Count;
  163. }
  164. internal bool TryGetUser(UserId userId, out UserProfile profile)
  165. {
  166. return _profiles.TryGetValue(userId.ToString(), out profile);
  167. }
  168. public IEnumerable<UserProfile> GetAllUsers()
  169. {
  170. return _profiles.Values;
  171. }
  172. internal IEnumerable<UserProfile> GetOpenedUsers()
  173. {
  174. return _profiles.Values.Where(x => x.AccountState == AccountState.Open);
  175. }
  176. internal IEnumerable<UserProfile> GetStoredOpenedUsers()
  177. {
  178. return _storedOpenedUsers;
  179. }
  180. internal void StoreOpenedUsers()
  181. {
  182. _storedOpenedUsers = _profiles.Values.Where(x => x.AccountState == AccountState.Open).ToArray();
  183. }
  184. internal UserProfile GetFirst()
  185. {
  186. return _profiles.First().Value;
  187. }
  188. }
  189. }