-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatcheck.pl
414 lines (368 loc) · 10.8 KB
/
datcheck.pl
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
use strict;
use warnings;
use Term::ProgressBar;
use List::Util qw( min max );
#init
my $substringmiss = "-miss";
my $substringmatch = "-match";
my $substringfuzzy = "-fuzzy";
my $substringall = "-all";
my $logmissing = "FALSE";
my $logmatching = "FALSE";
my $logfuzzy = "FALSE";
my $logall = "FALSE";
my $datfile = "";
my $system = "";
my $discdirectory = "";
my $substringh = "-h";
my $bincue = "FALSE";
my $datbincue = "FALSE";
my @linesdat;
my @linesgames;
my @linesmatch;
my @linesmiss;
my @alllinesout;
#check command line
foreach my $argument (@ARGV) {
if ($argument =~ /\Q$substringh\E/) {
print "datcheck v1.4 - Utility to compare No-Intro or Redump dat files to the rom or disc collection\n";
print " and report the matches and misses in the collection and extra files.\n";
print " This includes exact matches, and fuzzy matches using Levenshtein edit distance.\n";
print "\n";
print "with datcheck [ options ] [dat file ...] [directory ...] [system]\n";
print "\n";
print "Options:\n";
print " -miss write only missing files to log file\n";
print " -match write only matching files and fuzzing matching to log file\n";
print " -fuzzy write only fuzzing matching files to log file\n";
print " -all write missing, matching, and fuzzy matching files to log file\n";
print "\n";
print "Example:\n";
print ' datcheck -miss -match "D:/Atari - 2600.dat" "D:/Atari - 2600/Games" "Atari - 2600"' . "\n";
print "\n";
print "Author:\n";
print " Discord - Romeo#3620\n";
print "\n";
exit;
}
if ($argument =~ /\Q$substringmiss\E/) {
$logmissing = "TRUE";
}
if ($argument =~ /\Q$substringmatch\E/) {
$logmatching = "TRUE";
}
if ($argument =~ /\Q$substringfuzzy\E/) {
$logfuzzy = "TRUE";
}
if ($argument =~ /\Q$substringall\E/) {
$logall = "TRUE";
}
}
#set paths and system variables
if (scalar(@ARGV) < 4 or scalar(@ARGV) > 7) {
print "Invalid command line.. exit\n";
print "use: datcheck -h\n";
print "\n";
exit;
}
$datfile = $ARGV[-3];
$system = $ARGV[-1];
$discdirectory = $ARGV[-2];
#debug
print "dat file: $datfile\n";
print "system: $system\n";
print "game directory: $discdirectory\n";
my $tempstr = "";
if ($logmissing eq "TRUE") {
$tempstr = "missing ";
}
if ($logmatching eq "TRUE") {
$tempstr = $tempstr . "matching ";
}
if ($logfuzzy eq "TRUE") {
$tempstr = $tempstr . "fuzzy matching ";
}
if ($logall eq "TRUE") {
$tempstr = "missing matching fuzzy matching ";
}
print "log format: " . $tempstr . "and extra files\n";
#exit no parameters
if ($datfile eq "" or $system eq "" or $discdirectory eq "") {
print "Invalid command line.. exit\n";
print "use: datcheck -h\n";
print "\n";
exit;
}
#read dat file
open(FILE, "<", $datfile) or die "Could not open $datfile\n";
while (my $readline = <FILE>) {
push(@linesdat, $readline);
if (index(lc $readline, ".cue") != -1)
{
$datbincue = "TRUE";
}
}
my @sorteddatfile = sort @linesdat;
close (FILE);
#read games directory contents
my $dirname = $discdirectory;
opendir(DIR, $dirname) or die "Could not open $dirname\n";
while (my $filename = readdir(DIR)) {
if (-d $dirname . "/" . $filename) {
next;
} else {
push(@linesgames, $filename) unless $filename eq '.' or $filename eq '..';
if (index(lc $filename, ".cue") != -1)
{
$bincue = "TRUE";
}
}
}
closedir(DIR);
my $romname = "";
my $gamename = "";
my $resultromstart;
my $resultromend;
my $resultgamestart;
my $resultgameend;
my $extpos;
my $extlen;
my $quotepos;
my $match = 0;
my $totalmatches = 0;
my $totalmisses = 0;
my $totalmissesfiles = 0;
my $totalextrafiles = 0;
my $totalfuzzymatches = 0;
my $any_matched;
my $length = 0;
my $i=0;
my $j=0;
my $p=0;
my $q=0;
my @matches;
my @extrafiles;
my @sortedromenames;
my $max = scalar(@sorteddatfile);
my $progress = Term::ProgressBar->new({name => 'matching & missing', count => $max});
#loop though each dat entry
OUTER: foreach my $datline (@sorteddatfile)
{
$progress->update($_);
if (index(lc $datline, "<rom name=") != -1)
{
if (($datbincue eq "TRUE" and index(lc $datline, ".bin") == -1) or $datbincue eq "FALSE")
{
$p++;
#parse rom name
$resultromstart = index($datline, '<rom name="');
$resultromend = index($datline, 'size="');
$extpos = rindex $datline, ".";
$quotepos = rindex $datline, '"', $resultromend;
my $length = ($resultromend) - ($resultromstart + 12);
$romname = substr($datline, $resultromstart + 11, $length - ($quotepos - $extpos + 1));
$romname =~ s/amp;//g; #clean '&' in the dat file
push (@sortedromenames, $romname);
foreach my $gameline (@linesgames)
{
#parse game name
if (index(lc $gameline, ".m3u") == -1)
{
$match = 0;
my $length = length($gameline);
my $rightdot = rindex($gameline, ".");
my $suffixlength = $length - $rightdot;
$gamename = substr($gameline, 0, $length - $suffixlength);
#check for exact match between dat name and filename
if ($romname eq $gamename)
{
$match = 1;
$totalmatches++;
push(@linesmatch, $romname);
if ($logmatching eq "TRUE" or $logall eq "TRUE")
{
push(@alllinesout, ["MATCHED:", $gamename]);
}
next OUTER;
}
}
}
}
#default if no match report missing
if ($match == 0 and (($datbincue eq "TRUE" and index(lc $datline, ".bin") == -1) or $datbincue eq "FALSE"))
{
push(@linesmiss, $romname);
if ($logmissing eq "TRUE" or $logall eq "TRUE")
{
push(@alllinesout, ["MISSING:", $romname]);
}
$totalmisses++;
}
}
}
my $max2 = scalar(@linesgames);
my $progress2 = Term::ProgressBar->new({name => 'extra files', count => $max2});
#loop through each filename
OUTER2: foreach my $gamematch (@linesgames)
{
$progress2->update($_);
#parse game name
my $length = length($gamematch);
my $rightdot = rindex($gamematch, ".");
my $suffixlength = $length - $rightdot;
my $gamematch2 = substr($gamematch, 0, $length - $suffixlength);
#loop through each matched dat name
foreach my $rommatch (@linesmatch)
{
$match = 0;
#check for exact match between dat name and filename
if ($rommatch eq $gamematch2)
{
$match = 1;
next OUTER2;
}
}
#default if no match report extra file
if ($match == 0)
{
if ((($bincue eq "TRUE" and index(lc $gamematch, ".m3u") == -1) and index(lc $gamematch, ".bin") == -1) or $bincue eq "FALSE")
{
push (@alllinesout, ["EXTRA FILE:", $gamematch]);
push (@extrafiles, $gamematch2);
$totalextrafiles++;
}
}
}
my $max3 = scalar(@extrafiles);
my $progress3 = Term::ProgressBar->new({name => 'fuzzy matching', count => $max3});
#loop through each extra file
OUTER3: foreach my $extrafileentry (@extrafiles)
{
$progress3->update($_);
if ($logfuzzy eq "TRUE" or $logall eq "TRUE")
{
if (($bincue eq "TRUE" and not index(lc $extrafileentry, ".bin")) or $bincue eq "FALSE")
{
#Special case to remove (v1.0) and (no EDC)
my $entry = $extrafileentry;
$entry =~ s/\(v1.0\)//g;
$entry =~ s/\(no EDC\)//g;
my $bestmatch = find_most_similar_fuzzy_match($entry, scalar(@sortedromenames), 0, @sortedromenames);
push(@alllinesout, ["FUZZY MATCH:", "$extrafileentry to: $bestmatch"]);
$totalfuzzymatches++;
next OUTER3;
}
}
}
#print total have
my $totalnames = 0;
$totalnames = $p;
print "\ntotal matches: $totalmatches of $totalnames\n";
#print total miss
print "total misses in dat: $totalmisses\n";
#print extra files
print "total extra files in collection: $totalextrafiles\n";
#print total fuzzy have
print "total fuzzy matches to extra files: $totalfuzzymatches\n";
#open log file and print all sorted output
open(LOG, ">", "$system.txt") or die "Could not open $system.txt\n";
print LOG "log format: " . $tempstr . "and extra files\n";
print LOG "total matches: $totalmatches of $totalnames\n";
print LOG "total misses in dat: $totalmisses\n";
print LOG "total extra files in collection: $totalextrafiles\n";
print LOG "total fuzzy matches to extra files: $totalfuzzymatches\n";
print LOG "---------------------------------------\n";
my @sortedalllinesout = sort{$a->[1] cmp $b->[1]} @alllinesout;
for($i=0; $i<=$#sortedalllinesout; $i++)
{
for($j=0; $j<2; $j++)
{
print LOG "$sortedalllinesout[$i][$j] ";
}
print LOG "\n";
}
close (LOG);
#print log filename
print "log file: $system.txt\n";
exit;
sub find_most_similar_fuzzy_match
{
my ($strinput, $lengtharray, $caseSensetive, @strcomparedto) = @_;
my $i;
my $distance;
my $mindistance = 1000;
my $bestfuzzystring;
for ($i = 0; $i < $lengtharray; $i++)
{
$distance = levenshtein_distance($strinput, $strcomparedto[$i], $caseSensetive);
if ($distance < $mindistance)
{
$mindistance = $distance;
$bestfuzzystring = $strcomparedto[$i];
}
}
return $bestfuzzystring;
}
sub levenshtein_distance
{
my ($strinput, $strcomparedto, $caseSensitive) = @_;
my $i;
my $j;
my $lengthinput = length($strinput);
my $lengthcompared = length($strcomparedto);
my $distance;
my @matrix;
if ($lengthinput == 0 || $lengthcompared == 0)
{
$distance = -1;
return $distance;
}
my $input = $strinput;
my $compared = $strcomparedto;
if (!$caseSensitive)
{
$input = lc($strinput);
$compared = lc($strcomparedto);
}
for ($i = 0; $i < $lengthinput + 1; $i++)
{
$matrix[$i][0] = $i;
}
for ($i = 0; $i < $lengthcompared + 1; $i++)
{
$matrix[0][$i] = $i;
}
for ($i = 1; $i < $lengthinput + 1; $i++)
{
my $si = substr $input, $i - 1, 1;
for ($j = 1; $j < $lengthcompared + 1; $j++)
{
my $tj = substr $compared, $j - 1, 1;
my $cost = ($si eq $tj) ? 0 : 1;
my $above = $matrix[$i - 1][$j];
my $left = $matrix[$i][$j - 1];
my $diag = $matrix[$i - 1][$j - 1];
my @array = ($above + 1, $left + 1, $diag + $cost);
my $cell = min @array;
if ($i > 1 && $j > 1)
{
my $trans = $matrix[$i - 2][$j - 2] + 1;
if (substr $input, $i - 2, 1 ne substr $compared, $j - 1, 1)
{
$trans++;
}
if (substr $input, $i - 1, 1 ne substr $compared, $j - 2, 1)
{
$trans++;
}
if ($cell > $trans)
{
$cell = $trans;
}
}
$matrix[$i][$j] = $cell;
}
}
$distance = $matrix[$lengthinput][$lengthcompared];
return $distance;
}