profile_manager.cpp 8.5 KB

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