From 803241c64bf7d227bf203d1ea89f46a8f864c08b Mon Sep 17 00:00:00 2001 From: Aneesh Maganti <28660350+aminoa@users.noreply.github.com> Date: Mon, 10 Feb 2025 15:28:57 -0500 Subject: [PATCH] Core: Add TimePlayed class to track game playtime Creates TimePlayed class and implemented constructors, AddTime, GetTimePlayed, and Reload methods. Updates CMakeLists.txt and DolphinLib.props as appropriate. --- Source/Core/Core/CMakeLists.txt | 2 + Source/Core/Core/TimePlayed.cpp | 66 +++++++++++++++++++++++++++++++++ Source/Core/Core/TimePlayed.h | 40 ++++++++++++++++++++ Source/Core/DolphinLib.props | 2 + 4 files changed, 110 insertions(+) create mode 100644 Source/Core/Core/TimePlayed.cpp create mode 100644 Source/Core/Core/TimePlayed.h diff --git a/Source/Core/Core/CMakeLists.txt b/Source/Core/Core/CMakeLists.txt index 33a97d05e7..b4437747e8 100644 --- a/Source/Core/Core/CMakeLists.txt +++ b/Source/Core/Core/CMakeLists.txt @@ -539,6 +539,8 @@ add_library(core SysConf.h System.cpp System.h + TimePlayed.cpp + TimePlayed.h TitleDatabase.cpp TitleDatabase.h WC24PatchEngine.cpp diff --git a/Source/Core/Core/TimePlayed.cpp b/Source/Core/Core/TimePlayed.cpp new file mode 100644 index 0000000000..e20a0c014e --- /dev/null +++ b/Source/Core/Core/TimePlayed.cpp @@ -0,0 +1,66 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "Core/TimePlayed.h" + +#include +#include + +#include "Common/CommonTypes.h" +#include "Common/FileUtil.h" +#include "Common/IniFile.h" +#include "Common/NandPaths.h" + +TimePlayed::TimePlayed() + : m_game_id(""), m_ini_path(File::GetUserPath(D_CONFIG_IDX) + "TimePlayed.ini") +{ + Reload(); +} + +TimePlayed::TimePlayed(std::string game_id) + : m_game_id(Common::EscapeFileName(game_id)), // filter for unsafe characters + m_ini_path(File::GetUserPath(D_CONFIG_IDX) + "TimePlayed.ini") +{ + Reload(); +} + +TimePlayed::~TimePlayed() = default; + +void TimePlayed::AddTime(std::chrono::milliseconds time_emulated) +{ + if (m_game_id == "") + { + return; + } + + u64 previous_time; + m_time_list->Get(m_game_id, &previous_time); + m_time_list->Set(m_game_id, previous_time + static_cast(time_emulated.count())); + m_ini.Save(m_ini_path); +} + +std::chrono::milliseconds TimePlayed::GetTimePlayed() const +{ + if (m_game_id == "") + { + return std::chrono::milliseconds(0); + } + + u64 previous_time; + m_time_list->Get(m_game_id, &previous_time); + return std::chrono::milliseconds(previous_time); +} + +std::chrono::milliseconds TimePlayed::GetTimePlayed(std::string game_id) const +{ + std::string filtered_game_id = Common::EscapeFileName(game_id); + u64 previous_time; + m_time_list->Get(filtered_game_id, &previous_time); + return std::chrono::milliseconds(previous_time); +} + +void TimePlayed::Reload() +{ + 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 new file mode 100644 index 0000000000..306438b778 --- /dev/null +++ b/Source/Core/Core/TimePlayed.h @@ -0,0 +1,40 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +#include "Common/CommonTypes.h" +#include "Common/IniFile.h" + +class TimePlayed +{ +public: + // used for QT interface - general access to time played for games + TimePlayed(); + + TimePlayed(std::string game_id); + + // 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; + + ~TimePlayed(); + + void AddTime(std::chrono::milliseconds time_emulated); + + std::chrono::milliseconds GetTimePlayed() const; + std::chrono::milliseconds GetTimePlayed(std::string game_id) const; + + void Reload(); + +private: + std::string m_game_id; + std::string m_ini_path; + Common::IniFile m_ini; + Common::IniFile::Section* m_time_list; +}; diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index e41e9fc26a..367c347213 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -459,6 +459,7 @@ + @@ -1125,6 +1126,7 @@ +