Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- import os
- import sqlite3
- import sys
- def getString(offset, data):
- return data[offset:offset+256].split(b"\x00")[0].decode("utf-8")
- if len(sys.argv) < 2:
- print("Usage: datasheetDecompile.py rootFolder")
- else:
- db = sqlite3.connect(os.path.join(sys.argv[1], "datasheets.db"))
- cursor = db.cursor()
- for path, dirs, files in os.walk(sys.argv[1]):
- for file in files:
- fullName = os.path.join(path, file)
- if fullName.endswith(".loc.xml") and "en-us" in fullName:
- rawData = open(fullName, "r", errors="replace").read().split("\n")
- cursor.execute("CREATE TABLE IF NOT EXISTS [translations] (key, value, source)")
- for line in rawData:
- if "<string key" in line:
- key = line.split("<string key=\"")[1].split("\"")[0]
- value = line.split("</string>")[0].split("\">")[-1]
- cursor.execute("INSERT INTO [translations] VALUES(?,?,?)", (key, value, file))
- elif fullName.endswith(".datasheet"):
- shortName = os.path.join(path, file).split("\\datatables\\")[1].replace("\\", "/").split(".")[0]
- rawData = open(fullName, "rb").read()
- dataSize = struct.unpack("i", rawData[0x18:0x18+4])[0]
- headerSize = struct.unpack("i", rawData[0x38:0x38+4])[0]
- header = rawData[0x3c:0x3c+headerSize]
- data = rawData[0x3c+headerSize:]
- columns = struct.unpack("i", header[0x08:0x08+4])[0]
- rows = struct.unpack("i", header[0x0c:0x0c+4])[0]
- buffer = ""
- columnTypes = []
- for columnOffset in range(columns):
- columnData = header[0x20+columnOffset*12:0x20+columnOffset*12+12]
- hash, nameOffset, type = struct.unpack("Iii", columnData)
- columnName = getString(nameOffset, data)
- cursor.execute("CREATE TABLE IF NOT EXISTS [{}-meta] (columnName, hash, type)".format(shortName))
- cursor.execute("INSERT INTO [{}-meta] VALUES('{}','{}','{}')".format(shortName, columnName, hex(hash), type))
- columnTypes.append(type)
- if type == 2:
- buffer += f"[{columnName}] NUMERIC|"
- else:
- buffer += f"[{columnName}] TEXT|"
- buffer = buffer[:-1]
- #print("CREATE TABLE [{}] {}".format(shortName, tuple(buffer.split('|'))))
- if "|" in buffer:
- cursor.execute("CREATE TABLE [{}] {}".format(shortName, str(tuple(buffer.split('|'))).replace("'", "")))
- else:
- cursor.execute("CREATE TABLE [{}] ({})".format(shortName, str(buffer).replace("'", "")))
- for rowOffset in range(rows):
- rowData = header[0x20+columns*12+rowOffset*columns*8:0x20+columns*12+rowOffset*columns*8+columns*8]
- rowVars = struct.unpack("if"*columns, rowData)
- buffer = ""
- for i in range(0, columns*2, 2):
- left = rowVars[i]
- right = rowVars[i+1]
- rightInt = struct.unpack("I", struct.pack("f", right))[0]
- if hex(left) == hex(rightInt):
- buffer += getString(left, data) + "|"
- else:
- if hex(rightInt) in ["0x0", "0x1"] and columnTypes[i//2] == 3:
- buffer += str(hex(rightInt) == "0x1") + "|"
- else:
- right = int(right) if right.is_integer() else "{:.4f}".format(right)
- buffer += str(right) + "|"
- buffer = buffer[:-1]
- cursor.execute("INSERT INTO [{}] VALUES{}".format(shortName, tuple(buffer.split('|'))))
- db.commit()
- db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement