You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.8 KiB
Go

8 years ago
package main
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/golang/snappy"
"github.com/jordanorelli/hyperstone/bit"
8 years ago
"github.com/jordanorelli/hyperstone/dota"
)
// message represents the top-level envelope in the dota replay format. Each
// datum is contained within a message envelope. Each message envelope is at
// most 1<<16 bytes.
8 years ago
type message struct {
cmd dota.EDemoCommands
tick int64
compressed bool
body []byte
}
func (m message) String() string {
if len(m.body) > 30 {
return fmt.Sprintf("{cmd: %v tick: %v compressed: %t size: %d body: %q...}", m.cmd, m.tick, m.compressed, len(m.body), m.body[:27])
8 years ago
}
return fmt.Sprintf("{cmd: %v tick: %v compressed: %t size: %d body: %q}", m.cmd, m.tick, m.compressed, len(m.body), m.body)
8 years ago
}
type entity struct {
t uint32
size uint32
body []byte
}
func (e entity) String() string {
if len(e.body) < 32 {
return fmt.Sprintf("{%v %v %x}", e.t, e.size, e.body)
}
return fmt.Sprintf("{%v %v %x...}", e.t, e.size, e.body[:32])
}
func (m *message) check(dump bool) error {
8 years ago
if m.cmd != dota.EDemoCommands_DEM_Packet {
return fmt.Errorf("wrong command type in openPacket: %v", m.cmd)
}
if m.compressed {
buf, err := snappy.Decode(nil, m.body)
if err != nil {
return wrap(err, "open packet error: could not decode body")
}
m.body = buf
m.compressed = false
8 years ago
}
packet := new(dota.CDemoPacket)
if err := proto.Unmarshal(m.body, packet); err != nil {
return wrap(err, "onPacket unable to unmarshal message body")
}
if dump {
br := bit.NewBytesReader(packet.GetData())
for {
t := br.ReadUBitVar()
s := br.ReadVarInt()
b := make([]byte, s)
br.Read(b)
if br.Err() != nil {
break
}
e := entity{t: uint32(t), size: uint32(s), body: b}
fmt.Printf("\t%v\n", e)
}
}
8 years ago
return nil
}