profile_manager.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <random>
  5. #include <boost/optional.hpp>
  6. #include "core/hle/service/acc/profile_manager.h"
  7. #include "core/settings.h"
  8. namespace Service::Account {
  9. // TODO(ogniK): Get actual error codes
  10. constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1);
  11. constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2);
  12. constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20);
  13. const UUID& UUID::Generate() {
  14. std::random_device device;
  15. std::mt19937 gen(device());
  16. std::uniform_int_distribution<u64> distribution(1, std::numeric_limits<u64>::max());
  17. uuid[0] = distribution(gen);
  18. uuid[1] = distribution(gen);
  19. return *this;
  20. }
  21. ProfileManager::ProfileManager() {
  22. for (std::size_t i = 0; i < Settings::values.users.size(); ++i) {
  23. const auto& val = Settings::values.users[i];
  24. ASSERT(CreateNewUser(val.second, val.first).IsSuccess());
  25. }
  26. OpenUser(Settings::values.users[Settings::values.current_user].second);
  27. }
  28. ProfileManager::~ProfileManager() = default;
  29. /// After a users creation it needs to be "registered" to the system. AddToProfiles handles the
  30. /// internal management of the users profiles
  31. boost::optional<std::size_t> ProfileManager::AddToProfiles(const ProfileInfo& user) {
  32. if (user_count >= MAX_USERS) {
  33. return boost::none;
  34. }
  35. profiles[user_count] = user;
  36. return user_count++;
  37. }
  38. /// Deletes a specific profile based on it's profile index
  39. bool ProfileManager::RemoveProfileAtIndex(std::size_t index) {
  40. if (index >= MAX_USERS || index >= user_count) {
  41. return false;
  42. }
  43. if (index < user_count - 1) {
  44. std::rotate(profiles.begin() + index, profiles.begin() + index + 1, profiles.end());
  45. }
  46. profiles.back() = {};
  47. user_count--;
  48. return true;
  49. }
  50. /// Helper function to register a user to the system
  51. ResultCode ProfileManager::AddUser(const ProfileInfo& user) {
  52. if (AddToProfiles(user) == boost::none) {
  53. return ERROR_TOO_MANY_USERS;
  54. }
  55. return RESULT_SUCCESS;
  56. }
  57. /// Create a new user on the system. If the uuid of the user already exists, the user is not
  58. /// created.
  59. ResultCode ProfileManager::CreateNewUser(UUID uuid, const ProfileUsername& username) {
  60. if (user_count == MAX_USERS) {
  61. return ERROR_TOO_MANY_USERS;
  62. }
  63. if (!uuid) {
  64. return ERROR_ARGUMENT_IS_NULL;
  65. }
  66. if (username[0] == 0x0) {
  67. return ERROR_ARGUMENT_IS_NULL;
  68. }
  69. if (std::any_of(profiles.begin(), profiles.end(),
  70. [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) {
  71. return ERROR_USER_ALREADY_EXISTS;
  72. }
  73. ProfileInfo profile;
  74. profile.user_uuid = uuid;
  75. profile.username = username;
  76. profile.data = {};
  77. profile.creation_time = 0x0;
  78. profile.is_open = false;
  79. return AddUser(profile);
  80. }
  81. /// Creates a new user on the system. This function allows a much simpler method of registration
  82. /// specifically by allowing an std::string for the username. This is required specifically since
  83. /// we're loading a string straight from the config
  84. ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) {
  85. ProfileUsername username_output{};
  86. if (username.size() > username_output.size()) {
  87. std::copy_n(username.begin(), username_output.size(), username_output.begin());
  88. } else {
  89. std::copy(username.begin(), username.end(), username_output.begin());
  90. }
  91. return CreateNewUser(uuid, username_output);
  92. }
  93. /// Returns a users profile index based on their user id.
  94. boost::optional<std::size_t> ProfileManager::GetUserIndex(const UUID& uuid) const {
  95. if (!uuid) {
  96. return boost::none;
  97. }
  98. auto iter = std::find_if(profiles.begin(), profiles.end(),
  99. [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; });
  100. if (iter == profiles.end()) {
  101. return boost::none;
  102. }
  103. return static_cast<std::size_t>(std::distance(profiles.begin(), iter));
  104. }
  105. /// Returns a users profile index based on their profile
  106. boost::optional<std::size_t> ProfileManager::GetUserIndex(const ProfileInfo& user) const {
  107. return GetUserIndex(user.user_uuid);
  108. }
  109. /// Returns the data structure used by the switch when GetProfileBase is called on acc:*
  110. bool ProfileManager::GetProfileBase(boost::optional<std::size_t> index,
  111. ProfileBase& profile) const {
  112. if (index == boost::none || index >= MAX_USERS) {
  113. return false;
  114. }
  115. const auto& prof_info = profiles[index.get()];
  116. profile.user_uuid = prof_info.user_uuid;
  117. profile.username = prof_info.username;
  118. profile.timestamp = prof_info.creation_time;
  119. return true;
  120. }
  121. /// Returns the data structure used by the switch when GetProfileBase is called on acc:*
  122. bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) const {
  123. auto idx = GetUserIndex(uuid);
  124. return GetProfileBase(idx, profile);
  125. }
  126. /// Returns the data structure used by the switch when GetProfileBase is called on acc:*
  127. bool ProfileManager::GetProfileBase(const ProfileInfo& user, ProfileBase& profile) const {
  128. return GetProfileBase(user.user_uuid, profile);
  129. }
  130. /// Returns the current user count on the system. We keep a variable which tracks the count so we
  131. /// don't have to loop the internal profile array every call.
  132. std::size_t ProfileManager::GetUserCount() const {
  133. return user_count;
  134. }
  135. /// Lists the current "opened" users on the system. Users are typically not open until they sign
  136. /// into something or pick a profile. As of right now users should all be open until qlaunch is
  137. /// booting
  138. std::size_t ProfileManager::GetOpenUserCount() const {
  139. return std::count_if(profiles.begin(), profiles.end(),
  140. [](const ProfileInfo& p) { return p.is_open; });
  141. }
  142. /// Checks if a user id exists in our profile manager
  143. bool ProfileManager::UserExists(UUID uuid) const {
  144. return (GetUserIndex(uuid) != boost::none);
  145. }
  146. /// Opens a specific user
  147. void ProfileManager::OpenUser(UUID uuid) {
  148. auto idx = GetUserIndex(uuid);
  149. if (idx == boost::none) {
  150. return;
  151. }
  152. profiles[idx.get()].is_open = true;
  153. last_opened_user = uuid;
  154. }
  155. /// Closes a specific user
  156. void ProfileManager::CloseUser(UUID uuid) {
  157. auto idx = GetUserIndex(uuid);
  158. if (idx == boost::none) {
  159. return;
  160. }
  161. profiles[idx.get()].is_open = false;
  162. }
  163. /// Gets all valid user ids on the system
  164. UserIDArray ProfileManager::GetAllUsers() const {
  165. UserIDArray output;
  166. std::transform(profiles.begin(), profiles.end(), output.begin(),
  167. [](const ProfileInfo& p) { return p.user_uuid; });
  168. return output;
  169. }
  170. /// Get all the open users on the system and zero out the rest of the data. This is specifically
  171. /// needed for GetOpenUsers and we need to ensure the rest of the output buffer is zero'd out
  172. UserIDArray ProfileManager::GetOpenUsers() const {
  173. UserIDArray output;
  174. std::transform(profiles.begin(), profiles.end(), output.begin(), [](const ProfileInfo& p) {
  175. if (p.is_open)
  176. return p.user_uuid;
  177. return UUID{};
  178. });
  179. std::stable_partition(output.begin(), output.end(), [](const UUID& uuid) { return uuid; });
  180. return output;
  181. }
  182. /// Returns the last user which was opened
  183. UUID ProfileManager::GetLastOpenedUser() const {
  184. return last_opened_user;
  185. }
  186. /// Return the users profile base and the unknown arbitary data.
  187. bool ProfileManager::GetProfileBaseAndData(boost::optional<std::size_t> index, ProfileBase& profile,
  188. ProfileData& data) const {
  189. if (GetProfileBase(index, profile)) {
  190. data = profiles[index.get()].data;
  191. return true;
  192. }
  193. return false;
  194. }
  195. /// Return the users profile base and the unknown arbitary data.
  196. bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile,
  197. ProfileData& data) const {
  198. auto idx = GetUserIndex(uuid);
  199. return GetProfileBaseAndData(idx, profile, data);
  200. }
  201. /// Return the users profile base and the unknown arbitary data.
  202. bool ProfileManager::GetProfileBaseAndData(const ProfileInfo& user, ProfileBase& profile,
  203. ProfileData& data) const {
  204. return GetProfileBaseAndData(user.user_uuid, profile, data);
  205. }
  206. /// Returns if the system is allowing user registrations or not
  207. bool ProfileManager::CanSystemRegisterUser() const {
  208. return false; // TODO(ogniK): Games shouldn't have
  209. // access to user registration, when we
  210. // emulate qlaunch. Update this to dynamically change.
  211. }
  212. }; // namespace Service::Account