AccountManager.cs 6.5 KB

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