Skip to content

Instantly share code, notes, and snippets.

@daktak
Created January 16, 2013 22:32
Show Gist options
  • Save daktak/4551582 to your computer and use it in GitHub Desktop.
Save daktak/4551582 to your computer and use it in GitHub Desktop.
Load your CRCs into MAME description for loading into RCB
#!/bin/env python
# This script adds the CRC values of your ROMs to the description file
# http://code.google.com/p/romcollectionbrowser/wiki/HowToAddMAMEOffline
# Use the linked MAME.txt to generate your custom description file for
# offline loading into http://code.google.com/p/romcollectionbrowser/
import os
import stat
import zlib
from time import gmtime, strftime
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("romDir", help="Directory containing roms",
type=str)
parser.add_argument("inFile", help="File containing game CRCs",
type=str)
parser.add_argument("outFile", help="File to write out with new CRCs", type=str)
args = parser.parse_args()
romDir = args.romDir
mameFile = args.inFile
outFile = args.outFile
crcString = "CRC: "
gameString = "Game Filename: "
extString = ".zip"
def crc(fileName):
prev = 0
for eachLine in open(fileName,"rb"):
prev = zlib.crc32(eachLine, prev)
return "%X"%(prev & 0xFFFFFFFF)
def walktree (top = ".", depthfirst = True):
names = os.listdir(top)
if not depthfirst:
yield top, names
for name in names:
try:
st = os.lstat(os.path.join(top, name))
except os.error:
continue
if stat.S_ISDIR(st.st_mode):
for (newtop, children) in walktree (os.path.join(top, name), depthfirst):
yield newtop, children
if depthfirst:
yield top, names
print "Looking in "+romDir+" for roms and calculating CRC value."
crc_a=[]
file_a=[]
for (basepath, children) in walktree(romDir,False):
for child in children:
#print os.path.join(basepath, child)
myfile = os.path.join(basepath,child)
if os.path.isfile(myfile):
crc1 = crc(myfile)
crc_a.append(crc1)
file_a.append(child)
#print child,crc1
bufferline =[]
bufferYN = "False"
gameYN = "False"
f = open(outFile,'w')
gamename = ""
print "Reading "+mameFile
print "Writing "+outFile
with open(mameFile, 'r') as handle:
for index, line in enumerate(handle, 1):
if line.rstrip().startswith(crcString):
bufferYN = "True"
if gameYN == "False":
for lin in bufferline:
f.write(lin)
gameYN="False"
bufferline=[]
#print line
if line.rstrip().startswith(gameString):
ext = line.find(extString) + 4
gamename = line[15:ext]
#print gamename,file_a[2]
crcvalue = ""
if gamename in file_a:
num = file_a.index(gamename)
crcvalue = crc_a[num]
print crcvalue,gamename,num
bufferYN = "False"
gameYN = "True"
for lin in bufferline:
if lin.lstrip().startswith(crcString):
if gamename in file_a:
lin = lin.replace("\n",","+crcvalue+"\n")
f.write(lin)
#break
if bufferYN == "True":
bufferline.append(line)
else:
f.write(line)
f.close()
print "Finished."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment