1
0
Fork 0
mirror of https://github.com/dolphin-emu/dolphin.git synced 2025-03-06 21:00:21 +01:00

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.
This commit is contained in:
Aneesh Maganti 2025-02-10 15:28:57 -05:00 committed by Admiral H. Curtiss
parent 5acbdf730a
commit 803241c64b
No known key found for this signature in database
GPG key ID: F051B4C4044F33FB
4 changed files with 110 additions and 0 deletions

View file

@ -539,6 +539,8 @@ add_library(core
SysConf.h
System.cpp
System.h
TimePlayed.cpp
TimePlayed.h
TitleDatabase.cpp
TitleDatabase.h
WC24PatchEngine.cpp

View file

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

View file

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

View file

@ -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" />