Advertisement
Guest User

Untitled

a guest
Jan 26th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from json import dump,load
  2. from struct import pack as compile_bytes, unpack, calcsize as struct_size
  3.  
  4. class RomStructure:
  5.     format = "<"
  6.     fields = "".split()
  7.     def __init__(self,data):
  8.         self.data=data
  9.     def compile(self):
  10.         return compile_bytes(self.format,*map(self.data.get,self.fields))
  11.     @classmethod
  12.     def size(self):return struct_size(self.format)
  13.     @classmethod
  14.     def decompile(self,bytes):
  15.         return dict(zip(self.fields,unpack(self.format,bytes)))
  16.     @classmethod
  17.     def load_from(self,source,location):
  18.         return self.decompile(source[location:location+self.size()])
  19.  
  20. class MapMetaStructure(RomStructure):
  21.     format = "<IIIIHHBBBBHBB"
  22.     fields =("map_data event_data map_scripts connection_header music index label "
  23.             +"flash_thing weather type unknown show_label battle_type").split()
  24.  
  25. class MapConnectionStructure(RomStructure):
  26.     format = "<IIBBH"
  27.     fields = "type offset bank number filler".split()
  28.  
  29. class MapConnectionHeader(RomStructure):
  30.     format = "<II"
  31.     fields = "size location".split()
  32.  
  33. if __name__=="__main__":
  34.     get_offset = lambda ptr:ptr&0x00FFFFFF
  35.     from sys import argv;argv=argv[1:]
  36.     from pprint import pprint
  37.     i,end = 0,len(argv)
  38.     while i<end:
  39.         arg,i = argv[i],i+1
  40.         if arg[arg.rfind("."):]==".gba":#found the Rom
  41.             try: bytes = open(arg,'rb').read()#read the bytess
  42.             except: raise
  43.             else:
  44.                 while i<end:
  45.                     arg,i = argv[i],i+1
  46.                     try:offset = get_offset(int(arg,16))
  47.                     except:raise
  48.                     else:
  49.                         data=MapMetaStructure.load_from(bytes,offset)
  50.                         print("Map@",hex(offset))
  51.                         for key,value in data.items():
  52.                             if(isinstance(value,int)):print(key,hex(value))
  53.                             else:print(key,value)
  54.                             if key=='connection_header':
  55.                                 connection_header = MapConnectionHeader.load_from(bytes,get_offset(value))
  56.                                 print(connection_header['size'],"connections @",connection_header['location'])
  57.                                 for ci in range(connection_header['size']):
  58.                                     offset = get_offset(connection_header['location']+ci*MapConnectionStructure.size())
  59.                                     map_connection = MapConnectionStructure.load_from(bytes,offset)
  60.                                     pprint(map_connection)
  61.                         print("\nRecompiling:")
  62.                         print(MapMetaStructure(data).compile())
  63.                         print("="*80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement