From ca9045c19bc1eafc3b945944153a0ff555172442 Mon Sep 17 00:00:00 2001 From: Dentomologist Date: Sat, 15 Mar 2025 12:23:17 -0700 Subject: [PATCH] Rename TimePlayed to TimePlayedManager and make singleton Also add some locks and rename some variables appropriately. --- Source/Android/jni/GameList/GameFile.cpp | 3 ++- Source/Core/Core/HW/CPU.cpp | 4 ++-- Source/Core/Core/TimePlayed.cpp | 23 +++++++++++++++---- Source/Core/Core/TimePlayed.h | 19 ++++++++------- .../Core/DolphinQt/GameList/GameListModel.cpp | 9 ++++---- .../Core/DolphinQt/GameList/GameListModel.h | 2 +- 6 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Source/Android/jni/GameList/GameFile.cpp b/Source/Android/jni/GameList/GameFile.cpp index dc922d701c..d8a8e625c2 100644 --- a/Source/Android/jni/GameList/GameFile.cpp +++ b/Source/Android/jni/GameList/GameFile.cpp @@ -195,7 +195,8 @@ JNIEXPORT jint JNICALL Java_org_dolphinemu_dolphinemu_model_GameFile_getBannerHe JNIEXPORT jlong JNICALL Java_org_dolphinemu_dolphinemu_model_GameFile_getTimePlayedMsInternal(JNIEnv* env, jobject obj) { - const std::chrono::milliseconds time = TimePlayed().GetTimePlayed(GetRef(env, obj)->GetGameID()); + const std::chrono::milliseconds time = + TimePlayedManager::GetInstance().GetTimePlayed(GetRef(env, obj)->GetGameID()); return time.count(); } diff --git a/Source/Core/Core/HW/CPU.cpp b/Source/Core/Core/HW/CPU.cpp index 558b2098ab..0dbfdfeb05 100644 --- a/Source/Core/Core/HW/CPU.cpp +++ b/Source/Core/Core/HW/CPU.cpp @@ -78,7 +78,7 @@ void CPUManager::StartTimePlayedTimer() while (true) { - TimePlayed time_played; + auto& time_played_manager = TimePlayedManager::GetInstance(); auto curr_time = timer.now(); // Check that emulation is not paused @@ -88,7 +88,7 @@ void CPUManager::StartTimePlayedTimer() const std::string game_id = SConfig::GetInstance().GetGameID(); const auto diff_time = std::chrono::duration_cast(curr_time - prev_time); - time_played.AddTime(game_id, diff_time); + time_played_manager.AddTime(game_id, diff_time); } else if (m_state == State::Stepping) { diff --git a/Source/Core/Core/TimePlayed.cpp b/Source/Core/Core/TimePlayed.cpp index 9323a49393..00e72f4e70 100644 --- a/Source/Core/Core/TimePlayed.cpp +++ b/Source/Core/Core/TimePlayed.cpp @@ -4,6 +4,7 @@ #include "Core/TimePlayed.h" #include +#include #include #include "Common/CommonTypes.h" @@ -11,32 +12,44 @@ #include "Common/IniFile.h" #include "Common/NandPaths.h" -TimePlayed::TimePlayed() : m_ini_path(File::GetUserPath(D_CONFIG_IDX) + "TimePlayed.ini") +TimePlayedManager::TimePlayedManager() + : m_ini_path(File::GetUserPath(D_CONFIG_IDX) + "TimePlayed.ini") { Reload(); } -TimePlayed::~TimePlayed() = default; +TimePlayedManager::~TimePlayedManager() = default; -void TimePlayed::AddTime(const std::string& game_id, std::chrono::milliseconds time_emulated) +TimePlayedManager& TimePlayedManager::GetInstance() +{ + static TimePlayedManager time_played_manager; + return time_played_manager; +} + +void TimePlayedManager::AddTime(const std::string& game_id, std::chrono::milliseconds time_emulated) { std::string filtered_game_id = Common::EscapeFileName(game_id); u64 previous_time; + + std::lock_guard guard(m_mutex); m_time_list->Get(filtered_game_id, &previous_time); m_time_list->Set(filtered_game_id, previous_time + static_cast(time_emulated.count())); m_ini.Save(m_ini_path); } -std::chrono::milliseconds TimePlayed::GetTimePlayed(const std::string& game_id) const +std::chrono::milliseconds TimePlayedManager::GetTimePlayed(const std::string& game_id) const { std::string filtered_game_id = Common::EscapeFileName(game_id); u64 previous_time; + + std::lock_guard guard(m_mutex); m_time_list->Get(filtered_game_id, &previous_time); return std::chrono::milliseconds(previous_time); } -void TimePlayed::Reload() +void TimePlayedManager::Reload() { + std::lock_guard guard(m_mutex); m_ini.Load(m_ini_path); m_time_list = m_ini.GetOrCreateSection("TimePlayed"); } diff --git a/Source/Core/Core/TimePlayed.h b/Source/Core/Core/TimePlayed.h index 5d1f279db3..6adfc56348 100644 --- a/Source/Core/Core/TimePlayed.h +++ b/Source/Core/Core/TimePlayed.h @@ -4,23 +4,23 @@ #pragma once #include +#include #include #include "Common/CommonTypes.h" #include "Common/IniFile.h" -class TimePlayed +class TimePlayedManager { public: - TimePlayed(); + TimePlayedManager(const TimePlayedManager& other) = delete; + TimePlayedManager(TimePlayedManager&& other) = delete; + TimePlayedManager& operator=(const TimePlayedManager& other) = delete; + TimePlayedManager& operator=(TimePlayedManager&& other) = delete; - // not copyable due to the stored section pointer - TimePlayed(const TimePlayed& other) = delete; - TimePlayed(TimePlayed&& other) = delete; - TimePlayed& operator=(const TimePlayed& other) = delete; - TimePlayed& operator=(TimePlayed&& other) = delete; + ~TimePlayedManager(); - ~TimePlayed(); + static TimePlayedManager& GetInstance(); void AddTime(const std::string& game_id, std::chrono::milliseconds time_emulated); @@ -29,7 +29,10 @@ public: void Reload(); private: + TimePlayedManager(); + std::string m_ini_path; + mutable std::mutex m_mutex; Common::IniFile m_ini; Common::IniFile::Section* m_time_list; }; diff --git a/Source/Core/DolphinQt/GameList/GameListModel.cpp b/Source/Core/DolphinQt/GameList/GameListModel.cpp index 34b8c8570f..93edcef63f 100644 --- a/Source/Core/DolphinQt/GameList/GameListModel.cpp +++ b/Source/Core/DolphinQt/GameList/GameListModel.cpp @@ -23,7 +23,8 @@ const QSize GAMECUBE_BANNER_SIZE(96, 32); -GameListModel::GameListModel(QObject* parent) : QAbstractTableModel(parent) +GameListModel::GameListModel(QObject* parent) + : QAbstractTableModel(parent), m_time_played_manager(TimePlayedManager::GetInstance()) { connect(&m_tracker, &GameTracker::GameLoaded, this, &GameListModel::AddGame); connect(&m_tracker, &GameTracker::GameUpdated, this, &GameListModel::UpdateGame); @@ -195,7 +196,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const if (role == Qt::DisplayRole) { const std::string game_id = game.GetGameID(); - const std::chrono::milliseconds total_time = m_timer.GetTimePlayed(game_id); + const std::chrono::milliseconds total_time = m_time_played_manager.GetTimePlayed(game_id); const auto total_minutes = std::chrono::duration_cast(total_time); const auto total_hours = std::chrono::duration_cast(total_time); @@ -207,7 +208,7 @@ QVariant GameListModel::data(const QModelIndex& index, int role) const if (role == SORT_ROLE) { const std::string game_id = game.GetGameID(); - return static_cast(m_timer.GetTimePlayed(game_id).count()); + return static_cast(m_time_played_manager.GetTimePlayed(game_id).count()); } break; case Column::Tags: @@ -526,6 +527,6 @@ void GameListModel::OnEmulationStateChanged(Core::State state) { if (state == Core::State::Uninitialized) { - m_timer.Reload(); + m_time_played_manager.Reload(); } } diff --git a/Source/Core/DolphinQt/GameList/GameListModel.h b/Source/Core/DolphinQt/GameList/GameListModel.h index baac5d507d..f513a1dde1 100644 --- a/Source/Core/DolphinQt/GameList/GameListModel.h +++ b/Source/Core/DolphinQt/GameList/GameListModel.h @@ -98,7 +98,7 @@ private: GameTracker m_tracker; QList> m_games; Core::TitleDatabase m_title_database; - TimePlayed m_timer; + TimePlayedManager& m_time_played_manager; QString m_term; float m_scale = 1.0; };