AccountSaveDataManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Ryujinx.Common.Configuration;
  2. using Ryujinx.Common.Utilities;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text.Json.Serialization;
  7. namespace Ryujinx.HLE.HOS.Services.Account.Acc
  8. {
  9. class AccountSaveDataManager
  10. {
  11. private readonly string _profilesJsonPath = Path.Join(AppDataManager.BaseDirPath, "system", "Profiles.json");
  12. private struct ProfilesJson
  13. {
  14. [JsonPropertyName("profiles")]
  15. public List<UserProfileJson> Profiles { get; set; }
  16. [JsonPropertyName("last_opened")]
  17. public string LastOpened { get; set; }
  18. }
  19. private struct UserProfileJson
  20. {
  21. [JsonPropertyName("user_id")]
  22. public string UserId { get; set; }
  23. [JsonPropertyName("name")]
  24. public string Name { get; set; }
  25. [JsonPropertyName("account_state")]
  26. public AccountState AccountState { get; set; }
  27. [JsonPropertyName("online_play_state")]
  28. public AccountState OnlinePlayState { get; set; }
  29. [JsonPropertyName("last_modified_timestamp")]
  30. public long LastModifiedTimestamp { get; set; }
  31. [JsonPropertyName("image")]
  32. public byte[] Image { get; set; }
  33. }
  34. public UserId LastOpened { get; set; }
  35. public AccountSaveDataManager(ConcurrentDictionary<string, UserProfile> profiles)
  36. {
  37. // TODO: Use 0x8000000000000010 system savedata instead of a JSON file if needed.
  38. if (File.Exists(_profilesJsonPath))
  39. {
  40. ProfilesJson profilesJson = JsonHelper.DeserializeFromFile<ProfilesJson>(_profilesJsonPath);
  41. foreach (var profile in profilesJson.Profiles)
  42. {
  43. UserProfile addedProfile = new UserProfile(new UserId(profile.UserId), profile.Name, profile.Image, profile.LastModifiedTimestamp);
  44. profiles.AddOrUpdate(profile.UserId, addedProfile, (key, old) => addedProfile);
  45. }
  46. LastOpened = new UserId(profilesJson.LastOpened);
  47. }
  48. else
  49. {
  50. LastOpened = AccountManager.DefaultUserId;
  51. }
  52. }
  53. public void Save(ConcurrentDictionary<string, UserProfile> profiles)
  54. {
  55. ProfilesJson profilesJson = new ProfilesJson()
  56. {
  57. Profiles = new List<UserProfileJson>(),
  58. LastOpened = LastOpened.ToString()
  59. };
  60. foreach (var profile in profiles)
  61. {
  62. profilesJson.Profiles.Add(new UserProfileJson()
  63. {
  64. UserId = profile.Value.UserId.ToString(),
  65. Name = profile.Value.Name,
  66. AccountState = profile.Value.AccountState,
  67. OnlinePlayState = profile.Value.OnlinePlayState,
  68. LastModifiedTimestamp = profile.Value.LastModifiedTimestamp,
  69. Image = profile.Value.Image,
  70. });
  71. }
  72. File.WriteAllText(_profilesJsonPath, JsonHelper.Serialize(profilesJson, true));
  73. }
  74. }
  75. }