Debian Codegraph @

Source/Core/DolphinQt/GameList/GameTracker.cpp

raw symbols SCIP
1 // Copyright 2015 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/GameList/GameTracker.h"
5
6 #include <QApplication>
7 #include <QDir>
8 #include <QDirIterator>
9 #include <QFile>
10
11 #include "Common/Config/Config.h"
12
13 #include "Core/Config/UISettings.h"
14
15 #include "DiscIO/DirectoryBlob.h"
16
17 #include "DolphinQt/QtUtils/QueueOnObject.h"
18
19 #include "DolphinQt/Settings.h"
20
21 #include "UICommon/GameFile.h"
22
23 // NOTE: Qt likes to be case-sensitive here even though it shouldn't be thus this ugly regex hack
24 static const QStringList game_filters{
25 QStringLiteral("*.[gG][cC][mM]"), QStringLiteral("*.[bB][iI][nN]"),
26 QStringLiteral("*.[iI][sS][oO]"), QStringLiteral("*.[tT][gG][cC]"),
27 QStringLiteral("*.[cC][iI][sS][oO]"), QStringLiteral("*.[gG][cC][zZ]"),
28 QStringLiteral("*.[wW][bB][fF][sS]"), QStringLiteral("*.[wW][iI][aA]"),
29 QStringLiteral("*.[rR][vV][zZ]"), QStringLiteral("hif_000000.nfs"),
30 QStringLiteral("*.[wW][aA][dD]"), QStringLiteral("*.[eE][lL][fF]"),
31 QStringLiteral("*.[dD][oO][lL]"), QStringLiteral("*.[jJ][sS][oO][nN]")};
32
33 GameTracker::GameTracker(QObject* parent) : QFileSystemWatcher(parent)
34 {
35 qRegisterMetaType<std::shared_ptr<const UICommon::GameFile>>();
36 qRegisterMetaType<std::string>();
37
38 connect(qApp, &QApplication::aboutToQuit, this, [this] {
39 m_processing_halted = true;
40 m_load_thread.StopAndCancel();
41 });
42 connect(this, &QFileSystemWatcher::directoryChanged, this, &GameTracker::UpdateDirectory);
43 connect(this, &QFileSystemWatcher::fileChanged, this, &GameTracker::UpdateFile);
44 connect(&Settings::Instance(), &Settings::AutoRefreshToggled, this, [] {
45 const auto paths = Settings::Instance().GetPaths();
46
47 for (const auto& path : paths)
48 {
49 Settings::Instance().RemovePath(path);
50 Settings::Instance().AddPath(path);
51 }
52 });
53
54 connect(&Settings::Instance(), &Settings::MetadataRefreshRequested, this,
55 [this] { m_load_thread.EmplaceItem(Command{CommandType::UpdateMetadata, {}}); });
56
57 m_load_thread.Reset("GameList Tracker", [this](const Command& command) {
58 switch (command.type)
59 {
60 case CommandType::LoadCache:
61 LoadCache();
62 break;
63 case CommandType::Start:
64 StartInternal();
65 break;
66 case CommandType::AddDirectory:
67 AddDirectoryInternal(command.path);
68 break;
69 case CommandType::RemoveDirectory:
70 RemoveDirectoryInternal(command.path);
71 break;
72 case CommandType::UpdateDirectory:
73 UpdateDirectoryInternal(command.path);
74 break;
75 case CommandType::UpdateFile:
76 UpdateFileInternal(command.path);
77 break;
78 case CommandType::UpdateMetadata:
79 m_cache.UpdateAdditionalMetadata(
80 [this](const std::shared_ptr<const UICommon::GameFile>& game) { emit GameUpdated(game); },
81 m_processing_halted);
82 QueueOnObject(this, [] { Settings::Instance().NotifyMetadataRefreshComplete(); });
83 break;
84 case CommandType::ResumeProcessing:
85 m_processing_halted = false;
86 break;
87 case CommandType::PurgeCache:
88 m_cache.Clear(UICommon::GameFileCache::DeleteOnDisk::Yes);
89 break;
90 case CommandType::BeginRefresh:
91 m_refresh_in_progress = true;
92 QueueOnObject(this, [] { Settings::Instance().NotifyRefreshGameListStarted(); });
93 for (auto& file : m_tracked_files.keys())
94 emit GameRemoved(file.toStdString());
95 m_tracked_files.clear();
96 break;
97 case CommandType::EndRefresh:
98 m_refresh_in_progress = false;
99 m_cache.Save();
100 QueueOnObject(this, [] { Settings::Instance().NotifyRefreshGameListComplete(); });
101 break;
102 }
103 });
104
105 m_load_thread.EmplaceItem(Command{CommandType::LoadCache, {}});
106
107 // TODO: When language changes, reload m_title_database and call m_cache.UpdateAdditionalMetadata
108 }
109
110 void GameTracker::LoadCache()
111 {
112 m_cache.Load();
113 m_cache_loaded_event.Set();
114 }
115
116 void GameTracker::Start()
117 {
118 if (m_initial_games_emitted)
119 return;
120
121 m_initial_games_emitted = true;
122
123 m_load_thread.EmplaceItem(Command{CommandType::Start, {}});
124
125 m_cache_loaded_event.Wait();
126
127 m_cache.ForEach(
128 [this](const std::shared_ptr<const UICommon::GameFile>& game) { emit GameLoaded(game); });
129
130 m_initial_games_emitted_event.Set();
131 }
132
133 void GameTracker::StartInternal()
134 {
135 if (m_started)
136 return;
137
138 m_started = true;
139
140 QueueOnObject(this, [] { Settings::Instance().NotifyRefreshGameListStarted(); });
141
142 std::vector<std::string> paths;
143 paths.reserve(m_tracked_files.size());
144 for (const QString& path : m_tracked_files.keys())
145 paths.push_back(path.toStdString());
146
147 const auto emit_game_loaded = [this](const std::shared_ptr<const UICommon::GameFile>& game) {
148 emit GameLoaded(game);
149 };
150 const auto emit_game_updated = [this](const std::shared_ptr<const UICommon::GameFile>& game) {
151 emit GameUpdated(game);
152 };
153 const auto emit_game_removed = [this](const std::string& path) { emit GameRemoved(path); };
154
155 m_initial_games_emitted_event.Wait();
156
157 bool cache_updated =
158 m_cache.Update(paths, emit_game_loaded, emit_game_removed, m_processing_halted);
159 cache_updated |= m_cache.UpdateAdditionalMetadata(emit_game_updated, m_processing_halted);
160 if (cache_updated)
161 m_cache.Save();
162
163 QueueOnObject(this, [] { Settings::Instance().NotifyMetadataRefreshComplete(); });
164 QueueOnObject(this, [] { Settings::Instance().NotifyRefreshGameListComplete(); });
165 }
166
167 bool GameTracker::AddPath(const QString& dir)
168 {
169 if (Settings::Instance().IsAutoRefreshEnabled())
170 QueueOnObject(this, [this, dir] { return addPath(dir); });
171
172 m_tracked_paths.push_back(dir);
173
174 return true;
175 }
176
177 bool GameTracker::RemovePath(const QString& dir)
178 {
179 if (Settings::Instance().IsAutoRefreshEnabled())
180 QueueOnObject(this, [this, dir] { return removePath(dir); });
181
182 const auto index = m_tracked_paths.indexOf(dir);
183
184 if (index == -1)
185 return false;
186
187 m_tracked_paths.remove(index);
188
189 return true;
190 }
191
192 void GameTracker::AddDirectory(const QString& dir)
193 {
194 m_load_thread.EmplaceItem(Command{CommandType::AddDirectory, dir});
195 }
196
197 void GameTracker::RemoveDirectory(const QString& dir)
198 {
199 m_load_thread.EmplaceItem(Command{CommandType::RemoveDirectory, dir});
200 }
201
202 void GameTracker::RefreshAll()
203 {
204 m_processing_halted = true;
205 m_load_thread.Cancel();
206 m_load_thread.EmplaceItem(Command{CommandType::ResumeProcessing, {}});
207
208 if (m_needs_purge)
209 {
210 m_load_thread.EmplaceItem(Command{CommandType::PurgeCache, {}});
211 m_needs_purge = false;
212 }
213
214 m_load_thread.EmplaceItem(Command{CommandType::BeginRefresh});
215
216 for (const QString& dir : Settings::Instance().GetPaths())
217 {
218 m_load_thread.EmplaceItem(Command{CommandType::RemoveDirectory, dir});
219 m_load_thread.EmplaceItem(Command{CommandType::AddDirectory, dir});
220 }
221
222 m_load_thread.EmplaceItem(Command{CommandType::EndRefresh});
223 }
224
225 void GameTracker::UpdateDirectory(const QString& dir)
226 {
227 m_load_thread.EmplaceItem(Command{CommandType::UpdateDirectory, dir});
228 }
229
230 void GameTracker::UpdateFile(const QString& dir)
231 {
232 m_load_thread.EmplaceItem(Command{CommandType::UpdateFile, dir});
233 }
234
235 void GameTracker::AddDirectoryInternal(const QString& dir)
236 {
237 if (!QFileInfo(dir).exists())
238 return;
239 AddPath(dir);
240 UpdateDirectoryInternal(dir);
241 }
242
243 static std::unique_ptr<QDirIterator> GetIterator(const QString& dir)
244 {
245 return std::make_unique<QDirIterator>(dir, game_filters, QDir::NoFilter,
246 Config::Get(Config::MAIN_RECURSIVE_ISO_PATHS) ?
247 QDirIterator::Subdirectories :
248 QDirIterator::NoIteratorFlags);
249 }
250
251 void GameTracker::RemoveDirectoryInternal(const QString& dir)
252 {
253 RemovePath(dir);
254 const auto dir_it = GetIterator(dir);
255 while (dir_it->hasNext())
256 {
257 QString path = QFileInfo(dir_it->next()).canonicalFilePath();
258 if (const auto it = m_tracked_files.find(path); it != m_tracked_files.end())
259 {
260 auto& set = *it;
261 set.remove(dir);
262 if (set.isEmpty())
263 {
264 RemovePath(path);
265 m_tracked_files.erase(it);
266 if (m_started)
267 emit GameRemoved(path.toStdString());
268 }
269 }
270 }
271 }
272
273 void GameTracker::UpdateDirectoryInternal(const QString& dir)
274 {
275 const auto dir_it = GetIterator(dir);
276 while (dir_it->hasNext() && !m_processing_halted)
277 {
278 QString path = QFileInfo(dir_it->next()).canonicalFilePath();
279
280 if (const auto it = m_tracked_files.find(path); it != m_tracked_files.end())
281 {
282 it->insert(dir);
283 }
284 else
285 {
286 AddPath(path);
287 m_tracked_files[path] = QSet<QString>{dir};
288 LoadGame(path);
289 }
290 }
291
292 for (const auto& missing : FindMissingFiles(dir))
293 {
294 if (m_processing_halted)
295 break;
296
297 auto& tracked_file = m_tracked_files[missing];
298
299 tracked_file.remove(dir);
300 if (tracked_file.empty())
301 {
302 m_tracked_files.remove(missing);
303 if (m_started)
304 GameRemoved(missing.toStdString());
305 }
306 }
307 }
308
309 void GameTracker::UpdateFileInternal(const QString& file)
310 {
311 if (QFileInfo(file).exists())
312 {
313 if (m_started)
314 GameRemoved(file.toStdString());
315 AddPath(file);
316 LoadGame(file);
317 }
318 else if (RemovePath(file))
319 {
320 m_tracked_files.remove(file);
321 if (m_started)
322 emit GameRemoved(file.toStdString());
323 }
324 }
325
326 QSet<QString> GameTracker::FindMissingFiles(const QString& dir)
327 {
328 auto it = GetIterator(dir);
329
330 QSet<QString> missing_files;
331
332 for (const auto& key : m_tracked_files.keys())
333 {
334 if (m_tracked_files[key].contains(dir))
335 missing_files.insert(key);
336 }
337
338 while (it->hasNext())
339 {
340 QString path = QFileInfo(it->next()).canonicalFilePath();
341 if (m_tracked_files.contains(path))
342 missing_files.remove(path);
343 }
344
345 return missing_files;
346 }
347
348 void GameTracker::LoadGame(const QString& path)
349 {
350 if (!m_started)
351 return;
352
353 const std::string converted_path = path.toStdString();
354 if (!DiscIO::ShouldHideFromGameList(converted_path))
355 {
356 bool cache_changed = false;
357 auto game = m_cache.AddOrGet(converted_path, &cache_changed);
358 if (game)
359 emit GameLoaded(game);
360 if (cache_changed && !m_refresh_in_progress)
361 m_cache.Save();
362 }
363 }
364
365 void GameTracker::PurgeCache()
366 {
367 m_needs_purge = true;
368 Settings::Instance().RefreshGameList();
369 }