/
decoder.go
180 lines (155 loc) · 4.73 KB
/
decoder.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
package main
import (
"bytes"
"fmt"
"go/ast"
"reflect"
"strings"
)
type Decoder struct {
p packet
prefix string
buf *bytes.Buffer
tmpPrefix string
tmpCount int
}
func NewDecoder(p packet, b *bytes.Buffer) *Decoder {
return &Decoder{
p: p,
prefix: "t", // func (t *T) ...
buf: b,
tmpPrefix: "tmp",
tmpCount: 0,
}
}
func (de *Decoder) T() string {
de.tmpCount++
return fmt.Sprintf("%s%d", de.tmpPrefix, de.tmpCount-1)
}
func (de *Decoder) Write() {
de.writeStruct(de.p.spec, de.prefix)
}
func (de *Decoder) writeStruct(spec *ast.StructType, name string) {
for _, field := range spec.Fields.List {
tag := reflect.StructTag("")
if field.Tag != nil {
tag = reflect.StructTag(field.Tag.Value[1 : len(field.Tag.Value)-1])
}
var c string
if tag.Get("if") != "" && tag.Get("noreplace") != "true" {
c = strings.Replace(tag.Get("if"), ".", name+".", -1)
}
if c != "" {
fmt.Fprintf(de.buf, "if %s {\n", c)
}
for _, n := range field.Names {
de.writeType(field.Type, fmt.Sprintf("%s.%s", name, n), tag)
}
if c != "" {
fmt.Fprint(de.buf, "}\n")
}
}
}
func (de *Decoder) writeType(e ast.Expr, name string, tag reflect.StructTag) {
switch e := e.(type) {
case *ast.StructType:
de.writeStruct(e, name)
case *ast.SelectorExpr:
x := e.X.(*ast.Ident).Name
s := e.Sel.Name
de.writeField(x+"."+s, name, tag)
case *ast.Ident:
de.writeField(e.Name, name, tag)
case *ast.ArrayType:
lT := tag.Get("length") // length type
if lT == "rest" {
imports["io/ioutil"] = struct{}{}
fmt.Fprintf(de.buf, "if %s, err = ioutil.ReadAll(rr); err != nil { return }\n", name)
return
}
// length variable to read into
lv := de.T()
fmt.Fprintf(de.buf, "var %s %s\n", lv, lT)
de.writeField(lT, lv, "")
// TODO: Check for length > math.MaxInt16?
imports["fmt"] = struct{}{}
fmt.Fprintf(de.buf, `if %s < 0 {
return fmt.Errorf("negative array size: %%d < 0", %s)
}`, lv, lv)
fmt.Fprintf(de.buf, "\n%s = make([]%s, %s)\n", name, e.Elt, lv)
if i, ok := e.Elt.(*ast.Ident); ok && (i.Name == "byte" || i.Name == "uint8") {
fmt.Fprintf(de.buf, "if _, err = rr.Read(%s); err != nil { return err }\n", name)
return
}
iv := de.T()
fmt.Fprintf(de.buf, "for %s := range %s {\n", iv, name)
// Sneaky trick so we can use "unknown-ish" structs ([]T) and still get at their fields.
sName := e.Elt.(*ast.Ident).Name
if xx, ok := nonPackets[sName]; ok {
de.writeType(xx, fmt.Sprintf("%s[%s]", name, iv), tag)
} else if sName == "string" {
de.writeField(sName, fmt.Sprintf("%s[%s]", name, iv), tag)
} else {
fmt.Fprintf(de.buf, "// Can't find supporting struct %s for %s.%s\n", sName, de.p.name, name)
}
fmt.Fprint(de.buf, "}\n")
default:
fmt.Fprintf(de.buf, "// Unable to decode: %s (type: %T)\n", name, e)
}
}
func (de *Decoder) writeField(t, name string, tag reflect.StructTag) {
as := tag.Get("as")
if as != "" {
switch as {
case "json":
imports["encoding/json"] = struct{}{}
t := de.T()
fmt.Fprintf(de.buf, `var %[1]s string
if %[1]s, err = packets.ReadString(rr); err != nil { return err }
if err = json.Unmarshal([]byte(%[1]s), &%[2]s); err != nil { return err }
`, t, name)
default:
fmt.Fprintf(de.buf, "// Can't 'as' %s\n", as)
}
return
}
// TODO: For ints, unwrap binary.Read() trickery to reuse []byte tmp.
switch t {
case "bool":
fmt.Fprintf(de.buf, "if %s, err = packets.ReadBool(rr); err != nil { return err }", name)
case "int8", "uint8", "int16", "uint16", "int32", "int64", "float32", "float64":
fmt.Fprintf(de.buf, errWrap("binary.Read(rr, %s, %s)", Endianness, name))
case "string":
fmt.Fprintf(de.buf, "if %s, err = packets.ReadString(rr); err != nil { return err }", name)
case "packets.VarInt":
n := de.T()
fmt.Fprintf(de.buf, "%s, err := packets.ReadVarint(rr)\n", n)
fmt.Fprintf(de.buf, "if err != nil { return err }\n")
fmt.Fprintf(de.buf, "%s = packets.VarInt(%s)\n", name, n)
case "packets.VarLong":
n := de.T()
fmt.Fprintf(de.buf, "%s, err := packets.ReadVarint(rr)\n", n)
fmt.Fprintf(de.buf, "if err != nil { return err }\n")
fmt.Fprintf(de.buf, "%s = packets.VarLong(%s)\n", name, n)
case "packets.Position", "packets.Angle":
// TODO: Do we need to take &%s so it reads into var?
fmt.Fprintf(de.buf, errWrap("binary.Read(rr, %s, %s)", Endianness, name))
case "packets.UUID":
fmt.Fprintf(de.buf, errWrap("binary.Read(rr, %s, %s)", Endianness, name))
case "packets.Chat":
// Covered by the 'as' switch above.
case "packets.Chunk":
fallthrough
case "packets.Metadata":
fallthrough
case "packets.Slot":
fallthrough
case "packets.ObjectData":
fallthrough
case "packets.NBT":
fallthrough
default:
fmt.Fprintf(de.buf, "// Unable to decode: %s (%s)", name, t)
}
fmt.Fprintf(de.buf, "\n")
}