summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/acc/profile_manager.cpp
blob: 8819c57037db4c2c4007f0ec1245740fa5050570 (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
#include "profile_manager.h"

namespace Service::Account {
// TODO(ogniK): Get actual error codes
constexpr ResultCode ERROR_TOO_MANY_USERS(ErrorModule::Account, -1);
constexpr ResultCode ERROR_ARGUMENT_IS_NULL(ErrorModule::Account, 20);

size_t ProfileManager::AddToProfiles(const ProfileInfo& user) {
    if (user_count >= MAX_USERS) {
        return -1;
    }
    profiles[user_count] = std::move(user);
    return user_count++;
}

bool ProfileManager::RemoveProfileAtIdx(size_t index) {
    if (index >= MAX_USERS || index < 0 || index >= user_count)
        return false;
    profiles[index] = ProfileInfo{};
    if (index < user_count - 1)
        for (size_t i = index; i < user_count - 1; i++)
            profiles[i] = profiles[i + 1]; // Shift upper profiles down
    user_count--;
    return true;
}

ResultCode ProfileManager::AddUser(ProfileInfo user) {
    if (AddToProfiles(user) == -1) {
        return ERROR_TOO_MANY_USERS;
    }
    return RESULT_SUCCESS;
}

ResultCode ProfileManager::CreateNewUser(UUID uuid, std::array<u8, 0x20> username) {
    if (user_count == MAX_USERS)
        return ERROR_TOO_MANY_USERS;
    if (!uuid)
        return ERROR_ARGUMENT_IS_NULL;
    if (username[0] == 0x0)
        return ERROR_ARGUMENT_IS_NULL;
    ProfileInfo prof_inf;
    prof_inf.user_uuid = uuid;
    prof_inf.username = username;
    prof_inf.data = std::array<u8, MAX_DATA>();
    prof_inf.creation_time = 0x0;
    return AddUser(prof_inf);
}

size_t ProfileManager::GetUserIndex(UUID uuid) {
    for (unsigned i = 0; i < user_count; i++)
        if (profiles[i].user_uuid == uuid)
            return i;
    return -1;
}

size_t ProfileManager::GetUserIndex(ProfileInfo user) {
    return GetUserIndex(user.user_uuid);
}

bool ProfileManager::GetProfileBase(size_t index, ProfileBase& profile) {
    if (index >= MAX_USERS) {
        profile.Invalidate();
        return false;
    }
    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;
}

bool ProfileManager::GetProfileBase(UUID uuid, ProfileBase& profile) {
    auto idx = GetUserIndex(uuid);
    return GetProfileBase(idx, profile);
}

bool ProfileManager::GetProfileBase(ProfileInfo user, ProfileBase& profile) {
    return GetProfileBase(user.user_uuid, profile);
}

size_t ProfileManager::GetUserCount() {
    return user_count;
}

bool ProfileManager::UserExists(UUID uuid) {
    return (GetUserIndex(uuid) != -1);
}

}; // namespace Service::Account