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
|
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/launch_timestamp_cache.h"
#include <filesystem>
#include <fstream>
#include <mutex>
#include <sstream>
#include <string>
#include <unordered_map>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include "common/fs/fs.h"
#include "common/fs/file.h"
#include "common/fs/path_util.h"
#include "common/logging/log.h"
namespace Core::LaunchTimestampCache {
namespace {
using CacheMap = std::unordered_map<u64, s64>;
using CountMap = std::unordered_map<u64, u64>;
std::mutex g_mutex;
CacheMap g_cache;
CountMap g_counts;
bool g_loaded = false;
std::filesystem::path GetCachePath() {
return Common::FS::GetEdenPath(Common::FS::EdenPath::CacheDir) / "launched.json";
}
std::optional<std::string> ReadFileToString(const std::filesystem::path& path) {
const std::ifstream file{path, std::ios::in | std::ios::binary};
if (!file) {
return std::nullopt;
}
std::ostringstream ss;
ss << file.rdbuf();
return ss.str();
}
bool WriteStringToFile(const std::filesystem::path& path, const std::string& data) {
if (!Common::FS::CreateParentDirs(path)) {
return false;
}
std::ofstream file{path, std::ios::out | std::ios::binary | std::ios::trunc};
if (!file) {
return false;
}
file.write(data.data(), static_cast<std::streamsize>(data.size()));
return static_cast<bool>(file);
}
void Load() {
if (g_loaded) {
return;
}
g_loaded = true;
const auto path = GetCachePath();
if (!std::filesystem::exists(path)) {
return;
}
const auto data = ReadFileToString(path);
if (!data) {
LOG_WARNING(Core, "Failed to read launch timestamp cache: {}",
Common::FS::PathToUTF8String(path));
return;
}
try {
const auto json = nlohmann::json::parse(data->data(), data->data() + data->size());
if (!json.is_object()) {
return;
}
for (auto it = json.begin(); it != json.end(); ++it) {
const auto key_str = it.key();
const auto value = it.value();
u64 key{};
try {
key = std::stoull(key_str, nullptr, 16);
} catch (...) {
continue;
}
if (value.is_object()) {
if (value.contains("timestamp") && value["timestamp"].is_number_integer()) {
g_cache[key] = value["timestamp"].get<s64>();
}
if (value.contains("launch_count") && value["launch_count"].is_number_unsigned()) {
g_counts[key] = value["launch_count"].get<u64>();
}
} else if (value.is_number_integer()) {
// Legacy format: raw timestamp only
g_cache[key] = value.get<s64>();
}
}
} catch (const std::exception& e) {
LOG_WARNING(Core, "Failed to parse launch timestamp cache: {}", e.what());
}
}
void Save() {
nlohmann::json json = nlohmann::json::object();
for (const auto& [key, value] : g_cache) {
nlohmann::json entry = nlohmann::json::object();
entry["timestamp"] = value;
const auto count_it = g_counts.find(key);
entry["launch_count"] = count_it != g_counts.end() ? count_it->second : 0;
json[fmt::format("{:016X}", key)] = entry;
}
const auto path = GetCachePath();
if (!WriteStringToFile(path, json.dump(4))) {
LOG_WARNING(Core, "Failed to write launch timestamp cache: {}",
Common::FS::PathToUTF8String(path));
}
}
s64 NowSeconds() {
return std::time(nullptr);
}
} // namespace
void SaveLaunchTimestamp(u64 title_id) {
std::scoped_lock lk{g_mutex};
Load();
g_cache[title_id] = NowSeconds();
g_counts[title_id] = g_counts[title_id] + 1;
Save();
}
s64 GetLaunchTimestamp(u64 title_id) {
std::scoped_lock lk{g_mutex};
Load();
const auto it = g_cache.find(title_id);
if (it != g_cache.end()) {
return it->second;
}
// we need a timestamp, i decided on 01/01/2026 00:00
return 1767225600;
}
u64 GetLaunchCount(u64 title_id) {
std::scoped_lock lk{g_mutex};
Load();
const auto it = g_counts.find(title_id);
if (it != g_counts.end()) {
return it->second;
}
return 0;
}
} // namespace Core::LaunchTimestampCache
|