Advertisement
Tyler_Elric

my_reply.py

May 23rd, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. # Offset Organizer
  2. # Written by Full Metal
  3. # Feel free to modify
  4. # And redistribute.
  5.  
  6. import bottle
  7. import json
  8.  
  9. def safe_insert(l,i):
  10.     if i not in l:
  11.         l.append(i)
  12.         return len(l)-1
  13.     else:
  14.         for index,obj in enumerate(l):
  15.             if obj==i:return index
  16.     return -1
  17.  
  18. class Collection:
  19.  
  20.     store_name="offsets.json"
  21.  
  22.     def __init__(self):
  23.         try:
  24.             self.data=json.load(open(self.store_name))
  25.         except:
  26.             self.data={'roms':[],'offsets':[]}
  27.  
  28.     def __enter__(self):return self
  29.  
  30.     def __exit__(self, type, value, traceback):
  31.         json.dump(self.data,open(self.store_name,'w+'))
  32.  
  33.     def add_offset(self,offset,name,rom,length=4,type="pointer",desc=None,tags=None):
  34.         if isinstance(rom,str):
  35.             rom=rom.lower()
  36.             for i,obj in enumerate(self.data['roms']):
  37.                 if rom in obj['name'].lower():rom=i
  38.         self.data['offsets'].append({'rom':rom,'name':name,'desc':desc,'offset':offset,'type':type,'length':length,'tags':[a.lower() for a in tags] or []})
  39.  
  40.     def add_rom(self,name,desc,shortname=None,tags=None):
  41.         self.data['roms'].append({'name':name,'desc':desc,'shortname':shortname,'tags':[a.lower() for a in tags] or []})
  42.  
  43.     def get_roms(self):
  44.         for rom in self.data['roms']:
  45.             yield rom
  46.  
  47.     def get_offsets(self):
  48.         for offset in self.data['offsets']:
  49.             obj=offset.copy()
  50.             rom=self.data['roms'][obj['rom']]
  51.             obj['rom']=rom['name']
  52.             obj['tags'].extend(rom['tags'])
  53.             yield obj
  54.  
  55.     def search_for_offsets(self,tags):
  56.         tags=list(set([a.lower() for a in tags]))#Remove duplicate search terms.
  57.         results=[]
  58.         for offset in self.get_offsets():
  59.             num_matches=len([a for a in tags if a in offset['tags']])
  60.             if num_matches>0:
  61.                 results.append({'strength':num_matches,'type':'offset','object':offset})
  62.         return sorted(results,reverse=True,key=lambda obj:obj['strength'])
  63.  
  64.     def search_for_roms(self,tags):
  65.         tags=list(set([a.lower() for a in tags]))#Remove duplicate search terms.
  66.         results=[]
  67.         for rom in self.get_roms():
  68.             num_matches=len([a for a in tags if a in rom['tags']])
  69.             if num_matches>0:
  70.                 results.append({'strength':num_matches,'type':'rom','object':rom})
  71.         return sorted(results,reverse=True,key=lambda obj:obj['strength'])
  72.  
  73.     def search_all(self,tags):
  74.         res = []
  75.         res.extend(self.search_for_roms(tags))
  76.         res.extend(self.search_for_offsets(tags))
  77.         res=sorted(res,reverse=True,key=lambda obj:obj['strength'])
  78.         for r in res:print("Strength:",r['strength'],"type:",r['type'])
  79.         return res
  80.  
  81. def get_number(cmd_prompt="#> "):
  82.     base_prefix={'0x':16,'&h':16,'&b':2,'&o':8,'&d':10}
  83.     while True:
  84.         try:
  85.             r=input(cmd_prompt)
  86.             if r[:2] in base_prefix.keys():return int(r[2:],base_prefix[r[:2]])
  87.             return int(r)
  88.         except:continue
  89.  
  90. if __name__=="__main__":
  91.  
  92.     quit_words=['exit','quit','cancel','stop']
  93.  
  94.     with Collection() as collection:
  95.  
  96.         cmd=None
  97.         while True:
  98.  
  99.             cmd=input("> ").lower().split(' ') # Get each word.
  100.             if 'new' in cmd:
  101.                 if len(cmd)<2:
  102.                     cmd.extend(input("New what? > ").lower().split(' '))
  103.  
  104.                 if 'rom' in cmd[0]:
  105.                     con=True
  106.                     while con:
  107.                         collection.add_rom(input("Name > "),input("Description > "))
  108.                         con=input("Quit, or continue?").lower().split(' ')[0] not in quit_words
  109.  
  110.                 if 'offset' in cmd[0]:
  111.                     con=True
  112.                     while con:
  113.                         collection.add_offset(get_number("offset> "),input("Name> "),input("ROM> "))
  114.                         con=input("Quit, or continue?").lower().split(' ')[0] not in quit_words
  115.  
  116.             elif 'search' in cmd[0]:
  117.                 if len(cmd)<2:
  118.                     cmd.extend(input("Search for what? > ").lower().split(' '))
  119.                 for result in collection.search_all(cmd[1:]):
  120.                     if result['type']=='rom':
  121.                         obj=result['object']
  122.                         desc=obj['desc'] if obj['desc'] else "No description available."
  123.                         print("ROM   : {: >10}|{: >40}".format(obj['name'][:10],desc[:40]))
  124.                     elif result['type']=='offset':
  125.                         obj=result['object']
  126.                         desc=obj['desc'] if obj['desc'] else "No description available."
  127.                         print("Offset: {: >10}|{: >40} |{: >10}".format(obj['name'][:10],desc[:40],obj['rom'][:10]))
  128.  
  129.             elif 'tag' in cmd[0]:
  130.                 if len(cmd)<2:
  131.                     cmd.extend([a.lower for a in input("Tag what? > ").split(' ') if len(a)>0])
  132.                 if 'roms' in cmd:
  133.                     con=True
  134.                     while con:con=False
  135.                 if 'offsets' in cmd:
  136.                     con=True
  137.                     while con:con=False
  138.  
  139.             elif 'list' in cmd[0]:
  140.                 if len(cmd)<2:cmd.extend(input("List what? > ").lower().split(' '))
  141.                 if 'roms' in cmd:
  142.                     for rom in collection.get_roms():
  143.                         print("{}:\t{}".format(rom['name'],rom['desc']))
  144.                 if 'offsets' in cmd:
  145.                     print("{: ^15}|{: >15}|{: >10}|{: >10}| {: >6}|".format("Name","ROM","Location","Type","Length"))
  146.                     for offset in collection.get_offsets():
  147.                         print("{: ^15}|{: >15}|0x{:0>8X}|{: >10}| {: ^6}|".format(offset['name'][:15],offset['rom'][:15],offset['offset'],offset['type'],offset['length']))
  148.  
  149.             if len([w for w in cmd if w in quit_words])>0:
  150.                 break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement