-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (104 loc) · 3.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"encoding/xml"
"flag"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"github.com/ethanmoffat/eolib-go/internal/codegen"
eoxml "github.com/ethanmoffat/eolib-go/internal/xml"
)
var inputDir string
var outputDir string
func main() {
flag.StringVar(&inputDir, "i", "eo-protocol", "The input directory for eo-protocol files.")
flag.StringVar(&outputDir, "o", "pkg/eolib/protocol", "The output directory for generated code.")
flag.Parse()
if _, err := os.Stat(inputDir); err != nil {
fmt.Printf("error: input directory %s does not exist\n", inputDir)
os.Exit(1)
}
if _, err := os.Stat(outputDir); err != nil {
fmt.Printf("error: output directory %s does not exist\n", outputDir)
os.Exit(1)
}
fmt.Printf("Using parameters:\n inputDir: %s\n outputDir: %s\n", inputDir, outputDir)
protocolFiles := []string{}
filepath.WalkDir(path.Join(inputDir, "xml"), func(currentPath string, d fs.DirEntry, err error) error {
if path.Ext(currentPath) == ".xml" {
relativeDir := strings.ReplaceAll(currentPath, path.Join(inputDir, "xml"), "")
protocolFiles = append(protocolFiles, strings.ReplaceAll(relativeDir, "/protocol.xml", ""))
}
return nil
})
dirToPackageName := map[string]string{
"map": "eomap",
"net": "net",
"net/client": "client",
"net/server": "server",
"pub": "pub",
"": "protocol",
}
var fullSpec eoxml.Protocol // all XML specs in a single place, for type lookups
var protocs []eoxml.Protocol // each individual protoc file
for _, file := range protocolFiles {
fullInputPath := path.Join(inputDir, "xml", file, "protocol.xml")
fp, err := os.Open(fullInputPath)
if err != nil {
fmt.Printf("error opening file: %v\n", err)
os.Exit(1)
}
defer fp.Close()
bytes, err := io.ReadAll(fp)
if err != nil {
fmt.Printf("error reading file: %v\n", err)
os.Exit(1)
}
var next eoxml.Protocol
if err := xml.Unmarshal(bytes, &next); err != nil {
fmt.Printf("error unmarshalling xml: %v\n", err)
os.Exit(1)
}
for i := range next.Enums {
next.Enums[i].Package = dirToPackageName[strings.Trim(file, string(os.PathSeparator))]
next.Enums[i].PackagePath = file
}
for i := range next.Structs {
next.Structs[i].Package = dirToPackageName[strings.Trim(file, string(os.PathSeparator))]
next.Structs[i].PackagePath = file
}
for i := range next.Packets {
next.Packets[i].Package = dirToPackageName[strings.Trim(file, string(os.PathSeparator))]
next.Packets[i].PackagePath = file
}
fullSpec.Enums = append(fullSpec.Enums, next.Enums...)
fullSpec.Structs = append(fullSpec.Structs, next.Structs...)
fullSpec.Packets = append(fullSpec.Packets, next.Packets...)
protocs = append(protocs, next)
}
for i, file := range protocolFiles {
protoc := protocs[i]
if err := protoc.Validate(); err != nil {
fmt.Printf("error validating unmarshalled xml: %v\n", err)
os.Exit(1)
}
fullOutputPath := path.Join(outputDir, file)
fmt.Printf("generating code :: %s\n", file)
fmt.Printf(" %3d enums\n", len(protoc.Enums))
if err := codegen.GenerateEnums(fullOutputPath, protoc.Enums); err != nil {
fmt.Printf(" error generating enums: %v\n", err)
}
fmt.Printf(" %3d structs\n", len(protoc.Structs))
if err := codegen.GenerateStructs(fullOutputPath, protoc.Structs, fullSpec); err != nil {
fmt.Printf(" error generating structs: %v\n", err)
}
fmt.Printf(" %3d packets\n", len(protoc.Packets))
if err := codegen.GeneratePackets(fullOutputPath, protoc.Packets, fullSpec); err != nil {
fmt.Printf(" error generating packets: %v\n", err)
}
}
}