Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pcsx2-qt/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,8 @@ void MainWindow::onGameListEntryContextMenuRequested(const QPoint& point)
// Best two options given zero play time are to grey this out or to not show it at all.
if (entry_played_time)
connect(menu.addAction(tr("Reset Play Time")), &QAction::triggered, [this, entry, entry_played_time]() { clearGameListEntryPlayTime(*entry, entry_played_time); });
// Show this option whether play time exists or not.
connect(menu.addAction(tr("Edit Play Time")), &QAction::triggered, [this, entry, entry_played_time]() { editGameListEntryPlayTime(*entry, entry_played_time); });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this before "Reset Play Time"


// Check Wiki Page functionality is based on a serial redirect.
if (!entry->serial.empty())
Expand Down Expand Up @@ -3082,6 +3084,46 @@ void MainWindow::clearGameListEntryPlayTime(const GameList::Entry& entry, const
}
}

void MainWindow::editGameListEntryPlayTime(const GameList::Entry& entry, const time_t entry_played_time)
{
const u32 hours = static_cast<u32>(entry_played_time / 3600);
const u32 minutes = static_cast<u32>((entry_played_time % 3600) / 60);
const u32 seconds = static_cast<u32>((entry_played_time % 3600) % 60);

bool ok;
int new_hours = QInputDialog::getInt(this, tr("Edit Play Time For %1").arg(entry.title.empty() ? tr("empty title") : QString::fromStdString(entry.title)),
tr("Hours (Min: 0, Max: 99999):"), hours, 0, 99999, 1, &ok);

if (ok)
{
ok = NULL;
int new_minutes = QInputDialog::getInt(this, tr("Edit Play Time For %1").arg(entry.title.empty() ? tr("empty title") : QString::fromStdString(entry.title)),
tr("Minutes (Min: 0, Max: 59):"), minutes, 0, 59, 1, &ok);

if (ok)
{
ok = NULL;
int new_seconds = QInputDialog::getInt(this, tr("Edit Play Time For %1").arg(entry.title.empty() ? tr("empty title") : QString::fromStdString(entry.title)),
tr("Seconds (Min: 0, Max: 59):"), seconds, 0, 59, 1, &ok);

if (ok)
{
time_t new_time = (new_hours * 3600) + (new_minutes * 60) + new_seconds;

if (new_time == 0)
{
GameList::ClearPlayedTimeForSerial(entry.serial);
}
else
{
GameList::UpdatePlayedTimeForSerial(entry.serial, new_time);
m_game_list_widget->refresh(false, false);
}
}
}
}
}

void MainWindow::goToWikiPage(const GameList::Entry& entry)
{
QtUtils::OpenURL(this, fmt::format("https://wiki.pcsx2.net/{}", entry.serial).c_str());
Expand Down
1 change: 1 addition & 0 deletions pcsx2-qt/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ private Q_SLOTS:
const GameList::Entry& entry, std::optional<s32> save_slot = std::nullopt, std::optional<bool> fast_boot = std::nullopt, bool load_backup = false);
void setGameListEntryCoverImage(const GameList::Entry& entry);
void clearGameListEntryPlayTime(const GameList::Entry& entry, const time_t entry_played_time);
void editGameListEntryPlayTime(const GameList::Entry& entry, const time_t entry_played_time);
void goToWikiPage(const GameList::Entry& entry);
void openSnapshotsFolderForGame(const GameList::Entry& entry);

Expand Down
35 changes: 35 additions & 0 deletions pcsx2/GameList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,41 @@ void GameList::ClearPlayedTimeForSerial(const std::string& serial)
}
}

void GameList::UpdatePlayedTimeForSerial(const std::string& serial, std::time_t new_time)
{
if (serial.empty())
return;

std::time_t current_last = 0;
std::time_t current_time = 0;

std::unique_lock<std::recursive_mutex> lock(s_mutex);

for (const GameList::Entry& entry : s_entries)
{
if (entry.serial != serial)
continue;

current_last = entry.last_played_time;
current_time = entry.total_played_time;
}

// Allow updating time played, even if last played is zero (due to how UpdatePlayedTimeFile() works).
if (current_last == 0)
current_last = std::time(nullptr);

// Weird solution (i.e., to add the difference) to use the same function to update the file, but it works!
const PlayedTimeEntry pt(UpdatePlayedTimeFile(GetPlayedTimeFile(), serial, current_last, (new_time - current_time)));

for (GameList::Entry& entry : s_entries)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to iterate the game list twice? can you not do the update in a single pass?

{
if (entry.serial != serial)
continue;

entry.last_played_time = pt.last_played_time;
entry.total_played_time = pt.total_played_time;
}
}

std::time_t GameList::GetCachedPlayedTimeForSerial(const std::string& serial)
{
Expand Down
1 change: 1 addition & 0 deletions pcsx2/GameList.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ namespace GameList
/// Add played time for the specified serial.
void AddPlayedTimeForSerial(const std::string& serial, std::time_t last_time, std::time_t add_time);
void ClearPlayedTimeForSerial(const std::string& serial);
void UpdatePlayedTimeForSerial(const std::string& serial, std::time_t new_time);

/// Returns the total time played for a game. Requires the game to be scanned in the list.
std::time_t GetCachedPlayedTimeForSerial(const std::string& serial);
Expand Down