-
Notifications
You must be signed in to change notification settings - Fork 2
/
scan.cc
2257 lines (1924 loc) · 58.2 KB
/
scan.cc
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Advance project.
*
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Andrea Mazzoleni
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "portable.h"
#include "game.h"
#include "ziprom.h"
#include "conf.h"
#include "operatio.h"
#include "output.h"
#include "analyze.h"
#include "lib/readinfo.h"
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
void read_dir(const string& path, filepath_container& ds, bool recursive, const string& ext) {
DIR* dir = opendir(path.c_str());
if (!dir)
throw error() << "Failed open on dir " << path;
struct dirent* ent = readdir(dir);
while (ent) {
string subpath = path + "/" + ent->d_name;
struct stat st;
if (stat(subpath.c_str(), &st)!=0)
throw error() << "Failed stat on file " << subpath;
if (S_ISDIR(st.st_mode)) {
if (recursive) {
if (strcmp(ent->d_name, ".")!=0 && strcmp(ent->d_name, "..")!=0) {
read_dir(subpath.c_str(), ds, recursive, ext);
}
}
} else {
if (file_compare(file_ext(ent->d_name), ext) == 0) {
ds.insert(ds.end(), filepath(subpath.c_str()));
}
}
ent = readdir(dir);
}
closedir(dir);
}
void read_zip(const string& path, ziparchive& zar, zip_type type, bool ignore_error, bool rename_error) {
filepath_container ds;
read_dir(path, ds, false, ".zip");
for(filepath_container::iterator i=ds.begin();i!=ds.end();++i) {
ziparchive::iterator j;
try {
j = zar.open_and_insert(ziprom(i->file_get(), type, true));
} catch (error_invalid& e) {
if (ignore_error) {
cerr << "warning: damaged zip " << i->file_get() << "\n";
cerr << "warning: " << e << "\n";
cerr << "warning: ignoring it and resuming\n";
} else if (rename_error) {
cerr << "warning: damaged zip " << i->file_get() << "\n";
cerr << "warning: " << e << "\n";
#if HAVE_LONG_FNAME
string reject = i->file_get() + ".damaged";
#else
string reject = file_basepath(i->file_get()) + ".bad";
#endif
cerr << "warning: renaming it to " << reject << " and resuming\n";
file_move(i->file_get(), reject);
} else {
throw e << " opening zip " << i->file_get();
}
}
}
}
// ---------------------------------------------------------------------------
// statistics
// Association for a rom and wrong crc
struct rom_bad {
rom r;
unsigned bad_size;
crc_t bad_crc;
unsigned size_get() const { return r.size_get(); }
crc_t crc_get() const { return r.crc_get(); }
const string& name_get() const { return r.name_get(); }
unsigned bad_size_get() const { return bad_size; }
crc_t bad_crc_get() const { return bad_crc; }
};
typedef list<rom_bad> rom_bad_container;
struct rom_stat_t {
rom_by_name_set rom_miss; // rom missing
rom_by_name_set rom_equal; // rom good
rom_bad_container rom_bad; // rom wrong
rom_by_name_set unk_binary; // unknown binary file
rom_by_name_set unk_text; // unknown text file
rom_by_name_set unk_garbage; // unknown garbage file
rom_by_name_set nodump_equal; // nodump present
rom_bad_container nodump_bad; // nodump present bat wrong
rom_by_name_set nodump_miss; // nodump missing
};
void stat_rom_zip(
const ziprom& zd,
const game& gam,
rom_stat_t& result,
const analyze& ana)
{
// setup
rom_by_name_set b = gam.rs_get();
for(ziprom::const_iterator z=zd.begin();z!=zd.end();++z) {
// search for name
rom_by_name_set::iterator i = b.find(rom(z->name_get(), 0, 0, false));
if (i == b.end()) { // if name unknown
// rom to insert
rom r(z->name_get(), z->uncompressed_size_get(), z->crc_get(), false);
analyze_type t = ana(z->name_get(), z->uncompressed_size_get(), z->crc_get());
switch (t) {
case analyze_text :
result.unk_text.insert(r);
break;
case analyze_binary :
result.unk_binary.insert(r);
break;
case analyze_garbage :
result.unk_garbage.insert(r);
break;
}
} else { // if name know
if (i->nodump_get()) {
if (z->uncompressed_size_get() == i->size_get() && z->crc_get() == i->crc_get()) {
result.nodump_equal.insert(*i);
} else {
rom_bad r;
r.r = *i;
r.bad_size = z->uncompressed_size_get();
r.bad_crc = z->crc_get();
result.nodump_bad.insert(result.nodump_bad.end(), r);
}
} else {
if (z->uncompressed_size_get() == i->size_get() && z->crc_get() == i->crc_get()) {
result.rom_equal.insert(*i);
} else {
// rom is wrong
rom_bad r;
r.r = *i;
r.bad_size = z->uncompressed_size_get();
r.bad_crc = z->crc_get();
result.rom_bad.insert(result.rom_bad.end(), r);
}
}
// remove from original bag
b.erase(i);
}
}
for(rom_by_name_set::iterator i=b.begin();i!=b.end();++i) {
if (i->nodump_get()) {
result.nodump_miss.insert(*i);
} else {
result.rom_miss.insert(*i);
}
}
}
struct sample_stat_t {
sample_by_name_set sample_equal;
sample_by_name_set sample_miss;
sample_by_name_set unk_binary;
sample_by_name_set unk_text;
sample_by_name_set unk_garbage;
};
void sample_stat(
const ziprom& zd,
const game& gam,
sample_stat_t& result,
const analyze& ana)
{
result.sample_miss = gam.ss_get();
for(ziprom::const_iterator z=zd.begin();z!=zd.end();++z) {
sample_by_name_set::iterator i = result.sample_miss.find(sample(z->name_get()));
if (i == result.sample_miss.end()) { // if name unknown
sample s(z->name_get());
analyze_type t = ana(z->name_get(), z->uncompressed_size_get(), z->crc_get());
switch (t) {
case analyze_text :
result.unk_text.insert(s);
break;
case analyze_binary :
result.unk_binary.insert(s);
break;
case analyze_garbage :
result.unk_garbage.insert(s);
break;
}
} else { // if name know
result.sample_equal.insert(*i);
// remove from original bag
result.sample_miss.erase(i);
}
}
}
struct disk_stat_t {
sha1 hash;
disk_by_name_set disk_equal;
disk_by_name_set disk_bad;
disk_by_name_set unk_binary;
};
void disk_stat(
const string& z,
const game& gam,
disk_stat_t& result,
const analyze& ana)
{
string name = file_basename(z);
result.hash = disk_sha1(z);
disk_by_name_set::iterator i=gam.ds_get().find(disk(name));
if (i == gam.ds_get().end()) {
result.unk_binary.insert(disk(name));
} else {
if (!(result.hash == i->sha1_get())) {
result.disk_bad.insert(*i);
} else {
result.disk_equal.insert(*i);
}
}
}
// ----------------------------------------------------------------------------
// add a new rom set
void rom_add(
const operation& oper,
ziprom& z,
const game& g,
const ziparchive& zar,
gamerom_by_crc_multiset& rcb,
output& out)
{
bool title = false; // true if is printed the zip file name
rom_by_name_set good;
rom_by_name_set miss_unique;
rom_by_name_set miss_shared;
// split the roms in shared and unique
for(rom_by_name_set::iterator i=g.rs_get().begin();i!=g.rs_get().end();++i) {
pair<gamerom_by_crc_multiset::iterator,gamerom_by_crc_multiset::iterator> range;
range = rcb.equal_range(gamerom("",*i));
if (range.first != range.second)
++range.first; // one element is always present and it's the rom itself
if (range.first != range.second)
miss_shared.insert(*i);
else
miss_unique.insert(*i);
}
// for any unique missig rom
rom_by_name_set tmp_miss;
for(rom_by_name_set::iterator i=miss_unique.begin();i!=miss_unique.end();++i) {
bool found = false;
bool added = false;
// skip nodump
if (i->nodump_get())
continue;
// check if is in another zip
if (!found) {
ziprom::const_iterator k;
ziparchive::const_iterator j = zar.find_exclude(z, i->size_get(), i->crc_get(), k);
if (j!=zar.end()) {
if (oper.active_add() && !z.is_readonly()) {
z.add(k, i->name_get());
good.insert(*i);
added = true;
}
if (oper.output_add()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom("rom_good", "add", *i) << " " << k->parentname_get() << "/" << k->name_get() << "\n";
}
found = true;
}
}
if (!added)
// if not found insert in missing rom
tmp_miss.insert(*i);
}
miss_unique = tmp_miss;
// if all the roms are shared or at least one unique rom was found
if (miss_unique.empty() || !good.empty()) {
// for any shared missig rom
rom_by_name_set tmp_miss;
for(rom_by_name_set::iterator i=miss_shared.begin();i!=miss_shared.end();++i) {
bool found = false;
bool added = false;
// skip nodump
if (i->nodump_get())
continue;
// check if is in another zip
if (!found) {
ziprom::const_iterator k;
ziparchive::const_iterator j = zar.find_exclude(z, i->size_get(), i->crc_get(), k);
if (j!=zar.end()) {
if (oper.active_add() && !z.is_readonly()) {
z.add(k, i->name_get());
good.insert(*i);
added = true;
}
if (oper.output_add()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom("rom_good", "add", *i) << " " << k->parentname_get() << "/" << k->name_get() << "\n";
}
found = true;
}
}
if (!added)
// if not found insert in missing rom
tmp_miss.insert(*i);
}
miss_shared = tmp_miss;
}
if (title)
out() << "\n";
// if zip created with almost one good rom
if (!good.empty()) {
// update the zip
z.save();
z.unload();
if (!z.empty()) {
// get file information
struct stat fst;
string path = z.file_get();
if (stat(path.c_str(), &fst) != 0)
throw error() << "Failed stat file " << z.file_get();
// if no rom is missing
if (miss_unique.empty() && miss_shared.empty()) {
// add zip to directory list of game as good
g.rzs_add(infopath(z.file_get(), true, fst.st_size, z.is_readonly()));
} else {
// add zip to directory list of game as bad
g.rzs_add(infopath(z.file_get(), false, fst.st_size, z.is_readonly()));
}
}
}
}
// ----------------------------------------------------------------------------
// scan an existing rom set
bool rom_scan_add(
const operation& oper,
const char* s_add,
const char* s_cmd,
ziprom& z,
ziprom& reject,
bool& found,
bool& title,
string s_name,
unsigned s_size,
crc_t s_crc,
rom_by_name_set& rremove,
const ziparchive& zar,
output& out)
{
bool added = false;
if (!found) {
// check if it's present in the remove bag
for(rom_by_name_set::iterator j=rremove.begin();j!=rremove.end();++j) {
// compare crc, size
if (s_crc == j->crc_get() && s_size == j->size_get()) {
if (oper.active_fix() && !z.is_readonly()) {
// rename the rom
z.rename(j->name_get(), s_name, reject);
added = true;
// remove from the remove bag
rremove.erase(j);
}
if (oper.output_fix()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom(s_add, s_cmd, s_name, s_size, s_crc) << " " << j->name_get() << "\n";
}
found = true;
// interrupt
break;
}
}
}
if (!found) {
// check if it's present in the same zip
for(zip::iterator j=z.begin();j!=z.end();++j) {
// compare crc, size
if (s_crc == j->crc_get() && s_size == j->uncompressed_size_get()) {
string name = j->name_get();
if (s_name != name) {
// the name must be different (it always be)
if (oper.active_fix() && !z.is_readonly()) {
// add the rom
// (this operation is safe, because the zip iterator is a list)
z.add(j, s_name, reject);
added = true;
}
} else {
throw error() << "Failed internal check";
}
if (oper.output_fix()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom(s_add, s_cmd, s_name, s_size, s_crc) << " " << name << "\n";
}
found = true;
// interrupt
break;
}
}
}
if (!found) {
// check if it's present in the reject zip
for(zip::iterator j=reject.begin();j!=reject.end();++j) {
// compare crc, size
if (s_crc == j->crc_get() && s_size == j->uncompressed_size_get()) {
string name = j->name_get();
if (s_name != name) {
// the name must be different (it may not be)
if (oper.active_fix() && !z.is_readonly()) {
// add the rom
// (this operation is safe, because the zip iterator is a list)
z.add(j, s_name, reject);
added = true;
}
} else {
if (oper.active_fix() && !z.is_readonly()) {
// swap the roms
z.swap(s_name, reject, j);
added = true;
}
}
if (oper.output_fix()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom(s_add, s_cmd, s_name, s_size, s_crc) << " " << reject.file_get() << "/" << name << "\n";
}
found = true;
// interrupt
break;
}
}
}
if (!found) {
// check if it's in another zip with the exclusion of the reject zip
ziprom::const_iterator k;
ziparchive::const_iterator j = zar.find_exclude(z, s_size, s_crc, k);
if (j!=zar.end()) {
if (oper.active_fix() && !z.is_readonly()) {
z.add(k, s_name, reject);
added = true;
}
if (oper.output_fix()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom(s_add, s_cmd, s_name, s_size, s_crc) << " " << k->parentname_get() << "/" << k->name_get() << "\n";
}
found = true;
}
}
return added;
}
void rom_scan(
const operation& oper,
ziprom& z,
ziprom& reject,
const game& gam,
const ziparchive& zar,
output& out,
const analyze& ana)
{
bool title = false; // true if is printed the zip file name
reject.open();
// get rom status
rom_stat_t st;
stat_rom_zip(z, gam, st, ana);
// for any missing rom
rom_by_name_set tmp_rom_miss; // missing rom
for(rom_by_name_set::iterator i=st.rom_miss.begin();i!=st.rom_miss.end();++i) {
bool found = false;
bool added = false;
if (rom_scan_add(
oper,
"rom_good", "add",
z, reject, found, title,
i->name_get(), i->size_get(), i->crc_get(),
st.unk_binary, zar, out)) {
st.rom_equal.insert(*i);
added = true;
}
if (!added) {
// rom is missing
tmp_rom_miss.insert(*i);
}
}
st.rom_miss = tmp_rom_miss;
// for any wrong rom (rom with corret name but bad crc or size)
rom_bad_container tmp_rom_bad;
for(rom_bad_container::iterator i=st.rom_bad.begin();i!=st.rom_bad.end();++i) {
bool found = false;
bool added = false;
if (rom_scan_add(
oper,
"rom_good", "add",
z, reject, found, title,
i->name_get(), i->size_get(), i->crc_get(),
st.unk_binary, zar, out)) {
st.rom_equal.insert(i->r);
added = true;
}
if (!added)
// if not found insert in wrong rom
tmp_rom_bad.insert(tmp_rom_bad.end(), *i);
}
st.rom_bad = tmp_rom_bad;
for(rom_by_name_set::iterator i=st.unk_binary.begin();i!=st.unk_binary.end();++i) {
if (oper.active_remove_binary() && !z.is_readonly()) {
z.remove(i->name_get(), reject);
}
if (oper.output_remove_binary()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom("binary", "remove", *i) << "\n";
}
}
for(rom_by_name_set::iterator i=st.unk_text.begin();i!=st.unk_text.end();++i) {
if (oper.active_remove_text() && !z.is_readonly()) {
z.remove(i->name_get(), reject);
}
if (oper.output_remove_text()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom("text", "remove", *i) << "\n";
}
}
for(rom_by_name_set::iterator i=st.unk_garbage.begin();i!=st.unk_garbage.end();++i) {
if (oper.active_remove_garbage() && !z.is_readonly()) {
z.remove(i->name_get());
}
if (oper.output_remove_garbage()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom("garbage", "remove", *i) << "\n";
}
}
if (title)
out() << "\n";
// update the zip
z.save();
z.unload();
if (!z.empty()) {
// get file information
struct stat fst;
string path = z.file_get();
if (stat(path.c_str(), &fst) != 0)
throw error() << "Failed stat file " << z.file_get();
// if no rom is missing or wrong. Ignore nodump.
if (st.rom_miss.empty()
&& st.rom_bad.empty()
) {
// add zip to directory list of game as good
gam.rzs_add(infopath(z.file_get(), true, fst.st_size, z.is_readonly()));
} else {
// add zip to directory list of game as bad
gam.rzs_add(infopath(z.file_get(), false, fst.st_size, z.is_readonly()));
}
}
// update the reject zip
reject.save();
reject.unload();
}
void sample_scan(
const operation& oper,
ziprom& z,
ziprom& reject,
const game& gam,
output& out,
const analyze& ana)
{
bool title = false; // true if is printed the zip file name
reject.open();
// get sample status
sample_stat_t st;
sample_stat(z, gam, st, ana);
// for any miss sample
sample_by_name_set tmp_rom_miss;
for(sample_by_name_set::iterator i=st.sample_miss.begin();i!=st.sample_miss.end();++i) {
bool found = false;
bool added = false;
if (!found) {
// check if is present in remove bag with directory
for(sample_by_name_set::iterator j=st.unk_binary.begin();j!=st.unk_binary.end();++j) {
// compare name without dir
if (file_compare(i->name_get(), file_name(j->name_get()))==0) {
// found rom with different name
if (oper.active_fix() && !z.is_readonly()) {
z.add(j->name_get(), i->name_get());
st.sample_equal.insert(*i);
added = true;
}
if (oper.output_fix()) {
out.title("sample_zip", title, z.file_get());
out.cmd_sample("sound", "add", *i) << " " << j->name_get() << "\n";
}
found = true;
break;
}
}
}
if (!added)
// if not found insert in missing sample
tmp_rom_miss.insert(*i);
}
st.sample_miss = tmp_rom_miss;
for(sample_by_name_set::iterator i=st.unk_binary.begin();i!=st.unk_binary.end();++i) {
if (oper.active_remove_binary() && !z.is_readonly()) {
z.remove(i->name_get(), reject);
}
if (oper.output_remove_binary()) {
out.title("sample_zip", title, z.file_get());
out.cmd_sample("binary", "remove", *i) << "\n";
}
}
for(sample_by_name_set::iterator i=st.unk_text.begin();i!=st.unk_text.end();++i) {
if (oper.active_remove_text() && !z.is_readonly()) {
z.remove(i->name_get(), reject);
}
if (oper.output_remove_text()) {
out.title("sample_zip", title, z.file_get());
out.cmd_sample("text", "remove", *i) << "\n";
}
}
for(sample_by_name_set::iterator i=st.unk_garbage.begin();i!=st.unk_garbage.end();++i) {
if (oper.active_remove_garbage() && !z.is_readonly()) {
z.remove(i->name_get());
}
if (oper.output_remove_garbage()) {
out.title("sample_zip", title, z.file_get());
out.cmd_sample("garbage", "remove", *i) << "\n";
}
}
if (title)
out() << "\n";
// update the zip
z.save();
z.unload();
if (!z.empty()) {
// get file information
struct stat fst;
string path = z.file_get();
if (stat(path.c_str(), &fst) != 0)
throw error() << "Failed stat file " << z.file_get();
// if no sample is missing
if (st.sample_miss.empty()) {
gam.szs_add(infopath(z.file_get(), true, fst.st_size, z.is_readonly()));
} else {
gam.szs_add(infopath(z.file_get(), false, fst.st_size, z.is_readonly()));
}
}
// update the reject zip
reject.save();
reject.unload();
}
void disk_scan(
const operation& oper,
const string& z,
const game& gam,
output& out,
const analyze& ana)
{
disk_stat_t st;
disk_stat(z, gam, st, ana);
// get file information
struct stat fst;
if (stat(z.c_str(), &fst) != 0)
throw error() << "Failed stat file " << z;
if (!st.disk_equal.empty()) {
gam.dzs_add(infopath(z, true, fst.st_size, false));
} else {
gam.dzs_add(infopath(z, false, fst.st_size, false));
}
}
void unknown_scan(
ziprom& z,
const ziparchive& zar)
{
// create the list of all the roms to remove
rom_by_name_set remove;
for(ziprom::const_iterator i=z.begin();i!=z.end();++i) {
rom r(i->name_get(), i->uncompressed_size_get(), i->crc_get(), false);
remove.insert(r);
}
for(rom_by_name_set::iterator i=remove.begin();i!=remove.end();++i) {
ziprom::const_iterator k;
// search if the rom is present in a file in the own set
ziparchive::const_iterator j = zar.find(i->size_get(), i->crc_get(), zip_own, k);
if (j != zar.end()) {
// if present remove the duplicate in the unknown set
z.remove(i->name_get());
}
}
// update the zip
z.save();
z.unload();
}
// ----------------------------------------------------------------------------
// move a rom set
void rom_move(
const operation& oper,
ziprom& z,
ziprom& reject,
output& out)
{
bool title = false; // true if is printed the zip file name
reject.open();
// create the list of all the roms to remove
rom_by_name_set remove;
for(ziprom::const_iterator i=z.begin();i!=z.end();++i) {
rom r(i->name_get(), i->uncompressed_size_get(), i->crc_get(), false);
remove.insert(r);
}
for(rom_by_name_set::iterator i=remove.begin();i!=remove.end();++i) {
if (oper.active_move() && !z.is_readonly()) {
z.remove(i->name_get(), reject);
}
if (oper.output_move()) {
out.title("rom_zip", title, z.file_get());
out.cmd_rom("binary", "remove", *i) << "\n";
}
}
if (title)
out() << "\n";
// save the reject zip. First save and only after delete the data.
reject.save();
reject.unload();
// delete the empty zip.
z.save();
z.unload();
}
void sample_move(
const operation& oper,
ziprom& z,
ziprom& reject,
output& out)
{
bool title = false; // true if is printed the zip file name
reject.open();
// create the list of all the roms to remove
rom_by_name_set remove;
for(ziprom::const_iterator i=z.begin();i!=z.end();++i) {
rom r(i->name_get(), i->uncompressed_size_get(), i->crc_get(), false);
remove.insert(r);
}
for(rom_by_name_set::iterator i=remove.begin();i!=remove.end();++i) {
if (oper.active_move() && !z.is_readonly()) {
z.remove(i->name_get(), reject);
}
if (oper.output_move()) {
out.title("sample_zip", title, z.file_get());
out.cmd_rom("binary", "remove", *i) << "\n";
}
}
if (title)
out() << "\n";
// save the reject zip. First save and only after delete the data.
reject.save();
reject.unload();
// delete the empty zip.
z.save();
z.unload();
}
void disk_move(
const operation& oper,
const string& z,
const string& reject,
output& out)
{
bool title = false;
if (oper.active_move()) {
cerr << "log: " << "move " << z << " to " << reject << endl;
file_move(z, reject);
}
if (oper.output_move()) {
out.title("disk_chd", title, z);
out.cmd_disk("binary", "remove", file_name(z)) << "\n";
}
if (title)
out() << "\n";
}
// ----------------------------------------------------------------------------
// report
void rom_report(
const ziprom& z,
const game& gam,
output& out,
bool verbose,
const analyze& ana)
{
// if empty don't report
if (z.empty())
return;
// get rom status
rom_stat_t st;
stat_rom_zip(z, gam, st, ana);
// print the report
if (!st.rom_miss.empty() || !st.rom_bad.empty()
|| (verbose && (!st.nodump_miss.empty() || !st.nodump_bad.empty() || !st.nodump_equal.empty() || !st.unk_text.empty() || !st.unk_binary.empty() || !st.unk_garbage.empty()))
) {
bool title = false;
out.title("rom_zip", title, z.file_get());
for(rom_by_name_set::iterator i=st.rom_miss.begin();i!=st.rom_miss.end();++i) {
out.state_rom("rom_miss", *i) << "\n";
}
for(rom_by_name_set::iterator i=st.nodump_miss.begin();i!=st.nodump_miss.end();++i) {
out.state_rom("nodump_miss", *i) << "\n";
}
for(rom_bad_container::iterator i=st.rom_bad.begin();i!=st.rom_bad.end();++i) {
out.state_rom_real("rom_bad", i->r, i->bad_size_get(), i->bad_crc_get()) << "\n";
}
for(rom_bad_container::iterator i=st.nodump_bad.begin();i!=st.nodump_bad.end();++i) {
out.state_rom_real("nodump_bad", i->r, i->bad_size_get(), i->bad_crc_get()) << "\n";
}
for(rom_by_name_set::iterator i=st.unk_text.begin();i!=st.unk_text.end();++i) {
out.state_rom("text", *i) << "\n";
}
for(rom_by_name_set::iterator i=st.unk_binary.begin();i!=st.unk_binary.end();++i) {
out.state_rom("binary", *i) << "\n";
}
for(rom_by_name_set::iterator i=st.unk_garbage.begin();i!=st.unk_garbage.end();++i) {
out.state_rom("garbage", *i) << "\n";
}
for(rom_by_name_set::iterator i=st.nodump_equal.begin();i!=st.nodump_equal.end();++i) {
out.state_rom("nodump_good", i->name_get(), i->size_get(), i->crc_get()) << "\n";
}
for(rom_by_name_set::iterator i=st.rom_equal.begin();i!=st.rom_equal.end();++i) {
out.state_rom("rom_good", *i) << "\n";
}
out() << "\n";
}
}
void sample_report(
const ziprom& z,
const game& gam,
output& out,
bool verbose,
const analyze& ana)
{
// if empty don't report
if (z.empty())
return;
// get rom status
sample_stat_t st;
sample_stat(z, gam, st, ana);
// print the report
if (!st.sample_miss.empty()
|| (verbose && (!st.unk_text.empty() || !st.unk_binary.empty() || !st.unk_garbage.empty()))
) {
bool title = false;
out.title("sample_zip", title, z.file_get());
for(sample_by_name_set::iterator i=st.sample_miss.begin();i!=st.sample_miss.end();++i) {
out.state_sample("sound_miss", *i) << "\n";
}