summaryrefslogtreecommitdiffstats
path: root/src/yuzu/play_time.h
blob: 68e40955cde7b34ccdc20e0ff019d0f0ebc54305 (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
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <QString>

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <optional>
#include <thread>

#include "common/common_types.h"
#include "common/fs/fs.h"
#include "common/polyfill_thread.h"
#include "core/core.h"

namespace PlayTime {
struct PlayTimeElement {
    u64 program_id;
    u64 play_time;

    inline bool operator==(const PlayTimeElement& other) const {
        return program_id == other.program_id;
    }

    inline bool operator==(const u64 _program_id) const {
        return program_id == _program_id;
    }
};

class PlayTimeManager {
public:
    explicit PlayTimeManager() = default;
    ~PlayTimeManager() = default;

public:
    YUZU_NON_COPYABLE(PlayTimeManager);
    YUZU_NON_MOVEABLE(PlayTimeManager);

public:
    bool ResetProgramPlayTime(u64 program_id);
    void SetProgramId(u64 program_id);
    inline void UpdateTimestamp();
    void Start();
    void Stop();

private:
    u64 running_program_id;
    std::chrono::steady_clock::time_point last_timestamp;
    std::jthread play_time_thread;
    void AutoTimestamp(std::stop_token stop_token);
    void Save();
};

std::optional<std::filesystem::path> GetCurrentUserPlayTimePath();

bool UpdatePlayTime(u64 program_id, u64 add_play_time);

[[nodiscard]] bool ReadPlayTimeFile(std::vector<PlayTimeElement>& out_play_time_elements);
[[nodiscard]] bool WritePlayTimeFile(const std::vector<PlayTimeElement>& play_time_elements);

u64 GetPlayTime(u64 program_id);

QString ReadablePlayTime(qulonglong time_seconds);

} // namespace PlayTime