Advertisement
Guest User

Extract locres

a guest
Jul 4th, 2021
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.13 KB | None | 0 0
  1. import numpy
  2. import json
  3. import sys
  4.  
  5. if (len(sys.argv) <= 1): raise Warning("You need to provide filename of locres file")
  6.  
  7. locres = open(sys.argv[1], "rb")
  8.  
  9. Magic = locres.read(16)
  10.  
  11. if (Magic != b'\x0E\x14\x74\x75\x67\x4A\x03\xFC\x4A\x15\x90\x9D\xC3\x37\x7F\x1B'): raise Warning("Wrong locres magic! Either wrong file or old type.")
  12.  
  13. version = numpy.fromfile(locres, dtype=numpy.uint8, count=1)[0]
  14. break_block = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  15.  
  16. if (break_block <= 0): raise ValueError("Wrong break_block size. Value: %d" % (break_block))
  17.  
  18. if (version == 1):
  19.     RESERVED = locres.read(4)
  20.     unk = locres.read(4)
  21.     RESERVED = locres.read(4)
  22. elif (3 >= version >= 2):
  23.     RESERVED = locres.read(4)
  24.     Entry_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  25.     if (Entry_count <= 0): raise ValueError("Wrong Entry_count. Value: %d" % (Entry_count))
  26.     Category_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  27.     if (Entry_count <= 0): raise ValueError("Wrong Category_count. Value: %d" % (Category_count))
  28. else: raise ValueError("Unknown version: %d" % (version))
  29.  
  30. CategoryList = []
  31. CategoryOffset = []
  32. CategoryMagic = []
  33.  
  34. if (version == 1):
  35.     CategoryList.append("Unknown")
  36. elif (3 >= version >= 2):
  37.     CategoryMagic.append(locres.read(4))
  38.     category_string_size = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  39.     if (category_string_size < 0):
  40.         category_string = locres.read((0 - category_string_size) * 2)
  41.         CategoryList.append(category_string[:-2].decode(encoding='UTF-16-LE'))
  42.     elif (category_string_size > 1):
  43.         category_string = locres.read(0 + category_string_size)
  44.         CategoryList.append(category_string[:-1].decode(encoding='ascii'))
  45.     else:
  46.         category_string = locres.read(0 + category_string_size)
  47.         CategoryList.append("Unknown")  
  48.        
  49. TextList = []
  50. TextMagic = []
  51. TextMagic2 = []
  52. TextID = []
  53.  
  54. while(True):
  55.     block_count = numpy.fromfile(locres, dtype=numpy.uint32, count=1)[0]
  56.     for i in range(0, block_count):
  57.         if (version == 1):
  58.             string_character_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  59.             if (string_character_count < 0):
  60.                 string = locres.read((0 - string_character_count) * 2)
  61.                 TextList.append(string[:-2].decode(encoding='UTF-16-LE'))
  62.             else:
  63.                 string = locres.read(0 + string_character_count)
  64.                 TextList.append(string[:-1].decode(encoding='ascii'))
  65.             TextMagic.append(locres.read(4))
  66.             TextID.append(numpy.fromfile(locres, dtype=numpy.uint32, count=1)[0])
  67.         elif (3 >= version >= 2):
  68.             TextMagic.append(locres.read(4))
  69.             string_character_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  70.             if (string_character_count < 0):
  71.                 string = locres.read((0 - string_character_count) * 2)
  72.                 TextList.append(string[:-2].decode(encoding='UTF-16-LE'))
  73.             else:
  74.                 string = locres.read(0 + string_character_count)
  75.                 TextList.append(string[:-1].decode(encoding='ascii'))
  76.             TextMagic2.append(locres.read(4))
  77.             TextID.append(numpy.fromfile(locres, dtype=numpy.uint32, count=1)[0])
  78.     if (locres.tell() >= break_block):
  79.         CategoryOffset.append(len(TextList))
  80.         break
  81.     if (3 >= version >= 2): CategoryMagic.append(locres.read(4))
  82.     category_string_size = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  83.     if (category_string_size < 0):
  84.         category_string = locres.read((0 - category_string_size) * 2)
  85.         CategoryList.append(category_string[:-2].decode(encoding='UTF-16-LE'))
  86.     else:
  87.         category_string = locres.read(0 + category_string_size)
  88.         CategoryList.append(category_string[:-1].decode(encoding='ascii'))
  89.     CategoryOffset.append(len(TextList))        
  90.        
  91. if ((3 >= version >= 2)):
  92.     if (len(TextList) != Entry_count): raise ValueError("TextList count check failed. Expected: %d, counted: %d" % (Entry_count, len(TextList)))
  93.     elif (len(CategoryOffset) != Category_count): raise ValueError("CategoryOffset count check failed. Expected: %d, counted: %d" % (Category_count, len(CategoryOffset)))
  94.  
  95. Text_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  96.  
  97. TranslationList = []
  98.  
  99. for i in range(0, Text_count):
  100.         string_character_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  101.         if (string_character_count < 0):
  102.             string = locres.read((0 - string_character_count) * 2)
  103.             TranslationList.append(string[:-2].decode(encoding='UTF-16-LE'))
  104.             if (3 >= version >= 2): coding = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  105.         else:
  106.             string = locres.read(0 + string_character_count)
  107.             if (3 >= version >= 2):
  108.                 coding = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
  109.                 if (coding == 2): TranslationList.append(string[:-1].decode(encoding='iso-8859-1'))
  110.                 else: TranslationList.append(string[:-1].decode(encoding='ascii'))
  111.             else:
  112.                 TranslationList.append(string[:-1].decode(encoding='ascii'))
  113.        
  114.  
  115. locres.close()
  116.  
  117. data = {}
  118. data['Metadata'] = []
  119. data['Metadata'].append({
  120.     "Version": int(version)
  121.     })
  122.  
  123. data[CategoryList[0]] = []
  124.  
  125. x = 0
  126. while(CategoryOffset[0] > x):
  127.     if (version == 1):
  128.         data[CategoryList[0]].append({
  129.             "ID": TextList[x],
  130.             "MAGIC": "%s" % (TextMagic[x].hex()),
  131.             "TextID": int(TextID[x]),
  132.             "Text": TranslationList[TextID[x]]
  133.         })
  134.     elif (3 >= version >= 2):
  135.         data[CategoryList[0]].append({
  136.             "ID": TextList[x],
  137.             "MAGIC": "%s" % (TextMagic[x].hex()),
  138.             "MAGIC2": "%s" % (TextMagic2[x].hex()),
  139.             "TextID": int(TextID[x]),
  140.             "Text": TranslationList[TextID[x]]
  141.         })
  142.     x += 1
  143.  
  144. Flag2 = False
  145.  
  146. for i in range(1, len(CategoryList)):
  147.     data[CategoryList[i]] = []
  148.     while(x < CategoryOffset[i]):
  149.         if (version == 1):
  150.             data[CategoryList[i]].append({
  151.                 "ID": TextList[x],
  152.                 "MAGIC": "%s" % (TextMagic[x].hex()),
  153.                 "TextID": int(TextID[x]),
  154.                 "Text": TranslationList[TextID[x]]
  155.             })
  156.         if (3 >= version >= 2):
  157.             if (Flag2 == False):
  158.                 data[CategoryList[i]].append({
  159.                     "CategoryMagic": CategoryMagic[i].hex()
  160.                 })
  161.                 Flag2 = True
  162.             data[CategoryList[i]].append({
  163.                 "ID": TextList[x],
  164.                 "MAGIC": "%s" % (TextMagic[x].hex()),
  165.                 "MAGIC2": "%s" % (TextMagic2[x].hex()),
  166.                 "TextID": int(TextID[x]),
  167.                 "Text": TranslationList[TextID[x]]
  168.             })
  169.         x += 1
  170.     Flag2 = False
  171.  
  172. file = open("%s.json" % (sys.argv[1]), "w", encoding="UTF-8")
  173. json.dump(data, file, indent=4, ensure_ascii=False)
  174.  
  175. print("JSON dumped successfully")
  176. print("Unique texts: %d" % (len(TranslationList)))
  177. print("Entries: %d" % (len(TextList)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement