Skip to content

Commit 0c5b88b

Browse files
committed
Added debugging build tag
1 parent 5d92e4f commit 0c5b88b

File tree

6 files changed

+113
-11
lines changed

6 files changed

+113
-11
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ pushd test/cs-demos && git lfs pull && popd
8585
go test
8686
```
8787

88+
### Debugging
89+
90+
You can use the build tag `debugdemoinfocs` (i.e. `go test -tags debugdemoinfocs -v`) to print out debugging information - such as game events or unhandled demo-messages - during the parsing process.<br>
91+
Side-note: The tag isn't called `debug` to avoid naming conflicts with other libs (and underscores in tags don't work, apparently).
92+
93+
To change the default debugging behavior Go's `ldflags` paramter can be used. Example for additionally printing out the ingame-tick-numbers: `-ldflags '-X github.com/markus-wa/demoinfocs-golang.debugIngameTicks=YES'`
94+
95+
Check out `debug_on.go` for any other settings that can be changed.
96+
8897
### Generating protobuf code
8998

9099
Should you need to re-generate the protobuf generated code in the `msg` package, you will need the following tools:
@@ -93,7 +102,7 @@ Should you need to re-generate the protobuf generated code in the `msg` package,
93102

94103
- And `protoc-gen-gogofaster` from [gogoprotobuf](https://github.com/gogo/protobuf) to generate code for go.
95104

96-
go get -u github.com/gogo/protobuf/protoc-gen-gogofaster
105+
go get -u github.com/gogo/protobuf/protoc-gen-gogofaster
97106

98107
Make sure both are inside your `PATH` variable.
99108

debug_off.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//+build !debugdemoinfocs
2+
3+
// This file is just a bunch of NOPs for the release build, see debug_on.go for debugging stuff
4+
5+
package demoinfocs
6+
7+
import (
8+
msg "github.com/markus-wa/demoinfocs-golang/msg"
9+
)
10+
11+
const isDebug = false
12+
13+
func debugGameEvent(descriptor *msg.CSVCMsg_GameEventListDescriptorT, ge *msg.CSVCMsg_GameEvent) {
14+
// NOP
15+
}
16+
17+
func debugUnhandledMessage(cmd int, name string) {
18+
// NOP
19+
}
20+
21+
func debugIngameTick(tickNr int) {
22+
// NOP
23+
}

debug_on.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//+build debugdemoinfocs
2+
3+
// Functions to print out debug information if the build tag is enabled
4+
5+
package demoinfocs
6+
7+
import (
8+
"fmt"
9+
10+
msg "github.com/markus-wa/demoinfocs-golang/msg"
11+
)
12+
13+
const isDebug = true
14+
15+
const (
16+
yes = "YES"
17+
no = "NO"
18+
)
19+
20+
// Can be overridden via -ldflags '-X github.com/markus-wa/demoinfocs-golang.debugIngameTicks=YES'
21+
// Oh and btw we cant use bools for this, Go says 'cannot use -X with non-string symbol'
22+
var debugGameEvents = yes
23+
var debugUnhandledMessages = yes
24+
var debugIngameTicks = no
25+
26+
func debugGameEvent(d *msg.CSVCMsg_GameEventListDescriptorT, ge *msg.CSVCMsg_GameEvent) {
27+
if debugGameEvents == yes {
28+
// Map only the relevant data for each type
29+
data := make(map[string]interface{})
30+
for k, v := range mapGameEventData(d, ge) {
31+
switch v.Type {
32+
case 1:
33+
data[k] = v.ValString
34+
case 2:
35+
data[k] = v.ValFloat
36+
case 3:
37+
data[k] = v.ValLong
38+
case 4:
39+
data[k] = v.ValShort
40+
case 5:
41+
data[k] = v.ValByte
42+
case 6:
43+
data[k] = v.ValBool
44+
case 7:
45+
data[k] = v.ValUint64
46+
}
47+
}
48+
fmt.Println("GameEvent:", d.Name, "Data:", data)
49+
}
50+
}
51+
52+
func debugUnhandledMessage(cmd int, name string) {
53+
if debugUnhandledMessages == yes {
54+
fmt.Printf("UnhandledMessage: id=%d name=%s\n", cmd, name)
55+
}
56+
}
57+
58+
func debugIngameTick(tickNr int) {
59+
if debugIngameTicks == yes {
60+
fmt.Printf("IngameTick=%d\n", tickNr)
61+
}
62+
}

demopacket.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,22 @@ func (p *Parser) parsePacket() {
5353
m = new(msg.CSVCMsg_UserMessage)
5454

5555
default:
56-
if p.warn != nil {
56+
if p.warn != nil || isDebug {
5757
var name string
5858
if cmd < 8 || cmd >= 100 {
5959
name = msg.NET_Messages_name[int32(cmd)]
6060
} else {
6161
name = msg.SVC_Messages_name[int32(cmd)]
6262
}
63-
if name == "" {
64-
// Send a warning if the command is unknown
65-
// This might mean our proto files are out of date
66-
p.warn(fmt.Sprintf("Unknown message command %q", cmd))
63+
64+
debugUnhandledMessage(cmd, name)
65+
66+
if p.warn != nil {
67+
if name == "" {
68+
// Send a warning if the command is unknown
69+
// This might mean our proto files are out of date
70+
p.warn(fmt.Sprintf("Unknown message command %q", cmd))
71+
}
6772
}
6873
}
6974

packet_handlers.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ func (p *Parser) handleGameEventList(gel *msg.CSVCMsg_GameEventList) {
8888
p.setError(recoverFromUnexpectedEOF(recover()))
8989
}()
9090

91-
p.gehDescriptors = make(map[int32]*msg.CSVCMsg_GameEventListDescriptorT)
91+
p.gameEventDescs = make(map[int32]*msg.CSVCMsg_GameEventListDescriptorT)
9292
for _, d := range gel.GetDescriptors() {
93-
p.gehDescriptors[d.GetEventid()] = d
93+
p.gameEventDescs[d.GetEventid()] = d
9494
}
9595
}
9696

@@ -99,12 +99,14 @@ func (p *Parser) handleGameEvent(ge *msg.CSVCMsg_GameEvent) {
9999
p.setError(recoverFromUnexpectedEOF(recover()))
100100
}()
101101

102-
if p.gehDescriptors == nil {
102+
if p.gameEventDescs == nil {
103103
p.warn("Received GameEvent but event descriptors are missing")
104104
return
105105
}
106106

107-
d := p.gehDescriptors[ge.Eventid]
107+
d := p.gameEventDescs[ge.Eventid]
108+
109+
debugGameEvent(d, ge)
108110

109111
// Ignore events before players are connected to speed things up
110112
if len(p.connectedPlayers) == 0 && d.Name != "player_connect" {
@@ -726,4 +728,5 @@ type ingameTickNumber int
726728

727729
func (p *Parser) handleIngameTickNumber(n ingameTickNumber) {
728730
p.ingameTick = int(n)
731+
debugIngameTick(p.ingameTick)
729732
}

parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Parser struct {
4646
triggers map[int]*boundingBoxInformation
4747
instanceBaselines map[int][]byte
4848
preprocessedBaselines map[int]map[int]st.PropValue
49-
gehDescriptors map[int32]*msg.CSVCMsg_GameEventListDescriptorT
49+
gameEventDescs map[int32]*msg.CSVCMsg_GameEventListDescriptorT
5050
stringTables []*msg.CSVCMsg_CreateStringTable
5151
cancelChan chan struct{}
5252
warn WarnHandler

0 commit comments

Comments
 (0)