-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaudit.cpp
671 lines (541 loc) · 17.1 KB
/
audit.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#include "quazip.h"
#include "quazipfile.h"
#include "7zCrc.h"
#include "7zFile.h"
#include "7zVersion.h"
#include "7zAlloc.h"
#include "7zExtract.h"
#include "7zIn.h"
#include "audit.h"
#include "prototype.h"
#include "utils.h"
#include "processmanager.h"
#include "mainwindow.h"
#include "mameopt.h"
RomAuditor::RomAuditor(QObject *parent) :
QThread(parent),
hasAudited(false),
method(AUDIT_ONLY)
{
}
RomAuditor::~RomAuditor()
{
wait();
}
void RomAuditor::exportDat()
{
if (method == AUDIT_ONLY || fixDatFileName.isEmpty())
return;
QFile outFile(fixDatFileName);
if (outFile.open(QFile::WriteOnly | QFile::Text))
{
QXmlStreamWriter out(&outFile);
out.setAutoFormatting(true);
out.setAutoFormattingIndent(-1);
GameInfo *gameInfo;
QStringList gameNames;
//append all parent names to the list and sort it
foreach (QString gameName, pMameDat->games.keys())
{
gameInfo = pMameDat->games[gameName];
if (gameInfo->cloneof.isEmpty())
gameNames.append(gameName);
}
gameNames.sort();
QStringList gameNames_copy(gameNames);
//insert all sorted clone names to the list
int i = 0;
foreach (QString gameName, gameNames_copy)
{
gameInfo = pMameDat->games[gameName];
QList<QString> cloneList = gameInfo->clones.toList();
qSort(cloneList);
foreach (QString cloneName, cloneList)
gameNames.insert(++i, cloneName);
i ++;
}
//start writing xml
out.writeStartDocument();
out.writeDTD("<!DOCTYPE datafile PUBLIC \"-//Logiqx//DTD ROM Management Datafile//EN\" \"http://www.logiqx.com/Dats/datafile.dtd\">");
out.writeStartElement("datafile");
foreach (QString gameName, gameNames)
{
gameInfo = pMameDat->games[gameName];
if ((method == AUDIT_EXPORT_COMPLETE && !gameInfo->isExtRom) || gameInfo->available == GAME_MISSING)
{
GameInfo *gameInfo2 = NULL, *gameBiosInfo = NULL;
RomInfo *romInfo;
//we'd like to sort the roms, QMap is used
QMap<QString, quint32> missingRoms;
//if all roms of a game are missing, excluding missing bios
bool completelyMissing = false,
//if all roms of a clone are missing
completelyMissingClone = true;
//number of missing roms and nodumps
int missingCount = 0,
nodumpCount = 0,
//dont count bioses #3
biosRomsCount = 0;
//the game doesnt have a parent, default completelyMissingClone to false
if (gameInfo->romof.isEmpty())
completelyMissingClone = false;
else
//find parent (gameInfo2) and bios (gameBiosInfo) of the game
{
gameInfo2 = pMameDat->games[gameInfo->romof];
if (!gameInfo2->romof.isEmpty())
gameBiosInfo = pMameDat->games[gameInfo2->romof];
else if (gameInfo2->isBios)
gameBiosInfo = gameInfo2;
}
if (gameBiosInfo != NULL)
biosRomsCount = gameBiosInfo->roms.size();
//start iterating all roms
foreach (quint32 crc, gameInfo->roms.keys())
{
romInfo = gameInfo->roms.value(crc);
if (romInfo->status == "nodump")
nodumpCount++;
if (!romInfo->available)
{
//to reduce redundant data, continue loop if parent is also missing this rom
if (gameInfo2 != NULL &&
gameInfo2->roms.contains(crc) && !gameInfo2->roms.value(crc)->available)
{
//dont count bioses #1
if (/*!gameInfo2->isBios && */ (gameBiosInfo == NULL || !gameBiosInfo->roms.contains(crc)))
missingCount++;
continue;
}
missingRoms.insert(romInfo->name, crc);
//dont count bioses #2
if (gameBiosInfo == NULL || !gameBiosInfo->roms.contains(crc))
missingCount++;
}
else if (completelyMissingClone && gameInfo2 != NULL && !gameInfo2->roms.contains(crc))
{
//clone-specific rom is available
completelyMissingClone = false;
}
}
if (method != AUDIT_EXPORT_COMPLETE && missingRoms.size() == 0)
continue;
//it is possible that bios roms are nodump
if (missingCount >= gameInfo->roms.size() - biosRomsCount - nodumpCount)
completelyMissing = true;
if (gameInfo2 != NULL)
completelyMissing = completelyMissing || completelyMissingClone;
//toggle export methods
if ((method == AUDIT_EXPORT_INCOMPLETE && completelyMissing) ||
(method == AUDIT_EXPORT_MISSING && !completelyMissing))
continue;
//write xml
out.writeStartElement("game");
out.writeAttribute("name", gameName);
out.writeAttribute("sourcefile", gameInfo->sourcefile);
if (!gameInfo->romof.isEmpty())
out.writeAttribute("romof", gameInfo->romof);
out.writeTextElement("description", gameInfo->description);
if (!gameInfo->year.isEmpty())
out.writeTextElement("year", gameInfo->year);
out.writeTextElement("manufacturer", gameInfo->manufacturer);
foreach (quint32 crc, missingRoms)
{
romInfo = gameInfo->roms.value(crc);
out.writeStartElement("rom");
out.writeAttribute("name", romInfo->name);
out.writeAttribute("size", QString("%1").arg(romInfo->size));
out.writeAttribute("crc", QString("%1").arg(crc, 8, 16, QLatin1Char('0')));
out.writeEndElement();
}
out.writeEndElement();
}
}
out.writeEndDocument();
}
outFile.close();
win->poplog("Finished.");
}
void RomAuditor::audit(bool autoAudit, int _method, QString fileName)
{
if (isRunning())
return;
method = _method;
fixDatFileName = fileName;
//skip auditing and go export directly
if ((method == AUDIT_EXPORT_COMPLETE) || (method != AUDIT_ONLY && hasAudited))
{
exportDat();
return;
}
gameList->disableCtrls();
//must clear pMameDat in the main thread
// fixme: currently only console games are cleared
foreach (QString gameName, pMameDat->games.keys())
{
GameInfo *gameInfo = pMameDat->games[gameName];
if (gameInfo->isExtRom && gameList->isAuditConsoleFolder(gameInfo->romof))
{
pMameDat->games.remove(gameName);
delete gameInfo;
}
}
isConsoleFolder = gameList->isConsoleFolder();
if (autoAudit)
{
gameList->autoAudit = false;
isConsoleFolder = false;
}
hasAudited = true;
start(LowPriority);
}
void RomAuditor::run()
{
GameInfo *gameInfo, *gameInfo2;
RomInfo *romInfo;
//audit MAME
if(!isConsoleFolder)
{
QSet<QString> auditedGames;
//clear current state
foreach (QString gameName, pMameDat->games.keys())
{
gameInfo = pMameDat->games[gameName];
foreach (RomInfo *romInfo, gameInfo->roms)
if (romInfo->status == "nodump")
romInfo->available = true;
else
romInfo->available = false;
foreach (DiskInfo *diskInfo, gameInfo->disks)
{
if (diskInfo->status == "nodump")
diskInfo->available = true;
else
diskInfo->available = false;
}
}
QStringList dirPaths = mameOpts["rompath"]->currvalue.split(";");
//iterate rompaths
foreach (QString dirPath, dirPaths)
{
QDir dir(dirPath);
QStringList nameFilter = QStringList() << "*" ZIP_EXT;
nameFilter<<"*" SZIP_EXT;
QStringList romFiles = dir.entryList(nameFilter, QDir::Files | QDir::Readable | QDir::Hidden);
QStringList romDirs = dir.entryList(QStringList(), QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Readable | QDir::Hidden);
emit progressSwitched(romFiles.size(), QString(tr("Auditing %1 ...")).arg(dir.dirName() + "/"));
//iterate gameName/*.chd files
foreach (QString romDir, romDirs)
{
QDir dir2(utils->getPath(dirPath) + romDir);
QString gameName = dir2.dirName().toLower();
if (!pMameDat->games.contains(gameName))
continue;
gameInfo = pMameDat->games[gameName];
QStringList nameFilter2 = QStringList() << "*.chd";
QStringList chdFiles = dir2.entryList(nameFilter2, QDir::Files | QDir::Readable | QDir::Hidden);
foreach (QString chdFile, chdFiles)
{
QFileInfo fileInfo = QFileInfo(chdFile);
foreach (QString sha1, gameInfo->disks.keys())
{
DiskInfo *diskInfo = gameInfo->disks[sha1];
if (diskInfo->name == fileInfo.baseName().toLower())
{
diskInfo->available = true;
//also fill clones
foreach (QString cloneName, gameInfo->clones)
{
gameInfo2 = pMameDat->games[cloneName];
if (gameInfo2->disks.contains(sha1))
gameInfo2->disks[sha1]->available = true;
}
}
}
}
}
//iterate rom files
for (int i = 0; i < romFiles.size(); i++)
{
//update progressbar every 10 files
if (i % 10 == 0)
emit progressUpdated(i);
QString fullRomName = romFiles[i].toLower();
if(fullRomName.contains(ZIP_EXT))
{
QString gameName = fullRomName.remove(ZIP_EXT);
if (!pMameDat->games.contains(gameName))
continue;
gameInfo = pMameDat->games[gameName];
auditedGames.insert(gameName);
//open zip file
QuaZip zip(utils->getPath(dirPath) + romFiles[i]);
if(!zip.open(QuaZip::mdUnzip))
continue;
QuaZipFileInfo zipFileInfo;
QuaZipFile zipFile(&zip);
//iterate all files in the zip
for(bool more=zip.goToFirstFile(); more; more=zip.goToNextFile())
{
if(!zip.getCurrentFileInfo(&zipFileInfo))
continue;
//fill rom available status if crc recognized
if (gameInfo->roms.contains(zipFileInfo.crc))
gameInfo->roms.value(zipFileInfo.crc)->available = true;
//check if rom belongs to a clone
foreach (QString cloneName, gameInfo->clones)
{
auditedGames.insert(cloneName);
gameInfo2 = pMameDat->games[cloneName];
if (gameInfo2->roms.contains(zipFileInfo.crc))
gameInfo2->roms.value(zipFileInfo.crc)->available = true;
}
}
}
else if(fullRomName.contains(SZIP_EXT))
{
QString gameName = fullRomName.remove(SZIP_EXT);
if (!pMameDat->games.contains(gameName))
continue;
gameInfo = pMameDat->games[gameName];
auditedGames.insert(gameName);
CFileInStream archiveStream;
CLookToRead lookStream;
CSzArEx db;
SRes res;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
if (InFile_Open(&archiveStream.file, qPrintable(utils->getPath(dirPath) + romFiles[i])))
continue;
FileInStream_CreateVTable(&archiveStream);
LookToRead_CreateVTable(&lookStream, False);
lookStream.realStream = &archiveStream.s;
LookToRead_Init(&lookStream);
allocImp.Alloc = SzAlloc;
allocImp.Free = SzFree;
allocTempImp.Alloc = SzAllocTemp;
allocTempImp.Free = SzFreeTemp;
CrcGenerateTable();
SzArEx_Init(&db);
res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp);
if (res == SZ_OK)
{
UInt32 i;
Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */
for (i = 0; i < db.db.NumFiles; i++)
{
CSzFileItem *f = db.db.Files + i;
//fill rom available status if crc recognized
if (gameInfo->roms.contains(f->FileCRC))
gameInfo->roms.value(f->FileCRC)->available = true;
//check if rom belongs to a clone
foreach (QString cloneName, gameInfo->clones)
{
auditedGames.insert(cloneName);
gameInfo2 = pMameDat->games[cloneName];
if (gameInfo2->roms.contains(f->FileCRC))
gameInfo2->roms.value(f->FileCRC)->available = true;
}
}
IAlloc_Free(&allocImp, outBuffer);
}
SzArEx_Free(&db, &allocImp);
File_Close(&archiveStream.file);
}
}
}
// win->log(QString("audit 1.gamecount %1").arg(pMameDat->games.size()));
/* see if any rom of a game is not available */
//iterate games
foreach (QString gameName, pMameDat->games.keys())
{
gameInfo = pMameDat->games[gameName];
//fixme: skip auditing for consoles
if (gameInfo->isExtRom)
continue;
//if game rom file exists, default to passed, if not, skip and fail it
if (auditedGames.contains(gameName))
gameInfo->available = GAME_COMPLETE;
else
{
//fail unless: 1. all roms are nodump; 2. all roms are available in parent
bool allNoDump = true;
bool allinParent = true;
foreach (RomInfo *romInfo, gameInfo->roms)
{
//clone romInfo->available is already partially supplied by parent
if (!romInfo->available)
{
allinParent = false;
break;
}
}
foreach (RomInfo *romInfo, gameInfo->roms)
{
if (romInfo->status != "nodump")
{
allNoDump = false;
break;
}
}
gameInfo->available = (!allNoDump && !allinParent && gameInfo->disks.isEmpty()) ? GAME_MISSING : GAME_COMPLETE;
}
//iterate disks
foreach (DiskInfo *diskInfo, gameInfo->disks)
{
if (!diskInfo->available)
gameInfo->available = GAME_MISSING;
}
//iterate roms
foreach (quint32 crc, gameInfo->roms.keys())
{
romInfo = gameInfo->roms.value(crc);
//game rom passed
if (romInfo->available)
continue;
if (!gameInfo->romof.isEmpty())
{
//check parent
gameInfo2 = pMameDat->games[gameInfo->romof];
//parent rom passed
if (gameInfo2->roms.contains(crc) && gameInfo2->roms.value(crc)->available)
{
romInfo->available = true;
continue;
}
//check bios
if (!gameInfo2->romof.isEmpty())
{
gameInfo2 = pMameDat->games[gameInfo2->romof];
//bios rom passed
if (gameInfo2->roms.contains(crc) && gameInfo2->roms.value(crc)->available)
{
romInfo->available = true;
continue;
}
}
}
//failed audit
gameInfo->available = GAME_MISSING;
}
}
}
// win->log("finished auditing MAME games.");
pMameDat->completeData();
//audit each MESS system
foreach (QString gameName, pMameDat->games.keys())
{
GameInfo *gameInfo = pMameDat->games[gameName];
if (!gameInfo->devices.isEmpty())
{
if (!gameList->isAuditConsoleFolder(gameName))
continue;
auditConsole(gameName);
}
}
// win->log("finished auditing MESS systems.");
emit progressSwitched(-1);
}
// must be static func in a thread
void RomAuditor::auditConsole(QString consoleName)
{
QString _dirpath = mameOpts[consoleName + "_extra_software"]->globalvalue;
QDir dir(_dirpath);
if (_dirpath.isEmpty() || !dir.exists())
return;
QString dirPath = utils->getPath(_dirpath);
GameInfo *gameInfo = pMameDat->games[consoleName];
QString sourcefile = gameInfo->sourcefile;
QStringList nameFilters;
foreach (DeviceInfo *deviceInfo, gameInfo->devices)
foreach (QString ext, deviceInfo->extensionNames)
nameFilters << "*." + ext;
const QString allExtensionNames = nameFilters.join(";");
nameFilters << "*" ZIP_EXT;
nameFilters << "*" SZIP_EXT;
// iterate all files in the path
QStringList files = dir.entryList(nameFilters, QDir::Files | QDir::Readable);
emit progressSwitched(files.size(), QString(tr("Auditing %1 ...")).arg(consoleName));
for (int i = 0; i < files.size(); i++)
{
QString fileName = files[i];
QFileInfo fi(files[i]);
const QString suffixName = "." + fi.suffix();
if (suffixName == ZIP_EXT || suffixName == SZIP_EXT)
{
QHash<QString, MameFileInfo *> mameFileInfoList =
utils->iterateMameFile(dirPath, fi.completeBaseName(), allExtensionNames, MAMEFILE_GETINFO);
foreach (QString zipFileName, mameFileInfoList.keys())
{
QFileInfo zipFileInfo(zipFileName);
gameInfo = new GameInfo(pMameDat);
gameInfo->description = zipFileInfo.completeBaseName();
gameInfo->isExtRom = true;
gameInfo->romof = consoleName;
gameInfo->sourcefile = sourcefile;
gameInfo->available = GAME_COMPLETE;
QString key = dirPath + fileName + "/" + zipFileName;
pMameDat->games[key] = gameInfo;
}
utils->clearMameFileInfoList(mameFileInfoList);
}
else
{
gameInfo = new GameInfo(pMameDat);
gameInfo->description = fi.completeBaseName();
gameInfo->isExtRom = true;
gameInfo->romof = consoleName;
gameInfo->sourcefile = sourcefile;
gameInfo->available = GAME_COMPLETE;
pMameDat->games[dirPath + fileName] = gameInfo;
}
if (i % 10 == 0)
emit progressUpdated(i);
}
emit progressSwitched(-1);
}
MameExeRomAuditor::MameExeRomAuditor(QObject *parent) :
QObject(parent)
{
dlgAudit.setModal(true);
dlgAudit.setWindowTitle(tr("Checking..."));
dlgAudit.resize(400, 300);
QGridLayout *lay = new QGridLayout(&dlgAudit);
tbAudit = new QTextBrowser(&dlgAudit);
lay->addWidget(tbAudit, 0, 0, 1, 1);
QDialogButtonBox *buttonBox = new QDialogButtonBox(&dlgAudit);
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Ok);
lay->addWidget(buttonBox, 1, 0, 1, 1);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(auditorClosed()));
}
void MameExeRomAuditor::auditorReadyReadStandardOutput()
{
QProcess *proc = (QProcess *)sender();
tbAudit->append(QString::fromLocal8Bit(proc->readAllStandardOutput().data()));
}
void MameExeRomAuditor::auditorClosed()
{
loadProc->kill();
dlgAudit.accept();
}
void MameExeRomAuditor::audit(int method)
{
QStringList args;
if (method == VERIFY_CURRENT_ROMS || method == VERIFY_ALL_ROMS)
args << "-verifyroms";
else
args << "-verifysamples";
if (method == VERIFY_CURRENT_ROMS || method == VERIFY_CURRENT_SAMPLES)
args << currentGame;
if (hasLanguage)
{
QString langpath = utils->getPath(mameOpts["langpath"]->globalvalue);
args << "-langpath" << langpath;
args << "-language" << language;
}
tbAudit->clear();
loadProc = procMan->process(procMan->start(mame_binary, args, FALSE));
connect(loadProc, SIGNAL(readyReadStandardOutput()), this, SLOT(auditorReadyReadStandardOutput()));
dlgAudit.show();
}