diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-12 20:53:09 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-16 21:39:42 +0300 |
| commit | 82871ba1bac1d62f69e1933b66659e62d2e5e063 (patch) | |
| tree | a906310fba89b670bcfda9625a6d776553d482f6 /pkg/classes | |
| parent | net/svc messages finally getting parsed correctly (diff) | |
| download | sdp.go-82871ba1bac1d62f69e1933b66659e62d2e5e063.tar.gz sdp.go-82871ba1bac1d62f69e1933b66659e62d2e5e063.tar.bz2 sdp.go-82871ba1bac1d62f69e1933b66659e62d2e5e063.zip | |
another rewrite, v1.0.0
Diffstat (limited to 'pkg/classes')
| -rw-r--r-- | pkg/classes/cmdInfo.go | 27 | ||||
| -rw-r--r-- | pkg/classes/sendTable.go | 106 | ||||
| -rw-r--r-- | pkg/classes/serverClassInfo.go | 30 | ||||
| -rw-r--r-- | pkg/classes/stringTable.go | 82 | ||||
| -rw-r--r-- | pkg/classes/userCmdInfo.go | 72 |
5 files changed, 317 insertions, 0 deletions
diff --git a/pkg/classes/cmdInfo.go b/pkg/classes/cmdInfo.go new file mode 100644 index 0000000..6e56c41 --- /dev/null +++ b/pkg/classes/cmdInfo.go | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "github.com/pektezol/bitreader" | ||
| 5 | ) | ||
| 6 | |||
| 7 | type CmdInfo struct { | ||
| 8 | Flags int32 | ||
| 9 | ViewOrigin []float32 | ||
| 10 | ViewAngles []float32 | ||
| 11 | LocalViewAngles []float32 | ||
| 12 | ViewOrigin2 []float32 | ||
| 13 | ViewAngles2 []float32 | ||
| 14 | LocalViewAngles2 []float32 | ||
| 15 | } | ||
| 16 | |||
| 17 | func ParseCmdInfo(reader *bitreader.ReaderType) CmdInfo { | ||
| 18 | return CmdInfo{ | ||
| 19 | Flags: int32(reader.TryReadBits(32)), | ||
| 20 | ViewOrigin: []float32{reader.TryReadFloat32(), reader.TryReadFloat32(), reader.TryReadFloat32()}, | ||
| 21 | ViewAngles: []float32{reader.TryReadFloat32(), reader.TryReadFloat32(), reader.TryReadFloat32()}, | ||
| 22 | LocalViewAngles: []float32{reader.TryReadFloat32(), reader.TryReadFloat32(), reader.TryReadFloat32()}, | ||
| 23 | ViewOrigin2: []float32{reader.TryReadFloat32(), reader.TryReadFloat32(), reader.TryReadFloat32()}, | ||
| 24 | ViewAngles2: []float32{reader.TryReadFloat32(), reader.TryReadFloat32(), reader.TryReadFloat32()}, | ||
| 25 | LocalViewAngles2: []float32{reader.TryReadFloat32(), reader.TryReadFloat32(), reader.TryReadFloat32()}, | ||
| 26 | } | ||
| 27 | } | ||
diff --git a/pkg/classes/sendTable.go b/pkg/classes/sendTable.go new file mode 100644 index 0000000..4521464 --- /dev/null +++ b/pkg/classes/sendTable.go | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "github.com/pektezol/bitreader" | ||
| 5 | ) | ||
| 6 | |||
| 7 | type SendTable struct { | ||
| 8 | NeedsDecoder bool | ||
| 9 | NetTableName string | ||
| 10 | NumOfProps int16 | ||
| 11 | Props []prop | ||
| 12 | } | ||
| 13 | |||
| 14 | type prop struct { | ||
| 15 | SendPropType int8 | ||
| 16 | SendPropName string | ||
| 17 | SendPropFlags int32 | ||
| 18 | Priority int8 | ||
| 19 | ExcludeDtName string | ||
| 20 | LowValue float32 | ||
| 21 | HighValue float32 | ||
| 22 | NumBits int32 | ||
| 23 | NumElements int32 | ||
| 24 | } | ||
| 25 | |||
| 26 | type sendPropFlag int | ||
| 27 | |||
| 28 | const ( | ||
| 29 | Unsigned sendPropFlag = iota | ||
| 30 | Coord | ||
| 31 | NoScale | ||
| 32 | RoundDown | ||
| 33 | RoundUp | ||
| 34 | Normal | ||
| 35 | Exclude | ||
| 36 | Xyze | ||
| 37 | InsideArray | ||
| 38 | ProxyAlwaysYes | ||
| 39 | IsVectorElem | ||
| 40 | Collapsible | ||
| 41 | CoordMp | ||
| 42 | CoordMpLp // low precision | ||
| 43 | CoordMpInt | ||
| 44 | CellCoord | ||
| 45 | CellCoordLp | ||
| 46 | CellCoordInt | ||
| 47 | ChangesOften | ||
| 48 | ) | ||
| 49 | |||
| 50 | type sendPropType int | ||
| 51 | |||
| 52 | const ( | ||
| 53 | Int sendPropType = iota | ||
| 54 | Float | ||
| 55 | Vector3 | ||
| 56 | Vector2 | ||
| 57 | String | ||
| 58 | Array | ||
| 59 | DataTable | ||
| 60 | ) | ||
| 61 | |||
| 62 | func ParseSendTable(reader *bitreader.ReaderType) SendTable { | ||
| 63 | sendTable := SendTable{ | ||
| 64 | NeedsDecoder: reader.TryReadBool(), | ||
| 65 | NetTableName: reader.TryReadString(), | ||
| 66 | NumOfProps: int16(reader.TryReadBits(10)), | ||
| 67 | // SendPropType: int8(reader.TryReadBits(5)), | ||
| 68 | // SendPropName: reader.TryReadString(), | ||
| 69 | // SendPropFlags: int16(reader.TryReadBits(16)), | ||
| 70 | } | ||
| 71 | if sendTable.NumOfProps < 0 { | ||
| 72 | return sendTable | ||
| 73 | } | ||
| 74 | for count := 0; count < int(sendTable.NumOfProps); count++ { | ||
| 75 | propType := int8(reader.TryReadBits(5)) | ||
| 76 | if propType >= int8(7) { | ||
| 77 | return sendTable | ||
| 78 | } | ||
| 79 | prop := prop{ | ||
| 80 | SendPropType: propType, | ||
| 81 | SendPropName: reader.TryReadString(), | ||
| 82 | SendPropFlags: int32(reader.TryReadBits(19)), | ||
| 83 | Priority: int8(reader.TryReadBits(8)), | ||
| 84 | } | ||
| 85 | if propType == int8(DataTable) || CheckBit(int64(prop.SendPropFlags), int(Exclude)) { | ||
| 86 | prop.ExcludeDtName = reader.TryReadString() | ||
| 87 | } else { | ||
| 88 | switch propType { | ||
| 89 | case int8(String), int8(Int), int8(Float), int8(Vector3), int8(Vector2): | ||
| 90 | prop.LowValue = reader.TryReadFloat32() | ||
| 91 | prop.HighValue = reader.TryReadFloat32() | ||
| 92 | prop.NumBits = int32(reader.TryReadBits(7)) | ||
| 93 | case int8(Array): | ||
| 94 | prop.NumElements = int32(reader.TryReadBits(10)) | ||
| 95 | default: | ||
| 96 | return sendTable | ||
| 97 | } | ||
| 98 | } | ||
| 99 | sendTable.Props = append(sendTable.Props, prop) | ||
| 100 | } | ||
| 101 | return sendTable | ||
| 102 | } | ||
| 103 | |||
| 104 | func CheckBit(val int64, bit int) bool { | ||
| 105 | return (val & (int64(1) << bit)) != 0 | ||
| 106 | } | ||
diff --git a/pkg/classes/serverClassInfo.go b/pkg/classes/serverClassInfo.go new file mode 100644 index 0000000..c60dad1 --- /dev/null +++ b/pkg/classes/serverClassInfo.go | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "github.com/pektezol/bitreader" | ||
| 5 | ) | ||
| 6 | |||
| 7 | type ServerClassInfo struct { | ||
| 8 | ClassId int16 | ||
| 9 | ClassName string | ||
| 10 | DataTableName string | ||
| 11 | } | ||
| 12 | |||
| 13 | func ParseServerClassInfo(reader *bitreader.ReaderType, count int, numOfClasses int) ServerClassInfo { | ||
| 14 | return ServerClassInfo{ | ||
| 15 | ClassId: int16(reader.TryReadBits(16)), | ||
| 16 | ClassName: reader.TryReadString(), | ||
| 17 | DataTableName: reader.TryReadString(), | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | // func serverClassBits(numOfClasses int) int { | ||
| 22 | // return highestBitIndex(uint(numOfClasses)) + 1 | ||
| 23 | // } | ||
| 24 | |||
| 25 | // func highestBitIndex(i uint) int { | ||
| 26 | // var j int | ||
| 27 | // for j = 31; j >= 0 && (i&(1<<j)) == 0; j-- { | ||
| 28 | // } | ||
| 29 | // return j | ||
| 30 | // } | ||
diff --git a/pkg/classes/stringTable.go b/pkg/classes/stringTable.go new file mode 100644 index 0000000..c6709f5 --- /dev/null +++ b/pkg/classes/stringTable.go | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "bytes" | ||
| 5 | |||
| 6 | "github.com/pektezol/bitreader" | ||
| 7 | ) | ||
| 8 | |||
| 9 | type StringTable struct { | ||
| 10 | Name string | ||
| 11 | TableEntries []StringTableEntry | ||
| 12 | Classes []StringTableClass | ||
| 13 | } | ||
| 14 | |||
| 15 | type StringTableEntry struct { | ||
| 16 | Name string | ||
| 17 | EntryData StringTableEntryData | ||
| 18 | } | ||
| 19 | |||
| 20 | type StringTableEntryData struct { | ||
| 21 | // TODO: Parse StringTableEntry | ||
| 22 | } | ||
| 23 | |||
| 24 | type StringTableClass struct { | ||
| 25 | Name string | ||
| 26 | Data string | ||
| 27 | } | ||
| 28 | |||
| 29 | func ParseStringTables(reader *bitreader.ReaderType) []StringTable { | ||
| 30 | tableCount := reader.TryReadBits(8) | ||
| 31 | stringTables := make([]StringTable, tableCount) | ||
| 32 | for i := 0; i < int(tableCount); i++ { | ||
| 33 | var table StringTable | ||
| 34 | table.ParseStream(reader) | ||
| 35 | stringTables[i] = table | ||
| 36 | } | ||
| 37 | return stringTables | ||
| 38 | } | ||
| 39 | |||
| 40 | func (stringTable *StringTable) ParseStream(reader *bitreader.ReaderType) { | ||
| 41 | stringTable.Name = reader.TryReadString() | ||
| 42 | entryCount := reader.TryReadBits(16) | ||
| 43 | stringTable.TableEntries = make([]StringTableEntry, entryCount) | ||
| 44 | |||
| 45 | for i := 0; i < int(entryCount); i++ { | ||
| 46 | var entry StringTableEntry | ||
| 47 | entry.Parse(reader) | ||
| 48 | stringTable.TableEntries[i] = entry | ||
| 49 | } | ||
| 50 | |||
| 51 | if reader.TryReadBool() { | ||
| 52 | classCount := reader.TryReadBits(16) | ||
| 53 | stringTable.Classes = make([]StringTableClass, classCount) | ||
| 54 | |||
| 55 | for i := 0; i < int(classCount); i++ { | ||
| 56 | var class StringTableClass | ||
| 57 | class.Parse(reader) | ||
| 58 | stringTable.Classes[i] = class | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | func (stringTableEntry *StringTableEntry) Parse(reader *bitreader.ReaderType) { | ||
| 64 | stringTableEntry.Name = reader.TryReadString() | ||
| 65 | if reader.TryReadBool() { | ||
| 66 | byteLen, err := reader.ReadBits(16) | ||
| 67 | if err != nil { | ||
| 68 | return | ||
| 69 | } | ||
| 70 | dataBsr := reader.TryReadBytesToSlice(int(byteLen)) | ||
| 71 | _ = bitreader.Reader(bytes.NewReader(dataBsr), true) // TODO: Parse StringTableEntry | ||
| 72 | // stringTableEntry.EntryData.ParseStream(entryReader) | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | func (stringTableClass *StringTableClass) Parse(reader *bitreader.ReaderType) { | ||
| 77 | stringTableClass.Name = reader.TryReadString() | ||
| 78 | if reader.TryReadBool() { | ||
| 79 | dataLen := reader.TryReadBits(16) | ||
| 80 | stringTableClass.Data = reader.TryReadStringLen(int(dataLen)) | ||
| 81 | } | ||
| 82 | } | ||
diff --git a/pkg/classes/userCmdInfo.go b/pkg/classes/userCmdInfo.go new file mode 100644 index 0000000..a6d9091 --- /dev/null +++ b/pkg/classes/userCmdInfo.go | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "bytes" | ||
| 5 | |||
| 6 | "github.com/pektezol/bitreader" | ||
| 7 | ) | ||
| 8 | |||
| 9 | type UserCmdInfo struct { | ||
| 10 | CommandNumber int32 | ||
| 11 | TickCount int32 | ||
| 12 | ViewAnglesX float32 | ||
| 13 | ViewAnglesY float32 | ||
| 14 | ViewAnglesZ float32 | ||
| 15 | ForwardMove float32 | ||
| 16 | SideMove float32 | ||
| 17 | UpMove float32 | ||
| 18 | Buttons int32 | ||
| 19 | Impulse int8 | ||
| 20 | WeaponSelect int16 | ||
| 21 | WeaponSubType int8 | ||
| 22 | MouseDx int16 | ||
| 23 | MouseDy int16 | ||
| 24 | } | ||
| 25 | |||
| 26 | func ParseUserCmdInfo(data []byte) UserCmdInfo { | ||
| 27 | reader := bitreader.Reader(bytes.NewReader(data), true) | ||
| 28 | userCmdInfo := UserCmdInfo{} | ||
| 29 | if reader.TryReadBool() { | ||
| 30 | userCmdInfo.CommandNumber = int32(reader.TryReadBits(32)) | ||
| 31 | } | ||
| 32 | if reader.TryReadBool() { | ||
| 33 | userCmdInfo.TickCount = int32(reader.TryReadBits(32)) | ||
| 34 | } | ||
| 35 | if reader.TryReadBool() { | ||
| 36 | userCmdInfo.ViewAnglesX = reader.TryReadFloat32() | ||
| 37 | } | ||
| 38 | if reader.TryReadBool() { | ||
| 39 | userCmdInfo.ViewAnglesY = reader.TryReadFloat32() | ||
| 40 | } | ||
| 41 | if reader.TryReadBool() { | ||
| 42 | userCmdInfo.ViewAnglesZ = reader.TryReadFloat32() | ||
| 43 | } | ||
| 44 | if reader.TryReadBool() { | ||
| 45 | userCmdInfo.ForwardMove = reader.TryReadFloat32() | ||
| 46 | } | ||
| 47 | if reader.TryReadBool() { | ||
| 48 | userCmdInfo.SideMove = reader.TryReadFloat32() | ||
| 49 | } | ||
| 50 | if reader.TryReadBool() { | ||
| 51 | userCmdInfo.UpMove = reader.TryReadFloat32() | ||
| 52 | } | ||
| 53 | if reader.TryReadBool() { | ||
| 54 | userCmdInfo.Buttons = int32(reader.TryReadBits(32)) | ||
| 55 | } | ||
| 56 | if reader.TryReadBool() { | ||
| 57 | userCmdInfo.Impulse = int8(reader.TryReadBits(8)) | ||
| 58 | } | ||
| 59 | if reader.TryReadBool() { | ||
| 60 | userCmdInfo.WeaponSelect = int16(reader.TryReadBits(11)) | ||
| 61 | if reader.TryReadBool() { | ||
| 62 | userCmdInfo.WeaponSubType = int8(reader.TryReadBits(6)) | ||
| 63 | } | ||
| 64 | } | ||
| 65 | if reader.TryReadBool() { | ||
| 66 | userCmdInfo.MouseDx = int16(reader.TryReadBits(16)) | ||
| 67 | } | ||
| 68 | if reader.TryReadBool() { | ||
| 69 | userCmdInfo.MouseDy = int16(reader.TryReadBits(16)) | ||
| 70 | } | ||
| 71 | return userCmdInfo | ||
| 72 | } | ||