diff options
| author | BiSaXa <1669855+BiSaXa@users.noreply.github.com> | 2022-09-07 19:40:16 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-16 21:39:36 +0300 |
| commit | 4e71a481884c0c43aea3d0ee695ae68cdec56478 (patch) | |
| tree | d75c96612526a7c686d9f4d85af6b89c7cd864e6 | |
| parent | final commit before rewrite (diff) | |
| download | sdp.go-4e71a481884c0c43aea3d0ee695ae68cdec56478.tar.gz sdp.go-4e71a481884c0c43aea3d0ee695ae68cdec56478.tar.bz2 sdp.go-4e71a481884c0c43aea3d0ee695ae68cdec56478.zip | |
first rewrite commit using bisaxa/bitreader
| -rw-r--r-- | classes/classes.go | 155 | ||||
| -rw-r--r-- | classes/cmdInfo.go | 25 | ||||
| -rw-r--r-- | classes/stringTable.go | 27 | ||||
| -rw-r--r-- | classes/types.go | 28 | ||||
| -rw-r--r-- | classes/userCmdInfo.go | 72 | ||||
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | main.go | 14 | ||||
| -rw-r--r-- | messages/header.go | 26 | ||||
| -rw-r--r-- | messages/messages.go | 91 | ||||
| -rw-r--r-- | messages/types.go | 12 | ||||
| -rw-r--r-- | utils/bitreader.go | 94 | ||||
| -rw-r--r-- | utils/utils.go | 44 |
13 files changed, 259 insertions, 335 deletions
diff --git a/classes/classes.go b/classes/classes.go new file mode 100644 index 0000000..beaab77 --- /dev/null +++ b/classes/classes.go | |||
| @@ -0,0 +1,155 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "os" | ||
| 5 | |||
| 6 | "github.com/bisaxa/bitreader" | ||
| 7 | "github.com/bisaxa/demoparser/utils" | ||
| 8 | ) | ||
| 9 | |||
| 10 | func ParseCmdInfo(file *os.File, MSSC int) []CmdInfo { | ||
| 11 | reader := bitreader.Reader(file, true) | ||
| 12 | var cmdinfo CmdInfo | ||
| 13 | var cmdinfoarray []CmdInfo | ||
| 14 | for count := 0; count < MSSC; count++ { | ||
| 15 | cmdinfo.Flags = int32(reader.TryReadInt32()) | ||
| 16 | var floatArray [3]float32 | ||
| 17 | for i := 0; i < 3; i++ { | ||
| 18 | floatArray[i] = reader.TryReadFloat32() | ||
| 19 | } | ||
| 20 | cmdinfo.ViewOrigin = floatArray[:] | ||
| 21 | for i := 0; i < 3; i++ { | ||
| 22 | floatArray[i] = reader.TryReadFloat32() | ||
| 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) | ||
| 42 | } | ||
| 43 | return cmdinfoarray | ||
| 44 | } | ||
| 45 | |||
| 46 | func ParseUserCmdInfo(file *os.File, size int) UserCmdInfo { | ||
| 47 | count := 0 | ||
| 48 | reader := bitreader.Reader(file, true) | ||
| 49 | var usercmd UserCmdInfo | ||
| 50 | flag, err := reader.ReadBool() | ||
| 51 | utils.CheckError(err) | ||
| 52 | if flag { | ||
| 53 | usercmd.CommandNumber = int32(reader.TryReadInt32()) | ||
| 54 | count += 32 | ||
| 55 | } | ||
| 56 | count++ | ||
| 57 | flag, err = reader.ReadBool() | ||
| 58 | utils.CheckError(err) | ||
| 59 | if flag { | ||
| 60 | usercmd.TickCount = int32(reader.TryReadInt32()) | ||
| 61 | count += 32 | ||
| 62 | } | ||
| 63 | count++ | ||
| 64 | flag, err = reader.ReadBool() | ||
| 65 | utils.CheckError(err) | ||
| 66 | if flag { | ||
| 67 | usercmd.ViewAnglesX = reader.TryReadFloat32() | ||
| 68 | count += 32 | ||
| 69 | } | ||
| 70 | count++ | ||
| 71 | flag, err = reader.ReadBool() | ||
| 72 | utils.CheckError(err) | ||
| 73 | if flag { | ||
| 74 | usercmd.ViewAnglesY = reader.TryReadFloat32() | ||
| 75 | count += 32 | ||
| 76 | } | ||
| 77 | count++ | ||
| 78 | flag, err = reader.ReadBool() | ||
| 79 | utils.CheckError(err) | ||
| 80 | if flag { | ||
| 81 | usercmd.ViewAnglesZ = reader.TryReadFloat32() | ||
| 82 | count += 32 | ||
| 83 | } | ||
| 84 | count++ | ||
| 85 | flag, err = reader.ReadBool() | ||
| 86 | utils.CheckError(err) | ||
| 87 | if flag { | ||
| 88 | usercmd.ForwardMove = reader.TryReadFloat32() | ||
| 89 | count += 32 | ||
| 90 | } | ||
| 91 | count++ | ||
| 92 | flag, err = reader.ReadBool() | ||
| 93 | utils.CheckError(err) | ||
| 94 | if flag { | ||
| 95 | usercmd.SideMove = reader.TryReadFloat32() | ||
| 96 | count += 32 | ||
| 97 | } | ||
| 98 | count++ | ||
| 99 | flag, err = reader.ReadBool() | ||
| 100 | utils.CheckError(err) | ||
| 101 | if flag { | ||
| 102 | usercmd.UpMove = reader.TryReadFloat32() | ||
| 103 | count += 32 | ||
| 104 | } | ||
| 105 | count++ | ||
| 106 | flag, err = reader.ReadBool() | ||
| 107 | utils.CheckError(err) | ||
| 108 | if flag { | ||
| 109 | usercmd.Buttons = int32(reader.TryReadInt32()) | ||
| 110 | count += 32 | ||
| 111 | } | ||
| 112 | count++ | ||
| 113 | flag, err = reader.ReadBool() | ||
| 114 | utils.CheckError(err) | ||
| 115 | if flag { | ||
| 116 | //reader.SkipBits(8) | ||
| 117 | usercmd.Impulse = int8(reader.TryReadInt8()) | ||
| 118 | count += 8 | ||
| 119 | } | ||
| 120 | count++ | ||
| 121 | flag, err = reader.ReadBool() | ||
| 122 | utils.CheckError(err) | ||
| 123 | if flag { | ||
| 124 | value, err := reader.ReadBits(11) | ||
| 125 | utils.CheckError(err) | ||
| 126 | usercmd.WeaponSelect = int(value) | ||
| 127 | flag, err = reader.ReadBool() | ||
| 128 | utils.CheckError(err) | ||
| 129 | count += 11 | ||
| 130 | if flag { | ||
| 131 | value, err := reader.ReadBits(6) | ||
| 132 | utils.CheckError(err) | ||
| 133 | usercmd.WeaponSubtype = int(value) | ||
| 134 | count += 6 | ||
| 135 | } | ||
| 136 | count++ | ||
| 137 | } | ||
| 138 | count++ | ||
| 139 | flag, err = reader.ReadBool() | ||
| 140 | utils.CheckError(err) | ||
| 141 | if flag { | ||
| 142 | usercmd.MouseDx = int16(reader.TryReadInt16()) | ||
| 143 | count += 16 | ||
| 144 | } | ||
| 145 | count++ | ||
| 146 | flag, err = reader.ReadBool() | ||
| 147 | utils.CheckError(err) | ||
| 148 | if flag { | ||
| 149 | usercmd.MouseDy = int16(reader.TryReadInt16()) | ||
| 150 | count += 16 | ||
| 151 | } | ||
| 152 | count++ | ||
| 153 | reader.SkipBits(size*8 - count) | ||
| 154 | return usercmd | ||
| 155 | } | ||
diff --git a/classes/cmdInfo.go b/classes/cmdInfo.go deleted file mode 100644 index 7e7e1bc..0000000 --- a/classes/cmdInfo.go +++ /dev/null | |||
| @@ -1,25 +0,0 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | import "github.com/bisaxa/demoparser/utils" | ||
| 4 | |||
| 5 | type CmdInfo struct { | ||
| 6 | Flags int32 | ||
| 7 | ViewOrigin []float32 | ||
| 8 | ViewAngles []float32 | ||
| 9 | LocalViewAngles []float32 | ||
| 10 | ViewOrigin2 []float32 | ||
| 11 | ViewAngles2 []float32 | ||
| 12 | LocalViewAngles2 []float32 | ||
| 13 | } | ||
| 14 | |||
| 15 | func CmdInfoInit(bytes []byte) (output CmdInfo) { | ||
| 16 | var class CmdInfo | ||
| 17 | class.Flags = int32(utils.IntFromBytes(bytes[:4])) | ||
| 18 | class.ViewOrigin = utils.FloatArrFromBytes(bytes[4:16]) | ||
| 19 | class.ViewAngles = utils.FloatArrFromBytes(bytes[16:28]) | ||
| 20 | class.LocalViewAngles = utils.FloatArrFromBytes(bytes[28:40]) | ||
| 21 | class.ViewOrigin2 = utils.FloatArrFromBytes(bytes[40:52]) | ||
| 22 | class.ViewAngles2 = utils.FloatArrFromBytes(bytes[52:64]) | ||
| 23 | class.LocalViewAngles2 = utils.FloatArrFromBytes(bytes[64:76]) | ||
| 24 | return class | ||
| 25 | } | ||
diff --git a/classes/stringTable.go b/classes/stringTable.go deleted file mode 100644 index 0833612..0000000 --- a/classes/stringTable.go +++ /dev/null | |||
| @@ -1,27 +0,0 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | type StringTable struct { | ||
| 4 | NumOfTables int32 | ||
| 5 | TableName string | ||
| 6 | NumOfEntries int16 | ||
| 7 | EntryName string | ||
| 8 | EntrySize int16 | ||
| 9 | EntryData []byte | ||
| 10 | NumOfClientEntries int16 | ||
| 11 | ClientEntryName string | ||
| 12 | ClientEntrySize int16 | ||
| 13 | ClientEntryData []byte | ||
| 14 | } | ||
| 15 | |||
| 16 | /* | ||
| 17 | func StringTableInit(bytes []byte) (output StringTable) { | ||
| 18 | var class StringTable | ||
| 19 | class.NumOfTables = int(utils.IntFromBytes(bytes[:1])) | ||
| 20 | class.TableName = string(bytes[1:16]) | ||
| 21 | class.ViewAngles = utils.FloatArrFromBytes(bytes[16:28]) | ||
| 22 | class.LocalViewAngles = utils.FloatArrFromBytes(bytes[28:40]) | ||
| 23 | class.ViewOrigin2 = utils.FloatArrFromBytes(bytes[40:52]) | ||
| 24 | class.ViewAngles2 = utils.FloatArrFromBytes(bytes[52:64]) | ||
| 25 | class.LocalViewAngles2 = utils.FloatArrFromBytes(bytes[64:76]) | ||
| 26 | return class | ||
| 27 | }*/ | ||
diff --git a/classes/types.go b/classes/types.go new file mode 100644 index 0000000..3fcc692 --- /dev/null +++ b/classes/types.go | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | type CmdInfo struct { | ||
| 4 | Flags int32 | ||
| 5 | ViewOrigin []float32 | ||
| 6 | ViewAngles []float32 | ||
| 7 | LocalViewAngles []float32 | ||
| 8 | ViewOrigin2 []float32 | ||
| 9 | ViewAngles2 []float32 | ||
| 10 | LocalViewAngles2 []float32 | ||
| 11 | } | ||
| 12 | |||
| 13 | type UserCmdInfo struct { | ||
| 14 | CommandNumber int32 | ||
| 15 | TickCount int32 | ||
| 16 | ViewAnglesX float32 | ||
| 17 | ViewAnglesY float32 | ||
| 18 | ViewAnglesZ float32 | ||
| 19 | ForwardMove float32 | ||
| 20 | SideMove float32 | ||
| 21 | UpMove float32 | ||
| 22 | Buttons int32 | ||
| 23 | Impulse int8 | ||
| 24 | WeaponSelect int | ||
| 25 | WeaponSubtype int | ||
| 26 | MouseDx int16 | ||
| 27 | MouseDy int16 | ||
| 28 | } | ||
diff --git a/classes/userCmdInfo.go b/classes/userCmdInfo.go deleted file mode 100644 index d2de229..0000000 --- a/classes/userCmdInfo.go +++ /dev/null | |||
| @@ -1,72 +0,0 @@ | |||
| 1 | package classes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | |||
| 6 | "github.com/bisaxa/demoparser/utils" | ||
| 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 byte } | ||
| 20 | // WeaponSelect int32 } | ||
| 21 | // WeaponSubtype int32 Not worth the effort, no one cares about these | ||
| 22 | // MouseDx int16 } | ||
| 23 | // MouseDy int } | ||
| 24 | } | ||
| 25 | |||
| 26 | // It is so janky it hurts, but hey it is at least working (hopefully) | ||
| 27 | // Reading the data is really weird, who even implemented this smh | ||
| 28 | func UserCmdInfoInit(byteArr []byte, size int32) (output UserCmdInfo) { | ||
| 29 | var class UserCmdInfo | ||
| 30 | successCount := 0 | ||
| 31 | failedCount := 0 | ||
| 32 | looped := 0 | ||
| 33 | classIndex := 0 | ||
| 34 | //fmt.Println(byteArr) | ||
| 35 | fmt.Printf("%08b\n", byteArr) | ||
| 36 | for i := 0; i < 9; i++ { | ||
| 37 | if successCount+failedCount > 7 { | ||
| 38 | failedCount = -successCount | ||
| 39 | looped++ | ||
| 40 | } | ||
| 41 | firstBit, err := utils.ReadBitStateLSB(byteArr[successCount*4+looped], successCount+failedCount) | ||
| 42 | utils.CheckError(err) | ||
| 43 | if firstBit { | ||
| 44 | successCount++ | ||
| 45 | switch classIndex { | ||
| 46 | case 0: | ||
| 47 | class.CommandNumber = utils.Read32BitsAfterFirstBitInt32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 48 | case 1: | ||
| 49 | class.TickCount = utils.Read32BitsAfterFirstBitInt32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 50 | case 2: | ||
| 51 | class.ViewAnglesX = utils.Read32BitsAfterFirstBitFloat32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 52 | case 3: | ||
| 53 | class.ViewAnglesY = utils.Read32BitsAfterFirstBitFloat32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 54 | case 4: | ||
| 55 | class.ViewAnglesZ = utils.Read32BitsAfterFirstBitFloat32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 56 | case 5: | ||
| 57 | class.ForwardMove = utils.Read32BitsAfterFirstBitFloat32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 58 | case 6: | ||
| 59 | class.SideMove = utils.Read32BitsAfterFirstBitFloat32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 60 | case 7: | ||
| 61 | class.UpMove = utils.Read32BitsAfterFirstBitFloat32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 62 | case 8: | ||
| 63 | class.Buttons = utils.Read32BitsAfterFirstBitInt32(byteArr, successCount+failedCount, successCount*4+looped) | ||
| 64 | } | ||
| 65 | classIndex++ | ||
| 66 | } else { | ||
| 67 | failedCount++ | ||
| 68 | classIndex++ | ||
| 69 | } | ||
| 70 | } | ||
| 71 | return class | ||
| 72 | } | ||
| @@ -1,3 +1,5 @@ | |||
| 1 | module github.com/bisaxa/demoparser | 1 | module github.com/bisaxa/demoparser |
| 2 | 2 | ||
| 3 | go 1.19 | 3 | go 1.19 |
| 4 | |||
| 5 | require github.com/bisaxa/bitreader v1.0.1-0.20220907151525-64e0e23b8a0f | ||
| @@ -0,0 +1,4 @@ | |||
| 1 | github.com/bisaxa/bitreader v1.0.1-0.20220907142402-05d7eb4e0f7b h1:OBmlKXvF6Uo+uMowYnu3y3BJWAnZlQzLAJYuwXS97HI= | ||
| 2 | github.com/bisaxa/bitreader v1.0.1-0.20220907142402-05d7eb4e0f7b/go.mod h1:AKIwOt+ZqRtuT7SyjGAF8x0kVPhlK8fgTVMlPEh7d0I= | ||
| 3 | github.com/bisaxa/bitreader v1.0.1-0.20220907151525-64e0e23b8a0f h1:b48EQo2AaOOAxsWGk3NI+ZJDrXe9YT9KKSXz/agSuzQ= | ||
| 4 | github.com/bisaxa/bitreader v1.0.1-0.20220907151525-64e0e23b8a0f/go.mod h1:AKIwOt+ZqRtuT7SyjGAF8x0kVPhlK8fgTVMlPEh7d0I= | ||
| @@ -7,7 +7,6 @@ import ( | |||
| 7 | "os" | 7 | "os" |
| 8 | 8 | ||
| 9 | "github.com/bisaxa/demoparser/messages" | 9 | "github.com/bisaxa/demoparser/messages" |
| 10 | "github.com/bisaxa/demoparser/utils" | ||
| 11 | ) | 10 | ) |
| 12 | 11 | ||
| 13 | func main() { | 12 | func main() { |
| @@ -17,12 +16,13 @@ func main() { | |||
| 17 | files, err := ioutil.ReadDir(os.Args[1]) | 16 | files, err := ioutil.ReadDir(os.Args[1]) |
| 18 | if err != nil { // If it's not a directory | 17 | if err != nil { // If it's not a directory |
| 19 | file, err := os.Open(os.Args[1]) | 18 | file, err := os.Open(os.Args[1]) |
| 20 | utils.CheckError(err) | 19 | if err != nil { |
| 20 | panic(err) | ||
| 21 | } | ||
| 21 | messages.ParseHeader(file) | 22 | messages.ParseHeader(file) |
| 22 | for { | 23 | for { |
| 23 | code := messages.ParseMessage(file) | 24 | code := messages.ParseMessage(file) |
| 24 | if code == 7 { | 25 | if code == 7 { |
| 25 | messages.ParseMessage(file) | ||
| 26 | break | 26 | break |
| 27 | } | 27 | } |
| 28 | } | 28 | } |
| @@ -30,15 +30,17 @@ func main() { | |||
| 30 | } | 30 | } |
| 31 | for _, fileinfo := range files { // If it is a directory | 31 | for _, fileinfo := range files { // If it is a directory |
| 32 | file, err := os.Open(os.Args[1] + fileinfo.Name()) | 32 | file, err := os.Open(os.Args[1] + fileinfo.Name()) |
| 33 | utils.CheckError(err) | 33 | if err != nil { |
| 34 | messages.ParseHeader(file) | 34 | panic(err) |
| 35 | } | ||
| 36 | /*messages.ParseHeader(file) | ||
| 35 | for { | 37 | for { |
| 36 | code := messages.ParseMessage(file) | 38 | code := messages.ParseMessage(file) |
| 37 | if code == 7 { | 39 | if code == 7 { |
| 38 | messages.ParseMessage(file) | 40 | messages.ParseMessage(file) |
| 39 | break | 41 | break |
| 40 | } | 42 | } |
| 41 | } | 43 | }*/ |
| 42 | defer file.Close() | 44 | defer file.Close() |
| 43 | } | 45 | } |
| 44 | fmt.Scanln() | 46 | fmt.Scanln() |
diff --git a/messages/header.go b/messages/header.go new file mode 100644 index 0000000..6320c71 --- /dev/null +++ b/messages/header.go | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | package messages | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "os" | ||
| 6 | |||
| 7 | "github.com/bisaxa/bitreader" | ||
| 8 | "github.com/bisaxa/demoparser/utils" | ||
| 9 | ) | ||
| 10 | |||
| 11 | func ParseHeader(file *os.File) { | ||
| 12 | var header Header | ||
| 13 | reader := bitreader.Reader(file, true) | ||
| 14 | header.DemoFileStamp = string(utils.ReadByteFromFile(file, 8)) | ||
| 15 | header.DemoProtocol = int32(reader.TryReadInt32()) | ||
| 16 | header.NetworkProtocol = int32(reader.TryReadInt32()) | ||
| 17 | header.ServerName = string(utils.ReadByteFromFile(file, 260)) | ||
| 18 | header.ClientName = string(utils.ReadByteFromFile(file, 260)) | ||
| 19 | header.MapName = string(utils.ReadByteFromFile(file, 260)) | ||
| 20 | header.GameDirectory = string(utils.ReadByteFromFile(file, 260)) | ||
| 21 | header.PlaybackTime = float32(reader.TryReadFloat32()) | ||
| 22 | header.PlaybackTicks = int32(reader.TryReadInt32()) | ||
| 23 | header.PlaybackFrames = int32(reader.TryReadInt32()) | ||
| 24 | header.SignOnLength = int32(reader.TryReadInt32()) | ||
| 25 | fmt.Printf("%+v", header) | ||
| 26 | } | ||
diff --git a/messages/messages.go b/messages/messages.go index a2b0ebd..4d94728 100644 --- a/messages/messages.go +++ b/messages/messages.go | |||
| @@ -4,97 +4,66 @@ import ( | |||
| 4 | "fmt" | 4 | "fmt" |
| 5 | "os" | 5 | "os" |
| 6 | 6 | ||
| 7 | "github.com/bisaxa/bitreader" | ||
| 7 | "github.com/bisaxa/demoparser/classes" | 8 | "github.com/bisaxa/demoparser/classes" |
| 8 | "github.com/bisaxa/demoparser/utils" | 9 | "github.com/bisaxa/demoparser/utils" |
| 9 | ) | 10 | ) |
| 10 | 11 | ||
| 11 | const MSSC int32 = 2 | ||
| 12 | |||
| 13 | func ParseMessage(file *os.File) (statusCode int) { | 12 | func ParseMessage(file *os.File) (statusCode int) { |
| 14 | var message Message | 13 | reader := bitreader.Reader(file, true) |
| 15 | message.Type = utils.ReadByteFromFile(file, 1)[0] | 14 | messageType := reader.TryReadInt8() |
| 16 | message.Tick = int(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 15 | messageTick := reader.TryReadInt32() |
| 17 | message.Slot = utils.ReadByteFromFile(file, 1)[0] | 16 | messageSlot := reader.TryReadInt8() |
| 18 | switch message.Type { | 17 | fmt.Println(messageType, messageTick, messageSlot) |
| 18 | switch messageType { | ||
| 19 | case 0x01: // SignOn | 19 | case 0x01: // SignOn |
| 20 | var packet Packet | 20 | var packet Packet |
| 21 | // var cmdinfo classes.CmdInfo | 21 | packet.PacketInfo = classes.ParseCmdInfo(file, 2) |
| 22 | packet.PacketInfo = utils.ReadByteFromFile(file, 76*MSSC) | 22 | packet.InSequence = int32(reader.TryReadInt32()) |
| 23 | packet.InSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 23 | packet.OutSequence = int32(reader.TryReadInt32()) |
| 24 | packet.OutSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 24 | packet.Size = int32(reader.TryReadInt32()) |
| 25 | packet.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 25 | reader.SkipBytes(int(packet.Size)) |
| 26 | packet.Data = utils.ReadByteFromFile(file, packet.Size) | ||
| 27 | // cmdinfo = classes.CmdInfoInit(packet.PacketInfo) | ||
| 28 | // fmt.Println(cmdinfo) | ||
| 29 | return 1 | 26 | return 1 |
| 30 | case 0x02: // Packet | 27 | case 0x02: // Packet |
| 31 | var packet Packet | 28 | var packet Packet |
| 32 | // var cmdinfo classes.CmdInfo | 29 | packet.PacketInfo = classes.ParseCmdInfo(file, 2) |
| 33 | packet.PacketInfo = utils.ReadByteFromFile(file, 76*MSSC) | 30 | packet.InSequence = int32(reader.TryReadInt32()) |
| 34 | packet.InSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 31 | packet.OutSequence = int32(reader.TryReadInt32()) |
| 35 | packet.OutSequence = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 32 | packet.Size = int32(reader.TryReadInt32()) |
| 36 | packet.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 33 | reader.SkipBytes(int(packet.Size)) |
| 37 | packet.Data = utils.ReadByteFromFile(file, packet.Size) | ||
| 38 | // cmdinfo = classes.CmdInfoInit(packet.PacketInfo) | ||
| 39 | // fmt.Printf("[%d] %v\n", utils.IntFromBytes(Tick), cmdinfo) | ||
| 40 | return 2 | 34 | return 2 |
| 41 | case 0x03: // SyncTick | 35 | case 0x03: // SyncTick |
| 42 | return 3 | 36 | return 3 |
| 43 | case 0x04: // Consolecmd | 37 | case 0x04: // ConsoleCmd |
| 44 | var consolecmd ConsoleCmd | 38 | var consolecmd ConsoleCmd |
| 45 | consolecmd.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 39 | consolecmd.Size = int32(reader.TryReadInt32()) |
| 46 | consolecmd.Data = string(utils.ReadByteFromFile(file, consolecmd.Size)) | 40 | consolecmd.Data = string(utils.ReadByteFromFile(file, consolecmd.Size)) |
| 47 | //fmt.Printf("[%d] %s\n", message.Tick, consolecmd.Data) | ||
| 48 | return 4 | 41 | return 4 |
| 49 | case 0x05: // Usercmd | 42 | case 0x05: // UserCmd |
| 50 | var usercmd UserCmd | 43 | var usercmd UserCmd |
| 51 | var usercmdinfo classes.UserCmdInfo | 44 | usercmd.Cmd = int32(reader.TryReadInt32()) |
| 52 | usercmd.Cmd = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 45 | usercmd.Size = int32(reader.TryReadInt32()) |
| 53 | usercmd.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 46 | usercmd.Data = classes.ParseUserCmdInfo(file, int(usercmd.Size)) |
| 54 | usercmd.Data = utils.ReadByteFromFile(file, usercmd.Size) | ||
| 55 | usercmdinfo = classes.UserCmdInfoInit(usercmd.Data, usercmd.Size) | ||
| 56 | fmt.Printf("[%d] UserCmd: %v\n", message.Tick, usercmdinfo) | ||
| 57 | return 5 | 47 | return 5 |
| 58 | case 0x06: // DataTables | 48 | case 0x06: // DataTables |
| 59 | var datatables DataTables | 49 | var datatables DataTables |
| 60 | //var stringtable classes.StringTable | 50 | datatables.Size = int32(reader.TryReadInt32()) |
| 61 | datatables.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 51 | reader.SkipBytes(int(datatables.Size)) |
| 62 | datatables.Data = utils.ReadByteFromFile(file, datatables.Size) | ||
| 63 | // stringtable = classes.StringTableInit(Data) | ||
| 64 | // fmt.Printf("[%d] DataTables: %v\n", utils.IntFromBytes(Size), stringtable) | ||
| 65 | return 6 | 52 | return 6 |
| 66 | case 0x07: // Stop | 53 | case 0x07: // Stop |
| 67 | fmt.Println("Stop - End of Demo") | ||
| 68 | return 7 | 54 | return 7 |
| 69 | case 0x08: // CustomData | 55 | case 0x08: // CustomData |
| 70 | var customdata CustomData | 56 | var customdata CustomData |
| 71 | customdata.Unknown = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 57 | customdata.Unknown = int32(reader.TryReadInt32()) |
| 72 | customdata.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 58 | customdata.Size = int32(reader.TryReadInt32()) |
| 73 | customdata.Data = utils.ReadByteFromFile(file, customdata.Size) | 59 | reader.SkipBytes(int(customdata.Size)) |
| 74 | return 8 | 60 | return 8 |
| 75 | case 0x09: // StringTables | 61 | case 0x09: // StringTables |
| 76 | var stringtables StringTables | 62 | var stringtables StringTables |
| 77 | stringtables.Size = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | 63 | stringtables.Size = int32(reader.TryReadInt32()) |
| 78 | stringtables.Data = utils.ReadByteFromFile(file, stringtables.Size) | 64 | reader.SkipBytes(int(stringtables.Size)) |
| 79 | return 9 | 65 | return 9 |
| 80 | default: | 66 | default: |
| 81 | return 0 | 67 | return 0 |
| 82 | } | 68 | } |
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | func ParseHeader(file *os.File) { | ||
| 87 | var header Header | ||
| 88 | header.DemoFileStamp = string(utils.ReadByteFromFile(file, 8)) | ||
| 89 | header.DemoProtocol = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | ||
| 90 | header.NetworkProtocol = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | ||
| 91 | header.ServerName = string(utils.ReadByteFromFile(file, 260)) | ||
| 92 | header.ClientName = string(utils.ReadByteFromFile(file, 260)) | ||
| 93 | header.MapName = string(utils.ReadByteFromFile(file, 260)) | ||
| 94 | header.GameDirectory = string(utils.ReadByteFromFile(file, 260)) | ||
| 95 | header.PlaybackTime = float32(utils.FloatFromBytes(utils.ReadByteFromFile(file, 4))) | ||
| 96 | header.PlaybackTicks = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | ||
| 97 | header.PlaybackFrames = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | ||
| 98 | header.SignOnLength = int32(utils.IntFromBytes(utils.ReadByteFromFile(file, 4))) | ||
| 99 | fmt.Println(header) | ||
| 100 | } | 69 | } |
diff --git a/messages/types.go b/messages/types.go index bb19444..a1d51a8 100644 --- a/messages/types.go +++ b/messages/types.go | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | package messages | 1 | package messages |
| 2 | 2 | ||
| 3 | import "github.com/bisaxa/demoparser/classes" | ||
| 4 | |||
| 3 | type Header struct { | 5 | type Header struct { |
| 4 | DemoFileStamp string | 6 | DemoFileStamp string |
| 5 | DemoProtocol int32 | 7 | DemoProtocol int32 |
| @@ -14,14 +16,8 @@ type Header struct { | |||
| 14 | SignOnLength int32 | 16 | SignOnLength int32 |
| 15 | } | 17 | } |
| 16 | 18 | ||
| 17 | type Message struct { | ||
| 18 | Type byte | ||
| 19 | Tick int | ||
| 20 | Slot byte | ||
| 21 | } | ||
| 22 | |||
| 23 | type Packet struct { | 19 | type Packet struct { |
| 24 | PacketInfo []byte | 20 | PacketInfo []classes.CmdInfo |
| 25 | InSequence int32 | 21 | InSequence int32 |
| 26 | OutSequence int32 | 22 | OutSequence int32 |
| 27 | Size int32 | 23 | Size int32 |
| @@ -36,7 +32,7 @@ type ConsoleCmd struct { | |||
| 36 | type UserCmd struct { | 32 | type UserCmd struct { |
| 37 | Cmd int32 | 33 | Cmd int32 |
| 38 | Size int32 | 34 | Size int32 |
| 39 | Data []byte | 35 | Data classes.UserCmdInfo |
| 40 | } | 36 | } |
| 41 | 37 | ||
| 42 | type DataTables struct { | 38 | type DataTables struct { |
diff --git a/utils/bitreader.go b/utils/bitreader.go deleted file mode 100644 index ec7454e..0000000 --- a/utils/bitreader.go +++ /dev/null | |||
| @@ -1,94 +0,0 @@ | |||
| 1 | package utils | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "math" | ||
| 6 | "strconv" | ||
| 7 | ) | ||
| 8 | |||
| 9 | func ReadButtonsDataFromInt32(input int32) []string { | ||
| 10 | buttonList := [32]string{ | ||
| 11 | "Attack", | ||
| 12 | "Jump", | ||
| 13 | "Duck", | ||
| 14 | "Forward", | ||
| 15 | "Back", | ||
| 16 | "Use", | ||
| 17 | "Cancel", | ||
| 18 | "Left", | ||
| 19 | "Right", | ||
| 20 | "MoveLeft", | ||
| 21 | "MoveRight", | ||
| 22 | "Attack2", | ||
| 23 | "Run", | ||
| 24 | "Reload", | ||
| 25 | "Alt1", | ||
| 26 | "Alt2", | ||
| 27 | "Score", | ||
| 28 | "Speed", | ||
| 29 | "Walk", | ||
| 30 | "Zoom", | ||
| 31 | "Weapon1", | ||
| 32 | "Weapon2", | ||
| 33 | "BullRush", | ||
| 34 | "Grenade1", | ||
| 35 | "Grenade2", | ||
| 36 | "LookSpin", | ||
| 37 | "CurrentAbility", | ||
| 38 | "PreviousAbility", | ||
| 39 | "Ability1", | ||
| 40 | "Ability2", | ||
| 41 | "Ability3", | ||
| 42 | "Ability4", | ||
| 43 | } | ||
| 44 | var buttons []string | ||
| 45 | if input == 0 { | ||
| 46 | buttons = append(buttons, buttonList[0]) | ||
| 47 | return buttons | ||
| 48 | } | ||
| 49 | for i := 1; i < 33; i++ { | ||
| 50 | if ReadBitState(input, i) { | ||
| 51 | buttons = append(buttons, buttonList[i]) | ||
| 52 | } | ||
| 53 | } | ||
| 54 | return buttons | ||
| 55 | } | ||
| 56 | |||
| 57 | func ReadBitState(input int32, index int) bool { | ||
| 58 | value := input & (1 << index) | ||
| 59 | return value > 0 | ||
| 60 | } | ||
| 61 | |||
| 62 | func ReadBitStateLSB(input byte, index int) (bool, error) { | ||
| 63 | if index < 0 && index > 7 { | ||
| 64 | return false, fmt.Errorf("IndexOutOfBounds for type byte") | ||
| 65 | } | ||
| 66 | value := input & (1 << index) | ||
| 67 | return (value > 0), nil | ||
| 68 | } | ||
| 69 | |||
| 70 | func Read32BitsAfterFirstBitInt32(input []byte, index int, step int) int32 { | ||
| 71 | binary := "" | ||
| 72 | binary += fmt.Sprintf("%08b", input[step])[8-index : 8] | ||
| 73 | binary += fmt.Sprintf("%08b", input[step-1]) | ||
| 74 | binary += fmt.Sprintf("%08b", input[step-2]) | ||
| 75 | binary += fmt.Sprintf("%08b", input[step-3]) | ||
| 76 | binary += fmt.Sprintf("%08b", input[step-4])[:8-index] | ||
| 77 | output, err := strconv.ParseInt(binary, 2, 32) | ||
| 78 | CheckError(err) | ||
| 79 | return int32(output) | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | func Read32BitsAfterFirstBitFloat32(input []byte, index int, step int) float32 { | ||
| 84 | binary := "" | ||
| 85 | binary += fmt.Sprintf("%08b", input[step])[8-index : 8] | ||
| 86 | binary += fmt.Sprintf("%08b", input[step-1]) | ||
| 87 | binary += fmt.Sprintf("%08b", input[step-2]) | ||
| 88 | binary += fmt.Sprintf("%08b", input[step-3]) | ||
| 89 | binary += fmt.Sprintf("%08b", input[step-4])[:8-index] | ||
| 90 | output, err := strconv.ParseUint(binary, 2, 32) | ||
| 91 | CheckError(err) | ||
| 92 | return math.Float32frombits(uint32(output)) | ||
| 93 | |||
| 94 | } | ||
diff --git a/utils/utils.go b/utils/utils.go index 5226e80..46b707c 100644 --- a/utils/utils.go +++ b/utils/utils.go | |||
| @@ -1,55 +1,15 @@ | |||
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | import ( | 3 | import "os" |
| 4 | "encoding/binary" | ||
| 5 | "log" | ||
| 6 | "math" | ||
| 7 | "math/bits" | ||
| 8 | "os" | ||
| 9 | "unsafe" | ||
| 10 | ) | ||
| 11 | 4 | ||
| 12 | func CheckError(e error) { | 5 | func CheckError(e error) { |
| 13 | if e != nil { | 6 | if e != nil { |
| 14 | log.Panic(e) | 7 | panic(e) |
| 15 | } | 8 | } |
| 16 | } | 9 | } |
| 17 | 10 | ||
| 18 | func ReverseByteArrayValues(byteArr []byte, size int) []byte { | ||
| 19 | arr := make([]byte, size) | ||
| 20 | for index, byteValue := range byteArr { | ||
| 21 | arr[index] = bits.Reverse8(byteValue) | ||
| 22 | } | ||
| 23 | return arr | ||
| 24 | } | ||
| 25 | |||
| 26 | func ReadByteFromFile(file *os.File, size int32) []byte { | 11 | func ReadByteFromFile(file *os.File, size int32) []byte { |
| 27 | tmp := make([]byte, size) | 12 | tmp := make([]byte, size) |
| 28 | file.Read(tmp) | 13 | file.Read(tmp) |
| 29 | return tmp | 14 | return tmp |
| 30 | } | 15 | } |
| 31 | |||
| 32 | func IntFromBytes(byteArr []byte) uint32 { | ||
| 33 | int := binary.LittleEndian.Uint32(byteArr) | ||
| 34 | return int | ||
| 35 | } | ||
| 36 | |||
| 37 | func FloatFromBytes(byteArr []byte) float32 { | ||
| 38 | bits := binary.LittleEndian.Uint32(byteArr) | ||
| 39 | float := math.Float32frombits(bits) | ||
| 40 | return float | ||
| 41 | } | ||
| 42 | |||
| 43 | func FloatArrFromBytes(byteArr []byte) []float32 { | ||
| 44 | if len(byteArr) == 0 { | ||
| 45 | return nil | ||
| 46 | } | ||
| 47 | |||
| 48 | l := len(byteArr) / 4 | ||
| 49 | ptr := unsafe.Pointer(&byteArr[0]) | ||
| 50 | // It is important to keep in mind that the Go garbage collector | ||
| 51 | // will not interact with this data, and that if src if freed, | ||
| 52 | // the behavior of any Go code using the slice is nondeterministic. | ||
| 53 | // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices | ||
| 54 | return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l] | ||
| 55 | } | ||