summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAneesh Maganti <28660350+aminoa@users.noreply.github.com>2025-02-10 15:28:57 -0500
committerAdmiral H. Curtiss <pikachu025@gmail.com>2025-02-16 23:15:11 +0100
commit803241c64bf7d227bf203d1ea89f46a8f864c08b (patch)
tree8806fb24cc97adc53d251d89f02dff458836ea35
parent5acbdf730a44d00b46528833334f82f44a6a6e67 (diff)
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.
-rw-r--r--Source/Core/Core/CMakeLists.txt2
-rw-r--r--Source/Core/Core/TimePlayed.cpp66
-rw-r--r--Source/Core/Core/TimePlayed.h40
-rw-r--r--Source/Core/DolphinLib.props2
4 files changed, 110 insertions, 0 deletions
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 <chrono>
+#include <string>
+
+#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<u64>(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 <chrono>
+#include <string>
+
+#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 @@
<ClInclude Include="Core\SyncIdentifier.h" />
<ClInclude Include="Core\SysConf.h" />
<ClInclude Include="Core\System.h" />
+ <ClInclude Include="Core\TimePlayed.h" />
<ClInclude Include="Core\TitleDatabase.h" />
<ClInclude Include="Core\WC24PatchEngine.h" />
<ClInclude Include="Core\WiiRoot.h" />
@@ -1125,6 +1126,7 @@
<ClCompile Include="Core\State.cpp" />
<ClCompile Include="Core\SysConf.cpp" />
<ClCompile Include="Core\System.cpp" />
+ <ClCompile Include="Core\TimePlayed.cpp" />
<ClCompile Include="Core\TitleDatabase.cpp" />
<ClCompile Include="Core\WiiRoot.cpp" />
<ClCompile Include="Core\WiiUtils.cpp" />