From 6bdf479488dd87a35bf5ccf9ec334f49bf43ffc7 Mon Sep 17 00:00:00 2001 From: inix Date: Fri, 17 Oct 2025 22:47:43 +0200 Subject: [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 Reviewed-by: Lizzie Reviewed-by: crueter Reviewed-by: MaranBr Co-authored-by: inix Co-committed-by: inix --- src/frontend_common/CMakeLists.txt | 2 + src/frontend_common/play_time_manager.cpp | 203 ++++++++++++++++++++++++++++++ src/frontend_common/play_time_manager.h | 56 +++++++++ 3 files changed, 261 insertions(+) create mode 100644 src/frontend_common/play_time_manager.cpp create mode 100644 src/frontend_common/play_time_manager.h (limited to 'src/frontend_common') diff --git a/src/frontend_common/CMakeLists.txt b/src/frontend_common/CMakeLists.txt index aa44740596..c9ce6f7ad4 100644 --- a/src/frontend_common/CMakeLists.txt +++ b/src/frontend_common/CMakeLists.txt @@ -11,6 +11,8 @@ add_library(frontend_common STATIC firmware_manager.h firmware_manager.cpp data_manager.h data_manager.cpp + play_time_manager.cpp + play_time_manager.h ) create_target_directory_groups(frontend_common) diff --git a/src/frontend_common/play_time_manager.cpp b/src/frontend_common/play_time_manager.cpp new file mode 100644 index 0000000000..782433392f --- /dev/null +++ b/src/frontend_common/play_time_manager.cpp @@ -0,0 +1,203 @@ +// 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 "play_time_manager.h" + +#include +#include + +namespace PlayTime { + +namespace { + +struct PlayTimeElement { + ProgramId program_id; + PlayTime play_time; +}; + +std::optional GetCurrentUserPlayTimePath() { + return Common::FS::GetEdenPath(Common::FS::EdenPath::PlayTimeDir) / + "playtime.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 elements(num_elements); + + if (file.ReadSpan(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 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(elements) == elements.size(); +} + +} // namespace + +PlayTimeManager::PlayTimeManager() + : running_program_id() { + 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(timestamp - last_timestamp); + return static_cast(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::SetPlayTime(u64 program_id, u64 play_time) { + database[program_id] = play_time; + Save(); +} + +void PlayTimeManager::ResetProgramPlayTime(u64 program_id) { + database.erase(program_id); + Save(); +} + +std::string PlayTimeManager::GetReadablePlayTime(u64 time_seconds) { + if (time_seconds == 0) { + return {}; + } + + const auto time_minutes = std::max(static_cast(time_seconds) / 60.0, 1.0); + const auto time_hours = static_cast(time_seconds) / 3600.0; + const bool is_minutes = time_minutes < 60.0; + + if (is_minutes) { + return fmt::format("{:.0f} m", time_minutes); + } else { + const bool has_remainder = time_seconds % 60 != 0; + if (has_remainder) { + return fmt::format("{:.1f} h", time_hours); + } else { + return fmt::format("{:.0f} h", time_hours); + } + } +} + +std::string PlayTimeManager::GetPlayTimeHours(u64 time_seconds) { + return fmt::format("{}", time_seconds / 3600); +} + +std::string PlayTimeManager::GetPlayTimeMinutes(u64 time_seconds) { + return fmt::format("{}", (time_seconds % 3600) / 60); +} + +std::string PlayTimeManager::GetPlayTimeSeconds(u64 time_seconds) { + return fmt::format("{}", time_seconds % 60); +} + +} // namespace PlayTime diff --git a/src/frontend_common/play_time_manager.h b/src/frontend_common/play_time_manager.h new file mode 100644 index 0000000000..a99ccebb1e --- /dev/null +++ b/src/frontend_common/play_time_manager.h @@ -0,0 +1,56 @@ +// 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 + +#include "common/common_funcs.h" +#include "common/common_types.h" +#include + +// TODO(crueter): Extract this into frontend_common +namespace Service::Account { +class ProfileManager; +} + +namespace PlayTime { + +using ProgramId = u64; +using PlayTime = u64; +using PlayTimeDatabase = std::map; + +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 SetPlayTime(u64 program_id, u64 play_time); + void Start(); + void Stop(); + + static std::string GetReadablePlayTime(u64 time_seconds); + static std::string GetPlayTimeHours(u64 time_seconds); + static std::string GetPlayTimeMinutes(u64 time_seconds); + static std::string GetPlayTimeSeconds(u64 time_seconds); + +private: + void AutoTimestamp(std::stop_token stop_token); + void Save(); + + PlayTimeDatabase database; + u64 running_program_id; + std::jthread play_time_thread; +}; + + +} // namespace PlayTime -- cgit v1.2.3