aboutsummaryrefslogtreecommitdiffstats
path: root/packet/command/s2cchallenge/s2cchallenge.go
blob: 95cba707310947765ff5fa601b05925485cdd9de (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package s2cchallenge

import (
	"github.com/osm/quake/common/buffer"
	"github.com/osm/quake/common/context"
	"github.com/osm/quake/protocol"
	"github.com/osm/quake/protocol/fte"
	"github.com/osm/quake/protocol/mvd"
)

type Command struct {
	ChallengeID string
	Extensions  []*protocol.Extension
}

func (cmd *Command) Bytes() []byte {
	buf := buffer.New()

	buf.PutByte(byte(protocol.S2CChallenge))
	buf.PutString(cmd.ChallengeID)

	for _, ext := range cmd.Extensions {
		buf.PutBytes(ext.Bytes())
	}

	return buf.Bytes()
}

func Parse(ctx *context.Context, buf *buffer.Buffer) (*Command, error) {
	var err error
	var cmd Command

	if cmd.ChallengeID, err = buf.GetString(); err != nil {
		return nil, err
	}

	for buf.Off() < buf.Len() {
		version, err := buf.GetUint32()
		if err != nil {
			return nil, err
		}

		extensions, err := buf.GetUint32()
		if err != nil {
			return nil, err
		}

		if ctx.GetIsFTEEnabled() && version == fte.ProtocolVersion {
			ctx.SetFTEProtocolExtension(extensions)

			if extensions&fte.ExtensionFloatCoords != 0 {
				ctx.SetAngleSize(1)
				ctx.SetCoordSize(2)
			}
		}

		if ctx.GetIsFTE2Enabled() && version == fte.ProtocolVersion2 {
			ctx.SetFTE2ProtocolExtension(extensions)
		}

		if ctx.GetIsMVDEnabled() && version == mvd.ProtocolVersion {
			ctx.SetMVDProtocolExtension(extensions)
		}

		cmd.Extensions = append(cmd.Extensions, &protocol.Extension{
			Version:    version,
			Extensions: extensions,
		})
	}

	return &cmd, nil
}