Guest User

FC Quest Insert v1

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