-
Notifications
You must be signed in to change notification settings - Fork 104
/
entity.go
321 lines (269 loc) · 7.52 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package manta
import (
"fmt"
"github.com/dotabuff/manta/dota"
)
// EntityOp is a bitmask representing the type of operation performed on an Entity
type EntityOp int
const (
EntityOpNone EntityOp = 0x00
EntityOpCreated EntityOp = 0x01
EntityOpUpdated EntityOp = 0x02
EntityOpDeleted EntityOp = 0x04
EntityOpEntered EntityOp = 0x08
EntityOpLeft EntityOp = 0x10
EntityOpCreatedEntered EntityOp = EntityOpCreated | EntityOpEntered
EntityOpUpdatedEntered EntityOp = EntityOpUpdated | EntityOpEntered
EntityOpDeletedLeft EntityOp = EntityOpDeleted | EntityOpLeft
)
var entityOpNames = map[EntityOp]string{
EntityOpNone: "None",
EntityOpCreated: "Created",
EntityOpUpdated: "Updated",
EntityOpDeleted: "Deleted",
EntityOpEntered: "Entered",
EntityOpLeft: "Left",
EntityOpCreatedEntered: "Created+Entered",
EntityOpUpdatedEntered: "Updated+Entered",
EntityOpDeletedLeft: "Deleted+Left",
}
// Flag determines whether an EntityOp includes another. This is primarily
// offered to prevent bit flag errors in downstream clients.
func (o EntityOp) Flag(p EntityOp) bool {
return o&p != 0
}
// String returns a human identifiable string for the EntityOp
func (o EntityOp) String() string {
return entityOpNames[o]
}
// EntityHandler is a function that receives Entity updates
type EntityHandler func(*Entity, EntityOp) error
// Entity represents a single game entity in the replay
type Entity struct {
index int32
serial int32
class *class
active bool
state *fieldState
fpCache map[string]*fieldPath
fpNoop map[string]bool
}
// newEntity returns a new entity for the given index, serial and class
func newEntity(index, serial int32, class *class) *Entity {
return &Entity{
index: index,
serial: serial,
class: class,
active: true,
state: newFieldState(),
fpCache: make(map[string]*fieldPath),
fpNoop: make(map[string]bool),
}
}
// String returns a human identifiable string for the Entity
func (e *Entity) String() string {
return fmt.Sprintf("%d <%s>", e.index, e.class.name)
}
// Map returns a map of current entity state as key-value pairs
func (e *Entity) Map() map[string]interface{} {
values := make(map[string]interface{})
for _, fp := range e.class.getFieldPaths(newFieldPath(), e.state) {
values[e.class.getNameForFieldPath(fp)] = e.state.get(fp)
}
return values
}
// Dump prints the current entity state to standard output
func (e *Entity) Dump() {
_dump(e.String(), e.Map())
}
// Get returns the current value of the Entity state for the given key
func (e *Entity) Get(name string) interface{} {
if fp, ok := e.fpCache[name]; ok {
return e.state.get(fp)
}
if e.fpNoop[name] {
return nil
}
fp := newFieldPath()
if !e.class.getFieldPathForName(fp, name) {
e.fpNoop[name] = true
fp.release()
return nil
}
e.fpCache[name] = fp
return e.state.get(fp)
}
// Exists returns true if the given key exists in the Entity state
func (e *Entity) Exists(name string) bool {
return e.Get(name) != nil
}
// GetInt32 gets given key as an int32
func (e *Entity) GetInt32(name string) (int32, bool) {
x, ok := e.Get(name).(int32)
return x, ok
}
// GetUint32 gets given key as a uint32
func (e *Entity) GetUint32(name string) (uint32, bool) {
if v := e.Get(name); v != nil {
switch x := v.(type) {
case uint32:
return x, true
case uint64:
return uint32(x), true
}
}
return 0, false
}
// GetUint64 gets given key as a uint64
func (e *Entity) GetUint64(name string) (uint64, bool) {
x, ok := e.Get(name).(uint64)
return x, ok
}
// GetFloat32 gets given key as an float32
func (e *Entity) GetFloat32(name string) (float32, bool) {
x, ok := e.Get(name).(float32)
return x, ok
}
// GetString gets given key as a string
func (e *Entity) GetString(name string) (string, bool) {
x, ok := e.Get(name).(string)
return x, ok
}
// GetBool gets given key as a bool
func (e *Entity) GetBool(name string) (bool, bool) {
x, ok := e.Get(name).(bool)
return x, ok
}
// GetSerial return the serial of the class associated with this Entity
func (e *Entity) GetSerial() int32 {
return e.serial
}
// GetClassId returns the id of the class associated with this Entity
func (e *Entity) GetClassId() int32 {
return e.class.classId
}
// GetClassName returns the name of the class associated with this Entity
func (e *Entity) GetClassName() string {
return e.class.name
}
// GetIndex returns the index of this Entity
func (e *Entity) GetIndex() int32 {
return e.index
}
// FindEntity finds a given Entity by index
func (p *Parser) FindEntity(index int32) *Entity {
return p.entities[index]
}
const (
// SOURCE2
indexBits uint64 = 14
handleMask uint64 = (1 << indexBits) - 1
)
func handle2idx(handle uint64) int32 {
return int32(handle & handleMask)
}
func serialForHandle(handle uint64) int32 {
return int32(handle >> indexBits)
}
// FindEntityByHandle finds a given Entity by handle
func (p *Parser) FindEntityByHandle(handle uint64) *Entity {
idx := handle2idx(handle)
e := p.FindEntity(idx)
if e != nil && e.GetSerial() != serialForHandle(handle) {
return nil
}
return e
}
// FilterEntity finds entities by callback
func (p *Parser) FilterEntity(fb func(*Entity) bool) []*Entity {
entities := make([]*Entity, 0, 0)
for _, et := range p.entities {
if fb(et) {
entities = append(entities, et)
}
}
return entities
}
// Internal Callback for OnCSVCMsg_PacketEntities.
func (p *Parser) onCSVCMsg_PacketEntities(m *dota.CSVCMsg_PacketEntities) error {
r := newReader(m.GetEntityData())
var index = int32(-1)
var updates = int(m.GetUpdatedEntries())
var cmd uint32
var classId int32
var serial int32
var e *Entity
var op EntityOp
if !m.GetLegacyIsDelta() {
if p.entityFullPackets > 0 {
return nil
}
p.entityFullPackets++
}
type tuple struct {
e *Entity
op EntityOp
}
tuples := make([]tuple, 0, updates)
for ; updates > 0; updates-- {
index += int32(r.readUBitVar()) + 1
op = EntityOpNone
cmd = r.readBits(2)
if cmd&0x01 == 0 {
if cmd&0x02 != 0 {
classId = int32(r.readBits(p.classIdSize))
serial = int32(r.readBits(17))
r.readVarUint32()
class := p.classesById[classId]
if class == nil {
_panicf("unable to find new class %d", classId)
}
baseline := p.classBaselines[classId]
if baseline == nil {
_panicf("unable to find new baseline %d", classId)
}
e = newEntity(index, serial, class)
p.entities[index] = e
readFields(newReader(baseline), class.serializer, e.state)
readFields(r, class.serializer, e.state)
op = EntityOpCreated | EntityOpEntered
} else {
if e = p.entities[index]; e == nil {
_panicf("unable to find existing entity %d", index)
}
op = EntityOpUpdated
if !e.active {
e.active = true
op |= EntityOpEntered
}
readFields(r, e.class.serializer, e.state)
}
} else {
if e = p.entities[index]; e == nil {
_panicf("unable to find existing entity %d", index)
}
if !e.active {
_panicf("entity %d (%s) ordered to leave, already inactive", e.class.classId, e.class.name)
}
op = EntityOpLeft
if cmd&0x02 != 0 {
op |= EntityOpDeleted
p.entities[index] = nil
}
}
tuples = append(tuples, tuple{e, op})
}
for _, h := range p.entityHandlers {
for _, t := range tuples {
if err := h(t.e, t.op); err != nil {
return err
}
}
}
return nil
}
// OnEntity registers an EntityHandler that will be called when an entity
// is created, updated, deleted, etc.
func (p *Parser) OnEntity(h EntityHandler) {
p.entityHandlers = append(p.entityHandlers, h)
}