diff options
| author | BiSaXa <1669855+BiSaXa@users.noreply.github.com> | 2022-09-07 23:08:58 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-16 21:39:36 +0300 |
| commit | 0b8d982acae2ae102e6dee29afff3b621f32cd8f (patch) | |
| tree | 5754719ec8f17745bc6413f621974f1120443788 | |
| parent | first rewrite commit using bisaxa/bitreader (diff) | |
| download | sdp.go-0b8d982acae2ae102e6dee29afff3b621f32cd8f.tar.gz sdp.go-0b8d982acae2ae102e6dee29afff3b621f32cd8f.tar.bz2 sdp.go-0b8d982acae2ae102e6dee29afff3b621f32cd8f.zip | |
class parses and other stuff
| -rw-r--r-- | classes/classes.go | 104 | ||||
| -rw-r--r-- | classes/types.go | 32 | ||||
| -rw-r--r-- | messages/messages.go | 17 | ||||
| -rw-r--r-- | messages/types.go | 4 | ||||
| -rw-r--r-- | utils/utils.go | 34 |
5 files changed, 152 insertions, 39 deletions
diff --git a/classes/classes.go b/classes/classes.go index beaab77..1a431f3 100644 --- a/classes/classes.go +++ b/classes/classes.go | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | package classes | 1 | package classes |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/binary" | ||
| 4 | "os" | 5 | "os" |
| 5 | 6 | ||
| 6 | "github.com/bisaxa/bitreader" | 7 | "github.com/bisaxa/bitreader" |
| @@ -8,45 +9,26 @@ import ( | |||
| 8 | ) | 9 | ) |
| 9 | 10 | ||
| 10 | func ParseCmdInfo(file *os.File, MSSC int) []CmdInfo { | 11 | func ParseCmdInfo(file *os.File, MSSC int) []CmdInfo { |
| 11 | reader := bitreader.Reader(file, true) | 12 | array := utils.ReadByteFromFile(file, 76*int32(MSSC)) |
| 12 | var cmdinfo CmdInfo | ||
| 13 | var cmdinfoarray []CmdInfo | 13 | var cmdinfoarray []CmdInfo |
| 14 | for count := 0; count < MSSC; count++ { | 14 | for count := 0; count < MSSC; count++ { |
| 15 | cmdinfo.Flags = int32(reader.TryReadInt32()) | 15 | var cmdinfo CmdInfo |
| 16 | var floatArray [3]float32 | 16 | cmdinfo.Flags = int32(binary.LittleEndian.Uint32(array[0+76*count : 4+76*count])) |
| 17 | for i := 0; i < 3; i++ { | 17 | cmdinfo.ViewOrigin = utils.FloatArrFromBytes(array[4+76*count : 16+76*count]) |
| 18 | floatArray[i] = reader.TryReadFloat32() | 18 | cmdinfo.ViewAngles = utils.FloatArrFromBytes(array[16+76*count : 28+76*count]) |
| 19 | } | 19 | cmdinfo.LocalViewAngles = utils.FloatArrFromBytes(array[28+76*count : 40+76*count]) |
| 20 | cmdinfo.ViewOrigin = floatArray[:] | 20 | cmdinfo.ViewOrigin2 = utils.FloatArrFromBytes(array[40+76*count : 52+76*count]) |
| 21 | for i := 0; i < 3; i++ { | 21 | cmdinfo.ViewAngles2 = utils.FloatArrFromBytes(array[52+76*count : 64+76*count]) |
| 22 | floatArray[i] = reader.TryReadFloat32() | 22 | cmdinfo.LocalViewAngles2 = utils.FloatArrFromBytes(array[64+76*count : 76+76*count]) |
| 23 | } | ||
| 24 | cmdinfo.ViewAngles = floatArray[:] | ||
| 25 | for i := 0; i < 3; i++ { | ||
| 26 | floatArray[i] = reader.TryReadFloat32() | ||
| 27 | } | ||
| 28 | cmdinfo.LocalViewAngles = floatArray[:] | ||
| 29 | for i := 0; i < 3; i++ { | ||
| 30 | floatArray[i] = reader.TryReadFloat32() | ||
| 31 | } | ||
| 32 | cmdinfo.ViewOrigin2 = floatArray[:] | ||
| 33 | for i := 0; i < 3; i++ { | ||
| 34 | floatArray[i] = reader.TryReadFloat32() | ||
| 35 | } | ||
| 36 | cmdinfo.ViewAngles2 = floatArray[:] | ||
| 37 | for i := 0; i < 3; i++ { | ||
| 38 | floatArray[i] = reader.TryReadFloat32() | ||
| 39 | } | ||
| 40 | cmdinfo.LocalViewAngles2 = floatArray[:] | ||
| 41 | cmdinfoarray = append(cmdinfoarray, cmdinfo) | 23 | cmdinfoarray = append(cmdinfoarray, cmdinfo) |
| 42 | } | 24 | } |
| 43 | return cmdinfoarray | 25 | return cmdinfoarray |
| 44 | } | 26 | } |
| 45 | 27 | ||
| 46 | func ParseUserCmdInfo(file *os.File, size int) UserCmdInfo { | 28 | func ParseUserCmdInfo(file *os.File, size int) UserCmdInfo { |
| 47 | count := 0 | ||
| 48 | reader := bitreader.Reader(file, true) | 29 | reader := bitreader.Reader(file, true) |
| 49 | var usercmd UserCmdInfo | 30 | var usercmd UserCmdInfo |
| 31 | count := 0 | ||
| 50 | flag, err := reader.ReadBool() | 32 | flag, err := reader.ReadBool() |
| 51 | utils.CheckError(err) | 33 | utils.CheckError(err) |
| 52 | if flag { | 34 | if flag { |
| @@ -150,6 +132,68 @@ func ParseUserCmdInfo(file *os.File, size int) UserCmdInfo { | |||
| 150 | count += 16 | 132 | count += 16 |
| 151 | } | 133 | } |
| 152 | count++ | 134 | count++ |
| 153 | reader.SkipBits(size*8 - count) | 135 | reader.SkipBits(size*8 - count) // Skip remaining bits from specified size |
| 154 | return usercmd | 136 | return usercmd |
| 155 | } | 137 | } |
| 138 | |||
| 139 | /*func ParseStringTable(file *os.File, size int) []StringTable { | ||
| 140 | reader := bitreader.Reader(file, true) | ||
| 141 | var stringtable StringTable | ||
| 142 | var stringtablearray []StringTable | ||
| 143 | //count := 0 | ||
| 144 | stringtable.NumOfTables = int8(reader.TryReadInt8()) | ||
| 145 | for i := 0; i < int(stringtable.NumOfTables); i++ { | ||
| 146 | stringtable.TableName = utils.ReadStringFromFile(file) | ||
| 147 | stringtable.NumOfEntries = int16(reader.TryReadInt16()) | ||
| 148 | stringtable.EntryName = utils.ReadStringFromFile(file) | ||
| 149 | flag, err := reader.ReadBool() | ||
| 150 | utils.CheckError(err) | ||
| 151 | if flag { | ||
| 152 | stringtable.EntrySize = int16(reader.TryReadInt16()) | ||
| 153 | } | ||
| 154 | flag, err = reader.ReadBool() | ||
| 155 | utils.CheckError(err) | ||
| 156 | if flag { | ||
| 157 | fmt.Println(int(stringtable.EntrySize)) | ||
| 158 | reader.SkipBytes(int(stringtable.EntrySize)) | ||
| 159 | var bytearray []byte | ||
| 160 | for i := 0; i < int(stringtable.EntrySize); i++ { | ||
| 161 | value, err := reader.ReadBytes(1) | ||
| 162 | utils.CheckError(err) | ||
| 163 | bytearray = append(bytearray, byte(value)) | ||
| 164 | } | ||
| 165 | stringtable.EntryData = bytearrray | ||
| 166 | } | ||
| 167 | flag, err = reader.ReadBool() | ||
| 168 | utils.CheckError(err) | ||
| 169 | if flag { | ||
| 170 | stringtable.NumOfClientEntries = int16(reader.TryReadInt16()) | ||
| 171 | } | ||
| 172 | flag, err = reader.ReadBool() | ||
| 173 | utils.CheckError(err) | ||
| 174 | if flag { | ||
| 175 | stringtable.ClientEntryName = utils.ReadStringFromFile(file) | ||
| 176 | } | ||
| 177 | flag, err = reader.ReadBool() | ||
| 178 | utils.CheckError(err) | ||
| 179 | if flag { | ||
| 180 | stringtable.ClientEntrySize = int16(reader.TryReadInt16()) | ||
| 181 | } | ||
| 182 | flag, err = reader.ReadBool() | ||
| 183 | utils.CheckError(err) | ||
| 184 | if flag { | ||
| 185 | reader.SkipBytes(int(stringtable.ClientEntrySize)) | ||
| 186 | /*var bytearray []byte | ||
| 187 | for i := 0; i < int(stringtable.ClientEntrySize); i++ { | ||
| 188 | value, err := reader.ReadBytes(1) | ||
| 189 | utils.CheckError(err) | ||
| 190 | bytearray = append(bytearray, byte(value)) | ||
| 191 | } | ||
| 192 | stringtable.ClientEntryData = bytearrray | ||
| 193 | } | ||
| 194 | stringtablearray = append(stringtablearray, stringtable) | ||
| 195 | } | ||
| 196 | |||
| 197 | //reader.SkipBits(size*8 - 8) | ||
| 198 | return stringtablearray | ||
| 199 | }*/ | ||
diff --git a/classes/types.go b/classes/types.go index 3fcc692..20c793a 100644 --- a/classes/types.go +++ b/classes/types.go | |||
| @@ -26,3 +26,35 @@ type UserCmdInfo struct { | |||
| 26 | MouseDx int16 | 26 | MouseDx int16 |
| 27 | MouseDy int16 | 27 | MouseDy int16 |
| 28 | } | 28 | } |
| 29 | |||
| 30 | type DataTables struct { | ||
| 31 | SendTable []SendTable | ||
| 32 | ServerClassInfo []ServerClassInfo | ||
| 33 | } | ||
| 34 | |||
| 35 | type SendTable struct { | ||
| 36 | NetTableName string | ||
| 37 | NumOfProps int | ||
| 38 | SendPropType int | ||
| 39 | SendPropName string | ||
| 40 | SendPropFlags int | ||
| 41 | } | ||
| 42 | |||
| 43 | type ServerClassInfo struct { | ||
| 44 | } | ||
| 45 | |||
| 46 | type StringTable struct { | ||
| 47 | NumOfTables int8 | ||
| 48 | TableName string | ||
| 49 | NumOfEntries int16 | ||
| 50 | EntryName string | ||
| 51 | EntrySize int16 | ||
| 52 | EntryData []byte | ||
| 53 | NumOfClientEntries int16 | ||
| 54 | ClientEntryName string | ||
| 55 | ClientEntrySize int16 | ||
| 56 | ClientEntryData []byte | ||
| 57 | } | ||
| 58 | |||
| 59 | type GameEvent struct { | ||
| 60 | } | ||
diff --git a/messages/messages.go b/messages/messages.go index 4d94728..478b7c7 100644 --- a/messages/messages.go +++ b/messages/messages.go | |||
| @@ -14,7 +14,7 @@ func ParseMessage(file *os.File) (statusCode int) { | |||
| 14 | messageType := reader.TryReadInt8() | 14 | messageType := reader.TryReadInt8() |
| 15 | messageTick := reader.TryReadInt32() | 15 | messageTick := reader.TryReadInt32() |
| 16 | messageSlot := reader.TryReadInt8() | 16 | messageSlot := reader.TryReadInt8() |
| 17 | fmt.Println(messageType, messageTick, messageSlot) | 17 | //fmt.Println(messageType, messageTick, messageSlot) |
| 18 | switch messageType { | 18 | switch messageType { |
| 19 | case 0x01: // SignOn | 19 | case 0x01: // SignOn |
| 20 | var packet Packet | 20 | var packet Packet |
| @@ -22,7 +22,8 @@ func ParseMessage(file *os.File) (statusCode int) { | |||
| 22 | packet.InSequence = int32(reader.TryReadInt32()) | 22 | packet.InSequence = int32(reader.TryReadInt32()) |
| 23 | packet.OutSequence = int32(reader.TryReadInt32()) | 23 | packet.OutSequence = int32(reader.TryReadInt32()) |
| 24 | packet.Size = int32(reader.TryReadInt32()) | 24 | packet.Size = int32(reader.TryReadInt32()) |
| 25 | reader.SkipBytes(int(packet.Size)) | 25 | reader.SkipBytes(int(packet.Size)) // TODO: NET/SVC Message Parsing |
| 26 | fmt.Printf("[%d] (%d) SignOn: %v\n", messageTick, messageSlot, packet) | ||
| 26 | return 1 | 27 | return 1 |
| 27 | case 0x02: // Packet | 28 | case 0x02: // Packet |
| 28 | var packet Packet | 29 | var packet Packet |
| @@ -30,7 +31,8 @@ func ParseMessage(file *os.File) (statusCode int) { | |||
| 30 | packet.InSequence = int32(reader.TryReadInt32()) | 31 | packet.InSequence = int32(reader.TryReadInt32()) |
| 31 | packet.OutSequence = int32(reader.TryReadInt32()) | 32 | packet.OutSequence = int32(reader.TryReadInt32()) |
| 32 | packet.Size = int32(reader.TryReadInt32()) | 33 | packet.Size = int32(reader.TryReadInt32()) |
| 33 | reader.SkipBytes(int(packet.Size)) | 34 | reader.SkipBytes(int(packet.Size)) // TODO: NET/SVC Message Parsing |
| 35 | fmt.Printf("[%d] Packet: %v\n", messageTick, packet) | ||
| 34 | return 2 | 36 | return 2 |
| 35 | case 0x03: // SyncTick | 37 | case 0x03: // SyncTick |
| 36 | return 3 | 38 | return 3 |
| @@ -38,30 +40,33 @@ func ParseMessage(file *os.File) (statusCode int) { | |||
| 38 | var consolecmd ConsoleCmd | 40 | var consolecmd ConsoleCmd |
| 39 | consolecmd.Size = int32(reader.TryReadInt32()) | 41 | consolecmd.Size = int32(reader.TryReadInt32()) |
| 40 | consolecmd.Data = string(utils.ReadByteFromFile(file, consolecmd.Size)) | 42 | consolecmd.Data = string(utils.ReadByteFromFile(file, consolecmd.Size)) |
| 43 | fmt.Printf("[%d] ConsoleCmd: %s\n", messageTick, consolecmd.Data) | ||
| 41 | return 4 | 44 | return 4 |
| 42 | case 0x05: // UserCmd | 45 | case 0x05: // UserCmd |
| 43 | var usercmd UserCmd | 46 | var usercmd UserCmd |
| 44 | usercmd.Cmd = int32(reader.TryReadInt32()) | 47 | usercmd.Cmd = int32(reader.TryReadInt32()) |
| 45 | usercmd.Size = int32(reader.TryReadInt32()) | 48 | usercmd.Size = int32(reader.TryReadInt32()) |
| 46 | usercmd.Data = classes.ParseUserCmdInfo(file, int(usercmd.Size)) | 49 | usercmd.Data = classes.ParseUserCmdInfo(file, int(usercmd.Size)) |
| 50 | fmt.Printf("[%d] UserCmd: %v\n", messageTick, usercmd.Data) | ||
| 47 | return 5 | 51 | return 5 |
| 48 | case 0x06: // DataTables | 52 | case 0x06: // DataTables |
| 49 | var datatables DataTables | 53 | var datatables DataTables |
| 50 | datatables.Size = int32(reader.TryReadInt32()) | 54 | datatables.Size = int32(reader.TryReadInt32()) |
| 51 | reader.SkipBytes(int(datatables.Size)) | 55 | reader.SkipBytes(int(datatables.Size)) // TODO: DataTables Data |
| 52 | return 6 | 56 | return 6 |
| 53 | case 0x07: // Stop | 57 | case 0x07: // Stop |
| 58 | fmt.Printf("[%d] Stop\n", messageTick) | ||
| 54 | return 7 | 59 | return 7 |
| 55 | case 0x08: // CustomData | 60 | case 0x08: // CustomData |
| 56 | var customdata CustomData | 61 | var customdata CustomData |
| 57 | customdata.Unknown = int32(reader.TryReadInt32()) | 62 | customdata.Unknown = int32(reader.TryReadInt32()) |
| 58 | customdata.Size = int32(reader.TryReadInt32()) | 63 | customdata.Size = int32(reader.TryReadInt32()) |
| 59 | reader.SkipBytes(int(customdata.Size)) | 64 | reader.SkipBytes(int(customdata.Size)) // TODO: CustomData Data |
| 60 | return 8 | 65 | return 8 |
| 61 | case 0x09: // StringTables | 66 | case 0x09: // StringTables |
| 62 | var stringtables StringTables | 67 | var stringtables StringTables |
| 63 | stringtables.Size = int32(reader.TryReadInt32()) | 68 | stringtables.Size = int32(reader.TryReadInt32()) |
| 64 | reader.SkipBytes(int(stringtables.Size)) | 69 | reader.SkipBytes(int(stringtables.Size)) // TODO: StringTables Data |
| 65 | return 9 | 70 | return 9 |
| 66 | default: | 71 | default: |
| 67 | return 0 | 72 | return 0 |
diff --git a/messages/types.go b/messages/types.go index a1d51a8..a8dd19a 100644 --- a/messages/types.go +++ b/messages/types.go | |||
| @@ -37,7 +37,7 @@ type UserCmd struct { | |||
| 37 | 37 | ||
| 38 | type DataTables struct { | 38 | type DataTables struct { |
| 39 | Size int32 | 39 | Size int32 |
| 40 | Data []byte | 40 | Data classes.DataTables |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | type CustomData struct { | 43 | type CustomData struct { |
| @@ -48,5 +48,5 @@ type CustomData struct { | |||
| 48 | 48 | ||
| 49 | type StringTables struct { | 49 | type StringTables struct { |
| 50 | Size int32 | 50 | Size int32 |
| 51 | Data []byte | 51 | Data []classes.StringTable |
| 52 | } | 52 | } |
diff --git a/utils/utils.go b/utils/utils.go index 46b707c..d25fa36 100644 --- a/utils/utils.go +++ b/utils/utils.go | |||
| @@ -1,6 +1,11 @@ | |||
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | import "os" | 3 | import ( |
| 4 | "os" | ||
| 5 | "unsafe" | ||
| 6 | |||
| 7 | "github.com/bisaxa/bitreader" | ||
| 8 | ) | ||
| 4 | 9 | ||
| 5 | func CheckError(e error) { | 10 | func CheckError(e error) { |
| 6 | if e != nil { | 11 | if e != nil { |
| @@ -13,3 +18,30 @@ func ReadByteFromFile(file *os.File, size int32) []byte { | |||
| 13 | file.Read(tmp) | 18 | file.Read(tmp) |
| 14 | return tmp | 19 | return tmp |
| 15 | } | 20 | } |
| 21 | |||
| 22 | func ReadStringFromFile(file *os.File) string { | ||
| 23 | var output string | ||
| 24 | reader := bitreader.Reader(file, true) | ||
| 25 | for { | ||
| 26 | value, err := reader.ReadBytes(1) | ||
| 27 | CheckError(err) | ||
| 28 | if value == 0 { | ||
| 29 | break | ||
| 30 | } | ||
| 31 | output += string(rune(value)) | ||
| 32 | } | ||
| 33 | return output | ||
| 34 | } | ||
| 35 | |||
| 36 | func FloatArrFromBytes(byteArr []byte) []float32 { | ||
| 37 | if len(byteArr) == 0 { | ||
| 38 | return nil | ||
| 39 | } | ||
| 40 | l := len(byteArr) / 4 | ||
| 41 | ptr := unsafe.Pointer(&byteArr[0]) | ||
| 42 | // It is important to keep in mind that the Go garbage collector | ||
| 43 | // will not interact with this data, and that if src if freed, | ||
| 44 | // the behavior of any Go code using the slice is nondeterministic. | ||
| 45 | // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices | ||
| 46 | return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l] | ||
| 47 | } | ||