2023-03-07 05:31:05 +00:00
|
|
|
#!/bin/python
|
|
|
|
'''
|
|
|
|
genstructs.py - gopenfusion
|
|
|
|
|
|
|
|
Takes raw structures from a decompiled 'Assembly - CSharp.dll' from a main.unity3d fusionfall beta client,
|
2023-03-07 22:28:34 +00:00
|
|
|
and transpiles them to gopenfusion's custom packet structure & tags. Some useful constants are also grabbed
|
|
|
|
and transpiled. This requires a C compiler installed, since struct field padding is grabbed via the `offsetof()`
|
|
|
|
C macro. Some manual rearranging of structures from the disassembled source might be needed, however the script
|
|
|
|
should 'just werk' off of ilspycmd output. This script can also be modified to generate c-style structures
|
|
|
|
(because it already does!)
|
2023-03-07 05:31:05 +00:00
|
|
|
|
2023-03-07 05:37:01 +00:00
|
|
|
usage: ./genstructs.py [IN.cs] > structs.go
|
2023-03-07 21:08:48 +00:00
|
|
|
|
|
|
|
It's recommended to run structs.go through `gofmt`.
|
2023-03-07 05:31:05 +00:00
|
|
|
'''
|
|
|
|
from distutils.ccompiler import new_compiler
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
PACK_ALIGN = 4
|
|
|
|
|
|
|
|
def sanitizeName(name: str) -> str:
|
|
|
|
# all exported fields in go must start capitalized
|
2023-03-07 20:23:53 +00:00
|
|
|
return name[0:1].upper() + name[1:]
|
2023-03-07 05:31:05 +00:00
|
|
|
|
|
|
|
def writeToFile(source: str, filePath: str) -> None:
|
|
|
|
with open(filePath, "w") as out:
|
|
|
|
out.write(source)
|
|
|
|
|
2023-03-07 21:08:48 +00:00
|
|
|
WARN_INVALID = False
|
|
|
|
|
2023-03-07 05:31:05 +00:00
|
|
|
class StructTranspiler:
|
|
|
|
class StructField:
|
|
|
|
def __init__(self, name: str, type: str, marshal: str) -> None:
|
|
|
|
self.marshal = marshal
|
|
|
|
self.type = type
|
|
|
|
self.ctype = type # for transpilation to c
|
|
|
|
self.tags = ""
|
|
|
|
self.size = 0
|
|
|
|
self.padding = 0
|
|
|
|
self.name = sanitizeName(name)
|
|
|
|
self.cname = self.name
|
|
|
|
self.needsPatching = False
|
|
|
|
|
|
|
|
if type == "byte":
|
|
|
|
self.type = "uint8"
|
|
|
|
self.ctype = "char"
|
|
|
|
self.size = 1
|
|
|
|
elif type == "sbyte":
|
|
|
|
self.type = "int8"
|
|
|
|
self.ctype = "char"
|
|
|
|
self.size = 1
|
|
|
|
elif type == "short":
|
|
|
|
self.type = "int16"
|
|
|
|
self.ctype = "short"
|
|
|
|
self.size = 2
|
|
|
|
elif type == "int":
|
|
|
|
self.type = "int32"
|
|
|
|
self.ctype = "int"
|
|
|
|
self.size = 4
|
|
|
|
elif type == "uint":
|
|
|
|
self.type = "uint32"
|
|
|
|
self.ctype = "int"
|
|
|
|
self.size = 4
|
|
|
|
elif type == "float":
|
|
|
|
self.type = "float32"
|
|
|
|
self.ctype = "float"
|
|
|
|
self.size = 4
|
|
|
|
elif type == "long":
|
|
|
|
self.type = "int64"
|
|
|
|
self.ctype = "long"
|
|
|
|
self.size = 8
|
|
|
|
elif type == "ulong":
|
|
|
|
self.type = "uint64"
|
|
|
|
self.ctype = "long"
|
|
|
|
self.size = 8
|
|
|
|
elif type == "byte[]":
|
|
|
|
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
|
|
|
|
self.type = "[%d]byte" % self.size
|
|
|
|
self.ctype = "char"
|
|
|
|
self.cname += "[%d]" % self.size
|
|
|
|
elif type == "short[]":
|
|
|
|
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
|
|
|
|
self.type = "[%d]int16" % self.size
|
|
|
|
self.ctype = "short"
|
|
|
|
self.cname += "[%d]" % self.size
|
|
|
|
self.size *= 2
|
2023-03-07 19:30:43 +00:00
|
|
|
elif type == "string":
|
|
|
|
# all strings in fusionfall are utf16, in a uint16 array
|
2023-03-07 05:31:05 +00:00
|
|
|
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
|
2023-03-07 19:30:43 +00:00
|
|
|
self.type = "string"
|
2023-03-07 21:31:34 +00:00
|
|
|
self.addTag("size:\"%d\"" % self.size) # special tag to tell our decoder/encoder the size of the uint16[] array
|
2023-03-07 19:30:43 +00:00
|
|
|
self.ctype = "short"
|
2023-03-07 05:31:05 +00:00
|
|
|
self.cname += "[%d]" % self.size
|
2023-03-07 19:30:43 +00:00
|
|
|
self.size *= 2
|
|
|
|
elif type == "int[]":
|
|
|
|
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
|
|
|
|
self.type = "[%d]int32" % self.size
|
|
|
|
self.ctype = "int"
|
|
|
|
self.cname += "[%d]" % self.size
|
|
|
|
self.size *= 4
|
2023-03-07 05:31:05 +00:00
|
|
|
elif type == "float[]":
|
|
|
|
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
|
|
|
|
self.type = "[%d]float32" % self.size
|
|
|
|
self.ctype = "float"
|
|
|
|
self.cname += "[%d]" % self.size
|
|
|
|
self.size *= 4
|
2023-03-07 19:30:43 +00:00
|
|
|
elif type == "long[]":
|
2023-03-07 05:31:05 +00:00
|
|
|
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
|
2023-03-07 19:30:43 +00:00
|
|
|
self.type = "[%d]int64" % self.size
|
|
|
|
self.ctype = "long"
|
2023-03-07 05:31:05 +00:00
|
|
|
self.cname += "[%d]" % self.size
|
2023-03-07 19:30:43 +00:00
|
|
|
self.size *= 8
|
2023-03-07 05:31:05 +00:00
|
|
|
else:
|
|
|
|
# assume it's a structure that will be defined later
|
2023-03-07 19:30:43 +00:00
|
|
|
if type.find("[]") != -1: # it's an array!
|
2023-03-07 05:31:05 +00:00
|
|
|
type = type.replace("[]", "")
|
|
|
|
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
|
2023-03-07 20:23:53 +00:00
|
|
|
self.cname = self.name + "[%d]" % self.size
|
2023-03-07 05:31:05 +00:00
|
|
|
else:
|
2023-03-07 20:23:53 +00:00
|
|
|
self.cname = self.name
|
2023-03-07 05:31:05 +00:00
|
|
|
self.size = 1
|
|
|
|
self.type = sanitizeName(type)
|
|
|
|
self.ctype = sanitizeName(type)
|
|
|
|
self.needsPatching = True
|
|
|
|
|
2023-03-07 21:31:34 +00:00
|
|
|
def addTag(self, tag: str) -> None:
|
|
|
|
if len(self.tags) > 0: # if there's already a tag defined, make sure there's a space separating them
|
|
|
|
self.tags += " "
|
|
|
|
|
|
|
|
self.tags += tag
|
|
|
|
|
2023-03-07 05:31:05 +00:00
|
|
|
def __init__(self, lines: list[str]) -> None:
|
|
|
|
self.name = "UnknownStruct"
|
|
|
|
self.size = 0
|
|
|
|
self.fields: list[self.StructField] = []
|
|
|
|
self.lines = lines
|
|
|
|
self.cursor = 0
|
|
|
|
|
|
|
|
def getLine(self) -> str:
|
|
|
|
return self.lines[self.cursor]
|
|
|
|
|
|
|
|
def getNextLine(self) -> str:
|
|
|
|
self.cursor += 1
|
2023-03-07 21:08:48 +00:00
|
|
|
return self.getLine()
|
2023-03-07 05:31:05 +00:00
|
|
|
|
|
|
|
def needsPatching(self) -> int:
|
|
|
|
for i in range(len(self.fields)):
|
|
|
|
if self.fields[i].needsPatching:
|
|
|
|
return i
|
|
|
|
return -1
|
|
|
|
|
|
|
|
# skip lines until we run into a '[StructLayout('
|
|
|
|
def searchStructLayout(self) -> None:
|
|
|
|
line = self.getLine()
|
|
|
|
while True:
|
|
|
|
if line.find("[StructLayout(") != -1 and line.find("Size = ") != -1:
|
|
|
|
self.size = int(line[(line.find("Size = ") + len("Size = ")):line.find(")]")])
|
|
|
|
line = self.getNextLine()
|
|
|
|
self.name = sanitizeName(line[(line.find("public struct ") + len("public struct ")):len(line)-1])
|
|
|
|
self.getNextLine()
|
|
|
|
return
|
|
|
|
line = self.getNextLine()
|
|
|
|
|
|
|
|
# skip lines until we find a struct field
|
|
|
|
def searchMarshal(self) -> StructField:
|
|
|
|
line = self.getLine()
|
|
|
|
while line.find("}") == -1:
|
|
|
|
if line.find("[MarshalAs(") != -1:
|
|
|
|
marshal = line
|
|
|
|
line = self.getNextLine()
|
|
|
|
|
|
|
|
typeStart = line.find("public ") + len("public ")
|
|
|
|
typeEnd = line.find(" ", typeStart)
|
|
|
|
type = line[typeStart:typeEnd]
|
|
|
|
|
|
|
|
name = line[typeEnd+1:line.find(";", typeEnd)]
|
|
|
|
return self.StructField(name, type, marshal)
|
|
|
|
line = self.getNextLine()
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def toCStyle(self) -> str:
|
|
|
|
source = "typedef struct {\n"
|
|
|
|
for field in self.fields:
|
|
|
|
source += "\t%s %s;\n" % (field.ctype, field.cname)
|
|
|
|
source += "} %s;\n" % self.name
|
|
|
|
|
|
|
|
for field in self.fields:
|
|
|
|
source += "printf(\"%d \""
|
|
|
|
source += ", offsetof(%s, %s));\n" % (self.name, field.name)
|
|
|
|
|
|
|
|
source += "printf(\"\\n\");\n"
|
|
|
|
return source
|
|
|
|
|
|
|
|
def populatePadding(self, offsets: list[str]) -> None:
|
|
|
|
currentSize = 0
|
|
|
|
for i in range(len(self.fields)):
|
|
|
|
currentSize += self.fields[i].size
|
|
|
|
if i < len(self.fields)-1 and currentSize != int(offsets[i+1]):
|
|
|
|
self.fields[i].padding = int(offsets[i+1]) - currentSize
|
|
|
|
currentSize += self.fields[i].padding
|
|
|
|
|
|
|
|
if currentSize < self.size:
|
|
|
|
self.fields[len(self.fields)-1].padding = self.size - currentSize
|
|
|
|
|
|
|
|
def parseStructure(self) -> None:
|
|
|
|
self.searchStructLayout()
|
|
|
|
|
|
|
|
line = self.getLine()
|
|
|
|
while line.find("}") == -1:
|
|
|
|
field = self.searchMarshal()
|
|
|
|
if field != None:
|
|
|
|
self.fields.append(field)
|
|
|
|
|
|
|
|
line = self.getLine()
|
|
|
|
|
|
|
|
def toGoStyle(self) -> str:
|
2023-03-07 21:08:48 +00:00
|
|
|
global WARN_INVALID
|
|
|
|
|
2023-03-07 05:31:05 +00:00
|
|
|
source = "type " + self.name + " struct {"
|
|
|
|
currentSize = 0
|
|
|
|
for field in self.fields:
|
|
|
|
currentSize += field.size
|
|
|
|
|
|
|
|
if field.padding > 0:
|
|
|
|
currentSize += field.padding
|
2023-03-07 21:31:34 +00:00
|
|
|
field.addTag("pad:\"%d\"" % field.padding)
|
2023-03-07 05:31:05 +00:00
|
|
|
|
|
|
|
source += "\n\t" + field.name + " " + field.type
|
|
|
|
if len(field.tags) > 0:
|
|
|
|
source += " `" + field.tags + "`"
|
|
|
|
|
|
|
|
if currentSize != self.size:
|
|
|
|
source += "\n// WARNING: computed size is %d, needs to be %d!!" % (currentSize, self.size)
|
2023-03-07 21:08:48 +00:00
|
|
|
WARN_INVALID = True
|
2023-03-07 05:31:05 +00:00
|
|
|
else:
|
|
|
|
source += "\n// SIZE: %d" % self.size
|
|
|
|
source += "\n}\n"
|
|
|
|
return source
|
|
|
|
|
2023-03-07 22:28:34 +00:00
|
|
|
def transpileStructs(lines: list[str]) -> list[StructTranspiler]:
|
2023-03-07 05:31:05 +00:00
|
|
|
structs: list[StructTranspiler] = []
|
2023-03-07 21:53:12 +00:00
|
|
|
|
|
|
|
def tryPatching(struct: StructTranspiler) -> StructTranspiler:
|
|
|
|
f = struct.needsPatching()
|
|
|
|
lastf = None
|
2023-03-07 22:28:34 +00:00
|
|
|
while f != -1 and lastf != f:
|
2023-03-07 21:53:12 +00:00
|
|
|
# search for existing struct
|
|
|
|
name = struct.fields[f].type
|
|
|
|
for s in structs:
|
|
|
|
if s.name == name:
|
|
|
|
if struct.fields[f].size > 1: # was it an array?
|
|
|
|
struct.fields[f].type = ("[%d]" % struct.fields[f].size) + struct.fields[f].type
|
|
|
|
struct.fields[f].size *= s.size # field's size was set to 1 even if it wasn't an array
|
|
|
|
struct.fields[f].needsPatching = False # mark done
|
|
|
|
break
|
2023-03-07 22:28:34 +00:00
|
|
|
|
2023-03-07 21:53:12 +00:00
|
|
|
lastf = f
|
|
|
|
f = struct.needsPatching()
|
|
|
|
|
|
|
|
return struct
|
|
|
|
|
2023-03-07 05:31:05 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
struct = StructTranspiler(lines)
|
|
|
|
struct.parseStructure()
|
2023-03-07 21:53:12 +00:00
|
|
|
|
|
|
|
# try to resolve patches right here
|
|
|
|
struct = tryPatching(struct)
|
|
|
|
|
2023-03-07 05:31:05 +00:00
|
|
|
structs.append(struct)
|
|
|
|
lines = struct.lines[struct.cursor:]
|
|
|
|
except:
|
|
|
|
break
|
|
|
|
|
2023-03-07 21:53:12 +00:00
|
|
|
# move all structures that need patching to the end (so hopefully they'll be in the
|
|
|
|
# right order for our C source generation)
|
|
|
|
def sortStruct(s: StructTranspiler):
|
|
|
|
if s.needsPatching() != -1:
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
structs.sort(key=sortStruct)
|
|
|
|
|
2023-03-07 07:20:36 +00:00
|
|
|
# check for undefined types in structures and patch the sizes
|
2023-03-07 05:31:05 +00:00
|
|
|
for i in range(len(structs)):
|
2023-03-07 21:53:12 +00:00
|
|
|
structs[i] = tryPatching(structs[i])
|
2023-03-07 05:31:05 +00:00
|
|
|
|
|
|
|
# we compile a small c program to grab the exact offsets and alignments
|
|
|
|
source = "#include <stdio.h>\n#include <stddef.h>\n"
|
|
|
|
source += "int main() {\n"
|
|
|
|
source += "#pragma pack(push)\n#pragma pack(%d)\n" % PACK_ALIGN
|
|
|
|
for struct in structs:
|
|
|
|
source += struct.toCStyle()
|
|
|
|
source += "#pragma pack(pop)\nreturn 0;\n}\n"
|
|
|
|
|
|
|
|
writeToFile(source, "tmp.c")
|
|
|
|
compiler = new_compiler()
|
|
|
|
compiler.compile(['tmp.c'])
|
|
|
|
compiler.link_executable(['tmp.o'], 'tmp')
|
|
|
|
|
2023-03-07 07:20:36 +00:00
|
|
|
# patch padding bytes
|
2023-03-07 05:31:05 +00:00
|
|
|
lines = subprocess.getoutput(['./tmp']).splitlines()
|
|
|
|
for i in range(len(structs)):
|
|
|
|
structs[i].populatePadding(lines[i].split(" "))
|
2023-03-07 07:20:36 +00:00
|
|
|
|
2023-03-07 22:28:34 +00:00
|
|
|
return structs
|
|
|
|
|
|
|
|
class ConstTranspiler:
|
|
|
|
def __init__(self, lines: list[str]) -> None:
|
|
|
|
self.lines = lines
|
|
|
|
self.cursor = 0
|
|
|
|
self.value = 0
|
|
|
|
self.name = 0
|
|
|
|
|
|
|
|
def getLine(self) -> str:
|
|
|
|
return self.lines[self.cursor]
|
|
|
|
|
|
|
|
def getNextLine(self) -> str:
|
|
|
|
self.cursor += 1
|
|
|
|
return self.getLine()
|
|
|
|
|
|
|
|
# skip lines until we run into a 'public const uint '
|
|
|
|
def searchForConst(self) -> None:
|
|
|
|
line = self.getLine()
|
|
|
|
while True:
|
|
|
|
if line.find("public const uint ") != -1:
|
|
|
|
nameStart = (line.find("public const uint ") + len("public const uint "))
|
|
|
|
self.value = int(line[(line.find("= ", nameStart) + len("= ")):line.find("u;")])
|
|
|
|
self.name = line[nameStart:line.find(" = ", nameStart)]
|
|
|
|
self.getNextLine()
|
|
|
|
return
|
|
|
|
line = self.getNextLine()
|
|
|
|
|
|
|
|
def transpileConsts(lines: list[str]) -> list[ConstTranspiler]:
|
|
|
|
consts: list[ConstTranspiler] = []
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
const = ConstTranspiler(lines)
|
|
|
|
const.searchForConst()
|
|
|
|
|
|
|
|
consts.append(const)
|
|
|
|
lines = const.lines[const.cursor:]
|
|
|
|
except:
|
|
|
|
break
|
|
|
|
|
|
|
|
return consts
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
inFilePath = sys.argv[1]
|
|
|
|
|
|
|
|
iFile = open(inFilePath, "r")
|
|
|
|
lines = iFile.readlines()
|
|
|
|
structs = transpileStructs(lines)
|
|
|
|
consts = transpileConsts(lines)
|
|
|
|
|
2023-03-07 07:20:36 +00:00
|
|
|
# emit structures
|
2023-03-07 21:08:48 +00:00
|
|
|
source = "// generated via genstructs.py"
|
|
|
|
if WARN_INVALID:
|
|
|
|
source += " - WARN!! Not all structures are valid, grep 'WARNING'\n"
|
|
|
|
else:
|
|
|
|
source += " - All structure padding and member alignment verified\n"
|
2023-03-07 22:28:34 +00:00
|
|
|
|
|
|
|
source += "package protocol\n\nconst (\n"
|
|
|
|
for const in consts:
|
|
|
|
source += "\t%s = 0x%x\n" % (const.name, const.value)
|
|
|
|
source += ")\n\n"
|
|
|
|
|
2023-03-07 05:31:05 +00:00
|
|
|
for struct in structs:
|
2023-03-07 21:08:48 +00:00
|
|
|
source += struct.toGoStyle() + "\n"
|
|
|
|
print(source)
|
2023-03-07 05:31:05 +00:00
|
|
|
|
|
|
|
os.remove("tmp")
|
|
|
|
os.remove("tmp.c")
|
|
|
|
os.remove("tmp.o")
|
|
|
|
|