Path: blob/master/libmupen64plus/mupen64plus-core/tools/cheat_code_convert.py
2 views
#!/usr/bin/python1'''* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *2* Mupen64plus - code_convert.c *3* Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *4* Copyright (C) 2010 Rhett Osborne *5* *6* This program is free software; you can redistribute it and/or modify *7* it under the terms of the GNU General Public License as published by *8* the Free Software Foundation; either version 2 of the License, or *9* (at your option) any later version. *10* *11* This program is distributed in the hope that it will be useful, *12* but WITHOUT ANY WARRANTY; without even the implied warranty of *13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *14* GNU General Public License for more details. *15* *16* You should have received a copy of the GNU General Public License *17* along with this program; if not, write to the *18* Free Software Foundation, Inc., *19* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *20* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *2122Usage:2324python code_convert.py > ../data/mupencheat.txt < ../data/mupen64plus.cht2526'''2728from sys import stdin2930class cheat:31def __init__(self):32self.n=""33self.d=""34self.c=[]35self.v=036self.hb='00'37def add(self, l):38if(self.n == ""):39return40l.append(" cn %s"%(self.n))41if(self.d != ""): l.append(" cd %s"%(self.d))42for code in self.c:43l.append(" "+code)44def clear(self):45self.n=""46self.d=""47self.c=[]48self.v=0495051l=[]52cCount=053_cs = []54for i in range(225):55_cs.append(cheat())56cs = _cs[:]5758def print_l():59global l, cs60for cheat in cs:61cheat.add(l)62for line in l:63print line.replace("\x00", "")64l=[]65cCount=066for i in range(225):67cs[i].clear()6869lines = stdin.read().split("\n")7071for line in lines:72if len(line) < 2: continue73elif(line[:2] == "//" and line != "//----" and line != "//---" ):74l.append(line)75elif len(line) < 4: continue76elif(line[0] == '[' and line[-1] == ']' and len(line) > 23):77print_l()78l.append("\ncrc %s" % line[1:-1])79elif(line[:5] == "Name="):80l.append("gn %s" % (line[5:]))81elif(line[:5] == "Cheat"):82t = line[5:].split('=')[0]83if (len(t)>1 and t[-2] == '_'):84n = int(t[:-2])85if(t[-1] == 'N'):86cs[n].d = line.split("=")[1]87else:88for option in line.split("=")[1].split("$")[1:]:89if(len(option) < 4):90break;91if(option[-1]==','): end =-192else: end = None93if(option[2] == " "):94cs[n].c[cs[n].v] += "%s%s:\"%s\""%(cs[n].hb,option[:2],option[3:end].replace("\"", "\\\""))95else:96cs[n].c[cs[n].v] += "%s:\"%s\""%(option[:4],option[5:end].replace("\"", "\\\""))97cs[n].c[cs[n].v]+=','98cs[n].c[cs[n].v] = cs[n].c[cs[n].v][:-1]99else:100n = int(t)101cn = line.split('"')102cs[n].c = cn[2][1:].split(',')103cs[n].n = cn[1];104i=0105for cheat in cs[n].c:106if(cheat[-1] == '?'):107if(cheat[-2:] == '??' and cheat[-4:-2] != '??'):108cs[n].hb = cheat[-4:-2]109else:110cs[n].hb = '00'111cs[n].c[i] = cheat[:9] + "???? ";112cs[n].v=i113i+=1114if(n > cCount):115cCount = n116elif(line != "//----" and line != "//---" ):117l.append("//%s" %line)118119120