Start gen refactor

Signed-off-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
jolheiser
2021-08-05 20:15:52 -05:00
parent ef89758ede
commit 7923ad0c2a
7 changed files with 107 additions and 32 deletions

View File

@ -130,7 +130,8 @@ func makeBlockDeclaration(blocks []Block) *ast.DeclStmt {
return out
}
//go:generate go run $GOFILE > ./packetid.go
//go:generate go run $GOFILE
//go:generate go fmt block.go
func main() {
blocks, err := downloadInfo()
if err != nil {
@ -138,7 +139,14 @@ func main() {
os.Exit(1)
}
fmt.Println(`// Code generated by gen_blocks.go; DO NOT EDIT.
f, err := os.Create("entity.go")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer f.Close()
fmt.Fprintln(f, `// Code generated by gen_blocks.go; DO NOT EDIT.
// Package block stores information about blocks in Minecraft.
package block
@ -176,26 +184,26 @@ type Block struct {
`)
format.Node(os.Stdout, token.NewFileSet(), makeBlockDeclaration(blocks))
fmt.Println()
fmt.Println()
fmt.Println("// ByID is an index of minecraft blocks by their ID.")
fmt.Println("var ByID = map[ID]*Block{")
fmt.Fprintln()
fmt.Fprintln()
fmt.Fprintln(f, "// ByID is an index of minecraft blocks by their ID.")
fmt.Fprintln(f, "var ByID = map[ID]*Block{")
for _, b := range blocks {
fmt.Printf(" %d: &%s,\n", b.ID, strcase.ToCamel(b.Name))
fmt.Fprintf(f," %d: &%s,\n", b.ID, strcase.ToCamel(b.Name))
}
fmt.Println("}")
fmt.Fprintln(f, "}")
fmt.Println()
fmt.Println("// StateID maps all possible state IDs to a corresponding block ID.")
fmt.Println("var StateID = map[uint32]ID{")
fmt.Fprintln()
fmt.Fprintln(f, "// StateID maps all possible state IDs to a corresponding block ID.")
fmt.Fprintln(f, "var StateID = map[uint32]ID{")
for _, b := range blocks {
if b.MinStateID == b.MaxStateID {
fmt.Printf(" %d: %d,\n", b.MinStateID, b.ID)
fmt.Fprintf(f, " %d: %d,\n", b.MinStateID, b.ID)
} else {
for i := b.MinStateID; i <= b.MaxStateID; i++ {
fmt.Printf(" %d: %d,\n", i, b.ID)
fmt.Fprintf(f, " %d: %d,\n", i, b.ID)
}
}
}
fmt.Println("}")
fmt.Fprintln("}")
}