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
/
send_tables_helper.go
201 lines (181 loc) · 4.98 KB
/
send_tables_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
package yasha
import (
"sort"
"github.com/dotabuff/yasha/dota"
)
type DPTType int
const (
DPT_Int DPTType = iota
DPT_Float
DPT_Vector
DPT_VectorXY
DPT_String
DPT_Array
DPT_DataTable
DPT_Int64
DPT_NUMSendPropTypes
)
type Flag int
const (
SPROP_UNSIGNED Flag = 1 << 0
SPROP_COORD Flag = 1 << 1
SPROP_NOSCALE Flag = 1 << 2
SPROP_ROUNDDOWN Flag = 1 << 3
SPROP_ROUNDUP Flag = 1 << 4
SPROP_NORMAL Flag = 1 << 5
SPROP_EXCLUDE Flag = 1 << 6
SPROP_XYZE Flag = 1 << 7
SPROP_INSIDEARRAY Flag = 1 << 8
SPROP_PROXY_ALWAYS_YES Flag = 1 << 9
SPROP_IS_A_VECTOR_ELEM Flag = 1 << 10
SPROP_COLLAPSIBLE Flag = 1 << 11
SPROP_COORD_MP Flag = 1 << 12
SPROP_COORD_MP_LOWPRECISION Flag = 1 << 13
SPROP_COORD_MP_INTEGRAL Flag = 1 << 14
SPROP_CELL_COORD Flag = 1 << 15
SPROP_CELL_COORD_LOWPRECISION Flag = 1 << 16
SPROP_CELL_COORD_INTEGRAL Flag = 1 << 17
SPROP_CHANGES_OFTEN Flag = 1 << 18
SPROP_ENCODED_AGAINST_TICKCOUNT Flag = 1 << 19
)
type SendProp struct {
DtName string
VarName string
Type DPTType
Flags Flag
Priority int
NumBits int
LowValue float64
HighValue float64
}
type Helper struct {
sendTables map[string]*dota.CSVCMsg_SendTable
flatSendTable []*SendProp
excludedSendProp []*SendProp
}
func NewSendTablesHelper() *Helper {
return &Helper{
sendTables: map[string]*dota.CSVCMsg_SendTable{},
flatSendTable: []*SendProp{},
excludedSendProp: []*SendProp{},
}
}
func (sth *Helper) SetSendTable(name string, table *dota.CSVCMsg_SendTable) {
sth.sendTables[name] = table
}
func (sth *Helper) LoadSendTable(sendTableName string) []*SendProp {
sth.flatSendTable = []*SendProp{}
sth.excludedSendProp = []*SendProp{}
sth.excludedSendProp = sth.getPropsExcluded(sendTableName)
sth.buildHierarchy(sendTableName)
sth.sortByPriority()
return sth.flatSendTable
}
func (sth *Helper) getPropsExcluded(sendTableName string) []*SendProp {
result := []*SendProp{}
sendTable := sth.sendTables[sendTableName]
for _, prop := range sendTable.GetProps() {
flags := prop.GetFlags()
if (flags & int32(SPROP_EXCLUDE)) != 0 {
result = append(result, &SendProp{
Flags: Flag(flags),
DtName: prop.GetDtName(),
NumBits: int(prop.GetNumBits()),
Priority: int(prop.GetPriority()),
Type: DPTType(prop.GetType()),
VarName: prop.GetVarName(),
})
}
}
for _, prop := range sendTable.GetProps() {
if prop.GetType() == 6 {
result = append(result, sth.getPropsExcluded(prop.GetDtName())...)
}
}
return result
}
func (sth *Helper) buildHierarchy(sendTableName string) {
result := []*SendProp{}
sth.buildHierarchyIterateProps(sendTableName, &result)
sth.flatSendTable = append(sth.flatSendTable, result...)
}
func (sth *Helper) buildHierarchyIterateProps(sendTableName string, result *[]*SendProp) {
pTable := sth.sendTables[sendTableName]
for _, pProp := range pTable.GetProps() {
pFlags := pProp.GetFlags()
pType := pProp.GetType()
if pFlags&int32(SPROP_EXCLUDE) != 0 ||
pType == int32(DPT_Array) ||
sth.hasExcludedSendProp(sendTableName, pProp.GetVarName()) {
continue
}
if pType == int32(DPT_DataTable) {
if pFlags&int32(SPROP_COLLAPSIBLE) != 0 {
sth.buildHierarchyIterateProps(pProp.GetDtName(), result)
} else {
sth.buildHierarchy(pProp.GetDtName())
}
} else {
*result = append(*result, &SendProp{
DtName: sendTableName,
Flags: Flag(pProp.GetFlags()),
NumBits: int(pProp.GetNumBits()),
Priority: int(pProp.GetPriority()),
Type: DPTType(pProp.GetType()),
VarName: pProp.GetVarName(),
LowValue: float64(pProp.GetLowValue()),
HighValue: float64(pProp.GetHighValue()),
})
}
}
}
func (sth *Helper) hasExcludedSendProp(sendTableName string, pVarName string) bool {
for _, p := range sth.excludedSendProp {
if p.DtName == sendTableName && p.VarName == pVarName {
return true
}
}
return false
}
func (sth *Helper) sortByPriority() {
priorities := []int{}
has64 := false
for _, prop := range sth.flatSendTable {
priority := prop.Priority
unique := true
for _, existing := range priorities {
if priority == existing {
unique = false
break
}
}
if unique {
has64 = has64 || priority == 64
priorities = append(priorities, priority)
}
}
if !has64 {
priorities = append(priorities, 64)
}
sort.Ints(priorities)
start := 0
for _, priority := range priorities {
i := 0
for {
for i = start; i < len(sth.flatSendTable); i++ {
p := sth.flatSendTable[i]
if priority == p.Priority || ((p.Flags&SPROP_CHANGES_OFTEN) == SPROP_CHANGES_OFTEN) && priority == 64 {
if i != start {
sth.flatSendTable[i] = sth.flatSendTable[start]
sth.flatSendTable[start] = p
}
start++
break
}
}
if i == len(sth.flatSendTable) {
break
}
}
}
}