Wednesday, November 21, 2012

M.A.M.E CHD Convert v4 to v5

I think we can agree that converting the drive images from v4 to v5 is tedious.  Definitely something to be scripted.  Since it's easy to find a Windows' version of the chdman program (I'm using a version from .147u3), I decided to do the script in Windows with Perl.  If you don't have Perl for Windows, get it here.  Once Perl is installed, copy and save the script below.  The script will scan your mame/rom directory looking for sub-directories.  It will then change to each sub-dir looking for version 4 of the CHD.  If the CHD is version 4, it will check the v5 list.  If the CHD is version 4 and found in the v5 list, it will get converted to version 5.  The v5 list is based on 147u3.  I'll try to keep it up to date.  Hope this helps someone.

Things to note.  I've already ran a CRC check against each CHD and know they all verify successfully.  If you have a corrupt CHD and are not aware, this program will probably continue to run, the resulting CHD will not work (neither did the original CHD it tried to convert).  I didn't do a lot of error checking.  The qx process will capture all STDOUT.  The data is there if you wish to parse STDOUT looking for success or failures during the conversion process.  I've also commented out the deletion of the original file.  I should probably include moving the v4 file to a backup directory instead of leaving it in its original directory.

If you use the script, be sure to set the location of  your chdman.exe and the ROM directory within the script.  Not sure if anyone else has experienced chdman hangs but I've seen the application hang during the copy process.  I've read that the hang is due to a possible problem in the multi-processor code.  I haven't confirmed that but I did build a single vCPU guest in my virtual environment for this task.  No hangs yet.

#### Updated for .147u4 - 12/17/2012

#### Updated for .148 - 1/25/2012

#### Updated for .164 - 7/29/2015


<----  Begin script ---->

#!/usr/bin/perl -w
#
# Get info to determine version:
#    chdman info -i <input file name>
#    Will return "File Version: 4"
#
# Convert from version the latest version
#    chdman copy -i <input file name> -o <new file name>
#    remove the original file and rename the new file
#

our $CHDMAN="g:\\mame\\chdman.exe";
our $Temp_Name = "tempnew.chd";

our @CHD_V5 = qw(a51site4-2_01.chd
a51site4-2_0.chd
alien.chd
arctthnd.chd
uarctict.chd
area51t.chd
area51.chd
area51mx.chd
atronic.chd
gdl-0018.chd
bam2.chd
batlgear.chd
bg2_204j.chd
bg2_201j.chd
gdl-0023a.chd
beachhead2000_5-27-2003.chd
beachhead2002_5-27-2003.chd
beachhead2003desertwar_5-27-2003.chd
beachhead2000_9-16-2001.chd
bikiniko.chd
biofreak.chd
bldyr3b.chd
blitz.chd
blitz2k.chd
blitz99.chd
753jaa11.chd
853jaa11.chd
gcb07jca02.chd
gcb07jca01.chd
gcc01jca02.chd
gcc01jca01.chd
825jaa11.chd
847jaa11.chd
981jaa11.chd
a21jaa11.chd
b07jaa11.chd
993hdda01.chd
988jaa11.chd
858jaa11.chd
a05jaa11.chd
995jaa11.chd
c01jaa11.chd
c44jaa03.chd
985jaa01.chd
bntyhunt.chd
a45a02.chd
645c04.chd
rtimes.chd
telly.chd
calchase.chd
calspeed.chd
calspeda.chd
carnevil.chd
carnevi1.chd
cartfury.chd
chaosheat.chd
chaosheatj.chd
gds-0001.chd
gdl-0014a.chd
cliffhgr.chd
cobra.chd
922d02.chd
922b02.chd
comebaby.chd
gdx-0002b.chd
420uaa04.chd
csplayh1.chd
csplayh5.chd
csplayh7.chd
cubeqst.chd
885jab01.chd
ep_pharo.chd
810uba02.chd
szz_cf.chd
fateulc.chd
jam1-dvd0.chd
firefox.chd
flipmaze.chd
fuudol.chd
gamecst2.chd
gamecstl.chd
99bottles.chd
gammagic.chd
gauntdl.chd
gauntd24.chd
gauntleg.chd
gauntl12.chd
gdvsgd.chd
gobyrc.chd
rcdego.chd
941b02.chd
gdx-0013.chd
sed1dvd0.chd
vr_xp_system_6-11-2002.chd
globalvr_xp_system.chd
hydro.chd
hyperath.chd
hyperv2_pqi_6-12-02.chd
hyperv2_pqi_9-30-01.chd
hyprdriv.chd
gdl-0010.chd
gds-0039b.chd
gds-0027.chd
gds-0026b.chd
gds-0033.chd
gds-0032c.chd
jdreddb.chd
jdreddc.chd
jn010108.chd
cap-jjk-3.chd
cap-jjm-1.chd
b41c02.chd
junai.chd
junai2.chd
gdl-0040.chd
kdeadeye.chd
kn1-b.chd
kinst.chd
kinst2.chd
cdp-00146.chd
kollon.chd
kollonc.chd
landhigh.chd
mace.chd
macea.chd
mach3.chd
mahjngoh.chd
a40jab02.chd
maxforce.chd
c09c04.chd
c09d04.chd
gdx-0017f.chd
mjmania.chd
a29b02.chd
a29a02.chd
b33a02.chd
b47jxb02.chd
mwskinsa.chd
mwskins104.chd
mwskins.chd
nagano98.chd
720jaa01.chd
nbanfl.chd
nbashowt.chd
npy1cd0b.chd
nfsug1_1-disc2.chd
nfsug1_1-disc1.chd
gds-0023e.chd
nightrai.chd
offrthnd.chd
gdx-0007.chd
orbatak.chd
otenamhf.chd
otenamih.chd
otenki.chd
gdx-0004a.chd
gdx-0014a.chd
a00jac02.chd
a00uad02.chd
a00kac02.chd
a00uac02.chd
a00eaa02.chd
b11a02.chd
pbball96.chd
pp201.chd
831jhdda01.chd
gq986jaa01.chd
gq986jaa02.chd
a04jaa02.chd
a04jaa01.chd
gqa16jaa01.chd
gqa16jaa02.chd
b00jab01.chd
b00jaa02.chd
gqb30jaa01.chd
gqb30jaa02.chd
c00jab.chd
gea02jaa02.chd
gea02jaa01.chd
977kaa02.chd
a11jaa01.chd
a11jaa02.chd
977jaa02.chd
gc977jaa02.chd
gc977jaa01.chd
ppp2nd.chd
primrag2.chd
psattack.chd
gdl-0024.chd
psyvaria.chd
psyvarrv.chd
gds-0031.chd
pwrshovl.chd
gq460a08.chd
ge557a09.chd
quakeat.chd
pqiidediskonmodule.chd
676a04.chd
gdl-0032a.chd
raizpin.chd
raycris.chd
cap-wzd-3.chd
roadburn.chd
rotr.chd
rrv1-a.chd
gca18jaa.chd
savquest.chd
gdx-0018a.chd
scp1cd0.chd
gdl-0030a.chd
sf2049se.chd
sf2049te.chd
sf2049.chd
sfrush.chd
sfrushrk.chd
gds-0016.chd
shanghss.chd
shanghaito.chd
gdl-0021.chd
shikigam.chd
sianniv.chd
simpbowl.chd
sc21-dvd0b.chd
sc21-dvd0d.chd
sc31001-na-dvd0-b.chd
soutenry.chd
sf010101.chd
speeddrv.chd
spuzbobj.chd
spuzbobl.chd
a13b02.chd
a13c02.chd
gdl-0005.chd
hm-in2.chd
db1.chd
gv027j1.chd
tk10100-1-na-dvd0-a.chd
tk9100-1-na-dvd0-a.chd
tef1dvd0.chd
te51-dvd0.chd
tenthdeg.chd
thenanpa.chd
a41b02.chd
a41a02.chd
a41c02.chd
tst1dvd0.chd
755jaa01.chd
756jab01.chd
tokyocop.chd
gdl-0036a.chd
gdl-0026.chd
tkk2-a.chd
a30b02.chd
a30c02.chd
turrett.chd
gdl-0035.chd
usagi.chd
usvsthem.chd
vaportrx.chd
vaportrp.chd
vcircle.chd
gds-0036f.chd
voyager.chd
wmn1.chd
gdx-0016a.chd
warfa.chd
wargods_08-15-1996.chd
wargods_10-09-1996.chd
wargods_12-11-1995.chd
c22d02.chd
c22a02.chd
c22c02.chd
weddingr.chd
wg3dh.chd
c18jaa03.chd
c27jaa03.chd
xiistag.chd
b4xb02.chd
yuyuhaku.chd
zga1dvd0.chd
zdx1dvd0.chd
zokuoten.chd
zooo.chd
b44jaa01.chd
706jaa02.chd
887kba02.chd
887aaa02.chd
887jaa02.chd
887kaa02.chd
a22jaa02.chd
a34jaa02.chd
a27jaa02.chd
b19jaa02.chd
b20jaa02.chd
894jaa02.chd
a38jaa02.chd
845aaa02.chd
845jba02.chd
845jab02.chd
810eaa02.chd
gdt-0008c.chd
b17jaa02.chd
gdl-0001.chd
gdl-0006.chd
a12jaa01.chd
a12jaa02.chd
gdl-0034.chd
gdl-0039.chd
gdl-0039a.chd
gdl-0028c.chd
gds-0023c.chd
623jaa02.chd
802jab02.chd
gdl-0017.chd
gdl-0002.chd
gds-0004.chd
gds-0005.chd
gds-0019.chd
gdx-0003a.chd
gds-0024a.chd
gds-0036a.chd
gds-0036d.chd
gdt-0002.chd
gdt-0015.chd
gdt-0013e.chd
gds-0011.chd
gds-0010.chd
gdl-0020.chd
);

sub ctrl_c_handler {
 print "\nCtrl C pressed \n";
 exit 2;
}

sub CHD_Info
{
 my $ROM_Name = $_[0] or exit 1;
 my $CHDVer = 0;
 print $ROM_Name,"\n";
 my $output = qx{$CHDMAN info -i $ROM_Name};
 foreach (split(/\n/,$output)) {
 if ($_ =~ /^File/) {
   $CHDVer = $_;
   chomp $CHDVer;
  }
 }

 my $junk;
 ($junk, $CHDVer) = split(':', $CHDVer);

# $CHDVer = int($CHDVer);

 return int $CHDVer;
}

sub CHD_Copy
{
 my $ROM_Name = $_[0] or exit 1;
 my $stdout = qx {$CHDMAN copy -i $ROM_Name -o $Temp_Name};
 my $Old_Name = $ROM_Name . ".old";
 $stdout = qx {ren $ROM_Name $Old_Name};
 $stdout = qx {ren $Temp_Name $ROM_Name};
# $stdout = qx {del $Old_Name};
}

unless (-f $CHDMAN) {
    print "We need a working chdmgr program.  Check the variable CHDMAN in the script\n";
    exit 1;
}
print "Lets go...\n";

$SIG {"INT"} = \&ctrl_c_handler; # "INT" indicates "Interrupt" signal.

my $Src_Dir = "g:\\mame\\roms";
my $Version;
chdir 'g:\\mame\\roms';

my @Src_Chds = qx {dir /AD /b /on};
foreach my $Src_Chd (@Src_Chds) {
 chomp $Src_Chd;
 chdir $Src_Chd;
 print "Directory: ", $Src_Chd, "\n";
 if (-e $Temp_Name) {
  unlink $Temp_Name;
 }
 my @CHDS = qx {dir *.chd /b};
 foreach my $CHD (@CHDS) {
  chomp $CHD;
  if (grep {$_ eq $CHD} @CHD_V5) {
    $Version = CHD_Info $CHD;
    if ($Version == 4) {
     print "\n", $CHD, " is version: ", $Version, " - Now Converting to v5\n";
     CHD_Copy $CHD;
    }
    }
 }
 chdir "..";
}

<----End Script---->