AccountUtils.cs 1.8 KB

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