From 6f691e71bfa30de8789327a969cb7c2fdd1669f2 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Wed, 8 Aug 2018 22:26:42 +1000 Subject: began initial implementation of "ProfileManager" --- src/core/hle/service/acc/profile_manager.cpp | 89 ++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/core/hle/service/acc/profile_manager.cpp (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp new file mode 100644 index 000000000..8819c5703 --- /dev/null +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -0,0 +1,89 @@ +#include "profile_manager.h" + +namespace Service::Account { +// TODO(ogniK): Get actual error codes +constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1); +constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20); + +size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { + if (user_count >= MAX_USERS) { + return -1; + } + profiles[user_count] = std::move(user); + return user_count++; +} + +bool ProfileManager::RemoveProfileAtIdx(size_t index) { + if (index >= MAX_USERS || index < 0 || index >= user_count) + return false; + profiles[index] = ProfileInfo{}; + if (index < user_count - 1) + for (size_t i = index; i < user_count - 1; i++) + profiles[i] = profiles[i + 1]; // Shift upper profiles down + user_count--; + return true; +} + +ResultCode ProfileManager::AddUser(ProfileInfo user) { + if (AddToProfiles(user) == -1) { + return ERROR_TOO_MANY_USERS; + } + return RESULT_SUCCESS; +} + +ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array username) { + if (user_count == MAX_USERS) + return ERROR_TOO_MANY_USERS; + if (!uuid) + return ERROR_ARGUMENT_IS_NULL; + if (username[0] == 0x0) + return ERROR_ARGUMENT_IS_NULL; + ProfileInfo prof_inf; + prof_inf.user_uuid = uuid; + prof_inf.username = username; + prof_inf.data = std::array(); + prof_inf.creation_time = 0x0; + return AddUser(prof_inf); +} + +size_t ProfileManager::GetUserIndex(UUID uuid) { + for (unsigned i = 0; i < user_count; i++) + if (profiles[i].user_uuid == uuid) + return i; + return -1; +} + +size_t ProfileManager::GetUserIndex(ProfileInfo user) { + return GetUserIndex(user.user_uuid); +} + +bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) { + if (index >= MAX_USERS) { + profile.Invalidate(); + return false; + } + auto prof_info = profiles[index]; + profile.user_uuid = prof_info.user_uuid; + profile.username = prof_info.username; + profile.timestamp = prof_info.creation_time; + return true; +} + +bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) { + auto idx = GetUserIndex(uuid); + return GetProfileBase(idx, profile); +} + +bool ProfileManager::GetProfileBase(ProfileInfo user, ProfileBase& profile) { + return GetProfileBase(user.user_uuid, profile); +} + +size_t ProfileManager::GetUserCount() { + return user_count; +} + +bool ProfileManager::UserExists(UUID uuid) { + return (GetUserIndex(uuid) != -1); +} + +}; // namespace Service::Account -- cgit v1.2.3 From 03d7faf58390216729eb828b08be7e5f0ef82727 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Wed, 8 Aug 2018 23:41:12 +1000 Subject: GetProfileBase and GetProfileBaseAndData added --- src/core/hle/service/acc/profile_manager.cpp | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 8819c5703..925022018 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -43,10 +43,13 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array usernam prof_inf.username = username; prof_inf.data = std::array(); prof_inf.creation_time = 0x0; + prof_inf.is_open = false; return AddUser(prof_inf); } size_t ProfileManager::GetUserIndex(UUID uuid) { + if (!uuid) + return -1; for (unsigned i = 0; i < user_count; i++) if (profiles[i].user_uuid == uuid) return i; @@ -86,4 +89,61 @@ bool ProfileManager::UserExists(UUID uuid) { return (GetUserIndex(uuid) != -1); } +void ProfileManager::OpenUser(UUID uuid) { + auto idx = GetUserIndex(uuid); + if (idx == -1) + return; + profiles[idx].is_open = true; + last_openned_user = uuid; +} + +void ProfileManager::CloseUser(UUID uuid) { + auto idx = GetUserIndex(uuid); + if (idx == -1) + return; + profiles[idx].is_open = false; +} + +std::array ProfileManager::GetAllUsers() { + std::array output; + for (unsigned i = 0; i < user_count; i++) { + output[i] = profiles[i].user_uuid; + } + return output; +} + +std::array ProfileManager::GetOpenUsers() { + std::array output; + unsigned user_idx = 0; + for (unsigned i = 0; i < user_count; i++) { + if (profiles[i].is_open) { + output[i++] = profiles[i].user_uuid; + } + } + return output; +} + +const UUID& ProfileManager::GetLastOpennedUser() { + return last_openned_user; +} + +bool ProfileManager::GetProfileBaseAndData(size_t index, ProfileBase& profile, + std::array& data) { + if (GetProfileBase(index, profile)) { + std::memcpy(data.data(), profiles[index].data.data(), MAX_DATA); + return true; + } + return false; +} +bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile, + std::array& data) { + auto idx = GetUserIndex(uuid); + return GetProfileBaseAndData(idx, profile, data); +} + +bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile, + std::array& data) { + return GetProfileBaseAndData(user.user_uuid, profile, data); +} + }; // namespace Service::Account -- cgit v1.2.3 From 75169c75708d9f54297537b387182bee10ada9ab Mon Sep 17 00:00:00 2001 From: David Marcec Date: Thu, 9 Aug 2018 01:09:12 +1000 Subject: Inital pass of account backend implementation This commit verified working on puyo --- src/core/hle/service/acc/profile_manager.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 925022018..1633d5d48 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -1,3 +1,4 @@ +#include "core/settings.h" #include "profile_manager.h" namespace Service::Account { @@ -5,6 +6,10 @@ namespace Service::Account { constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1); constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20); +ProfileManager::ProfileManager() { + CreateNewUser(UUID{1, 0}, Settings::values.username); +} + size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { if (user_count >= MAX_USERS) { return -1; @@ -39,14 +44,23 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array usernam if (username[0] == 0x0) return ERROR_ARGUMENT_IS_NULL; ProfileInfo prof_inf; - prof_inf.user_uuid = uuid; - prof_inf.username = username; + prof_inf.user_uuid = std::move(uuid); + prof_inf.username = std::move(username); prof_inf.data = std::array(); prof_inf.creation_time = 0x0; prof_inf.is_open = false; return AddUser(prof_inf); } +ResultCode ProfileManager::CreateNewUser(UUID uuid, std::string username) { + std::array username_output; + if (username.size() > username_output.size()) + std::copy_n(username.begin(), username_output.size(), username_output.begin()); + else + std::copy(username.begin(), username.end(), username_output.begin()); + return CreateNewUser(uuid, std::move(username_output)); +} + size_t ProfileManager::GetUserIndex(UUID uuid) { if (!uuid) return -1; -- cgit v1.2.3 From e9978fd4f55b34804154193e1a619d28896d5a59 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Thu, 9 Aug 2018 01:37:55 +1000 Subject: Open first user added --- src/core/hle/service/acc/profile_manager.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 1633d5d48..03021cb64 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -7,7 +7,9 @@ constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1); constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20); ProfileManager::ProfileManager() { - CreateNewUser(UUID{1, 0}, Settings::values.username); + auto user_uuid = UUID{1, 0}; + CreateNewUser(user_uuid, Settings::values.username); + OpenUser(user_uuid); } size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { -- cgit v1.2.3 From 4e1471ef219dc376b8aa637ae05ad43851b8e407 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Thu, 9 Aug 2018 13:30:58 +1000 Subject: Don't add user if the uuid already exists --- src/core/hle/service/acc/profile_manager.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 03021cb64..ff2b71cce 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -4,6 +4,7 @@ namespace Service::Account { // TODO(ogniK): Get actual error codes 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); ProfileManager::ProfileManager() { @@ -45,6 +46,9 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array usernam return ERROR_ARGUMENT_IS_NULL; if (username[0] == 0x0) return ERROR_ARGUMENT_IS_NULL; + for (unsigned i = 0; i < user_count; i++) + if (uuid == profiles[i].user_uuid) + return ERROR_USER_ALREADY_EXISTS; ProfileInfo prof_inf; prof_inf.user_uuid = std::move(uuid); prof_inf.username = std::move(username); -- cgit v1.2.3 From 2a3b335b156552515e28f62df2617d06c241a29a Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sat, 11 Aug 2018 10:33:11 +1000 Subject: Added IsUserRegistrationRequestPermitted --- src/core/hle/service/acc/profile_manager.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index ff2b71cce..8e7d7194c 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -166,4 +166,10 @@ bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profil return GetProfileBaseAndData(user.user_uuid, profile, data); } +bool ProfileManager::CanSystemRegisterUser() { + return false; // TODO(ogniK): Games shouldn't have + // access to user registration, when we + // emulate qlaunch. Update this to dynamically change. +} + }; // namespace Service::Account -- cgit v1.2.3 From 82fa0bcea7c0231742716f7c79255eb107d4a933 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sat, 11 Aug 2018 16:47:33 +1000 Subject: First round of account changes --- src/core/hle/service/acc/profile_manager.cpp | 52 +++++++++++++++------------- 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 8e7d7194c..fda796966 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -1,3 +1,7 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + #include "core/settings.h" #include "profile_manager.h" @@ -15,14 +19,14 @@ ProfileManager::ProfileManager() { size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { if (user_count >= MAX_USERS) { - return -1; + return std::numeric_limits::max(); } profiles[user_count] = std::move(user); return user_count++; } bool ProfileManager::RemoveProfileAtIdx(size_t index) { - if (index >= MAX_USERS || index < 0 || index >= user_count) + if (index >= MAX_USERS || index >= user_count) return false; profiles[index] = ProfileInfo{}; if (index < user_count - 1) @@ -33,13 +37,13 @@ bool ProfileManager::RemoveProfileAtIdx(size_t index) { } ResultCode ProfileManager::AddUser(ProfileInfo user) { - if (AddToProfiles(user) == -1) { + if (AddToProfiles(user) == std::numeric_limits::max()) { return ERROR_TOO_MANY_USERS; } return RESULT_SUCCESS; } -ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array username) { +ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& username) { if (user_count == MAX_USERS) return ERROR_TOO_MANY_USERS; if (!uuid) @@ -64,67 +68,67 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::string username) { std::copy_n(username.begin(), username_output.size(), username_output.begin()); else std::copy(username.begin(), username.end(), username_output.begin()); - return CreateNewUser(uuid, std::move(username_output)); + return CreateNewUser(uuid, username_output); } -size_t ProfileManager::GetUserIndex(UUID uuid) { +size_t ProfileManager::GetUserIndex(const UUID& uuid) const { if (!uuid) - return -1; + return std::numeric_limits::max(); for (unsigned i = 0; i < user_count; i++) if (profiles[i].user_uuid == uuid) return i; - return -1; + return std::numeric_limits::max(); } -size_t ProfileManager::GetUserIndex(ProfileInfo user) { +size_t ProfileManager::GetUserIndex(ProfileInfo user) const { return GetUserIndex(user.user_uuid); } -bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) { +bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) const { if (index >= MAX_USERS) { profile.Invalidate(); return false; } - auto prof_info = profiles[index]; + const auto& prof_info = profiles[index]; profile.user_uuid = prof_info.user_uuid; profile.username = prof_info.username; profile.timestamp = prof_info.creation_time; return true; } -bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) { +bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) const { auto idx = GetUserIndex(uuid); return GetProfileBase(idx, profile); } -bool ProfileManager::GetProfileBase(ProfileInfo user, ProfileBase& profile) { +bool ProfileManager::GetProfileBase(ProfileInfo user, ProfileBase& profile) const { return GetProfileBase(user.user_uuid, profile); } -size_t ProfileManager::GetUserCount() { +size_t ProfileManager::GetUserCount() const { return user_count; } -bool ProfileManager::UserExists(UUID uuid) { - return (GetUserIndex(uuid) != -1); +bool ProfileManager::UserExists(UUID uuid) const { + return (GetUserIndex(uuid) != std::numeric_limits::max()); } void ProfileManager::OpenUser(UUID uuid) { auto idx = GetUserIndex(uuid); - if (idx == -1) + if (idx == std::numeric_limits::max()) return; profiles[idx].is_open = true; - last_openned_user = uuid; + last_opened_user = uuid; } void ProfileManager::CloseUser(UUID uuid) { auto idx = GetUserIndex(uuid); - if (idx == -1) + if (idx == std::numeric_limits::max()) return; profiles[idx].is_open = false; } -std::array ProfileManager::GetAllUsers() { +std::array ProfileManager::GetAllUsers() const { std::array output; for (unsigned i = 0; i < user_count; i++) { output[i] = profiles[i].user_uuid; @@ -132,7 +136,7 @@ std::array ProfileManager::GetAllUsers() { return output; } -std::array ProfileManager::GetOpenUsers() { +std::array ProfileManager::GetOpenUsers() const { std::array output; unsigned user_idx = 0; for (unsigned i = 0; i < user_count; i++) { @@ -143,8 +147,8 @@ std::array ProfileManager::GetOpenUsers() { return output; } -const UUID& ProfileManager::GetLastOpennedUser() { - return last_openned_user; +UUID ProfileManager::GetLastOpenedUser() const { + return last_opened_user; } bool ProfileManager::GetProfileBaseAndData(size_t index, ProfileBase& profile, @@ -166,7 +170,7 @@ bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profil return GetProfileBaseAndData(user.user_uuid, profile, data); } -bool ProfileManager::CanSystemRegisterUser() { +bool ProfileManager::CanSystemRegisterUser() const { return false; // TODO(ogniK): Games shouldn't have // access to user registration, when we // emulate qlaunch. Update this to dynamically change. -- cgit v1.2.3 From dfea525cbe3a337d3a1fa30136029599ae47ee71 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sat, 11 Aug 2018 18:26:13 +1000 Subject: Second round of account changes --- src/core/hle/service/acc/profile_manager.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index fda796966..14d65ff1b 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -2,8 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "core/hle/service/acc/profile_manager.h" #include "core/settings.h" -#include "profile_manager.h" namespace Service::Account { // TODO(ogniK): Get actual error codes @@ -28,10 +28,9 @@ size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { bool ProfileManager::RemoveProfileAtIdx(size_t index) { if (index >= MAX_USERS || index >= user_count) return false; - profiles[index] = ProfileInfo{}; if (index < user_count - 1) - for (size_t i = index; i < user_count - 1; i++) - profiles[i] = profiles[i + 1]; // Shift upper profiles down + std::rotate(profiles.begin() + index, profiles.begin() + index + 1, profiles.end()); + profiles.back() = {}; user_count--; return true; } @@ -50,9 +49,10 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& userna return ERROR_ARGUMENT_IS_NULL; if (username[0] == 0x0) return ERROR_ARGUMENT_IS_NULL; - for (unsigned i = 0; i < user_count; i++) - if (uuid == profiles[i].user_uuid) - return ERROR_USER_ALREADY_EXISTS; + if (std::any_of(profiles.begin(), profiles.end(), + [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) { + return ERROR_USER_ALREADY_EXISTS; + } ProfileInfo prof_inf; prof_inf.user_uuid = std::move(uuid); prof_inf.username = std::move(username); @@ -62,7 +62,7 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& userna return AddUser(prof_inf); } -ResultCode ProfileManager::CreateNewUser(UUID uuid, std::string username) { +ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) { std::array username_output; if (username.size() > username_output.size()) std::copy_n(username.begin(), username_output.size(), username_output.begin()); @@ -74,10 +74,13 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::string username) { size_t ProfileManager::GetUserIndex(const UUID& uuid) const { if (!uuid) return std::numeric_limits::max(); - for (unsigned i = 0; i < user_count; i++) - if (profiles[i].user_uuid == uuid) - return i; - return std::numeric_limits::max(); + + auto iter = std::find_if(profiles.begin(), profiles.end(), + [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; }); + if (iter == profiles.end()) { + return std::numeric_limits::max(); + } + return static_cast(std::distance(profiles.begin(), iter)); } size_t ProfileManager::GetUserIndex(ProfileInfo user) const { -- cgit v1.2.3 From acff9227626ce0903efbcdf91d1a12b695889d59 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sat, 11 Aug 2018 18:46:42 +1000 Subject: If statement style change --- src/core/hle/service/acc/profile_manager.cpp | 30 ++++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 14d65ff1b..8f3dab6a0 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -26,10 +26,12 @@ size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { } bool ProfileManager::RemoveProfileAtIdx(size_t index) { - if (index >= MAX_USERS || index >= user_count) + if (index >= MAX_USERS || index >= user_count) { return false; - if (index < user_count - 1) + } + if (index < user_count - 1) { std::rotate(profiles.begin() + index, profiles.begin() + index + 1, profiles.end()); + } profiles.back() = {}; user_count--; return true; @@ -43,12 +45,15 @@ ResultCode ProfileManager::AddUser(ProfileInfo user) { } ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& username) { - if (user_count == MAX_USERS) + if (user_count == MAX_USERS) { return ERROR_TOO_MANY_USERS; - if (!uuid) + } + if (!uuid) { return ERROR_ARGUMENT_IS_NULL; - if (username[0] == 0x0) + } + if (username[0] == 0x0) { return ERROR_ARGUMENT_IS_NULL; + } if (std::any_of(profiles.begin(), profiles.end(), [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) { return ERROR_USER_ALREADY_EXISTS; @@ -64,17 +69,18 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& userna ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) { std::array username_output; - if (username.size() > username_output.size()) + if (username.size() > username_output.size()) { std::copy_n(username.begin(), username_output.size(), username_output.begin()); - else + } else { std::copy(username.begin(), username.end(), username_output.begin()); + } return CreateNewUser(uuid, username_output); } size_t ProfileManager::GetUserIndex(const UUID& uuid) const { - if (!uuid) + if (!uuid) { return std::numeric_limits::max(); - + } auto iter = std::find_if(profiles.begin(), profiles.end(), [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; }); if (iter == profiles.end()) { @@ -118,16 +124,18 @@ bool ProfileManager::UserExists(UUID uuid) const { void ProfileManager::OpenUser(UUID uuid) { auto idx = GetUserIndex(uuid); - if (idx == std::numeric_limits::max()) + if (idx == std::numeric_limits::max()) { return; + } profiles[idx].is_open = true; last_opened_user = uuid; } void ProfileManager::CloseUser(UUID uuid) { auto idx = GetUserIndex(uuid); - if (idx == std::numeric_limits::max()) + if (idx == std::numeric_limits::max()) { return; + } profiles[idx].is_open = false; } -- cgit v1.2.3 From 662218e997de83fdcc7250f2348a750b1e5b3a51 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sat, 11 Aug 2018 20:15:59 +1000 Subject: Removed all for loops from the profile manager --- src/core/hle/service/acc/profile_manager.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 8f3dab6a0..ef793b311 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -141,20 +141,15 @@ void ProfileManager::CloseUser(UUID uuid) { std::array ProfileManager::GetAllUsers() const { std::array output; - for (unsigned i = 0; i < user_count; i++) { - output[i] = profiles[i].user_uuid; - } + std::transform(profiles.begin(), profiles.end(), output.begin(), + [](const ProfileInfo& p) { return p.user_uuid; }); return output; } std::array ProfileManager::GetOpenUsers() const { std::array output; - unsigned user_idx = 0; - for (unsigned i = 0; i < user_count; i++) { - if (profiles[i].is_open) { - output[i++] = profiles[i].user_uuid; - } - } + std::copy_if(profiles.begin(), profiles.end(), output.begin(), + [](const ProfileInfo& p) { return p.is_open; }); return output; } -- cgit v1.2.3 From b8e70faa2df59086f04ad1d128c742ea23037dc3 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sat, 11 Aug 2018 20:45:06 +1000 Subject: Added GetOpenUserCount --- src/core/hle/service/acc/profile_manager.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index ef793b311..e8f6884d1 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -118,6 +118,11 @@ size_t ProfileManager::GetUserCount() const { return user_count; } +size_t ProfileManager::GetOpenUserCount() const { + return std::count_if(profiles.begin(), profiles.end(), + [](const ProfileInfo& p) { return p.is_open; }); +} + bool ProfileManager::UserExists(UUID uuid) const { return (GetUserIndex(uuid) != std::numeric_limits::max()); } @@ -148,8 +153,12 @@ std::array ProfileManager::GetAllUsers() const { std::array ProfileManager::GetOpenUsers() const { std::array output; - std::copy_if(profiles.begin(), profiles.end(), output.begin(), - [](const ProfileInfo& p) { return p.is_open; }); + std::transform(profiles.begin(), profiles.end(), output.begin(), [](const ProfileInfo& p) { + if (p.is_open) + return p.user_uuid; + return UUID{}; + }); + std::stable_partition(output.begin(), output.end(), [](const UUID& uuid) { return uuid; }); return output; } -- cgit v1.2.3 From 0b6f8ba51e61792ea23a55394a963d0961a9906f Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sun, 12 Aug 2018 01:34:22 +1000 Subject: Code cleanup for profile manager --- src/core/hle/service/acc/profile_manager.cpp | 60 ++++++++++++++-------------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index e8f6884d1..ee13ae3cd 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "core/hle/service/acc/profile_manager.h" #include "core/settings.h" @@ -12,20 +13,21 @@ constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2); constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20); ProfileManager::ProfileManager() { + // TODO(ogniK): Create the default user we have for now until loading/saving users is added auto user_uuid = UUID{1, 0}; CreateNewUser(user_uuid, Settings::values.username); OpenUser(user_uuid); } -size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { +boost::optional ProfileManager::AddToProfiles(const ProfileInfo& user) { if (user_count >= MAX_USERS) { - return std::numeric_limits::max(); + return boost::none; } profiles[user_count] = std::move(user); return user_count++; } -bool ProfileManager::RemoveProfileAtIdx(size_t index) { +bool ProfileManager::RemoveProfileAtIndex(size_t index) { if (index >= MAX_USERS || index >= user_count) { return false; } @@ -38,7 +40,7 @@ bool ProfileManager::RemoveProfileAtIdx(size_t index) { } ResultCode ProfileManager::AddUser(ProfileInfo user) { - if (AddToProfiles(user) == std::numeric_limits::max()) { + if (AddToProfiles(user) == boost::none) { return ERROR_TOO_MANY_USERS; } return RESULT_SUCCESS; @@ -58,13 +60,13 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& userna [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) { return ERROR_USER_ALREADY_EXISTS; } - ProfileInfo prof_inf; - prof_inf.user_uuid = std::move(uuid); - prof_inf.username = std::move(username); - prof_inf.data = std::array(); - prof_inf.creation_time = 0x0; - prof_inf.is_open = false; - return AddUser(prof_inf); + ProfileInfo profile; + profile.user_uuid = std::move(uuid); + profile.username = std::move(username); + profile.data = {}; + profile.creation_time = 0x0; + profile.is_open = false; + return AddUser(profile); } ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) { @@ -77,28 +79,27 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) return CreateNewUser(uuid, username_output); } -size_t ProfileManager::GetUserIndex(const UUID& uuid) const { +boost::optional ProfileManager::GetUserIndex(const UUID& uuid) const { if (!uuid) { - return std::numeric_limits::max(); + return boost::none; } auto iter = std::find_if(profiles.begin(), profiles.end(), [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; }); if (iter == profiles.end()) { - return std::numeric_limits::max(); + return boost::none; } return static_cast(std::distance(profiles.begin(), iter)); } -size_t ProfileManager::GetUserIndex(ProfileInfo user) const { +boost::optional ProfileManager::GetUserIndex(ProfileInfo user) const { return GetUserIndex(user.user_uuid); } -bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) const { - if (index >= MAX_USERS) { - profile.Invalidate(); +bool ProfileManager::GetProfileBase(boost::optional index, ProfileBase& profile) const { + if (index == boost::none || index >= MAX_USERS) { return false; } - const auto& prof_info = profiles[index]; + const auto& prof_info = profiles[index.get()]; profile.user_uuid = prof_info.user_uuid; profile.username = prof_info.username; profile.timestamp = prof_info.creation_time; @@ -124,24 +125,24 @@ size_t ProfileManager::GetOpenUserCount() const { } bool ProfileManager::UserExists(UUID uuid) const { - return (GetUserIndex(uuid) != std::numeric_limits::max()); + return (GetUserIndex(uuid) != boost::none); } void ProfileManager::OpenUser(UUID uuid) { auto idx = GetUserIndex(uuid); - if (idx == std::numeric_limits::max()) { + if (idx == boost::none) { return; } - profiles[idx].is_open = true; + profiles[idx.get()].is_open = true; last_opened_user = uuid; } void ProfileManager::CloseUser(UUID uuid) { auto idx = GetUserIndex(uuid); - if (idx == std::numeric_limits::max()) { + if (idx == boost::none) { return; } - profiles[idx].is_open = false; + profiles[idx.get()].is_open = false; } std::array ProfileManager::GetAllUsers() const { @@ -166,22 +167,23 @@ UUID ProfileManager::GetLastOpenedUser() const { return last_opened_user; } -bool ProfileManager::GetProfileBaseAndData(size_t index, ProfileBase& profile, - std::array& data) { +bool ProfileManager::GetProfileBaseAndData(boost::optional index, ProfileBase& profile, + std::array& data) const { if (GetProfileBase(index, profile)) { - std::memcpy(data.data(), profiles[index].data.data(), MAX_DATA); + std::memcpy(data.data(), profiles[index.get()].data.data(), MAX_DATA); return true; } return false; } + bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile, - std::array& data) { + std::array& data) const { auto idx = GetUserIndex(uuid); return GetProfileBaseAndData(idx, profile, data); } bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile, - std::array& data) { + std::array& data) const { return GetProfileBaseAndData(user.user_uuid, profile, data); } -- cgit v1.2.3 From 2592e41301b39db27730b148e7dbfd41d83864c2 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sun, 12 Aug 2018 01:51:31 +1000 Subject: Added better explanations in the profile manager --- src/core/hle/service/acc/profile_manager.cpp | 32 +++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src/core/hle/service/acc/profile_manager.cpp') diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index ee13ae3cd..62c2121fa 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -19,6 +19,8 @@ ProfileManager::ProfileManager() { OpenUser(user_uuid); } +/// After a users creation it needs to be "registered" to the system. AddToProfiles handles the +/// internal management of the users profiles boost::optional ProfileManager::AddToProfiles(const ProfileInfo& user) { if (user_count >= MAX_USERS) { return boost::none; @@ -27,6 +29,7 @@ boost::optional ProfileManager::AddToProfiles(const ProfileInfo& user) { return user_count++; } +/// Deletes a specific profile based on it's profile index bool ProfileManager::RemoveProfileAtIndex(size_t index) { if (index >= MAX_USERS || index >= user_count) { return false; @@ -39,6 +42,7 @@ bool ProfileManager::RemoveProfileAtIndex(size_t index) { return true; } +/// Helper function to register a user to the system ResultCode ProfileManager::AddUser(ProfileInfo user) { if (AddToProfiles(user) == boost::none) { return ERROR_TOO_MANY_USERS; @@ -46,6 +50,8 @@ ResultCode ProfileManager::AddUser(ProfileInfo user) { return RESULT_SUCCESS; } +/// Create a new user on the system. If the uuid of the user already exists, the user is not +/// created. ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& username) { if (user_count == MAX_USERS) { return ERROR_TOO_MANY_USERS; @@ -62,13 +68,16 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array& userna } ProfileInfo profile; profile.user_uuid = std::move(uuid); - profile.username = std::move(username); + profile.username = username; profile.data = {}; profile.creation_time = 0x0; profile.is_open = false; return AddUser(profile); } +/// Creates a new user on the system. This function allows a much simpler method of registration +/// specifically by allowing an std::string for the username. This is required specifically since +/// we're loading a string straight from the config ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) { std::array username_output; if (username.size() > username_output.size()) { @@ -79,6 +88,7 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) return CreateNewUser(uuid, username_output); } +/// Returns a users profile index based on their user id. boost::optional ProfileManager::GetUserIndex(const UUID& uuid) const { if (!uuid) { return boost::none; @@ -91,10 +101,12 @@ boost::optional ProfileManager::GetUserIndex(const UUID& uuid) const { return static_cast(std::distance(profiles.begin(), iter)); } +/// Returns a users profile index based on their profile boost::optional ProfileManager::GetUserIndex(ProfileInfo user) const { return GetUserIndex(user.user_uuid); } +/// Returns the data structure used by the switch when GetProfileBase is called on acc:* bool ProfileManager::GetProfileBase(boost::optional index, ProfileBase& profile) const { if (index == boost::none || index >= MAX_USERS) { return false; @@ -106,28 +118,37 @@ bool ProfileManager::GetProfileBase(boost::optional index, ProfileBase& return true; } +/// Returns the data structure used by the switch when GetProfileBase is called on acc:* bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) const { auto idx = GetUserIndex(uuid); return GetProfileBase(idx, profile); } +/// Returns the data structure used by the switch when GetProfileBase is called on acc:* bool ProfileManager::GetProfileBase(ProfileInfo user, ProfileBase& profile) const { return GetProfileBase(user.user_uuid, profile); } +/// Returns the current user count on the system. We keep a variable which tracks the count so we +/// don't have to loop the internal profile array every call. size_t ProfileManager::GetUserCount() const { return user_count; } +/// Lists the current "opened" users on the system. Users are typically not open until they sign +/// into something or pick a profile. As of right now users should all be open until qlaunch is +/// booting size_t ProfileManager::GetOpenUserCount() const { return std::count_if(profiles.begin(), profiles.end(), [](const ProfileInfo& p) { return p.is_open; }); } +/// Checks if a user id exists in our profile manager bool ProfileManager::UserExists(UUID uuid) const { return (GetUserIndex(uuid) != boost::none); } +/// Opens a specific user void ProfileManager::OpenUser(UUID uuid) { auto idx = GetUserIndex(uuid); if (idx == boost::none) { @@ -137,6 +158,7 @@ void ProfileManager::OpenUser(UUID uuid) { last_opened_user = uuid; } +/// Closes a specific user void ProfileManager::CloseUser(UUID uuid) { auto idx = GetUserIndex(uuid); if (idx == boost::none) { @@ -145,6 +167,7 @@ void ProfileManager::CloseUser(UUID uuid) { profiles[idx.get()].is_open = false; } +/// Gets all valid user ids on the system std::array ProfileManager::GetAllUsers() const { std::array output; std::transform(profiles.begin(), profiles.end(), output.begin(), @@ -152,6 +175,8 @@ std::array ProfileManager::GetAllUsers() const { return output; } +/// Get all the open users on the system and zero out the rest of the data. This is specifically +/// needed for GetOpenUsers and we need to ensure the rest of the output buffer is zero'd out std::array ProfileManager::GetOpenUsers() const { std::array output; std::transform(profiles.begin(), profiles.end(), output.begin(), [](const ProfileInfo& p) { @@ -163,10 +188,12 @@ std::array ProfileManager::GetOpenUsers() const { return output; } +/// Returns the last user which was opened UUID ProfileManager::GetLastOpenedUser() const { return last_opened_user; } +/// Return the users profile base and the unknown arbitary data. bool ProfileManager::GetProfileBaseAndData(boost::optional index, ProfileBase& profile, std::array& data) const { if (GetProfileBase(index, profile)) { @@ -176,17 +203,20 @@ bool ProfileManager::GetProfileBaseAndData(boost::optional index, Profil return false; } +/// Return the users profile base and the unknown arbitary data. bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile, std::array& data) const { auto idx = GetUserIndex(uuid); return GetProfileBaseAndData(idx, profile, data); } +/// Return the users profile base and the unknown arbitary data. bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile, std::array& data) const { return GetProfileBaseAndData(user.user_uuid, profile, data); } +/// Returns if the system is allowing user registrations or not bool ProfileManager::CanSystemRegisterUser() const { return false; // TODO(ogniK): Games shouldn't have // access to user registration, when we -- cgit v1.2.3