aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBiSaXa <1669855+BiSaXa@users.noreply.github.com>2022-08-27 23:26:28 +0300
committerBiSaXa <1669855+BiSaXa@users.noreply.github.com>2022-08-27 23:26:28 +0300
commit2f9aeb762294fe96606c76c2fefa17a6540639b5 (patch)
treec68ae6039dcf3ab338edffecbf2971d0220a444f
parentit's a mess (diff)
downloadsdp.go-2f9aeb762294fe96606c76c2fefa17a6540639b5.tar.gz
sdp.go-2f9aeb762294fe96606c76c2fefa17a6540639b5.tar.bz2
sdp.go-2f9aeb762294fe96606c76c2fefa17a6540639b5.zip
some cleaning
-rw-r--r--classes/userCmdInfo.go8
-rw-r--r--go.mod2
-rw-r--r--go.sum2
-rw-r--r--main.go17
-rw-r--r--messages/messages.go78
-rw-r--r--messages/types.go20
-rw-r--r--utils/header.go43
-rw-r--r--utils/utils.go12
8 files changed, 78 insertions, 104 deletions
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 @@
1package classes 1package classes
2 2
3import (
4 "parser/utils"
5
6 "github.com/potterxu/bitreader"
7)
8
9type UserCmdInfo struct { 3type UserCmdInfo struct {
10 CommandNumber int32 4 CommandNumber int32
11 TickCount int32 5 TickCount int32
@@ -17,6 +11,7 @@ type UserCmdInfo struct {
17 UpMove float32 11 UpMove float32
18} 12}
19 13
14/*
20func UserCmdInfoInit(byteArr []byte, size int) (output UserCmdInfo) { 15func UserCmdInfoInit(byteArr []byte, size int) (output UserCmdInfo) {
21 var class UserCmdInfo 16 var class UserCmdInfo
22 reversedByteArr := utils.ReverseByteArrayValues(byteArr, size) 17 reversedByteArr := utils.ReverseByteArrayValues(byteArr, size)
@@ -111,3 +106,4 @@ func UserCmdInfoInit(byteArr []byte, size int) (output UserCmdInfo) {
111 } 106 }
112 return class 107 return class
113} 108}
109*/
diff --git a/go.mod b/go.mod
index e58d4e9..d0e178d 100644
--- a/go.mod
+++ b/go.mod
@@ -1,5 +1,3 @@
1module parser 1module parser
2 2
3go 1.19 3go 1.19
4
5require 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 @@
1github.com/potterxu/bitreader v0.0.2 h1:oFOXpMwuatGrIY6pmBAQRpwt1lRWfINOtU+XT03/eTc=
2github.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() {
17 if err != nil { // If it's not a directory 17 if err != nil { // If it's not a directory
18 file, err := os.Open(os.Args[1]) 18 file, err := os.Open(os.Args[1])
19 utils.CheckError(err) 19 utils.CheckError(err)
20 utils.HeaderOut(file) 20 messages.ParseHeader(file)
21 for { 21 for {
22 code := messages.MessageTypeCheck(file) 22 code := messages.ParseMessage(file)
23 if code == 7 { 23 if code == 7 {
24 messages.MessageTypeCheck(file) 24 messages.ParseMessage(file)
25 break // TODO: Check last CustomData 25 break
26 } 26 }
27 } 27 }
28 defer file.Close() 28 defer file.Close()
@@ -30,16 +30,15 @@ func main() {
30 for _, fileinfo := range files { // If it is a directory 30 for _, fileinfo := range files { // If it is a directory
31 file, err := os.Open(os.Args[1] + fileinfo.Name()) 31 file, err := os.Open(os.Args[1] + fileinfo.Name())
32 utils.CheckError(err) 32 utils.CheckError(err)
33 utils.HeaderOut(file) 33 messages.ParseHeader(file)
34 for { 34 for {
35 code := messages.MessageTypeCheck(file) 35 code := messages.ParseMessage(file)
36 if code == 7 { 36 if code == 7 {
37 messages.MessageTypeCheck(file) 37 messages.ParseMessage(file)
38 break // TODO: Check last CustomData 38 break
39 } 39 }
40 } 40 }
41 defer file.Close() 41 defer file.Close()
42 } 42 }
43
44 fmt.Scanln() 43 fmt.Scanln()
45} 44}
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
3import ( 3import (
4 "fmt" 4 "fmt"
5 "os" 5 "os"
6 "parser/classes"
7 "parser/utils" 6 "parser/utils"
8) 7)
9 8
10const ( 9const MSSC int32 = 2
11 MSSC int32 = 2
12)
13 10
14func MessageTypeCheck(file *os.File) (statusCode int) { 11func ParseMessage(file *os.File) (statusCode int) {
15 Type := make([]byte, 1) 12 var message Message
16 Tick := make([]byte, 4) 13 message.Type = utils.ReadByteFromFile(file, 1)[0]
17 Slot := make([]byte, 1) 14 message.Tick = int(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
18 file.Read(Type) 15 message.Slot = utils.ReadByteFromFile(file, 1)[0]
19 file.Read(Tick) 16 switch message.Type {
20 file.Read(Slot)
21 switch Type[0] {
22 case 0x01: // SignOn 17 case 0x01: // SignOn
23 var packet Packet 18 var packet Packet
24 var cmdinfo classes.CmdInfo 19 // var cmdinfo classes.CmdInfo
25 packet.PacketInfo = utils.ReadByteFromFile(file, 76*MSSC) 20 packet.PacketInfo = utils.ReadByteFromFile(file, 76*MSSC)
26 packet.InSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 21 packet.InSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
27 packet.OutSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 22 packet.OutSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
28 packet.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 23 packet.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
29 packet.Data = utils.ReadByteFromFile(file, packet.Size) 24 packet.Data = utils.ReadByteFromFile(file, packet.Size)
30 cmdinfo = classes.CmdInfoInit(packet.PacketInfo) 25 // cmdinfo = classes.CmdInfoInit(packet.PacketInfo)
31 fmt.Println(cmdinfo) 26 // fmt.Println(cmdinfo)
32 return 1 27 return 1
33 case 0x02: // Packet 28 case 0x02: // Packet
34 var packet Packet 29 var packet Packet
35 var cmdinfo classes.CmdInfo 30 // var cmdinfo classes.CmdInfo
36 packet.PacketInfo = utils.ReadByteFromFile(file, 76*MSSC) 31 packet.PacketInfo = utils.ReadByteFromFile(file, 76*MSSC)
37 packet.InSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 32 packet.InSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
38 packet.OutSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 33 packet.OutSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
39 packet.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 34 packet.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
40 packet.Data = utils.ReadByteFromFile(file, packet.Size) 35 packet.Data = utils.ReadByteFromFile(file, packet.Size)
41 cmdinfo = classes.CmdInfoInit(packet.PacketInfo) 36 // cmdinfo = classes.CmdInfoInit(packet.PacketInfo)
42 fmt.Printf("[%d] %v\n", utils.IntFromBytes(Tick), cmdinfo) 37 // fmt.Printf("[%d] %v\n", utils.IntFromBytes(Tick), cmdinfo)
43 return 2 38 return 2
44 case 0x03: // SyncTick 39 case 0x03: // SyncTick
45 return 3 40 return 3
@@ -47,16 +42,16 @@ func MessageTypeCheck(file *os.File) (statusCode int) {
47 var consolecmd ConsoleCmd 42 var consolecmd ConsoleCmd
48 consolecmd.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 43 consolecmd.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
49 consolecmd.Data = string(utils.ReadByteFromFile(file, consolecmd.Size)) 44 consolecmd.Data = string(utils.ReadByteFromFile(file, consolecmd.Size))
50 fmt.Printf("[%d] %s\n", utils.IntFromBytes(Tick), consolecmd.Data) 45 fmt.Printf("[%d] %s\n", message.Tick, consolecmd.Data)
51 return 4 46 return 4
52 case 0x05: // Usercmd FIXME: Correct bit-packing inside classes 47 case 0x05: // Usercmd FIXME: Correct bit-packing inside classes
53 var usercmd UserCmd 48 var usercmd UserCmd
54 var usercmdinfo classes.UserCmdInfo 49 // var usercmdinfo classes.UserCmdInfo
55 usercmd.Cmd = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 50 usercmd.Cmd = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
56 usercmd.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) 51 usercmd.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
57 usercmd.Data = utils.ReadByteFromFile(file, usercmd.Size) 52 usercmd.Data = utils.ReadByteFromFile(file, usercmd.Size)
58 usercmdinfo = classes.UserCmdInfoInit(usercmd.Data, int(usercmd.Size)) 53 // usercmdinfo = classes.UserCmdInfoInit(usercmd.Data, int(usercmd.Size))
59 fmt.Printf("[%d] UserCmd: %v\n", utils.IntFromBytes(Tick), usercmdinfo) 54 // fmt.Printf("[%d] UserCmd: %v\n", utils.IntFromBytes(Tick), usercmdinfo)
60 return 5 55 return 5
61 case 0x06: // DataTables 56 case 0x06: // DataTables
62 var datatables DataTables 57 var datatables DataTables
@@ -67,26 +62,37 @@ func MessageTypeCheck(file *os.File) (statusCode int) {
67 // fmt.Printf("[%d] DataTables: %v\n", utils.IntFromBytes(Size), stringtable) 62 // fmt.Printf("[%d] DataTables: %v\n", utils.IntFromBytes(Size), stringtable)
68 return 6 63 return 6
69 case 0x07: // Stop 64 case 0x07: // Stop
70 fmt.Println("Stop") 65 fmt.Println("Stop - End of Demo")
71 return 7 66 return 7
72 case 0x08: // CustomData 67 case 0x08: // CustomData
73 Unknown := make([]byte, 4) 68 var customdata CustomData
74 file.Read(Unknown) 69 customdata.Unknown = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
75 Size := make([]byte, 4) 70 customdata.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
76 file.Read(Size) 71 customdata.Data = utils.ReadByteFromFile(file, customdata.Size)
77 Data := make([]byte, utils.IntFromBytes(Size))
78 file.Read(Data)
79 return 8 72 return 8
80 case 0x09: // StringTables 73 case 0x09: // StringTables
81 Size := make([]byte, 4) 74 var stringtables StringTables
82 file.Read(Size) 75 stringtables.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
83 Data := make([]byte, utils.IntFromBytes(Size)) 76 stringtables.Data = utils.ReadByteFromFile(file, stringtables.Size)
84 file.Read(Data)
85 return 9 77 return 9
86 default: 78 default:
87 return 0 79 return 0
88 } 80 }
89 //fmt.Println(Type[0]) 81
90 //fmt.Println(utils.IntFromBytes(Tick)) 82}
91 //fmt.Println(Slot[0]) 83
84func ParseHeader(file *os.File) {
85 var header Header
86 header.DemoFileStamp = string(utils.ReadByteFromFile(file, 8))
87 header.DemoProtocol = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
88 header.NetworkProtocol = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
89 header.ServerName = string(utils.ReadByteFromFile(file, 260))
90 header.ClientName = string(utils.ReadByteFromFile(file, 260))
91 header.MapName = string(utils.ReadByteFromFile(file, 260))
92 header.GameDirectory = string(utils.ReadByteFromFile(file, 260))
93 header.PlaybackTime = float32(utils.FloatFromBytes(utils.ReadByteFromFile(file, 4)))
94 header.PlaybackTicks = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
95 header.PlaybackFrames = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
96 header.SignOnLength = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4)))
97 fmt.Println(header)
92} 98}
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 @@
1package messages 1package messages
2 2
3type Header struct {
4 DemoFileStamp string
5 DemoProtocol int32
6 NetworkProtocol int32
7 ServerName string
8 ClientName string
9 MapName string
10 GameDirectory string
11 PlaybackTime float32
12 PlaybackTicks int32
13 PlaybackFrames int32
14 SignOnLength int32
15}
16
17type Message struct {
18 Type byte
19 Tick int
20 Slot byte
21}
22
3type Packet struct { 23type Packet struct {
4 PacketInfo []byte 24 PacketInfo []byte
5 InSequence int32 25 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 @@
1package utils
2
3import (
4 "fmt"
5 "os"
6)
7
8func HeaderOut(file *os.File) {
9 DemoFileStamp := make([]byte, 8)
10 DemoProtocol := make([]byte, 4)
11 NetworkProtocol := make([]byte, 4)
12 ServerName := make([]byte, 260)
13 ClientName := make([]byte, 260)
14 MapName := make([]byte, 260)
15 GameDirectory := make([]byte, 260)
16 PlaybackTime := make([]byte, 4)
17 PlaybackTicks := make([]byte, 4)
18 PlaybackFrames := make([]byte, 4)
19 SignOnLength := make([]byte, 4)
20 file.Read(DemoFileStamp)
21 file.Read(DemoProtocol)
22 file.Read(NetworkProtocol)
23 file.Read(ServerName)
24 file.Read(ClientName)
25 file.Read(MapName)
26 file.Read(GameDirectory)
27 file.Read(PlaybackTime)
28 file.Read(PlaybackTicks)
29 file.Read(PlaybackFrames)
30 file.Read(SignOnLength)
31
32 fmt.Println(string(DemoFileStamp))
33 fmt.Println(IntFromBytes(DemoProtocol))
34 fmt.Println(IntFromBytes(NetworkProtocol))
35 fmt.Println(string(ServerName))
36 fmt.Println(string(ClientName))
37 fmt.Println(string(MapName))
38 fmt.Println(string(GameDirectory))
39 fmt.Println(FloatFromBytes(PlaybackTime))
40 fmt.Println(IntFromBytes(PlaybackTicks))
41 fmt.Println(IntFromBytes(PlaybackFrames))
42 fmt.Println(IntFromBytes(SignOnLength))
43}
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 (
9 "unsafe" 9 "unsafe"
10) 10)
11 11
12func CheckError(e error) {
13 if e != nil {
14 log.Panic(e)
15 }
16}
17
12func ReverseByteArrayValues(byteArr []byte, size int) []byte { 18func ReverseByteArrayValues(byteArr []byte, size int) []byte {
13 arr := make([]byte, size) 19 arr := make([]byte, size)
14 for index, byteValue := range byteArr { 20 for index, byteValue := range byteArr {
@@ -23,12 +29,6 @@ func ReadByteFromFile(file *os.File, size int32) []byte {
23 return tmp 29 return tmp
24} 30}
25 31
26func CheckError(e error) {
27 if e != nil {
28 log.Panic(e)
29 }
30}
31
32func IntFromBytes(byteArr []byte) uint32 { 32func IntFromBytes(byteArr []byte) uint32 {
33 int := binary.LittleEndian.Uint32(byteArr) 33 int := binary.LittleEndian.Uint32(byteArr)
34 return int 34 return int