forked from Mrs4s/MiraiGo
/
main.go
197 lines (183 loc) · 4.64 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"io"
"os"
"reflect"
"strconv"
)
type JceField struct {
Name string
Type ast.Expr
Id int
}
type JceStruct struct {
Name string
Fields []JceField
}
type Generator struct {
JceStructs []JceStruct
}
func main() {
file := flag.String("file", "structs.txt", "file to parse")
output := flag.String("o", "", "output file")
flag.Parse()
f, err := parser.ParseFile(token.NewFileSet(), *file, nil, parser.ParseComments)
assert(err == nil, err)
g := Generator{}
ast.Inspect(f, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.FuncDecl:
return false
case *ast.TypeSpec:
x, ok := n.Type.(*ast.StructType)
if !ok {
return false
}
if x.Fields != nil {
var jce JceStruct
for _, f := range x.Fields.List {
if f.Tag == nil {
continue
}
unquoted, _ := strconv.Unquote(f.Tag.Value)
tag, ok := reflect.StructTag(unquoted).Lookup("jceId")
if !ok {
continue
}
id, _ := strconv.Atoi(tag)
if len(f.Names) != 1 {
panic("unexpected name count")
}
jce.Fields = append(jce.Fields, JceField{
Name: f.Names[0].Name,
Type: f.Type,
Id: id,
})
}
jce.Name = n.Name.Name
g.JceStructs = append(g.JceStructs, jce)
}
}
return true
})
buf := new(bytes.Buffer)
assert(err == nil, err)
g.Generate(buf)
formated, err := format.Source(buf.Bytes())
if err != nil {
fmt.Printf("%s\n", buf.Bytes())
panic(err)
}
_ = os.WriteFile(*output, formated, 0o644)
}
func (g Generator) Generate(w io.Writer) {
io.WriteString(w, "// Code generated by internal/generator/jce_gen; DO NOT EDIT.\n\npackage jce\n\n")
for _, jce := range g.JceStructs {
if jce.Name == "" || len(jce.Fields) == 0 {
continue
}
var generate func(typ ast.Expr, i int)
generate = func(typ ast.Expr, i int) {
field := jce.Fields[i]
switch typ := typ.(type) {
case *ast.Ident:
switch typ.Name {
case "uint8", "uint16", "uint32", "uint64":
typename := []byte(typ.Name)[1:]
casted := fmt.Sprintf("%s(pkt.%s)", typename, field.Name)
typename[0] ^= ' '
fmt.Fprintf(w, "w.Write%s(%s, %d)\n", typename, casted, field.Id)
case "int8", "int16", "int32", "int64",
"byte", "string":
typename := []byte(typ.Name)
typename[0] ^= ' '
fmt.Fprintf(w, "w.Write%s(pkt.%s, %d)\n", typename, field.Name, field.Id)
case "int":
fmt.Fprintf(w, "w.WriteInt64(int64(pkt.%s), %d)\n", field.Name, field.Id)
default:
fmt.Fprintf(w, `{ // write %s tag=%d
w.writeHead(10, %d)
w.buf.Write(pkt.%s.ToBytes())
w.writeHead(11, 0)}`+"\n", field.Name, field.Id, field.Id, field.Name)
}
case *ast.StarExpr:
_ = typ.X.(*ast.Ident) // assert
generate(typ.X, i)
case *ast.ArrayType:
assert(typ.Len == nil, "unexpected array len")
var method string
switch typeName(typ) {
case "[]byte":
method = "WriteBytes"
case "[]int64":
method = "WriteInt64Slice"
case "[][]byte":
method = "WriteBytesSlice"
default:
fmt.Fprintf(w,
"\t"+`{ // write pkt.%s tag=%d
w.writeHead(9, %d)
if len(pkt.%s) == 0 {
w.writeHead(12, 0) // w.WriteInt32(0, 0)
} else {
w.WriteInt32(int32(len(pkt.%s)), 0)
for _, i := range pkt.%s {
w.writeHead(10, 0)
w.buf.Write(i.ToBytes())
w.writeHead(11, 0)
}
}}`+"\n", field.Name, field.Id, field.Id, field.Name, field.Name, field.Name)
return
}
assert(method != "", typeName(typ))
fmt.Fprintf(w, "w.%s(pkt.%s, %d)\n", method, field.Name, field.Id)
case *ast.MapType:
var method string
typName := typeName(typ)
switch typName {
case "map[string]string":
method = "writeMapStrStr"
case "map[string][]byte":
method = "writeMapStrBytes"
case "map[string]map[string][]byte":
method = "writeMapStrMapStrBytes"
}
assert(method != "", typName)
fmt.Fprintf(w, "w.%s(pkt.%s, %d)\n", method, field.Name, field.Id)
}
}
fmt.Fprintf(w, "func (pkt *%s) ToBytes() []byte {\nw := NewJceWriter()\n", jce.Name)
for i := range jce.Fields {
generate(jce.Fields[i].Type, i)
}
fmt.Fprintf(w, "return w.Bytes()\n}\n\n")
}
}
func assert(cond bool, val any) {
if !cond {
panic("assertion failed: " + fmt.Sprint(val))
}
}
func typeName(typ ast.Expr) string {
switch typ := typ.(type) {
case *ast.Ident:
return typ.Name
case *ast.StarExpr:
return "*" + typeName(typ.X)
case *ast.ArrayType:
if typ.Len != nil {
panic("unexpected array type")
}
return "[]" + typeName(typ.Elt)
case *ast.MapType:
return "map[" + typeName(typ.Key) + "]" + typeName(typ.Value)
}
panic("unexpected type")
}