11 changed files with 259 additions and 270 deletions
-
4src/yuzu/CMakeLists.txt
-
8src/yuzu/game_list.cpp
-
5src/yuzu/game_list.h
-
2src/yuzu/game_list_p.h
-
23src/yuzu/game_list_worker.cpp
-
6src/yuzu/game_list_worker.h
-
11src/yuzu/main.cpp
-
177src/yuzu/play_time.cpp
-
68src/yuzu/play_time.h
-
179src/yuzu/play_time_manager.cpp
-
44src/yuzu/play_time_manager.h
@ -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 |
|||
@ -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
|
|||
@ -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 |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue