aboutsummaryrefslogtreecommitdiff
path: root/pkg/packets/headers.go
blob: 543476b8bde148f2d85190c9befca6d5b1138dfc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package packets

import (
	"fmt"

	"github.com/pektezol/bitreader"
)

type Headers struct {
	DemoFileStamp   string
	DemoProtocol    int32
	NetworkProtocol int32
	ServerName      string
	ClientName      string
	MapName         string
	GameDirectory   string
	PlaybackTime    float32
	PlaybackTicks   int32
	PlaybackFrames  int32
	SignOnLength    int32
}

func ParseHeaders(reader *bitreader.ReaderType) Headers {
	headers := Headers{
		DemoFileStamp:   reader.TryReadString(),
		DemoProtocol:    int32(reader.TryReadInt32()),
		NetworkProtocol: int32(reader.TryReadInt32()),
		ServerName:      reader.TryReadStringLen(260),
		ClientName:      reader.TryReadStringLen(260),
		MapName:         reader.TryReadStringLen(260),
		GameDirectory:   reader.TryReadStringLen(260),
		PlaybackTime:    reader.TryReadFloat32(),
		PlaybackTicks:   int32(reader.TryReadInt32()),
		PlaybackFrames:  int32(reader.TryReadInt32()),
		SignOnLength:    int32(reader.TryReadInt32()),
	}
	if headers.DemoFileStamp != "HL2DEMO" {
		panic("invalid demo file stamp")
	}
	if headers.DemoProtocol != 4 {
		panic("this parser only supports demos from new engine")
	}
	if headers.NetworkProtocol != 2001 {
		panic("this parser only supports demos from portal 2")
	}
	fmt.Printf("Headers: %+v\n", headers)
	return headers
}