AccountSaveDataManager.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Ryujinx.Common.Configuration;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Common.Utilities;
  4. using Ryujinx.HLE.HOS.Services.Account.Acc.Types;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.IO;
  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 static readonly ProfilesJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
  15. public UserId LastOpened { get; set; }
  16. public AccountSaveDataManager(ConcurrentDictionary<string, UserProfile> profiles)
  17. {
  18. // TODO: Use 0x8000000000000010 system savedata instead of a JSON file if needed.
  19. if (File.Exists(_profilesJsonPath))
  20. {
  21. try
  22. {
  23. ProfilesJson profilesJson = JsonHelper.DeserializeFromFile(_profilesJsonPath, SerializerContext.ProfilesJson);
  24. foreach (var profile in profilesJson.Profiles)
  25. {
  26. UserProfile addedProfile = new UserProfile(new UserId(profile.UserId), profile.Name, profile.Image, profile.LastModifiedTimestamp);
  27. profiles.AddOrUpdate(profile.UserId, addedProfile, (key, old) => addedProfile);
  28. }
  29. LastOpened = new UserId(profilesJson.LastOpened);
  30. }
  31. catch (Exception e)
  32. {
  33. Logger.Error?.Print(LogClass.Application, $"Failed to parse {_profilesJsonPath}: {e.Message} Loading default profile!");
  34. LastOpened = AccountManager.DefaultUserId;
  35. }
  36. }
  37. else
  38. {
  39. LastOpened = AccountManager.DefaultUserId;
  40. }
  41. }
  42. public void Save(ConcurrentDictionary<string, UserProfile> profiles)
  43. {
  44. ProfilesJson profilesJson = new ProfilesJson()
  45. {
  46. Profiles = new List<UserProfileJson>(),
  47. LastOpened = LastOpened.ToString()
  48. };
  49. foreach (var profile in profiles)
  50. {
  51. profilesJson.Profiles.Add(new UserProfileJson()
  52. {
  53. UserId = profile.Value.UserId.ToString(),
  54. Name = profile.Value.Name,
  55. AccountState = profile.Value.AccountState,
  56. OnlinePlayState = profile.Value.OnlinePlayState,
  57. LastModifiedTimestamp = profile.Value.LastModifiedTimestamp,
  58. Image = profile.Value.Image,
  59. });
  60. }
  61. JsonHelper.SerializeToFile(_profilesJsonPath, profilesJson, SerializerContext.ProfilesJson);
  62. }
  63. }
  64. }