Advertisement
Guest User

Untitled

a guest
Nov 16th, 2021
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. import struct
  2. import os
  3. import sqlite3
  4. import sys
  5.  
  6. def getString(offset, data):
  7.     return data[offset:offset+256].split(b"\x00")[0].decode("utf-8")
  8.  
  9. if len(sys.argv) < 2:
  10.     print("Usage: datasheetDecompile.py rootFolder")
  11. else:
  12.     db = sqlite3.connect(os.path.join(sys.argv[1], "datasheets.db"))
  13.     cursor = db.cursor()
  14.        
  15.     for path, dirs, files in os.walk(sys.argv[1]):
  16.         for file in files:
  17.             fullName = os.path.join(path, file)
  18.            
  19.             if fullName.endswith(".loc.xml") and "en-us" in fullName:
  20.                 rawData = open(fullName, "r", errors="replace").read().split("\n")
  21.                
  22.                 cursor.execute("CREATE TABLE IF NOT EXISTS [translations] (key, value, source)")
  23.  
  24.                 for line in rawData:
  25.                     if "<string key" in line:
  26.                         key = line.split("<string key=\"")[1].split("\"")[0]
  27.                         value = line.split("</string>")[0].split("\">")[-1]
  28.  
  29.                         cursor.execute("INSERT INTO [translations] VALUES(?,?,?)", (key, value, file))
  30.                    
  31.             elif fullName.endswith(".datasheet"):
  32.                 shortName = os.path.join(path, file).split("\\datatables\\")[1].replace("\\", "/").split(".")[0]
  33.            
  34.                 rawData = open(fullName, "rb").read()
  35.  
  36.                 dataSize = struct.unpack("i", rawData[0x18:0x18+4])[0]
  37.                 headerSize = struct.unpack("i", rawData[0x38:0x38+4])[0]
  38.  
  39.                 header = rawData[0x3c:0x3c+headerSize]
  40.                 data = rawData[0x3c+headerSize:]
  41.  
  42.                 columns = struct.unpack("i", header[0x08:0x08+4])[0]
  43.                 rows = struct.unpack("i", header[0x0c:0x0c+4])[0]
  44.  
  45.                 buffer = ""
  46.                 columnTypes = []
  47.  
  48.                 for columnOffset in range(columns):
  49.                     columnData = header[0x20+columnOffset*12:0x20+columnOffset*12+12]
  50.                     hash, nameOffset, type = struct.unpack("Iii", columnData)
  51.                     columnName = getString(nameOffset, data)
  52.                    
  53.                     cursor.execute("CREATE TABLE IF NOT EXISTS [{}-meta] (columnName, hash, type)".format(shortName))
  54.                     cursor.execute("INSERT INTO [{}-meta] VALUES('{}','{}','{}')".format(shortName, columnName, hex(hash), type))
  55.                    
  56.                     columnTypes.append(type)
  57.                    
  58.                     if type == 2:
  59.                         buffer += f"[{columnName}] NUMERIC|"
  60.                     else:
  61.                         buffer += f"[{columnName}] TEXT|"
  62.                    
  63.                 buffer = buffer[:-1]
  64.                
  65.                 #print("CREATE TABLE [{}] {}".format(shortName, tuple(buffer.split('|'))))
  66.                
  67.                 if "|" in buffer:
  68.                     cursor.execute("CREATE TABLE [{}] {}".format(shortName, str(tuple(buffer.split('|'))).replace("'", "")))
  69.                 else:
  70.                     cursor.execute("CREATE TABLE [{}] ({})".format(shortName, str(buffer).replace("'", "")))
  71.  
  72.                 for rowOffset in range(rows):
  73.                     rowData = header[0x20+columns*12+rowOffset*columns*8:0x20+columns*12+rowOffset*columns*8+columns*8]
  74.                     rowVars = struct.unpack("if"*columns, rowData)
  75.                    
  76.                     buffer = ""
  77.                    
  78.                     for i in range(0, columns*2, 2):
  79.                         left = rowVars[i]
  80.                         right = rowVars[i+1]
  81.                         rightInt = struct.unpack("I", struct.pack("f", right))[0]
  82.                        
  83.                         if hex(left) == hex(rightInt):
  84.                             buffer += getString(left, data) + "|"
  85.                         else:
  86.                             if hex(rightInt) in ["0x0", "0x1"] and columnTypes[i//2] == 3:
  87.                                 buffer += str(hex(rightInt) == "0x1") + "|"
  88.                             else:
  89.                                 right = int(right) if right.is_integer() else "{:.4f}".format(right)
  90.                                 buffer += str(right) + "|"
  91.                        
  92.                     buffer = buffer[:-1]
  93.                    
  94.                     cursor.execute("INSERT INTO [{}] VALUES{}".format(shortName, tuple(buffer.split('|'))))
  95.  
  96.             db.commit()
  97.     db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement