forked from dotabuff/manta
/
flattened_serializers.go
198 lines (163 loc) · 5.16 KB
/
flattened_serializers.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
package manta
import (
"encoding/json"
"github.com/dotabuff/manta/dota"
"github.com/golang/protobuf/proto"
)
// Field is always filled, table only for sub-tables
type dt_property struct {
Field *dt_field
Table *dt
}
// A datatable field
type dt_field struct {
Name string
Type string
Index int32
Flags *int32
BitCount *int32
LowValue *float32
HighValue *float32
Version *int32
Serializer *PropertySerializer `json:"-"`
}
// A single datatable
type dt struct {
Name string
Flags *int32
Version int32
Properties []*dt_property
}
// The flattened serializers object
type flattened_serializers struct {
Serializers map[string]map[int32]*dt // serializer name -> [versions]
proto *dota.CSVCMsg_FlattenedSerializer
pst *PropertySerializerTable
}
// Dumps a flattened table as json
func (sers *flattened_serializers) dump_json(name string) string {
// Can't marshal map[int32]x
type jContainer struct {
Version int32
Data *dt
}
j := make([]jContainer, 0)
for i, o := range sers.Serializers[name] {
j = append(j, jContainer{i, o})
}
str, _ := json.MarshalIndent(j, "", " ") // two space ident
return string(str)
}
// Fills properties for a data table
func (sers *flattened_serializers) recurse_table(cur *dota.ProtoFlattenedSerializerT) *dt {
// Basic table structure
table := &dt{
Name: sers.proto.GetSymbols()[cur.GetSerializerNameSym()],
Version: cur.GetSerializerVersion(),
Properties: make([]*dt_property, 0),
}
props := sers.proto.GetFields()
// Append all the properties
for _, idx := range cur.GetFieldsIndex() {
pField := props[idx]
prop := &dt_property{nil, nil}
// Field can always be set
prop.Field = &dt_field{
Name: sers.proto.GetSymbols()[pField.GetVarNameSym()],
Index: -1,
Flags: pField.EncodeFlags,
BitCount: pField.BitCount,
LowValue: pField.LowValue,
HighValue: pField.HighValue,
Type: (sers.proto.GetSymbols()[pField.GetVarTypeSym()]),
Version: pField.FieldSerializerVersion,
Serializer: sers.pst.GetPropertySerializerByName(sers.proto.GetSymbols()[pField.GetVarTypeSym()]),
}
// Optional: Attach the serializer version for the property if applicable
if pField.FieldSerializerNameSym != nil {
pFieldName := sers.proto.GetSymbols()[pField.GetFieldSerializerNameSym()]
pFieldVersion := pField.GetFieldSerializerVersion()
pSerializer := sers.Serializers[pFieldName][pFieldVersion]
if pSerializer == nil {
_panicf("Error: Serializer version %d for %s hasn't been added yet.", pFieldVersion, pFieldName)
}
prop.Table = pSerializer
}
// Optional: Adjust array fields
if prop.Field.Serializer.IsArray {
// Add our own temp table for the array
tmpDt := &dt{
Name: prop.Field.Name,
Flags: nil,
Version: 0,
Properties: make([]*dt_property, 0),
}
// Add each array field to the table
for i := uint32(0); i < prop.Field.Serializer.Length; i++ {
tmpDt.Properties = append(tmpDt.Properties, &dt_property{
Field: &dt_field{
Name: _sprintf("%04d", i),
Type: prop.Field.Serializer.Name,
Index: int32(i),
Flags: prop.Field.Flags,
BitCount: prop.Field.BitCount,
LowValue: prop.Field.LowValue,
HighValue: prop.Field.HighValue,
Version: prop.Field.Version,
Serializer: prop.Field.Serializer.ArraySerializer,
},
Table: prop.Table, // This carries on the actual table instead of overriding it
})
// Copy parent prop to rename it's name according to the array index
if prop.Table != nil {
nTable := *prop.Table
nTable.Name = _sprintf("%04d", i)
tmpDt.Properties[len(tmpDt.Properties)-1].Table = &nTable
}
}
prop.Table = tmpDt
}
table.Properties = append(
table.Properties,
prop,
)
}
return table
}
// Parses a CDemoSendTables packet
func ParseSendTables(m *dota.CDemoSendTables, pst *PropertySerializerTable) *flattened_serializers {
// This packet just contains a single large buffer
r := NewReader(m.GetData())
// The buffer starts with a varint encoded length
size := int(r.readVarUint32())
if size != r.remBytes() {
_panicf("expected %d additional bytes, got %d", size, r.remBytes())
}
// Read the rest of the buffer as a CSVCMsg_FlattenedSerializer.
buf := r.readBytes(size)
msg := &dota.CSVCMsg_FlattenedSerializer{}
if err := proto.Unmarshal(buf, msg); err != nil {
_panicf("cannot decode proto: %s", err)
}
// Create the flattened_serializers object and fill it
fs := &flattened_serializers{
Serializers: make(map[string]map[int32]*dt),
proto: msg,
pst: pst,
}
// Iterate through all flattened serializers and fill their properties
for _, o := range msg.GetSerializers() {
sName := msg.GetSymbols()[o.GetSerializerNameSym()]
sVer := o.GetSerializerVersion()
if fs.Serializers[sName] == nil {
fs.Serializers[sName] = make(map[int32]*dt)
}
fs.Serializers[sName][sVer] = fs.recurse_table(o)
}
return fs
}
// Internal callback for OnCDemoSendTables.
func (p *Parser) onCDemoSendTables(m *dota.CDemoSendTables) error {
p.Serializers = ParseSendTables(m, GetDefaultPropertySerializerTable()).Serializers
return nil
}