-
Notifications
You must be signed in to change notification settings - Fork 104
/
string_table.go
296 lines (254 loc) · 7.73 KB
/
string_table.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
package manta
import (
"github.com/dotabuff/manta/dota"
"github.com/golang/snappy"
)
const (
stringtableKeyHistorySize = 32
)
// Holds and maintains the string table information for an
// instance of the Parser.
type stringTables struct {
Tables map[int32]*stringTable
NameIndex map[string]int32
nextIndex int32
}
// Retrieves a string table by its name. Check the bool.
func (ts *stringTables) GetTableByName(name string) (*stringTable, bool) {
i, ok := ts.NameIndex[name]
if !ok {
return nil, false
}
t, ok := ts.Tables[i]
return t, ok
}
// Creates a new empty StringTables.
func newStringTables() *stringTables {
return &stringTables{
Tables: make(map[int32]*stringTable),
NameIndex: make(map[string]int32),
nextIndex: 0,
}
}
// Holds and maintains the information for a string table.
type stringTable struct {
index int32
name string
Items map[int32]*stringTableItem
userDataFixedSize bool
userDataSize int32
flags int32
varintBitCounts bool
}
func (st *stringTable) GetIndex() int32 { return st.index }
func (st *stringTable) GetName() string { return st.name }
func (st *stringTable) GetItem(index int32) *stringTableItem { return st.Items[index] }
// Holds and maintains a single entry in a string table.
type stringTableItem struct {
Index int32
Key string
Value []byte
}
// Internal callback for CDemoStringTables.
// These appear to be periodic state dumps and appear every 1800 outer ticks.
// XXX TODO: decide if we want to at all integrate these updates,
// or trust create/update entirely. Let's ignore them for now.
func (p *Parser) onCDemoStringTables(m *dota.CDemoStringTables) error {
return nil
}
// Internal callback for CSVCMsg_CreateStringTable.
// XXX TODO: This is currently using an artificial, internally crafted message.
// This should be replaced with the real message once we have updated protos.
func (p *Parser) onCSVCMsg_CreateStringTable(m *dota.CSVCMsg_CreateStringTable) error {
// Create a new string table at the next index position
t := &stringTable{
index: p.stringTables.nextIndex,
name: m.GetName(),
Items: make(map[int32]*stringTableItem),
userDataFixedSize: m.GetUserDataFixedSize(),
userDataSize: m.GetUserDataSize(),
flags: m.GetFlags(),
varintBitCounts: m.GetUsingVarintBitcounts(),
}
// Increment the index
p.stringTables.nextIndex += 1
// Decompress the data if necessary
buf := m.GetStringData()
if m.GetDataCompressed() {
// old replays = lzss
// new replays = snappy
r := newReader(buf)
var err error
if s := r.readStringN(4); s != "LZSS" {
if buf, err = snappy.Decode(nil, buf); err != nil {
return err
}
} else {
if buf, err = unlzss(buf); err != nil {
return err
}
}
}
// Parse the items out of the string table data
items := parseStringTable(buf, m.GetNumEntries(), t.name, t.userDataFixedSize, t.userDataSize, t.flags, t.varintBitCounts)
// Insert the items into the table
for _, item := range items {
t.Items[item.Index] = item
}
// Add the table to the parser state
p.stringTables.Tables[t.index] = t
p.stringTables.NameIndex[t.name] = t.index
// Apply the updates to baseline state
if t.name == "instancebaseline" {
p.updateInstanceBaseline()
}
// Emit events for modifier table entry updates
if t.name == "ActiveModifiers" {
if err := p.emitModifierTableEvents(items); err != nil {
return err
}
}
return nil
}
// Internal callback for CSVCMsg_UpdateStringTable.
func (p *Parser) onCSVCMsg_UpdateStringTable(m *dota.CSVCMsg_UpdateStringTable) error {
// TODO: integrate
t, ok := p.stringTables.Tables[m.GetTableId()]
if !ok {
_panicf("missing string table %d", m.GetTableId())
}
if v(5) {
_debugf("tick=%d name=%s changedEntries=%d size=%d", p.Tick, t.name, m.GetNumChangedEntries(), len(m.GetStringData()))
}
// Parse the updates out of the string table data
items := parseStringTable(m.GetStringData(), m.GetNumChangedEntries(), t.name, t.userDataFixedSize, t.userDataSize, t.flags, t.varintBitCounts)
// Apply the updates to the parser state
for _, item := range items {
index := item.Index
if _, ok := t.Items[index]; ok {
if item.Key != "" && item.Key != t.Items[index].Key {
t.Items[index].Key = item.Key
}
if len(item.Value) > 0 {
t.Items[index].Value = item.Value
}
} else {
t.Items[index] = item
}
}
// Apply the updates to baseline state
if t.name == "instancebaseline" {
p.updateInstanceBaseline()
}
// Emit events for modifier table entry updates
if t.name == "ActiveModifiers" {
if err := p.emitModifierTableEvents(items); err != nil {
return err
}
}
return nil
}
// Parse a string table data blob, returning a list of item updates.
func parseStringTable(buf []byte, numUpdates int32, name string, userDataFixed bool, userDataSize int32, flags int32, varintBitCounts bool) (items []*stringTableItem) {
defer func() {
if err := recover(); err != nil {
_debugf("warning: unable to parse string table %s: %s", name, err)
return
}
}()
items = make([]*stringTableItem, 0)
// Create a reader for the buffer
r := newReader(buf)
// Start with an index of -1.
// If the first item is at index 0 it will use a incr operation.
index := int32(-1)
// Maintain a list of key history
keys := make([]string, 0, stringtableKeyHistorySize)
// Some tables have no data
if len(buf) == 0 {
return items
}
// Loop through entries in the data structure
//
// Each entry is a tuple consisting of {index, key, value}
//
// Index can either be incremented from the previous position or
// overwritten with a given entry.
//
// Key may be omitted (will be represented here as "")
//
// Value may be omitted
for i := 0; i < int(numUpdates); i++ {
key := ""
value := []byte{}
// Read a boolean to determine whether the operation is an increment or
// has a fixed index position. A fixed index position of zero should be
// the last data in the buffer, and indicates that all data has been read.
incr := r.readBoolean()
if incr {
index++
} else {
index = int32(r.readVarUint32()) + 1
}
// Some values have keys, some don't.
hasKey := r.readBoolean()
if hasKey {
// Some entries use reference a position in the key history for
// part of the key. If referencing the history, read the position
// and size from the buffer, then use those to build the string
// combined with an extra string read (null terminated).
// Alternatively, just read the string.
useHistory := r.readBoolean()
if useHistory {
pos := r.readBits(5)
size := r.readBits(5)
if int(pos) >= len(keys) {
key += r.readString()
} else {
s := keys[pos]
if int(size) > len(s) {
key += s + r.readString()
} else {
key += s[0:size] + r.readString()
}
}
} else {
key = r.readString()
}
if len(keys) >= stringtableKeyHistorySize {
copy(keys[0:], keys[1:])
keys[len(keys)-1] = ""
keys = keys[:len(keys)-1]
}
keys = append(keys, key)
}
// Some entries have a value.
hasValue := r.readBoolean()
if hasValue {
bitSize := uint32(0)
isCompressed := false
if userDataFixed {
bitSize = uint32(userDataSize)
} else {
if (flags & 0x1) != 0 {
isCompressed = r.readBoolean()
}
if varintBitCounts {
bitSize = r.readUBitVar() * 8
} else {
bitSize = r.readBits(17) * 8
}
}
value = r.readBitsAsBytes(bitSize)
if isCompressed {
tmp, err := snappy.Decode(nil, value)
if err != nil {
_panicf("unable to decode snappy compressed stringtable item (%s, %d, %s): %s", name, index, key, err)
}
value = tmp
}
}
items = append(items, &stringTableItem{index, key, value})
}
return items
}