Advertisement
ultiomos

datconv.py

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