acc: Fix account UUID duplication error

This commit is contained in:
Zach Hilman 2018-10-13 13:02:33 -04:00
parent e408bbceed
commit 45f2a2fe29
8 changed files with 103 additions and 77 deletions

View File

@ -106,6 +106,8 @@ private:
const FileUtil::IOFile image(GetImagePath(user_id), "rb"); 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...");
ctx.WriteBuffer(backup_jpeg); ctx.WriteBuffer(backup_jpeg);
rb.Push<u32>(backup_jpeg_size); rb.Push<u32>(backup_jpeg_size);
} else { } else {
@ -126,11 +128,14 @@ private:
const FileUtil::IOFile image(GetImagePath(user_id), "rb"); 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<u32>(backup_jpeg_size); rb.Push<u32>(backup_jpeg_size);
else } else {
rb.Push<u32>(std::min<u32>(image.GetSize(), MAX_JPEG_IMAGE_SIZE)); rb.Push<u32>(std::min<u32>(image.GetSize(), MAX_JPEG_IMAGE_SIZE));
} }
}
const ProfileManager& profile_manager; const ProfileManager& profile_manager;
UUID user_id; ///< The user id this profile refers to. UUID user_id; ///< The user id this profile refers to.

View File

@ -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_USER_ALREADY_EXISTS(ErrorModule::Account, -2);
constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20); 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() { const UUID& UUID::Generate() {
std::random_device device; std::random_device device;
std::mt19937 gen(device()); std::mt19937 gen(device());
@ -45,11 +47,11 @@ ProfileManager::ProfileManager() {
if (user_count == 0) if (user_count == 0)
CreateNewUser(UUID{}.Generate(), "yuzu"); CreateNewUser(UUID{}.Generate(), "yuzu");
auto current = Settings::values.current_user; auto current = std::clamp<int>(Settings::values.current_user, 0, MAX_USERS - 1);
if (!GetAllUsers()[current]) if (UserExistsIndex(current))
current = 0; current = 0;
OpenUser(GetAllUsers()[current]); OpenUser(*GetUser(current));
} }
ProfileManager::~ProfileManager() { ProfileManager::~ProfileManager() {
@ -126,6 +128,12 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username)
return CreateNewUser(uuid, username_output); return CreateNewUser(uuid, username_output);
} }
boost::optional<UUID> 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. /// Returns a users profile index based on their user id.
boost::optional<std::size_t> ProfileManager::GetUserIndex(const UUID& uuid) const { boost::optional<std::size_t> ProfileManager::GetUserIndex(const UUID& uuid) const {
if (!uuid) { if (!uuid) {
@ -189,6 +197,12 @@ bool ProfileManager::UserExists(UUID uuid) const {
return (GetUserIndex(uuid) != boost::none); 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 /// Opens a specific user
void ProfileManager::OpenUser(UUID uuid) { void ProfileManager::OpenUser(UUID uuid) {
auto idx = GetUserIndex(uuid); auto idx = GetUserIndex(uuid);
@ -292,7 +306,7 @@ bool ProfileManager::SetProfileBase(UUID uuid, const ProfileBase& profile_new) {
void ProfileManager::ParseUserSaveFile() { void ProfileManager::ParseUserSaveFile() {
FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
"/system/save/8000000000000010/su/avators/profiles.dat", ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat",
"rb"); "rb");
ProfileDataRaw data; ProfileDataRaw data;
@ -322,7 +336,7 @@ void ProfileManager::WriteUserSaveFile() {
} }
FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + FileUtil::IOFile save(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
"/system/save/8000000000000010/su/avators/profiles.dat", ACC_SAVE_AVATORS_BASE_PATH + "profiles.dat",
"wb"); "wb");
save.Resize(sizeof(ProfileDataRaw)); save.Resize(sizeof(ProfileDataRaw));

View File

@ -96,6 +96,7 @@ public:
ResultCode AddUser(const ProfileInfo& user); ResultCode AddUser(const ProfileInfo& user);
ResultCode CreateNewUser(UUID uuid, const ProfileUsername& username); ResultCode CreateNewUser(UUID uuid, const ProfileUsername& username);
ResultCode CreateNewUser(UUID uuid, const std::string& username); ResultCode CreateNewUser(UUID uuid, const std::string& username);
boost::optional<UUID> GetUser(std::size_t index) const;
boost::optional<std::size_t> GetUserIndex(const UUID& uuid) const; boost::optional<std::size_t> GetUserIndex(const UUID& uuid) const;
boost::optional<std::size_t> GetUserIndex(const ProfileInfo& user) const; boost::optional<std::size_t> GetUserIndex(const ProfileInfo& user) const;
bool GetProfileBase(boost::optional<std::size_t> index, ProfileBase& profile) const; bool GetProfileBase(boost::optional<std::size_t> index, ProfileBase& profile) const;
@ -109,6 +110,7 @@ public:
std::size_t GetUserCount() const; std::size_t GetUserCount() const;
std::size_t GetOpenUserCount() const; std::size_t GetOpenUserCount() const;
bool UserExists(UUID uuid) const; bool UserExists(UUID uuid) const;
bool UserExistsIndex(std::size_t index) const;
void OpenUser(UUID uuid); void OpenUser(UUID uuid);
void CloseUser(UUID uuid); void CloseUser(UUID uuid);
UserIDArray GetOpenUsers() const; UserIDArray GetOpenUsers() const;

View File

@ -28,7 +28,15 @@
namespace Service::AM { 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") { IWindowController::IWindowController() : ServiceFramework("IWindowController") {
// clang-format off // clang-format off
@ -728,22 +736,23 @@ void IApplicationFunctions::EndBlockingHomeButton(Kernel::HLERequestContext& ctx
} }
void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) { void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) {
constexpr std::array<u8, 0x8> header_data{ LaunchParameters params{};
0xca, 0x97, 0x94, 0xc7, // Magic
1, 0, 0, 0, // IsAccountSelected (bool)
};
std::vector<u8> buffer(POP_LAUNCH_PARAMETER_BUFFER_SIZE); params.magic = POP_LAUNCH_PARAMETER_MAGIC;
params.is_account_selected = 1;
std::memcpy(buffer.data(), header_data.data(), header_data.size());
Account::ProfileManager profile_manager{}; Account::ProfileManager profile_manager{};
const auto uuid = profile_manager.GetAllUsers()[Settings::values.current_user].uuid; const auto uuid = profile_manager.GetUser(Settings::values.current_user);
std::memcpy(buffer.data() + header_data.size(), uuid.data(), sizeof(u128)); ASSERT(uuid != boost::none);
params.current_user = uuid->uuid;
IPC::ResponseBuilder rb{ctx, 2, 0, 1}; IPC::ResponseBuilder rb{ctx, 2, 0, 1};
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
std::vector<u8> buffer(sizeof(LaunchParameters));
std::memcpy(buffer.data(), &params, buffer.size());
rb.PushIpcInterface<AM::IStorage>(buffer); rb.PushIpcInterface<AM::IStorage>(buffer);
LOG_DEBUG(Service_AM, "called"); LOG_DEBUG(Service_AM, "called");

View File

@ -125,7 +125,8 @@ void Config::ReadValues() {
Settings::values.use_docked_mode = qt_config->value("use_docked_mode", false).toBool(); 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.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<int>(qt_config->value("current_user", 0).toInt(), 0,
Service::Account::MAX_USERS - 1);
Settings::values.language_index = qt_config->value("language_index", 1).toInt(); Settings::values.language_index = qt_config->value("language_index", 1).toInt();
qt_config->endGroup(); qt_config->endGroup();

View File

@ -131,38 +131,33 @@ static QPixmap GetIcon(Service::Account::UUID uuid) {
void ConfigureSystem::PopulateUserList() { void ConfigureSystem::PopulateUserList() {
const auto& profiles = profile_manager->GetAllUsers(); const auto& profiles = profile_manager->GetAllUsers();
std::transform( for (const auto& user : profiles) {
profiles.begin(), profiles.end(), std::back_inserter(list_items),
[this](const Service::Account::UUID& user) {
Service::Account::ProfileBase profile; Service::Account::ProfileBase profile;
if (!profile_manager->GetProfileBase(user, profile)) if (!profile_manager->GetProfileBase(user, profile))
return QList<QStandardItem*>{}; continue;
const auto username = Common::StringFromFixedZeroTerminatedBuffer( const auto username = Common::StringFromFixedZeroTerminatedBuffer(
reinterpret_cast<const char*>(profile.username.data()), profile.username.size()); reinterpret_cast<const char*>(profile.username.data()), profile.username.size());
return QList<QStandardItem*>{new QStandardItem{ list_items.push_back(QList<QStandardItem*>{new QStandardItem{
GetIcon(user).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), GetIcon(user).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
QString::fromStdString(username + '\n' + user.FormatSwitch())}}; QString::fromStdString(username + '\n' + user.FormatSwitch())}});
}); }
list_items.erase(
std::remove_if(list_items.begin(), list_items.end(),
[](const auto& list) { return list == QList<QStandardItem*>{}; }),
list_items.end());
for (const auto& item : list_items) for (const auto& item : list_items)
item_model->appendRow(item); item_model->appendRow(item);
} }
void ConfigureSystem::UpdateCurrentUser() { 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& current_user = profile_manager->GetUser(Settings::values.current_user);
const auto username = GetAccountUsername(current_user); ASSERT(current_user != boost::none);
const auto username = GetAccountUsername(*current_user);
scene->clear(); scene->clear();
scene->addPixmap( 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)); ui->current_user_username->setText(QString::fromStdString(username));
} }
@ -255,13 +250,12 @@ void ConfigureSystem::AddUser() {
void ConfigureSystem::RenameUser() { void ConfigureSystem::RenameUser() {
const auto user = tree_view->currentIndex().row(); const auto user = tree_view->currentIndex().row();
ASSERT(user < 8); const auto uuid = profile_manager->GetUser(user);
ASSERT(uuid != boost::none);
const auto uuid = profile_manager->GetAllUsers()[user]; const auto username = GetAccountUsername(*uuid);
const auto username = GetAccountUsername(uuid);
Service::Account::ProfileBase profile; Service::Account::ProfileBase profile;
if (!profile_manager->GetProfileBase(uuid, profile)) if (!profile_manager->GetProfileBase(*uuid, profile))
return; return;
bool ok = false; bool ok = false;
@ -273,26 +267,28 @@ void ConfigureSystem::RenameUser() {
return; return;
const auto username_std = new_username.toStdString(); const auto username_std = new_username.toStdString();
if (username_std.size() > profile.username.size()) if (username_std.size() > profile.username.size()) {
std::copy_n(username_std.begin(), profile.username.size(), profile.username.begin()); std::copy_n(username_std.begin(), std::min(profile.username.size(), username_std.size()),
else profile.username.begin());
} else {
std::copy(username_std.begin(), username_std.end(), profile.username.begin()); std::copy(username_std.begin(), username_std.end(), profile.username.begin());
}
profile_manager->SetProfileBase(uuid, profile); profile_manager->SetProfileBase(*uuid, profile);
item_model->setItem( item_model->setItem(
user, 0, user, 0,
new QStandardItem{ new QStandardItem{
GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), GetIcon(*uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
QString::fromStdString(username_std + '\n' + uuid.FormatSwitch())}); QString::fromStdString(username_std + '\n' + uuid->FormatSwitch())});
UpdateCurrentUser(); UpdateCurrentUser();
} }
void ConfigureSystem::DeleteUser() { void ConfigureSystem::DeleteUser() {
const auto index = tree_view->currentIndex().row(); const auto index = tree_view->currentIndex().row();
ASSERT(index < 8); const auto uuid = profile_manager->GetUser(index);
const auto uuid = profile_manager->GetAllUsers()[index]; ASSERT(uuid != boost::none);
const auto username = GetAccountUsername(uuid); const auto username = GetAccountUsername(*uuid);
const auto confirm = QMessageBox::question( const auto confirm = QMessageBox::question(
this, tr("Confirm Delete"), this, tr("Confirm Delete"),
@ -305,7 +301,7 @@ void ConfigureSystem::DeleteUser() {
Settings::values.current_user = 0; Settings::values.current_user = 0;
UpdateCurrentUser(); UpdateCurrentUser();
if (!profile_manager->RemoveUser(uuid)) if (!profile_manager->RemoveUser(*uuid))
return; return;
item_model->removeRows(tree_view->currentIndex().row(), 1); item_model->removeRows(tree_view->currentIndex().row(), 1);
@ -317,9 +313,9 @@ void ConfigureSystem::DeleteUser() {
void ConfigureSystem::SetUserImage() { void ConfigureSystem::SetUserImage() {
const auto index = tree_view->currentIndex().row(); const auto index = tree_view->currentIndex().row();
ASSERT(index < 8); const auto uuid = profile_manager->GetUser(index);
const auto uuid = profile_manager->GetAllUsers()[index]; ASSERT(uuid != boost::none);
const auto username = GetAccountUsername(uuid); const auto username = GetAccountUsername(*uuid);
const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(), const auto file = QFileDialog::getOpenFileName(this, tr("Select User Image"), QString(),
"JPEG Images (*.jpg *.jpeg)"); "JPEG Images (*.jpg *.jpeg)");
@ -327,20 +323,20 @@ void ConfigureSystem::SetUserImage() {
if (file.isEmpty()) if (file.isEmpty())
return; return;
FileUtil::Delete(GetImagePath(uuid)); FileUtil::Delete(GetImagePath(*uuid));
const auto raw_path = const auto raw_path =
FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010"; FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "/system/save/8000000000000010";
if (FileUtil::Exists(raw_path) && !FileUtil::IsDirectory(raw_path)) if (FileUtil::Exists(raw_path) && !FileUtil::IsDirectory(raw_path))
FileUtil::Delete(raw_path); FileUtil::Delete(raw_path);
FileUtil::CreateFullPath(GetImagePath(uuid)); FileUtil::CreateFullPath(GetImagePath(*uuid));
FileUtil::Copy(file.toStdString(), GetImagePath(uuid)); FileUtil::Copy(file.toStdString(), GetImagePath(*uuid));
item_model->setItem( item_model->setItem(
index, 0, index, 0,
new QStandardItem{ new QStandardItem{
GetIcon(uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation), GetIcon(*uuid).scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation),
QString::fromStdString(username + '\n' + uuid.FormatSwitch())}); QString::fromStdString(username + '\n' + uuid->FormatSwitch())});
UpdateCurrentUser(); UpdateCurrentUser();
} }

View File

@ -762,19 +762,16 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target
Service::Account::ProfileManager manager{}; Service::Account::ProfileManager manager{};
const auto user_ids = manager.GetAllUsers(); const auto user_ids = manager.GetAllUsers();
QStringList list; QStringList list;
std::transform( for (const auto& user_id : user_ids) {
user_ids.begin(), user_ids.end(), std::back_inserter(list),
[&manager](const auto& user_id) -> QString {
if (user_id == Service::Account::UUID{}) if (user_id == Service::Account::UUID{})
return ""; continue;
Service::Account::ProfileBase base; Service::Account::ProfileBase base;
if (!manager.GetProfileBase(user_id, base)) if (!manager.GetProfileBase(user_id, base))
return ""; continue;
return QString::fromStdString(Common::StringFromFixedZeroTerminatedBuffer( list.push_back(QString::fromStdString(Common::StringFromFixedZeroTerminatedBuffer(
reinterpret_cast<const char*>(base.username.data()), base.username.size())); reinterpret_cast<const char*>(base.username.data()), base.username.size())));
}); }
list.removeAll("");
bool ok = false; bool ok = false;
const auto index_string = const auto index_string =
@ -787,10 +784,11 @@ void GMainWindow::OnGameListOpenFolder(u64 program_id, GameListOpenTarget target
const auto index = list.indexOf(index_string); const auto index = list.indexOf(index_string);
ASSERT(index != -1 && index < 8); 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, path = nand_dir + FileSys::SaveDataFactory::GetFullPath(FileSys::SaveDataSpaceId::NandUser,
FileSys::SaveDataType::SaveData, FileSys::SaveDataType::SaveData,
program_id, user_id.uuid, 0); program_id, user_id->uuid, 0);
if (!FileUtil::Exists(path)) { if (!FileUtil::Exists(path)) {
FileUtil::CreateFullPath(path); FileUtil::CreateFullPath(path);

View File

@ -8,6 +8,7 @@
#include "common/file_util.h" #include "common/file_util.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/param_package.h" #include "common/param_package.h"
#include "core/hle/service/acc/profile_manager.h"
#include "core/settings.h" #include "core/settings.h"
#include "input_common/main.h" #include "input_common/main.h"
#include "yuzu_cmd/config.h" #include "yuzu_cmd/config.h"
@ -128,8 +129,8 @@ void Config::ReadValues() {
Settings::values.enable_nfc = sdl2_config->GetBoolean("System", "enable_nfc", true); Settings::values.enable_nfc = sdl2_config->GetBoolean("System", "enable_nfc", true);
const auto size = sdl2_config->GetInteger("System", "users_size", 0); const auto size = sdl2_config->GetInteger("System", "users_size", 0);
Settings::values.current_user = Settings::values.current_user = std::clamp<int>(
std::clamp<int>(sdl2_config->GetInteger("System", "current_user", 0), 0, 7); sdl2_config->GetInteger("System", "current_user", 0), 0, Service::Account::MAX_USERS - 1);
// Miscellaneous // Miscellaneous
Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace"); Settings::values.log_filter = sdl2_config->Get("Miscellaneous", "log_filter", "*:Trace");