-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.go
140 lines (130 loc) · 2.88 KB
/
compose.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
package nbtreader
import (
"encoding/binary"
"io"
"math"
)
func (t Byte) compose(w io.Writer) error {
return pushByte(w, t)
}
func (t Short) compose(w io.Writer) error {
return pushShort(w, t)
}
func (t Int) compose(w io.Writer) error {
return pushInt(w, t)
}
func (t Long) compose(w io.Writer) error {
return pushLong(w, t)
}
func (t Float) compose(w io.Writer) error {
return pushFloat(w, t)
}
func (t Double) compose(w io.Writer) error {
return pushDouble(w, t)
}
func (t ByteArray) compose(w io.Writer) error {
if err := pushInt(w, len(t)); err != nil {
return err
}
for _, b := range t {
if err := pushByte(w, b); err != nil {
return err
}
}
return nil
}
func (t String) compose(w io.Writer) error {
return pushString(w, t)
}
func (t List) compose(w io.Writer) error {
itemCap := len(t.Elements)
if itemCap == 0 {
if err := pushByte(w, Tag_End); err != nil {
return err
}
return pushInt(w, 0)
}
if err := pushByte(w, t.TagType); err != nil {
return err
}
if err := pushInt(w, itemCap); err != nil {
return err
}
for _, entry := range t.Elements {
if err := entry.compose(w); err != nil {
return err
}
}
return nil
}
func (t Compound) compose(w io.Writer) error {
for _, tag := range t.getOrdered() {
if err := pushByte(w, tag.Value.Type()); err != nil {
return err
}
if err := pushString(w, tag.Key); err != nil {
return nil
}
if err := tag.Value.compose(w); err != nil {
return err
}
}
return pushByte(w, Tag_End)
}
func (t IntArray) compose(w io.Writer) error {
if err := pushInt(w, len(t)); err != nil {
return err
}
for _, i := range t {
if err := pushInt(w, i); err != nil {
return err
}
}
return nil
}
func (t LongArray) compose(w io.Writer) error {
if err := pushInt(w, len(t)); err != nil {
return err
}
for _, l := range t {
if err := pushLong(w, l); err != nil {
return err
}
}
return nil
}
func pushByte[B Byte | int8 | TagType](w io.Writer, b B) error {
_, err := w.Write([]byte{byte(b)})
return err
}
func pushShort[S Short | int16 | uint8](w io.Writer, s S) error {
var buf [2]byte
binary.BigEndian.PutUint16(buf[:], uint16(s))
_, err := w.Write(buf[:])
return err
}
func pushInt[I Int | int32 | uint16 | int](w io.Writer, i I) error {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(i))
_, err := w.Write(buf[:])
return err
}
func pushLong[L Long | int64 | uint32 | int](w io.Writer, l L) error {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], uint64(l))
_, err := w.Write(buf[:])
return err
}
func pushFloat[F Float | float32](w io.Writer, f F) error {
return pushInt(w, Int(math.Float32bits(float32(f))))
}
func pushDouble[D Double | float64](w io.Writer, d D) error {
return pushLong(w, Long(math.Float64bits(float64(d))))
}
func pushString(w io.Writer, s String) error {
if err := pushShort(w, s.Len()); err != nil {
return err
}
_, err := w.Write([]byte(s))
return err
}