mirror of
https://github.com/yuzu-emu/yuzu.git
synced 2024-11-15 20:10:05 +00:00
Address review comments
This commit is contained in:
parent
5464423667
commit
667ec28697
@ -195,8 +195,8 @@ add_executable(yuzu
|
||||
multiplayer/state.cpp
|
||||
multiplayer/state.h
|
||||
multiplayer/validation.h
|
||||
play_time.cpp
|
||||
play_time.h
|
||||
play_time_manager.cpp
|
||||
play_time_manager.h
|
||||
precompiled_headers.h
|
||||
qt_common.cpp
|
||||
qt_common.h
|
||||
|
@ -312,8 +312,10 @@ void GameList::OnFilterCloseClicked() {
|
||||
}
|
||||
|
||||
GameList::GameList(FileSys::VirtualFilesystem vfs_, FileSys::ManualContentProvider* provider_,
|
||||
Core::System& system_, GMainWindow* parent)
|
||||
: QWidget{parent}, vfs{std::move(vfs_)}, provider{provider_}, system{system_} {
|
||||
PlayTime::PlayTimeManager& play_time_manager_, Core::System& system_,
|
||||
GMainWindow* parent)
|
||||
: QWidget{parent}, vfs{std::move(vfs_)}, provider{provider_},
|
||||
play_time_manager{play_time_manager_}, system{system_} {
|
||||
watcher = new QFileSystemWatcher(this);
|
||||
connect(watcher, &QFileSystemWatcher::directoryChanged, this, &GameList::RefreshGameDirectory);
|
||||
|
||||
@ -826,7 +828,7 @@ void GameList::PopulateAsync(QVector<UISettings::GameDir>& game_dirs) {
|
||||
emit ShouldCancelWorker();
|
||||
|
||||
GameListWorker* worker =
|
||||
new GameListWorker(vfs, provider, game_dirs, compatibility_list, system);
|
||||
new GameListWorker(vfs, provider, game_dirs, compatibility_list, play_time_manager, system);
|
||||
|
||||
connect(worker, &GameListWorker::EntryReady, this, &GameList::AddEntry, Qt::QueuedConnection);
|
||||
connect(worker, &GameListWorker::DirEntryReady, this, &GameList::AddDirEntry,
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "core/core.h"
|
||||
#include "uisettings.h"
|
||||
#include "yuzu/compatibility_list.h"
|
||||
#include "yuzu/play_time_manager.h"
|
||||
|
||||
namespace Core {
|
||||
class System;
|
||||
@ -79,7 +80,8 @@ public:
|
||||
};
|
||||
|
||||
explicit GameList(std::shared_ptr<FileSys::VfsFilesystem> vfs_,
|
||||
FileSys::ManualContentProvider* provider_, Core::System& system_,
|
||||
FileSys::ManualContentProvider* provider_,
|
||||
PlayTime::PlayTimeManager& play_time_manager_, Core::System& system_,
|
||||
GMainWindow* parent = nullptr);
|
||||
~GameList() override;
|
||||
|
||||
@ -168,6 +170,7 @@ private:
|
||||
|
||||
friend class GameListSearchField;
|
||||
|
||||
const PlayTime::PlayTimeManager& play_time_manager;
|
||||
Core::System& system;
|
||||
};
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
#include "common/common_types.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/string_util.h"
|
||||
#include "yuzu/play_time.h"
|
||||
#include "yuzu/play_time_manager.h"
|
||||
#include "yuzu/uisettings.h"
|
||||
#include "yuzu/util/util.h"
|
||||
|
||||
|
@ -27,7 +27,6 @@
|
||||
#include "yuzu/game_list.h"
|
||||
#include "yuzu/game_list_p.h"
|
||||
#include "yuzu/game_list_worker.h"
|
||||
#include "yuzu/play_time.h"
|
||||
#include "yuzu/uisettings.h"
|
||||
|
||||
namespace {
|
||||
@ -195,6 +194,7 @@ QList<QStandardItem*> MakeGameListEntry(const std::string& path, const std::stri
|
||||
const std::size_t size, const std::vector<u8>& icon,
|
||||
Loader::AppLoader& loader, u64 program_id,
|
||||
const CompatibilityList& compatibility_list,
|
||||
const PlayTime::PlayTimeManager& play_time_manager,
|
||||
const FileSys::PatchManager& patch) {
|
||||
const auto it = FindMatchingCompatibilityEntry(compatibility_list, program_id);
|
||||
|
||||
@ -213,7 +213,7 @@ QList<QStandardItem*> MakeGameListEntry(const std::string& path, const std::stri
|
||||
new GameListItemCompat(compatibility),
|
||||
new GameListItem(file_type_string),
|
||||
new GameListItemSize(size),
|
||||
new GameListItemPlayTime(PlayTime::GetPlayTime(program_id)),
|
||||
new GameListItemPlayTime(play_time_manager.GetPlayTime(program_id)),
|
||||
};
|
||||
|
||||
const auto patch_versions = GetGameListCachedObject(
|
||||
@ -229,9 +229,12 @@ QList<QStandardItem*> MakeGameListEntry(const std::string& path, const std::stri
|
||||
GameListWorker::GameListWorker(FileSys::VirtualFilesystem vfs_,
|
||||
FileSys::ManualContentProvider* provider_,
|
||||
QVector<UISettings::GameDir>& game_dirs_,
|
||||
const CompatibilityList& compatibility_list_, Core::System& system_)
|
||||
const CompatibilityList& compatibility_list_,
|
||||
const PlayTime::PlayTimeManager& play_time_manager_,
|
||||
Core::System& system_)
|
||||
: vfs{std::move(vfs_)}, provider{provider_}, game_dirs{game_dirs_},
|
||||
compatibility_list{compatibility_list_}, system{system_} {}
|
||||
compatibility_list{compatibility_list_},
|
||||
play_time_manager{play_time_manager_}, system{system_} {}
|
||||
|
||||
GameListWorker::~GameListWorker() = default;
|
||||
|
||||
@ -282,7 +285,7 @@ void GameListWorker::AddTitlesToGameList(GameListDir* parent_dir) {
|
||||
}
|
||||
|
||||
emit EntryReady(MakeGameListEntry(file->GetFullPath(), name, file->GetSize(), icon, *loader,
|
||||
program_id, compatibility_list, patch),
|
||||
program_id, compatibility_list, play_time_manager, patch),
|
||||
parent_dir);
|
||||
}
|
||||
}
|
||||
@ -359,7 +362,8 @@ void GameListWorker::ScanFileSystem(ScanTarget target, const std::string& dir_pa
|
||||
|
||||
emit EntryReady(MakeGameListEntry(physical_name, name,
|
||||
Common::FS::GetSize(physical_name), icon,
|
||||
*loader, id, compatibility_list, patch),
|
||||
*loader, id, compatibility_list,
|
||||
play_time_manager, patch),
|
||||
parent_dir);
|
||||
}
|
||||
} else {
|
||||
@ -372,10 +376,11 @@ void GameListWorker::ScanFileSystem(ScanTarget target, const std::string& dir_pa
|
||||
const FileSys::PatchManager patch{program_id, system.GetFileSystemController(),
|
||||
system.GetContentProvider()};
|
||||
|
||||
emit EntryReady(
|
||||
MakeGameListEntry(physical_name, name, Common::FS::GetSize(physical_name),
|
||||
icon, *loader, program_id, compatibility_list, patch),
|
||||
parent_dir);
|
||||
emit EntryReady(MakeGameListEntry(physical_name, name,
|
||||
Common::FS::GetSize(physical_name), icon,
|
||||
*loader, program_id, compatibility_list,
|
||||
play_time_manager, patch),
|
||||
parent_dir);
|
||||
}
|
||||
}
|
||||
} else if (is_dir) {
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include <QString>
|
||||
|
||||
#include "yuzu/compatibility_list.h"
|
||||
#include "yuzu/play_time_manager.h"
|
||||
|
||||
namespace Core {
|
||||
class System;
|
||||
@ -36,7 +37,9 @@ public:
|
||||
explicit GameListWorker(std::shared_ptr<FileSys::VfsFilesystem> vfs_,
|
||||
FileSys::ManualContentProvider* provider_,
|
||||
QVector<UISettings::GameDir>& game_dirs_,
|
||||
const CompatibilityList& compatibility_list_, Core::System& system_);
|
||||
const CompatibilityList& compatibility_list_,
|
||||
const PlayTime::PlayTimeManager& play_time_manager_,
|
||||
Core::System& system_);
|
||||
~GameListWorker() override;
|
||||
|
||||
/// Starts the processing of directory tree information.
|
||||
@ -76,6 +79,7 @@ private:
|
||||
FileSys::ManualContentProvider* provider;
|
||||
QVector<UISettings::GameDir>& game_dirs;
|
||||
const CompatibilityList& compatibility_list;
|
||||
const PlayTime::PlayTimeManager& play_time_manager;
|
||||
|
||||
QStringList watch_list;
|
||||
std::atomic_bool stop_processing;
|
||||
|
@ -146,7 +146,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.h"
|
||||
#include "yuzu/play_time_manager.h"
|
||||
#include "yuzu/startup_checks.h"
|
||||
#include "yuzu/uisettings.h"
|
||||
#include "yuzu/util/clickable_label.h"
|
||||
@ -980,7 +980,7 @@ void GMainWindow::InitializeWidgets() {
|
||||
render_window = new GRenderWindow(this, emu_thread.get(), input_subsystem, *system);
|
||||
render_window->hide();
|
||||
|
||||
game_list = new GameList(vfs, provider.get(), *system, this);
|
||||
game_list = new GameList(vfs, provider.get(), *play_time_manager, *system, this);
|
||||
ui->horizontalLayout->addWidget(game_list);
|
||||
|
||||
game_list_placeholder = new GameListPlaceholder(this);
|
||||
@ -2469,11 +2469,8 @@ void GMainWindow::OnGameListRemovePlayTimeData(u64 program_id) {
|
||||
QMessageBox::No) != QMessageBox::Yes) {
|
||||
return;
|
||||
}
|
||||
if (!play_time_manager->ResetProgramPlayTime(program_id)) {
|
||||
QMessageBox::warning(this, tr("Error Resetting Play Time Data"),
|
||||
tr("Play time couldn't be cleared"));
|
||||
return;
|
||||
}
|
||||
|
||||
play_time_manager->ResetProgramPlayTime(program_id);
|
||||
game_list->PopulateAsync(UISettings::values.game_dirs);
|
||||
}
|
||||
|
||||
|
@ -1,177 +0,0 @@
|
||||
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "common/fs/file.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.h"
|
||||
|
||||
namespace PlayTime {
|
||||
|
||||
void PlayTimeManager::SetProgramId(u64 program_id) {
|
||||
this->running_program_id = program_id;
|
||||
}
|
||||
|
||||
inline void PlayTimeManager::UpdateTimestamp() {
|
||||
this->last_timestamp = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void PlayTimeManager::Start() {
|
||||
UpdateTimestamp();
|
||||
play_time_thread =
|
||||
std::jthread([&](std::stop_token stop_token) { this->AutoTimestamp(stop_token); });
|
||||
}
|
||||
|
||||
void PlayTimeManager::Stop() {
|
||||
play_time_thread.request_stop();
|
||||
}
|
||||
|
||||
void PlayTimeManager::AutoTimestamp(std::stop_token stop_token) {
|
||||
Common::SetCurrentThreadName("PlayTimeReport");
|
||||
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
const auto duration = 30s;
|
||||
while (Common::StoppableTimedWait(stop_token, duration)) {
|
||||
Save();
|
||||
}
|
||||
|
||||
Save();
|
||||
}
|
||||
|
||||
void PlayTimeManager::Save() {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
const auto duration =
|
||||
static_cast<u64>(std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::steady_clock::duration(now - this->last_timestamp))
|
||||
.count());
|
||||
UpdateTimestamp();
|
||||
if (!UpdatePlayTime(running_program_id, duration)) {
|
||||
LOG_ERROR(Common, "Failed to update play time");
|
||||
}
|
||||
}
|
||||
|
||||
bool UpdatePlayTime(u64 program_id, u64 add_play_time) {
|
||||
std::vector<PlayTimeElement> play_time_elements;
|
||||
if (!ReadPlayTimeFile(play_time_elements)) {
|
||||
return false;
|
||||
}
|
||||
const auto it = std::find(play_time_elements.begin(), play_time_elements.end(), program_id);
|
||||
|
||||
if (it == play_time_elements.end()) {
|
||||
play_time_elements.push_back({.program_id = program_id, .play_time = add_play_time});
|
||||
} else {
|
||||
play_time_elements.at(it - play_time_elements.begin()).play_time += add_play_time;
|
||||
}
|
||||
if (!WritePlayTimeFile(play_time_elements)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
u64 GetPlayTime(u64 program_id) {
|
||||
std::vector<PlayTimeElement> play_time_elements;
|
||||
|
||||
if (!ReadPlayTimeFile(play_time_elements)) {
|
||||
return 0;
|
||||
}
|
||||
const auto it = std::find(play_time_elements.begin(), play_time_elements.end(), program_id);
|
||||
if (it == play_time_elements.end()) {
|
||||
return 0;
|
||||
}
|
||||
return play_time_elements.at(it - play_time_elements.begin()).play_time;
|
||||
}
|
||||
|
||||
bool PlayTimeManager::ResetProgramPlayTime(u64 program_id) {
|
||||
std::vector<PlayTimeElement> play_time_elements;
|
||||
|
||||
if (!ReadPlayTimeFile(play_time_elements)) {
|
||||
return false;
|
||||
}
|
||||
const auto it = std::find(play_time_elements.begin(), play_time_elements.end(), program_id);
|
||||
if (it == play_time_elements.end()) {
|
||||
return false;
|
||||
}
|
||||
play_time_elements.erase(it);
|
||||
if (!WritePlayTimeFile(play_time_elements)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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::GetYuzuPath(Common::FS::YuzuPath::PlayTimeDir) /
|
||||
uuid->RawString().append(".bin");
|
||||
}
|
||||
|
||||
[[nodiscard]] bool ReadPlayTimeFile(std::vector<PlayTimeElement>& out_play_time_elements) {
|
||||
const auto filename = GetCurrentUserPlayTimePath();
|
||||
if (!filename.has_value()) {
|
||||
LOG_ERROR(Common, "Failed to get current user path");
|
||||
return false;
|
||||
}
|
||||
|
||||
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(Common, "Failed to open play time file: {}",
|
||||
Common::FS::PathToUTF8String(filename.value()));
|
||||
return false;
|
||||
}
|
||||
const size_t elem_num = file.GetSize() / sizeof(PlayTimeElement);
|
||||
out_play_time_elements.resize(elem_num);
|
||||
const bool success = file.ReadSpan<PlayTimeElement>(out_play_time_elements) == elem_num;
|
||||
file.Close();
|
||||
return success;
|
||||
} else {
|
||||
out_play_time_elements.clear();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool WritePlayTimeFile(const std::vector<PlayTimeElement>& play_time_elements) {
|
||||
const auto filename = GetCurrentUserPlayTimePath();
|
||||
if (!filename.has_value()) {
|
||||
LOG_ERROR(Common, "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(Common, "Failed to open play time file: {}",
|
||||
Common::FS::PathToUTF8String(filename.value()));
|
||||
return false;
|
||||
}
|
||||
const bool success =
|
||||
file.WriteSpan<PlayTimeElement>(play_time_elements) == play_time_elements.size();
|
||||
file.Close();
|
||||
return success;
|
||||
}
|
||||
|
||||
QString ReadablePlayTime(qulonglong time_seconds) {
|
||||
static constexpr std::array units{"m", "h"};
|
||||
if (time_seconds == 0) {
|
||||
return QLatin1String("");
|
||||
}
|
||||
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 int unit = time_minutes < 60 ? 0 : 1;
|
||||
const auto value = unit == 0 ? time_minutes : time_hours;
|
||||
|
||||
return QStringLiteral("%L1 %2")
|
||||
.arg(value, 0, 'f', unit && time_seconds % 60 != 0)
|
||||
.arg(QString::fromUtf8(units[unit]));
|
||||
}
|
||||
|
||||
} // namespace PlayTime
|
@ -1,68 +0,0 @@
|
||||
// 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
|
179
src/yuzu/play_time_manager.cpp
Normal file
179
src/yuzu/play_time_manager.cpp
Normal file
@ -0,0 +1,179 @@
|
||||
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "common/alignment.h"
|
||||
#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::GetYuzuPath(Common::FS::YuzuPath::PlayTimeDir) /
|
||||
uuid->RawString().append(".bin");
|
||||
}
|
||||
|
||||
[[nodiscard]] bool ReadPlayTimeFile(PlayTimeDatabase& out_play_time_db) {
|
||||
const auto filename = GetCurrentUserPlayTimePath();
|
||||
|
||||
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 auto filename = GetCurrentUserPlayTimePath();
|
||||
|
||||
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() {
|
||||
if (!ReadPlayTimeFile(database)) {
|
||||
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)) {
|
||||
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
|
44
src/yuzu/play_time_manager.h
Normal file
44
src/yuzu/play_time_manager.h
Normal file
@ -0,0 +1,44 @@
|
||||
// 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"
|
||||
|
||||
namespace PlayTime {
|
||||
|
||||
using ProgramId = u64;
|
||||
using PlayTime = u64;
|
||||
using PlayTimeDatabase = std::map<ProgramId, PlayTime>;
|
||||
|
||||
class PlayTimeManager {
|
||||
public:
|
||||
explicit PlayTimeManager();
|
||||
~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:
|
||||
PlayTimeDatabase database;
|
||||
u64 running_program_id;
|
||||
std::jthread play_time_thread;
|
||||
void AutoTimestamp(std::stop_token stop_token);
|
||||
void Save();
|
||||
};
|
||||
|
||||
QString ReadablePlayTime(qulonglong time_seconds);
|
||||
|
||||
} // namespace PlayTime
|
Loading…
Reference in New Issue
Block a user