-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
GameTracker.cpp
197 lines (171 loc) · 5.1 KB
/
GameTracker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include "Core/ConfigManager.h"
#include "DiscIO/DirectoryBlob.h"
#include "DolphinQt2/GameList/GameTracker.h"
#include "DolphinQt2/QtUtils/QueueOnObject.h"
#include "DolphinQt2/Settings.h"
static const QStringList game_filters{
QStringLiteral("*.gcm"), QStringLiteral("*.iso"), QStringLiteral("*.tgc"),
QStringLiteral("*.ciso"), QStringLiteral("*.gcz"), QStringLiteral("*.wbfs"),
QStringLiteral("*.wad"), QStringLiteral("*.elf"), QStringLiteral("*.dol")};
GameTracker::GameTracker(QObject* parent) : QFileSystemWatcher(parent)
{
qRegisterMetaType<std::shared_ptr<const UICommon::GameFile>>();
connect(this, &QFileSystemWatcher::directoryChanged, this, &GameTracker::UpdateDirectory);
connect(this, &QFileSystemWatcher::fileChanged, this, &GameTracker::UpdateFile);
m_load_thread.Reset([this](Command command) {
switch (command.type)
{
case CommandType::LoadCache:
m_cache.Load();
break;
case CommandType::AddDirectory:
AddDirectoryInternal(command.path);
break;
case CommandType::RemoveDirectory:
RemoveDirectoryInternal(command.path);
break;
case CommandType::UpdateDirectory:
UpdateDirectoryInternal(command.path);
break;
case CommandType::UpdateFile:
UpdateFileInternal(command.path);
break;
}
});
m_load_thread.EmplaceItem(Command{CommandType::LoadCache, {}});
// TODO: When language changes, reload m_title_database and call m_cache.UpdateAdditionalMetadata
}
void GameTracker::AddDirectory(const QString& dir)
{
m_load_thread.EmplaceItem(Command{CommandType::AddDirectory, dir});
}
void GameTracker::RemoveDirectory(const QString& dir)
{
m_load_thread.EmplaceItem(Command{CommandType::RemoveDirectory, dir});
}
void GameTracker::ReloadDirectory(const QString& dir)
{
m_load_thread.EmplaceItem(Command{CommandType::RemoveDirectory, dir});
m_load_thread.EmplaceItem(Command{CommandType::AddDirectory, dir});
}
void GameTracker::UpdateDirectory(const QString& dir)
{
m_load_thread.EmplaceItem(Command{CommandType::UpdateDirectory, dir});
}
void GameTracker::UpdateFile(const QString& dir)
{
m_load_thread.EmplaceItem(Command{CommandType::UpdateFile, dir});
}
void GameTracker::AddDirectoryInternal(const QString& dir)
{
if (!QFileInfo(dir).exists())
return;
addPath(dir);
UpdateDirectoryInternal(dir);
}
static std::unique_ptr<QDirIterator> GetIterator(const QString& dir)
{
return std::make_unique<QDirIterator>(dir, game_filters, QDir::NoFilter,
SConfig::GetInstance().m_RecursiveISOFolder ?
QDirIterator::Subdirectories :
QDirIterator::NoIteratorFlags);
}
void GameTracker::RemoveDirectoryInternal(const QString& dir)
{
removePath(dir);
auto it = GetIterator(dir);
while (it->hasNext())
{
QString path = QFileInfo(it->next()).canonicalFilePath();
if (m_tracked_files.contains(path))
{
m_tracked_files[path].remove(dir);
if (m_tracked_files[path].empty())
{
removePath(path);
m_tracked_files.remove(path);
emit GameRemoved(path);
}
}
}
}
void GameTracker::UpdateDirectoryInternal(const QString& dir)
{
auto it = GetIterator(dir);
while (it->hasNext())
{
QString path = QFileInfo(it->next()).canonicalFilePath();
if (m_tracked_files.contains(path))
{
auto& tracked_file = m_tracked_files[path];
if (!tracked_file.contains(dir))
tracked_file.insert(dir);
}
else
{
addPath(path);
m_tracked_files[path] = QSet<QString>{dir};
LoadGame(path);
}
}
for (const auto& missing : FindMissingFiles(dir))
{
auto& tracked_file = m_tracked_files[missing];
tracked_file.remove(dir);
if (tracked_file.empty())
{
m_tracked_files.remove(missing);
GameRemoved(missing);
}
}
}
void GameTracker::UpdateFileInternal(const QString& file)
{
if (QFileInfo(file).exists())
{
GameRemoved(file);
addPath(file);
LoadGame(file);
}
else if (removePath(file))
{
m_tracked_files.remove(file);
emit GameRemoved(file);
}
}
QSet<QString> GameTracker::FindMissingFiles(const QString& dir)
{
auto it = GetIterator(dir);
QSet<QString> missing_files;
for (const auto& key : m_tracked_files.keys())
{
if (m_tracked_files[key].contains(dir))
missing_files.insert(key);
}
while (it->hasNext())
{
QString path = QFileInfo(it->next()).canonicalFilePath();
if (m_tracked_files.contains(path))
missing_files.remove(path);
}
return missing_files;
}
void GameTracker::LoadGame(const QString& path)
{
const std::string converted_path = path.toStdString();
if (!DiscIO::ShouldHideFromGameList(converted_path))
{
bool cache_changed = false;
auto game = m_cache.AddOrGet(converted_path, &cache_changed, m_title_database);
if (game)
emit GameLoaded(std::move(game));
if (cache_changed)
m_cache.Save();
}
}