-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsrm-to-sav.py
executable file
·58 lines (47 loc) · 1.29 KB
/
srm-to-sav.py
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
#!/usr/bin/python
import sys, getopt
def usage():
print 'Usage: srm-to-sav.py -i <input.srm> -o <output.sav> [--byteswap]'
print 'Converts RetroArch SRM files to raw GBA SAV format'
def main(argv):
inputfile = None
outputfile = None
byteswap = None
try:
opts, args = getopt.getopt(argv,"hi:o:",["input=","output=","byteswap"])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt in ("-i", "--input"):
inputfile = arg
elif opt in ("-o", "--output"):
outputfile = arg
elif opt == "--byteswap":
byteswap = True
if (not inputfile or not outputfile):
usage()
sys.exit(2)
with open(inputfile, "rb") as infile, open(outputfile, "wb") as outfile:
byte = infile.read(1)
while ord(byte) == 255:
byte = infile.read(1)
data = byte + infile.read()
if byteswap:
for i in xrange(len(data) / 8):
outfile.write(data[i*8+7])
outfile.write(data[i*8+6])
outfile.write(data[i*8+5])
outfile.write(data[i*8+4])
outfile.write(data[i*8+3])
outfile.write(data[i*8+2])
outfile.write(data[i*8+1])
outfile.write(data[i*8])
else:
outfile.write(data)
print "Extracted", len(data), "bytes from", inputfile, "to", outputfile
if __name__ == "__main__":
main(sys.argv[1:])