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
commit 7b07424885
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) GameListModel::GameListModel(QObject* parent) : QAbstractTableModel(parent)
{ {
connect(&m_tracker, &GameTracker::GameLoaded, this, &GameListModel::UpdateGame); connect(&m_tracker, &GameTracker::GameLoaded, this, &GameListModel::UpdateGame);
connect(&m_tracker, &GameTracker::GameRemoved, this, connect(&m_tracker, &GameTracker::GameRemoved, this, &GameListModel::RemoveGame);
[this](const QString& path) { RemoveGame(path.toStdString()); });
connect(&Settings::Instance(), &Settings::PathAdded, &m_tracker, &GameTracker::AddDirectory); connect(&Settings::Instance(), &Settings::PathAdded, &m_tracker, &GameTracker::AddDirectory);
connect(&Settings::Instance(), &Settings::PathRemoved, &m_tracker, &GameTracker::RemoveDirectory); connect(&Settings::Instance(), &Settings::PathRemoved, &m_tracker, &GameTracker::RemoveDirectory);
connect(&Settings::Instance(), &Settings::PathReloadRequested, &m_tracker, connect(&Settings::Instance(), &Settings::PathReloadRequested, &m_tracker,
@ -28,6 +27,8 @@ GameListModel::GameListModel(QObject* parent) : QAbstractTableModel(parent)
for (const QString& dir : Settings::Instance().GetPaths()) for (const QString& dir : Settings::Instance().GetPaths())
m_tracker.AddDirectory(dir); m_tracker.AddDirectory(dir);
m_tracker.Start();
connect(&Settings::Instance(), &Settings::ThemeChanged, [this] { connect(&Settings::Instance(), &Settings::ThemeChanged, [this] {
// Tell the view to repaint. The signal 'dataChanged' also seems like it would work here, but // 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. // 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) switch (command.type)
{ {
case CommandType::LoadCache: case CommandType::LoadCache:
m_cache.Load(); LoadCache();
break; break;
case CommandType::Start:
StartInternal();
case CommandType::AddDirectory: case CommandType::AddDirectory:
AddDirectoryInternal(command.path); AddDirectoryInternal(command.path);
break; break;
@ -52,6 +54,52 @@ GameTracker::GameTracker(QObject* parent) : QFileSystemWatcher(parent)
// TODO: When language changes, reload m_title_database and call m_cache.UpdateAdditionalMetadata // 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) void GameTracker::AddDirectory(const QString& dir)
{ {
m_load_thread.EmplaceItem(Command{CommandType::AddDirectory, dir}); m_load_thread.EmplaceItem(Command{CommandType::AddDirectory, dir});
@ -108,7 +156,8 @@ void GameTracker::RemoveDirectoryInternal(const QString& dir)
{ {
removePath(path); removePath(path);
m_tracked_files.remove(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()) if (tracked_file.empty())
{ {
m_tracked_files.remove(missing); 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()) if (QFileInfo(file).exists())
{ {
GameRemoved(file); if (m_started)
GameRemoved(file.toStdString());
addPath(file); addPath(file);
LoadGame(file); LoadGame(file);
} }
else if (removePath(file)) else if (removePath(file))
{ {
m_tracked_files.remove(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) void GameTracker::LoadGame(const QString& path)
{ {
if (!m_started)
return;
const std::string converted_path = path.toStdString(); const std::string converted_path = path.toStdString();
if (!DiscIO::ShouldHideFromGameList(converted_path)) if (!DiscIO::ShouldHideFromGameList(converted_path))
{ {

View File

@ -5,6 +5,8 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <mutex>
#include <string>
#include <QFileSystemWatcher> #include <QFileSystemWatcher>
#include <QMap> #include <QMap>
@ -26,15 +28,23 @@ class GameTracker final : public QFileSystemWatcher
public: public:
explicit GameTracker(QObject* parent = nullptr); 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 AddDirectory(const QString& dir);
void RemoveDirectory(const QString& dir); void RemoveDirectory(const QString& dir);
void ReloadDirectory(const QString& dir); void ReloadDirectory(const QString& dir);
signals: signals:
void GameLoaded(std::shared_ptr<const UICommon::GameFile> game); void GameLoaded(std::shared_ptr<const UICommon::GameFile> game);
void GameRemoved(const QString& path); void GameRemoved(const std::string& path);
private: private:
void LoadCache();
void StartInternal();
void UpdateDirectory(const QString& dir); void UpdateDirectory(const QString& dir);
void UpdateFile(const QString& path); void UpdateFile(const QString& path);
void AddDirectoryInternal(const QString& dir); void AddDirectoryInternal(const QString& dir);
@ -47,6 +57,7 @@ private:
enum class CommandType enum class CommandType
{ {
LoadCache, LoadCache,
Start,
AddDirectory, AddDirectory,
RemoveDirectory, RemoveDirectory,
UpdateDirectory, UpdateDirectory,
@ -64,6 +75,10 @@ private:
Common::WorkQueueThread<Command> m_load_thread; Common::WorkQueueThread<Command> m_load_thread;
UICommon::GameFileCache m_cache; UICommon::GameFileCache m_cache;
Core::TitleDatabase m_title_database; 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::shared_ptr<const UICommon::GameFile>)
Q_DECLARE_METATYPE(std::string)

View File

@ -73,7 +73,10 @@ std::shared_ptr<const GameFile> GameFileCache::AddOrGet(const std::string& path,
return result; return result;
} }
bool GameFileCache::Update(const std::vector<std::string>& all_game_paths) bool GameFileCache::Update(
const std::vector<std::string>& all_game_paths,
std::function<void(const std::shared_ptr<const GameFile>&)> game_added_to_cache,
std::function<void(const std::string&)> game_removed_from_cache)
{ {
// Copy game paths into a set, except ones that match DiscIO::ShouldHideFromGameList. // Copy game paths into a set, except ones that match DiscIO::ShouldHideFromGameList.
// TODO: Prevent DoFileSearch from looking inside /files/ directories of DirectoryBlobs at all? // TODO: Prevent DoFileSearch from looking inside /files/ directories of DirectoryBlobs at all?
@ -89,7 +92,7 @@ bool GameFileCache::Update(const std::vector<std::string>& all_game_paths)
bool cache_changed = false; bool cache_changed = false;
// Delete paths that aren't in game_paths from m_cached_files, // Delete paths that aren't in game_paths from m_cached_files,
// while simultaneously deleting paths that aren't in m_cached_files from game_paths. // while simultaneously deleting paths that are in m_cached_files from game_paths.
// For the sake of speed, we don't care about maintaining the order of m_cached_files. // For the sake of speed, we don't care about maintaining the order of m_cached_files.
{ {
auto it = m_cached_files.begin(); auto it = m_cached_files.begin();
@ -102,21 +105,27 @@ bool GameFileCache::Update(const std::vector<std::string>& all_game_paths)
} }
else else
{ {
if (game_removed_from_cache)
game_removed_from_cache((*it)->GetFilePath());
cache_changed = true; cache_changed = true;
--end; --end;
*it = std::move(*end); *it = std::move(*end);
m_cached_files.pop_back();
} }
} }
m_cached_files.erase(it, m_cached_files.end());
} }
// Now that the previous loop has run, game_paths only contains paths that // Now that the previous loop has run, game_paths only contains paths that
// aren't in m_cached_files, so we simply add all of them to m_cached_files. // aren't in m_cached_files, so we simply add all of them to m_cached_files.
for (const auto& path : game_paths) for (const std::string& path : game_paths)
{ {
auto file = std::make_shared<GameFile>(path); auto file = std::make_shared<GameFile>(path);
if (file->IsValid()) if (file->IsValid())
{ {
if (game_added_to_cache)
game_added_to_cache(file);
cache_changed = true; cache_changed = true;
m_cached_files.push_back(std::move(file)); m_cached_files.push_back(std::move(file));
} }
@ -125,12 +134,19 @@ bool GameFileCache::Update(const std::vector<std::string>& all_game_paths)
return cache_changed; return cache_changed;
} }
bool GameFileCache::UpdateAdditionalMetadata(const Core::TitleDatabase& title_database) bool GameFileCache::UpdateAdditionalMetadata(
const Core::TitleDatabase& title_database,
std::function<void(const std::shared_ptr<const GameFile>&)> game_updated)
{ {
bool cache_changed = false; bool cache_changed = false;
for (auto& file : m_cached_files) for (std::shared_ptr<GameFile>& file : m_cached_files)
cache_changed |= UpdateAdditionalMetadata(&file, title_database); {
const bool updated = UpdateAdditionalMetadata(&file, title_database);
cache_changed |= updated;
if (game_updated && updated)
game_updated(file);
}
return cache_changed; return cache_changed;
} }

View File

@ -40,8 +40,12 @@ public:
const Core::TitleDatabase& title_database); const Core::TitleDatabase& title_database);
// These functions return true if the call modified the cache. // These functions return true if the call modified the cache.
bool Update(const std::vector<std::string>& all_game_paths); bool Update(const std::vector<std::string>& all_game_paths,
bool UpdateAdditionalMetadata(const Core::TitleDatabase& title_database); std::function<void(const std::shared_ptr<const GameFile>&)> game_added_to_cache = {},
std::function<void(const std::string&)> game_removed_from_cache = {});
bool UpdateAdditionalMetadata(
const Core::TitleDatabase& title_database,
std::function<void(const std::shared_ptr<const GameFile>&)> game_updated = {});
bool Load(); bool Load();
bool Save(); bool Save();