Advertisement
Guest User

Untitled

a guest
Jan 26th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  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 decompile(self,bytes):
  13.         return dict(zip(self.fields,unpack(self.format,bytes)))
  14.     @classmethod
  15.     def load_from(self,source,location):
  16.         return self.decompile(source[location:location+struct_size(self.format)])
  17.  
  18. class MapMetaStructure(RomStructure):
  19.     format = "<IIIIHHBBBBHBB"
  20.     fields =("map_data event_data map_scripts connection_header music index label "
  21.             +"flash_thing weather type show_label battle_type").split()
  22.  
  23. class MapConnectionStructure:
  24.     format = "<IIBBH"
  25.     fields = "type offset bank number filler".split()
  26.  
  27. class MapConnectionHeader:
  28.     format = "<II"
  29.     fields = "size location".split()
  30.  
  31. if __name__=="__main__":
  32.     get_offset = lambda ptr:ptr&0x00FFFFFF
  33.     from sys import argv;argv=argv[1:]
  34.     from pprint import pprint
  35.     i,end = 0,len(argv)
  36.     while i<end:
  37.         arg,i = argv[i],i+1
  38.         if arg[arg.rfind("."):]==".gba":#found the Rom
  39.             try: bytes = open(arg,'rb').read()#read the bytess
  40.             except: raise
  41.             else:
  42.                 while i<end:
  43.                     arg,i = argv[i],i+1
  44.                     try:offset = get_offset(int(arg,16))
  45.                     except:raise
  46.                     else:
  47.                         data=MapMetaStructure.load_from(bytes,offset)
  48.                         print("Map@",hex(offset))
  49.                         for key,value in data.items():
  50.                             if(isinstance(value,int)):print(key,hex(value))
  51.                             else:print(key,value)
  52.                         print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement