slowly getting more organized

master
Jordan Orelli 8 years ago
parent 19c7e820bf
commit 063bf79c03

@ -2,11 +2,8 @@ package main
import ( import (
"fmt" "fmt"
"io"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/jordanorelli/hyperstone/bit"
"github.com/jordanorelli/hyperstone/dota"
) )
// datagram represents the top-level envelope in the dota replay format. All // datagram represents the top-level envelope in the dota replay format. All
@ -24,42 +21,14 @@ func (g dataGram) String() string {
return fmt.Sprintf("{dataGram cmd: %v tick: %v size: %d body: %x}", g.cmd, g.tick, len(g.body), g.body) return fmt.Sprintf("{dataGram cmd: %v tick: %v size: %d body: %x}", g.cmd, g.tick, len(g.body), g.body)
} }
func (g *dataGram) check(dump bool) error { func (g *dataGram) Open(m *messageFactory) (proto.Message, error) {
if g.cmd != EDemoCommands_DEM_Packet { msg := m.BuildDatagram(g.cmd)
return fmt.Errorf("wrong command type in openPacket: %v", g.cmd) if msg == nil {
return nil, fmt.Errorf("skipped")
} }
err := proto.Unmarshal(g.body, msg)
packet := new(dota.CDemoPacket) if err != nil {
if err := proto.Unmarshal(g.body, packet); err != nil { return nil, err
return wrap(err, "onPacket unable to unmarshal message body")
}
br := bit.NewBytesReader(packet.GetData())
for {
t := entityType(br.ReadUBitVar())
s := br.ReadVarInt()
b := make([]byte, s)
br.Read(b)
switch err := br.Err(); err {
case nil:
break
case io.EOF:
return nil
default:
return err
}
if dump {
fmt.Printf("\t%v\n", entity{t: t, size: uint32(s), body: b})
}
e := messages.BuildEntity(t)
if e == nil {
fmt.Printf("\tno known entity for type id %d size: %d\n", int(t), len(b))
continue
}
err := proto.Unmarshal(b, e)
if err != nil {
fmt.Printf("entity unmarshal error: %v\n", err)
}
} }
return nil return msg, nil
} }

@ -4,10 +4,11 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"reflect"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/golang/snappy" "github.com/golang/snappy"
"github.com/jordanorelli/hyperstone/bit"
"github.com/jordanorelli/hyperstone/dota"
) )
type parser struct { type parser struct {
@ -43,27 +44,58 @@ func (p *parser) run() error {
if err != nil { if err != nil {
return wrap(err, "read datagram error in run loop") return wrap(err, "read datagram error in run loop")
} }
if p.dumpDatagrams { p.handleDataGram(gram)
fmt.Println(gram) }
} }
if len(gram.body) == 0 { func (p *parser) handleDataGram(d *dataGram) error {
continue if p.dumpDatagrams {
} fmt.Println(d)
}
switch gram.cmd { if len(d.body) == 0 {
case EDemoCommands_DEM_Packet: return nil
if err := gram.check(p.dumpPackets); err != nil { }
fmt.Printf("error: %v\n", err)
} msg, err := d.Open(&messages)
if err != nil {
fmt.Printf("datagram open error: %v\n", err)
return nil
}
switch v := msg.(type) {
case *dota.CDemoPacket:
p.handleDemoPacket(v)
}
return nil
}
func (p *parser) handleDemoPacket(packet *dota.CDemoPacket) error {
br := bit.NewBytesReader(packet.GetData())
for {
t := entityType(br.ReadUBitVar())
s := br.ReadVarInt()
b := make([]byte, s)
br.Read(b)
switch err := br.Err(); err {
case nil:
break
case io.EOF:
return nil
default: default:
m := messages.BuildDatagram(gram.cmd) return err
if m != nil { }
err := proto.Unmarshal(gram.body, m) if p.dumpPackets {
if err != nil { fmt.Printf("\t%v\n", entity{t: t, size: uint32(s), body: b})
fmt.Printf("cmd unmarshal error unpacking data of length %d with cmd type %s into message type %v: %v\n", len(gram.body), gram.cmd, reflect.TypeOf(m), err) }
} e := messages.BuildEntity(t)
} if e == nil {
fmt.Printf("\tno known entity for type id %d size: %d\n", int(t), len(b))
continue
}
err := proto.Unmarshal(b, e)
if err != nil {
fmt.Printf("entity unmarshal error: %v\n", err)
} }
} }
} }

Loading…
Cancel
Save