-
Notifications
You must be signed in to change notification settings - Fork 90
/
class.go
118 lines (91 loc) · 2.69 KB
/
class.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
package sendtables2
import (
"fmt"
"strings"
st "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/sendtables"
)
type fpNameTreeCache struct {
next map[int]*fpNameTreeCache
name string
}
type class struct {
classId int32
name string
serializer *serializer
createdHandlers []st.EntityCreatedHandler
fpNameCache *fpNameTreeCache
}
func (c *class) ID() int {
return int(c.classId)
}
func (c *class) Name() string {
return c.name
}
func (c *class) DataTableID() int {
panic("not implemented")
}
func (c *class) DataTableName() string {
panic("not implemented")
}
func (c *class) BaseClasses() (res []st.ServerClass) {
panic("not implemented")
}
func (c *class) PropertyEntries() []string {
return c.collectFieldsEntries(c.serializer.fields, "")
}
func (c *class) PropertyEntryDefinitions() []st.PropertyEntry {
panic("not implemented")
}
func (c *class) OnEntityCreated(handler st.EntityCreatedHandler) {
c.createdHandlers = append(c.createdHandlers, handler)
}
func (c *class) String() string {
props := make([]string, 0, len(c.serializer.fields))
for _, f := range c.serializer.fields {
props = append(props, fmt.Sprintf("%s: %s", f.varName, f.varType))
}
return fmt.Sprintf("%d %s\n %s", c.classId, c.name, strings.Join(props, "\n "))
}
func (c *class) collectFieldsEntries(fields []*field, prefix string) []string {
paths := make([]string, 0)
for _, field := range fields {
if field.serializer != nil {
subPaths := c.collectFieldsEntries(field.serializer.fields, prefix+field.serializer.name+".")
paths = append(paths, subPaths...)
} else {
paths = append(paths, prefix+field.varName)
}
}
return paths
}
func (c *class) getNameForFieldPath(fp *fieldPath) string {
currentCacheNode := c.fpNameCache
for i := 0; i <= fp.last; i++ {
if currentCacheNode.next == nil {
currentCacheNode.next = make(map[int]*fpNameTreeCache)
}
pos := fp.path[i]
next, exists := currentCacheNode.next[pos]
if !exists {
next = &fpNameTreeCache{}
currentCacheNode.next[pos] = next
}
currentCacheNode = next
}
if currentCacheNode.name == "" {
currentCacheNode.name = strings.Join(c.serializer.getNameForFieldPath(fp, 0), ".")
}
return currentCacheNode.name
}
func (c *class) getTypeForFieldPath(fp *fieldPath) *fieldType {
return c.serializer.getTypeForFieldPath(fp, 0)
}
func (c *class) getDecoderForFieldPath(fp *fieldPath) fieldDecoder {
return c.serializer.getDecoderForFieldPath(fp, 0)
}
func (c *class) getFieldPathForName(fp *fieldPath, name string) bool {
return c.serializer.getFieldPathForName(fp, name)
}
func (c *class) getFieldPaths(fp *fieldPath, state *fieldState) []*fieldPath {
return c.serializer.getFieldPaths(fp, state)
}