profile_manager.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "core/hle/service/acc/profile_manager.h"
  5. #include "core/settings.h"
  6. namespace Service::Account {
  7. // TODO(ogniK): Get actual error codes
  8. constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1);
  9. constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2);
  10. constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20);
  11. ProfileManager::ProfileManager() {
  12. auto user_uuid = UUID{1, 0};
  13. CreateNewUser(user_uuid, Settings::values.username);
  14. OpenUser(user_uuid);
  15. }
  16. size_t ProfileManager::AddToProfiles(const ProfileInfo& user) {
  17. if (user_count >= MAX_USERS) {
  18. return std::numeric_limits<size_t>::max();
  19. }
  20. profiles[user_count] = std::move(user);
  21. return user_count++;
  22. }
  23. bool ProfileManager::RemoveProfileAtIdx(size_t index) {
  24. if (index >= MAX_USERS || index >= user_count)
  25. return false;
  26. if (index < user_count - 1)
  27. std::rotate(profiles.begin() + index, profiles.begin() + index + 1, profiles.end());
  28. profiles.back() = {};
  29. user_count--;
  30. return true;
  31. }
  32. ResultCode ProfileManager::AddUser(ProfileInfo user) {
  33. if (AddToProfiles(user) == std::numeric_limits<size_t>::max()) {
  34. return ERROR_TOO_MANY_USERS;
  35. }
  36. return RESULT_SUCCESS;
  37. }
  38. ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array<u8, 0x20>& username) {
  39. if (user_count == MAX_USERS)
  40. return ERROR_TOO_MANY_USERS;
  41. if (!uuid)
  42. return ERROR_ARGUMENT_IS_NULL;
  43. if (username[0] == 0x0)
  44. return ERROR_ARGUMENT_IS_NULL;
  45. if (std::any_of(profiles.begin(), profiles.end(),
  46. [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) {
  47. return ERROR_USER_ALREADY_EXISTS;
  48. }
  49. ProfileInfo prof_inf;
  50. prof_inf.user_uuid = std::move(uuid);
  51. prof_inf.username = std::move(username);
  52. prof_inf.data = std::array<u8, MAX_DATA>();
  53. prof_inf.creation_time = 0x0;
  54. prof_inf.is_open = false;
  55. return AddUser(prof_inf);
  56. }
  57. ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) {
  58. std::array<u8, 0x20> username_output;
  59. if (username.size() > username_output.size())
  60. std::copy_n(username.begin(), username_output.size(), username_output.begin());
  61. else
  62. std::copy(username.begin(), username.end(), username_output.begin());
  63. return CreateNewUser(uuid, username_output);
  64. }
  65. size_t ProfileManager::GetUserIndex(const UUID& uuid) const {
  66. if (!uuid)
  67. return std::numeric_limits<size_t>::max();
  68. auto iter = std::find_if(profiles.begin(), profiles.end(),
  69. [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; });
  70. if (iter == profiles.end()) {
  71. return std::numeric_limits<size_t>::max();
  72. }
  73. return static_cast<size_t>(std::distance(profiles.begin(), iter));
  74. }
  75. size_t ProfileManager::GetUserIndex(ProfileInfo user) const {
  76. return GetUserIndex(user.user_uuid);
  77. }
  78. bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) const {
  79. if (index >= MAX_USERS) {
  80. profile.Invalidate();
  81. return false;
  82. }
  83. const auto& prof_info = profiles[index];
  84. profile.user_uuid = prof_info.user_uuid;
  85. profile.username = prof_info.username;
  86. profile.timestamp = prof_info.creation_time;
  87. return true;
  88. }
  89. bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) const {
  90. auto idx = GetUserIndex(uuid);
  91. return GetProfileBase(idx, profile);
  92. }
  93. bool ProfileManager::GetProfileBase(ProfileInfo user, ProfileBase& profile) const {
  94. return GetProfileBase(user.user_uuid, profile);
  95. }
  96. size_t ProfileManager::GetUserCount() const {
  97. return user_count;
  98. }
  99. bool ProfileManager::UserExists(UUID uuid) const {
  100. return (GetUserIndex(uuid) != std::numeric_limits<size_t>::max());
  101. }
  102. void ProfileManager::OpenUser(UUID uuid) {
  103. auto idx = GetUserIndex(uuid);
  104. if (idx == std::numeric_limits<size_t>::max())
  105. return;
  106. profiles[idx].is_open = true;
  107. last_opened_user = uuid;
  108. }
  109. void ProfileManager::CloseUser(UUID uuid) {
  110. auto idx = GetUserIndex(uuid);
  111. if (idx == std::numeric_limits<size_t>::max())
  112. return;
  113. profiles[idx].is_open = false;
  114. }
  115. std::array<UUID, MAX_USERS> ProfileManager::GetAllUsers() const {
  116. std::array<UUID, MAX_USERS> output;
  117. for (unsigned i = 0; i < user_count; i++) {
  118. output[i] = profiles[i].user_uuid;
  119. }
  120. return output;
  121. }
  122. std::array<UUID, MAX_USERS> ProfileManager::GetOpenUsers() const {
  123. std::array<UUID, MAX_USERS> output;
  124. unsigned user_idx = 0;
  125. for (unsigned i = 0; i < user_count; i++) {
  126. if (profiles[i].is_open) {
  127. output[i++] = profiles[i].user_uuid;
  128. }
  129. }
  130. return output;
  131. }
  132. UUID ProfileManager::GetLastOpenedUser() const {
  133. return last_opened_user;
  134. }
  135. bool ProfileManager::GetProfileBaseAndData(size_t index, ProfileBase& profile,
  136. std::array<u8, MAX_DATA>& data) {
  137. if (GetProfileBase(index, profile)) {
  138. std::memcpy(data.data(), profiles[index].data.data(), MAX_DATA);
  139. return true;
  140. }
  141. return false;
  142. }
  143. bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile,
  144. std::array<u8, MAX_DATA>& data) {
  145. auto idx = GetUserIndex(uuid);
  146. return GetProfileBaseAndData(idx, profile, data);
  147. }
  148. bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile,
  149. std::array<u8, MAX_DATA>& data) {
  150. return GetProfileBaseAndData(user.user_uuid, profile, data);
  151. }
  152. bool ProfileManager::CanSystemRegisterUser() const {
  153. return false; // TODO(ogniK): Games shouldn't have
  154. // access to user registration, when we
  155. // emulate qlaunch. Update this to dynamically change.
  156. }
  157. }; // namespace Service::Account