This repository has been archived by the owner on May 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
outer_parser.go
164 lines (151 loc) · 4.26 KB
/
outer_parser.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
package yasha
import (
"bytes"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/dotabuff/yasha/dota"
)
type OuterParser struct {
reader *BytesReader
Sequence int64
Items map[int64]*OuterParserItem
}
func OuterParserFromFile(path string) *OuterParser {
if strings.HasSuffix(path, ".dem.bz2") {
return NewOuterParser(ReadBz2File(path))
} else if strings.HasSuffix(path, ".dem") {
return NewOuterParser(ReadFile(path))
} else {
panic("expected path to .dem or .dem.bz2 instead of " + path)
}
}
func NewOuterParser(data []byte) *OuterParser {
if len(data) < headerLength {
panic("File too small.")
}
magic := ReadStringZ(data, 0)
if magic != headerMagic {
panic("demofilestamp doesn't match, was: " + spew.Sdump(magic))
}
totalLength := len(data) - headerLength
if totalLength < 1 {
panic("couldn't open file")
}
buffer := data[headerLength:(headerLength + totalLength)]
return &OuterParser{reader: NewBytesReader(buffer)}
}
func (p *OuterParser) readEDemoCommands() (dota.EDemoCommands, bool) {
command := dota.EDemoCommands(p.reader.ReadVarInt32())
compressed := (command & dota.EDemoCommands_DEM_IsCompressed) == dota.EDemoCommands_DEM_IsCompressed
command = command & ^dota.EDemoCommands_DEM_IsCompressed
return command, compressed
}
func (p *OuterParser) Analyze(callback func(*OuterParserBaseItem)) {
p.Sequence = 1
command, compressed := p.readEDemoCommands()
if command == dota.EDemoCommands_DEM_Error {
panic(command)
}
for p.reader.CanRead() {
tick := int(p.reader.ReadVarInt32())
length := int(p.reader.ReadVarInt32())
obj, err := p.AsBaseEvent(command.String())
if err == nil {
item := &OuterParserItem{
Sequence: p.Sequence,
Object: obj,
Tick: tick,
}
p.Sequence++
if compressed {
item.Data = SnappyUncompress(p.reader.Read(length))
} else {
item.Data = p.reader.Read(length)
}
switch o := obj.(type) {
case *SignonPacket:
full := &dota.CDemoPacket{}
ProtoUnmarshal(item.Data, full)
p.AnalyzePacket(callback, dota.EDemoCommands_DEM_SignonPacket, tick, full.GetData())
case *dota.CDemoPacket:
ProtoUnmarshal(item.Data, o)
p.AnalyzePacket(callback, dota.EDemoCommands_DEM_Packet, tick, o.GetData())
case *dota.CDemoFullPacket:
ProtoUnmarshal(item.Data, o)
item.From = dota.EDemoCommands_DEM_FullPacket
item.Data = nil
item.Object = o.GetStringTable()
callback(parseOne(item))
p.AnalyzePacket(callback, dota.EDemoCommands_DEM_FullPacket, tick, o.GetPacket().GetData())
case *dota.CDemoSendTables:
ProtoUnmarshal(item.Data, o)
p.AnalyzePacket(callback, dota.EDemoCommands_DEM_SendTables, tick, o.GetData())
default:
callback(parseOne(item))
}
} else {
p.reader.Skip(length)
}
command, compressed = p.readEDemoCommands()
if command == dota.EDemoCommands_DEM_Error {
panic(command)
}
}
}
func (p *OuterParser) AnalyzePacket(callback func(*OuterParserBaseItem), fromEvent dota.EDemoCommands, tick int, data []byte) {
reader := NewBytesReader(data)
for reader.CanRead() {
iType := int(reader.ReadVarInt32())
length := int(reader.ReadVarInt32())
obj, err := p.AsBaseEventNETSVC(iType)
if err != nil {
spew.Println(err)
reader.Skip(length)
} else {
item := &OuterParserItem{
Sequence: p.Sequence,
From: fromEvent,
Object: obj,
Tick: tick,
Data: reader.Read(length),
}
p.Sequence++
switch obj.(type) {
case *dota.CSVCMsg_UserMessage:
message := &dota.CSVCMsg_UserMessage{}
ProtoUnmarshal(item.Data, message)
um, err := p.AsBaseEventBUMDUM(int(message.GetMsgType()))
if err == nil {
item.Object = um
item.Data = message.GetMsgData()
callback(parseOne(item))
}
default:
callback(parseOne(item))
}
}
}
}
func parseOne(item *OuterParserItem) *OuterParserBaseItem {
err := ProtoUnmarshal(item.Data, item.Object)
if err != nil {
spew.Println("parseOne()")
spew.Dump(item)
panic(err)
return &OuterParserBaseItem{}
}
item.Data = nil
return &OuterParserBaseItem{
Sequence: item.Sequence,
Tick: item.Tick,
From: item.From,
Object: item.Object,
}
}
func ReadStringZ(datas []byte, offset int) string {
idx := bytes.IndexByte(datas[offset:], '\000')
if idx < 0 {
return ""
}
return string(datas[offset:idx])
}