Rename TimePlayed to TimePlayedManager and make singleton

Also add some locks and rename some variables appropriately.
This commit is contained in:
Dentomologist
2025-03-15 12:23:17 -07:00
parent 2a771937cf
commit ca9045c19b
6 changed files with 39 additions and 21 deletions

View File

@@ -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();
}

View File

@@ -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<std::chrono::milliseconds>(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)
{

View File

@@ -4,6 +4,7 @@
#include "Core/TimePlayed.h"
#include <chrono>
#include <mutex>
#include <string>
#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<u64>(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");
}

View File

@@ -4,23 +4,23 @@
#pragma once
#include <chrono>
#include <mutex>
#include <string>
#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;
};

View File

@@ -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<std::chrono::minutes>(total_time);
const auto total_hours = std::chrono::duration_cast<std::chrono::hours>(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<qlonglong>(m_timer.GetTimePlayed(game_id).count());
return static_cast<qlonglong>(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();
}
}

View File

@@ -98,7 +98,7 @@ private:
GameTracker m_tracker;
QList<std::shared_ptr<const UICommon::GameFile>> m_games;
Core::TitleDatabase m_title_database;
TimePlayed m_timer;
TimePlayedManager& m_time_played_manager;
QString m_term;
float m_scale = 1.0;
};