| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- // Copyright 2018 yuzu emulator team
- // Licensed under GPLv2 or any later version
- // Refer to the license.txt file included.
- #include <cstring>
- #include <random>
- #include <fmt/format.h>
- #include "common/fs/file.h"
- #include "common/fs/fs.h"
- #include "common/fs/path_util.h"
- #include "common/settings.h"
- #include "core/hle/service/acc/profile_manager.h"
- namespace Service::Account {
- namespace FS = Common::FS;
- using Common::NewUUID;
- struct UserRaw {
- NewUUID uuid{};
- NewUUID uuid2{};
- u64 timestamp{};
- ProfileUsername username{};
- ProfileData extra_data{};
- };
- static_assert(sizeof(UserRaw) == 0xC8, "UserRaw has incorrect size.");
- struct ProfileDataRaw {
- INSERT_PADDING_BYTES(0x10);
- std::array<UserRaw, MAX_USERS> users{};
- };
- static_assert(sizeof(ProfileDataRaw) == 0x650, "ProfileDataRaw has incorrect size.");
- // TODO(ogniK): Get actual error codes
- constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, u32(-1));
- constexpr ResultCode ERROR_USER_ALREADY_EXISTS(ErrorModule::Account, u32(-2));
- constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20);
- constexpr char ACC_SAVE_AVATORS_BASE_PATH[] = "system/save/8000000000000010/su/avators";
- ProfileManager::ProfileManager() {
- ParseUserSaveFile();
- // Create an user if none are present
- if (user_count == 0) {
- CreateNewUser(NewUUID::MakeRandom(), "yuzu");
- }
- auto current =
- std::clamp<int>(static_cast<s32>(Settings::values.current_user), 0, MAX_USERS - 1);
- // If user index don't exist. Load the first user and change the active user
- if (!UserExistsIndex(current)) {
- current = 0;
- Settings::values.current_user = 0;
- }
- OpenUser(*GetUser(current));
- }
- ProfileManager::~ProfileManager() {
- WriteUserSaveFile();
- }
- /// After a users creation it needs to be "registered" to the system. AddToProfiles handles the
- /// internal management of the users profiles
- std::optional<std::size_t> ProfileManager::AddToProfiles(const ProfileInfo& profile) {
- if (user_count >= MAX_USERS) {
- return std::nullopt;
- }
- profiles[user_count] = profile;
- return user_count++;
- }
- /// Deletes a specific profile based on it's profile index
- bool ProfileManager::RemoveProfileAtIndex(std::size_t index) {
- if (index >= MAX_USERS || index >= user_count) {
- return false;
- }
- if (index < user_count - 1) {
- std::rotate(profiles.begin() + index, profiles.begin() + index + 1, profiles.end());
- }
- profiles.back() = {};
- user_count--;
- return true;
- }
- /// Helper function to register a user to the system
- ResultCode ProfileManager::AddUser(const ProfileInfo& user) {
- if (!AddToProfiles(user)) {
- return ERROR_TOO_MANY_USERS;
- }
- return ResultSuccess;
- }
- /// Create a new user on the system. If the uuid of the user already exists, the user is not
- /// created.
- ResultCode ProfileManager::CreateNewUser(NewUUID uuid, const ProfileUsername& username) {
- if (user_count == MAX_USERS) {
- return ERROR_TOO_MANY_USERS;
- }
- if (uuid.IsInvalid()) {
- return ERROR_ARGUMENT_IS_NULL;
- }
- 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;
- }
- return AddUser({
- .user_uuid = uuid,
- .username = username,
- .creation_time = 0,
- .data = {},
- .is_open = false,
- });
- }
- /// 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(NewUUID uuid, const std::string& username) {
- ProfileUsername 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, username_output);
- }
- std::optional<NewUUID> ProfileManager::GetUser(std::size_t index) const {
- if (index >= MAX_USERS) {
- return std::nullopt;
- }
- return profiles[index].user_uuid;
- }
- /// Returns a users profile index based on their user id.
- std::optional<std::size_t> ProfileManager::GetUserIndex(const NewUUID& uuid) const {
- if (uuid.IsInvalid()) {
- return std::nullopt;
- }
- const auto iter = std::find_if(profiles.begin(), profiles.end(),
- [&uuid](const ProfileInfo& p) { return p.user_uuid == uuid; });
- if (iter == profiles.end()) {
- return std::nullopt;
- }
- return static_cast<std::size_t>(std::distance(profiles.begin(), iter));
- }
- /// Returns a users profile index based on their profile
- std::optional<std::size_t> ProfileManager::GetUserIndex(const 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(std::optional<std::size_t> index, ProfileBase& profile) const {
- if (!index || index >= MAX_USERS) {
- return false;
- }
- 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;
- }
- /// Returns the data structure used by the switch when GetProfileBase is called on acc:*
- bool ProfileManager::GetProfileBase(NewUUID uuid, ProfileBase& profile) const {
- 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(const 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.
- std::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
- std::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(NewUUID uuid) const {
- return GetUserIndex(uuid).has_value();
- }
- bool ProfileManager::UserExistsIndex(std::size_t index) const {
- if (index >= MAX_USERS) {
- return false;
- }
- return profiles[index].user_uuid.IsValid();
- }
- /// Opens a specific user
- void ProfileManager::OpenUser(NewUUID uuid) {
- const auto idx = GetUserIndex(uuid);
- if (!idx) {
- return;
- }
- profiles[*idx].is_open = true;
- last_opened_user = uuid;
- }
- /// Closes a specific user
- void ProfileManager::CloseUser(NewUUID uuid) {
- const auto idx = GetUserIndex(uuid);
- if (!idx) {
- return;
- }
- profiles[*idx].is_open = false;
- }
- /// Gets all valid user ids on the system
- UserIDArray ProfileManager::GetAllUsers() const {
- UserIDArray output{};
- std::ranges::transform(profiles, output.begin(),
- [](const ProfileInfo& p) { return p.user_uuid; });
- 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
- UserIDArray ProfileManager::GetOpenUsers() const {
- UserIDArray output{};
- std::ranges::transform(profiles, output.begin(), [](const ProfileInfo& p) {
- if (p.is_open)
- return p.user_uuid;
- return Common::InvalidUUID;
- });
- std::stable_partition(output.begin(), output.end(),
- [](const NewUUID& uuid) { return uuid.IsValid(); });
- return output;
- }
- /// Returns the last user which was opened
- NewUUID ProfileManager::GetLastOpenedUser() const {
- return last_opened_user;
- }
- /// Return the users profile base and the unknown arbitary data.
- bool ProfileManager::GetProfileBaseAndData(std::optional<std::size_t> index, ProfileBase& profile,
- ProfileData& data) const {
- if (GetProfileBase(index, profile)) {
- data = profiles[*index].data;
- return true;
- }
- return false;
- }
- /// Return the users profile base and the unknown arbitary data.
- bool ProfileManager::GetProfileBaseAndData(NewUUID uuid, ProfileBase& profile,
- ProfileData& data) const {
- const auto idx = GetUserIndex(uuid);
- return GetProfileBaseAndData(idx, profile, data);
- }
- /// Return the users profile base and the unknown arbitary data.
- bool ProfileManager::GetProfileBaseAndData(const ProfileInfo& user, ProfileBase& profile,
- ProfileData& 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
- // emulate qlaunch. Update this to dynamically change.
- }
- bool ProfileManager::RemoveUser(NewUUID uuid) {
- const auto index = GetUserIndex(uuid);
- if (!index) {
- return false;
- }
- profiles[*index] = ProfileInfo{};
- std::stable_partition(profiles.begin(), profiles.end(),
- [](const ProfileInfo& profile) { return profile.user_uuid.IsValid(); });
- return true;
- }
- bool ProfileManager::SetProfileBase(NewUUID uuid, const ProfileBase& profile_new) {
- const auto index = GetUserIndex(uuid);
- if (!index || profile_new.user_uuid.IsInvalid()) {
- 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;
- }
- bool ProfileManager::SetProfileBaseAndData(Common::NewUUID uuid, const ProfileBase& profile_new,
- const ProfileData& data_new) {
- const auto index = GetUserIndex(uuid);
- if (index.has_value() && SetProfileBase(uuid, profile_new)) {
- profiles[*index].data = data_new;
- return true;
- }
- return false;
- }
- void ProfileManager::ParseUserSaveFile() {
- const auto save_path(FS::GetYuzuPath(FS::YuzuPath::NANDDir) / ACC_SAVE_AVATORS_BASE_PATH /
- "profiles.dat");
- const FS::IOFile save(save_path, FS::FileAccessMode::Read, FS::FileType::BinaryFile);
- if (!save.IsOpen()) {
- LOG_WARNING(Service_ACC, "Failed to load profile data from save data... Generating new "
- "user 'yuzu' with random NewUUID.");
- return;
- }
- ProfileDataRaw data;
- if (!save.ReadObject(data)) {
- LOG_WARNING(Service_ACC, "profiles.dat is smaller than expected... Generating new user "
- "'yuzu' with random NewUUID.");
- return;
- }
- for (const auto& user : data.users) {
- if (user.uuid.IsInvalid()) {
- continue;
- }
- AddUser({
- .user_uuid = user.uuid,
- .username = user.username,
- .creation_time = user.timestamp,
- .data = user.extra_data,
- .is_open = false,
- });
- }
- std::stable_partition(profiles.begin(), profiles.end(),
- [](const ProfileInfo& profile) { return profile.user_uuid.IsValid(); });
- }
- void ProfileManager::WriteUserSaveFile() {
- ProfileDataRaw raw{};
- for (std::size_t i = 0; i < MAX_USERS; ++i) {
- raw.users[i] = {
- .uuid = profiles[i].user_uuid,
- .uuid2 = profiles[i].user_uuid,
- .timestamp = profiles[i].creation_time,
- .username = profiles[i].username,
- .extra_data = profiles[i].data,
- };
- }
- const auto raw_path(FS::GetYuzuPath(FS::YuzuPath::NANDDir) / "system/save/8000000000000010");
- if (FS::IsFile(raw_path) && !FS::RemoveFile(raw_path)) {
- return;
- }
- const auto save_path(FS::GetYuzuPath(FS::YuzuPath::NANDDir) / ACC_SAVE_AVATORS_BASE_PATH /
- "profiles.dat");
- if (!FS::CreateParentDirs(save_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;
- }
- FS::IOFile save(save_path, FS::FileAccessMode::Write, FS::FileType::BinaryFile);
- if (!save.IsOpen() || !save.SetSize(sizeof(ProfileDataRaw)) || !save.WriteObject(raw)) {
- LOG_WARNING(Service_ACC, "Failed to write save data to file... No changes to user data "
- "made in current session will be saved.");
- }
- }
- }; // namespace Service::Account
|