profile_manager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <random>
  6. #include <fmt/format.h>
  7. #include "common/file_util.h"
  8. #include "core/hle/service/acc/profile_manager.h"
  9. #include "core/settings.h"
  10. namespace Service::Account {
  11. using Common::UUID;
  12. struct UserRaw {
  13. UUID uuid;
  14. UUID uuid2;
  15. u64 timestamp;
  16. ProfileUsername username;
  17. ProfileData extra_data;
  18. };
  19. static_assert(sizeof(UserRaw) == 0xC8, "UserRaw has incorrect size.");
  20. struct ProfileDataRaw {
  21. INSERT_PADDING_BYTES(0x10);
  22. std::array<UserRaw, MAX_USERS> users;
  23. };
  24. static_assert(sizeof(ProfileDataRaw) == 0x650, "ProfileDataRaw has incorrect size.");
  25. // TODO(ogniK): Get actual error codes
  26. constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1);
  27. constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2);
  28. constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20);
  29. constexpr char ACC_SAVE_AVATORS_BASE_PATH[] = "/system/save/8000000000000010/su/avators/";
  30. ProfileManager::ProfileManager() {
  31. ParseUserSaveFile();
  32. if (user_count == 0)
  33. CreateNewUser(UUID::Generate(), "yuzu");
  34. auto current = std::clamp<int>(Settings::values.current_user, 0, MAX_USERS - 1);
  35. if (UserExistsIndex(current))
  36. current = 0;
  37. OpenUser(*GetUser(current));
  38. }
  39. ProfileManager::~ProfileManager() {
  40. WriteUserSaveFile();
  41. }
  42. /// After a users creation it needs to be "registered" to the system. AddToProfiles handles the
  43. /// internal management of the users profiles
  44. std::optional<std::size_t> ProfileManager::AddToProfiles(const ProfileInfo& profile) {
  45. if (user_count >= MAX_USERS) {
  46. return {};
  47. }
  48. profiles[user_count] = profile;
  49. return user_count++;
  50. }
  51. /// Deletes a specific profile based on it's profile index
  52. bool ProfileManager::RemoveProfileAtIndex(std::size_t index) {
  53. if (index >= MAX_USERS || index >= user_count) {
  54. return false;
  55. }
  56. if (index < user_count - 1) {
  57. std::rotate(profiles.begin() + index, profiles.begin() + index + 1, profiles.end());
  58. }
  59. profiles.back() = {};
  60. user_count--;
  61. return true;
  62. }
  63. /// Helper function to register a user to the system
  64. ResultCode ProfileManager::AddUser(const ProfileInfo& user) {
  65. if (!AddToProfiles(user)) {
  66. return ERROR_TOO_MANY_USERS;
  67. }
  68. return RESULT_SUCCESS;
  69. }
  70. /// Create a new user on the system. If the uuid of the user already exists, the user is not
  71. /// created.
  72. ResultCode ProfileManager::CreateNewUser(UUID uuid, const ProfileUsername& username) {
  73. if (user_count == MAX_USERS) {
  74. return ERROR_TOO_MANY_USERS;
  75. }
  76. if (!uuid) {
  77. return ERROR_ARGUMENT_IS_NULL;
  78. }
  79. if (username[0] == 0x0) {
  80. return ERROR_ARGUMENT_IS_NULL;
  81. }
  82. if (std::any_of(profiles.begin(), profiles.end(),
  83. [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) {
  84. return ERROR_USER_ALREADY_EXISTS;
  85. }
  86. ProfileInfo profile;
  87. profile.user_uuid = uuid;
  88. profile.username = username;
  89. profile.data = {};
  90. profile.creation_time = 0x0;
  91. profile.is_open = false;
  92. return AddUser(profile);
  93. }
  94. /// Creates a new user on the system. This function allows a much simpler method of registration
  95. /// specifically by allowing an std::string for the username. This is required specifically since
  96. /// we're loading a string straight from the config
  97. ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) {
  98. ProfileUsername username_output{};
  99. if (username.size() > username_output.size()) {
  100. std::copy_n(username.begin(), username_output.size(), username_output.begin());
  101. } else {
  102. std::copy(username.begin(), username.end(), username_output.begin());
  103. }
  104. return CreateNewUser(uuid, username_output);
  105. }
  106. std::optional<UUID> ProfileManager::GetUser(std::size_t index) const {
  107. if (index >= MAX_USERS) {
  108. return {};
  109. }
  110. return profiles[index].user_uuid;
  111. }
  112. /// Returns a users profile index based on their user id.
  113. std::optional<std::size_t> ProfileManager::GetUserIndex(const UUID& uuid) const {
  114. if (!uuid) {
  115. return {};
  116. }
  117. const auto iter = std::find_if(profiles.begin(), profiles.end(),
  118. [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; });
  119. if (iter == profiles.end()) {
  120. return {};
  121. }
  122. return static_cast<std::size_t>(std::distance(profiles.begin(), iter));
  123. }
  124. /// Returns a users profile index based on their profile
  125. std::optional<std::size_t> ProfileManager::GetUserIndex(const ProfileInfo& user) const {
  126. return GetUserIndex(user.user_uuid);
  127. }
  128. /// Returns the data structure used by the switch when GetProfileBase is called on acc:*
  129. bool ProfileManager::GetProfileBase(std::optional<std::size_t> index, ProfileBase& profile) const {
  130. if (!index || index >= MAX_USERS) {
  131. return false;
  132. }
  133. const auto& prof_info = profiles[*index];
  134. profile.user_uuid = prof_info.user_uuid;
  135. profile.username = prof_info.username;
  136. profile.timestamp = prof_info.creation_time;
  137. return true;
  138. }
  139. /// Returns the data structure used by the switch when GetProfileBase is called on acc:*
  140. bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) const {
  141. const auto idx = GetUserIndex(uuid);
  142. return GetProfileBase(idx, profile);
  143. }
  144. /// Returns the data structure used by the switch when GetProfileBase is called on acc:*
  145. bool ProfileManager::GetProfileBase(const ProfileInfo& user, ProfileBase& profile) const {
  146. return GetProfileBase(user.user_uuid, profile);
  147. }
  148. /// Returns the current user count on the system. We keep a variable which tracks the count so we
  149. /// don't have to loop the internal profile array every call.
  150. std::size_t ProfileManager::GetUserCount() const {
  151. return user_count;
  152. }
  153. /// Lists the current "opened" users on the system. Users are typically not open until they sign
  154. /// into something or pick a profile. As of right now users should all be open until qlaunch is
  155. /// booting
  156. std::size_t ProfileManager::GetOpenUserCount() const {
  157. return std::count_if(profiles.begin(), profiles.end(),
  158. [](const ProfileInfo& p) { return p.is_open; });
  159. }
  160. /// Checks if a user id exists in our profile manager
  161. bool ProfileManager::UserExists(UUID uuid) const {
  162. return GetUserIndex(uuid).has_value();
  163. }
  164. bool ProfileManager::UserExistsIndex(std::size_t index) const {
  165. if (index >= MAX_USERS)
  166. return false;
  167. return profiles[index].user_uuid.uuid != Common::INVALID_UUID;
  168. }
  169. /// Opens a specific user
  170. void ProfileManager::OpenUser(UUID uuid) {
  171. const auto idx = GetUserIndex(uuid);
  172. if (!idx) {
  173. return;
  174. }
  175. profiles[*idx].is_open = true;
  176. last_opened_user = uuid;
  177. }
  178. /// Closes a specific user
  179. void ProfileManager::CloseUser(UUID uuid) {
  180. const auto idx = GetUserIndex(uuid);
  181. if (!idx) {
  182. return;
  183. }
  184. profiles[*idx].is_open = false;
  185. }
  186. /// Gets all valid user ids on the system
  187. UserIDArray ProfileManager::GetAllUsers() const {
  188. UserIDArray output;
  189. std::transform(profiles.begin(), profiles.end(), output.begin(),
  190. [](const ProfileInfo& p) { return p.user_uuid; });
  191. return output;
  192. }
  193. /// Get all the open users on the system and zero out the rest of the data. This is specifically
  194. /// needed for GetOpenUsers and we need to ensure the rest of the output buffer is zero'd out
  195. UserIDArray ProfileManager::GetOpenUsers() const {
  196. UserIDArray output;
  197. std::transform(profiles.begin(), profiles.end(), output.begin(), [](const ProfileInfo& p) {
  198. if (p.is_open)
  199. return p.user_uuid;
  200. return UUID{};
  201. });
  202. std::stable_partition(output.begin(), output.end(), [](const UUID& uuid) { return uuid; });
  203. return output;
  204. }
  205. /// Returns the last user which was opened
  206. UUID ProfileManager::GetLastOpenedUser() const {
  207. return last_opened_user;
  208. }
  209. /// Return the users profile base and the unknown arbitary data.
  210. bool ProfileManager::GetProfileBaseAndData(std::optional<std::size_t> index, ProfileBase& profile,
  211. ProfileData& data) const {
  212. if (GetProfileBase(index, profile)) {
  213. data = profiles[*index].data;
  214. return true;
  215. }
  216. return false;
  217. }
  218. /// Return the users profile base and the unknown arbitary data.
  219. bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile,
  220. ProfileData& data) const {
  221. const auto idx = GetUserIndex(uuid);
  222. return GetProfileBaseAndData(idx, profile, data);
  223. }
  224. /// Return the users profile base and the unknown arbitary data.
  225. bool ProfileManager::GetProfileBaseAndData(const ProfileInfo& user, ProfileBase& profile,
  226. ProfileData& data) const {
  227. return GetProfileBaseAndData(user.user_uuid, profile, data);
  228. }
  229. /// Returns if the system is allowing user registrations or not
  230. bool ProfileManager::CanSystemRegisterUser() const {
  231. return false; // TODO(ogniK): Games shouldn't have
  232. // access to user registration, when we
  233. // emulate qlaunch. Update this to dynamically change.
  234. }
  235. bool ProfileManager::RemoveUser(UUID uuid) {
  236. const auto index = GetUserIndex(uuid);
  237. if (!index) {
  238. return false;
  239. }
  240. profiles[*index] = ProfileInfo{};
  241. std::stable_partition(profiles.begin(), profiles.end(),
  242. [](const ProfileInfo& profile) { return profile.user_uuid; });
  243. return true;
  244. }
  245. bool ProfileManager::SetProfileBase(UUID uuid, const ProfileBase& profile_new) {
  246. const auto index = GetUserIndex(uuid);
  247. if (!index || profile_new.user_uuid == UUID(Common::INVALID_UUID)) {
  248. return false;
  249. }
  250. auto& profile = profiles[*index];
  251. profile.user_uuid = profile_new.user_uuid;
  252. profile.username = profile_new.username;
  253. profile.creation_time = profile_new.timestamp;
  254. return true;
  255. }
  256. bool ProfileManager::SetProfileBaseAndData(Common::UUID uuid, const ProfileBase& profile_new,
  257. const ProfileData& data_new) {
  258. const auto index = GetUserIndex(uuid);
  259. if (index.has_value() && SetProfileBase(uuid, profile_new)) {
  260. profiles[*index].data = data_new;
  261. return true;
  262. }
  263. return false;
  264. }
  265. void ProfileManager::ParseUserSaveFile() {
  266. FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  267. ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat",
  268. "rb");
  269. if (!save.IsOpen()) {
  270. LOG_WARNING(Service_ACC, "Failed to load profile data from save data... Generating new "
  271. "user 'yuzu' with random UUID.");
  272. return;
  273. }
  274. ProfileDataRaw data;
  275. if (save.ReadBytes(&data, sizeof(ProfileDataRaw)) != sizeof(ProfileDataRaw)) {
  276. LOG_WARNING(Service_ACC, "profiles.dat is smaller than expected... Generating new user "
  277. "'yuzu' with random UUID.");
  278. return;
  279. }
  280. for (const auto& user : data.users) {
  281. if (user.uuid == UUID(Common::INVALID_UUID)) {
  282. continue;
  283. }
  284. AddUser({user.uuid, user.username, user.timestamp, user.extra_data, false});
  285. }
  286. std::stable_partition(profiles.begin(), profiles.end(),
  287. [](const ProfileInfo& profile) { return profile.user_uuid; });
  288. }
  289. void ProfileManager::WriteUserSaveFile() {
  290. ProfileDataRaw raw{};
  291. for (std::size_t i = 0; i < MAX_USERS; ++i) {
  292. raw.users[i].username = profiles[i].username;
  293. raw.users[i].uuid2 = profiles[i].user_uuid;
  294. raw.users[i].uuid = profiles[i].user_uuid;
  295. raw.users[i].timestamp = profiles[i].creation_time;
  296. raw.users[i].extra_data = profiles[i].data;
  297. }
  298. const auto raw_path =
  299. FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010";
  300. if (FileUtil::Exists(raw_path) && !FileUtil::IsDirectory(raw_path))
  301. FileUtil::Delete(raw_path);
  302. const auto path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
  303. ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat";
  304. if (!FileUtil::CreateFullPath(path)) {
  305. LOG_WARNING(Service_ACC, "Failed to create full path of profiles.dat. Create the directory "
  306. "nand/system/save/8000000000000010/su/avators to mitigate this "
  307. "issue.");
  308. return;
  309. }
  310. FileUtil::IOFile save(path, "wb");
  311. if (!save.IsOpen()) {
  312. LOG_WARNING(Service_ACC, "Failed to write save data to file... No changes to user data "
  313. "made in current session will be saved.");
  314. return;
  315. }
  316. save.Resize(sizeof(ProfileDataRaw));
  317. save.WriteBytes(&raw, sizeof(ProfileDataRaw));
  318. }
  319. }; // namespace Service::Account