forked from Thinkofname/steven-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
186 lines (180 loc) · 3.92 KB
/
metadata.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
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package protocol
import (
"encoding/binary"
"encoding/json"
"errors"
"io"
"strconv"
"github.com/thinkofdeath/steven/format"
)
// Metadata is a simple index -> value map used in the Minecraft protocol.
// A limited number of types are supported:
// int8
// int16
// int32
// float32
// string
// ItemStack
// []int32
// []float32
// VarInt
// UUID
// format.AnyComponent
type Metadata map[int]interface{}
func readMetadata(r io.Reader) (Metadata, error) {
m := make(Metadata)
for {
id, err := ReadByte(r)
if err != nil || id == 0xFF {
return m, err
}
index := int(id)
t, err := ReadByte(r)
if err != nil {
return m, err
}
switch t {
case 0:
var val int8
err = binary.Read(r, binary.BigEndian, &val)
m[index] = val
case 1:
var val VarInt
val, err = ReadVarInt(r)
m[index] = int(val)
case 2:
var val float32
err = binary.Read(r, binary.BigEndian, &val)
m[index] = val
case 3:
m[index], err = ReadString(r)
case 4:
str, err := ReadString(r)
if err != nil {
return m, err
}
var msg format.AnyComponent
err = json.Unmarshal([]byte(str), &msg)
m[index] = msg
case 5:
i := ItemStack{}
err = i.Deserialize(r)
m[index] = i
case 6:
m[index], err = ReadBool(r)
case 7:
var val [3]float32
err = binary.Read(r, binary.BigEndian, &val)
m[index] = val
case 8:
var val Position
err = binary.Read(r, binary.BigEndian, &val)
m[index] = val
case 9:
var ok bool
ok, err = ReadBool(r)
if err != nil {
return m, err
}
pos := new(Position)
if ok {
err = binary.Read(r, binary.BigEndian, pos)
}
m[index] = pos
case 10: // Direction
m[index], err = ReadVarInt(r)
case 11:
var ok bool
ok, err = ReadBool(r)
if err != nil {
return m, err
}
uuid := new(UUID)
if ok {
err = uuid.Deserialize(r)
}
m[index] = uuid
case 12:
var id VarInt
id, err = ReadVarInt(r)
m[index] = uint16(id)
default:
err = errors.New("invalid metadata type " + strconv.Itoa(int(t)))
}
if err != nil {
return m, err
}
}
}
func writeMetadata(w io.Writer, m Metadata) error {
for index, v := range m {
err := WriteByte(w, byte(index))
if err != nil {
return err
}
switch v := v.(type) {
case int8:
WriteByte(w, 0)
err = binary.Write(w, binary.BigEndian, v)
case int:
WriteByte(w, 1)
err = WriteVarInt(w, VarInt(v))
case float32:
WriteByte(w, 2)
err = binary.Write(w, binary.BigEndian, v)
case string:
WriteByte(w, 3)
err = WriteString(w, v)
case format.AnyComponent:
WriteByte(w, 4)
val, _ := json.Marshal(v)
err = WriteString(w, string(val))
case ItemStack:
WriteByte(w, 5)
v.Serialize(w)
case bool:
WriteByte(w, 6)
err = WriteBool(w, v)
case [3]float32:
WriteByte(w, 7)
err = binary.Write(w, binary.BigEndian, v)
case Position:
WriteByte(w, 8)
err = binary.Write(w, binary.BigEndian, v)
case *Position:
WriteByte(w, 9)
WriteBool(w, v != nil)
if v != nil {
err = binary.Write(w, binary.BigEndian, v)
}
case VarInt: // Direction
WriteByte(w, 10)
err = WriteVarInt(w, v)
case *UUID:
WriteByte(w, 11)
WriteBool(w, v != nil)
if v != nil {
v.Serialize(w)
}
case uint16:
WriteVarInt(w, VarInt(v))
}
if err != nil {
return err
}
}
return WriteByte(w, 0xFF)
}