Merge pull request #6975 from JosJuice/qt-fast-gamelist

DolphinQt2: Show cached games before checking whether they exist on disk
This commit is contained in:
spycrab
2018-05-27 04:31:32 +02:00
committed by GitHub
5 changed files with 108 additions and 17 deletions

View File

@ -18,8 +18,7 @@ const QSize GAMECUBE_BANNER_SIZE(96, 32);
GameListModel::GameListModel(QObject* parent) : QAbstractTableModel(parent)
{
connect(&m_tracker, &GameTracker::GameLoaded, this, &GameListModel::UpdateGame);
connect(&m_tracker, &GameTracker::GameRemoved, this,
[this](const QString& path) { RemoveGame(path.toStdString()); });
connect(&m_tracker, &GameTracker::GameRemoved, this, &GameListModel::RemoveGame);
connect(&Settings::Instance(), &Settings::PathAdded, &m_tracker, &GameTracker::AddDirectory);
connect(&Settings::Instance(), &Settings::PathRemoved, &m_tracker, &GameTracker::RemoveDirectory);
connect(&Settings::Instance(), &Settings::PathReloadRequested, &m_tracker,
@ -28,6 +27,8 @@ GameListModel::GameListModel(QObject* parent) : QAbstractTableModel(parent)
for (const QString& dir : Settings::Instance().GetPaths())
m_tracker.AddDirectory(dir);
m_tracker.Start();
connect(&Settings::Instance(), &Settings::ThemeChanged, [this] {
// Tell the view to repaint. The signal 'dataChanged' also seems like it would work here, but
// unfortunately it won't cause a repaint until the view is focused.

View File

@ -30,8 +30,10 @@ GameTracker::GameTracker(QObject* parent) : QFileSystemWatcher(parent)
switch (command.type)
{
case CommandType::LoadCache:
m_cache.Load();
LoadCache();
break;
case CommandType::Start:
StartInternal();
case CommandType::AddDirectory:
AddDirectoryInternal(command.path);
break;
@ -52,6 +54,52 @@ GameTracker::GameTracker(QObject* parent) : QFileSystemWatcher(parent)
// TODO: When language changes, reload m_title_database and call m_cache.UpdateAdditionalMetadata
}
void GameTracker::LoadCache()
{
std::lock_guard<std::mutex> lk(m_mutex);
m_cache.Load();
}
void GameTracker::Start()
{
if (m_initial_games_emitted)
return;
m_initial_games_emitted = true;
std::lock_guard<std::mutex> lk(m_mutex);
m_load_thread.EmplaceItem(Command{CommandType::Start, {}});
m_cache.ForEach(
[this](const std::shared_ptr<const UICommon::GameFile>& game) { emit GameLoaded(game); });
}
void GameTracker::StartInternal()
{
if (m_started)
return;
m_started = true;
std::vector<std::string> paths;
paths.reserve(m_tracked_files.size());
for (const QString& path : m_tracked_files.keys())
paths.push_back(path.toStdString());
auto emit_game_loaded = [this](const std::shared_ptr<const UICommon::GameFile>& game) {
emit GameLoaded(std::move(game));
};
auto emit_game_removed = [this](const std::string& path) { emit GameRemoved(path); };
std::lock_guard<std::mutex> lk(m_mutex);
bool cache_updated = m_cache.Update(paths, emit_game_loaded, emit_game_removed);
cache_updated |= m_cache.UpdateAdditionalMetadata(m_title_database, emit_game_loaded);
if (cache_updated)
m_cache.Save();
}
void GameTracker::AddDirectory(const QString& dir)
{
m_load_thread.EmplaceItem(Command{CommandType::AddDirectory, dir});
@ -108,7 +156,8 @@ void GameTracker::RemoveDirectoryInternal(const QString& dir)
{
removePath(path);
m_tracked_files.remove(path);
emit GameRemoved(path);
if (m_started)
emit GameRemoved(path.toStdString());
}
}
}
@ -143,7 +192,8 @@ void GameTracker::UpdateDirectoryInternal(const QString& dir)
if (tracked_file.empty())
{
m_tracked_files.remove(missing);
GameRemoved(missing);
if (m_started)
GameRemoved(missing.toStdString());
}
}
}
@ -152,14 +202,16 @@ void GameTracker::UpdateFileInternal(const QString& file)
{
if (QFileInfo(file).exists())
{
GameRemoved(file);
if (m_started)
GameRemoved(file.toStdString());
addPath(file);
LoadGame(file);
}
else if (removePath(file))
{
m_tracked_files.remove(file);
emit GameRemoved(file);
if (m_started)
emit GameRemoved(file.toStdString());
}
}
@ -187,6 +239,9 @@ QSet<QString> GameTracker::FindMissingFiles(const QString& dir)
void GameTracker::LoadGame(const QString& path)
{
if (!m_started)
return;
const std::string converted_path = path.toStdString();
if (!DiscIO::ShouldHideFromGameList(converted_path))
{

View File

@ -5,6 +5,8 @@
#pragma once
#include <memory>
#include <mutex>
#include <string>
#include <QFileSystemWatcher>
#include <QMap>
@ -26,15 +28,23 @@ class GameTracker final : public QFileSystemWatcher
public:
explicit GameTracker(QObject* parent = nullptr);
// A GameTracker won't emit any signals until this function has been called.
// Before you call this function, make sure to call AddDirectory for all
// directories you want to track, otherwise games will briefly disappear
// until you call AddDirectory and the GameTracker finishes scanning the file system.
void Start();
void AddDirectory(const QString& dir);
void RemoveDirectory(const QString& dir);
void ReloadDirectory(const QString& dir);
signals:
void GameLoaded(std::shared_ptr<const UICommon::GameFile> game);
void GameRemoved(const QString& path);
void GameRemoved(const std::string& path);
private:
void LoadCache();
void StartInternal();
void UpdateDirectory(const QString& dir);
void UpdateFile(const QString& path);
void AddDirectoryInternal(const QString& dir);
@ -47,6 +57,7 @@ private:
enum class CommandType
{
LoadCache,
Start,
AddDirectory,
RemoveDirectory,
UpdateDirectory,
@ -64,6 +75,10 @@ private:
Common::WorkQueueThread<Command> m_load_thread;
UICommon::GameFileCache m_cache;
Core::TitleDatabase m_title_database;
std::mutex m_mutex;
bool m_started = false;
bool m_initial_games_emitted = false;
};
Q_DECLARE_METATYPE(std::shared_ptr<const UICommon::GameFile>)
Q_DECLARE_METATYPE(std::string)