Advertisement
TerusTheBird

Pokemon hex data to binary converter

Apr 26th, 2020
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. #!/bin/python
  2. # coding: utf-8
  3. # datconv.py
  4.  
  5. import sys, re, argparse
  6.  
  7. def str2byt(x):
  8.     done = False
  9.     out = b''
  10.     byte = 0
  11.     for nybble in x:
  12.         if not done:
  13.             byte = int( nybble, 16 )
  14.             byte <<= 4
  15.         else:
  16.             byte |= ( int( nybble, 16 ) & 0xF )
  17.             out = out + bytes([byte])
  18.         done = not done
  19.     return out
  20.  
  21. hx1 = lambda n: hex(n)[2:].zfill(2)
  22.  
  23. REGEX_DEC = re.compile(r'^  d[bw]   ((?:[0-9]{3},?)+)\s*(?:;.*)?$')
  24. REGEX_HEX = re.compile(r'^  d[bw]   ((?:0(?:[A-Fa-f0-9]{2})+h,?)+)\s*(?:;.*)?$')
  25.  
  26. def divhx(x):
  27.     pcs = []
  28.     for i in range(len(x) // 2):
  29.         pcs.append(x[i*2:i*2+2])
  30.     return ''.join(pcs[::-1])
  31.    
  32. def divhxl(x):
  33.     out = ''
  34.     for k in x:
  35.         out += divhx(k)
  36.     return out
  37.  
  38. def Main(args):
  39.     txt = open(args.filein, 'r').read()
  40.     v = not args.quiet
  41.     lins = txt.split('\n')
  42.     if args.guess:
  43.         r1 = REGEX_DEC
  44.         r2 = REGEX_HEX
  45.         for i in range(len(lins)):
  46.             if r1.match(lins[i]):
  47.                 args.decimal = True
  48.                 break
  49.             elif r2.match(lins[i]):
  50.                 args.decimal = False
  51.                 break
  52.         else:
  53.             raise Exception
  54.     if args.decimal:
  55.         reg = REGEX_DEC
  56.     else:
  57.         reg = REGEX_HEX
  58.     datout = ''
  59.     uuct = 0
  60.     for i,l in enumerate(lins):
  61.         mm = reg.match(l)
  62.         if mm:
  63.             cur_dats = mm.groups()[0].split(',')
  64.             if args.decimal:
  65.                 cur_dats_2 = [hx1(int(x[1:-1])) for x in cur_dats]
  66.             else:
  67.                 cur_dats_2 = [x[1:-1] for x in cur_dats]
  68.             datout += ''.join(cur_dats_2)
  69.         else:
  70.             if v:
  71.                 print('Line does not match pattern:')
  72.                 print(i+1, l)
  73.                 uuct += 1
  74.                 if uuct > 10:
  75.                     input()
  76.                     uuct = 0
  77.                    
  78.     open(args.fileout, 'wb').write(str2byt(datout))
  79.  
  80. if __name__ == '__main__':
  81.     parser = argparse.ArgumentParser(description='converts pokesource data to bin')
  82.     parser.add_argument('filein', type=str, help='file in')
  83.     parser.add_argument('fileout', type=str, help='file out')
  84.     parser.add_argument('-q', dest='quiet', action='store_true', help='quiet')
  85.     parser.add_argument('-d', dest='decimal', action='store_true', help='data is decimal')
  86.     parser.add_argument('-g', dest='guess', action='store_true', help='guess hex or dec')
  87.    
  88.     args = parser.parse_args()
  89.    
  90.     Main(args)
  91. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement