From 0b8d982acae2ae102e6dee29afff3b621f32cd8f Mon Sep 17 00:00:00 2001 From: BiSaXa <1669855+BiSaXa@users.noreply.github.com> Date: Wed, 7 Sep 2022 23:08:58 +0300 Subject: class parses and other stuff --- classes/classes.go | 104 ++++++++++++++++++++++++++++++++++++--------------- classes/types.go | 32 ++++++++++++++++ messages/messages.go | 17 ++++++--- messages/types.go | 4 +- 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 @@ package classes import ( + "encoding/binary" "os" "github.com/bisaxa/bitreader" @@ -8,45 +9,26 @@ import ( ) func ParseCmdInfo(file *os.File, MSSC int) []CmdInfo { - reader := bitreader.Reader(file, true) - var cmdinfo CmdInfo + array := utils.ReadByteFromFile(file, 76*int32(MSSC)) var cmdinfoarray []CmdInfo for count := 0; count < MSSC; count++ { - cmdinfo.Flags = int32(reader.TryReadInt32()) - var floatArray [3]float32 - for i := 0; i < 3; i++ { - floatArray[i] = reader.TryReadFloat32() - } - cmdinfo.ViewOrigin = floatArray[:] - for i := 0; i < 3; i++ { - floatArray[i] = reader.TryReadFloat32() - } - cmdinfo.ViewAngles = floatArray[:] - for i := 0; i < 3; i++ { - floatArray[i] = reader.TryReadFloat32() - } - cmdinfo.LocalViewAngles = floatArray[:] - for i := 0; i < 3; i++ { - floatArray[i] = reader.TryReadFloat32() - } - cmdinfo.ViewOrigin2 = floatArray[:] - for i := 0; i < 3; i++ { - floatArray[i] = reader.TryReadFloat32() - } - cmdinfo.ViewAngles2 = floatArray[:] - for i := 0; i < 3; i++ { - floatArray[i] = reader.TryReadFloat32() - } - cmdinfo.LocalViewAngles2 = floatArray[:] + var cmdinfo CmdInfo + cmdinfo.Flags = int32(binary.LittleEndian.Uint32(array[0+76*count : 4+76*count])) + cmdinfo.ViewOrigin = utils.FloatArrFromBytes(array[4+76*count : 16+76*count]) + cmdinfo.ViewAngles = utils.FloatArrFromBytes(array[16+76*count : 28+76*count]) + cmdinfo.LocalViewAngles = utils.FloatArrFromBytes(array[28+76*count : 40+76*count]) + cmdinfo.ViewOrigin2 = utils.FloatArrFromBytes(array[40+76*count : 52+76*count]) + cmdinfo.ViewAngles2 = utils.FloatArrFromBytes(array[52+76*count : 64+76*count]) + cmdinfo.LocalViewAngles2 = utils.FloatArrFromBytes(array[64+76*count : 76+76*count]) cmdinfoarray = append(cmdinfoarray, cmdinfo) } return cmdinfoarray } func ParseUserCmdInfo(file *os.File, size int) UserCmdInfo { - count := 0 reader := bitreader.Reader(file, true) var usercmd UserCmdInfo + count := 0 flag, err := reader.ReadBool() utils.CheckError(err) if flag { @@ -150,6 +132,68 @@ func ParseUserCmdInfo(file *os.File, size int) UserCmdInfo { count += 16 } count++ - reader.SkipBits(size*8 - count) + reader.SkipBits(size*8 - count) // Skip remaining bits from specified size return usercmd } + +/*func ParseStringTable(file *os.File, size int) []StringTable { + reader := bitreader.Reader(file, true) + var stringtable StringTable + var stringtablearray []StringTable + //count := 0 + stringtable.NumOfTables = int8(reader.TryReadInt8()) + for i := 0; i < int(stringtable.NumOfTables); i++ { + stringtable.TableName = utils.ReadStringFromFile(file) + stringtable.NumOfEntries = int16(reader.TryReadInt16()) + stringtable.EntryName = utils.ReadStringFromFile(file) + flag, err := reader.ReadBool() + utils.CheckError(err) + if flag { + stringtable.EntrySize = int16(reader.TryReadInt16()) + } + flag, err = reader.ReadBool() + utils.CheckError(err) + if flag { + fmt.Println(int(stringtable.EntrySize)) + reader.SkipBytes(int(stringtable.EntrySize)) + var bytearray []byte + for i := 0; i < int(stringtable.EntrySize); i++ { + value, err := reader.ReadBytes(1) + utils.CheckError(err) + bytearray = append(bytearray, byte(value)) + } + stringtable.EntryData = bytearrray + } + flag, err = reader.ReadBool() + utils.CheckError(err) + if flag { + stringtable.NumOfClientEntries = int16(reader.TryReadInt16()) + } + flag, err = reader.ReadBool() + utils.CheckError(err) + if flag { + stringtable.ClientEntryName = utils.ReadStringFromFile(file) + } + flag, err = reader.ReadBool() + utils.CheckError(err) + if flag { + stringtable.ClientEntrySize = int16(reader.TryReadInt16()) + } + flag, err = reader.ReadBool() + utils.CheckError(err) + if flag { + reader.SkipBytes(int(stringtable.ClientEntrySize)) + /*var bytearray []byte + for i := 0; i < int(stringtable.ClientEntrySize); i++ { + value, err := reader.ReadBytes(1) + utils.CheckError(err) + bytearray = append(bytearray, byte(value)) + } + stringtable.ClientEntryData = bytearrray + } + stringtablearray = append(stringtablearray, stringtable) + } + + //reader.SkipBits(size*8 - 8) + return stringtablearray +}*/ 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 { MouseDx int16 MouseDy int16 } + +type DataTables struct { + SendTable []SendTable + ServerClassInfo []ServerClassInfo +} + +type SendTable struct { + NetTableName string + NumOfProps int + SendPropType int + SendPropName string + SendPropFlags int +} + +type ServerClassInfo struct { +} + +type StringTable struct { + NumOfTables int8 + TableName string + NumOfEntries int16 + EntryName string + EntrySize int16 + EntryData []byte + NumOfClientEntries int16 + ClientEntryName string + ClientEntrySize int16 + ClientEntryData []byte +} + +type GameEvent struct { +} 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) { messageType := reader.TryReadInt8() messageTick := reader.TryReadInt32() messageSlot := reader.TryReadInt8() - fmt.Println(messageType, messageTick, messageSlot) + //fmt.Println(messageType, messageTick, messageSlot) switch messageType { case 0x01: // SignOn var packet Packet @@ -22,7 +22,8 @@ func ParseMessage(file *os.File) (statusCode int) { packet.InSequence = int32(reader.TryReadInt32()) packet.OutSequence = int32(reader.TryReadInt32()) packet.Size = int32(reader.TryReadInt32()) - reader.SkipBytes(int(packet.Size)) + reader.SkipBytes(int(packet.Size)) // TODO: NET/SVC Message Parsing + fmt.Printf("[%d] (%d) SignOn: %v\n", messageTick, messageSlot, packet) return 1 case 0x02: // Packet var packet Packet @@ -30,7 +31,8 @@ func ParseMessage(file *os.File) (statusCode int) { packet.InSequence = int32(reader.TryReadInt32()) packet.OutSequence = int32(reader.TryReadInt32()) packet.Size = int32(reader.TryReadInt32()) - reader.SkipBytes(int(packet.Size)) + reader.SkipBytes(int(packet.Size)) // TODO: NET/SVC Message Parsing + fmt.Printf("[%d] Packet: %v\n", messageTick, packet) return 2 case 0x03: // SyncTick return 3 @@ -38,30 +40,33 @@ func ParseMessage(file *os.File) (statusCode int) { var consolecmd ConsoleCmd consolecmd.Size = int32(reader.TryReadInt32()) consolecmd.Data = string(utils.ReadByteFromFile(file, consolecmd.Size)) + fmt.Printf("[%d] ConsoleCmd: %s\n", messageTick, consolecmd.Data) return 4 case 0x05: // UserCmd var usercmd UserCmd usercmd.Cmd = int32(reader.TryReadInt32()) usercmd.Size = int32(reader.TryReadInt32()) usercmd.Data = classes.ParseUserCmdInfo(file, int(usercmd.Size)) + fmt.Printf("[%d] UserCmd: %v\n", messageTick, usercmd.Data) return 5 case 0x06: // DataTables var datatables DataTables datatables.Size = int32(reader.TryReadInt32()) - reader.SkipBytes(int(datatables.Size)) + reader.SkipBytes(int(datatables.Size)) // TODO: DataTables Data return 6 case 0x07: // Stop + fmt.Printf("[%d] Stop\n", messageTick) return 7 case 0x08: // CustomData var customdata CustomData customdata.Unknown = int32(reader.TryReadInt32()) customdata.Size = int32(reader.TryReadInt32()) - reader.SkipBytes(int(customdata.Size)) + reader.SkipBytes(int(customdata.Size)) // TODO: CustomData Data return 8 case 0x09: // StringTables var stringtables StringTables stringtables.Size = int32(reader.TryReadInt32()) - reader.SkipBytes(int(stringtables.Size)) + reader.SkipBytes(int(stringtables.Size)) // TODO: StringTables Data return 9 default: 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 { type DataTables struct { Size int32 - Data []byte + Data classes.DataTables } type CustomData struct { @@ -48,5 +48,5 @@ type CustomData struct { type StringTables struct { Size int32 - Data []byte + Data []classes.StringTable } 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 @@ package utils -import "os" +import ( + "os" + "unsafe" + + "github.com/bisaxa/bitreader" +) func CheckError(e error) { if e != nil { @@ -13,3 +18,30 @@ func ReadByteFromFile(file *os.File, size int32) []byte { file.Read(tmp) return tmp } + +func ReadStringFromFile(file *os.File) string { + var output string + reader := bitreader.Reader(file, true) + for { + value, err := reader.ReadBytes(1) + CheckError(err) + if value == 0 { + break + } + output += string(rune(value)) + } + return output +} + +func FloatArrFromBytes(byteArr []byte) []float32 { + if len(byteArr) == 0 { + return nil + } + l := len(byteArr) / 4 + ptr := unsafe.Pointer(&byteArr[0]) + // It is important to keep in mind that the Go garbage collector + // will not interact with this data, and that if src if freed, + // the behavior of any Go code using the slice is nondeterministic. + // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices + return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l] +} -- cgit v1.2.3