AccountManager.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Ryujinx.Common;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Ryujinx.HLE.HOS.Services.Account.Acc
  6. {
  7. public class AccountManager
  8. {
  9. private ConcurrentDictionary<string, UserProfile> _profiles;
  10. public UserProfile LastOpenedUser { get; private set; }
  11. public AccountManager()
  12. {
  13. _profiles = new ConcurrentDictionary<string, UserProfile>();
  14. UserId defaultUserId = new UserId("00000000000000010000000000000000");
  15. byte[] defaultUserImage = EmbeddedResources.Read("Ryujinx.HLE/HOS/Services/Account/Acc/DefaultUserImage.jpg");
  16. AddUser(defaultUserId, "Player", defaultUserImage);
  17. OpenUser(defaultUserId);
  18. }
  19. public void AddUser(UserId userId, string name, byte[] image)
  20. {
  21. UserProfile profile = new UserProfile(userId, name, image);
  22. _profiles.AddOrUpdate(userId.ToString(), profile, (key, old) => profile);
  23. }
  24. public void OpenUser(UserId userId)
  25. {
  26. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  27. {
  28. (LastOpenedUser = profile).AccountState = AccountState.Open;
  29. }
  30. }
  31. public void CloseUser(UserId userId)
  32. {
  33. if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
  34. {
  35. profile.AccountState = AccountState.Closed;
  36. }
  37. }
  38. public int GetUserCount()
  39. {
  40. return _profiles.Count;
  41. }
  42. internal bool TryGetUser(UserId userId, out UserProfile profile)
  43. {
  44. return _profiles.TryGetValue(userId.ToString(), out profile);
  45. }
  46. internal IEnumerable<UserProfile> GetAllUsers()
  47. {
  48. return _profiles.Values;
  49. }
  50. internal IEnumerable<UserProfile> GetOpenedUsers()
  51. {
  52. return _profiles.Values.Where(x => x.AccountState == AccountState.Open);
  53. }
  54. internal UserProfile GetFirst()
  55. {
  56. return _profiles.First().Value;
  57. }
  58. }
  59. }