| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <atomic> |
| 7 |
#include <memory> |
| 8 |
#include <string> |
| 9 |
|
| 10 |
#include <QFileSystemWatcher> |
| 11 |
#include <QMap> |
| 12 |
#include <QSet> |
| 13 |
#include <QString> |
| 14 |
#include <QVector> |
| 15 |
|
| 16 |
#include "Common/Event.h" |
| 17 |
#include "Common/WorkQueueThread.h" |
| 18 |
#include "UICommon/GameFileCache.h" |
| 19 |
|
| 20 |
namespace UICommon |
| 21 |
{ |
| 22 |
class GameFile; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class GameTracker final : public QFileSystemWatcher |
| 29 |
{ |
| 30 |
Q_OBJECT |
| 31 |
|
| 32 |
public: |
| 33 |
explicit GameTracker(QObject* parent = nullptr); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
void Start(); |
| 40 |
|
| 41 |
void AddDirectory(const QString& dir); |
| 42 |
void RemoveDirectory(const QString& dir); |
| 43 |
void RefreshAll(); |
| 44 |
void PurgeCache(); |
| 45 |
|
| 46 |
signals: |
| 47 |
void GameLoaded(const std::shared_ptr<const UICommon::GameFile>& game); |
| 48 |
void GameUpdated(const std::shared_ptr<const UICommon::GameFile>& game); |
| 49 |
void GameRemoved(const std::string& path); |
| 50 |
|
| 51 |
private: |
| 52 |
void LoadCache(); |
| 53 |
void StartInternal(); |
| 54 |
void UpdateDirectory(const QString& dir); |
| 55 |
void UpdateFile(const QString& path); |
| 56 |
void AddDirectoryInternal(const QString& dir); |
| 57 |
void RemoveDirectoryInternal(const QString& dir); |
| 58 |
void UpdateDirectoryInternal(const QString& dir); |
| 59 |
void UpdateFileInternal(const QString& path); |
| 60 |
QSet<QString> FindMissingFiles(const QString& dir); |
| 61 |
void LoadGame(const QString& path); |
| 62 |
|
| 63 |
bool AddPath(const QString& path); |
| 64 |
bool RemovePath(const QString& path); |
| 65 |
|
| 66 |
enum class CommandType |
| 67 |
{ |
| 68 |
LoadCache, |
| 69 |
Start, |
| 70 |
AddDirectory, |
| 71 |
RemoveDirectory, |
| 72 |
UpdateDirectory, |
| 73 |
UpdateFile, |
| 74 |
UpdateMetadata, |
| 75 |
ResumeProcessing, |
| 76 |
PurgeCache, |
| 77 |
BeginRefresh, |
| 78 |
EndRefresh, |
| 79 |
}; |
| 80 |
|
| 81 |
struct Command |
| 82 |
{ |
| 83 |
CommandType type; |
| 84 |
QString path; |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
QMap<QString, QSet<QString>> m_tracked_files; |
| 89 |
QVector<QString> m_tracked_paths; |
| 90 |
Common::WorkQueueThread<Command> m_load_thread; |
| 91 |
UICommon::GameFileCache m_cache; |
| 92 |
Common::Event m_cache_loaded_event; |
| 93 |
Common::Event m_initial_games_emitted_event; |
| 94 |
bool m_initial_games_emitted = false; |
| 95 |
bool m_started = false; |
| 96 |
bool m_needs_purge = false; |
| 97 |
bool m_refresh_in_progress = false; |
| 98 |
std::atomic_bool m_processing_halted = false; |
| 99 |
}; |
| 100 |
|
| 101 |
Q_DECLARE_METATYPE(std::shared_ptr<const UICommon::GameFile>) |
| 102 |
Q_DECLARE_METATYPE(std::string) |