From 2f9aeb762294fe96606c76c2fefa17a6540639b5 Mon Sep 17 00:00:00 2001 From: BiSaXa <1669855+BiSaXa@users.noreply.github.com> Date: Sat, 27 Aug 2022 23:26:28 +0300 Subject: some cleaning --- classes/userCmdInfo.go | 8 ++---- go.mod | 2 -- go.sum | 2 -- main.go | 17 ++++++----- messages/messages.go | 78 +++++++++++++++++++++++++++----------------------- messages/types.go | 20 +++++++++++++ utils/header.go | 43 ---------------------------- utils/utils.go | 12 ++++---- 8 files changed, 78 insertions(+), 104 deletions(-) delete mode 100644 utils/header.go diff --git a/classes/userCmdInfo.go b/classes/userCmdInfo.go index 86b186c..aae1804 100644 --- a/classes/userCmdInfo.go +++ b/classes/userCmdInfo.go @@ -1,11 +1,5 @@ package classes -import ( - "parser/utils" - - "github.com/potterxu/bitreader" -) - type UserCmdInfo struct { CommandNumber int32 TickCount int32 @@ -17,6 +11,7 @@ type UserCmdInfo struct { UpMove float32 } +/* func UserCmdInfoInit(byteArr []byte, size int) (output UserCmdInfo) { var class UserCmdInfo reversedByteArr := utils.ReverseByteArrayValues(byteArr, size) @@ -111,3 +106,4 @@ func UserCmdInfoInit(byteArr []byte, size int) (output UserCmdInfo) { } return class } +*/ diff --git a/go.mod b/go.mod index e58d4e9..d0e178d 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module parser go 1.19 - -require github.com/potterxu/bitreader v0.0.2 diff --git a/go.sum b/go.sum index 7476ab8..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +0,0 @@ -github.com/potterxu/bitreader v0.0.2 h1:oFOXpMwuatGrIY6pmBAQRpwt1lRWfINOtU+XT03/eTc= -github.com/potterxu/bitreader v0.0.2/go.mod h1:zvQewWmu5YxT6gjvCjlpziVv3S/CTHMeRqo+pcgROmo= diff --git a/main.go b/main.go index 417aeeb..7978414 100644 --- a/main.go +++ b/main.go @@ -17,12 +17,12 @@ func main() { if err != nil { // If it's not a directory file, err := os.Open(os.Args[1]) utils.CheckError(err) - utils.HeaderOut(file) + messages.ParseHeader(file) for { - code := messages.MessageTypeCheck(file) + code := messages.ParseMessage(file) if code == 7 { - messages.MessageTypeCheck(file) - break // TODO: Check last CustomData + messages.ParseMessage(file) + break } } defer file.Close() @@ -30,16 +30,15 @@ func main() { for _, fileinfo := range files { // If it is a directory file, err := os.Open(os.Args[1] + fileinfo.Name()) utils.CheckError(err) - utils.HeaderOut(file) + messages.ParseHeader(file) for { - code := messages.MessageTypeCheck(file) + code := messages.ParseMessage(file) if code == 7 { - messages.MessageTypeCheck(file) - break // TODO: Check last CustomData + messages.ParseMessage(file) + break } } defer file.Close() } - fmt.Scanln() } diff --git a/messages/messages.go b/messages/messages.go index 04c7268..7d00ec1 100644 --- a/messages/messages.go +++ b/messages/messages.go @@ -3,43 +3,38 @@ package messages import ( "fmt" "os" - "parser/classes" "parser/utils" ) -const ( - MSSC int32 = 2 -) +const MSSC int32 = 2 -func MessageTypeCheck(file *os.File) (statusCode int) { - Type := make([]byte, 1) - Tick := make([]byte, 4) - Slot := make([]byte, 1) - file.Read(Type) - file.Read(Tick) - file.Read(Slot) - switch Type[0] { +func ParseMessage(file *os.File) (statusCode int) { + var message Message + message.Type = utils.ReadByteFromFile(file, 1)[0] + message.Tick = int(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + message.Slot = utils.ReadByteFromFile(file, 1)[0] + switch message.Type { case 0x01: // SignOn var packet Packet - var cmdinfo classes.CmdInfo + // var cmdinfo classes.CmdInfo packet.PacketInfo = utils.ReadByteFromFile(file, 76*MSSC) packet.InSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) packet.OutSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) packet.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) packet.Data = utils.ReadByteFromFile(file, packet.Size) - cmdinfo = classes.CmdInfoInit(packet.PacketInfo) - fmt.Println(cmdinfo) + // cmdinfo = classes.CmdInfoInit(packet.PacketInfo) + // fmt.Println(cmdinfo) return 1 case 0x02: // Packet var packet Packet - var cmdinfo classes.CmdInfo + // var cmdinfo classes.CmdInfo packet.PacketInfo = utils.ReadByteFromFile(file, 76*MSSC) packet.InSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) packet.OutSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) packet.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) packet.Data = utils.ReadByteFromFile(file, packet.Size) - cmdinfo = classes.CmdInfoInit(packet.PacketInfo) - fmt.Printf("[%d] %v\n", utils.IntFromBytes(Tick), cmdinfo) + // cmdinfo = classes.CmdInfoInit(packet.PacketInfo) + // fmt.Printf("[%d] %v\n", utils.IntFromBytes(Tick), cmdinfo) return 2 case 0x03: // SyncTick return 3 @@ -47,16 +42,16 @@ func MessageTypeCheck(file *os.File) (statusCode int) { var consolecmd ConsoleCmd consolecmd.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) consolecmd.Data = string(utils.ReadByteFromFile(file, consolecmd.Size)) - fmt.Printf("[%d] %s\n", utils.IntFromBytes(Tick), consolecmd.Data) + fmt.Printf("[%d] %s\n", message.Tick, consolecmd.Data) return 4 case 0x05: // Usercmd FIXME: Correct bit-packing inside classes var usercmd UserCmd - var usercmdinfo classes.UserCmdInfo + // var usercmdinfo classes.UserCmdInfo usercmd.Cmd = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) usercmd.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) usercmd.Data = utils.ReadByteFromFile(file, usercmd.Size) - usercmdinfo = classes.UserCmdInfoInit(usercmd.Data, int(usercmd.Size)) - fmt.Printf("[%d] UserCmd: %v\n", utils.IntFromBytes(Tick), usercmdinfo) + // usercmdinfo = classes.UserCmdInfoInit(usercmd.Data, int(usercmd.Size)) + // fmt.Printf("[%d] UserCmd: %v\n", utils.IntFromBytes(Tick), usercmdinfo) return 5 case 0x06: // DataTables var datatables DataTables @@ -67,26 +62,37 @@ func MessageTypeCheck(file *os.File) (statusCode int) { // fmt.Printf("[%d] DataTables: %v\n", utils.IntFromBytes(Size), stringtable) return 6 case 0x07: // Stop - fmt.Println("Stop") + fmt.Println("Stop - End of Demo") return 7 case 0x08: // CustomData - Unknown := make([]byte, 4) - file.Read(Unknown) - Size := make([]byte, 4) - file.Read(Size) - Data := make([]byte, utils.IntFromBytes(Size)) - file.Read(Data) + var customdata CustomData + customdata.Unknown = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + customdata.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + customdata.Data = utils.ReadByteFromFile(file, customdata.Size) return 8 case 0x09: // StringTables - Size := make([]byte, 4) - file.Read(Size) - Data := make([]byte, utils.IntFromBytes(Size)) - file.Read(Data) + var stringtables StringTables + stringtables.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + stringtables.Data = utils.ReadByteFromFile(file, stringtables.Size) return 9 default: return 0 } - //fmt.Println(Type[0]) - //fmt.Println(utils.IntFromBytes(Tick)) - //fmt.Println(Slot[0]) + +} + +func ParseHeader(file *os.File) { + var header Header + header.DemoFileStamp = string(utils.ReadByteFromFile(file, 8)) + header.DemoProtocol = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + header.NetworkProtocol = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + header.ServerName = string(utils.ReadByteFromFile(file, 260)) + header.ClientName = string(utils.ReadByteFromFile(file, 260)) + header.MapName = string(utils.ReadByteFromFile(file, 260)) + header.GameDirectory = string(utils.ReadByteFromFile(file, 260)) + header.PlaybackTime = float32(utils.FloatFromBytes(utils.ReadByteFromFile(file, 4))) + header.PlaybackTicks = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + header.PlaybackFrames = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + header.SignOnLength = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) + fmt.Println(header) } diff --git a/messages/types.go b/messages/types.go index ee42a96..bb19444 100644 --- a/messages/types.go +++ b/messages/types.go @@ -1,5 +1,25 @@ package messages +type Header struct { + DemoFileStamp string + DemoProtocol int32 + NetworkProtocol int32 + ServerName string + ClientName string + MapName string + GameDirectory string + PlaybackTime float32 + PlaybackTicks int32 + PlaybackFrames int32 + SignOnLength int32 +} + +type Message struct { + Type byte + Tick int + Slot byte +} + type Packet struct { PacketInfo []byte InSequence int32 diff --git a/utils/header.go b/utils/header.go deleted file mode 100644 index 68def9e..0000000 --- a/utils/header.go +++ /dev/null @@ -1,43 +0,0 @@ -package utils - -import ( - "fmt" - "os" -) - -func HeaderOut(file *os.File) { - DemoFileStamp := make([]byte, 8) - DemoProtocol := make([]byte, 4) - NetworkProtocol := make([]byte, 4) - ServerName := make([]byte, 260) - ClientName := make([]byte, 260) - MapName := make([]byte, 260) - GameDirectory := make([]byte, 260) - PlaybackTime := make([]byte, 4) - PlaybackTicks := make([]byte, 4) - PlaybackFrames := make([]byte, 4) - SignOnLength := make([]byte, 4) - file.Read(DemoFileStamp) - file.Read(DemoProtocol) - file.Read(NetworkProtocol) - file.Read(ServerName) - file.Read(ClientName) - file.Read(MapName) - file.Read(GameDirectory) - file.Read(PlaybackTime) - file.Read(PlaybackTicks) - file.Read(PlaybackFrames) - file.Read(SignOnLength) - - fmt.Println(string(DemoFileStamp)) - fmt.Println(IntFromBytes(DemoProtocol)) - fmt.Println(IntFromBytes(NetworkProtocol)) - fmt.Println(string(ServerName)) - fmt.Println(string(ClientName)) - fmt.Println(string(MapName)) - fmt.Println(string(GameDirectory)) - fmt.Println(FloatFromBytes(PlaybackTime)) - fmt.Println(IntFromBytes(PlaybackTicks)) - fmt.Println(IntFromBytes(PlaybackFrames)) - fmt.Println(IntFromBytes(SignOnLength)) -} diff --git a/utils/utils.go b/utils/utils.go index 6db28b0..5226e80 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -9,6 +9,12 @@ import ( "unsafe" ) +func CheckError(e error) { + if e != nil { + log.Panic(e) + } +} + func ReverseByteArrayValues(byteArr []byte, size int) []byte { arr := make([]byte, size) for index, byteValue := range byteArr { @@ -23,12 +29,6 @@ func ReadByteFromFile(file *os.File, size int32) []byte { return tmp } -func CheckError(e error) { - if e != nil { - log.Panic(e) - } -} - func IntFromBytes(byteArr []byte) uint32 { int := binary.LittleEndian.Uint32(byteArr) return int -- cgit v1.2.3