Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- import json
- import sys
- if (len(sys.argv) <= 1): raise Warning("You need to provide filename of locres file")
- locres = open(sys.argv[1], "rb")
- Magic = locres.read(16)
- 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.")
- version = numpy.fromfile(locres, dtype=numpy.uint8, count=1)[0]
- break_block = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (break_block <= 0): raise ValueError("Wrong break_block size. Value: %d" % (break_block))
- if (version == 1):
- RESERVED = locres.read(4)
- unk = locres.read(4)
- RESERVED = locres.read(4)
- elif (3 >= version >= 2):
- RESERVED = locres.read(4)
- Entry_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (Entry_count <= 0): raise ValueError("Wrong Entry_count. Value: %d" % (Entry_count))
- Category_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (Entry_count <= 0): raise ValueError("Wrong Category_count. Value: %d" % (Category_count))
- else: raise ValueError("Unknown version: %d" % (version))
- CategoryList = []
- CategoryOffset = []
- CategoryMagic = []
- if (version == 1):
- CategoryList.append("Unknown")
- elif (3 >= version >= 2):
- CategoryMagic.append(locres.read(4))
- category_string_size = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (category_string_size < 0):
- category_string = locres.read((0 - category_string_size) * 2)
- CategoryList.append(category_string[:-2].decode(encoding='UTF-16-LE'))
- elif (category_string_size > 1):
- category_string = locres.read(0 + category_string_size)
- CategoryList.append(category_string[:-1].decode(encoding='ascii'))
- else:
- category_string = locres.read(0 + category_string_size)
- CategoryList.append("Unknown")
- TextList = []
- TextMagic = []
- TextMagic2 = []
- TextID = []
- while(True):
- block_count = numpy.fromfile(locres, dtype=numpy.uint32, count=1)[0]
- for i in range(0, block_count):
- if (version == 1):
- string_character_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (string_character_count < 0):
- string = locres.read((0 - string_character_count) * 2)
- TextList.append(string[:-2].decode(encoding='UTF-16-LE'))
- else:
- string = locres.read(0 + string_character_count)
- TextList.append(string[:-1].decode(encoding='ascii'))
- TextMagic.append(locres.read(4))
- TextID.append(numpy.fromfile(locres, dtype=numpy.uint32, count=1)[0])
- elif (3 >= version >= 2):
- TextMagic.append(locres.read(4))
- string_character_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (string_character_count < 0):
- string = locres.read((0 - string_character_count) * 2)
- TextList.append(string[:-2].decode(encoding='UTF-16-LE'))
- else:
- string = locres.read(0 + string_character_count)
- TextList.append(string[:-1].decode(encoding='ascii'))
- TextMagic2.append(locres.read(4))
- TextID.append(numpy.fromfile(locres, dtype=numpy.uint32, count=1)[0])
- if (locres.tell() >= break_block):
- CategoryOffset.append(len(TextList))
- break
- if (3 >= version >= 2): CategoryMagic.append(locres.read(4))
- category_string_size = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (category_string_size < 0):
- category_string = locres.read((0 - category_string_size) * 2)
- CategoryList.append(category_string[:-2].decode(encoding='UTF-16-LE'))
- else:
- category_string = locres.read(0 + category_string_size)
- CategoryList.append(category_string[:-1].decode(encoding='ascii'))
- CategoryOffset.append(len(TextList))
- if ((3 >= version >= 2)):
- if (len(TextList) != Entry_count): raise ValueError("TextList count check failed. Expected: %d, counted: %d" % (Entry_count, len(TextList)))
- elif (len(CategoryOffset) != Category_count): raise ValueError("CategoryOffset count check failed. Expected: %d, counted: %d" % (Category_count, len(CategoryOffset)))
- Text_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- TranslationList = []
- for i in range(0, Text_count):
- string_character_count = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (string_character_count < 0):
- string = locres.read((0 - string_character_count) * 2)
- TranslationList.append(string[:-2].decode(encoding='UTF-16-LE'))
- if (3 >= version >= 2): coding = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- else:
- string = locres.read(0 + string_character_count)
- if (3 >= version >= 2):
- coding = numpy.fromfile(locres, dtype=numpy.int32, count=1)[0]
- if (coding == 2): TranslationList.append(string[:-1].decode(encoding='iso-8859-1'))
- else: TranslationList.append(string[:-1].decode(encoding='ascii'))
- else:
- TranslationList.append(string[:-1].decode(encoding='ascii'))
- locres.close()
- data = {}
- data['Metadata'] = []
- data['Metadata'].append({
- "Version": int(version)
- })
- data[CategoryList[0]] = []
- x = 0
- while(CategoryOffset[0] > x):
- if (version == 1):
- data[CategoryList[0]].append({
- "ID": TextList[x],
- "MAGIC": "%s" % (TextMagic[x].hex()),
- "TextID": int(TextID[x]),
- "Text": TranslationList[TextID[x]]
- })
- elif (3 >= version >= 2):
- data[CategoryList[0]].append({
- "ID": TextList[x],
- "MAGIC": "%s" % (TextMagic[x].hex()),
- "MAGIC2": "%s" % (TextMagic2[x].hex()),
- "TextID": int(TextID[x]),
- "Text": TranslationList[TextID[x]]
- })
- x += 1
- Flag2 = False
- for i in range(1, len(CategoryList)):
- data[CategoryList[i]] = []
- while(x < CategoryOffset[i]):
- if (version == 1):
- data[CategoryList[i]].append({
- "ID": TextList[x],
- "MAGIC": "%s" % (TextMagic[x].hex()),
- "TextID": int(TextID[x]),
- "Text": TranslationList[TextID[x]]
- })
- if (3 >= version >= 2):
- if (Flag2 == False):
- data[CategoryList[i]].append({
- "CategoryMagic": CategoryMagic[i].hex()
- })
- Flag2 = True
- data[CategoryList[i]].append({
- "ID": TextList[x],
- "MAGIC": "%s" % (TextMagic[x].hex()),
- "MAGIC2": "%s" % (TextMagic2[x].hex()),
- "TextID": int(TextID[x]),
- "Text": TranslationList[TextID[x]]
- })
- x += 1
- Flag2 = False
- file = open("%s.json" % (sys.argv[1]), "w", encoding="UTF-8")
- json.dump(data, file, indent=4, ensure_ascii=False)
- print("JSON dumped successfully")
- print("Unique texts: %d" % (len(TranslationList)))
- print("Entries: %d" % (len(TextList)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement