This repository has been archived by the owner on May 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
string_table_state_helper.go
346 lines (295 loc) · 8.47 KB
/
string_table_state_helper.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package yasha
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"github.com/dotabuff/yasha/dota"
"github.com/golang/protobuf/proto"
)
type CacheItem struct {
Bits int
IsFixedSize bool
MaxEntries int
Name string
}
type ModifierBuffs []*dota.CDOTAModifierBuffTableEntry
func (m ModifierBuffs) Len() int { return len(m) }
func (m ModifierBuffs) Less(i, j int) bool { return m[i].GetSerialNum() < m[j].GetSerialNum() }
func (m ModifierBuffs) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
type StateHelper struct {
packets OuterParserBaseItems
lastCreateIndex int
// contains meta information for parsing the table
metaTables map[int]*CacheItem
// the first CSTs
baseTables map[int]*StringTable
// every UST we get, we calculate the ST and put it in here.
evolution map[int][]*StringTable
// every UST we get, we calculate the ST and put it in here.
current map[int]*StringTable
ClassInfosNameMapping map[int]string
ActiveModifierDelta ModifierBuffs
Mapping map[int][]*SendProp
Multiples map[int]map[string]int
Baseline map[int]map[string]interface{}
pendingBaseline []*StringTableItem
}
func NewStateHelper() *StateHelper {
return &StateHelper{
packets: OuterParserBaseItems{},
metaTables: map[int]*CacheItem{},
baseTables: map[int]*StringTable{},
evolution: map[int][]*StringTable{},
current: map[int]*StringTable{},
Baseline: map[int]map[string]interface{}{},
pendingBaseline: []*StringTableItem{},
}
}
func writeStringTables(directory string, tick int, t string) {
err := os.MkdirAll(directory, os.ModePerm|os.ModeDir)
if err != nil {
panic(err)
}
path := fmt.Sprintf("%s/tick_%010d.txt", directory, tick)
err = ioutil.WriteFile(path, []byte(t), 0644)
if err != nil {
panic(err)
}
}
func (helper *StateHelper) AppendPacket(packet *OuterParserBaseItem) {
switch obj := packet.Object.(type) {
case *dota.CDemoStringTables:
// looks like we don't need them, they are just like CST
// helper.OnCDST(packet.Tick, obj)
case *dota.CSVCMsg_CreateStringTable:
helper.packets = append(helper.packets, packet)
helper.OnCST(packet.Tick, obj)
case *dota.CSVCMsg_UpdateStringTable:
helper.packets = append(helper.packets, packet)
helper.OnUST(packet.Tick, obj)
default:
panic("Cannot handle this type")
}
}
func (helper *StateHelper) OnCDST(tick int, obj *dota.CDemoStringTables) {
/*
for _, table := range obj.GetTables() {
writeStringTables("CDemoStringTables/"+table.GetTableName(), tick, spew.Sdump(table))
}
*/
}
func (helper *StateHelper) OnCST(tick int, obj *dota.CSVCMsg_CreateStringTable) {
if tick != 0 {
// tested against a ton of replays, hasn't happened yet...
panic("creating string table after first tick?")
}
helper.metaTables[helper.lastCreateIndex] = &CacheItem{
Bits: int(obj.GetUserDataSizeBits()),
IsFixedSize: obj.GetUserDataFixedSize(),
MaxEntries: int(obj.GetMaxEntries()),
Name: obj.GetName(),
}
table := StringTable{
Index: helper.lastCreateIndex,
Name: obj.GetName(),
Tick: tick,
Items: ParseCST(obj),
}
switch table.Name {
case "ActiveModifiers":
helper.parseActiveModifiers(table.Items)
case "instancebaseline":
helper.updateInstanceBaseline(table.Items)
case "userinfo":
parseUserinfo(table.Items)
default:
// panic("Cannot parse table: " + table.Name)
}
// writeStringTables("CreateStringTable/"+table.Name, tick, spew.Sdump(table))
helper.baseTables[helper.lastCreateIndex] = &table
helper.current[helper.lastCreateIndex] = &table
helper.evolution[helper.lastCreateIndex] = append(helper.evolution[helper.lastCreateIndex], &table)
helper.lastCreateIndex++
}
// NOTE:
// We ignore the "userinfo" table decoding process since it's a PITA and has no useful info anyway.
// In case we ever need it, a struct describing the binary is at:
// https://github.com/mitsuhiko/dota2-demoinfo2/blob/4ca45a87c631787eab140d313a3f21210b543741/demofile.h#L48
func (helper *StateHelper) OnUST(tick int, obj *dota.CSVCMsg_UpdateStringTable) {
tableId := int(obj.GetTableId())
meta := helper.metaTables[tableId]
update := ParseUST(obj, meta)
current := helper.current[tableId]
switch current.Name {
case "ActiveModifiers":
helper.parseActiveModifiers(update)
case "userinfo":
parseUserinfo(update)
case "instancebaseline":
helper.updateInstanceBaseline(update)
default:
// panic("Cannot parse table: " + meta.Name)
}
// writeStringTables("UpdateStringTable/"+current.Name, tick, spew.Sdump(update))
for key, value := range update {
current.Items[key] = value
}
stCopy := &StringTable{
Index: current.Index,
Items: map[int]*StringTableItem{},
Name: current.Name,
Tick: current.Tick,
}
for key, value := range current.Items {
stCopy.Items[key] = value
}
helper.evolution[tableId] = append(helper.evolution[tableId], stCopy)
}
func (helper *StateHelper) updateInstanceBaseline(update map[int]*StringTableItem) {
for _, item := range helper.pendingBaseline {
helper.updateInstanceBaselineItem(item)
}
for _, item := range update {
helper.updateInstanceBaselineItem(item)
}
}
func (helper *StateHelper) updateInstanceBaselineItem(item *StringTableItem) {
classId, err := strconv.Atoi(item.Str)
if err != nil {
panic(err)
}
className := helper.ClassInfosNameMapping[classId]
if className == "DT_DOTAPlayer" {
return
}
mapping := helper.Mapping[classId]
multiples := helper.Multiples[classId]
if len(mapping) == 0 || len(multiples) == 0 {
helper.pendingBaseline = append(helper.pendingBaseline, item)
return
}
baseline, found := helper.Baseline[classId]
if !found {
baseline = map[string]interface{}{}
helper.Baseline[classId] = baseline
}
if len(item.Data) > 0 {
br := NewBitReader(item.Data)
indices := br.ReadPropertiesIndex()
baseValues := br.ReadPropertiesValues(mapping, multiples, indices)
for key, value := range baseValues {
baseline[key] = value
}
}
helper.Baseline[classId] = baseline
}
func (helper *StateHelper) parseActiveModifiers(entries map[int]*StringTableItem) {
for _, e := range entries {
if len(e.Data) > 0 {
o := &dota.CDOTAModifierBuffTableEntry{}
err := proto.Unmarshal(e.Data, o)
if err != nil {
panic(err)
}
e.Data = e.Data[:0]
e.ModifierBuff = o
helper.ActiveModifierDelta = append(helper.ActiveModifierDelta, o)
}
}
}
func (helper *StateHelper) GetStateAtTick(tick int) map[int]*StringTable {
state := map[int]*StringTable{}
for _, evo := range helper.evolution {
for _, table := range evo {
if table.Tick >= tick {
return state
}
state[table.Index] = table
}
}
return state
}
func (helper *StateHelper) GetTableAtTick(tick int, tableName string) (result *StringTable) {
for _, evo := range helper.evolution {
for _, table := range evo {
if table.Name == tableName {
if table.Tick >= tick {
return result
}
result = table
}
}
}
return result
}
func (helper *StateHelper) GetTableNow(tableName string) (result *StringTable) {
for _, table := range helper.current {
if table.Name == tableName {
return table
}
}
return
}
func parseUserinfo(entries map[int]*StringTableItem) {
for _, e := range entries {
if len(e.Data) == 0 {
continue
}
raw := &rawUserinfo{}
buf := bytes.NewBuffer(e.Data)
err := binary.Read(buf, binary.LittleEndian, raw)
info := &Userinfo{}
info.XUID = raw.Xuid
info.UserID = int(raw.UserID)
info.FriendsID = uint(raw.FriendsID)
friendsName := []byte{}
for _, b := range raw.FriendsName {
if b == 0 {
break
}
friendsName = append(friendsName, b)
}
info.FriendsName = string(friendsName)
name := []byte{}
for _, b := range raw.Name {
if b == 0 {
break
}
name = append(name, b)
}
info.Name = string(name)
guid := []byte{}
for _, b := range raw.Guid {
if b == 0 {
break
}
guid = append(guid, b)
}
info.GUID = string(guid)
info.SteamID = guidToCommunityID(info.GUID)
if err != nil {
panic(err)
}
e.Userinfo = info
e.Data = e.Data[:0]
}
}
var (
guidPatter = regexp.MustCompile(`STEAM_(\d+):(\d+):(\d+)`)
steamID64Identifier uint64 = 0x0110000100000000
)
// https://developer.valvesoftware.com/wiki/SteamID
func guidToCommunityID(guid string) uint64 {
matches := guidPatter.FindStringSubmatch(guid)
if len(matches) != 4 {
return 0
}
// x, _ := strconv.Atoi(matches[1])
y, _ := strconv.Atoi(matches[2])
z, _ := strconv.Atoi(matches[3])
return (uint64(z) * 2) + steamID64Identifier + uint64(y)
}