@@ -251,6 +251,18 @@ var byteSlicePool = sync.Pool{
251251 },
252252}
253253
254+ var defaultNetMessageCreators = map [int ]NetMessageCreator {
255+ // We could pool CSVCMsg_PacketEntities as they take up A LOT of the allocations
256+ // but unless we're on a system that's doing a lot of concurrent parsing there isn't really a point
257+ // as handling packets is a lot slower than creating them and we can't pool until they are handled.
258+ int (msg .SVC_Messages_svc_PacketEntities ): func () proto.Message { return new (msg.CSVCMsg_PacketEntities ) },
259+ int (msg .SVC_Messages_svc_GameEventList ): func () proto.Message { return new (msg.CSVCMsg_GameEventList ) },
260+ int (msg .SVC_Messages_svc_GameEvent ): func () proto.Message { return new (msg.CSVCMsg_GameEvent ) },
261+ int (msg .SVC_Messages_svc_CreateStringTable ): func () proto.Message { return new (msg.CSVCMsg_CreateStringTable ) },
262+ int (msg .SVC_Messages_svc_UpdateStringTable ): func () proto.Message { return new (msg.CSVCMsg_UpdateStringTable ) },
263+ int (msg .SVC_Messages_svc_UserMessage ): func () proto.Message { return new (msg.CSVCMsg_UserMessage ) },
264+ }
265+
254266func (p * Parser ) parsePacket () {
255267 // Booooring
256268 // 152 bytes CommandInfo, 4 bytes SeqNrIn, 4 bytes SeqNrOut
@@ -259,65 +271,44 @@ func (p *Parser) parsePacket() {
259271
260272 // Here we go
261273 p .bitReader .BeginChunk (p .bitReader .ReadSignedInt (32 ) << 3 )
274+
262275 for ! p .bitReader .ChunkFinished () {
263276 cmd := int (p .bitReader .ReadVarInt32 ())
264277 size := int (p .bitReader .ReadVarInt32 ())
265278
266279 p .bitReader .BeginChunk (size << 3 )
267- var m proto.Message
268- switch cmd {
269- case int (msg .SVC_Messages_svc_PacketEntities ):
270- // We could pool CSVCMsg_PacketEntities as they take up A LOT of the allocations
271- // but unless we're on a system that's doing a lot of concurrent parsing there isn't really a point
272- // as handling packets is a lot slower than creating them and we can't pool until they are handled.
273- m = new (msg.CSVCMsg_PacketEntities )
274-
275- case int (msg .SVC_Messages_svc_GameEventList ):
276- m = new (msg.CSVCMsg_GameEventList )
277-
278- case int (msg .SVC_Messages_svc_GameEvent ):
279- m = new (msg.CSVCMsg_GameEvent )
280-
281- case int (msg .SVC_Messages_svc_CreateStringTable ):
282- m = new (msg.CSVCMsg_CreateStringTable )
283-
284- case int (msg .SVC_Messages_svc_UpdateStringTable ):
285- m = new (msg.CSVCMsg_UpdateStringTable )
286280
287- case int (msg .SVC_Messages_svc_UserMessage ):
288- m = new (msg.CSVCMsg_UserMessage )
281+ msgCreator := defaultNetMessageCreators [cmd ]
289282
290- default :
291- var name string
283+ if msgCreator == nil {
284+ var msgName string
292285 if cmd < 8 || cmd >= 100 {
293- name = msg .NET_Messages_name [int32 (cmd )]
286+ msgName = msg .NET_Messages_name [int32 (cmd )]
294287 } else {
295- name = msg .SVC_Messages_name [int32 (cmd )]
288+ msgName = msg .SVC_Messages_name [int32 (cmd )]
296289 }
297290
298- debugUnhandledMessage (cmd , name )
299-
300- if name != "" {
301- // Handle additional net-messages as defined by the user
302- creator := p .additionalNetMessageCreators [cmd ]
303- if creator != nil {
304- m = creator ()
305- break
306- }
307- } else {
291+ if msgName == "" {
308292 // Send a warning if the command is unknown
309293 // This might mean our proto files are out of date
310294 p .eventDispatcher .Dispatch (events.ParserWarn {Message : fmt .Sprintf ("Unknown message command %q" , cmd )})
311295 }
312296
313- // On to the next one
314- p .bitReader .EndChunk ()
315- continue
297+ // Handle additional net-messages as defined by the user
298+ msgCreator = p .additionalNetMessageCreators [cmd ]
299+ if msgCreator == nil {
300+ debugUnhandledMessage (cmd , msgName )
301+
302+ // On to the next one
303+ p .bitReader .EndChunk ()
304+ continue
305+ }
316306 }
317307
318308 b := byteSlicePool .Get ().(* []byte )
319309 p .bitReader .ReadBytesInto (b , size )
320310
311+ m := msgCreator ()
321312 if proto .Unmarshal (* b , m ) != nil {
322313 // TODO: Don't crash here, happens with demos that work in gotv
323314 panic (fmt .Sprintf ("Failed to unmarshal cmd %d" , cmd ))
@@ -330,6 +321,7 @@ func (p *Parser) parsePacket() {
330321
331322 p .bitReader .EndChunk ()
332323 }
324+
333325 p .bitReader .EndChunk ()
334326}
335327
0 commit comments