Created
September 7, 2021 07:59
-
-
Save NWPlayer123/aeb4db821d23cb1282e113733dfefcae to your computer and use it in GitHub Desktop.
see original description
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from struct import unpack | |
import sys | |
def align(value): | |
return value + (0x10 - (value % 0x10)) | |
def full(f): | |
return unpack("<I", f.read(4))[0] | |
def LBL1(f): | |
start = f.tell() | |
count = full(f) | |
table = [] | |
for i in range(count): | |
table.append(unpack("<2I", f.read(8))) | |
assert table == sorted(table, key=lambda x: x[1]) #can read strings in-place | |
returnme = [] | |
for entry in table: | |
f.seek(start + entry[1]) | |
addme = [] | |
for i in range(entry[0]): | |
size = ord(f.read(1)) | |
string = f.read(size) | |
index = full(f) | |
addme.append([string, index]) | |
returnme.append(addme) | |
return returnme | |
def TXTW(f, fullsize): | |
start = f.tell() | |
count = full(f) | |
table = [] | |
for i in range(count): | |
table.append(full(f)) | |
table.append(fullsize) #so we don't get an off-by-one | |
returnme = [] | |
for i in range(count): | |
f.seek(start + table[i]) | |
string = f.read(table[i+1] - table[i] - 2) #null byte terminator | |
returnme.append(string) | |
return returnme | |
with open(sys.argv[1], "rb") as f: | |
with open(".".join(sys.argv[1].split(".")[:-1]) + ".txt", "wb") as o: | |
header = unpack("<8sH2xBBB3xH12x", f.read(0x20)) | |
assert header[0] == "MsgStdBn" | |
assert header[1] == 0xFEFF | |
for i in range(header[4] - 1): | |
header2 = unpack("<4sI8x", f.read(0x10)) | |
start = f.tell() | |
if header2[0] == "LBL1": | |
labels = LBL1(f) | |
elif header2[0] == "TXTW": #custom section | |
strings = TXTW(f, header2[1]) | |
f.seek(start + header2[1]) | |
f.seek(align(f.tell())) | |
unsorted = [] | |
for entry in labels: | |
for entry2 in entry: | |
unsorted.append(entry2) | |
unsorted.sort(key=lambda x: x[1]) | |
for i in range(len(strings)): | |
o.write(unsorted[i][0].encode("UTF-16-LE") + ":\r\n".encode("UTF-16-LE")) | |
string = strings[i] | |
string = string.replace("\x0E\x00\x02\x00\x09\x00\x08\x00\x04\x00\x62\x00\x72\x00\x00\x00", "\r\n".encode("UTF-16-LE")) | |
o.write(string + "\r\n".encode("UTF-16-LE")) | |
o.write("----------------------------------------\r\n".encode("UTF-16-LE")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment