summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/acc/profile_manager.h
blob: 5aa73a0302cab52830e3a9527eb31c58284edee1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
#include <array>
#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;

struct UUID {
    // UUIDs which are 0 are considered invalid!
    u128 uuid{0, 0};
    UUID() = default;
    explicit UUID(const u128& id) {
        uuid[0] = id[0];
        uuid[1] = id[1];
    };
    explicit UUID(const u64& lo, const u64& hi) {
        uuid[0] = lo;
        uuid[1] = hi;
    };
    operator bool() const {
        return uuid[0] != 0x0 && uuid[1] != 0x0;
    }

    bool operator==(const UUID& rhs) {
        return uuid[0] == rhs.uuid[0] && uuid[1] == rhs.uuid[1];
    }

    bool operator!=(const UUID& rhs) {
        return uuid[0] != rhs.uuid[0] || uuid[1] != rhs.uuid[1];
    }

    // TODO(ogniK): Properly generate uuids based on RFC-4122
    const UUID& Generate() {
        uuid[0] = (static_cast<u64>(std::rand()) << 32) | std::rand();
        uuid[1] = (static_cast<u64>(std::rand()) << 32) | std::rand();
        return *this;
    }
    void Invalidate() {
        uuid[0] = 0;
        uuid[1] = 0;
    }
    std::string Format() {
        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;
    std::array<u8, MAX_DATA> data;
};

struct ProfileBase {
    UUID user_uuid;
    u64_le timestamp;
    std::array<u8, 0x20> username;

    const void Invalidate() {
        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:
    ProfileManager() = default; // TODO(ogniK): Load from system save
    ResultCode AddUser(ProfileInfo user);
    ResultCode CreateNewUser(UUID uuid, std::array<u8, 0x20> username);
    size_t GetUserIndex(UUID uuid);
    size_t GetUserIndex(ProfileInfo user);
    bool GetProfileBase(size_t index, ProfileBase& profile);
    bool GetProfileBase(UUID uuid, ProfileBase& profile);
    bool GetProfileBase(ProfileInfo user, ProfileBase& profile);
    size_t GetUserCount();
    bool UserExists(UUID uuid);

private:
    std::array<ProfileInfo, MAX_USERS> profiles{};
    size_t user_count = 0;
    size_t AddToProfiles(const ProfileInfo& profile);
    bool RemoveProfileAtIdx(size_t index);
};
using ProfileManagerPtr = std::unique_ptr<ProfileManager>;

}; // namespace Service::Account