AccountSaveDataManager.cs 3.6 KB

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