From e7e3d5898e4750e8ca8d859791dddf27705819b9 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 21:48:35 -0400 Subject: settings: Add users and current_user settings and remove username --- src/core/settings.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/settings.h b/src/core/settings.h index ca80718e2..0fa726d5d 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -8,6 +8,7 @@ #include #include #include "common/common_types.h" +#include "core/hle/service/acc/profile_manager.h" namespace Settings { @@ -114,7 +115,8 @@ struct Values { // System bool use_docked_mode; bool enable_nfc; - std::string username; + int current_user; + std::vector> users; int language_index; // Controls -- cgit v1.2.3 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 ++++++---- src/core/hle/service/acc/profile_manager.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'src/core') 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; diff --git a/src/core/hle/service/acc/profile_manager.h b/src/core/hle/service/acc/profile_manager.h index bffd4cf4d..9ce3eb47c 100644 --- a/src/core/hle/service/acc/profile_manager.h +++ b/src/core/hle/service/acc/profile_manager.h @@ -81,7 +81,7 @@ static_assert(sizeof(ProfileBase) == 0x38, "ProfileBase is an invalid size"); /// objects class ProfileManager { public: - ProfileManager(); // TODO(ogniK): Load from system save + ProfileManager(); ~ProfileManager(); ResultCode AddUser(const ProfileInfo& user); -- cgit v1.2.3 From d3fbf45705e03b992f0ada890cabeac88b86ba3c Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 21:49:29 -0400 Subject: am: Pass current user UUID to launch parameters --- src/core/hle/service/am/am.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/core') diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index ecf72ae24..2dc647ec8 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -26,6 +26,8 @@ namespace Service::AM { +constexpr std::size_t POP_LAUNCH_PARAMETER_BUFFER_SIZE = 0x88; + IWindowController::IWindowController() : ServiceFramework("IWindowController") { // clang-format off static const FunctionInfo functions[] = { @@ -724,16 +726,16 @@ void IApplicationFunctions::EndBlockingHomeButton(Kernel::HLERequestContext& ctx } void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) { - constexpr std::array data{{ + constexpr std::array header_data{ 0xca, 0x97, 0x94, 0xc7, // Magic 1, 0, 0, 0, // IsAccountSelected (bool) - 1, 0, 0, 0, // User Id (word 0) - 0, 0, 0, 0, // User Id (word 1) - 0, 0, 0, 0, // User Id (word 2) - 0, 0, 0, 0 // User Id (word 3) - }}; + }; + + std::vector buffer(POP_LAUNCH_PARAMETER_BUFFER_SIZE); - std::vector buffer(data.begin(), data.end()); + std::memcpy(buffer.data(), header_data.data(), header_data.size()); + const auto current_uuid = Settings::values.users[Settings::values.current_user].second.uuid; + std::memcpy(buffer.data() + header_data.size(), current_uuid.data(), sizeof(u128)); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; -- cgit v1.2.3 From 19c5cf9c637d7fb685ca6977fb7cbf06e075cedf Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 22:35:02 -0400 Subject: acc: Load user images from config dir --- src/core/hle/service/acc/acc.cpp | 54 +++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) (limited to 'src/core') diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp index e61748ca3..0149ea8b3 100644 --- a/src/core/hle/service/acc/acc.cpp +++ b/src/core/hle/service/acc/acc.cpp @@ -3,8 +3,11 @@ // Refer to the license.txt file included. #include +#include "common/common_paths.h" #include "common/common_types.h" +#include "common/file_util.h" #include "common/logging/log.h" +#include "common/string_util.h" #include "common/swap.h" #include "core/core_timing.h" #include "core/hle/ipc_helpers.h" @@ -16,6 +19,9 @@ #include "core/hle/service/acc/profile_manager.h" namespace Service::Account { + +constexpr u32 MAX_JPEG_IMAGE_SIZE = 0x20000; + // TODO: RE this structure struct UserData { INSERT_PADDING_WORDS(1); @@ -27,6 +33,11 @@ struct UserData { }; static_assert(sizeof(UserData) == 0x80, "UserData structure has incorrect size"); +static std::string GetImagePath(const std::string& username) { + return FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "users" + DIR_SEP + username + + ".jpg"; +} + class IProfile final : public ServiceFramework { public: explicit IProfile(UUID user_id, ProfileManager& profile_manager) @@ -38,6 +49,15 @@ public: {11, &IProfile::LoadImage, "LoadImage"}, }; RegisterHandlers(functions); + + ProfileBase profile_base{}; + if (profile_manager.GetProfileBase(user_id, profile_base)) { + image = std::make_unique( + GetImagePath(Common::StringFromFixedZeroTerminatedBuffer( + reinterpret_cast(profile_base.username.data()), + profile_base.username.size())), + "rb"); + } } private: @@ -73,11 +93,11 @@ private: } void LoadImage(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_ACC, "(STUBBED) called"); + LOG_DEBUG(Service_ACC, "called"); // smallest jpeg https://github.com/mathiasbynens/small/blob/master/jpeg.jpg - // TODO(mailwl): load actual profile image from disk, width 256px, max size 0x20000 - constexpr u32 jpeg_size = 107; - static constexpr std::array jpeg{ + // used as a backup should the one on disk not exist + constexpr u32 backup_jpeg_size = 107; + static constexpr std::array backup_jpeg{ 0xff, 0xd8, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x08, 0x06, 0x06, 0x05, 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09, 0x08, 0x09, 0x09, 0x0a, @@ -87,22 +107,38 @@ private: 0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08, 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9, }; - ctx.WriteBuffer(jpeg); + IPC::ResponseBuilder rb{ctx, 3}; rb.Push(RESULT_SUCCESS); - rb.Push(jpeg_size); + + if (image == nullptr) { + ctx.WriteBuffer(backup_jpeg); + rb.Push(backup_jpeg_size); + } else { + const auto size = std::min(image->GetSize(), MAX_JPEG_IMAGE_SIZE); + std::vector buffer(size); + image->ReadBytes(buffer.data(), buffer.size()); + + ctx.WriteBuffer(buffer.data(), buffer.size()); + rb.Push(buffer.size()); + } } void GetImageSize(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_ACC, "(STUBBED) called"); - constexpr u32 jpeg_size = 107; + LOG_DEBUG(Service_ACC, "called"); + constexpr u32 backup_jpeg_size = 107; IPC::ResponseBuilder rb{ctx, 3}; rb.Push(RESULT_SUCCESS); - rb.Push(jpeg_size); + + if (image == nullptr) + rb.Push(backup_jpeg_size); + else + rb.Push(std::min(image->GetSize(), MAX_JPEG_IMAGE_SIZE)); } const ProfileManager& profile_manager; UUID user_id; ///< The user id this profile refers to. + std::unique_ptr image = nullptr; }; class IManagerForApplication final : public ServiceFramework { -- 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/acc.cpp | 31 ++++---- src/core/hle/service/acc/profile_manager.cpp | 101 +++++++++++++++++++++++++-- src/core/hle/service/acc/profile_manager.h | 15 ++++ src/core/hle/service/am/am.cpp | 8 ++- src/core/settings.h | 2 - 5 files changed, 129 insertions(+), 28 deletions(-) (limited to 'src/core') diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp index 0149ea8b3..cee309cb1 100644 --- a/src/core/hle/service/acc/acc.cpp +++ b/src/core/hle/service/acc/acc.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include #include "common/common_paths.h" #include "common/common_types.h" @@ -33,9 +34,9 @@ struct UserData { }; static_assert(sizeof(UserData) == 0x80, "UserData structure has incorrect size"); -static std::string GetImagePath(const std::string& username) { - return FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "users" + DIR_SEP + username + - ".jpg"; +static std::string GetImagePath(UUID uuid) { + return FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + + "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg"; } class IProfile final : public ServiceFramework { @@ -49,15 +50,6 @@ public: {11, &IProfile::LoadImage, "LoadImage"}, }; RegisterHandlers(functions); - - ProfileBase profile_base{}; - if (profile_manager.GetProfileBase(user_id, profile_base)) { - image = std::make_unique( - GetImagePath(Common::StringFromFixedZeroTerminatedBuffer( - reinterpret_cast(profile_base.username.data()), - profile_base.username.size())), - "rb"); - } } private: @@ -111,13 +103,15 @@ private: IPC::ResponseBuilder rb{ctx, 3}; rb.Push(RESULT_SUCCESS); - if (image == nullptr) { + const FileUtil::IOFile image(GetImagePath(user_id), "rb"); + + if (!image.IsOpen()) { ctx.WriteBuffer(backup_jpeg); rb.Push(backup_jpeg_size); } else { - const auto size = std::min(image->GetSize(), MAX_JPEG_IMAGE_SIZE); + const auto size = std::min(image.GetSize(), MAX_JPEG_IMAGE_SIZE); std::vector buffer(size); - image->ReadBytes(buffer.data(), buffer.size()); + image.ReadBytes(buffer.data(), buffer.size()); ctx.WriteBuffer(buffer.data(), buffer.size()); rb.Push(buffer.size()); @@ -130,15 +124,16 @@ private: IPC::ResponseBuilder rb{ctx, 3}; rb.Push(RESULT_SUCCESS); - if (image == nullptr) + const FileUtil::IOFile image(GetImagePath(user_id), "rb"); + + if (!image.IsOpen()) rb.Push(backup_jpeg_size); else - rb.Push(std::min(image->GetSize(), MAX_JPEG_IMAGE_SIZE)); + rb.Push(std::min(image.GetSize(), MAX_JPEG_IMAGE_SIZE)); } const ProfileManager& profile_manager; UUID user_id; ///< The user id this profile refers to. - std::unique_ptr image = nullptr; }; class IManagerForApplication final : public ServiceFramework { 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 diff --git a/src/core/hle/service/acc/profile_manager.h b/src/core/hle/service/acc/profile_manager.h index 9ce3eb47c..1e5c2460e 100644 --- a/src/core/hle/service/acc/profile_manager.h +++ b/src/core/hle/service/acc/profile_manager.h @@ -45,6 +45,15 @@ struct UUID { std::string Format() const { return fmt::format("0x{:016X}{:016X}", uuid[1], uuid[0]); } + + std::string FormatSwitch() const { + std::array s{}; + std::memcpy(s.data(), uuid.data(), sizeof(u128)); + return fmt::format("{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{" + ":02x}{:02x}{:02x}{:02x}{:02x}", + s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], s[8], s[9], s[10], s[11], + s[12], s[13], s[14], s[15]); + } }; static_assert(sizeof(UUID) == 16, "UUID is an invalid size!"); @@ -108,7 +117,13 @@ public: bool CanSystemRegisterUser() const; + bool RemoveUser(UUID uuid); + bool SetProfileBase(UUID uuid, const ProfileBase& profile); + private: + void ParseUserSaveFile(); + void WriteUserSaveFile(); + std::array profiles{}; std::size_t user_count = 0; boost::optional AddToProfiles(const ProfileInfo& profile); diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 2dc647ec8..9dfcec59b 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -4,11 +4,13 @@ #include #include +#include #include #include "core/core.h" #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/event.h" #include "core/hle/kernel/process.h" +#include "core/hle/service/acc/profile_manager.h" #include "core/hle/service/am/am.h" #include "core/hle/service/am/applet_ae.h" #include "core/hle/service/am/applet_oe.h" @@ -734,8 +736,10 @@ void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) { std::vector buffer(POP_LAUNCH_PARAMETER_BUFFER_SIZE); std::memcpy(buffer.data(), header_data.data(), header_data.size()); - const auto current_uuid = Settings::values.users[Settings::values.current_user].second.uuid; - std::memcpy(buffer.data() + header_data.size(), current_uuid.data(), sizeof(u128)); + + Account::ProfileManager profile_manager{}; + const auto uuid = profile_manager.GetAllUsers()[Settings::values.current_user].uuid; + std::memcpy(buffer.data() + header_data.size(), uuid.data(), sizeof(u128)); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; diff --git a/src/core/settings.h b/src/core/settings.h index 0fa726d5d..b5aeff29b 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -8,7 +8,6 @@ #include #include #include "common/common_types.h" -#include "core/hle/service/acc/profile_manager.h" namespace Settings { @@ -116,7 +115,6 @@ struct Values { bool use_docked_mode; bool enable_nfc; int current_user; - std::vector> users; int language_index; // Controls -- 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') 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/acc.cpp | 9 +++++++-- src/core/hle/service/acc/profile_manager.cpp | 24 ++++++++++++++++++----- src/core/hle/service/acc/profile_manager.h | 2 ++ src/core/hle/service/am/am.cpp | 29 ++++++++++++++++++---------- 4 files changed, 47 insertions(+), 17 deletions(-) (limited to 'src/core') diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp index cee309cb1..cf065c2e0 100644 --- a/src/core/hle/service/acc/acc.cpp +++ b/src/core/hle/service/acc/acc.cpp @@ -106,6 +106,8 @@ private: const FileUtil::IOFile image(GetImagePath(user_id), "rb"); if (!image.IsOpen()) { + LOG_WARNING(Service_ACC, + "Failed to load user provided image! Falling back to built-in backup..."); ctx.WriteBuffer(backup_jpeg); rb.Push(backup_jpeg_size); } else { @@ -126,10 +128,13 @@ private: const FileUtil::IOFile image(GetImagePath(user_id), "rb"); - if (!image.IsOpen()) + if (!image.IsOpen()) { + LOG_WARNING(Service_ACC, + "Failed to load user provided image! Falling back to built-in backup..."); rb.Push(backup_jpeg_size); - else + } else { rb.Push(std::min(image.GetSize(), MAX_JPEG_IMAGE_SIZE)); + } } const ProfileManager& profile_manager; 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)); diff --git a/src/core/hle/service/acc/profile_manager.h b/src/core/hle/service/acc/profile_manager.h index 1e5c2460e..482c1d8a9 100644 --- a/src/core/hle/service/acc/profile_manager.h +++ b/src/core/hle/service/acc/profile_manager.h @@ -96,6 +96,7 @@ public: ResultCode AddUser(const ProfileInfo& user); ResultCode CreateNewUser(UUID uuid, const ProfileUsername& username); ResultCode CreateNewUser(UUID uuid, const std::string& username); + boost::optional GetUser(std::size_t index) const; boost::optional GetUserIndex(const UUID& uuid) const; boost::optional GetUserIndex(const ProfileInfo& user) const; bool GetProfileBase(boost::optional index, ProfileBase& profile) const; @@ -109,6 +110,7 @@ public: std::size_t GetUserCount() const; std::size_t GetOpenUserCount() const; bool UserExists(UUID uuid) const; + bool UserExistsIndex(std::size_t index) const; void OpenUser(UUID uuid); void CloseUser(UUID uuid); UserIDArray GetOpenUsers() const; diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 9dfcec59b..4ed66d817 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -28,7 +28,15 @@ namespace Service::AM { -constexpr std::size_t POP_LAUNCH_PARAMETER_BUFFER_SIZE = 0x88; +constexpr u32 POP_LAUNCH_PARAMETER_MAGIC = 0xC79497CA; + +struct LaunchParameters { + u32_le magic; + u32_le is_account_selected; + u128 current_user; + INSERT_PADDING_BYTES(0x70); +}; +static_assert(sizeof(LaunchParameters) == 0x88); IWindowController::IWindowController() : ServiceFramework("IWindowController") { // clang-format off @@ -728,22 +736,23 @@ void IApplicationFunctions::EndBlockingHomeButton(Kernel::HLERequestContext& ctx } void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) { - constexpr std::array header_data{ - 0xca, 0x97, 0x94, 0xc7, // Magic - 1, 0, 0, 0, // IsAccountSelected (bool) - }; + LaunchParameters params{}; - std::vector buffer(POP_LAUNCH_PARAMETER_BUFFER_SIZE); - - std::memcpy(buffer.data(), header_data.data(), header_data.size()); + params.magic = POP_LAUNCH_PARAMETER_MAGIC; + params.is_account_selected = 1; Account::ProfileManager profile_manager{}; - const auto uuid = profile_manager.GetAllUsers()[Settings::values.current_user].uuid; - std::memcpy(buffer.data() + header_data.size(), uuid.data(), sizeof(u128)); + const auto uuid = profile_manager.GetUser(Settings::values.current_user); + ASSERT(uuid != boost::none); + params.current_user = uuid->uuid; IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); + + std::vector buffer(sizeof(LaunchParameters)); + std::memcpy(buffer.data(), ¶ms, buffer.size()); + rb.PushIpcInterface(buffer); LOG_DEBUG(Service_AM, "called"); -- 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 +++++++++++++++++++++------- src/core/hle/service/acc/profile_manager.h | 2 +- 2 files changed, 37 insertions(+), 13 deletions(-) (limited to 'src/core') 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)); } diff --git a/src/core/hle/service/acc/profile_manager.h b/src/core/hle/service/acc/profile_manager.h index 482c1d8a9..235208d56 100644 --- a/src/core/hle/service/acc/profile_manager.h +++ b/src/core/hle/service/acc/profile_manager.h @@ -36,7 +36,7 @@ struct UUID { } // TODO(ogniK): Properly generate uuids based on RFC-4122 - const UUID& Generate(); + static UUID Generate(); // Set the UUID to {0,0} to be considered an invalid user void Invalidate() { -- cgit v1.2.3