AccountManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 ConcurrentDictionary<string, UserProfile> _profiles;
  22. public UserProfile LastOpenedUser { get; private set; }
  23. public AccountManager(HorizonClient horizonClient, string initialProfileName = null)
  24. {
  25. _horizonClient = horizonClient;
  26. _profiles = new ConcurrentDictionary<string, UserProfile>();
  27. _accountSaveDataManager = new AccountSaveDataManager(_profiles);
  28. if (!_profiles.TryGetValue(DefaultUserId.ToString(), out _))
  29. {
  30. byte[] defaultUserImage = EmbeddedResources.Read("Ryujinx.HLE/HOS/Services/Account/Acc/DefaultUserImage.jpg");
  31. AddUser("RyuPlayer", defaultUserImage, DefaultUserId);
  32. OpenUser(DefaultUserId);
  33. }
  34. else
  35. {
  36. UserId commandLineUserProfileOverride = default;
  37. if (!string.IsNullOrEmpty(initialProfileName))
  38. {
  39. commandLineUserProfileOverride = _profiles.Values.FirstOrDefault(x => x.Name == initialProfileName)?.UserId ?? default;
  40. if (commandLineUserProfileOverride.IsNull)
  41. Logger.Warning?.Print(LogClass.Application, $"The command line specified profile named '{initialProfileName}' was not found");
  42. }
  43. OpenUser(commandLineUserProfileOverride.IsNull ? _accountSaveDataManager.LastOpened : commandLineUserProfileOverride);
  44. }
  45. }
  46. public void AddUser(string name, byte[] image, UserId userId = new UserId())
  47. {
  48. if (userId.IsNull)
  49. {
  50. userId = new UserId(Guid.NewGuid().ToString().Replace("-", ""));
  51. }
  52. UserProfile profile = new UserProfile(userId, name, image);
  53. _profiles.AddOrUpdate(userId.ToString(), profile, (key, old) => profile);
  54. _accountSaveDataManager.Save(_profiles);
  55. }
  56. public void OpenUser(UserId userId)
  57. {
  58. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  59. {
  60. // TODO: Support multiple open users ?
  61. foreach (UserProfile userProfile in GetAllUsers())
  62. {
  63. if (userProfile == LastOpenedUser)
  64. {
  65. userProfile.AccountState = AccountState.Closed;
  66. break;
  67. }
  68. }
  69. (LastOpenedUser = profile).AccountState = AccountState.Open;
  70. _accountSaveDataManager.LastOpened = userId;
  71. }
  72. _accountSaveDataManager.Save(_profiles);
  73. }
  74. public void CloseUser(UserId userId)
  75. {
  76. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  77. {
  78. profile.AccountState = AccountState.Closed;
  79. }
  80. _accountSaveDataManager.Save(_profiles);
  81. }
  82. public void OpenUserOnlinePlay(UserId userId)
  83. {
  84. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  85. {
  86. // TODO: Support multiple open online users ?
  87. foreach (UserProfile userProfile in GetAllUsers())
  88. {
  89. if (userProfile == LastOpenedUser)
  90. {
  91. userProfile.OnlinePlayState = AccountState.Closed;
  92. break;
  93. }
  94. }
  95. profile.OnlinePlayState = AccountState.Open;
  96. }
  97. _accountSaveDataManager.Save(_profiles);
  98. }
  99. public void CloseUserOnlinePlay(UserId userId)
  100. {
  101. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  102. {
  103. profile.OnlinePlayState = AccountState.Closed;
  104. }
  105. _accountSaveDataManager.Save(_profiles);
  106. }
  107. public void SetUserImage(UserId userId, byte[] image)
  108. {
  109. foreach (UserProfile userProfile in GetAllUsers())
  110. {
  111. if (userProfile.UserId == userId)
  112. {
  113. userProfile.Image = image;
  114. break;
  115. }
  116. }
  117. _accountSaveDataManager.Save(_profiles);
  118. }
  119. public void SetUserName(UserId userId, string name)
  120. {
  121. foreach (UserProfile userProfile in GetAllUsers())
  122. {
  123. if (userProfile.UserId == userId)
  124. {
  125. userProfile.Name = name;
  126. break;
  127. }
  128. }
  129. _accountSaveDataManager.Save(_profiles);
  130. }
  131. public void DeleteUser(UserId userId)
  132. {
  133. DeleteSaveData(userId);
  134. _profiles.Remove(userId.ToString(), out _);
  135. OpenUser(DefaultUserId);
  136. _accountSaveDataManager.Save(_profiles);
  137. }
  138. private void DeleteSaveData(UserId userId)
  139. {
  140. var saveDataFilter = SaveDataFilter.Make(programId: default, saveType: default,
  141. new LibHac.Fs.UserId((ulong)userId.High, (ulong)userId.Low), saveDataId: default, index: default);
  142. using var saveDataIterator = new UniqueRef<SaveDataIterator>();
  143. _horizonClient.Fs.OpenSaveDataIterator(ref saveDataIterator.Ref(), SaveDataSpaceId.User, in saveDataFilter).ThrowIfFailure();
  144. Span<SaveDataInfo> saveDataInfo = stackalloc SaveDataInfo[10];
  145. while (true)
  146. {
  147. saveDataIterator.Get.ReadSaveDataInfo(out long readCount, saveDataInfo).ThrowIfFailure();
  148. if (readCount == 0)
  149. {
  150. break;
  151. }
  152. for (int i = 0; i < readCount; i++)
  153. {
  154. _horizonClient.Fs.DeleteSaveData(SaveDataSpaceId.User, saveDataInfo[i].SaveDataId).ThrowIfFailure();
  155. }
  156. }
  157. }
  158. internal int GetUserCount()
  159. {
  160. return _profiles.Count;
  161. }
  162. internal bool TryGetUser(UserId userId, out UserProfile profile)
  163. {
  164. return _profiles.TryGetValue(userId.ToString(), out profile);
  165. }
  166. public IEnumerable<UserProfile> GetAllUsers()
  167. {
  168. return _profiles.Values;
  169. }
  170. internal IEnumerable<UserProfile> GetOpenedUsers()
  171. {
  172. return _profiles.Values.Where(x => x.AccountState == AccountState.Open);
  173. }
  174. internal UserProfile GetFirst()
  175. {
  176. return _profiles.First().Value;
  177. }
  178. }
  179. }