summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/acc/profile_manager.h
blob: e21863e641286959c7481df742bb95dfd3776a3b (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>
#include <optional>

#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/swap.h"
#include "common/uuid.h"
#include "core/hle/result.h"

namespace Service::Account {

constexpr std::size_t MAX_USERS{8};
constexpr std::size_t profile_username_size{32};

using ProfileUsername = std::array<u8, profile_username_size>;
using UserIDArray = std::array<Common::UUID, MAX_USERS>;

/// Contains extra data related to a user.
/// TODO: RE this structure
struct UserData {
    INSERT_PADDING_WORDS_NOINIT(1);
    u32 icon_id;
    u8 bg_color_id;
    INSERT_PADDING_BYTES_NOINIT(0x7);
    INSERT_PADDING_BYTES_NOINIT(0x10);
    INSERT_PADDING_BYTES_NOINIT(0x60);
};
static_assert(sizeof(UserData) == 0x80, "UserData structure has incorrect size");

/// This holds general information about a users profile. This is where we store all the information
/// based on a specific user
struct ProfileInfo {
    Common::UUID user_uuid{};
    ProfileUsername username{};
    u64 creation_time{};
    UserData data{}; // TODO(ognik): Work out what this is
    bool is_open{};
};

struct ProfileBase {
    Common::UUID user_uuid;
    u64_le timestamp;
    ProfileUsername username;

    // Zero out all the fields to make the profile slot considered "Empty"
    void Invalidate() {
        user_uuid = {};
        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();
    ~ProfileManager();

    Result AddUser(const ProfileInfo& user);
    Result CreateNewUser(Common::UUID uuid, const ProfileUsername& username);
    Result CreateNewUser(Common::UUID uuid, const std::string& username);
    std::optional<Common::UUID> GetUser(std::size_t index) const;
    std::optional<std::size_t> GetUserIndex(const Common::UUID& uuid) const;
    std::optional<std::size_t> GetUserIndex(const ProfileInfo& user) const;
    bool GetProfileBase(std::optional<std::size_t> index, ProfileBase& profile) const;
    bool GetProfileBase(Common::UUID uuid, ProfileBase& profile) const;
    bool GetProfileBase(const ProfileInfo& user, ProfileBase& profile) const;
    bool GetProfileBaseAndData(std::optional<std::size_t> index, ProfileBase& profile,
                               UserData& data) const;
    bool GetProfileBaseAndData(Common::UUID uuid, ProfileBase& profile, UserData& data) const;
    bool GetProfileBaseAndData(const ProfileInfo& user, ProfileBase& profile, UserData& data) const;
    std::size_t GetUserCount() const;
    std::size_t GetOpenUserCount() const;
    bool UserExists(Common::UUID uuid) const;
    bool UserExistsIndex(std::size_t index) const;
    void OpenUser(Common::UUID uuid);
    void CloseUser(Common::UUID uuid);
    UserIDArray GetOpenUsers() const;
    UserIDArray GetAllUsers() const;
    Common::UUID GetLastOpenedUser() const;
    UserIDArray GetStoredOpenedUsers() const;
    void StoreOpenedUsers();

    bool CanSystemRegisterUser() const;

    bool RemoveUser(Common::UUID uuid);
    bool SetProfileBase(Common::UUID uuid, const ProfileBase& profile_new);
    bool SetProfileBaseAndData(Common::UUID uuid, const ProfileBase& profile_new,
                               const UserData& data_new);

    void WriteUserSaveFile();

private:
    void ParseUserSaveFile();
    std::optional<std::size_t> AddToProfiles(const ProfileInfo& profile);
    bool RemoveProfileAtIndex(std::size_t index);

    bool is_save_needed{};
    std::array<ProfileInfo, MAX_USERS> profiles{};
    std::array<ProfileInfo, MAX_USERS> stored_opened_profiles{};
    std::size_t user_count{};
    Common::UUID last_opened_user{};
};

}; // namespace Service::Account