genstructs.py: include go source header

This commit is contained in:
CPunch 2023-03-07 15:08:48 -06:00
parent b639346605
commit 3f4ce93f9f
3 changed files with 18 additions and 3 deletions

View File

@ -1,6 +1,6 @@
# gopenfusion # gopenfusion
A toy implementation of the Fusionfall Packet Protocol written in Go. A toy implementation of the [Fusionfall Packet Protocol](https://openpunk.com/pages/fusionfall-openfusion/) written in Go.
## Generating structures ## Generating structures

View File

@ -1,3 +1,4 @@
// generated via genstructs.py - All structure padding and member alignment verified
package protocol package protocol
type SPCStyle struct { type SPCStyle struct {

View File

@ -9,6 +9,8 @@
(because it already does!) (because it already does!)
usage: ./genstructs.py [IN.cs] > structs.go usage: ./genstructs.py [IN.cs] > structs.go
It's recommended to run structs.go through `gofmt`.
''' '''
from distutils.ccompiler import new_compiler from distutils.ccompiler import new_compiler
import subprocess import subprocess
@ -25,6 +27,8 @@ def writeToFile(source: str, filePath: str) -> None:
with open(filePath, "w") as out: with open(filePath, "w") as out:
out.write(source) out.write(source)
WARN_INVALID = False
class StructTranspiler: class StructTranspiler:
class StructField: class StructField:
def __init__(self, name: str, type: str, marshal: str) -> None: def __init__(self, name: str, type: str, marshal: str) -> None:
@ -132,7 +136,7 @@ class StructTranspiler:
def getNextLine(self) -> str: def getNextLine(self) -> str:
self.cursor += 1 self.cursor += 1
return self.lines[self.cursor] return self.getLine()
def needsPatching(self) -> int: def needsPatching(self) -> int:
for i in range(len(self.fields)): for i in range(len(self.fields)):
@ -206,6 +210,8 @@ class StructTranspiler:
line = self.getLine() line = self.getLine()
def toGoStyle(self) -> str: def toGoStyle(self) -> str:
global WARN_INVALID
source = "type " + self.name + " struct {" source = "type " + self.name + " struct {"
currentSize = 0 currentSize = 0
for field in self.fields: for field in self.fields:
@ -223,6 +229,7 @@ class StructTranspiler:
if currentSize != self.size: if currentSize != self.size:
source += "\n// WARNING: computed size is %d, needs to be %d!!" % (currentSize, self.size) source += "\n// WARNING: computed size is %d, needs to be %d!!" % (currentSize, self.size)
WARN_INVALID = True
else: else:
source += "\n// SIZE: %d" % self.size source += "\n// SIZE: %d" % self.size
source += "\n}\n" source += "\n}\n"
@ -277,8 +284,15 @@ if __name__ == '__main__':
structs[i].populatePadding(lines[i].split(" ")) structs[i].populatePadding(lines[i].split(" "))
# emit structures # emit structures
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"
source += "package protocol\n\n"
for struct in structs: for struct in structs:
print(struct.toGoStyle()) source += struct.toGoStyle() + "\n"
print(source)
os.remove("tmp") os.remove("tmp")
os.remove("tmp.c") os.remove("tmp.c")