aboutsummaryrefslogtreecommitdiffstats
path: root/packet/command/modellist/modellist.go
blob: 2df83b028c04600e32f0c909458e96fdf91f092a (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
73
74
75
76
77
78
79
80
81
82
83
package modellist

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

type Command struct {
	ProtocolVersion uint32
	NumModels       byte
	Models          []string
	Index           byte
}

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

	buf.PutByte(protocol.SVCModelList)

	if cmd.ProtocolVersion >= 26 {
		buf.PutByte(cmd.NumModels)

		for i := 0; i < len(cmd.Models); i++ {
			buf.PutString(cmd.Models[i])
		}
		buf.PutByte(0x00)

		buf.PutByte(cmd.Index)
	} else {
		for i := 0; i < len(cmd.Models); i++ {
			buf.PutString(cmd.Models[i])
		}
		buf.PutByte(0x00)
	}

	return buf.Bytes()
}

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

	cmd.ProtocolVersion = ctx.GetProtocolVersion()

	if cmd.ProtocolVersion >= 26 {
		if cmd.NumModels, err = buf.ReadByte(); err != nil {
			return nil, err
		}

		for {
			var model string
			if model, err = buf.GetString(); err != nil {
				return nil, err
			}

			if model == "" {
				break
			}

			cmd.Models = append(cmd.Models, model)
		}

		if cmd.Index, err = buf.ReadByte(); err != nil {
			return nil, err
		}
	} else {
		for {
			var model string
			if model, err = buf.GetString(); err != nil {
				return nil, err
			}

			if model == "" {
				break
			}

			cmd.Models = append(cmd.Models, model)
		}
	}

	return &cmd, nil
}