|
| 1 | +#!/usr/bin/python |
| 2 | +# public domain by @bparker06 |
| 3 | +# https://github.com/libretro/libretro-database/issues/388 |
| 4 | +# https://raw.githubusercontent.com/mupen64plus/mupen64plus-core/master/data/mupencheat.txt |
| 5 | +import re, os |
| 6 | + |
| 7 | +cheatfile = '' |
| 8 | +cheats = '' |
| 9 | +game = '' |
| 10 | +gamefile = None |
| 11 | +gamefilename = '' |
| 12 | +cheatnum = -1 |
| 13 | +subcheatnum = 0 |
| 14 | + |
| 15 | +def slugify(value): |
| 16 | + # ripped off from Django and modified |
| 17 | + import unicodedata |
| 18 | + value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') |
| 19 | + value = unicode(re.sub('[^\w\s\-\.\(\)]', '', value).strip()) |
| 20 | + |
| 21 | + return value |
| 22 | + |
| 23 | +with open('mupencheat.txt', 'rb') as f: |
| 24 | + cheatfile = f.read() |
| 25 | + |
| 26 | +for line in cheatfile.split('\n'): |
| 27 | + if line.startswith('gn '): |
| 28 | + if gamefile is not None: |
| 29 | + if cheatnum >= 0: |
| 30 | + gamefile.write('cheats = ' + str(cheatnum + 1) + '\n') |
| 31 | + gamefile.write(cheats + '\"\n') |
| 32 | + |
| 33 | + gamefile.close() |
| 34 | + gamefile = None |
| 35 | + subcheatnum = 0 |
| 36 | + cheats = '' |
| 37 | + |
| 38 | + if cheatnum < 0: |
| 39 | + os.unlink(gamefilename) |
| 40 | + |
| 41 | + cheatnum = -1 |
| 42 | + |
| 43 | + game = line.split('gn ')[1] |
| 44 | + game = re.sub(r'^[\s*|=]', '', game) |
| 45 | + gamefilename = 'cheats/' + str(slugify(unicode(game))) + '.cht' |
| 46 | + gamefile = open(gamefilename, 'wb') |
| 47 | + elif line.startswith(' cn '): |
| 48 | + name = line.split(' cn ')[1] |
| 49 | + cheatnum += 1 |
| 50 | + |
| 51 | + if subcheatnum > 0: |
| 52 | + cheats += '\"\n' |
| 53 | + subcheatnum = 0 |
| 54 | + |
| 55 | + cheats += 'cheat' + str(cheatnum) + '_desc = \"' + name + '\"\n' |
| 56 | + cheats += 'cheat' + str(cheatnum) + '_enable = false\n' |
| 57 | + elif line.startswith(' ') and not line.startswith(' cd '): |
| 58 | + cheat = line.split(' ')[1] |
| 59 | + cheat = re.sub(r'(\?\?\?\?).*', '\\1', cheat) |
| 60 | + cheat = re.sub(r'\?\?\?\?', 'XXXX', cheat) |
| 61 | + |
| 62 | + if subcheatnum == 0: |
| 63 | + cheats += 'cheat' + str(cheatnum) + '_code = \"' + cheat |
| 64 | + else: |
| 65 | + cheats += ';' + cheat |
| 66 | + |
| 67 | + subcheatnum += 1 |
0 commit comments