From e7e3d5898e4750e8ca8d859791dddf27705819b9 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 21:48:35 -0400 Subject: [PATCH 01/11] settings: Add users and current_user settings and remove username --- src/core/settings.h | 4 +++- src/yuzu/configuration/config.cpp | 34 +++++++++++++++++++++++++++++-- src/yuzu_cmd/config.cpp | 22 +++++++++++++++++--- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/core/settings.h b/src/core/settings.h index ca80718e27..0fa726d5d1 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 diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index d029590ff3..36f0c4f4c8 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -123,7 +123,25 @@ void Config::ReadValues() { qt_config->beginGroup("System"); Settings::values.use_docked_mode = qt_config->value("use_docked_mode", false).toBool(); Settings::values.enable_nfc = qt_config->value("enable_nfc", true).toBool(); - Settings::values.username = qt_config->value("username", "yuzu").toString().toStdString(); + + Settings::values.users.clear(); + const auto size = qt_config->beginReadArray("users"); + for (int i = 0; i < size; ++i) { + qt_config->setArrayIndex(i); + const Service::Account::UUID uuid(qt_config->value("uuid_low").toULongLong(), + qt_config->value("uuid_high").toULongLong()); + Settings::values.users.emplace_back(qt_config->value("username").toString().toStdString(), + uuid); + } + + qt_config->endArray(); + + if (Settings::values.users.empty()) + Settings::values.users.emplace_back("yuzu", Service::Account::UUID{}.Generate()); + + Settings::values.current_user = + std::clamp(qt_config->value("current_user", 0).toInt(), 0, size); + Settings::values.language_index = qt_config->value("language_index", 1).toInt(); qt_config->endGroup(); @@ -260,7 +278,19 @@ void Config::SaveValues() { qt_config->beginGroup("System"); qt_config->setValue("use_docked_mode", Settings::values.use_docked_mode); qt_config->setValue("enable_nfc", Settings::values.enable_nfc); - qt_config->setValue("username", QString::fromStdString(Settings::values.username)); + qt_config->setValue("current_user", Settings::values.current_user); + + qt_config->beginWriteArray("users", Settings::values.users.size()); + for (std::size_t i = 0; i < Settings::values.users.size(); ++i) { + qt_config->setArrayIndex(i); + const auto& user = Settings::values.users[i]; + qt_config->setValue("uuid_low", user.second.uuid[0]); + qt_config->setValue("uuid_high", user.second.uuid[1]); + qt_config->setValue("username", QString::fromStdString(user.first)); + } + + qt_config->endArray(); + qt_config->setValue("language_index", Settings::values.language_index); qt_config->endGroup(); diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index 654a15a5cf..613894449e 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -126,9 +126,25 @@ void Config::ReadValues() { // System Settings::values.use_docked_mode = sdl2_config->GetBoolean("System", "use_docked_mode", false); Settings::values.enable_nfc = sdl2_config->GetBoolean("System", "enable_nfc", true); - Settings::values.username = sdl2_config->Get("System", "username", "yuzu"); - if (Settings::values.username.empty()) { - Settings::values.username = "yuzu"; + const auto size = sdl2_config->GetInteger("System", "users_size", 0); + + Settings::values.users.clear(); + for (std::size_t i = 0; i < size; ++i) { + const auto uuid_low = std::stoull( + sdl2_config->Get("System", fmt::format("users_{}_uuid_low", i), "0"), nullptr, 0); + const auto uuid_high = std::stoull( + sdl2_config->Get("System", fmt::format("users_{}_uuid_high", i), "0"), nullptr, 0); + Settings::values.users.emplace_back( + sdl2_config->Get("System", fmt::format("users_{}_username", i), ""), + Service::Account::UUID{uuid_low, uuid_high}); + } + + if (Settings::values.users.empty()) { + Settings::values.users.emplace_back("yuzu", Service::Account::UUID{1, 0}); + LOG_WARNING( + Config, + "You are using the default UUID of {1, 0}! This might cause issues down the road! " + "Please consider randomizing a UUID and adding it to the sdl2_config.ini file."); } // Miscellaneous From aeffd4b436dceb798b4ffc1f8babb350a741280a Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 21:49:06 -0400 Subject: [PATCH 02/11] 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(-) diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index bcb3475db6..b4b4b52b79 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 bffd4cf4d8..9ce3eb47cf 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); From d3fbf45705e03b992f0ada890cabeac88b86ba3c Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 21:49:29 -0400 Subject: [PATCH 03/11] am: Pass current user UUID to launch parameters --- src/core/hle/service/am/am.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index ecf72ae24e..2dc647ec86 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(data.begin(), data.end()); + 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)); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; From b2a8209c5be07f045ad6823eb6a0246a9a537a34 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 21:53:04 -0400 Subject: [PATCH 04/11] qt: Add Profile Manager UI to system settings --- src/yuzu/configuration/configure_system.cpp | 165 ++++++++++++- src/yuzu/configuration/configure_system.h | 19 ++ src/yuzu/configuration/configure_system.ui | 250 ++++++++++++++------ 3 files changed, 354 insertions(+), 80 deletions(-) diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index e9ed9c38f7..9a41c1f6ce 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -2,7 +2,12 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include +#include #include +#include +#include "common/common_paths.h" +#include "common/logging/backend.h" #include "core/core.h" #include "core/settings.h" #include "ui_configure_system.h" @@ -24,6 +29,17 @@ static const std::array days_in_month = {{ 31, }}; +// Same backup JPEG used by acc IProfile::GetImage if no jpeg found +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, 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e, + 0x0b, 0x09, 0x09, 0x0d, 0x11, 0x0d, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x10, 0x0a, 0x0c, 0x12, 0x13, + 0x12, 0x10, 0x13, 0x0f, 0x10, 0x10, 0x10, 0xff, 0xc9, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01, + 0x01, 0x01, 0x11, 0x00, 0xff, 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, 0xff, 0xda, 0x00, 0x08, + 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9, +}; + ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui::ConfigureSystem) { ui->setupUi(this); connect(ui->combo_birthmonth, @@ -32,6 +48,44 @@ ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui:: connect(ui->button_regenerate_console_id, &QPushButton::clicked, this, &ConfigureSystem::refreshConsoleID); + layout = new QVBoxLayout; + tree_view = new QTreeView; + item_model = new QStandardItemModel(tree_view); + tree_view->setModel(item_model); + + tree_view->setAlternatingRowColors(true); + tree_view->setSelectionMode(QHeaderView::SingleSelection); + tree_view->setSelectionBehavior(QHeaderView::SelectRows); + tree_view->setVerticalScrollMode(QHeaderView::ScrollPerPixel); + tree_view->setHorizontalScrollMode(QHeaderView::ScrollPerPixel); + tree_view->setSortingEnabled(true); + tree_view->setEditTriggers(QHeaderView::NoEditTriggers); + tree_view->setUniformRowHeights(true); + tree_view->setIconSize({64, 64}); + tree_view->setContextMenuPolicy(Qt::NoContextMenu); + + item_model->insertColumns(0, 1); + item_model->setHeaderData(0, Qt::Horizontal, "Users"); + + // We must register all custom types with the Qt Automoc system so that we are able to use it + // with signals/slots. In this case, QList falls under the umbrells of custom types. + qRegisterMetaType>("QList"); + + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(0); + layout->addWidget(tree_view); + + ui->scrollArea->setLayout(layout); + + connect(tree_view, &QTreeView::clicked, this, &ConfigureSystem::SelectUser); + + connect(ui->pm_add, &QPushButton::pressed, this, &ConfigureSystem::AddUser); + connect(ui->pm_rename, &QPushButton::pressed, this, &ConfigureSystem::RenameUser); + connect(ui->pm_remove, &QPushButton::pressed, this, &ConfigureSystem::DeleteUser); + + scene = new QGraphicsScene; + ui->current_user_icon->setScene(scene); + this->setConfiguration(); } @@ -39,8 +93,51 @@ ConfigureSystem::~ConfigureSystem() = default; void ConfigureSystem::setConfiguration() { enabled = !Core::System::GetInstance().IsPoweredOn(); - ui->edit_username->setText(QString::fromStdString(Settings::values.username)); + ui->combo_language->setCurrentIndex(Settings::values.language_index); + + item_model->removeRows(0, item_model->rowCount()); + list_items.clear(); + + std::transform(Settings::values.users.begin(), Settings::values.users.end(), + std::back_inserter(list_items), + [](const std::pair& user) { + const auto icon_url = QString::fromStdString( + FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "users" + + DIR_SEP + user.first + ".jpg"); + QPixmap icon{icon_url}; + + if (!icon) { + icon.fill(QColor::fromRgb(0, 0, 0)); + icon.loadFromData(backup_jpeg.data(), backup_jpeg.size()); + } + + return QList{new QStandardItem{ + icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(user.first + "\n" + user.second.Format())}}; + }); + + for (const auto& item : list_items) + item_model->appendRow(item); + + UpdateCurrentUser(); +} + +void ConfigureSystem::UpdateCurrentUser() { + const auto& current_user = Settings::values.users[Settings::values.current_user]; + const auto icon_url = + QString::fromStdString(FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "users" + + DIR_SEP + current_user.first + ".jpg"); + QPixmap icon{icon_url}; + + if (!icon) { + icon.fill(QColor::fromRgb(0, 0, 0)); + icon.loadFromData(backup_jpeg.data(), backup_jpeg.size()); + } + + scene->clear(); + scene->addPixmap(icon.scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); + ui->current_user_username->setText(QString::fromStdString(current_user.first)); } void ConfigureSystem::ReadSystemSettings() {} @@ -48,7 +145,7 @@ void ConfigureSystem::ReadSystemSettings() {} void ConfigureSystem::applyConfiguration() { if (!enabled) return; - Settings::values.username = ui->edit_username->text().toStdString(); + Settings::values.language_index = ui->combo_language->currentIndex(); Settings::Apply(); } @@ -92,3 +189,67 @@ void ConfigureSystem::refreshConsoleID() { ui->label_console_id->setText( tr("Console ID: 0x%1").arg(QString::number(console_id, 16).toUpper())); } + +void ConfigureSystem::SelectUser(const QModelIndex& index) { + Settings::values.current_user = + std::clamp(index.row(), 0, Settings::values.users.size() - 1); + + UpdateCurrentUser(); + + if (Settings::values.users.size() >= 2) + ui->pm_remove->setEnabled(true); + else + ui->pm_remove->setEnabled(false); + + ui->pm_rename->setEnabled(true); +} + +void ConfigureSystem::AddUser() { + Service::Account::UUID uuid; + uuid.Generate(); + + bool ok = false; + const auto username = + QInputDialog::getText(this, tr("Enter Username"), tr("Enter a username for the new user:"), + QLineEdit::Normal, QString(), &ok); + + Settings::values.users.emplace_back(username.toStdString(), uuid); + + setConfiguration(); +} + +void ConfigureSystem::RenameUser() { + const auto user = tree_view->currentIndex().row(); + + bool ok = false; + const auto new_username = QInputDialog::getText( + this, tr("Enter Username"), tr("Enter a new username:"), QLineEdit::Normal, + QString::fromStdString(Settings::values.users[user].first), &ok); + + if (!ok) + return; + + Settings::values.users[user].first = new_username.toStdString(); + + setConfiguration(); +} + +void ConfigureSystem::DeleteUser() { + const auto user = Settings::values.users.begin() + tree_view->currentIndex().row(); + const auto confirm = QMessageBox::question( + this, tr("Confirm Delete"), + tr("You are about to delete user with name %1. Are you sure?").arg(user->first.c_str())); + + if (confirm == QMessageBox::No) + return; + + if (Settings::values.current_user == tree_view->currentIndex().row()) + Settings::values.current_user = 0; + + Settings::values.users.erase(user); + + setConfiguration(); + + ui->pm_remove->setEnabled(false); + ui->pm_rename->setEnabled(false); +} diff --git a/src/yuzu/configuration/configure_system.h b/src/yuzu/configuration/configure_system.h index f13de17d44..aa20a3c306 100644 --- a/src/yuzu/configuration/configure_system.h +++ b/src/yuzu/configuration/configure_system.h @@ -5,6 +5,11 @@ #pragma once #include +#include +#include +#include +#include +#include #include namespace Ui { @@ -21,13 +26,27 @@ public: void applyConfiguration(); void setConfiguration(); + void UpdateCurrentUser(); + public slots: void updateBirthdayComboBox(int birthmonth_index); void refreshConsoleID(); + void SelectUser(const QModelIndex& index); + void AddUser(); + void RenameUser(); + void DeleteUser(); + private: void ReadSystemSettings(); + QVBoxLayout* layout; + QTreeView* tree_view; + QStandardItemModel* item_model; + QGraphicsScene* scene; + + std::vector> list_items; + std::unique_ptr ui; bool enabled; diff --git a/src/yuzu/configuration/configure_system.ui b/src/yuzu/configuration/configure_system.ui index f3f8db038d..2a6dcdb24f 100644 --- a/src/yuzu/configuration/configure_system.ui +++ b/src/yuzu/configuration/configure_system.ui @@ -7,7 +7,7 @@ 0 0 360 - 377 + 483 @@ -22,34 +22,28 @@ System Settings - - - - Username - - - - - - - - 0 - 0 - - - - 32 - - - + + + Language + + + + Birthday - + + + + Console ID: + + + + @@ -120,14 +114,7 @@ - - - - Language - - - - + Note: this can be overridden when region setting is auto-select @@ -187,31 +174,31 @@ Russian (Русский) - - - Taiwanese - - - - - British English - - - - - Canadian French - - - - - Latin American Spanish - - - - - Simplified Chinese - - + + + Taiwanese + + + + + British English + + + + + Canadian French + + + + + Latin American Spanish + + + + + Simplified Chinese + + Traditional Chinese (正體中文) @@ -219,14 +206,14 @@ - + Sound output mode - + @@ -245,14 +232,7 @@ - - - - Console ID: - - - - + @@ -271,6 +251,133 @@ + + + + Profile Manager + + + + QLayout::SetNoConstraint + + + + + + + + 0 + 0 + + + + Current User + + + + + + + + 48 + 48 + + + + + 48 + 48 + + + + Qt::ScrollBarAlwaysOff + + + Qt::ScrollBarAlwaysOff + + + false + + + + + + + + 0 + 0 + + + + Username + + + + + + + + + + 0 + 0 + + + + QFrame::StyledPanel + + + false + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Add + + + + + + + false + + + Rename + + + + + + + false + + + Remove + + + + + + + + @@ -281,19 +388,6 @@ - - - - Qt::Vertical - - - - 20 - 40 - - - - From 466960c8ab3c10091058e4472d7d2b3aa3c808f0 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 21:53:26 -0400 Subject: [PATCH 05/11] qt: Allow user to select emu user on open save data --- src/yuzu/main.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index be98966145..1de3b817f8 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -757,12 +757,33 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target open_target = "Save Data"; const std::string nand_dir = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); ASSERT(program_id != 0); - // TODO(tech4me): Update this to work with arbitrary user profile - // Refer to core/hle/service/acc/profile_manager.cpp ProfileManager constructor - constexpr u128 user_id = {1, 0}; + + QStringList list{}; + std::transform(Settings::values.users.begin(), Settings::values.users.end(), + std::back_inserter(list), + [](const auto& user) { return QString::fromStdString(user.first); }); + + bool ok = false; + const auto index_string = + QInputDialog::getItem(this, tr("Select User"), + tr("Please select the user's save data you would like to open."), + list, Settings::values.current_user, false, &ok); + if (!ok) + return; + + const auto index = list.indexOf(index_string); + ASSERT(index != -1); + + const auto user_id = Settings::values.users[index].second.uuid; path = nand_dir + FileSys::SaveDataFactory::GetFullPath(FileSys::SaveDataSpaceId::NandUser, FileSys::SaveDataType::SaveData, program_id, user_id, 0); + + if (!FileUtil::Exists(path)) { + FileUtil::CreateFullPath(path); + FileUtil::CreateDir(path); + } + break; } case GameListOpenTarget::ModData: { From 19c5cf9c637d7fb685ca6977fb7cbf06e075cedf Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 9 Oct 2018 22:35:02 -0400 Subject: [PATCH 06/11] acc: Load user images from config dir --- src/core/hle/service/acc/acc.cpp | 54 ++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp index e61748ca38..0149ea8b3d 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 { From 702622b8f1eaa1b297a27a305ac56faeadf542d7 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Wed, 10 Oct 2018 21:49:20 -0400 Subject: [PATCH 07/11] 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 - src/yuzu/configuration/config.cpp | 30 +--- src/yuzu/configuration/configure_system.cpp | 179 ++++++++++++++----- src/yuzu/configuration/configure_system.h | 18 +- src/yuzu/configuration/configure_system.ui | 10 ++ src/yuzu/main.cpp | 27 ++- src/yuzu_cmd/config.cpp | 20 +-- 11 files changed, 308 insertions(+), 133 deletions(-) diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp index 0149ea8b3d..cee309cb1f 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 b4b4b52b79..b0ea06b480 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(); - OpenUser(Settings::values.users[Settings::values.current_user].second); + if (user_count == 0) + CreateNewUser(UUID{}.Generate(), "yuzu"); + + auto current = Settings::values.current_user; + if (!GetAllUsers()[current]) + current = 0; + + 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 9ce3eb47cf..1e5c2460e1 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 2dc647ec86..9dfcec59bc 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 0fa726d5d1..b5aeff29b1 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 diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 36f0c4f4c8..f7a9a8dd49 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -4,6 +4,7 @@ #include #include "common/file_util.h" +#include "core/hle/service/acc/profile_manager.h" #include "input_common/main.h" #include "yuzu/configuration/config.h" #include "yuzu/ui_settings.h" @@ -124,23 +125,7 @@ void Config::ReadValues() { Settings::values.use_docked_mode = qt_config->value("use_docked_mode", false).toBool(); Settings::values.enable_nfc = qt_config->value("enable_nfc", true).toBool(); - Settings::values.users.clear(); - const auto size = qt_config->beginReadArray("users"); - for (int i = 0; i < size; ++i) { - qt_config->setArrayIndex(i); - const Service::Account::UUID uuid(qt_config->value("uuid_low").toULongLong(), - qt_config->value("uuid_high").toULongLong()); - Settings::values.users.emplace_back(qt_config->value("username").toString().toStdString(), - uuid); - } - - qt_config->endArray(); - - if (Settings::values.users.empty()) - Settings::values.users.emplace_back("yuzu", Service::Account::UUID{}.Generate()); - - Settings::values.current_user = - std::clamp(qt_config->value("current_user", 0).toInt(), 0, size); + Settings::values.current_user = std::clamp(qt_config->value("current_user", 0).toInt(), 0, 7); Settings::values.language_index = qt_config->value("language_index", 1).toInt(); qt_config->endGroup(); @@ -280,17 +265,6 @@ void Config::SaveValues() { qt_config->setValue("enable_nfc", Settings::values.enable_nfc); qt_config->setValue("current_user", Settings::values.current_user); - qt_config->beginWriteArray("users", Settings::values.users.size()); - for (std::size_t i = 0; i < Settings::values.users.size(); ++i) { - qt_config->setArrayIndex(i); - const auto& user = Settings::values.users[i]; - qt_config->setValue("uuid_low", user.second.uuid[0]); - qt_config->setValue("uuid_high", user.second.uuid[1]); - qt_config->setValue("username", QString::fromStdString(user.first)); - } - - qt_config->endArray(); - qt_config->setValue("language_index", Settings::values.language_index); qt_config->endGroup(); diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index 9a41c1f6ce..af2acdd453 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -2,10 +2,15 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include +#include #include -#include +#include +#include #include -#include +#include +#include +#include #include "common/common_paths.h" #include "common/logging/backend.h" #include "core/core.h" @@ -14,6 +19,11 @@ #include "yuzu/configuration/configure_system.h" #include "yuzu/main.h" +static std::string GetImagePath(Service::Account::UUID uuid) { + return FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + + "/system/save/8000000000000010/su/avators/" + uuid.FormatSwitch() + ".jpg"; +} + static const std::array days_in_month = {{ 31, 29, @@ -40,7 +50,9 @@ static constexpr std::array backup_jpeg{ 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, 0xd2, 0xcf, 0x20, 0xff, 0xd9, }; -ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui::ConfigureSystem) { +ConfigureSystem::ConfigureSystem(QWidget* parent) + : QWidget(parent), ui(new Ui::ConfigureSystem), + profile_manager(std::make_unique()) { ui->setupUi(this); connect(ui->combo_birthmonth, static_cast(&QComboBox::currentIndexChanged), this, @@ -82,6 +94,7 @@ ConfigureSystem::ConfigureSystem(QWidget* parent) : QWidget(parent), ui(new Ui:: connect(ui->pm_add, &QPushButton::pressed, this, &ConfigureSystem::AddUser); connect(ui->pm_rename, &QPushButton::pressed, this, &ConfigureSystem::RenameUser); connect(ui->pm_remove, &QPushButton::pressed, this, &ConfigureSystem::DeleteUser); + connect(ui->pm_set_image, &QPushButton::pressed, this, &ConfigureSystem::SetUserImage); scene = new QGraphicsScene; ui->current_user_icon->setScene(scene); @@ -99,49 +112,69 @@ void ConfigureSystem::setConfiguration() { item_model->removeRows(0, item_model->rowCount()); list_items.clear(); - std::transform(Settings::values.users.begin(), Settings::values.users.end(), - std::back_inserter(list_items), - [](const std::pair& user) { - const auto icon_url = QString::fromStdString( - FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "users" + - DIR_SEP + user.first + ".jpg"); - QPixmap icon{icon_url}; - - if (!icon) { - icon.fill(QColor::fromRgb(0, 0, 0)); - icon.loadFromData(backup_jpeg.data(), backup_jpeg.size()); - } - - return QList{new QStandardItem{ - icon.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(user.first + "\n" + user.second.Format())}}; - }); - - for (const auto& item : list_items) - item_model->appendRow(item); + ui->pm_add->setEnabled(profile_manager->GetUserCount() < 8); + PopulateUserList(); UpdateCurrentUser(); } -void ConfigureSystem::UpdateCurrentUser() { - const auto& current_user = Settings::values.users[Settings::values.current_user]; - const auto icon_url = - QString::fromStdString(FileUtil::GetUserPath(FileUtil::UserPath::ConfigDir) + "users" + - DIR_SEP + current_user.first + ".jpg"); +static QPixmap GetIcon(Service::Account::UUID uuid) { + const auto icon_url = QString::fromStdString(GetImagePath(uuid)); QPixmap icon{icon_url}; if (!icon) { - icon.fill(QColor::fromRgb(0, 0, 0)); + icon.fill(Qt::black); icon.loadFromData(backup_jpeg.data(), backup_jpeg.size()); } + return icon; +} + +void ConfigureSystem::PopulateUserList() { + const auto& profiles = profile_manager->GetAllUsers(); + std::transform( + profiles.begin(), profiles.end(), std::back_inserter(list_items), + [this](const Service::Account::UUID& user) { + Service::Account::ProfileBase profile; + if (!profile_manager->GetProfileBase(user, profile)) + return QList{}; + const auto username = Common::StringFromFixedZeroTerminatedBuffer( + reinterpret_cast(profile.username.data()), profile.username.size()); + + return QList{new QStandardItem{ + GetIcon(user).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username + '\n' + user.FormatSwitch())}}; + }); + + list_items.erase( + std::remove_if(list_items.begin(), list_items.end(), + [](const auto& list) { return list == QList{}; }), + list_items.end()); + + for (const auto& item : list_items) + item_model->appendRow(item); +} + +void ConfigureSystem::UpdateCurrentUser() { + const auto& current_user = profile_manager->GetAllUsers()[Settings::values.current_user]; + const auto username = GetAccountUsername(current_user); + scene->clear(); - scene->addPixmap(icon.scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); - ui->current_user_username->setText(QString::fromStdString(current_user.first)); + scene->addPixmap( + GetIcon(current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); + ui->current_user_username->setText(QString::fromStdString(username)); } void ConfigureSystem::ReadSystemSettings() {} +std::string ConfigureSystem::GetAccountUsername(Service::Account::UUID uuid) { + Service::Account::ProfileBase profile; + if (!profile_manager->GetProfileBase(uuid, profile)) + return ""; + return Common::StringFromFixedZeroTerminatedBuffer( + reinterpret_cast(profile.username.data()), profile.username.size()); +} + void ConfigureSystem::applyConfiguration() { if (!enabled) return; @@ -192,16 +225,16 @@ void ConfigureSystem::refreshConsoleID() { void ConfigureSystem::SelectUser(const QModelIndex& index) { Settings::values.current_user = - std::clamp(index.row(), 0, Settings::values.users.size() - 1); + std::clamp(index.row(), 0, profile_manager->GetUserCount() - 1); UpdateCurrentUser(); - if (Settings::values.users.size() >= 2) - ui->pm_remove->setEnabled(true); - else - ui->pm_remove->setEnabled(false); + ui->pm_remove->setEnabled(profile_manager->GetUserCount() >= 2); + ui->pm_remove->setEnabled(false); ui->pm_rename->setEnabled(true); + + ui->pm_set_image->setEnabled(true); } void ConfigureSystem::AddUser() { @@ -212,33 +245,57 @@ void ConfigureSystem::AddUser() { const auto username = QInputDialog::getText(this, tr("Enter Username"), tr("Enter a username for the new user:"), QLineEdit::Normal, QString(), &ok); + if (!ok) + return; - Settings::values.users.emplace_back(username.toStdString(), uuid); + profile_manager->CreateNewUser(uuid, username.toStdString()); - setConfiguration(); + item_model->appendRow(new QStandardItem{ + GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username.toStdString() + '\n' + uuid.FormatSwitch())}); } void ConfigureSystem::RenameUser() { const auto user = tree_view->currentIndex().row(); + ASSERT(user < 8); + + const auto uuid = profile_manager->GetAllUsers()[user]; + const auto username = GetAccountUsername(uuid); + + Service::Account::ProfileBase profile; + if (!profile_manager->GetProfileBase(uuid, profile)) + return; bool ok = false; - const auto new_username = QInputDialog::getText( - this, tr("Enter Username"), tr("Enter a new username:"), QLineEdit::Normal, - QString::fromStdString(Settings::values.users[user].first), &ok); + const auto new_username = + QInputDialog::getText(this, tr("Enter Username"), tr("Enter a new username:"), + QLineEdit::Normal, QString::fromStdString(username), &ok); if (!ok) return; - Settings::values.users[user].first = new_username.toStdString(); + const auto username_std = new_username.toStdString(); + if (username_std.size() > profile.username.size()) + std::copy_n(username_std.begin(), profile.username.size(), profile.username.begin()); + else + std::copy(username_std.begin(), username_std.end(), profile.username.begin()); - setConfiguration(); + profile_manager->SetProfileBase(uuid, profile); + + list_items[user][0] = new QStandardItem{ + GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username_std + '\n' + uuid.FormatSwitch())}; } void ConfigureSystem::DeleteUser() { - const auto user = Settings::values.users.begin() + tree_view->currentIndex().row(); + const auto index = tree_view->currentIndex().row(); + ASSERT(index < 8); + const auto uuid = profile_manager->GetAllUsers()[index]; + const auto username = GetAccountUsername(uuid); + const auto confirm = QMessageBox::question( this, tr("Confirm Delete"), - tr("You are about to delete user with name %1. Are you sure?").arg(user->first.c_str())); + tr("You are about to delete user with name %1. Are you sure?").arg(username.c_str())); if (confirm == QMessageBox::No) return; @@ -246,10 +303,38 @@ void ConfigureSystem::DeleteUser() { if (Settings::values.current_user == tree_view->currentIndex().row()) Settings::values.current_user = 0; - Settings::values.users.erase(user); + if (!profile_manager->RemoveUser(uuid)) + return; - setConfiguration(); + item_model->removeRows(tree_view->currentIndex().row(), 1); ui->pm_remove->setEnabled(false); ui->pm_rename->setEnabled(false); } + +void ConfigureSystem::SetUserImage() { + const auto index = tree_view->currentIndex().row(); + ASSERT(index < 8); + const auto uuid = profile_manager->GetAllUsers()[index]; + const auto username = GetAccountUsername(uuid); + + const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(), + "JPEG Images (*.jpg *.jpeg)"); + + if (file.isEmpty()) + return; + + FileUtil::Delete(GetImagePath(uuid)); + + 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); + + FileUtil::CreateFullPath(GetImagePath(uuid)); + FileUtil::Copy(file.toStdString(), GetImagePath(uuid)); + + list_items[index][0] = new QStandardItem{ + GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username + '\n' + uuid.FormatSwitch())}; +} diff --git a/src/yuzu/configuration/configure_system.h b/src/yuzu/configuration/configure_system.h index aa20a3c306..868bb8bdf7 100644 --- a/src/yuzu/configuration/configure_system.h +++ b/src/yuzu/configuration/configure_system.h @@ -5,12 +5,16 @@ #pragma once #include -#include + #include -#include -#include -#include #include +#include "core/hle/service/acc/profile_manager.h" + +class QVBoxLayout; +class QTreeView; +class QStandardItemModel; +class QGraphicsScene; +class QStandardItem; namespace Ui { class ConfigureSystem; @@ -26,6 +30,7 @@ public: void applyConfiguration(); void setConfiguration(); + void PopulateUserList(); void UpdateCurrentUser(); public slots: @@ -36,9 +41,11 @@ public slots: void AddUser(); void RenameUser(); void DeleteUser(); + void SetUserImage(); private: void ReadSystemSettings(); + std::string GetAccountUsername(Service::Account::UUID uuid); QVBoxLayout* layout; QTreeView* tree_view; @@ -50,8 +57,9 @@ private: std::unique_ptr ui; bool enabled; - std::u16string username; int birthmonth, birthday; int language_index; int sound_index; + + std::unique_ptr profile_manager; }; diff --git a/src/yuzu/configuration/configure_system.ui b/src/yuzu/configuration/configure_system.ui index 2a6dcdb24f..020b32a370 100644 --- a/src/yuzu/configuration/configure_system.ui +++ b/src/yuzu/configuration/configure_system.ui @@ -333,6 +333,16 @@ + + + + false + + + Set Image + + + diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 1de3b817f8..9a3535e77e 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -10,6 +10,7 @@ // VFS includes must be before glad as they will conflict with Windows file api, which uses defines. #include "core/file_sys/vfs.h" #include "core/file_sys/vfs_real.h" +#include "core/hle/service/acc/profile_manager.h" // These are wrappers to avoid the calls to CreateDirectory and CreateFile becuase of the Windows // defines. @@ -758,10 +759,22 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target const std::string nand_dir = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir); ASSERT(program_id != 0); - QStringList list{}; - std::transform(Settings::values.users.begin(), Settings::values.users.end(), - std::back_inserter(list), - [](const auto& user) { return QString::fromStdString(user.first); }); + Service::Account::ProfileManager manager{}; + const auto user_ids = manager.GetAllUsers(); + QStringList list; + std::transform( + user_ids.begin(), user_ids.end(), std::back_inserter(list), + [&manager](const auto& user_id) -> QString { + if (user_id == Service::Account::UUID{}) + return ""; + Service::Account::ProfileBase base; + if (!manager.GetProfileBase(user_id, base)) + return ""; + + return QString::fromStdString(Common::StringFromFixedZeroTerminatedBuffer( + reinterpret_cast(base.username.data()), base.username.size())); + }); + list.removeAll(""); bool ok = false; const auto index_string = @@ -772,12 +785,12 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target return; const auto index = list.indexOf(index_string); - ASSERT(index != -1); + ASSERT(index != -1 && index < 8); - const auto user_id = Settings::values.users[index].second.uuid; + const auto user_id = manager.GetAllUsers()[index]; path = nand_dir + FileSys::SaveDataFactory::GetFullPath(FileSys::SaveDataSpaceId::NandUser, FileSys::SaveDataType::SaveData, - program_id, user_id, 0); + program_id, user_id.uuid, 0); if (!FileUtil::Exists(path)) { FileUtil::CreateFullPath(path); diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index 613894449e..f6083dcb3e 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -128,24 +128,8 @@ void Config::ReadValues() { Settings::values.enable_nfc = sdl2_config->GetBoolean("System", "enable_nfc", true); const auto size = sdl2_config->GetInteger("System", "users_size", 0); - Settings::values.users.clear(); - for (std::size_t i = 0; i < size; ++i) { - const auto uuid_low = std::stoull( - sdl2_config->Get("System", fmt::format("users_{}_uuid_low", i), "0"), nullptr, 0); - const auto uuid_high = std::stoull( - sdl2_config->Get("System", fmt::format("users_{}_uuid_high", i), "0"), nullptr, 0); - Settings::values.users.emplace_back( - sdl2_config->Get("System", fmt::format("users_{}_username", i), ""), - Service::Account::UUID{uuid_low, uuid_high}); - } - - if (Settings::values.users.empty()) { - Settings::values.users.emplace_back("yuzu", Service::Account::UUID{1, 0}); - LOG_WARNING( - Config, - "You are using the default UUID of {1, 0}! This might cause issues down the road! " - "Please consider randomizing a UUID and adding it to the sdl2_config.ini file."); - } + Settings::values.current_user = + std::clamp(sdl2_config->GetInteger("System", "current_user", 0), 0, 7); // Miscellaneous Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace"); From e408bbceed90da8965480e23d05fb764fcbfbb84 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Thu, 11 Oct 2018 09:16:32 -0400 Subject: [PATCH 08/11] configure_system: Clear selection after user delete --- src/core/hle/service/acc/profile_manager.cpp | 2 +- src/yuzu/configuration/configure_system.cpp | 28 ++++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index b0ea06b480..43743d39e5 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); diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index af2acdd453..87301b5a2f 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -13,6 +13,7 @@ #include #include "common/common_paths.h" #include "common/logging/backend.h" +#include "common/string_util.h" #include "core/core.h" #include "core/settings.h" #include "ui_configure_system.h" @@ -112,8 +113,6 @@ void ConfigureSystem::setConfiguration() { item_model->removeRows(0, item_model->rowCount()); list_items.clear(); - ui->pm_add->setEnabled(profile_manager->GetUserCount() < 8); - PopulateUserList(); UpdateCurrentUser(); } @@ -156,6 +155,8 @@ void ConfigureSystem::PopulateUserList() { } void ConfigureSystem::UpdateCurrentUser() { + ui->pm_add->setEnabled(profile_manager->GetUserCount() < 8); + const auto& current_user = profile_manager->GetAllUsers()[Settings::values.current_user]; const auto username = GetAccountUsername(current_user); @@ -230,10 +231,7 @@ void ConfigureSystem::SelectUser(const QModelIndex& index) { UpdateCurrentUser(); ui->pm_remove->setEnabled(profile_manager->GetUserCount() >= 2); - ui->pm_remove->setEnabled(false); - ui->pm_rename->setEnabled(true); - ui->pm_set_image->setEnabled(true); } @@ -282,9 +280,12 @@ void ConfigureSystem::RenameUser() { profile_manager->SetProfileBase(uuid, profile); - list_items[user][0] = new QStandardItem{ - GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username_std + '\n' + uuid.FormatSwitch())}; + item_model->setItem( + user, 0, + new QStandardItem{ + GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username_std + '\n' + uuid.FormatSwitch())}); + UpdateCurrentUser(); } void ConfigureSystem::DeleteUser() { @@ -302,11 +303,13 @@ void ConfigureSystem::DeleteUser() { if (Settings::values.current_user == tree_view->currentIndex().row()) Settings::values.current_user = 0; + UpdateCurrentUser(); if (!profile_manager->RemoveUser(uuid)) return; item_model->removeRows(tree_view->currentIndex().row(), 1); + tree_view->clearSelection(); ui->pm_remove->setEnabled(false); ui->pm_rename->setEnabled(false); @@ -334,7 +337,10 @@ void ConfigureSystem::SetUserImage() { FileUtil::CreateFullPath(GetImagePath(uuid)); FileUtil::Copy(file.toStdString(), GetImagePath(uuid)); - list_items[index][0] = new QStandardItem{ - GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username + '\n' + uuid.FormatSwitch())}; + item_model->setItem( + index, 0, + new QStandardItem{ + GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username + '\n' + uuid.FormatSwitch())}); + UpdateCurrentUser(); } From 45f2a2fe29373f261144c097d169dad8b65fe012 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 13 Oct 2018 13:02:33 -0400 Subject: [PATCH 09/11] 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 ++++--- src/yuzu/configuration/config.cpp | 3 +- src/yuzu/configuration/configure_system.cpp | 82 ++++++++++---------- src/yuzu/main.cpp | 26 +++---- src/yuzu_cmd/config.cpp | 5 +- 8 files changed, 103 insertions(+), 77 deletions(-) diff --git a/src/core/hle/service/acc/acc.cpp b/src/core/hle/service/acc/acc.cpp index cee309cb1f..cf065c2e01 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 43743d39e5..e6f1a0ae85 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 1e5c2460e1..482c1d8a95 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 9dfcec59bc..4ed66d8177 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"); diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index f7a9a8dd49..1fe9a7edd9 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -125,7 +125,8 @@ void Config::ReadValues() { Settings::values.use_docked_mode = qt_config->value("use_docked_mode", false).toBool(); Settings::values.enable_nfc = qt_config->value("enable_nfc", true).toBool(); - Settings::values.current_user = std::clamp(qt_config->value("current_user", 0).toInt(), 0, 7); + Settings::values.current_user = std::clamp(qt_config->value("current_user", 0).toInt(), 0, + Service::Account::MAX_USERS - 1); Settings::values.language_index = qt_config->value("language_index", 1).toInt(); qt_config->endGroup(); diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index 87301b5a2f..02e061ebc3 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -131,38 +131,33 @@ static QPixmap GetIcon(Service::Account::UUID uuid) { void ConfigureSystem::PopulateUserList() { const auto& profiles = profile_manager->GetAllUsers(); - std::transform( - profiles.begin(), profiles.end(), std::back_inserter(list_items), - [this](const Service::Account::UUID& user) { - Service::Account::ProfileBase profile; - if (!profile_manager->GetProfileBase(user, profile)) - return QList{}; - const auto username = Common::StringFromFixedZeroTerminatedBuffer( - reinterpret_cast(profile.username.data()), profile.username.size()); + for (const auto& user : profiles) { + Service::Account::ProfileBase profile; + if (!profile_manager->GetProfileBase(user, profile)) + continue; - return QList{new QStandardItem{ - GetIcon(user).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username + '\n' + user.FormatSwitch())}}; - }); + const auto username = Common::StringFromFixedZeroTerminatedBuffer( + reinterpret_cast(profile.username.data()), profile.username.size()); - list_items.erase( - std::remove_if(list_items.begin(), list_items.end(), - [](const auto& list) { return list == QList{}; }), - list_items.end()); + list_items.push_back(QList{new QStandardItem{ + GetIcon(user).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username + '\n' + user.FormatSwitch())}}); + } for (const auto& item : list_items) item_model->appendRow(item); } void ConfigureSystem::UpdateCurrentUser() { - ui->pm_add->setEnabled(profile_manager->GetUserCount() < 8); + ui->pm_add->setEnabled(profile_manager->GetUserCount() < Service::Account::MAX_USERS); - const auto& current_user = profile_manager->GetAllUsers()[Settings::values.current_user]; - const auto username = GetAccountUsername(current_user); + const auto& current_user = profile_manager->GetUser(Settings::values.current_user); + ASSERT(current_user != boost::none); + const auto username = GetAccountUsername(*current_user); scene->clear(); scene->addPixmap( - GetIcon(current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); + GetIcon(*current_user).scaled(48, 48, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); ui->current_user_username->setText(QString::fromStdString(username)); } @@ -255,13 +250,12 @@ void ConfigureSystem::AddUser() { void ConfigureSystem::RenameUser() { const auto user = tree_view->currentIndex().row(); - ASSERT(user < 8); - - const auto uuid = profile_manager->GetAllUsers()[user]; - const auto username = GetAccountUsername(uuid); + const auto uuid = profile_manager->GetUser(user); + ASSERT(uuid != boost::none); + const auto username = GetAccountUsername(*uuid); Service::Account::ProfileBase profile; - if (!profile_manager->GetProfileBase(uuid, profile)) + if (!profile_manager->GetProfileBase(*uuid, profile)) return; bool ok = false; @@ -273,26 +267,28 @@ void ConfigureSystem::RenameUser() { return; const auto username_std = new_username.toStdString(); - if (username_std.size() > profile.username.size()) - std::copy_n(username_std.begin(), profile.username.size(), profile.username.begin()); - else + if (username_std.size() > profile.username.size()) { + std::copy_n(username_std.begin(), std::min(profile.username.size(), username_std.size()), + profile.username.begin()); + } else { std::copy(username_std.begin(), username_std.end(), profile.username.begin()); + } - profile_manager->SetProfileBase(uuid, profile); + profile_manager->SetProfileBase(*uuid, profile); item_model->setItem( user, 0, new QStandardItem{ - GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username_std + '\n' + uuid.FormatSwitch())}); + GetIcon(*uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username_std + '\n' + uuid->FormatSwitch())}); UpdateCurrentUser(); } void ConfigureSystem::DeleteUser() { const auto index = tree_view->currentIndex().row(); - ASSERT(index < 8); - const auto uuid = profile_manager->GetAllUsers()[index]; - const auto username = GetAccountUsername(uuid); + const auto uuid = profile_manager->GetUser(index); + ASSERT(uuid != boost::none); + const auto username = GetAccountUsername(*uuid); const auto confirm = QMessageBox::question( this, tr("Confirm Delete"), @@ -305,7 +301,7 @@ void ConfigureSystem::DeleteUser() { Settings::values.current_user = 0; UpdateCurrentUser(); - if (!profile_manager->RemoveUser(uuid)) + if (!profile_manager->RemoveUser(*uuid)) return; item_model->removeRows(tree_view->currentIndex().row(), 1); @@ -317,9 +313,9 @@ void ConfigureSystem::DeleteUser() { void ConfigureSystem::SetUserImage() { const auto index = tree_view->currentIndex().row(); - ASSERT(index < 8); - const auto uuid = profile_manager->GetAllUsers()[index]; - const auto username = GetAccountUsername(uuid); + const auto uuid = profile_manager->GetUser(index); + ASSERT(uuid != boost::none); + const auto username = GetAccountUsername(*uuid); const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(), "JPEG Images (*.jpg *.jpeg)"); @@ -327,20 +323,20 @@ void ConfigureSystem::SetUserImage() { if (file.isEmpty()) return; - FileUtil::Delete(GetImagePath(uuid)); + FileUtil::Delete(GetImagePath(*uuid)); 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); - FileUtil::CreateFullPath(GetImagePath(uuid)); - FileUtil::Copy(file.toStdString(), GetImagePath(uuid)); + FileUtil::CreateFullPath(GetImagePath(*uuid)); + FileUtil::Copy(file.toStdString(), GetImagePath(*uuid)); item_model->setItem( index, 0, new QStandardItem{ - GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username + '\n' + uuid.FormatSwitch())}); + GetIcon(*uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), + QString::fromStdString(username + '\n' + uuid->FormatSwitch())}); UpdateCurrentUser(); } diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 9a3535e77e..47f4948411 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -762,19 +762,16 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target Service::Account::ProfileManager manager{}; const auto user_ids = manager.GetAllUsers(); QStringList list; - std::transform( - user_ids.begin(), user_ids.end(), std::back_inserter(list), - [&manager](const auto& user_id) -> QString { - if (user_id == Service::Account::UUID{}) - return ""; - Service::Account::ProfileBase base; - if (!manager.GetProfileBase(user_id, base)) - return ""; + for (const auto& user_id : user_ids) { + if (user_id == Service::Account::UUID{}) + continue; + Service::Account::ProfileBase base; + if (!manager.GetProfileBase(user_id, base)) + continue; - return QString::fromStdString(Common::StringFromFixedZeroTerminatedBuffer( - reinterpret_cast(base.username.data()), base.username.size())); - }); - list.removeAll(""); + list.push_back(QString::fromStdString(Common::StringFromFixedZeroTerminatedBuffer( + reinterpret_cast(base.username.data()), base.username.size()))); + } bool ok = false; const auto index_string = @@ -787,10 +784,11 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target const auto index = list.indexOf(index_string); ASSERT(index != -1 && index < 8); - const auto user_id = manager.GetAllUsers()[index]; + const auto user_id = manager.GetUser(index); + ASSERT(user_id != boost::none); path = nand_dir + FileSys::SaveDataFactory::GetFullPath(FileSys::SaveDataSpaceId::NandUser, FileSys::SaveDataType::SaveData, - program_id, user_id.uuid, 0); + program_id, user_id->uuid, 0); if (!FileUtil::Exists(path)) { FileUtil::CreateFullPath(path); diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index f6083dcb3e..b456266a6a 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -8,6 +8,7 @@ #include "common/file_util.h" #include "common/logging/log.h" #include "common/param_package.h" +#include "core/hle/service/acc/profile_manager.h" #include "core/settings.h" #include "input_common/main.h" #include "yuzu_cmd/config.h" @@ -128,8 +129,8 @@ void Config::ReadValues() { Settings::values.enable_nfc = sdl2_config->GetBoolean("System", "enable_nfc", true); const auto size = sdl2_config->GetInteger("System", "users_size", 0); - Settings::values.current_user = - std::clamp(sdl2_config->GetInteger("System", "current_user", 0), 0, 7); + Settings::values.current_user = std::clamp( + sdl2_config->GetInteger("System", "current_user", 0), 0, Service::Account::MAX_USERS - 1); // Miscellaneous Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace"); From bfad41b0c12a308b0a5a10e3162d74140e3c121a Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 14 Oct 2018 14:49:32 -0400 Subject: [PATCH 10/11] profile_manager: Create save data if it doesn't exist on use --- src/core/hle/service/acc/profile_manager.cpp | 50 +++++++++++++++----- src/core/hle/service/acc/profile_manager.h | 2 +- src/yuzu/configuration/configure_system.cpp | 2 +- src/yuzu/configuration/configure_system.h | 8 ++-- 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index e6f1a0ae85..06f7d1b156 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"); - ProfileDataRaw data; - save.Seek(0, SEEK_SET); - if (save.ReadBytes(&data, sizeof(ProfileDataRaw)) != sizeof(ProfileDataRaw)) + 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; + 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 482c1d8a95..235208d565 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() { diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index 02e061ebc3..a88fabc361 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -163,7 +163,7 @@ void ConfigureSystem::UpdateCurrentUser() { void ConfigureSystem::ReadSystemSettings() {} -std::string ConfigureSystem::GetAccountUsername(Service::Account::UUID uuid) { +std::string ConfigureSystem::GetAccountUsername(Service::Account::UUID uuid) const { Service::Account::ProfileBase profile; if (!profile_manager->GetProfileBase(uuid, profile)) return ""; diff --git a/src/yuzu/configuration/configure_system.h b/src/yuzu/configuration/configure_system.h index 868bb8bdf7..6adadfccf9 100644 --- a/src/yuzu/configuration/configure_system.h +++ b/src/yuzu/configuration/configure_system.h @@ -10,11 +10,11 @@ #include #include "core/hle/service/acc/profile_manager.h" -class QVBoxLayout; -class QTreeView; -class QStandardItemModel; class QGraphicsScene; class QStandardItem; +class QStandardItemModel; +class QTreeView; +class QVBoxLayout; namespace Ui { class ConfigureSystem; @@ -45,7 +45,7 @@ public slots: private: void ReadSystemSettings(); - std::string GetAccountUsername(Service::Account::UUID uuid); + std::string GetAccountUsername(Service::Account::UUID uuid) const; QVBoxLayout* layout; QTreeView* tree_view; From e7ac42677be6c13e5286fb42004aa94b0da45391 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Wed, 24 Oct 2018 09:25:13 -0400 Subject: [PATCH 11/11] configure_system: Clear current username before overwriting Prevents bug where old username would remain if the new username was shorter in length. --- src/yuzu/configuration/configure_system.cpp | 14 ++++++++++---- src/yuzu/configuration/configure_system.h | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/yuzu/configuration/configure_system.cpp b/src/yuzu/configuration/configure_system.cpp index a88fabc361..83cc49dfc0 100644 --- a/src/yuzu/configuration/configure_system.cpp +++ b/src/yuzu/configuration/configure_system.cpp @@ -15,6 +15,7 @@ #include "common/logging/backend.h" #include "common/string_util.h" #include "core/core.h" +#include "core/hle/service/acc/profile_manager.h" #include "core/settings.h" #include "ui_configure_system.h" #include "yuzu/configuration/configure_system.h" @@ -266,6 +267,7 @@ void ConfigureSystem::RenameUser() { if (!ok) return; + std::fill(profile.username.begin(), profile.username.end(), '\0'); const auto username_std = new_username.toStdString(); if (username_std.size() > profile.username.size()) { std::copy_n(username_std.begin(), std::min(profile.username.size(), username_std.size()), @@ -280,7 +282,10 @@ void ConfigureSystem::RenameUser() { user, 0, new QStandardItem{ GetIcon(*uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), - QString::fromStdString(username_std + '\n' + uuid->FormatSwitch())}); + tr("%1\n%2", "%1 is the profile username, %2 is the formatted UUID (e.g. " + "00112233-4455-6677-8899-AABBCCDDEEFF))") + .arg(QString::fromStdString(username_std), + QString::fromStdString(uuid->FormatSwitch()))}); UpdateCurrentUser(); } @@ -290,9 +295,10 @@ void ConfigureSystem::DeleteUser() { ASSERT(uuid != boost::none); const auto username = GetAccountUsername(*uuid); - const auto confirm = QMessageBox::question( - this, tr("Confirm Delete"), - tr("You are about to delete user with name %1. Are you sure?").arg(username.c_str())); + const auto confirm = + QMessageBox::question(this, tr("Confirm Delete"), + tr("You are about to delete user with name %1. Are you sure?") + .arg(QString::fromStdString(username))); if (confirm == QMessageBox::No) return; diff --git a/src/yuzu/configuration/configure_system.h b/src/yuzu/configuration/configure_system.h index 6adadfccf9..b73e0719cb 100644 --- a/src/yuzu/configuration/configure_system.h +++ b/src/yuzu/configuration/configure_system.h @@ -8,7 +8,11 @@ #include #include -#include "core/hle/service/acc/profile_manager.h" + +namespace Service::Account { +class ProfileManager; +struct UUID; +} // namespace Service::Account class QGraphicsScene; class QStandardItem;