Advertisement
Guest User

QB String Insert v1

a guest
Jan 1st, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.03 KB | None | 0 0
  1. import struct
  2. import zlib
  3. import os
  4. import shutil
  5.  
  6. INPUT_COL = 4
  7. if os.path.isfile('shared.orig'):
  8.     pass
  9. elif os.path.isfile('shared.bin'):
  10.     shutil.copy('shared.bin','shared.orig')
  11. else:
  12.     print('Missing shared.bin.')
  13.     quit()
  14.  
  15. s1 = " .,'!?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~-&>():;/{%"
  16. s2 = " .,’!?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~‐&>():;/\x1F%"
  17. translate_table = str.maketrans(s1, s2)    #For converting between ASCII and fullwidth SHIFT-JIS characters
  18.  
  19. inputdata = []
  20. with open('s2_new.tsv', 'r', encoding='utf-8') as f:
  21.     for line in f:
  22.         line = line.rstrip('\r\n').split('\t')
  23.         if line[0] != '':
  24.             Expl_index = int(line[0])
  25.             linenum = 0
  26.         else:
  27.             linenum += 1
  28.         while '"' in line[INPUT_COL]:
  29.             line[INPUT_COL] = line[INPUT_COL].replace('"', '“', 1)
  30.             line[INPUT_COL] = line[INPUT_COL].replace('"', '”', 1)
  31.         s = bytes(line[INPUT_COL].translate(translate_table), 'cp932') + b'\x00'
  32.         inputdata.append([Expl_index,
  33.                           linenum,
  34.                           s])
  35. inputdata = sorted(inputdata, key=lambda x: len(x[2]), reverse=True)
  36. strings = []
  37. count = 0
  38. num_p = 0
  39. ptrs = []
  40. null_ptr = 0
  41. ptrs.append(0)
  42. Sent = bytearray()
  43. for i, (Expl_index, linenum, new_s) in enumerate(inputdata):
  44.     if new_s == b'\x00':                #Special Case
  45.         inputdata[i].append(null_ptr)
  46.         continue
  47.     if new_s in Sent:
  48.         ptr_tgt = Sent.find(new_s)
  49.         if ptr_tgt in ptrs:             #string+ptr exist
  50.             inputdata[i].append(ptrs.index(ptr_tgt))
  51.         else:                           #string exist, no ptr
  52.             inputdata[i].append(len(ptrs))
  53.             ptrs.append(ptr_tgt)
  54.     else:                               #neither string nor ptr exist
  55.         inputdata[i].append(len(ptrs))
  56.         ptrs.append(len(Sent))
  57.         Sent += new_s
  58. ptrs = [x + 0xC + 4 * len(ptrs) for x in ptrs]
  59. ptrs[0] = 7     #For null pointer
  60.  
  61. Sent_len = 0xC + 4 * len(ptrs) + len(Sent)
  62. Sent = b''.join((b'Sent',
  63.                  struct.pack('<I', Sent_len),
  64.                  struct.pack('<I', len(ptrs)),
  65.                  b''.join([struct.pack('<I', x) for x in ptrs]),
  66.                  Sent))
  67. ##with open('Sent.bin', 'wb') as f:
  68. ##    f.write(Sent)
  69.  
  70. Expl_index_old = 0
  71. Expl = bytearray()
  72. Expl_data = []
  73. Expl_data_sub = []
  74. inputdata = sorted(sorted(inputdata, key=lambda x: x[1]), key=lambda x: x[0])
  75. for Expl_index, linenum, news, Sent_index in inputdata:
  76.     if Expl_index == Expl_index_old:
  77.         Expl_data_sub.append(Sent_index)
  78.     else:
  79.         Expl_data.append([len(Expl_data_sub)] + Expl_data_sub)
  80.         Expl_data_sub = []
  81.         Expl_index_old = Expl_index
  82.         Expl_data_sub.append(Sent_index)
  83. Expl_data.append([len(Expl_data_sub)] + Expl_data_sub)
  84. Expl = bytearray()
  85. ptrs = []
  86. for nums in Expl_data:
  87.     ptrs.append(len(Expl))
  88.     Expl += b''.join([struct.pack('<H', x) for x in nums])
  89. Expl = b''.join((b'Expl',
  90.                  struct.pack('<I', 0xC + 4 * len(ptrs) + len(Expl)),
  91.                  struct.pack('<I', len(Expl_data)),
  92.                  b''.join([struct.pack('<I', x + 0xC + 4 * len(ptrs)) for x in ptrs]),
  93.                  Expl))
  94. ##with open('Expl.bin', 'wb') as f:
  95. ##    f.write(Expl)
  96. #-----End of Sent/Expl
  97. inputdata = []
  98. linenum = 0
  99. INPUT_COL = 2
  100. with open('s1.tsv', 'r', encoding='utf-8') as f:
  101.     for line in f:
  102.         line = line.rstrip('\r\n').split('\t')
  103.         while '"' in line[INPUT_COL]:
  104.             line[INPUT_COL] = line[INPUT_COL].replace('"', '“', 1)
  105.             line[INPUT_COL] = line[INPUT_COL].replace('"', '”', 1)
  106.         if line[INPUT_COL] != '':
  107.             s = bytes(line[INPUT_COL].translate(translate_table), 'cp932') + b'\x00'
  108.             inputdata.append([linenum, s])
  109.             linenum += 1
  110. if linenum != 772:
  111.     print('Input error in s1. Must be exactly 772 strings.')
  112.     quit()
  113. inputdata = sorted(inputdata, key=lambda x: len(x[1]), reverse=True)
  114. Strg = bytearray()
  115. for i, (index, new_s) in enumerate(inputdata):
  116.     if new_s in Strg:
  117.         ptr_tgt = Strg.find(new_s)
  118.         inputdata[i].append(ptr_tgt)
  119.     else:
  120.         inputdata[i].append(len(Strg))
  121.         Strg += new_s
  122. inputdata = sorted(inputdata, key=lambda x: x[0])
  123. ptrs = [x[2] for x in inputdata]
  124. Strg = b''.join((b'Strg',
  125.                  struct.pack('<I', 0xC + 4 * linenum + len(Strg)),
  126.                  struct.pack('<I', linenum),
  127.                  b''.join([struct.pack('<I', x + 0xC + 4 * len(ptrs)) for x in ptrs]),
  128.                  Strg))
  129. ##with open('Strg.bin', 'wb') as f:
  130. ##    f.write(Strg)
  131.  
  132. with open('shared.orig','rb') as f:
  133.     f.seek(0x40800)
  134.     filedata = bytearray(zlib.decompress(f.read(0x87F1)))
  135. filedata = bytearray(filedata[:0xB520])
  136. for file, ptr_pos in ((Strg, 0x50),
  137.                       (Expl, 0x54),
  138.                       (Sent, 0x58)):
  139.     filedata[ptr_pos:ptr_pos+4] = struct.pack('<I', len(filedata))
  140.     filedata += file
  141.     if len(filedata) % 4 != 0:
  142.         filedata += b'\x00' * (4 - (len(filedata) % 4))
  143. ##with open('40800.uncompressed', 'wb') as f:
  144. ##    f.write(filedata)
  145. size = len(filedata)
  146. filedata = zlib.compress(filedata, 9)
  147. compressed_size = len(filedata)
  148. if compressed_size > 0x800 * 26:
  149.     print('Too big')
  150.     quit()
  151. tmpfilename = 'shared.tmp'
  152. shutil.copy('shared.bin', tmpfilename)
  153. with open(tmpfilename, 'rb') as f:
  154.     with open('shared.bin', 'wb') as g:
  155.         g.write(f.read(0x40800))
  156.         g.write(filedata)
  157.         g.write(b'\x00' * (0x4D800 - g.tell()))
  158.         f.seek(0x4D800)
  159.         g.write(f.read())
  160. os.remove(tmpfilename)
  161. with open('EBOOT.BIN', 'rb') as f:
  162.     filedata = bytearray(f.read())
  163. filedata[0x15DCA8:0x15DCAC] = struct.pack('<I', size)
  164. filedata[0x15DCB0:0x15DCB4] = struct.pack('<I', compressed_size)
  165. with open('EBOOT.BIN', 'wb') as f:
  166.     f.write(filedata)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement