forked from libretro/libretro-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cht64write.py
67 lines (54 loc) · 1.77 KB
/
cht64write.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
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
# public domain by @bparker06
# https://github.com/libretro/libretro-database/issues/388
# https://raw.githubusercontent.com/mupen64plus/mupen64plus-core/master/data/mupencheat.txt
import re, os
cheatfile = ''
cheats = ''
game = ''
gamefile = None
gamefilename = ''
cheatnum = -1
subcheatnum = 0
def slugify(value):
# ripped off from Django and modified
import unicodedata
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
value = unicode(re.sub('[^\w\s\-\.\(\)]', '', value).strip())
return value
with open('mupencheat.txt', 'rb') as f:
cheatfile = f.read()
for line in cheatfile.split('\n'):
if line.startswith('gn '):
if gamefile is not None:
if cheatnum >= 0:
gamefile.write('cheats = ' + str(cheatnum + 1) + '\n')
gamefile.write(cheats + '\"\n')
gamefile.close()
gamefile = None
subcheatnum = 0
cheats = ''
if cheatnum < 0:
os.unlink(gamefilename)
cheatnum = -1
game = line.split('gn ')[1]
game = re.sub(r'^[\s*|=]', '', game)
gamefilename = 'cheats/' + str(slugify(unicode(game))) + '.cht'
gamefile = open(gamefilename, 'wb')
elif line.startswith(' cn '):
name = line.split(' cn ')[1]
cheatnum += 1
if subcheatnum > 0:
cheats += '\"\n'
subcheatnum = 0
cheats += 'cheat' + str(cheatnum) + '_desc = \"' + name + '\"\n'
cheats += 'cheat' + str(cheatnum) + '_enable = false\n'
elif line.startswith(' ') and not line.startswith(' cd '):
cheat = line.split(' ')[1]
cheat = re.sub(r'(\?\?\?\?).*', '\\1', cheat)
cheat = re.sub(r'\?\?\?\?', 'XXXX', cheat)
if subcheatnum == 0:
cheats += 'cheat' + str(cheatnum) + '_code = \"' + cheat
else:
cheats += ';' + cheat
subcheatnum += 1