From aeffd4b436dceb798b4ffc1f8babb350a741280a Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 21:49:06 -0400 Subject: profile_manager: Load users from emulator settings --- src/core/hle/service/acc/profile_manager.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index bcb3475db..b4b4b52b7 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -23,10 +23,12 @@ const UUID& UUID::Generate() { } ProfileManager::ProfileManager() { - // TODO(ogniK): Create the default user we have for now until loading/saving users is added - auto user_uuid = UUID{1, 0}; - ASSERT(CreateNewUser(user_uuid, Settings::values.username).IsSuccess()); - OpenUser(user_uuid); + for (std::size_t i = 0; i < Settings::values.users.size(); ++i) { + const auto& val = Settings::values.users[i]; + ASSERT(CreateNewUser(val.second, val.first).IsSuccess()); + } + + OpenUser(Settings::values.users[Settings::values.current_user].second); } ProfileManager::~ProfileManager() = default; -- cgit v1.2.3 From 702622b8f1eaa1b297a27a305ac56faeadf542d7 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Wed, 10 Oct 2018 21:49:20 -0400 Subject: profile_manager: Load user icons, names, and UUIDs from system save --- src/core/hle/service/acc/profile_manager.cpp | 101 +++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 6 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index b4b4b52b7..b0ea06b48 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -4,10 +4,27 @@ #include #include +#include "common/file_util.h" #include "core/hle/service/acc/profile_manager.h" #include "core/settings.h" namespace Service::Account { + +struct UserRaw { + UUID uuid; + UUID uuid2; + u64 timestamp; + ProfileUsername username; + INSERT_PADDING_BYTES(0x80); +}; +static_assert(sizeof(UserRaw) == 0xC8, "UserRaw has incorrect size."); + +struct ProfileDataRaw { + INSERT_PADDING_BYTES(0x10); + std::array users; +}; +static_assert(sizeof(ProfileDataRaw) == 0x650, "ProfileDataRaw has incorrect size."); + // TODO(ogniK): Get actual error codes constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1); constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2); @@ -23,15 +40,21 @@ const UUID& UUID::Generate() { } ProfileManager::ProfileManager() { - for (std::size_t i = 0; i < Settings::values.users.size(); ++i) { - const auto& val = Settings::values.users[i]; - ASSERT(CreateNewUser(val.second, val.first).IsSuccess()); - } + ParseUserSaveFile(); + + if (user_count == 0) + CreateNewUser(UUID{}.Generate(), "yuzu"); + + auto current = Settings::values.current_user; + if (!GetAllUsers()[current]) + current = 0; - OpenUser(Settings::values.users[Settings::values.current_user].second); + OpenUser(GetAllUsers()[current]); } -ProfileManager::~ProfileManager() = default; +ProfileManager::~ProfileManager() { + WriteUserSaveFile(); +} /// After a users creation it needs to be "registered" to the system. AddToProfiles handles the /// internal management of the users profiles @@ -241,4 +264,70 @@ bool ProfileManager::CanSystemRegisterUser() const { // emulate qlaunch. Update this to dynamically change. } +bool ProfileManager::RemoveUser(UUID uuid) { + auto index = GetUserIndex(uuid); + if (index == boost::none) { + return false; + } + + profiles[*index] = ProfileInfo{}; + std::stable_partition(profiles.begin(), profiles.end(), + [](const ProfileInfo& profile) { return profile.user_uuid; }); + return true; +} + +bool ProfileManager::SetProfileBase(UUID uuid, const ProfileBase& profile_new) { + auto index = GetUserIndex(uuid); + if (profile_new.user_uuid == UUID(INVALID_UUID) || index == boost::none) { + return false; + } + + auto& profile = profiles[*index]; + profile.user_uuid = profile_new.user_uuid; + profile.username = profile_new.username; + profile.creation_time = profile_new.timestamp; + + return true; +} + +void ProfileManager::ParseUserSaveFile() { + FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + + "/system/save/8000000000000010/su/avators/profiles.dat", + "rb"); + + ProfileDataRaw data; + save.Seek(0, SEEK_SET); + if (save.ReadBytes(&data, sizeof(ProfileDataRaw)) != sizeof(ProfileDataRaw)) + return; + + for (std::size_t i = 0; i < MAX_USERS; ++i) { + const auto& user = data.users[i]; + + if (user.uuid != UUID(INVALID_UUID)) + AddUser({user.uuid, user.username, user.timestamp, {}, false}); + } + + std::stable_partition(profiles.begin(), profiles.end(), + [](const ProfileInfo& profile) { return profile.user_uuid; }); +} + +void ProfileManager::WriteUserSaveFile() { + ProfileDataRaw raw{}; + + for (std::size_t i = 0; i < MAX_USERS; ++i) { + raw.users[i].username = profiles[i].username; + raw.users[i].uuid2 = profiles[i].user_uuid; + raw.users[i].uuid = profiles[i].user_uuid; + raw.users[i].timestamp = profiles[i].creation_time; + } + + FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + + "/system/save/8000000000000010/su/avators/profiles.dat", + "rb"); + + save.Resize(sizeof(ProfileDataRaw)); + save.Seek(0, SEEK_SET); + save.WriteBytes(&raw, sizeof(ProfileDataRaw)); +} + }; // namespace Service::Account -- cgit v1.2.3 From e408bbceed90da8965480e23d05fb764fcbfbb84 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Thu, 11 Oct 2018 09:16:32 -0400 Subject: configure_system: Clear selection after user delete --- src/core/hle/service/acc/profile_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index b0ea06b48..43743d39e 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -323,7 +323,7 @@ void ProfileManager::WriteUserSaveFile() { FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010/su/avators/profiles.dat", - "rb"); + "wb"); save.Resize(sizeof(ProfileDataRaw)); save.Seek(0, SEEK_SET); -- cgit v1.2.3 From 45f2a2fe29373f261144c097d169dad8b65fe012 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 13 Oct 2018 13:02:33 -0400 Subject: acc: Fix account UUID duplication error --- src/core/hle/service/acc/profile_manager.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 43743d39e..e6f1a0ae8 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -30,6 +30,8 @@ constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1); constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2); constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20); +constexpr const char* ACC_SAVE_AVATORS_BASE_PATH = "/system/save/8000000000000010/su/avators/"; + const UUID& UUID::Generate() { std::random_device device; std::mt19937 gen(device()); @@ -45,11 +47,11 @@ ProfileManager::ProfileManager() { if (user_count == 0) CreateNewUser(UUID{}.Generate(), "yuzu"); - auto current = Settings::values.current_user; - if (!GetAllUsers()[current]) + auto current = std::clamp(Settings::values.current_user, 0, MAX_USERS - 1); + if (UserExistsIndex(current)) current = 0; - OpenUser(GetAllUsers()[current]); + OpenUser(*GetUser(current)); } ProfileManager::~ProfileManager() { @@ -126,6 +128,12 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) return CreateNewUser(uuid, username_output); } +boost::optional ProfileManager::GetUser(std::size_t index) const { + if (index >= MAX_USERS) + return boost::none; + return profiles[index].user_uuid; +} + /// Returns a users profile index based on their user id. boost::optional ProfileManager::GetUserIndex(const UUID& uuid) const { if (!uuid) { @@ -189,6 +197,12 @@ bool ProfileManager::UserExists(UUID uuid) const { return (GetUserIndex(uuid) != boost::none); } +bool ProfileManager::UserExistsIndex(std::size_t index) const { + if (index >= MAX_USERS) + return false; + return profiles[index].user_uuid.uuid != INVALID_UUID; +} + /// Opens a specific user void ProfileManager::OpenUser(UUID uuid) { auto idx = GetUserIndex(uuid); @@ -292,7 +306,7 @@ bool ProfileManager::SetProfileBase(UUID uuid, const ProfileBase& profile_new) { void ProfileManager::ParseUserSaveFile() { FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + - "/system/save/8000000000000010/su/avators/profiles.dat", + ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat", "rb"); ProfileDataRaw data; @@ -322,7 +336,7 @@ void ProfileManager::WriteUserSaveFile() { } FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + - "/system/save/8000000000000010/su/avators/profiles.dat", + ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat", "wb"); save.Resize(sizeof(ProfileDataRaw)); -- cgit v1.2.3 From bfad41b0c12a308b0a5a10e3162d74140e3c121a Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 14 Oct 2018 14:49:32 -0400 Subject: profile_manager: Create save data if it doesn't exist on use --- src/core/hle/service/acc/profile_manager.cpp | 48 +++++++++++++++++++++------- 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index e6f1a0ae8..06f7d1b15 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -30,22 +30,20 @@ constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1); constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2); constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20); -constexpr const char* ACC_SAVE_AVATORS_BASE_PATH = "/system/save/8000000000000010/su/avators/"; +constexpr char ACC_SAVE_AVATORS_BASE_PATH[] = "/system/save/8000000000000010/su/avators/"; -const UUID& UUID::Generate() { +UUID UUID::Generate() { std::random_device device; std::mt19937 gen(device()); std::uniform_int_distribution distribution(1, std::numeric_limits::max()); - uuid[0] = distribution(gen); - uuid[1] = distribution(gen); - return *this; + return UUID{distribution(gen), distribution(gen)}; } ProfileManager::ProfileManager() { ParseUserSaveFile(); if (user_count == 0) - CreateNewUser(UUID{}.Generate(), "yuzu"); + CreateNewUser(UUID::Generate(), "yuzu"); auto current = std::clamp(Settings::values.current_user, 0, MAX_USERS - 1); if (UserExistsIndex(current)) @@ -309,10 +307,18 @@ void ProfileManager::ParseUserSaveFile() { ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat", "rb"); + if (!save.IsOpen()) { + LOG_WARNING(Service_ACC, "Failed to load profile data from save data... Generating new " + "user 'yuzu' with random UUID."); + return; + } + ProfileDataRaw data; - save.Seek(0, SEEK_SET); - if (save.ReadBytes(&data, sizeof(ProfileDataRaw)) != sizeof(ProfileDataRaw)) + if (save.ReadBytes(&data, sizeof(ProfileDataRaw)) != sizeof(ProfileDataRaw)) { + LOG_WARNING(Service_ACC, "profiles.dat is smaller than expected... Generating new user " + "'yuzu' with random UUID."); return; + } for (std::size_t i = 0; i < MAX_USERS; ++i) { const auto& user = data.users[i]; @@ -335,12 +341,30 @@ void ProfileManager::WriteUserSaveFile() { raw.users[i].timestamp = profiles[i].creation_time; } - FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + - ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat", - "wb"); + const auto raw_path = + FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010"; + if (FileUtil::Exists(raw_path) && !FileUtil::IsDirectory(raw_path)) + FileUtil::Delete(raw_path); + + const auto path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + + ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat"; + + if (!FileUtil::CreateFullPath(path)) { + LOG_WARNING(Service_ACC, "Failed to create full path of profiles.dat. Create the directory " + "nand/system/save/8000000000000010/su/avators to mitigate this " + "issue."); + return; + } + + FileUtil::IOFile save(path, "wb"); + + if (!save.IsOpen()) { + LOG_WARNING(Service_ACC, "Failed to write save data to file... No changes to user data " + "made in current session will be saved."); + return; + } save.Resize(sizeof(ProfileDataRaw)); - save.Seek(0, SEEK_SET); save.WriteBytes(&raw, sizeof(ProfileDataRaw)); } -- cgit v1.2.3