forked from markus-wa/demoinfocs-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.go
395 lines (314 loc) · 10.2 KB
/
parsing.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package demoinfocs
import (
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/gogo/protobuf/proto"
"github.com/markus-wa/go-unassert"
"github.com/markus-wa/demoinfocs-golang/common"
"github.com/markus-wa/demoinfocs-golang/events"
"github.com/markus-wa/demoinfocs-golang/msg"
)
const maxOsPath = 260
const (
playerWeaponPrefix = "m_hMyWeapons."
playerWeaponPrePrefix = "bcc_nonlocaldata."
gameRulesPrefix = "cs_gamerules_data"
)
// Parsing errors
var (
// ErrCancelled signals that parsing was cancelled via Parser.Cancel()
ErrCancelled = errors.New("parsing was cancelled before it finished (ErrCancelled)")
// ErrUnexpectedEndOfDemo signals that the demo is incomplete / corrupt -
// these demos may still be useful, check how far the parser got.
ErrUnexpectedEndOfDemo = errors.New("demo stream ended unexpectedly (ErrUnexpectedEndOfDemo)")
// ErrInvalidFileType signals that the input isn't a valid CS:GO demo.
ErrInvalidFileType = errors.New("invalid File-Type; expecting HL2DEMO in the first 8 bytes (ErrInvalidFileType)")
)
// ParseHeader attempts to parse the header of the demo and returns it.
// If not done manually this will be called by Parser.ParseNextFrame() or Parser.ParseToEnd().
//
// Returns ErrInvalidFileType if the filestamp (first 8 bytes) doesn't match HL2DEMO.
func (p *Parser) ParseHeader() (common.DemoHeader, error) {
var h common.DemoHeader
h.Filestamp = p.bitReader.ReadCString(8)
h.Protocol = p.bitReader.ReadSignedInt(32)
h.NetworkProtocol = p.bitReader.ReadSignedInt(32)
h.ServerName = p.bitReader.ReadCString(maxOsPath)
h.ClientName = p.bitReader.ReadCString(maxOsPath)
h.MapName = p.bitReader.ReadCString(maxOsPath)
h.GameDirectory = p.bitReader.ReadCString(maxOsPath)
h.PlaybackTime = time.Duration(p.bitReader.ReadFloat() * float32(time.Second))
h.PlaybackTicks = p.bitReader.ReadSignedInt(32)
h.PlaybackFrames = p.bitReader.ReadSignedInt(32)
h.SignonLength = p.bitReader.ReadSignedInt(32)
if h.Filestamp != "HL2DEMO" {
return h, ErrInvalidFileType
}
// Initialize queue if the buffer size wasn't specified, the amount of ticks
// seems to be a good indicator of how many events we'll get
if p.msgQueue == nil {
p.initMsgQueue(h.PlaybackTicks)
}
p.header = &h
return h, nil
}
// ParseToEnd attempts to parse the demo until the end.
// Aborts and returns ErrCancelled if Cancel() is called before the end.
//
// See also: ParseNextFrame() for other possible errors.
func (p *Parser) ParseToEnd() (err error) {
defer func() {
// Make sure all the messages of the demo are handled
p.msgDispatcher.SyncAllQueues()
// Close msgQueue
if p.msgQueue != nil {
close(p.msgQueue)
}
if err == nil {
err = recoverFromUnexpectedEOF(recover())
}
}()
if p.header == nil {
_, err = p.ParseHeader()
if err != nil {
return
}
}
for {
select {
case <-p.cancelChan:
return ErrCancelled
default:
if !p.parseFrame() {
return p.error()
}
}
if err = p.error(); err != nil {
return
}
}
}
func recoverFromUnexpectedEOF(r interface{}) error {
if r != nil {
if r == io.ErrUnexpectedEOF || r == io.EOF {
return ErrUnexpectedEndOfDemo
}
panic(r)
}
return nil
}
// Cancel aborts ParseToEnd().
// All information that was already read up to this point may still be used (and new events may still be sent out).
func (p *Parser) Cancel() {
p.cancelChan <- struct{}{}
}
/*
ParseNextFrame attempts to parse the next frame / demo-tick (not ingame tick).
Returns true unless the demo command 'stop' or an error was encountered.
May return ErrUnexpectedEndOfDemo for incomplete / corrupt demos.
May panic if the demo is corrupt in some way.
See also: ParseToEnd() for parsing the complete demo in one go (faster).
*/
func (p *Parser) ParseNextFrame() (moreFrames bool, err error) {
defer func() {
// Make sure all the messages of the frame are handled
p.msgDispatcher.SyncAllQueues()
// Close msgQueue (only if we are done)
if p.msgQueue != nil && !moreFrames {
close(p.msgQueue)
}
if err == nil {
err = recoverFromUnexpectedEOF(recover())
}
}()
if p.header == nil {
_, err = p.ParseHeader()
if err != nil {
return
}
}
moreFrames = p.parseFrame()
return moreFrames, p.error()
}
// Demo commands as documented at https://developer.valvesoftware.com/wiki/DEM_Format
type demoCommand byte
const (
dcSignon demoCommand = 1
dcPacket demoCommand = 2
dcSynctick demoCommand = 3
dcConsoleCommand demoCommand = 4
dcUserCommand demoCommand = 5
dcDataTables demoCommand = 6
dcStop demoCommand = 7
dcCustomData demoCommand = 8
dcStringTables demoCommand = 9
)
func (p *Parser) parseFrame() bool {
cmd := demoCommand(p.bitReader.ReadSingleByte())
// Send ingame tick number update
p.msgQueue <- ingameTickNumber(p.bitReader.ReadSignedInt(32))
// Skip 'player slot'
p.bitReader.Skip(8)
debugDemoCommand(cmd)
switch cmd {
case dcSynctick:
// Ignore
case dcStop:
return false
case dcConsoleCommand:
// Skip
p.bitReader.Skip(p.bitReader.ReadSignedInt(32) << 3)
case dcDataTables:
p.msgDispatcher.SyncAllQueues()
p.bitReader.BeginChunk(p.bitReader.ReadSignedInt(32) << 3)
p.stParser.ParsePacket(p.bitReader)
p.bitReader.EndChunk()
debugAllServerClasses(p.ServerClasses())
p.mapEquipment()
p.bindEntities()
p.eventDispatcher.Dispatch(events.DataTablesParsed{})
case dcStringTables:
p.msgDispatcher.SyncAllQueues()
p.parseStringTables()
case dcUserCommand:
// Skip
p.bitReader.Skip(32)
p.bitReader.Skip(p.bitReader.ReadSignedInt(32) << 3)
case dcSignon:
fallthrough
case dcPacket:
p.parsePacket()
case dcCustomData:
// Might as well panic since we'll be way off if we dont skip the whole thing
panic("Found CustomData but not handled")
default:
panic(fmt.Sprintf("I haven't programmed that pathway yet (command %v unknown)", cmd))
}
// Queue up some post processing
p.msgQueue <- frameParsedToken
return true
}
var byteSlicePool = sync.Pool{
New: func() interface{} {
s := make([]byte, 0, 256)
return &s
},
}
var defaultNetMessageCreators = map[int]NetMessageCreator{
// We could pool CSVCMsg_PacketEntities as they take up A LOT of the allocations
// but unless we're on a system that's doing a lot of concurrent parsing there isn't really a point
// as handling packets is a lot slower than creating them and we can't pool until they are handled.
int(msg.SVC_Messages_svc_PacketEntities): func() proto.Message { return new(msg.CSVCMsg_PacketEntities) },
int(msg.SVC_Messages_svc_GameEventList): func() proto.Message { return new(msg.CSVCMsg_GameEventList) },
int(msg.SVC_Messages_svc_GameEvent): func() proto.Message { return new(msg.CSVCMsg_GameEvent) },
int(msg.SVC_Messages_svc_CreateStringTable): func() proto.Message { return new(msg.CSVCMsg_CreateStringTable) },
int(msg.SVC_Messages_svc_UpdateStringTable): func() proto.Message { return new(msg.CSVCMsg_UpdateStringTable) },
int(msg.SVC_Messages_svc_UserMessage): func() proto.Message { return new(msg.CSVCMsg_UserMessage) },
int(msg.NET_Messages_net_SetConVar): func() proto.Message { return new(msg.CNETMsg_SetConVar) },
}
func (p *Parser) parsePacket() {
// Booooring
// 152 bytes CommandInfo, 4 bytes SeqNrIn, 4 bytes SeqNrOut
// See at the bottom of the file what the CommandInfo would contain if you are interested.
p.bitReader.Skip((152 + 4 + 4) << 3)
// Here we go
p.bitReader.BeginChunk(p.bitReader.ReadSignedInt(32) << 3)
for !p.bitReader.ChunkFinished() {
cmd := int(p.bitReader.ReadVarInt32())
size := int(p.bitReader.ReadVarInt32())
p.bitReader.BeginChunk(size << 3)
msgCreator := defaultNetMessageCreators[cmd]
if msgCreator == nil {
var msgName string
if cmd < 8 || cmd >= 100 {
msgName = msg.NET_Messages_name[int32(cmd)]
} else {
msgName = msg.SVC_Messages_name[int32(cmd)]
}
if msgName == "" {
// Send a warning if the command is unknown
// This might mean our proto files are out of date
p.eventDispatcher.Dispatch(events.ParserWarn{Message: fmt.Sprintf("unknown message command %q", cmd)})
unassert.Error("unknown message command %q", cmd)
}
// Handle additional net-messages as defined by the user
msgCreator = p.additionalNetMessageCreators[cmd]
if msgCreator == nil {
debugUnhandledMessage(cmd, msgName)
// On to the next one
p.bitReader.EndChunk()
continue
}
}
b := byteSlicePool.Get().(*[]byte)
p.bitReader.ReadBytesInto(b, size)
m := msgCreator()
if proto.Unmarshal(*b, m) != nil {
// TODO: Don't crash here, happens with demos that work in gotv
panic(fmt.Sprintf("Failed to unmarshal cmd %d", cmd))
}
p.msgQueue <- m
// Reset length to 0 and pool
*b = (*b)[:0]
byteSlicePool.Put(b)
p.bitReader.EndChunk()
}
p.bitReader.EndChunk()
}
type frameParsedTokenType struct{}
var frameParsedToken = new(frameParsedTokenType)
func (p *Parser) handleFrameParsed(*frameParsedTokenType) {
// PlayerFlashed events need to be dispatched at the end of the tick
// because Player.FlashDuration is updated after the game-events are parsed.
for _, eventHandler := range p.delayedEventHandlers {
eventHandler()
}
p.delayedEventHandlers = p.delayedEventHandlers[:0]
p.currentFrame++
p.eventDispatcher.Dispatch(events.TickDone{})
p.eventDispatcher.Dispatch(events.FrameDone{})
}
/*
Format of 'CommandInfos' - I honestly have no clue what they are good for.
This data is skipped in Parser.parsePacket().
If you find a use for this please let me know!
Here is all i know:
CommandInfo [152 bytes]
- [2]Split
Split [76 bytes]
- flags [4 bytes]
- viewOrigin [12 bytes]
- viewAngles [12 bytes]
- localViewAngles [12 bytes]
- viewOrigin2 [12 bytes]
- viewAngles2 [12 bytes]
- localViewAngles2 [12 bytes]
Origin [12 bytes]
- X [4 bytes]
- Y [4 bytes]
- Z [4 bytes]
Angle [12 bytes]
- X [4 bytes]
- Y [4 bytes]
- Z [4 bytes]
They are parsed in the following order:
split1.flags
split1.viewOrigin.x
split1.viewOrigin.y
split1.viewOrigin.z
split1.viewAngles.x
split1.viewAngles.y
split1.viewAngles.z
split1.localViewAngles.x
split1.localViewAngles.y
split1.localViewAngles.z
split1.viewOrigin2...
split1.viewAngles2...
split1.localViewAngles2...
split2.flags
...
Or just check this file's history for an example on how to parse them
*/