2018-08-11 08:47:33 +02:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-08-08 14:26:42 +02:00
|
|
|
#pragma once
|
2018-08-11 08:47:33 +02:00
|
|
|
|
2018-08-08 14:26:42 +02:00
|
|
|
#include <array>
|
2018-08-20 23:24:13 +02:00
|
|
|
|
2018-08-11 17:34:22 +02:00
|
|
|
#include "boost/optional.hpp"
|
2018-08-08 14:26:42 +02:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/swap.h"
|
|
|
|
#include "core/hle/result.h"
|
|
|
|
|
|
|
|
namespace Service::Account {
|
|
|
|
constexpr size_t MAX_USERS = 8;
|
|
|
|
constexpr size_t MAX_DATA = 128;
|
2018-08-20 23:31:07 +02:00
|
|
|
constexpr u128 INVALID_UUID{{0, 0}};
|
2018-08-08 14:26:42 +02:00
|
|
|
|
|
|
|
struct UUID {
|
|
|
|
// UUIDs which are 0 are considered invalid!
|
2018-08-11 10:26:13 +02:00
|
|
|
u128 uuid = INVALID_UUID;
|
2018-08-08 14:26:42 +02:00
|
|
|
UUID() = default;
|
2018-08-11 08:47:33 +02:00
|
|
|
explicit UUID(const u128& id) : uuid{id} {}
|
2018-08-20 23:27:48 +02:00
|
|
|
explicit UUID(const u64 lo, const u64 hi) : uuid{{lo, hi}} {}
|
|
|
|
|
2018-08-11 08:47:33 +02:00
|
|
|
explicit operator bool() const {
|
2018-08-20 23:27:48 +02:00
|
|
|
return uuid != INVALID_UUID;
|
2018-08-08 14:26:42 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 08:47:33 +02:00
|
|
|
bool operator==(const UUID& rhs) const {
|
2018-08-20 23:27:48 +02:00
|
|
|
return uuid == rhs.uuid;
|
2018-08-08 14:26:42 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 08:47:33 +02:00
|
|
|
bool operator!=(const UUID& rhs) const {
|
|
|
|
return !operator==(rhs);
|
2018-08-08 14:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ogniK): Properly generate uuids based on RFC-4122
|
2018-08-20 23:24:13 +02:00
|
|
|
const UUID& Generate();
|
2018-08-11 17:51:31 +02:00
|
|
|
|
|
|
|
// Set the UUID to {0,0} to be considered an invalid user
|
2018-08-08 14:26:42 +02:00
|
|
|
void Invalidate() {
|
2018-08-11 10:26:13 +02:00
|
|
|
uuid = INVALID_UUID;
|
2018-08-08 14:26:42 +02:00
|
|
|
}
|
2018-08-11 08:47:33 +02:00
|
|
|
std::string Format() const {
|
2018-08-08 14:26:42 +02:00
|
|
|
return fmt::format("0x{:016X}{:016X}", uuid[1], uuid[0]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static_assert(sizeof(UUID) == 16, "UUID is an invalid size!");
|
|
|
|
|
|
|
|
/// This holds general information about a users profile. This is where we store all the information
|
|
|
|
/// based on a specific user
|
|
|
|
struct ProfileInfo {
|
|
|
|
UUID user_uuid;
|
|
|
|
std::array<u8, 0x20> username;
|
|
|
|
u64 creation_time;
|
2018-08-08 15:41:12 +02:00
|
|
|
std::array<u8, MAX_DATA> data; // TODO(ognik): Work out what this is
|
|
|
|
bool is_open;
|
2018-08-08 14:26:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ProfileBase {
|
|
|
|
UUID user_uuid;
|
|
|
|
u64_le timestamp;
|
|
|
|
std::array<u8, 0x20> username;
|
|
|
|
|
2018-08-11 17:51:31 +02:00
|
|
|
// Zero out all the fields to make the profile slot considered "Empty"
|
2018-08-11 16:41:17 +02:00
|
|
|
void Invalidate() {
|
2018-08-08 14:26:42 +02:00
|
|
|
user_uuid.Invalidate();
|
|
|
|
timestamp = 0;
|
|
|
|
username.fill(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static_assert(sizeof(ProfileBase) == 0x38, "ProfileBase is an invalid size");
|
|
|
|
|
|
|
|
/// The profile manager is used for handling multiple user profiles at once. It keeps track of open
|
|
|
|
/// users, all the accounts registered on the "system" as well as fetching individual "ProfileInfo"
|
|
|
|
/// objects
|
|
|
|
class ProfileManager {
|
|
|
|
public:
|
2018-08-08 17:09:12 +02:00
|
|
|
ProfileManager(); // TODO(ogniK): Load from system save
|
2018-08-08 14:26:42 +02:00
|
|
|
ResultCode AddUser(ProfileInfo user);
|
2018-08-11 08:47:33 +02:00
|
|
|
ResultCode CreateNewUser(UUID uuid, std::array<u8, 0x20>& username);
|
2018-08-11 10:26:13 +02:00
|
|
|
ResultCode CreateNewUser(UUID uuid, const std::string& username);
|
2018-08-11 17:34:22 +02:00
|
|
|
boost::optional<size_t> GetUserIndex(const UUID& uuid) const;
|
|
|
|
boost::optional<size_t> GetUserIndex(ProfileInfo user) const;
|
|
|
|
bool GetProfileBase(boost::optional<size_t> index, ProfileBase& profile) const;
|
2018-08-11 08:47:33 +02:00
|
|
|
bool GetProfileBase(UUID uuid, ProfileBase& profile) const;
|
|
|
|
bool GetProfileBase(ProfileInfo user, ProfileBase& profile) const;
|
2018-08-11 17:34:22 +02:00
|
|
|
bool GetProfileBaseAndData(boost::optional<size_t> index, ProfileBase& profile,
|
|
|
|
std::array<u8, MAX_DATA>& data) const;
|
|
|
|
bool GetProfileBaseAndData(UUID uuid, ProfileBase& profile,
|
|
|
|
std::array<u8, MAX_DATA>& data) const;
|
2018-08-08 15:41:12 +02:00
|
|
|
bool GetProfileBaseAndData(ProfileInfo user, ProfileBase& profile,
|
2018-08-11 17:34:22 +02:00
|
|
|
std::array<u8, MAX_DATA>& data) const;
|
2018-08-11 08:47:33 +02:00
|
|
|
size_t GetUserCount() const;
|
2018-08-11 12:45:06 +02:00
|
|
|
size_t GetOpenUserCount() const;
|
2018-08-11 08:47:33 +02:00
|
|
|
bool UserExists(UUID uuid) const;
|
2018-08-08 15:41:12 +02:00
|
|
|
void OpenUser(UUID uuid);
|
|
|
|
void CloseUser(UUID uuid);
|
2018-08-11 08:47:33 +02:00
|
|
|
std::array<UUID, MAX_USERS> GetOpenUsers() const;
|
|
|
|
std::array<UUID, MAX_USERS> GetAllUsers() const;
|
|
|
|
UUID GetLastOpenedUser() const;
|
2018-08-08 14:26:42 +02:00
|
|
|
|
2018-08-11 08:47:33 +02:00
|
|
|
bool CanSystemRegisterUser() const;
|
2018-08-11 02:33:11 +02:00
|
|
|
|
2018-08-08 14:26:42 +02:00
|
|
|
private:
|
|
|
|
std::array<ProfileInfo, MAX_USERS> profiles{};
|
|
|
|
size_t user_count = 0;
|
2018-08-11 17:34:22 +02:00
|
|
|
boost::optional<size_t> AddToProfiles(const ProfileInfo& profile);
|
|
|
|
bool RemoveProfileAtIndex(size_t index);
|
2018-08-11 08:47:33 +02:00
|
|
|
UUID last_opened_user{0, 0};
|
2018-08-08 14:26:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}; // namespace Service::Account
|