profile_manager.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. }
  27. if (index < user_count - 1) {
  28. std::rotate(profiles.begin() + index, profiles.begin() + index + 1, profiles.end());
  29. }
  30. profiles.back() = {};
  31. user_count--;
  32. return true;
  33. }
  34. ResultCode ProfileManager::AddUser(ProfileInfo user) {
  35. if (AddToProfiles(user) == std::numeric_limits<size_t>::max()) {
  36. return ERROR_TOO_MANY_USERS;
  37. }
  38. return RESULT_SUCCESS;
  39. }
  40. ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array<u8, 0x20>& username) {
  41. if (user_count == MAX_USERS) {
  42. return ERROR_TOO_MANY_USERS;
  43. }
  44. if (!uuid) {
  45. return ERROR_ARGUMENT_IS_NULL;
  46. }
  47. if (username[0] == 0x0) {
  48. return ERROR_ARGUMENT_IS_NULL;
  49. }
  50. if (std::any_of(profiles.begin(), profiles.end(),
  51. [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) {
  52. return ERROR_USER_ALREADY_EXISTS;
  53. }
  54. ProfileInfo prof_inf;
  55. prof_inf.user_uuid = std::move(uuid);
  56. prof_inf.username = std::move(username);
  57. prof_inf.data = std::array<u8, MAX_DATA>();
  58. prof_inf.creation_time = 0x0;
  59. prof_inf.is_open = false;
  60. return AddUser(prof_inf);
  61. }
  62. ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) {
  63. std::array<u8, 0x20> username_output;
  64. if (username.size() > username_output.size()) {
  65. std::copy_n(username.begin(), username_output.size(), username_output.begin());
  66. } else {
  67. std::copy(username.begin(), username.end(), username_output.begin());
  68. }
  69. return CreateNewUser(uuid, username_output);
  70. }
  71. size_t ProfileManager::GetUserIndex(const UUID& uuid) const {
  72. if (!uuid) {
  73. return std::numeric_limits<size_t>::max();
  74. }
  75. auto iter = std::find_if(profiles.begin(), profiles.end(),
  76. [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; });
  77. if (iter == profiles.end()) {
  78. return std::numeric_limits<size_t>::max();
  79. }
  80. return static_cast<size_t>(std::distance(profiles.begin(), iter));
  81. }
  82. size_t ProfileManager::GetUserIndex(ProfileInfo user) const {
  83. return GetUserIndex(user.user_uuid);
  84. }
  85. bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) const {
  86. if (index >= MAX_USERS) {
  87. profile.Invalidate();
  88. return false;
  89. }
  90. const auto& prof_info = profiles[index];
  91. profile.user_uuid = prof_info.user_uuid;
  92. profile.username = prof_info.username;
  93. profile.timestamp = prof_info.creation_time;
  94. return true;
  95. }
  96. bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) const {
  97. auto idx = GetUserIndex(uuid);
  98. return GetProfileBase(idx, profile);
  99. }
  100. bool ProfileManager::GetProfileBase(ProfileInfo user, ProfileBase& profile) const {
  101. return GetProfileBase(user.user_uuid, profile);
  102. }
  103. size_t ProfileManager::GetUserCount() const {
  104. return user_count;
  105. }
  106. size_t ProfileManager::GetOpenUserCount() const {
  107. return std::count_if(profiles.begin(), profiles.end(),
  108. [](const ProfileInfo& p) { return p.is_open; });
  109. }
  110. bool ProfileManager::UserExists(UUID uuid) const {
  111. return (GetUserIndex(uuid) != std::numeric_limits<size_t>::max());
  112. }
  113. void ProfileManager::OpenUser(UUID uuid) {
  114. auto idx = GetUserIndex(uuid);
  115. if (idx == std::numeric_limits<size_t>::max()) {
  116. return;
  117. }
  118. profiles[idx].is_open = true;
  119. last_opened_user = uuid;
  120. }
  121. void ProfileManager::CloseUser(UUID uuid) {
  122. auto idx = GetUserIndex(uuid);
  123. if (idx == std::numeric_limits<size_t>::max()) {
  124. return;
  125. }
  126. profiles[idx].is_open = false;
  127. }
  128. std::array<UUID, MAX_USERS> ProfileManager::GetAllUsers() const {
  129. std::array<UUID, MAX_USERS> output;
  130. std::transform(profiles.begin(), profiles.end(), output.begin(),
  131. [](const ProfileInfo& p) { return p.user_uuid; });
  132. return output;
  133. }
  134. std::array<UUID, MAX_USERS> ProfileManager::GetOpenUsers() const {
  135. std::array<UUID, MAX_USERS> output;
  136. std::transform(profiles.begin(), profiles.end(), output.begin(), [](const ProfileInfo& p) {
  137. if (p.is_open)
  138. return p.user_uuid;
  139. return UUID{};
  140. });
  141. std::stable_partition(output.begin(), output.end(), [](const UUID& uuid) { return uuid; });
  142. return output;
  143. }
  144. UUID ProfileManager::GetLastOpenedUser() const {
  145. return last_opened_user;
  146. }
  147. bool ProfileManager::GetProfileBaseAndData(size_t index, ProfileBase& profile,
  148. std::array<u8, MAX_DATA>& data) {
  149. if (GetProfileBase(index, profile)) {
  150. std::memcpy(data.data(), profiles[index].data.data(), MAX_DATA);
  151. return true;
  152. }
  153. return false;
  154. }
  155. bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile,
  156. std::array<u8, MAX_DATA>& data) {
  157. auto idx = GetUserIndex(uuid);
  158. return GetProfileBaseAndData(idx, profile, data);
  159. }
  160. bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile,
  161. std::array<u8, MAX_DATA>& data) {
  162. return GetProfileBaseAndData(user.user_uuid, profile, data);
  163. }
  164. bool ProfileManager::CanSystemRegisterUser() const {
  165. return false; // TODO(ogniK): Games shouldn't have
  166. // access to user registration, when we
  167. // emulate qlaunch. Update this to dynamically change.
  168. }
  169. }; // namespace Service::Account