Advertisement
Guest User

BR2 Dialogue Script Dumper v1

a guest
Jul 11th, 2015
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. import struct
  2. import os
  3. import sys
  4.  
  5. def get_data(filename):
  6.     with open(filename, 'rb') as f:
  7.         return f.read()
  8.  
  9. def is_jis_char(s):
  10.     try:
  11.         s.decode('cp932')
  12.     except UnicodeDecodeError:
  13.         return False
  14.     if len(s.decode('cp932')) == 1:
  15.         return True
  16.     return False
  17.  
  18. def script_unpack(filename):
  19.     filedata = get_data(filename)
  20.     num_of_records = struct.unpack('<H',filedata[:2])[0] / 2
  21.     pos = 0
  22.     ptrs = []
  23.     for i in range(num_of_records):
  24.         ptr = struct.unpack('<H',filedata[pos:pos+2])[0]
  25.         tmp_list = [x[1] for x in ptrs]
  26.         if ptr in tmp_list:
  27.     ##        print 'hit'
  28.             ptrs.append(('duplicate',tmp_list.index(ptr)))
  29.         else:
  30.             ptrs.append((i, ptr))
  31.         pos += 2
  32.     output = [filename + '\n']
  33.     for i, (num, ptr) in enumerate(ptrs):
  34.         if isinstance(num, basestring):
  35.             output.append(str(i) + '\tDuplicate of {}\n'.format(str(ptr)))
  36.             continue
  37.         output.append(str(i) + '\t')
  38.         pos = ptr
  39.         while True:
  40.             if is_jis_char(filedata[pos:pos+2]):
  41.                 output.append(filedata[pos:pos+2])
  42.     ##            print filedata[pos:pos+2].decode('cp932')
  43.                 pos += 2
  44.             elif (ord(filedata[pos]) > 0x19 and ord(filedata[pos]) < 0x80) or \
  45.                     (ord(filedata[pos]) > 0xa0 and ord(filedata[pos]) < 0xe0):
  46.                 output.append(filedata[pos])
  47.                 pos += 1
  48.             elif filedata[pos] == '\x0d':
  49.                 output.append('\tlinebreak\n\t')
  50.                 pos += 1
  51.             elif filedata[pos:pos+2] == '\x08\x0d':
  52.                 output.append('\tPTT\n\t')
  53.                 pos += 2
  54.             elif filedata[pos:pos+2] == '\x08\x03':
  55.                 output.append('\tpagebreak\n\t')
  56.                 pos += 2
  57.             elif filedata[pos] == '\x08':
  58.                 output.append('\tpause\n\t')
  59.                 pos += 1
  60.             elif filedata[pos] == '\x03':
  61.                 output.append('\tpagebreak2\n\t')
  62.                 pos += 1
  63.             elif filedata[pos:pos+2] == '\x00\x00':
  64.                 output.append('\tterminalcode\n\t')
  65.                 break
  66.             elif filedata[pos] == '\x00':
  67.                 output.append('\tnull\n\t')
  68.                 pos += 1
  69.             else:
  70.                 print i, hex(pos)
  71.                 quit()
  72.         output[-1] = output[-1][:-1]
  73.  
  74.     with open(os.path.splitext(filename)[0] + '.tsv', 'wb') as f:
  75.         f.write(''.join(output))
  76.  
  77. if __name__ == '__main__':
  78.     if len(sys.argv) < 2:
  79.         print ('Usage:\n'
  80.                'python script.py script_file_name_minus_extension')
  81.         quit()
  82. ##    For use with IDLE
  83. ##    sys.argv.append('TALK01')
  84.     script_unpack(sys.argv[1] + '.bzh')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement