-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfcrawler.cpp
340 lines (268 loc) · 9.71 KB
/
fcrawler.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <QVariantMap>
#include "fcrawler.h"
#include "fgame.h"
#include "flogging.h"
FCrawler::FCrawler()
{
}
FCrawler::~FCrawler()
{
}
void FCrawler::scanAllFolders()
{
db.beginTransaction();
updateSteamDirs();
QList<FWatchedFolder> folder = db.getWatchedFoldersList();
for (int i=0; i<folder.length(); ++i)
{
FWatchedFolder f = folder[i];
if(f.forLauncher)
{
scanforLauncher(f);
}
else
{
FGameType folderType = getType(folder.at(i));
if(folderType==FGameType::Steam)
{
getSteamGames(f);
}
else if (folderType==FGameType::Galaxy)
{
getGalaxyGames(f);
}
}
}
//Origin only on Windows
#if OS == Windows
getOriginGames();
#endif
db.endTransaction();
}
void FCrawler::updateSteamDirs()
{
//Getting the Steam-Directories first
QStringList steamFolders;
steamFolders << "C:\\Program Files\\Steam\\steamapps\\libraryfolders.vdf";
steamFolders << "~/.local/share/Steam/steamapps/libraryfolders.vdf";
steamFolders << "C:\\Program Files (x86)\\Steam\\steamapps\\libraryfolders.vdf";
QFile libFolder;
libFolder.setFileName(steamFolders[0]);
int i = 1;
while( !libFolder.exists() && i < steamFolders.length())
{
libFolder.setFileName(steamFolders[i]);
++i;
}
if (!libFolder.exists())
return;
libFolder.open(QIODevice::ReadOnly|QIODevice::Text);
QString fileContent = libFolder.readAll();
QStringList fileLines = fileContent.split("\n");
libFolder.close();
int found = 1;
for (int i =0; i<fileLines.length(); ++i)
{
if (fileLines[i].contains("\""+QString::number(found)+"\"")) // = "1", "2",...
{
int last = fileLines[i].lastIndexOf("\"");
int prelast = fileLines[i].lastIndexOf("\"", -2);
QString dir = fileLines[i].mid(prelast+1, last-prelast-1) + QDir::separator() + "steamapps";
FWatchedFolder wf;
wf.setDirectory(QDir(dir));
if (!db.watchedFolderExists(&wf))
{
db.addWatchedFolder(wf);
++found;
}
}
}
}
void FCrawler::scanforLauncher(FWatchedFolder folder)
{
QStringList fileEndings = db.getLauncher(folder.getLauncherID()).getFileEndings().split(",", QString::SkipEmptyParts);
if (fileEndings.length() == 0)
{
qCWarning(fLibCrawler) << "Carwler for " << folder.getDirectory().absolutePath() << " could not find any Roms!";
return;
}
for (int i=0; i<fileEndings.length(); ++i)
fileEndings[i] = "*." + fileEndings[i];
QDir target = folder.getDirectory();
target.setNameFilters(fileEndings);
QStringList launcherFiles = target.entryList();
for(QString foundGame : launcherFiles) {
qCDebug(fLibCrawler) << "Found " + foundGame + " in " + target.absolutePath();
FGame g;
g.setName(foundGame.left(foundGame.length()-4));
g.setExe(foundGame);
g.setLauncher(db.getLauncher(folder.getLauncherID()));
g.setType(ROM);
g.setPath(target.absolutePath());
if (!db.gameExists(g))
if (!db.addGame(g))
qCWarning(fLibCrawler) << ("Error on insert Game " + g.getName());
else
qCDebug(fLibCrawler) << ("Game exists: " + g.getName());
}
}
FGameType FCrawler::getType(FWatchedFolder folder)
{
//TODO: Proper Type-Check
QDir fldr = folder.getDirectory();
fldr.setNameFilters(QStringList()<<"*.acf");
QStringList steamFiles = fldr.entryList();
if (steamFiles.length() > 0)
return FGameType::Steam;
qCInfo(fLibCrawler) << "Check if Galaxy-Folder exists";
QDir f = fldr.absolutePath() + QDir::separator() + "!Downloads";
if (f.exists())
return FGameType::Galaxy;
return FGameType::unknown;
}
void FCrawler::getGalaxyGames(FWatchedFolder folder)
{
QStringList subfolders = folder.getDirectory().entryList();
qCDebug(fLibCrawler) << ("Scanning Galaxy-Dir");
for (int j=0; j<subfolders.length(); ++j)
{
if(subfolders[j]=="!Downloads")
continue;
//Get Info-File
QStringList filters;
filters << "goggame-*.info";
QDir dir(folder.getDirectory().absolutePath() + QDir::separator() + subfolders[j]);
QStringList InfoFile = dir.entryList(filters);
if (InfoFile.length()==1)
{
QFile oLogFile;
oLogFile.setFileName(folder.getDirectory().absolutePath() + QDir::separator() + subfolders[j] + QDir::separator() + InfoFile[0]);
oLogFile.open(QIODevice::ReadOnly|QIODevice::Text);
QString fileContent = oLogFile.readAll();
QStringList fileLines = fileContent.split("\n");
oLogFile.close();
FGame g;
g.setType(FGameType::Galaxy);
bool nameFound = false;
bool pathFound = false;
bool workingdirFound = false;
bool argumentFound = false;
for(int i=0;i<fileLines.length();++i)
{
QString line = fileLines[i].replace(QString("\t"), QString(" "));
QString val;
if(line.contains("\"name\"") && !nameFound) {
val =line.mid(26, line.lastIndexOf("\"")-26);
g.setName(val);
qCDebug(fLibCrawler) << "Name: " + val;
nameFound = true;
}
else if(line.contains("\"path\"") && !pathFound){
val = line.mid(27, line.lastIndexOf("\"")-27);
g.setExe(val);
pathFound = true;
qCInfo(fLibCrawler) << "Path: " + val;
}
else if(line.contains("\"workingDir\"") && !workingdirFound){
val = line.mid(28, line.lastIndexOf("\"")-28);
g.setPath(folder.getDirectory().absolutePath() + QDir::separator() + subfolders[j] + QDir::separator() + val);
workingdirFound = true;
qCInfo(fLibCrawler) << "workingDir: " + val;
}
else if(line.contains("\"arguments\"") && !argumentFound){
val =line.mid(28, line.lastIndexOf("\"")-28);
argumentFound = true;
qCInfo(fLibCrawler) << "arguments: " + val;
}
if(pathFound && nameFound && argumentFound && workingdirFound)
break;
}
if (pathFound && nameFound && workingdirFound) //This should be enough
{
if (!db.gameExists(g))
{
if (!db.addGame(g))
{
qCCritical(fLibCrawler) << ("Error on insert Game!");
}
}
else
{
qCDebug(fLibCrawler) << ("Game exists: " + g.getName());
}
}
}
else
continue;
}
}
void FCrawler::getOriginGames()
{
QString file = "C:\\ProgramData\\Origin\\Logs\\Client_Log.txt";
QList<QString> doneIDs;
QFile oLogFile;
oLogFile.setFileName(file);
if (!oLogFile.exists())
return;
oLogFile.open(QIODevice::ReadOnly|QIODevice::Text);
QString fileContent = oLogFile.readAll();
QStringList fileLines = fileContent.split("\n");
oLogFile.close();
//Example-String:
// 457 May 26 17:59:46.363 Event Origin::Downloader::ContentProtocolPackage::InitializeVerified 7460 [Game Title: Mass Effect 2 Digital Art Books][ProductID:OXX-MASS:57404]CRC Calculation Complete.
for (int i=0; i<fileLines.length(); ++i)
{
QString line = fileLines[i];
if (line.contains("Finished: "))
{
int gamenameLength = line.indexOf(", ctid:", 135)-135;
QString gameName = line.mid(135, gamenameLength);
QString gameID = line.right(7);
if (!doneIDs.contains(gameID))
{
FGame g;
g.setPath("-");
g.setName(gameName);
g.setType(FGameType::Origin);
g.setExe(gameID);
doneIDs.append(gameID);
if (!db.gameExists(g))
{
if(!db.addGame(g))
qCCritical(fLibCrawler) << "Error on insert Game!" << g.getName();
}
else
qCDebug(fLibCrawler) << "Game exists: " << g.getName();
}
}
}
}
void FCrawler::getSteamGames(FWatchedFolder folder)
{
QDir SteamAppsDir = folder.getDirectory();
SteamAppsDir.setNameFilters(QStringList()<<"*.acf");
QStringList steamFiles = SteamAppsDir.entryList();
for (QString file : steamFiles)
{
//Get File-Content
QFile f;
f.setFileName(SteamAppsDir.absoluteFilePath(file));
f.open(QIODevice::ReadOnly|QIODevice::Text);
QString fileContent = f.readAll();
f.close();
QStringList fileLines = fileContent.replace("\"", "").split("\n");
//Create Game
FGame g;
g.setName(fileLines[4].split("\t")[3]);
g.setType(FGameType::Steam);
g.setExe(fileLines[2].split("\t")[3]);
g.setPath("-");
if(!db.gameExists(g))
{
if(!db.addGame(g))
qCCritical(fLibCrawler) << "Error on insert Game!" << g.getName();
}
else
qCDebug(fLibCrawler) << "Game exists: " << g.getName();
}
}