aboutsummaryrefslogtreecommitdiff
path: root/pkg/packets/headers.go
blob: 6c5ab10a58ba222ca5a435296022165e48a5c35d (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
49
50
51
52
53
54
55
56
57
package packets

import (
	"github.com/pektezol/bitreader"
	"github.com/pektezol/sdp.go/pkg/writer"
)

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.Reader) Headers {
	headers := Headers{
		DemoFileStamp:   reader.TryReadString(),
		DemoProtocol:    int32(reader.TryReadSInt32()),
		NetworkProtocol: int32(reader.TryReadSInt32()),
		ServerName:      reader.TryReadStringLength(260),
		ClientName:      reader.TryReadStringLength(260),
		MapName:         reader.TryReadStringLength(260),
		GameDirectory:   reader.TryReadStringLength(260),
		PlaybackTime:    reader.TryReadFloat32(),
		PlaybackTicks:   int32(reader.TryReadSInt32()),
		PlaybackFrames:  int32(reader.TryReadSInt32()),
		SignOnLength:    int32(reader.TryReadSInt32()),
	}
	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")
	}
	writer.AppendLine("\nDemo File Stamp: %s", headers.DemoFileStamp)
	writer.AppendLine("Demo Protocol: %d", headers.DemoProtocol)
	writer.AppendLine("Network Protocol: %d", headers.NetworkProtocol)
	writer.AppendLine("Server Name: %s", headers.ServerName)
	writer.AppendLine("Client Name: %s", headers.ClientName)
	writer.AppendLine("Map Name: %s", headers.MapName)
	writer.AppendLine("Game Directory: %s", headers.GameDirectory)
	writer.AppendLine("Playback Time: %f", headers.PlaybackTime)
	writer.AppendLine("Playback Ticks: %d", headers.PlaybackTicks)
	writer.AppendLine("Playback Frames: %d", headers.PlaybackFrames)
	writer.AppendLine("Sign On Length: %d\n", headers.SignOnLength)
	return headers
}