Advertisement
Guest User

Untitled

a guest
Mar 17th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.60 KB | None | 0 0
  1. import os
  2. import sys
  3. import binascii
  4. import struct
  5. import re
  6.  
  7. def get_data(filename):
  8. totalbytes = os.path.getsize(filename)
  9. infile = open(filename, 'rb')
  10. totalfiledata = infile.read(totalbytes)
  11. return totalfiledata
  12.  
  13. def extract(filename):
  14. filedata = get_data(filename)
  15. filedata = filedata[struct.unpack('<I',filedata[0x2c:0x30])[0]:]
  16. firsttablecount = struct.unpack('<I',filedata[0x8:0xc])[0]
  17. strentrycount = struct.unpack('<I',filedata[0xc:0x10])[0]
  18. strentrypos = (firsttablecount*0x10) + 0x20
  19. stringpos = (firsttablecount*0x10) + (strentrycount*0x20) + 0x20
  20. newfiledata = ''
  21. a = 0
  22. newstringcount = 0
  23. for entry in range(strentrypos,strentrypos+(strentrycount*0x20),0x20):
  24. currstringpos = struct.unpack('<I',filedata[entry+0x14:entry+0x18])[0]
  25. if entry+0x20 != strentrypos+(strentrycount*0x20):
  26. nextstringpos = struct.unpack('<I',filedata[entry+0x34:entry+0x38])[0]
  27. stringlen = nextstringpos-currstringpos
  28. string = filedata[currstringpos:currstringpos+stringlen-1]
  29.  
  30. while string.find('\xFF\x41') != -1 or string.find('\xFF\x42') != -1 or string.find('\xFF\x43') != -1 or string.find('\xFF\x44') != -1:
  31. startfuri = string.find('\xFF\x41')
  32. if startfuri == -1:
  33. startfuri = string.find('\xFF\x42')
  34. if startfuri == -1:
  35. startfuri = string.find('\xFF\x43')
  36. if startfuri == -1:
  37. startfuri = string.find('\xFF\x44')
  38. if startfuri == -1:
  39. print 'Start furi going further than fucking 44'
  40. endfuri = string.find('\xFF\x00',startfuri)
  41. if startfuri == -1 or endfuri == -1:
  42. print 'think we hit a bad bit in %x' % currstringpos
  43. break
  44. string = string[:startfuri] + string[endfuri+2:]
  45. while string.find('\xFF\x80') != -1:
  46. string = string.replace('\xFF\x80','\line')
  47. if a >= 10:
  48. print 'replace broke, breaking'
  49. a = 0
  50. break
  51. a += 1
  52. else:
  53. string = filedata[currstringpos:len(filedata)-4]
  54. while string.find('\xFF\x41') != -1 or string.find('\xFF\x42') != -1 or string.find('\xFF\x43') != -1 or string.find('\xFF\x44') != -1:
  55. startfuri = string.find('\xFF\x41')
  56. if startfuri == -1:
  57. startfuri = string.find('\xFF\x42')
  58. if startfuri == -1:
  59. startfuri = string.find('\xFF\x43')
  60. if startfuri == -1:
  61. startfuri = string.find('\xFF\x44')
  62. if startfuri == -1:
  63. print 'Start furi going further than fucking 44'
  64. endfuri = string.find('\xFF\x00',startfuri)
  65. if startfuri == -1 or endfuri == -1:
  66. print 'think we hit a bad bit in %x' % currstringpos
  67. break
  68. string = string[:startfuri] + string[endfuri+2:]
  69. while string.find('\xFF\x80') != -1:
  70. string = string.replace('\xFF\x80','\line')
  71. if a >= 10:
  72. print 'replace broke, breaking'
  73. a = 0
  74. break
  75. a += 1
  76. a = 0
  77. newfiledata += string + '\n'
  78. newstringcount += 1
  79. newfiledata = 'pBin\n' + str(newstringcount) + '\n' + newfiledata
  80.  
  81. outfile = open(filename + '.strings','wb')
  82. outfile.write(newfiledata)
  83. outfile.close()
  84.  
  85. def runcompile(filename):
  86. filedata = get_data(filename)
  87. origdata1 = get_data(filename[:len(filename)-8])
  88. origdata = origdata1[struct.unpack('<I',origdata1[0x2c:0x30])[0]:]
  89. strings = filedata.split('\n')
  90. newfiledata = origdata[:(struct.unpack('<I',origdata[0x8:0xc])[0] * 0x10) + 0x20]
  91. currpos = len(newfiledata)
  92. firstrun = True
  93. currstringsize = 0
  94. newstrings = ''
  95. newstringcount = 0
  96. oldstringcount = int(strings[1])
  97. strings.pop(0)
  98. strings.pop(0)
  99. string2 = ''
  100. temppos = len(newfiledata)
  101. tempposscene = 0x20
  102. linesmade = 0
  103. totalstringlen = int(len(strings)-1)
  104. totalstringlen = (struct.unpack('<I',origdata[0x8:0xc])[0] * 0x10) + (totalstringlen*0x20) + 0x20
  105. LastString = False
  106. for string in strings:
  107. if string == '':
  108. continue
  109. if string.find('\line') != -1:
  110. string = string.replace('\line','\xFF\x80')
  111. if string.find('##') != -1:
  112. string = string.replace('##','')
  113.  
  114. stringlist = string.split(' ')
  115. for word in stringlist:
  116. if (currstringsize + len(word)) >= 30:
  117. if linesmade == 1:
  118. string2 = string2 + '\page' + word
  119. totalstringlen += 0x20
  120. linesmade = 0
  121. else:
  122. string2 = string2 + '\xFF\x80' + word
  123. linesmade += 1
  124. currstringsize = 0 + len(word)
  125. else:
  126. if firstrun == True:
  127. string2 = word
  128. currstringsize = len(word)
  129. firstrun = False
  130. else:
  131. string2 = string2 + ' ' + word
  132. currstringsize += len(word) + 1
  133. string = string2
  134. if string.find('\page') != -1:
  135. newpagestrings = string.split('\page')
  136. pagesadded = 0
  137.  
  138. for newpagestring in newpagestrings:
  139. if newstringcount == 0 or newpagestring == newpagestrings[0]:
  140. newfiledata += origdata[currpos:currpos+0x14] + struct.pack('<I',totalstringlen) + origdata[currpos+0x18:currpos+0x20]
  141. currpos += 0x20
  142. else:
  143. newfiledata += origdata[currpos-0x20:currpos-0xc] + struct.pack('<I',totalstringlen) + origdata[currpos-0x8:currpos-0x4] + struct.pack('<I',4294967295)
  144. for fixstring in range(0,newstringcount-1): #fix string locations as we add new pages
  145. fixvar = struct.unpack('<I',newfiledata[temppos+0x14:temppos+0x18])[0]
  146. fixvar += 0x20
  147. newfiledata = newfiledata[:temppos+0x14] + struct.pack('<I',fixvar) + newfiledata[temppos+0x18:]
  148. temppos += 0x20
  149. if pagesadded >= 1:
  150. for findscene in range(0x20,(struct.unpack('<I',newfiledata[0x8:0xc])[0] * 0x10) + 0x20,0x10):
  151. #find the scene location we need to add new strings to
  152. findscenenum = struct.unpack('<I',newfiledata[findscene:findscene+0x4])[0]
  153. if findscenenum >= newstringcount:
  154. scenepos = findscene
  155. scenepos -= 0x10
  156. amounttofix = (struct.unpack('<I',newfiledata[0x8:0xc])[0]*0x10) - ((scenepos)-0x20)
  157. break
  158. if scenepos == 0:
  159. scenepos = (struct.unpack('<I',newfiledata[0x8:0xc])[0]*0x10) + 0x10
  160. amounttofix = (struct.unpack('<I',newfiledata[0x8:0xc])[0]*0x10) - ((scenepos)-0x20)
  161. for fixsceneloc in range(scenepos,amounttofix+scenepos,0x10): #fix scene change stuff too
  162. if fixsceneloc == scenepos:
  163. fixvar = struct.unpack('<I',newfiledata[fixsceneloc+0x4:fixsceneloc+0x8])[0]
  164. fixvar += 0x1
  165. newfiledata = newfiledata[:fixsceneloc+0x4] + struct.pack('<I',fixvar) + newfiledata[fixsceneloc+0x8:]
  166. else:
  167. fixvar = struct.unpack('<I',newfiledata[fixsceneloc:fixsceneloc+0x4])[0]
  168. fixvar += 0x1
  169. newfiledata = newfiledata[:fixsceneloc] + struct.pack('<I',fixvar) + newfiledata[fixsceneloc+0x4:]
  170.  
  171. pagesadded += 1
  172. fixvar = 0
  173. totalstringlen += len(newpagestring)+1
  174. newstrings += newpagestring + '\x00'
  175. newstringcount += 1
  176. temppos = (struct.unpack('<I',origdata[0x8:0xc])[0] * 0x10) + 0x20
  177. LastString = False
  178. scenepos = 0
  179. amounttofix = 0
  180. else: #no new pages, just add the string
  181. newfiledata += origdata[currpos:currpos+0x14] + struct.pack('<I',totalstringlen) + origdata[currpos+0x18:currpos+0x20]
  182. currpos += 0x20
  183. totalstringlen += len(string) + 1
  184. newstrings += string + '\x00'
  185. newstringcount += 1
  186.  
  187. firstrun = True
  188. currstringsize = 0
  189. linesmade = 0
  190. string2 = ''
  191. currstringsize = 0
  192. stringlist = []
  193. newpagestrings = []
  194.  
  195. newfiledata += newstrings + '\x00\x00\x00'
  196. newfiledata = newfiledata[:0xc] + struct.pack('<I',newstringcount) + newfiledata[0x10:]
  197. replacepos = struct.unpack('<I',origdata1[0x2c:0x30])[0]
  198. newfinalfile = origdata1[:replacepos] + newfiledata
  199.  
  200. outfile = open(filename[:len(filename)-8] + '.new','wb')
  201. outfile.write(newfinalfile)
  202. outfile.close()
  203.  
  204. def texe(filename):
  205. filedata = get_data(filename)
  206. stringcount = struct.unpack('<I',filedata[0x8:0xc])[0]
  207. newfiledata = ''
  208. a = 0
  209. for stringentry in range(0x10,(stringcount*0x4)+0x10,0x4):
  210. stringloc = struct.unpack('<I',filedata[stringentry:stringentry+0x4])[0]
  211. print "0x%0x" % stringloc
  212. if stringentry+0x4 == stringcount*0x4+0x10:
  213. nextstringpos = len(filedata)
  214. else:
  215. nextstringpos = struct.unpack('<I',filedata[stringentry+0x4:stringentry+0x8])[0]
  216. string = filedata[stringloc:nextstringpos-1]
  217. while string.find('\x0a') != -1:
  218. string = string.replace('\x0a','\line')
  219. if a >= 50:
  220. print 'replace broke, breaking'
  221. break
  222. a += 1
  223. a = 0
  224. newfiledata += string + '\n'
  225.  
  226. outfile = open(filename + '.strings','wb')
  227. outfile.write(newfiledata)
  228. outfile.close()
  229.  
  230. def texc(filename):
  231. filedata = get_data(filename)
  232. origfiledata = get_data(filename[:len(filename)-8])
  233. newfiledata = ''
  234. newfiledata = origfiledata[:0x10]
  235. strings = filedata.split('\n')
  236. currpos = 0x10
  237. totalstringlen = (struct.unpack('<I',origfiledata[0x8:0xc])[0] * 0x4) + 0x10
  238. newstrings = ''
  239. for string in strings:
  240. if string == '':
  241. continue
  242. if string.find('\line') != -1:
  243. string = string.replace('\line','\x0a')
  244. string += '\x00'
  245. newfiledata += struct.pack('<I',totalstringlen)
  246. newstrings += string
  247. currpos += 0x4
  248. totalstringlen += len(string)
  249.  
  250. newfiledata += newstrings
  251.  
  252. outfile = open(filename[:len(filename)-8] + '.new','wb')
  253. outfile.write(newfiledata)
  254. outfile.close()
  255.  
  256. if __name__ == '__main__':
  257. if sys.argv[1] == '-e':
  258. testfile = get_data(sys.argv[2])
  259. if testfile[:0x4] == 'pBin':
  260. extract(sys.argv[2])
  261. else:
  262. texe(sys.argv[2])
  263. elif sys.argv[1] == '-c':
  264. testfile = get_data(sys.argv[2])
  265. if testfile[:0x4] == 'pBin':
  266.  
  267. runcompile(sys.argv[2])
  268. else:
  269. texc(sys.argv[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement