[core, android] Initial playtime implementation (#2535)

So firstly, playtime code is moved to src/common and qt specific code to yuzu/utils.cpp.

The dependency on ProfileManager was removed because it was working properly on Android, and I think a shared playtime is better behavior.
Now, playtime is stored in a file called "playtime.bin".

JNI code is from Azahar although modified by me, as well as that I added code to reset the game's playtime which was missing for some reason on there.

Before this gets merged, I plan to add the ability to manually edit the database as well.

Note: Code still needs a bit of cleanup.

Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/2535
Reviewed-by: CamilleLaVey <camillelavey99@gmail.com>
Reviewed-by: Lizzie <lizzie@eden-emu.dev>
Reviewed-by: crueter <crueter@eden-emu.dev>
Reviewed-by: MaranBr <maranbr@eden-emu.dev>
Co-authored-by: inix <Nixy01@proton.me>
Co-committed-by: inix <Nixy01@proton.me>
This commit is contained in:
inix 2025-10-17 22:47:43 +02:00 committed by crueter
parent 9c7ed0f59d
commit 6bdf479488
No known key found for this signature in database
GPG key ID: 425ACD2D4830EBC6
23 changed files with 586 additions and 45 deletions

View file

@ -198,11 +198,11 @@ add_executable(yuzu
multiplayer/state.cpp
multiplayer/state.h
multiplayer/validation.h
play_time_manager.cpp
play_time_manager.h
precompiled_headers.h
startup_checks.cpp
startup_checks.h
set_play_time_dialog.cpp
set_play_time_dialog.h
util/clickable_label.cpp
util/clickable_label.h
util/controller_navigation.cpp

View file

@ -557,13 +557,15 @@ void GameList::AddGamePopup(QMenu& context_menu, u64 program_id, const std::stri
QAction* remove_update = remove_menu->addAction(tr("Remove Installed Update"));
QAction* remove_dlc = remove_menu->addAction(tr("Remove All Installed DLC"));
QAction* remove_custom_config = remove_menu->addAction(tr("Remove Custom Configuration"));
QAction* remove_play_time_data = remove_menu->addAction(tr("Remove Play Time Data"));
QAction* remove_cache_storage = remove_menu->addAction(tr("Remove Cache Storage"));
QAction* remove_gl_shader_cache = remove_menu->addAction(tr("Remove OpenGL Pipeline Cache"));
QAction* remove_vk_shader_cache = remove_menu->addAction(tr("Remove Vulkan Pipeline Cache"));
remove_menu->addSeparator();
QAction* remove_shader_cache = remove_menu->addAction(tr("Remove All Pipeline Caches"));
QAction* remove_all_content = remove_menu->addAction(tr("Remove All Installed Contents"));
QMenu* play_time_menu = context_menu.addMenu(tr("Manage Play Time"));
QAction* set_play_time = play_time_menu->addAction(tr("Edit Play Time Data"));
QAction* remove_play_time_data = play_time_menu->addAction(tr("Remove Play Time Data"));
QMenu* dump_romfs_menu = context_menu.addMenu(tr("Dump RomFS"));
QAction* dump_romfs = dump_romfs_menu->addAction(tr("Dump RomFS"));
QAction* dump_romfs_sdmc = dump_romfs_menu->addAction(tr("Dump RomFS to SDMC"));
@ -629,6 +631,8 @@ void GameList::AddGamePopup(QMenu& context_menu, u64 program_id, const std::stri
connect(remove_custom_config, &QAction::triggered, [this, program_id, path]() {
emit RemoveFileRequested(program_id, QtCommon::Game::GameListRemoveTarget::CustomConfiguration, path);
});
connect(set_play_time, &QAction::triggered,
[this, program_id]() { emit SetPlayTimeRequested(program_id); });
connect(remove_play_time_data, &QAction::triggered,
[this, program_id]() { emit RemovePlayTimeRequested(program_id); });
connect(remove_cache_storage, &QAction::triggered, [this, program_id, path] {

View file

@ -23,7 +23,7 @@
#include "qt_common/config/uisettings.h"
#include "qt_common/util/game.h"
#include "yuzu/compatibility_list.h"
#include "yuzu/play_time_manager.h"
#include "frontend_common/play_time_manager.h"
namespace Core {
class System;
@ -104,6 +104,7 @@ signals:
void RemoveFileRequested(u64 program_id, QtCommon::Game::GameListRemoveTarget target,
const std::string& game_path);
void RemovePlayTimeRequested(u64 program_id);
void SetPlayTimeRequested(u64 program_id);
void DumpRomFSRequested(u64 program_id, const std::string& game_path, DumpRomFSTarget target);
void VerifyIntegrityRequested(const std::string& game_path);
void CopyTIDRequested(u64 program_id);

View file

@ -21,7 +21,7 @@
#include "common/common_types.h"
#include "common/logging/log.h"
#include "common/string_util.h"
#include "yuzu/play_time_manager.h"
#include "frontend_common/play_time_manager.h"
#include "qt_common/config/uisettings.h"
#include "yuzu/util/util.h"
@ -241,7 +241,7 @@ public:
void setData(const QVariant& value, int role) override {
qulonglong time_seconds = value.toULongLong();
GameListItem::setData(PlayTime::ReadablePlayTime(time_seconds), Qt::DisplayRole);
GameListItem::setData(QString::fromStdString(PlayTime::PlayTimeManager::GetReadablePlayTime(time_seconds)), Qt::DisplayRole);
GameListItem::setData(value, PlayTimeRole);
}

View file

@ -20,7 +20,7 @@
#include "core/file_sys/registered_cache.h"
#include "qt_common/config/uisettings.h"
#include "yuzu/compatibility_list.h"
#include "yuzu/play_time_manager.h"
#include "frontend_common/play_time_manager.h"
namespace Core {
class System;

View file

@ -15,6 +15,8 @@
#include <memory>
#include <thread>
#include "set_play_time_dialog.h"
#ifdef __APPLE__
#include <unistd.h> // for chdir
#endif
@ -164,7 +166,7 @@ static FileSys::VirtualFile VfsDirectoryCreateFileWrapper(const FileSys::Virtual
#include "yuzu/install_dialog.h"
#include "yuzu/loading_screen.h"
#include "yuzu/main.h"
#include "yuzu/play_time_manager.h"
#include "frontend_common/play_time_manager.h"
#include "yuzu/startup_checks.h"
#include "qt_common/config/uisettings.h"
#include "yuzu/util/clickable_label.h"
@ -448,7 +450,7 @@ GMainWindow::GMainWindow(bool has_broken_vulkan)
SetDiscordEnabled(UISettings::values.enable_discord_presence.GetValue());
discord_rpc->Update();
play_time_manager = std::make_unique<PlayTime::PlayTimeManager>(QtCommon::system->GetProfileManager());
play_time_manager = std::make_unique<PlayTime::PlayTimeManager>();
Network::Init();
@ -1576,6 +1578,8 @@ void GMainWindow::ConnectWidgetEvents() {
connect(game_list, &GameList::RemoveFileRequested, this, &GMainWindow::OnGameListRemoveFile);
connect(game_list, &GameList::RemovePlayTimeRequested, this,
&GMainWindow::OnGameListRemovePlayTimeData);
connect(game_list, &GameList::SetPlayTimeRequested, this,
&GMainWindow::OnGameListSetPlayTime);
connect(game_list, &GameList::DumpRomFSRequested, this, &GMainWindow::OnGameListDumpRomFS);
connect(game_list, &GameList::VerifyIntegrityRequested, this,
&GMainWindow::OnGameListVerifyIntegrity);
@ -2636,6 +2640,19 @@ void GMainWindow::OnGameListRemoveFile(u64 program_id, QtCommon::Game::GameListR
}
}
void GMainWindow::OnGameListSetPlayTime(u64 program_id) {
const u64 current_play_time = play_time_manager->GetPlayTime(program_id);
SetPlayTimeDialog dialog(this, current_play_time);
if (dialog.exec() == QDialog::Accepted) {
const u64 total_seconds = dialog.GetTotalSeconds();
play_time_manager->SetPlayTime(program_id, total_seconds);
game_list->PopulateAsync(UISettings::values.game_dirs);
}
}
void GMainWindow::OnGameListRemovePlayTimeData(u64 program_id) {
if (QMessageBox::question(this, tr("Remove Play Time Data"), tr("Reset play time?"),
QMessageBox::Yes | QMessageBox::No,

View file

@ -345,6 +345,7 @@ private slots:
void OnGameListRemoveFile(u64 program_id, QtCommon::Game::GameListRemoveTarget target,
const std::string& game_path);
void OnGameListRemovePlayTimeData(u64 program_id);
void OnGameListSetPlayTime(u64 program_id);
void OnGameListDumpRomFS(u64 program_id, const std::string& game_path, DumpRomFSTarget target);
void OnGameListVerifyIntegrity(const std::string& game_path);
void OnGameListCopyTID(u64 program_id);

View file

@ -1,184 +0,0 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/fs/file.h"
#include "common/fs/fs.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_manager.h"
namespace PlayTime {
namespace {
struct PlayTimeElement {
ProgramId program_id;
PlayTime play_time;
};
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::GetEdenPath(Common::FS::EdenPath::PlayTimeDir) /
uuid->RawString().append(".bin");
}
[[nodiscard]] bool ReadPlayTimeFile(PlayTimeDatabase& out_play_time_db,
const Service::Account::ProfileManager& manager) {
const auto filename = GetCurrentUserPlayTimePath(manager);
if (!filename.has_value()) {
LOG_ERROR(Frontend, "Failed to get current user path");
return false;
}
out_play_time_db.clear();
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(Frontend, "Failed to open play time file: {}",
Common::FS::PathToUTF8String(filename.value()));
return false;
}
const size_t num_elements = file.GetSize() / sizeof(PlayTimeElement);
std::vector<PlayTimeElement> elements(num_elements);
if (file.ReadSpan<PlayTimeElement>(elements) != num_elements) {
return false;
}
for (const auto& [program_id, play_time] : elements) {
if (program_id != 0) {
out_play_time_db[program_id] = play_time;
}
}
}
return true;
}
[[nodiscard]] bool WritePlayTimeFile(const PlayTimeDatabase& play_time_db,
const Service::Account::ProfileManager& manager) {
const auto filename = GetCurrentUserPlayTimePath(manager);
if (!filename.has_value()) {
LOG_ERROR(Frontend, "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(Frontend, "Failed to open play time file: {}",
Common::FS::PathToUTF8String(filename.value()));
return false;
}
std::vector<PlayTimeElement> elements;
elements.reserve(play_time_db.size());
for (auto& [program_id, play_time] : play_time_db) {
if (program_id != 0) {
elements.push_back(PlayTimeElement{program_id, play_time});
}
}
return file.WriteSpan<PlayTimeElement>(elements) == elements.size();
}
} // namespace
PlayTimeManager::PlayTimeManager(Service::Account::ProfileManager& profile_manager)
: manager{profile_manager} {
if (!ReadPlayTimeFile(database, manager)) {
LOG_ERROR(Frontend, "Failed to read play time database! Resetting to default.");
}
}
PlayTimeManager::~PlayTimeManager() {
Save();
}
void PlayTimeManager::SetProgramId(u64 program_id) {
running_program_id = program_id;
}
void PlayTimeManager::Start() {
play_time_thread = std::jthread([&](std::stop_token stop_token) { AutoTimestamp(stop_token); });
}
void PlayTimeManager::Stop() {
play_time_thread = {};
}
void PlayTimeManager::AutoTimestamp(std::stop_token stop_token) {
Common::SetCurrentThreadName("PlayTimeReport");
using namespace std::literals::chrono_literals;
using std::chrono::seconds;
using std::chrono::steady_clock;
auto timestamp = steady_clock::now();
const auto GetDuration = [&]() -> u64 {
const auto last_timestamp = std::exchange(timestamp, steady_clock::now());
const auto duration = std::chrono::duration_cast<seconds>(timestamp - last_timestamp);
return static_cast<u64>(duration.count());
};
while (!stop_token.stop_requested()) {
Common::StoppableTimedWait(stop_token, 30s);
database[running_program_id] += GetDuration();
Save();
}
}
void PlayTimeManager::Save() {
if (!WritePlayTimeFile(database, manager)) {
LOG_ERROR(Frontend, "Failed to update play time database!");
}
}
u64 PlayTimeManager::GetPlayTime(u64 program_id) const {
auto it = database.find(program_id);
if (it != database.end()) {
return it->second;
} else {
return 0;
}
}
void PlayTimeManager::ResetProgramPlayTime(u64 program_id) {
database.erase(program_id);
Save();
}
QString ReadablePlayTime(qulonglong time_seconds) {
if (time_seconds == 0) {
return {};
}
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 bool is_minutes = time_minutes < 60;
const char* unit = is_minutes ? "m" : "h";
const auto value = is_minutes ? time_minutes : time_hours;
return QStringLiteral("%L1 %2")
.arg(value, 0, 'f', !is_minutes && time_seconds % 60 != 0)
.arg(QString::fromUtf8(unit));
}
} // namespace PlayTime

View file

@ -1,54 +0,0 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <QString>
#include <map>
#include "common/common_funcs.h"
#include "common/common_types.h"
#include <common/polyfill_thread.h>
// TODO(crueter): Extract this into frontend_common
namespace Service::Account {
class ProfileManager;
}
namespace PlayTime {
using ProgramId = u64;
using PlayTime = u64;
using PlayTimeDatabase = std::map<ProgramId, PlayTime>;
class PlayTimeManager {
public:
explicit PlayTimeManager(Service::Account::ProfileManager& profile_manager);
~PlayTimeManager();
YUZU_NON_COPYABLE(PlayTimeManager);
YUZU_NON_MOVEABLE(PlayTimeManager);
u64 GetPlayTime(u64 program_id) const;
void ResetProgramPlayTime(u64 program_id);
void SetProgramId(u64 program_id);
void Start();
void Stop();
private:
void AutoTimestamp(std::stop_token stop_token);
void Save();
PlayTimeDatabase database;
u64 running_program_id;
std::jthread play_time_thread;
Service::Account::ProfileManager& manager;
};
QString ReadablePlayTime(qulonglong time_seconds);
} // namespace PlayTime

View file

@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "yuzu/set_play_time_dialog.h"
#include "frontend_common/play_time_manager.h"
#include "ui_set_play_time_dialog.h"
SetPlayTimeDialog::SetPlayTimeDialog(QWidget* parent, u64 current_play_time)
: QDialog(parent), ui{std::make_unique<Ui::SetPlayTimeDialog>()} {
ui->setupUi(this);
ui->hoursSpinBox->setValue(
QString::fromStdString(PlayTime::PlayTimeManager::GetPlayTimeHours(current_play_time)).toInt());
ui->minutesSpinBox->setValue(
QString::fromStdString(PlayTime::PlayTimeManager::GetPlayTimeMinutes(current_play_time)).toInt());
ui->secondsSpinBox->setValue(
QString::fromStdString(PlayTime::PlayTimeManager::GetPlayTimeSeconds(current_play_time)).toInt());
connect(ui->hoursSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this,
&SetPlayTimeDialog::OnValueChanged);
connect(ui->minutesSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this,
&SetPlayTimeDialog::OnValueChanged);
connect(ui->secondsSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this,
&SetPlayTimeDialog::OnValueChanged);
}
SetPlayTimeDialog::~SetPlayTimeDialog() = default;
u64 SetPlayTimeDialog::GetTotalSeconds() const {
const u64 hours = static_cast<u64>(ui->hoursSpinBox->value());
const u64 minutes = static_cast<u64>(ui->minutesSpinBox->value());
const u64 seconds = static_cast<u64>(ui->secondsSpinBox->value());
return hours * 3600 + minutes * 60 + seconds;
}
void SetPlayTimeDialog::OnValueChanged() {
if (ui->errorLabel->isVisible()) {
ui->errorLabel->setVisible(false);
}
const u64 total_seconds = GetTotalSeconds();
constexpr u64 max_reasonable_time = 9999ULL * 3600;
if (total_seconds > max_reasonable_time) {
ui->errorLabel->setText(tr("Total play time reached maximum."));
ui->errorLabel->setVisible(true);
}
}

View file

@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <QDialog>
#include <memory>
#include "common/common_types.h"
namespace Ui {
class SetPlayTimeDialog;
}
class SetPlayTimeDialog : public QDialog {
Q_OBJECT
public:
explicit SetPlayTimeDialog(QWidget* parent, u64 current_play_time);
~SetPlayTimeDialog() override;
u64 GetTotalSeconds() const;
private:
void OnValueChanged();
std::unique_ptr<Ui::SetPlayTimeDialog> ui;
};

View file

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SetPlayTimeDialog</class>
<widget class="QDialog" name="SetPlayTimeDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>150</height>
</rect>
</property>
<property name="windowTitle">
<string>Set Play Time Data</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="inputLayout">
<item>
<widget class="QLabel" name="labelHours">
<property name="text">
<string>Hours:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="hoursSpinBox">
<property name="maximum">
<number>9999</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelMinutes">
<property name="text">
<string>Minutes:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="minutesSpinBox">
<property name="maximum">
<number>59</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelSeconds">
<property name="text">
<string>Seconds:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="secondsSpinBox">
<property name="maximum">
<number>59</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="errorLabel">
<property name="styleSheet">
<string notr="true">QLabel { color : red; }</string>
</property>
<property name="text">
<string/>
</property>
<property name="visible">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>SetPlayTimeDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>199</x>
<y>129</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>74</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>SetPlayTimeDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>199</x>
<y>129</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>74</y>
</hint>
</hints>
</connection>
</connections>
</ui>