Guest User

Last Ranker Script Insert

a guest
Jun 2nd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | None | 0 0
  1. import pdb
  2. import binascii
  3. import struct
  4. import io
  5. import os
  6. import pdb
  7. import shutil
  8. import gzip
  9.  
  10. def text_encode(s):
  11.     #Non-printing characters don't copy to/from Google Sheets correctly
  12.     #e.g., replace \x0c with {0c}
  13.     s1 = ''
  14.     flag = False
  15.     for char in s:
  16.         if ord(char) < 0x20 and ord(char) != 0xA:
  17.             if not flag:
  18.                 flag = True
  19.                 s1 += '{'
  20.             s1 += '{:0>2x}'.format(ord(char))
  21.         else:
  22.             if flag:
  23.                 flag = False
  24.                 s1 += '}'
  25.             s1 += char
  26.     return s1
  27.  
  28. def text_decode(s):
  29.     while '{' in s:
  30.         pos1 = s.find('{')
  31.         pos2 = s.find('}')
  32.         s = s[:pos1] + \
  33.             binascii.unhexlify(s[pos1+1:pos2].strip()).decode('ascii') + \
  34.             s[pos2+1:]
  35.     return s
  36.  
  37. def build_offset_list(s):
  38.     #Find offset of each string within string table
  39.     pos = 0
  40.     str_offset_list = []        
  41.     str_offset_list.append(0)
  42.     for i, x in enumerate(range(string_num - 1), 1):
  43.         pos = string.find(b'\x00', pos) + 1
  44.         str_offset_list.append(pos)
  45.     return(str_offset_list)
  46.  
  47. ##pdb.set_trace()
  48. filename = 'MAP_T_KAN_00'
  49. if os.path.isfile(filename + '.orig'):
  50.     with open(filename + '.orig', 'rb') as f:
  51.         filedata = f.read()
  52. else:
  53.     if os.path.isfile(filename + '.BIN'):
  54.         with gzip.open(filename + '.BIN') as f:
  55.             filedata = f.read()
  56.         with open(filename + '.orig', 'wb') as f:
  57.             f.write(filedata)
  58.         quit()
  59.     else:
  60.         print('File {} not found.'.format(filename + '.BIN'))
  61. bscr_offset = struct.unpack('<I', filedata[0xC:0x10])[0]
  62. bscrsize = struct.unpack('<I', filedata[bscr_offset+0x24:bscr_offset+0x28])[0]
  63. #Reduce down to bscr sub-file
  64. filedata = bytearray(filedata[bscr_offset:bscr_offset+bscrsize])    
  65. #------------------
  66. string_num, string_start, script_size, script_start = \
  67.             struct.unpack('<IIII', filedata[0x30:0x40])
  68. string = filedata[string_start:script_start]
  69. script = filedata[script_start:script_start+script_size]
  70. str_offset_list = build_offset_list(string)
  71. pos = script_start          #Find index number of each string within script
  72. count = 0
  73. scp_offset_list = []
  74. for x in range(script_size // 0x10):
  75.     if filedata[pos:pos+4] == b'\x1a\xf0\xf1\x5a':
  76.         count += 1
  77.         str_offset = struct.unpack('<I', filedata[pos+4:pos+8])[0]
  78.         #Where is the pointer, which string # is the pointer
  79.         scp_offset_list.append([pos + 4, str_offset_list.index(str_offset)])
  80.     pos += 0x10
  81. ##print(count)
  82. inputdata = []              #Load input data (from Google Sheets)
  83. with open(filename + '.tsv', 'r', encoding='utf-8') as f:
  84.     for line in f:
  85.         line = line.rstrip('\r\n').split('\t')
  86.         if line[0] != '':
  87.             inputdata.append(text_decode(line[2]).encode('utf-8'))
  88.         else:
  89.             inputdata[-1] += b'\n' + text_decode(line[2]).encode('utf-8')
  90. string = b'\x00'.join(inputdata) + b'\x00'
  91. if len(inputdata) > script_start - string_start:
  92.     print('Too long.')
  93.     quit()
  94. str_offset_list = build_offset_list(string)
  95. #Replace string data with new
  96. string += b'\x00' * (script_start - string_start - len(string))
  97. filedata[string_start:script_start] = string
  98. #Update pointers
  99. for scp_offset, i in scp_offset_list:
  100.     filedata[scp_offset:
  101.              scp_offset+4] = struct.pack('<I', str_offset_list[i])
  102. with open('bscr.bin', 'wb') as f:
  103.     f.write(filedata)
  104. data_temp = io.BytesIO()
  105. with open(filename + '.orig', 'rb') as g:
  106.     data_temp.write(g.read(bscr_offset))
  107.     data_temp.write(filedata)
  108.     g.seek(bscr_offset + bscrsize)
  109.     data_temp.write(g.read())
  110. with open(filename + '.BIN', 'wb') as f:
  111.     f.write(gzip.compress(data_temp.getvalue()))
Add Comment
Please, Sign In to add comment