mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-12-09 13:19:00 +00:00
Make UpdatePlayTime not depend on Qt (#3629)
* Make UpdatePlayTime not depend on Qt * sir clang offnir, the all-formatting
This commit is contained in:
@@ -2,7 +2,9 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <sstream>
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
#include <hwinfo/hwinfo.h>
|
#include <hwinfo/hwinfo.h>
|
||||||
|
|
||||||
@@ -389,74 +391,70 @@ void Emulator::LoadSystemModules(const std::string& game_serial) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_QT_GUI
|
|
||||||
void Emulator::UpdatePlayTime(const std::string& serial) {
|
void Emulator::UpdatePlayTime(const std::string& serial) {
|
||||||
const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
|
const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
|
||||||
QString filePath = QString::fromStdString((user_dir / "play_time.txt").string());
|
const auto filePath = (user_dir / "play_time.txt").string();
|
||||||
|
|
||||||
QFile file(filePath);
|
std::ifstream in(filePath);
|
||||||
if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
|
if (!in && !std::ofstream(filePath)) {
|
||||||
LOG_INFO(Loader, "Error opening play_time.txt");
|
LOG_INFO(Loader, "Error opening play_time.txt");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto end_time = std::chrono::steady_clock::now();
|
auto end_time = std::chrono::steady_clock::now();
|
||||||
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time);
|
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time);
|
||||||
int totalSeconds = duration.count();
|
int total_seconds = static_cast<int>(duration.count());
|
||||||
|
|
||||||
QTextStream in(&file);
|
std::vector<std::string> lines;
|
||||||
QStringList lines;
|
std::string line;
|
||||||
QString content;
|
while (std::getline(in, line)) {
|
||||||
while (!in.atEnd()) {
|
lines.push_back(line);
|
||||||
content += in.readLine() + "\n";
|
|
||||||
}
|
}
|
||||||
file.close();
|
in.close();
|
||||||
|
|
||||||
QStringList existingLines = content.split('\n', Qt::SkipEmptyParts);
|
int accumulated_seconds = 0;
|
||||||
int accumulatedSeconds = 0;
|
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
||||||
for (const QString& line : existingLines) {
|
for (const auto& l : lines) {
|
||||||
QStringList parts = line.split(' ');
|
std::istringstream iss(l);
|
||||||
if (parts.size() == 2 && parts[0] == QString::fromStdString(serial)) {
|
std::string s, time_str;
|
||||||
QStringList timeParts = parts[1].split(':');
|
if (iss >> s >> time_str && s == serial) {
|
||||||
if (timeParts.size() == 3) {
|
int h, m, s_;
|
||||||
int hours = timeParts[0].toInt();
|
char c1, c2;
|
||||||
int minutes = timeParts[1].toInt();
|
std::istringstream ts(time_str);
|
||||||
int seconds = timeParts[2].toInt();
|
if (ts >> h >> c1 >> m >> c2 >> s_ && c1 == ':' && c2 == ':') {
|
||||||
accumulatedSeconds = hours * 3600 + minutes * 60 + seconds;
|
accumulated_seconds = h * 3600 + m * 60 + s_;
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
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)) {
|
accumulated_seconds += total_seconds;
|
||||||
QTextStream out(&file);
|
int hours = accumulated_seconds / 3600;
|
||||||
bool lineUpdated = false;
|
int minutes = (accumulated_seconds % 3600) / 60;
|
||||||
|
int seconds = accumulated_seconds % 60;
|
||||||
|
|
||||||
for (const QString& line : existingLines) {
|
std::string playTimeSaved = fmt::format("{:d}:{:02d}:{:02d}", hours, minutes, seconds);
|
||||||
if (line.startsWith(QString::fromStdString(serial))) {
|
|
||||||
out << QString::fromStdString(serial) + " " + playTimeSaved + "\n";
|
|
||||||
lineUpdated = true;
|
|
||||||
} else {
|
|
||||||
out << line << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!lineUpdated) {
|
std::ofstream outfile(filePath, std::ios::trunc);
|
||||||
out << QString::fromStdString(serial) + " " + playTimeSaved + "\n";
|
bool lineUpdated = false;
|
||||||
|
for (const auto& l : lines) {
|
||||||
|
std::istringstream iss(l);
|
||||||
|
std::string s;
|
||||||
|
if (iss >> s && s == serial) {
|
||||||
|
outfile << fmt::format("{} {}\n", serial, playTimeSaved);
|
||||||
|
lineUpdated = true;
|
||||||
|
} else {
|
||||||
|
outfile << l << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LOG_INFO(Loader, "Playing time for {}: {}", serial, playTimeSaved.toStdString());
|
|
||||||
|
if (!lineUpdated) {
|
||||||
|
outfile << fmt::format("{} {}\n", serial, playTimeSaved);
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INFO(Loader, "Playing time for {}: {}", serial, playTimeSaved);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace Core
|
} // namespace Core
|
||||||
|
|||||||
Reference in New Issue
Block a user