-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
157 lines (140 loc) · 3.31 KB
/
entity.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
package ecs
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"github.com/google/uuid"
)
type Entity uint64
func (e Entity) MarshalBinary() ([]byte, error) {
id := encoderWorld.EntityUUID(e)
if id.Version() < 1 {
return nil, fmt.Errorf("entity %d has no UUID", e)
}
return id[:], nil
}
func (e *Entity) UnmarshalBinary(data []byte) error {
if len(data) != 16 {
return fmt.Errorf("invalid UUID length: %d", len(data))
}
var d [16]byte
copy(d[:], data)
id := uuid.UUID(d)
if id.Version() < 1 {
return fmt.Errorf("invalid UUID")
}
*e = decoderWorld.getEntityByUUID(id)
return nil
}
func (e Entity) MarshalText() (text []byte, err error) {
if e == 0 {
return nil, nil
}
return []byte(encoderWorld.EntityUUID(e).String()), nil
}
func (e *Entity) UnmarshalText(text []byte) error {
if string(text) == "" || string(text) == "00000000-0000-0000-0000-000000000000" {
return nil
}
id, err := uuid.Parse(string(text))
if err != nil {
return err
}
*e = decoderWorld.getEntityByUUID(id)
return nil
}
func (e *Entity) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if s == "" {
return nil
}
if s == "00000000-0000-0000-0000-000000000000" {
return nil
}
id, err := uuid.Parse(s)
if err != nil {
return err
}
*e = decoderWorld.getEntityByUUID(id)
return nil
}
func (e Entity) MarshalJSON() ([]byte, error) {
s := encoderWorld.EntityUUID(e).String()
return json.Marshal(s)
}
type Entities []Entity
func (e Entities) MarshalBinary() ([]byte, error) {
eslice := make([]byte, len(e)*16+8)
offset := binary.PutUvarint(eslice, uint64(len(e)))
if offset != 8 {
panic("unexpected offset")
}
for _, e := range e {
id := encoderWorld.EntityUUID(e)
if id.Version() < 1 {
return nil, fmt.Errorf("entity %d has no UUID", e)
}
copy(eslice[offset:], id[:])
offset += 16
}
return eslice[:], nil
}
func (e *Entities) UnmarshalBinary(data []byte) error {
//TODO: implement (e *Entities) UnmarshalBinary(data []byte) error
return fmt.Errorf("not implemented")
}
func (e Entities) MarshalText() (text []byte, err error) {
slcs := make([]string, 0, len(e))
for _, e := range e {
slcs = append(slcs, encoderWorld.EntityUUID(e).String())
}
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(slcs); err != nil {
return nil, err
}
return []byte(buf.String()), nil
}
func (e *Entities) UnmarshalText(text []byte) error {
slc := make([]string, 0)
if _, err := toml.NewDecoder(bytes.NewReader(text)).Decode(&slc); err != nil {
return err
}
eslc := make([]Entity, 0, len(slc))
for _, s := range slc {
id, err := uuid.Parse(s)
if err == nil {
eslc = append(eslc, decoderWorld.getEntityByUUID(id))
}
}
v := Entities(eslc)
*e = v
return nil
}
func (e *Entities) UnmarshalJSON(b []byte) error {
slc := make([]string, 0)
if err := json.Unmarshal(b, &slc); err != nil {
return err
}
eslc := make([]Entity, 0, len(slc))
for _, s := range slc {
id, err := uuid.Parse(s)
if err == nil {
eslc = append(eslc, decoderWorld.getEntityByUUID(id))
}
}
v := Entities(eslc)
*e = v
return nil
}
func (e Entities) MarshalJSON() ([]byte, error) {
slc := make([]string, 0, len(e))
for _, e := range e {
slc = append(slc, encoderWorld.EntityUUID(e).String())
}
return json.Marshal(slc)
}