forked from pallando/gfwl-stripper
/
mainwindow.cpp
509 lines (442 loc) · 15.7 KB
/
mainwindow.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDir>
#include <QTextStream>
#include <QSettings>
#include <QStandardPaths>
#include <QProcessEnvironment>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iomanip>
using namespace std;
// Block size (area around offset to search for hex string)
#define BLOCKSIZE 63
// Macro to manage mkdir call (Windows vs Unix)
#ifdef _WIN32
#include <direct.h>
#define MKDIR(PATH) mkdir(PATH)
#else
#include <sys/stat.h>
#define MKDIR(PATH) mkdir(PATH, 0775)
#endif
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
readSettings();
detectPaths();
detectSteamUsers();
selectGame(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::readSettings()
{
QSettings settings("Games.ini", QSettings::IniFormat);
QStringList groups = settings.childGroups();
int nbgroups = groups.size();
for(int i=0; i<nbgroups; ++i)
{
settings.beginGroup(groups[i]);
// Name of the game
GameFWL newgame;
newgame.setName(groups[i]);
// AppID, savegames extension and offset to strip header
newgame.setAppid(settings.value("app","0").toString());
newgame.setSavename(settings.value("savename").toString());
newgame.setExtension(settings.value("extension").toString());
newgame.setOffset(settings.value("offset").toString());
newgame.setHexstring(settings.value("hexstring").toString().toUpper());
// Get possible paths to the savegames
QStringList steamcloud = settings.value("steamcloud").toStringList();
QStringList steamapps = settings.value("steamapps").toStringList();
QStringList documents = settings.value("documents").toStringList();
QStringList local = settings.value("local").toStringList();
QStringList appdata = settings.value("appdata").toStringList();
QStringList publicfolder = settings.value("public").toStringList();
QStringList publicdata = settings.value("publicdata").toStringList();
// Check if paths are empty before storing them
if(steamcloud.size() > 1)
if(!steamcloud[0].isEmpty())
newgame.setPathInSteamCloud(steamcloud);
if(steamapps.size() > 1)
if(!steamapps[0].isEmpty())
newgame.setPathInSteamApps(steamapps);
if(documents.size() > 1)
if(!documents[0].isEmpty())
newgame.setPathInDocs(documents);
if(local.size() > 1)
if(!local[0].isEmpty())
newgame.setPathInLocal(local);
if(appdata.size() > 1)
if(!appdata[0].isEmpty())
newgame.setPathInAppData(appdata);
if(publicfolder.size() > 1)
if(!publicfolder[0].isEmpty())
newgame.setPathInPublic(publicfolder);
if(publicdata.size() > 1)
if(!publicdata[0].isEmpty())
newgame.setPathInPublicData(publicdata);
// Done reading game info
settings.endGroup();
// New game added to the list of games
games.push_back(newgame);
}
sort(games.begin(),games.end());
foreach(GameFWL game, games)
ui->game_selection->addItem(game.getName());
}
void MainWindow::detectPaths()
{
QSettings settings("HKEY_CURRENT_USER\\Software\\Valve\\Steam", QSettings::NativeFormat);
steampath = QDir::toNativeSeparators(settings.value("SteamPath", "").toString());
QStringList doclist = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
if(!doclist.empty())
documentspath = QDir::toNativeSeparators(doclist[0]);
QStringList genericdata = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
if(!genericdata.empty())
localpath = genericdata[0] + QDir::separator();
if(genericdata.size() > 1)
publicdatapath = genericdata[1] + QDir::separator();
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
appdatapath = env.value("APPDATA") + QDir::separator();
publicpath = env.value("PUBLIC") + QDir::separator();
// Add trailing separators if paths not empty
if(!steampath.isEmpty())
steampath += QDir::separator();
if(!documentspath.isEmpty())
documentspath += QDir::separator();
if(!localpath.isEmpty())
localpath += QDir::separator();
if(!appdatapath.isEmpty())
appdatapath += QDir::separator();
if(!publicdatapath.isEmpty())
publicdatapath += QDir::separator();
if(!publicpath.isEmpty())
publicpath += QDir::separator();
}
void MainWindow::detectSavePath()
{
int found = 0;
GameFWL game = games[gameidx];
QStringList possiblepaths;
QString detectedpath;
// We add the possible paths in documents
if(!documentspath.isEmpty())
foreach(QString path, game.getPathInDocs())
possiblepaths.push_back(QDir::toNativeSeparators(documentspath + path));
// We add the possible paths in Steam Cloud
QString steamcloudpath;
if(!steampath.isEmpty() && !game.getPathInSteamCloud().isEmpty())
{
steamcloudpath = steampath + QString("userdata") + QDir::separator();
// We look in each Steam user folder
QDir userdata(steamcloudpath);
QFileInfoList steamusers = userdata.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
for(int i=0; i<steamusers.size() && !found; ++i)
{
QString user = QDir::toNativeSeparators(steamusers[i].canonicalFilePath() + QDir::separator());
foreach(QString appid, game.getPathInSteamCloud())
possiblepaths.push_back(QDir::toNativeSeparators(user + appid + QString("/remote")));
}
}
// We add the possible paths in SteamApps
QString steamappspath;
if(!steampath.isEmpty())
{
steamappspath = steampath + QString("steamapps") + QDir::separator();
foreach(QString path, game.getPathInSteamApps())
possiblepaths.push_back(QDir::toNativeSeparators(steamappspath + path));
}
// We add the possible paths in "appdata"
if(!appdatapath.isEmpty())
foreach(QString path, game.getPathInAppData())
possiblepaths.push_back(QDir::toNativeSeparators(appdatapath + path));
// We add the possible paths in "local"
if(!localpath.isEmpty())
foreach(QString path, game.getPathInLocal())
possiblepaths.push_back(QDir::toNativeSeparators(localpath + path));
// We add the possible paths in "public"
if(!publicpath.isEmpty())
foreach(QString path, game.getPathInPublic())
possiblepaths.push_back(QDir::toNativeSeparators(publicpath + path));
// We add the possible paths in "publicdata"
if(!publicdatapath.isEmpty())
foreach(QString path, game.getPathInPublicData())
possiblepaths.push_back(QDir::toNativeSeparators(publicdatapath + path));
for(int i=0; i<possiblepaths.size() && found<1; ++i)
{
QDir folder(possiblepaths[i]);
QFileInfoList files = folder.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Time);
foreach(QFileInfo file, files)
{
if(file.completeSuffix() == game.getExtension())
++found;
if(found > 0)
detectedpath = folder.canonicalPath();
}
if(found == 0)
{
QFileInfoList subfolders = folder.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
foreach(QFileInfo subfolder, subfolders)
{
QDir subdir(subfolder.canonicalFilePath());
QFileInfoList subfiles = subdir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot, QDir::Time);
foreach(QFileInfo subfile, subfiles)
{
if(subfile.completeSuffix() == game.getExtension())
++found;
if(found > 0)
detectedpath = subdir.canonicalPath();
}
}
}
}
ui->filename_lineEdit->setText(QDir::toNativeSeparators(detectedpath));
ui->statusBar->showMessage(QString("Detected files: ") + QString::number(found));
// We set detectedpath to "Not found." to avoid asking automatically the user the path when the tool starts.
if(detectedpath.isEmpty())
detectedpath = "Not found.";
selectFolder(detectedpath);
}
void MainWindow::detectSteamUsers()
{
if(!steampath.isEmpty())
{
// List to display
QStringList display;
// Path to Steam Cloud
QString steamcloudpath = steampath + QString("userdata") + QDir::separator();
// We look in each Steam user folder
QDir userdata(steamcloudpath);
QFileInfoList steamuids = userdata.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
for(int i=0; i<steamuids.size(); ++i)
{
QString uid = QDir::toNativeSeparators(steamuids[i].baseName());
QString user = getPersona(uid);
display.push_back(uid + QString(" (") + user + QString(")"));
uids.push_back(uid);
}
ui->dest_selection->addItems(display);
}
else
{
uids.clear();
ui->dest_selection->setDisabled(true);
ui->dest_label->setText(QString("Steam not found."));
}
}
QString MainWindow::getPersona(QString uid)
{
QString res;
if(!steampath.isEmpty())
{
QString steamcloudpath = steampath + QString("userdata") + QDir::separator();
QString localconfig = steamcloudpath + uid + QDir::separator() + QString("config") + QDir::separator() + "localconfig.vdf";
ifstream file(localconfig.toStdString().c_str());
string line;
if(file.is_open())
{
while(getline(file,line) && res.isEmpty())
{
QString qline = QString::fromStdString(line);
if(qline.contains("PersonaName"))
res = qline.replace("\"PersonaName\"","").replace("\t","").replace('"',"");
}
file.close();
}
}
return res;
}
int MainWindow::copyToSteam()
{
int nbcopied = 0;
// If we've found SteamIDs
if(!uids.isEmpty())
{
GameFWL game = games[gameidx];
QString appid = game.getAppid();
QString steamuid = uids[ui->dest_selection->currentIndex()];
QChar sep = QDir::separator();
QString steamcloudappid = steampath + QString("userdata") + sep + steamuid + sep + appid + sep;
QString steamcloudbackup = documentspath + "backup." + appid;
// We check how many backups exist and name the next one accordingly.
QDir dir(steamcloudbackup);
int nbbak = 0;
while(dir.exists())
{
++nbbak;
dir.setPath(steamcloudbackup + QString(".") + QString::number(nbbak));
}
steamcloudbackup = dir.absolutePath();
// Check if directory already exists
bool backup = false;
dir.setPath(steamcloudappid);
if(dir.exists())
{
// Old saves backup
backup = dir.rename(steamcloudappid,steamcloudbackup);
}
else
backup = true;
// Copy patched files to Steam if previous folder has been backed up
if(backup)
{
int mkd1 = MKDIR(steamcloudappid.toStdString().c_str());
if(mkd1 == 0 || errno == EEXIST)
{
// Copy files in appid folder
QDir appidfolder = QDir(appid);
QFileInfoList files = appidfolder.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
foreach(QFileInfo file, files)
{
QFile tmp(file.canonicalFilePath());
QString newfile = steamcloudappid + file.fileName();
if(tmp.copy(newfile))
++nbcopied;
}
// Copy remote folder
QString remotefolder = steamcloudappid + QString("remote") + sep;
int mkd2 = MKDIR(remotefolder.toStdString().c_str());
if(mkd2 == 0 || errno == EEXIST)
{
// Copy files in "remote" folder
QDir outputfolder = QDir(appid + sep + "remote" + sep);
files = outputfolder.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
foreach(QFileInfo file, files)
{
QFile tmp(file.canonicalFilePath());
QString newfile = remotefolder + file.fileName();
if(tmp.copy(newfile))
++nbcopied;
}
}
}
}
}
return nbcopied;
}
void MainWindow::selectFolder(const QString & path)
{
if(path.isEmpty())
{
QString currentpath = ui->filename_lineEdit->text();
savepath = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this, tr("Choose savegame directory"),currentpath));
if(savepath != 0)
{
ui->filename_lineEdit->setText(savepath);
ui->start_button->setEnabled(true);
ui->statusBar->clearMessage();
}
else
{
ui->start_button->setEnabled(false);
}
}
else
{
// We do nothing if the path was not found automatically.
if(path.compare(QString("Not found."),Qt::CaseInsensitive) != 0)
{
savepath = path;
ui->start_button->setEnabled(true);
}
else
{
ui->start_button->setEnabled(false);
}
}
}
void MainWindow::processFolder()
{
QString message;
QDir savefolder(savepath);
GameFWL game = games[gameidx];
int processed = 0;
QString appid = game.getAppid();
QString outputfolder = appid + QDir::separator() + "remote" + QDir::separator();
if(savefolder.exists())
{
QFileInfoList files = savefolder.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
for(int i=0; i<files.size(); ++i)
{
if(files[i].completeSuffix() == game.getExtension())
{
int mkd1 = MKDIR(appid.toStdString().c_str());
if(mkd1 == 0 || errno == EEXIST)
{
int mkd2 = MKDIR(outputfolder.toStdString().c_str());
if(mkd2 == 0 || errno == EEXIST)
{
QString newname = files[i].fileName();
if(!game.getSavename().isEmpty())
{
if(~newname.startsWith(game.getSavename(),Qt::CaseInsensitive))
{
int ind = newname.indexOf(game.getSavename(),0,Qt::CaseInsensitive);
if(ind != -1)
newname = newname.remove(0,ind);
}
}
QString savefile = files[i].canonicalFilePath();
QString newsavefile = outputfolder + newname;
ifstream input_file(savefile.toStdString().c_str(),ios::binary);
ofstream output_file(newsavefile.toStdString().c_str(),ios::binary);
if (input_file.is_open() && output_file.is_open())
{
// We try to find the hex string around "offset"
int firsthalf = (BLOCKSIZE+1)/2;
char memblock[BLOCKSIZE];
input_file.seekg(game.getOffset()-firsthalf);
input_file.read(memblock,BLOCKSIZE);
ostringstream oss;
for (int block = 0; block < BLOCKSIZE; ++block)
{
int z = memblock[block]&0xff;
oss << hex << setfill('0') << setw(2) << uppercase << z;
}
QString filehexstring = QString::fromStdString(oss.str());
int hexpos = filehexstring.indexOf(game.getHexstring());
// If we found the string, we cut where it starts
if(hexpos != -1)
input_file.seekg(game.getOffset()-firsthalf+hexpos/2);
// Otherwise, we use the default offset
else
{
// We try to look for the hex string around the start
input_file.seekg(0);
input_file.read(memblock,BLOCKSIZE);
ostringstream oss2;
for (int block = 0; block < BLOCKSIZE; ++block)
{
int z = memblock[block]&0xff;
oss2 << hex << setfill('0') << setw(2) << uppercase << z;
}
filehexstring = QString::fromStdString(oss2.str());
hexpos = filehexstring.indexOf(game.getHexstring());
if(hexpos != -1)
input_file.seekg(hexpos/2);
else
input_file.seekg(game.getOffset());
}
output_file << input_file.rdbuf();
output_file.close();
input_file.close();
}
++processed;
}
}
}
}
}
message = "Processed " + QString::number(processed) + " file(s) into " + appid + ". ";
int nbcopied = copyToSteam();
if(nbcopied > 0)
message = message + QString::number(nbcopied) + " files copied to Steam. ";
ui->statusBar->showMessage(message);
}