profile_manager.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <optional>
  6. #include "common/common_funcs.h"
  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 UserData {
  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(UserData) == 0x80, "UserData 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{};
  31. ProfileUsername username{};
  32. u64 creation_time{};
  33. UserData 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 = {};
  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. Result AddUser(const ProfileInfo& user);
  56. Result CreateNewUser(Common::UUID uuid, const ProfileUsername& username);
  57. Result 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. UserData& data) const;
  66. bool GetProfileBaseAndData(Common::UUID uuid, ProfileBase& profile, UserData& data) const;
  67. bool GetProfileBaseAndData(const ProfileInfo& user, ProfileBase& profile, UserData& data) const;
  68. std::size_t GetUserCount() const;
  69. std::size_t GetOpenUserCount() const;
  70. bool UserExists(Common::UUID uuid) const;
  71. bool UserExistsIndex(std::size_t index) const;
  72. void OpenUser(Common::UUID uuid);
  73. void CloseUser(Common::UUID uuid);
  74. UserIDArray GetOpenUsers() const;
  75. UserIDArray GetAllUsers() const;
  76. Common::UUID GetLastOpenedUser() const;
  77. UserIDArray GetStoredOpenedUsers() const;
  78. void StoreOpenedUsers();
  79. bool CanSystemRegisterUser() const;
  80. bool RemoveUser(Common::UUID uuid);
  81. bool SetProfileBase(Common::UUID uuid, const ProfileBase& profile_new);
  82. bool SetProfileBaseAndData(Common::UUID uuid, const ProfileBase& profile_new,
  83. const UserData& data_new);
  84. void WriteUserSaveFile();
  85. private:
  86. void ParseUserSaveFile();
  87. std::optional<std::size_t> AddToProfiles(const ProfileInfo& profile);
  88. bool RemoveProfileAtIndex(std::size_t index);
  89. std::array<ProfileInfo, MAX_USERS> profiles{};
  90. std::array<ProfileInfo, MAX_USERS> stored_opened_profiles{};
  91. std::size_t user_count{};
  92. Common::UUID last_opened_user{};
  93. };
  94. }; // namespace Service::Account