aboutsummaryrefslogtreecommitdiff
path: root/pkg/classes/dataTables.go
blob: c17e5f628c489c5f8b7f5c3a17ac3e309ed2494e (plain) (blame)
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
package classes

import (
	"fmt"

	"github.com/pektezol/bitreader"
)

type DataTables struct {
	Size            int32
	SendTable       []SendTable
	ServerClassInfo []ServerClassInfo
}

type SendTable struct {
	NeedsDecoder bool
	NetTableName string
	NumOfProps   int16
	Props        []prop
}

type ServerClassInfo struct {
	ClassId       uint16
	ClassName     string
	DataTableName string
}

type prop struct {
	SendPropType  sendPropType
	SendPropName  string
	SendPropFlags uint32
	Priority      uint8
	ExcludeDtName string
	LowValue      float32
	HighValue     float32
	NumBits       int32
	NumElements   int32
}

func (dataTables *DataTables) ParseDataTables(reader *bitreader.Reader) {
	dataTables.Size = int32(reader.TryReadSInt32())
	dataTableReader := bitreader.NewReaderFromBytes(reader.TryReadBytesToSlice(uint64(dataTables.Size)), true)
	count := 0
	for dataTableReader.TryReadBool() {
		count++
		dataTables.SendTable = append(dataTables.SendTable, ParseSendTable(dataTableReader))
	}
	numOfClasses := dataTableReader.TryReadBits(16)
	for count = 0; count < int(numOfClasses); count++ {
		dataTables.ServerClassInfo = append(dataTables.ServerClassInfo, ParseServerClassInfo(dataTableReader, count, int(numOfClasses)))
	}
}

func ParseSendTable(reader *bitreader.Reader) SendTable {
	sendTable := SendTable{
		NeedsDecoder: reader.TryReadBool(),
		NetTableName: reader.TryReadString(),
		NumOfProps:   int16(reader.TryReadBits(10)),
	}
	if sendTable.NumOfProps < 0 {
		return sendTable
	}
	for count := 0; count < int(sendTable.NumOfProps); count++ {
		propType := int8(reader.TryReadBits(5))
		if propType >= int8(7) {
			return sendTable
		}
		prop := prop{
			SendPropType:  sendPropType(propType),
			SendPropName:  reader.TryReadString(),
			SendPropFlags: uint32(reader.TryReadBits(19)),
			Priority:      reader.TryReadUInt8(),
		}
		if propType == int8(ESendPropTypeDataTable) || checkBit(prop.SendPropFlags, 6) {
			prop.ExcludeDtName = reader.TryReadString()
		} else {
			switch propType {
			case int8(ESendPropTypeString), int8(ESendPropTypeInt), int8(ESendPropTypeFloat), int8(ESendPropTypeVector3), int8(ESendPropTypeVector2):
				prop.LowValue = reader.TryReadFloat32()
				prop.HighValue = reader.TryReadFloat32()
				prop.NumBits = int32(reader.TryReadBits(7))
			case int8(ESendPropTypeArray):
				prop.NumElements = int32(reader.TryReadBits(10))
			default:
				return sendTable
			}
		}
		sendTable.Props = append(sendTable.Props, prop)
	}
	return sendTable
}

func ParseServerClassInfo(reader *bitreader.Reader, count int, numOfClasses int) ServerClassInfo {
	serverClassInfo := ServerClassInfo{
		ClassId:       reader.TryReadUInt16(),
		ClassName:     reader.TryReadString(),
		DataTableName: reader.TryReadString(),
	}
	return serverClassInfo
}

// func serverClassBits(numOfClasses int) int {
// 	return highestBitIndex(uint(numOfClasses)) + 1
// }

// func highestBitIndex(i uint) int {
// 	var j int
// 	for j = 31; j >= 0 && (i&(1<<j)) == 0; j-- {
// 	}
// 	return j
// }

func checkBit(val uint32, bit int) bool {
	return (val & (uint32(1) << bit)) != 0
}

type sendPropType int

const (
	ESendPropTypeInt sendPropType = iota
	ESendPropTypeFloat
	ESendPropTypeVector3
	ESendPropTypeVector2
	ESendPropTypeString
	ESendPropTypeArray
	ESendPropTypeDataTable
)

const (
	ESendPropFlagUnsigned       string = "Unsigned"
	ESendPropFlagCoord          string = "Coord"
	ESendPropFlagNoScale        string = "NoScale"
	ESendPropFlagRoundDown      string = "RoundDown"
	ESendPropFlagRoundUp        string = "RoundUp"
	ESendPropFlagNormal         string = "Normal"
	ESendPropFlagExclude        string = "Exclude"
	ESendPropFlagXyze           string = "Xyze"
	ESendPropFlagInsideArray    string = "InsideArray"
	ESendPropFlagProxyAlwaysYes string = "ProxyAlwaysYes"
	ESendPropFlagIsVectorElem   string = "IsVectorElem"
	ESendPropFlagCollapsible    string = "Collapsible"
	ESendPropFlagCoordMp        string = "CoordMp"
	ESendPropFlagCoordMpLp      string = "CoordMpLp"
	ESendPropFlagCoordMpInt     string = "CoordMpInt"
	ESendPropFlagCellCoord      string = "CellCoord"
	ESendPropFlagCellCoordLp    string = "CellCoordLp"
	ESendPropFlagCellCoordInt   string = "CellCoordInt"
	ESendPropFlagChangesOften   string = "ChangesOften"
)

func (prop prop) GetFlags() []string {
	flags := []string{}
	if checkBit(prop.SendPropFlags, 0) {
		flags = append(flags, ESendPropFlagUnsigned)
	}
	if checkBit(prop.SendPropFlags, 1) {
		flags = append(flags, ESendPropFlagCoord)
	}
	if checkBit(prop.SendPropFlags, 2) {
		flags = append(flags, ESendPropFlagNoScale)
	}
	if checkBit(prop.SendPropFlags, 3) {
		flags = append(flags, ESendPropFlagRoundDown)
	}
	if checkBit(prop.SendPropFlags, 4) {
		flags = append(flags, ESendPropFlagRoundUp)
	}
	if checkBit(prop.SendPropFlags, 5) {
		flags = append(flags, ESendPropFlagNormal)
	}
	if checkBit(prop.SendPropFlags, 6) {
		flags = append(flags, ESendPropFlagExclude)
	}
	if checkBit(prop.SendPropFlags, 7) {
		flags = append(flags, ESendPropFlagXyze)
	}
	if checkBit(prop.SendPropFlags, 8) {
		flags = append(flags, ESendPropFlagInsideArray)
	}
	if checkBit(prop.SendPropFlags, 9) {
		flags = append(flags, ESendPropFlagProxyAlwaysYes)
	}
	if checkBit(prop.SendPropFlags, 10) {
		flags = append(flags, ESendPropFlagIsVectorElem)
	}
	if checkBit(prop.SendPropFlags, 11) {
		flags = append(flags, ESendPropFlagCollapsible)
	}
	if checkBit(prop.SendPropFlags, 12) {
		flags = append(flags, ESendPropFlagCoordMp)
	}
	if checkBit(prop.SendPropFlags, 13) {
		flags = append(flags, ESendPropFlagCoordMpLp)
	}
	if checkBit(prop.SendPropFlags, 14) {
		flags = append(flags, ESendPropFlagCoordMpInt)
	}
	if checkBit(prop.SendPropFlags, 15) {
		flags = append(flags, ESendPropFlagCellCoord)
	}
	if checkBit(prop.SendPropFlags, 16) {
		flags = append(flags, ESendPropFlagCellCoordLp)
	}
	if checkBit(prop.SendPropFlags, 17) {
		flags = append(flags, ESendPropFlagCellCoordInt)
	}
	if checkBit(prop.SendPropFlags, 18) {
		flags = append(flags, ESendPropFlagChangesOften)
	}
	return flags
}

func (sendPropType sendPropType) String() string {
	switch sendPropType {
	case ESendPropTypeInt:
		return "Int"
	case ESendPropTypeFloat:
		return "Float"
	case ESendPropTypeVector3:
		return "Vector3"
	case ESendPropTypeVector2:
		return "Vector2"
	case ESendPropTypeString:
		return "String"
	case ESendPropTypeArray:
		return "Array"
	case ESendPropTypeDataTable:
		return "DataTable"
	default:
		return fmt.Sprintf("%d", int(sendPropType))
	}
}