Skip to content
Snippets Groups Projects
Commit 0b6f8ba5 authored by David Marcec's avatar David Marcec
Browse files

Code cleanup for profile manager

parent d0b29504
No related branches found
No related tags found
No related merge requests found
...@@ -54,6 +54,8 @@ private: ...@@ -54,6 +54,8 @@ private:
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.PushRaw(profile_base); rb.PushRaw(profile_base);
} else { } else {
LOG_ERROR(Service_ACC, "Failed to get profile base and data for user={}",
user_id.Format());
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code
} }
...@@ -67,6 +69,7 @@ private: ...@@ -67,6 +69,7 @@ private:
rb.Push(RESULT_SUCCESS); rb.Push(RESULT_SUCCESS);
rb.PushRaw(profile_base); rb.PushRaw(profile_base);
} else { } else {
LOG_ERROR(Service_ACC, "Failed to get profile base for user={}", user_id.Format());
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code rb.Push(ResultCode(-1)); // TODO(ogniK): Get actual error code
} }
...@@ -93,7 +96,7 @@ private: ...@@ -93,7 +96,7 @@ private:
rb.Push<u32>(jpeg_size); rb.Push<u32>(jpeg_size);
} }
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.
}; };
...@@ -202,7 +205,7 @@ void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestCo ...@@ -202,7 +205,7 @@ void Module::Interface::GetBaasAccountManagerForApplication(Kernel::HLERequestCo
Module::Interface::Interface(std::shared_ptr<Module> module, Module::Interface::Interface(std::shared_ptr<Module> module,
std::shared_ptr<ProfileManager> profile_manager, const char* name) std::shared_ptr<ProfileManager> profile_manager, const char* name)
: ServiceFramework(name), module(std::move(module)), : ServiceFramework(name), module(std::move(module)),
profile_manager(std::make_shared<ProfileManager>(*profile_manager)) {} profile_manager(std::move(profile_manager)) {}
void InstallInterfaces(SM::ServiceManager& service_manager) { void InstallInterfaces(SM::ServiceManager& service_manager) {
auto module = std::make_shared<Module>(); auto module = std::make_shared<Module>();
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <boost/optional.hpp>
#include "core/hle/service/acc/profile_manager.h" #include "core/hle/service/acc/profile_manager.h"
#include "core/settings.h" #include "core/settings.h"
...@@ -12,20 +13,21 @@ constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, -2); ...@@ -12,20 +13,21 @@ 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);
ProfileManager::ProfileManager() { ProfileManager::ProfileManager() {
// TODO(ogniK): Create the default user we have for now until loading/saving users is added
auto user_uuid = UUID{1, 0}; auto user_uuid = UUID{1, 0};
CreateNewUser(user_uuid, Settings::values.username); CreateNewUser(user_uuid, Settings::values.username);
OpenUser(user_uuid); OpenUser(user_uuid);
} }
size_t ProfileManager::AddToProfiles(const ProfileInfo& user) { boost::optional<size_t> ProfileManager::AddToProfiles(const ProfileInfo& user) {
if (user_count >= MAX_USERS) { if (user_count >= MAX_USERS) {
return std::numeric_limits<size_t>::max(); return boost::none;
} }
profiles[user_count] = std::move(user); profiles[user_count] = std::move(user);
return user_count++; return user_count++;
} }
bool ProfileManager::RemoveProfileAtIdx(size_t index) { bool ProfileManager::RemoveProfileAtIndex(size_t index) {
if (index >= MAX_USERS || index >= user_count) { if (index >= MAX_USERS || index >= user_count) {
return false; return false;
} }
...@@ -38,7 +40,7 @@ bool ProfileManager::RemoveProfileAtIdx(size_t index) { ...@@ -38,7 +40,7 @@ bool ProfileManager::RemoveProfileAtIdx(size_t index) {
} }
ResultCode ProfileManager::AddUser(ProfileInfo user) { ResultCode ProfileManager::AddUser(ProfileInfo user) {
if (AddToProfiles(user) == std::numeric_limits<size_t>::max()) { if (AddToProfiles(user) == boost::none) {
return ERROR_TOO_MANY_USERS; return ERROR_TOO_MANY_USERS;
} }
return RESULT_SUCCESS; return RESULT_SUCCESS;
...@@ -58,13 +60,13 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array<u8, 0x20>& userna ...@@ -58,13 +60,13 @@ ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array<u8, 0x20>& userna
[&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) { [&uuid](const ProfileInfo& profile) { return uuid == profile.user_uuid; })) {
return ERROR_USER_ALREADY_EXISTS; return ERROR_USER_ALREADY_EXISTS;
} }
ProfileInfo prof_inf; ProfileInfo profile;
prof_inf.user_uuid = std::move(uuid); profile.user_uuid = std::move(uuid);
prof_inf.username = std::move(username); profile.username = std::move(username);
prof_inf.data = std::array<u8, MAX_DATA>(); profile.data = {};
prof_inf.creation_time = 0x0; profile.creation_time = 0x0;
prof_inf.is_open = false; profile.is_open = false;
return AddUser(prof_inf); return AddUser(profile);
} }
ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) { ResultCode ProfileManager::CreateNewUser(UUID uuid, const std::string& username) {
...@@ -77,28 +79,27 @@ 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); return CreateNewUser(uuid, username_output);
} }
size_t ProfileManager::GetUserIndex(const UUID& uuid) const { boost::optional<size_t> ProfileManager::GetUserIndex(const UUID& uuid) const {
if (!uuid) { if (!uuid) {
return std::numeric_limits<size_t>::max(); return boost::none;
} }
auto iter = std::find_if(profiles.begin(), profiles.end(), auto iter = std::find_if(profiles.begin(), profiles.end(),
[&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; }); [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; });
if (iter == profiles.end()) { if (iter == profiles.end()) {
return std::numeric_limits<size_t>::max(); return boost::none;
} }
return static_cast<size_t>(std::distance(profiles.begin(), iter)); return static_cast<size_t>(std::distance(profiles.begin(), iter));
} }
size_t ProfileManager::GetUserIndex(ProfileInfo user) const { boost::optional<size_t> ProfileManager::GetUserIndex(ProfileInfo user) const {
return GetUserIndex(user.user_uuid); return GetUserIndex(user.user_uuid);
} }
bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) const { bool ProfileManager::GetProfileBase(boost::optional<size_t> index, ProfileBase& profile) const {
if (index >= MAX_USERS) { if (index == boost::none || index >= MAX_USERS) {
profile.Invalidate();
return false; return false;
} }
const auto& prof_info = profiles[index]; const auto& prof_info = profiles[index.get()];
profile.user_uuid = prof_info.user_uuid; profile.user_uuid = prof_info.user_uuid;
profile.username = prof_info.username; profile.username = prof_info.username;
profile.timestamp = prof_info.creation_time; profile.timestamp = prof_info.creation_time;
...@@ -124,24 +125,24 @@ size_t ProfileManager::GetOpenUserCount() const { ...@@ -124,24 +125,24 @@ size_t ProfileManager::GetOpenUserCount() const {
} }
bool ProfileManager::UserExists(UUID uuid) const { bool ProfileManager::UserExists(UUID uuid) const {
return (GetUserIndex(uuid) != std::numeric_limits<size_t>::max()); return (GetUserIndex(uuid) != boost::none);
} }
void ProfileManager::OpenUser(UUID uuid) { void ProfileManager::OpenUser(UUID uuid) {
auto idx = GetUserIndex(uuid); auto idx = GetUserIndex(uuid);
if (idx == std::numeric_limits<size_t>::max()) { if (idx == boost::none) {
return; return;
} }
profiles[idx].is_open = true; profiles[idx.get()].is_open = true;
last_opened_user = uuid; last_opened_user = uuid;
} }
void ProfileManager::CloseUser(UUID uuid) { void ProfileManager::CloseUser(UUID uuid) {
auto idx = GetUserIndex(uuid); auto idx = GetUserIndex(uuid);
if (idx == std::numeric_limits<size_t>::max()) { if (idx == boost::none) {
return; return;
} }
profiles[idx].is_open = false; profiles[idx.get()].is_open = false;
} }
std::array<UUID, MAX_USERS> ProfileManager::GetAllUsers() const { std::array<UUID, MAX_USERS> ProfileManager::GetAllUsers() const {
...@@ -166,22 +167,23 @@ UUID ProfileManager::GetLastOpenedUser() const { ...@@ -166,22 +167,23 @@ UUID ProfileManager::GetLastOpenedUser() const {
return last_opened_user; return last_opened_user;
} }
bool ProfileManager::GetProfileBaseAndData(size_t index, ProfileBase& profile, bool ProfileManager::GetProfileBaseAndData(boost::optional<size_t> index, ProfileBase& profile,
std::array<u8, MAX_DATA>& data) { std::array<u8, MAX_DATA>& data) const {
if (GetProfileBase(index, profile)) { 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 true;
} }
return false; return false;
} }
bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile, bool ProfileManager::GetProfileBaseAndData(UUID uuid, ProfileBase& profile,
std::array<u8, MAX_DATA>& data) { std::array<u8, MAX_DATA>& data) const {
auto idx = GetUserIndex(uuid); auto idx = GetUserIndex(uuid);
return GetProfileBaseAndData(idx, profile, data); return GetProfileBaseAndData(idx, profile, data);
} }
bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile, bool ProfileManager::GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile,
std::array<u8, MAX_DATA>& data) { std::array<u8, MAX_DATA>& data) const {
return GetProfileBaseAndData(user.user_uuid, profile, data); return GetProfileBaseAndData(user.user_uuid, profile, data);
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#pragma once #pragma once
#include <array> #include <array>
#include "boost/optional.hpp"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/swap.h" #include "common/swap.h"
#include "core/hle/result.h" #include "core/hle/result.h"
...@@ -82,15 +83,17 @@ public: ...@@ -82,15 +83,17 @@ public:
ResultCode AddUser(ProfileInfo user); ResultCode AddUser(ProfileInfo user);
ResultCode CreateNewUser(UUID uuid, std::array<u8, 0x20>& username); ResultCode CreateNewUser(UUID uuid, std::array<u8, 0x20>& username);
ResultCode CreateNewUser(UUID uuid, const std::string& username); ResultCode CreateNewUser(UUID uuid, const std::string& username);
size_t GetUserIndex(const UUID& uuid) const; boost::optional<size_t> GetUserIndex(const UUID& uuid) const;
size_t GetUserIndex(ProfileInfo user) const; boost::optional<size_t> GetUserIndex(ProfileInfo user) const;
bool GetProfileBase(size_t index, ProfileBase& profile) const; bool GetProfileBase(boost::optional<size_t> index, ProfileBase& profile) const;
bool GetProfileBase(UUID uuid, ProfileBase& profile) const; bool GetProfileBase(UUID uuid, ProfileBase& profile) const;
bool GetProfileBase(ProfileInfo user, ProfileBase& profile) const; bool GetProfileBase(ProfileInfo user, ProfileBase& profile) const;
bool GetProfileBaseAndData(size_t index, ProfileBase& profile, std::array<u8, MAX_DATA>& data); bool GetProfileBaseAndData(boost::optional<size_t> index, ProfileBase& profile,
bool GetProfileBaseAndData(UUID uuid, ProfileBase& profile, std::array<u8, MAX_DATA>& data); std::array<u8, MAX_DATA>& data) const;
bool GetProfileBaseAndData(UUID uuid, ProfileBase& profile,
std::array<u8, MAX_DATA>& data) const;
bool GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile, bool GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile,
std::array<u8, MAX_DATA>& data); std::array<u8, MAX_DATA>& data) const;
size_t GetUserCount() const; size_t GetUserCount() const;
size_t GetOpenUserCount() const; size_t GetOpenUserCount() const;
bool UserExists(UUID uuid) const; bool UserExists(UUID uuid) const;
...@@ -105,10 +108,9 @@ public: ...@@ -105,10 +108,9 @@ public:
private: private:
std::array<ProfileInfo, MAX_USERS> profiles{}; std::array<ProfileInfo, MAX_USERS> profiles{};
size_t user_count = 0; size_t user_count = 0;
size_t AddToProfiles(const ProfileInfo& profile); boost::optional<size_t> AddToProfiles(const ProfileInfo& profile);
bool RemoveProfileAtIdx(size_t index); bool RemoveProfileAtIndex(size_t index);
UUID last_opened_user{0, 0}; UUID last_opened_user{0, 0};
}; };
using ProfileManagerPtr = std::unique_ptr<ProfileManager>;
}; // namespace Service::Account }; // namespace Service::Account
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment