mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-05-09 12:08:13 +03:00
Play Time (#1305)
* Play Time * SDL * QT * Sort / Formatting * Timer 1 minute * remove the seconds removes the seconds from the screen, but in the play_time.txt file it continues to record the seconds for better accuracy, and the screen is cleaner * fixes the invisible 0 * SDL . . . * Fix Timer
This commit is contained in:
parent
6fe26173dc
commit
d4eae28ce2
34 changed files with 303 additions and 7 deletions
|
@ -8,6 +8,7 @@
|
|||
#include "common/logging/backend.h"
|
||||
#include "common/logging/log.h"
|
||||
#ifdef ENABLE_QT_GUI
|
||||
#include <QtCore>
|
||||
#include "common/memory_patcher.h"
|
||||
#endif
|
||||
#include "common/assert.h"
|
||||
|
@ -79,6 +80,17 @@ Emulator::Emulator() {
|
|||
|
||||
// Load renderdoc module.
|
||||
VideoCore::LoadRenderDoc();
|
||||
|
||||
// Start the timer (Play Time)
|
||||
#ifdef ENABLE_QT_GUI
|
||||
start_time = std::chrono::steady_clock::now();
|
||||
const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
|
||||
QString filePath = QString::fromStdString((user_dir / "play_time.txt").string());
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
|
||||
LOG_INFO(Loader, "Error opening or creating play_time.txt");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Emulator::~Emulator() {
|
||||
|
@ -122,6 +134,14 @@ void Emulator::Run(const std::filesystem::path& file) {
|
|||
}
|
||||
#ifdef ENABLE_QT_GUI
|
||||
MemoryPatcher::g_game_serial = id;
|
||||
|
||||
// Timer for 'Play Time'
|
||||
QTimer* timer = new QTimer();
|
||||
QObject::connect(timer, &QTimer::timeout, [this, id]() {
|
||||
UpdatePlayTime(id);
|
||||
start_time = std::chrono::steady_clock::now();
|
||||
});
|
||||
timer->start(60000); // 60000 ms = 1 minute
|
||||
#endif
|
||||
title = param_sfo->GetString("TITLE").value_or("Unknown title");
|
||||
LOG_INFO(Loader, "Game id: {} Title: {}", id, title);
|
||||
|
@ -228,6 +248,10 @@ void Emulator::Run(const std::filesystem::path& file) {
|
|||
window->waitEvent();
|
||||
}
|
||||
|
||||
#ifdef ENABLE_QT_GUI
|
||||
UpdatePlayTime(id);
|
||||
#endif
|
||||
|
||||
std::exit(0);
|
||||
}
|
||||
|
||||
|
@ -269,4 +293,74 @@ void Emulator::LoadSystemModules(const std::filesystem::path& file) {
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef ENABLE_QT_GUI
|
||||
void Emulator::UpdatePlayTime(const std::string& serial) {
|
||||
const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
|
||||
QString filePath = QString::fromStdString((user_dir / "play_time.txt").string());
|
||||
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
|
||||
LOG_INFO(Loader, "Error opening play_time.txt");
|
||||
return;
|
||||
}
|
||||
|
||||
auto end_time = std::chrono::steady_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time);
|
||||
int totalSeconds = duration.count();
|
||||
|
||||
QTextStream in(&file);
|
||||
QStringList lines;
|
||||
QString content;
|
||||
while (!in.atEnd()) {
|
||||
content += in.readLine() + "\n";
|
||||
}
|
||||
file.close();
|
||||
|
||||
QStringList existingLines = content.split('\n', Qt::SkipEmptyParts);
|
||||
int accumulatedSeconds = 0;
|
||||
bool found = false;
|
||||
|
||||
for (const QString& line : existingLines) {
|
||||
QStringList parts = line.split(' ');
|
||||
if (parts.size() == 2 && parts[0] == QString::fromStdString(serial)) {
|
||||
QStringList timeParts = parts[1].split(':');
|
||||
if (timeParts.size() == 3) {
|
||||
int hours = timeParts[0].toInt();
|
||||
int minutes = timeParts[1].toInt();
|
||||
int seconds = timeParts[2].toInt();
|
||||
accumulatedSeconds = hours * 3600 + minutes * 60 + seconds;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
accumulatedSeconds += totalSeconds;
|
||||
int hours = accumulatedSeconds / 3600;
|
||||
int minutes = (accumulatedSeconds % 3600) / 60;
|
||||
int seconds = accumulatedSeconds % 60;
|
||||
QString playTimeSaved = QString::number(hours) + ":" +
|
||||
QString::number(minutes).rightJustified(2, '0') + ":" +
|
||||
QString::number(seconds).rightJustified(2, '0');
|
||||
|
||||
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
QTextStream out(&file);
|
||||
bool lineUpdated = false;
|
||||
|
||||
for (const QString& line : existingLines) {
|
||||
if (line.startsWith(QString::fromStdString(serial))) {
|
||||
out << QString::fromStdString(serial) + " " + playTimeSaved + "\n";
|
||||
lineUpdated = true;
|
||||
} else {
|
||||
out << line << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (!lineUpdated) {
|
||||
out << QString::fromStdString(serial) + " " + playTimeSaved + "\n";
|
||||
}
|
||||
}
|
||||
LOG_INFO(Loader, "Playing time for {}: {}", serial, playTimeSaved.toStdString());
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace Core
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue