aboutsummaryrefslogtreecommitdiff
path: root/packets/classes/stringtable.go
blob: a1432f93f781227ad2429f77d8d607680c324177 (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
package classes

import (
	"bytes"

	"github.com/pektezol/bitreader"
)

type StringTable struct {
	TableName          string
	NumOfEntries       int16
	EntryName          string
	EntrySize          int16
	EntryData          []byte
	NumOfClientEntries int16
	ClientEntryName    string
	ClientEntrySize    int16
	ClientEntryData    []byte
}

func ParseStringTable(data []byte) []StringTable {
	reader := bitreader.Reader(bytes.NewReader(data), true)
	var stringTables []StringTable
	numOfTables := reader.TryReadInt8()
	for i := 0; i < int(numOfTables); i++ {
		var stringTable StringTable
		stringTable.TableName = reader.TryReadString()
		stringTable.NumOfEntries = int16(reader.TryReadInt16())
		stringTable.EntryName = reader.TryReadString()
		if reader.TryReadBool() {
			stringTable.EntrySize = int16(reader.TryReadInt16())
		}
		if reader.TryReadBool() {
			stringTable.EntryData = reader.TryReadBytesToSlice(int(stringTable.EntrySize))
		}
		if reader.TryReadBool() {
			stringTable.NumOfClientEntries = int16(reader.TryReadInt16())
		}
		if reader.TryReadBool() {
			stringTable.ClientEntryName = reader.TryReadString()
		}
		if reader.TryReadBool() {
			stringTable.ClientEntrySize = int16(reader.TryReadInt16())
		}
		if reader.TryReadBool() {
			stringTable.ClientEntryData = reader.TryReadBytesToSlice(int(stringTable.ClientEntrySize))
		}
		stringTables = append(stringTables, stringTable)
	}
	return stringTables
}