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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
package mvd
import (
"errors"
"github.com/osm/quake/common/buffer"
"github.com/osm/quake/common/context"
"github.com/osm/quake/protocol"
"github.com/osm/quake/protocol/mvd"
)
var ErrUnknownType = errors.New("unknown type")
type Demo struct {
Data []Data
}
type Data struct {
Target uint32
Timestamp byte
Command byte
Cmd *Cmd
Read *Read
Set *Set
Multiple *Multiple
}
func (d *Demo) Bytes() []byte {
buf := buffer.New()
for i := 0; i < len(d.Data); i++ {
buf.PutBytes(d.Data[i].Bytes())
}
return buf.Bytes()
}
func (d *Data) Bytes() []byte {
buf := buffer.New()
buf.PutByte(d.Timestamp)
buf.PutByte(d.Command)
switch d.Command & 0x7 {
case mvd.DemoMultiple:
buf.PutBytes(d.Multiple.Bytes())
fallthrough
case mvd.DemoStats:
fallthrough
case mvd.DemoSingle:
fallthrough
case mvd.DemoAll:
fallthrough
case protocol.DemoRead:
buf.PutBytes(d.Read.Bytes())
case protocol.DemoSet:
buf.PutBytes(d.Set.Bytes())
case protocol.DemoCmd:
buf.PutBytes(d.Cmd.Bytes())
}
return buf.Bytes()
}
func Parse(ctx *context.Context, data []byte) (*Demo, error) {
var err error
var cmd Demo
buf := buffer.New(buffer.WithData(data))
ctx.SetIsMVD(true)
for buf.Off() < buf.Len() {
var data Data
process:
if data.Timestamp, err = buf.ReadByte(); err != nil {
return nil, err
}
if data.Command, err = buf.ReadByte(); err != nil {
return nil, err
}
switch data.Command & 0x7 {
case mvd.DemoMultiple:
if data.Multiple, err = parseMultiple(ctx, buf); err != nil {
return nil, err
}
data.Target = data.Multiple.LastTo
if data.Multiple.IsHiddenPacket {
cmd.Data = append(cmd.Data, data)
if buf.Off() == buf.Len() {
goto end
}
goto process
}
fallthrough
case mvd.DemoStats:
fallthrough
case mvd.DemoSingle:
// Target determines which client the data is intended
// to reach and can be used in conjunction with the
// updateuserinfo to determine who the client is.
data.Target = uint32(data.Command >> 3)
fallthrough
case mvd.DemoAll:
fallthrough
case protocol.DemoRead:
if data.Read, err = parseRead(ctx, buf); err != nil {
return nil, err
}
case protocol.DemoSet:
if data.Set, err = parseSet(ctx, buf); err != nil {
return nil, err
}
case protocol.DemoCmd:
if data.Cmd, err = parseCmd(ctx, buf); err != nil {
return nil, err
}
default:
return nil, ErrUnknownType
}
cmd.Data = append(cmd.Data, data)
}
end:
return &cmd, nil
}
|