Advertisement
Guest User

QB String Insert v2

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