summaryrefslogtreecommitdiffstats
path: root/src/yuzu/play_time.cpp
blob: 6be0327b2a99d65165dc049df416bde09025ad9c (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/fs/file.h"
#include "common/fs/path_util.h"
#include "common/logging/log.h"
#include "common/settings.h"
#include "common/thread.h"
#include "core/hle/service/acc/profile_manager.h"

#include "yuzu/play_time.h"

namespace PlayTime {

void PlayTimeManager::SetProgramId(u64 program_id) {
    this->running_program_id = program_id;
}

inline void PlayTimeManager::UpdateTimestamp() {
    this->last_timestamp = std::chrono::steady_clock::now();
}

void PlayTimeManager::Start() {
    UpdateTimestamp();
    play_time_thread =
        std::jthread([&](std::stop_token stop_token) { this->AutoTimestamp(stop_token); });
}

void PlayTimeManager::Stop() {
    play_time_thread.request_stop();
}

void PlayTimeManager::AutoTimestamp(std::stop_token stop_token) {
    Common::SetCurrentThreadName("PlayTimeReport");

    using namespace std::literals::chrono_literals;

    const auto duration = 30s;
    while (Common::StoppableTimedWait(stop_token, duration)) {
        Save();
    }

    Save();
}

void PlayTimeManager::Save() {
    const auto now = std::chrono::steady_clock::now();
    const auto duration =
        static_cast<u64>(std::chrono::duration_cast<std::chrono::seconds>(
                             std::chrono::steady_clock::duration(now - this->last_timestamp))
                             .count());
    UpdateTimestamp();
    if (!UpdatePlayTime(running_program_id, duration)) {
        LOG_ERROR(Common, "Failed to update play time");
    }
}

bool UpdatePlayTime(u64 program_id, u64 add_play_time) {
    std::vector<PlayTimeElement> play_time_elements;
    if (!ReadPlayTimeFile(play_time_elements)) {
        return false;
    }
    const auto it = std::find(play_time_elements.begin(), play_time_elements.end(), program_id);

    if (it == play_time_elements.end()) {
        play_time_elements.push_back({.program_id = program_id, .play_time = add_play_time});
    } else {
        play_time_elements.at(it - play_time_elements.begin()).play_time += add_play_time;
    }
    if (!WritePlayTimeFile(play_time_elements)) {
        return false;
    }
    return true;
}

u64 GetPlayTime(u64 program_id) {
    std::vector<PlayTimeElement> play_time_elements;

    if (!ReadPlayTimeFile(play_time_elements)) {
        return 0;
    }
    const auto it = std::find(play_time_elements.begin(), play_time_elements.end(), program_id);
    if (it == play_time_elements.end()) {
        return 0;
    }
    return play_time_elements.at(it - play_time_elements.begin()).play_time;
}

bool PlayTimeManager::ResetProgramPlayTime(u64 program_id) {
    std::vector<PlayTimeElement> play_time_elements;

    if (!ReadPlayTimeFile(play_time_elements)) {
        return false;
    }
    const auto it = std::find(play_time_elements.begin(), play_time_elements.end(), program_id);
    if (it == play_time_elements.end()) {
        return false;
    }
    play_time_elements.erase(it);
    if (!WritePlayTimeFile(play_time_elements)) {
        return false;
    }
    return true;
}

std::optional<std::filesystem::path> GetCurrentUserPlayTimePath() {
    const Service::Account::ProfileManager manager;
    const auto uuid = manager.GetUser(static_cast<s32>(Settings::values.current_user));
    if (!uuid.has_value()) {
        return std::nullopt;
    }
    return Common::FS::GetYuzuPath(Common::FS::YuzuPath::PlayTimeDir) /
           uuid->RawString().append(".bin");
}

[[nodiscard]] bool ReadPlayTimeFile(std::vector<PlayTimeElement>& out_play_time_elements) {
    const auto filename = GetCurrentUserPlayTimePath();
    if (!filename.has_value()) {
        LOG_ERROR(Common, "Failed to get current user path");
        return false;
    }

    if (Common::FS::Exists(filename.value())) {
        Common::FS::IOFile file{filename.value(), Common::FS::FileAccessMode::Read,
                                Common::FS::FileType::BinaryFile};
        if (!file.IsOpen()) {
            LOG_ERROR(Common, "Failed to open play time file: {}",
                      Common::FS::PathToUTF8String(filename.value()));
            return false;
        }
        const size_t elem_num = file.GetSize() / sizeof(PlayTimeElement);
        out_play_time_elements.resize(elem_num);
        const bool success = file.ReadSpan<PlayTimeElement>(out_play_time_elements) == elem_num;
        file.Close();
        return success;
    } else {
        out_play_time_elements.clear();
        return true;
    }
}

[[nodiscard]] bool WritePlayTimeFile(const std::vector<PlayTimeElement>& play_time_elements) {
    const auto filename = GetCurrentUserPlayTimePath();
    if (!filename.has_value()) {
        LOG_ERROR(Common, "Failed to get current user path");
        return false;
    }
    Common::FS::IOFile file{filename.value(), Common::FS::FileAccessMode::Write,
                            Common::FS::FileType::BinaryFile};

    if (!file.IsOpen()) {
        LOG_ERROR(Common, "Failed to open play time file: {}",
                  Common::FS::PathToUTF8String(filename.value()));
        return false;
    }
    const bool success =
        file.WriteSpan<PlayTimeElement>(play_time_elements) == play_time_elements.size();
    file.Close();
    return success;
}

QString ReadablePlayTime(qulonglong time_seconds) {
    static constexpr std::array units{"m", "h"};
    if (time_seconds == 0) {
        return QLatin1String("");
    }
    const auto time_minutes = std::max(static_cast<double>(time_seconds) / 60, 1.0);
    const auto time_hours = static_cast<double>(time_seconds) / 3600;
    const int unit = time_minutes < 60 ? 0 : 1;
    const auto value = unit == 0 ? time_minutes : time_hours;

    return QStringLiteral("%L1 %2")
        .arg(value, 0, 'f', unit && time_seconds % 60 != 0)
        .arg(QString::fromUtf8(units[unit]));
}

} // namespace PlayTime