Guest User

FC Quest Insert

a guest
Mar 11th, 2017
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import shutil
  3. import struct
  4. import textwrap
  5.  
  6. #Up to 50 characters used in original file (counting the star as 1 character)
  7. INPUT_COL = 3
  8. trans_tbl_fr = (('|',';'),
  9.                 ('é','<'),
  10.                 ('è','>'),
  11.                 ('à','^'),
  12.                 ('ê','_'),
  13.                 ('ù','+'),
  14.                 ('…','...'),
  15.                 ('ç','c'),
  16.                 ('â','a'),
  17.                 ('ä','a'),
  18.                 ('î','i'),
  19.                 ('ï','i'),
  20.                 ('ö','o'),
  21.                 ('ô','o'),
  22.                 ('ü','u'),
  23.                 ('û','u'),
  24.                 ('ë','e'),
  25.                 ('æ','ae'),
  26.                 ('œ','oe'))
  27. trans_tbl_fr = dict((ord(x[0]), x[1]) for x in trans_tbl_fr)
  28.  
  29. if os.path.isfile('T_QUEST.orig'):
  30.     pass
  31. elif os.path.isfile('T_QUEST._DT'):
  32.     shutil.copy('T_QUEST._DT', 'T_QUEST.orig')
  33. else:
  34.     print('Missing T_QUEST. Place in this tool\'s folder.')
  35.     quit()
  36. if not os.path.isfile('t_quest.tsv'):
  37.     print('Missing input file t_quest.tsv. Place in this tool\'s folder.')
  38.     quit()
  39.  
  40. inputdata = []
  41. with open('t_quest.tsv', 'r', encoding='utf-8') as f:
  42.     for line in f:
  43.         line = line.split('\t')
  44.         if len(line) < 3:
  45.             print('hit')
  46.             break
  47.         if line[0] != '':
  48.             inputdata.append([])
  49.         if line[1] != '':
  50.             inputdata[-1].append([line[INPUT_COL]])
  51.         else:
  52.             inputdata[-1][-1].append(line[INPUT_COL])
  53.  
  54. #First 0x12 bytes of each quest are unknown
  55. unknown_data = []
  56. with open('T_QUEST.orig', 'rb') as f:
  57.     filedata = f.read()
  58. ptr_start = 0x0
  59. ptr_end = 0xAE
  60. ptrs = tuple(struct.unpack(
  61.     '<H', filedata[x:x+2])[0] for x in range(ptr_start, ptr_end, 2))
  62. for ptr in reversed(ptrs):
  63.     unknown_data.append(filedata[ptr:ptr + 0x12])
  64.  
  65. filedata = bytearray(0xAE)
  66. header_ptrs = []
  67. for quest in inputdata:
  68.     header_ptrs.append(len(filedata))
  69.     filedata += unknown_data.pop()
  70.     quest_ptrs_pos = len(filedata)
  71.     quest_ptrs = []
  72.     filedata += bytearray(0x24)
  73.     for s_list in quest:
  74.         quest_ptrs.append(len(filedata))
  75.         s_out = []
  76.         for s in s_list:
  77.             s = s.translate(trans_tbl_fr)
  78.             if s == 'X':
  79.                 s_out.append('')
  80.                 continue
  81.             if s == '':
  82.                 continue
  83.             s_out.append('\x01 '.join(textwrap.wrap(s, 50)))
  84.         filedata += '\x01'.join(s_out).encode('cp932') + b'\x00'
  85.     filedata[quest_ptrs_pos:quest_ptrs_pos + 0x24] = (
  86.         struct.pack('<' + 'H' * 18, *quest_ptrs))
  87. filedata[:0xAE] = struct.pack('<' + 'H' * len(header_ptrs), *header_ptrs)
  88.  
  89. with open('T_QUEST._DT', 'wb') as f:
  90.     f.write(filedata)
RAW Paste Data