profile_manager.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <optional>
  7. #include "common/common_types.h"
  8. #include "common/swap.h"
  9. #include "common/uuid.h"
  10. #include "core/hle/result.h"
  11. namespace Service::Account {
  12. constexpr std::size_t MAX_USERS{8};
  13. constexpr std::size_t profile_username_size{32};
  14. using ProfileUsername = std::array<u8, profile_username_size>;
  15. using UserIDArray = std::array<Common::UUID, MAX_USERS>;
  16. /// Contains extra data related to a user.
  17. /// TODO: RE this structure
  18. struct ProfileData {
  19. INSERT_PADDING_WORDS_NOINIT(1);
  20. u32 icon_id;
  21. u8 bg_color_id;
  22. INSERT_PADDING_BYTES_NOINIT(0x7);
  23. INSERT_PADDING_BYTES_NOINIT(0x10);
  24. INSERT_PADDING_BYTES_NOINIT(0x60);
  25. };
  26. static_assert(sizeof(ProfileData) == 0x80, "ProfileData structure has incorrect size");
  27. /// This holds general information about a users profile. This is where we store all the information
  28. /// based on a specific user
  29. struct ProfileInfo {
  30. Common::UUID user_uuid{Common::INVALID_UUID};
  31. ProfileUsername username{};
  32. u64 creation_time{};
  33. ProfileData data{}; // TODO(ognik): Work out what this is
  34. bool is_open{};
  35. };
  36. struct ProfileBase {
  37. Common::UUID user_uuid;
  38. u64_le timestamp;
  39. ProfileUsername username;
  40. // Zero out all the fields to make the profile slot considered "Empty"
  41. void Invalidate() {
  42. user_uuid.Invalidate();
  43. timestamp = 0;
  44. username.fill(0);
  45. }
  46. };
  47. static_assert(sizeof(ProfileBase) == 0x38, "ProfileBase is an invalid size");
  48. /// The profile manager is used for handling multiple user profiles at once. It keeps track of open
  49. /// users, all the accounts registered on the "system" as well as fetching individual "ProfileInfo"
  50. /// objects
  51. class ProfileManager {
  52. public:
  53. ProfileManager();
  54. ~ProfileManager();
  55. ResultCode AddUser(const ProfileInfo& user);
  56. ResultCode CreateNewUser(Common::UUID uuid, const ProfileUsername& username);
  57. ResultCode CreateNewUser(Common::UUID uuid, const std::string& username);
  58. std::optional<Common::UUID> GetUser(std::size_t index) const;
  59. std::optional<std::size_t> GetUserIndex(const Common::UUID& uuid) const;
  60. std::optional<std::size_t> GetUserIndex(const ProfileInfo& user) const;
  61. bool GetProfileBase(std::optional<std::size_t> index, ProfileBase& profile) const;
  62. bool GetProfileBase(Common::UUID uuid, ProfileBase& profile) const;
  63. bool GetProfileBase(const ProfileInfo& user, ProfileBase& profile) const;
  64. bool GetProfileBaseAndData(std::optional<std::size_t> index, ProfileBase& profile,
  65. ProfileData& data) const;
  66. bool GetProfileBaseAndData(Common::UUID uuid, ProfileBase& profile, ProfileData& data) const;
  67. bool GetProfileBaseAndData(const ProfileInfo& user, ProfileBase& profile,
  68. ProfileData& data) const;
  69. std::size_t GetUserCount() const;
  70. std::size_t GetOpenUserCount() const;
  71. bool UserExists(Common::UUID uuid) const;
  72. bool UserExistsIndex(std::size_t index) const;
  73. void OpenUser(Common::UUID uuid);
  74. void CloseUser(Common::UUID uuid);
  75. UserIDArray GetOpenUsers() const;
  76. UserIDArray GetAllUsers() const;
  77. Common::UUID GetLastOpenedUser() const;
  78. bool CanSystemRegisterUser() const;
  79. bool RemoveUser(Common::UUID uuid);
  80. bool SetProfileBase(Common::UUID uuid, const ProfileBase& profile_new);
  81. bool SetProfileBaseAndData(Common::UUID uuid, const ProfileBase& profile_new,
  82. const ProfileData& data_new);
  83. private:
  84. void ParseUserSaveFile();
  85. void WriteUserSaveFile();
  86. std::optional<std::size_t> AddToProfiles(const ProfileInfo& profile);
  87. bool RemoveProfileAtIndex(std::size_t index);
  88. std::array<ProfileInfo, MAX_USERS> profiles{};
  89. std::size_t user_count{};
  90. Common::UUID last_opened_user{Common::INVALID_UUID};
  91. };
  92. }; // namespace Service::Account