forked from l33ts/PUBG_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-getcmd-pcapfile.js
205 lines (189 loc) · 6.22 KB
/
test-getcmd-pcapfile.js
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
<<<<<<< HEAD
// Usage: LOGLEVEL=info node test-getcmd-pcapfile.js '/SOME/COOL/DIR/pubg-game1.pcap' | pino
const PacketByPacket = require('p-by-p')
const PacketParser = require('./backend/packetparser')
const utils = require('./backend/utils')
const logger = require('./backend/logger')
const CONSTS = require('./backend/constants')
const bunchStash = require('./backend/bunchstash')
const gamestate = require('./backend/gamestate')
const stringify = require("json-stringify-pretty-compact")
const parser = PacketParser()
const optionSummary = {}
function main () {
const args = process.argv.slice(2)
if (args.length !== 1 && args.length !== 2) {
console.log(`Usage: test-getcmd-pcapfile.js <pcapfile> <countToSendToGameState>`)
console.log(
` e.g.: test-getcmd-pcapfile.js /DIR/pubg-game1.pcap`
)
}
const pbyp = PacketByPacket(args[0])
const eventCountMap = new Map()
let bunchEvents = []
let errCount = 0
let packetNumber = 0
let allFinalEvents = []
const countType = (type) => {
const old = eventCountMap.get(type)
const newValue = old == null ? 1 : old + 1
eventCountMap.set(type, newValue)
}
pbyp.on('packet', packet => {
// packet contains { header, data }
packetNumber++
let result = null
try {
result = parser.parse(packet.data, packet.header.timestamp, packetNumber)
} catch (err) {
console.log('Parsing error:', err)
errCount++
}
if (result != null && result.type === CONSTS.EventTypes.UEBUNCHES && result.valid) {
bunchEvents.push(result)
}
})
pbyp.on('error', err => {
logger.error(err, 'Error happend')
})
pbyp.on('end', result => {
if (errCount) {
logger.error({ errCount }, 'During parse, got errors')
}
// got all the packet parsed into events, now feed them into stash
logger.warn({count: bunchEvents.length}, 'Got all the UEBunch events')
for (let evt of bunchEvents) {
const newEvts = bunchStash.feedEvent(evt)
if (newEvts) {
for (let newEvt of newEvts) {
if (newEvt.type === CONSTS.EventTypes.GAMESTART) {
gamestate.resetGameState()
}
countType(newEvt.type)
allFinalEvents.push(newEvt)
}
}
}
let printCount = 0
for (let evt of allFinalEvents) {
printCount++
printEvent(printCount, evt)
if (printCount > 200000) {
break
}
}
logger.warn({count: allFinalEvents.length}, 'Got all final events')
logger.warn('Count by result.type')
for (let pair of eventCountMap) {
console.log(pair)
}
if (args[1]) {
const countToSendToGameState = Math.min(parseInt(args[1]), allFinalEvents.length)
for (let i = 0; i < countToSendToGameState; i++) {
gamestate.processPUBGEvent(allFinalEvents[i])
}
logger.warn(`Feed ${countToSendToGameState} events into gamestate.`)
logger.warn(stringify(gamestate.dump()))
}
})
pbyp.resume()
}
function printEvent (count, evt) {
console.log(`[${utils.toLocalISOString(evt.time)} - ${evt.type.toString()} - guid:${evt.guid} -- ${JSON.stringify(evt.data)} --- (${count})`)
}
main()
=======
// Usage: LOGLEVEL=info node test-getcmd-pcapfile.js '/SOME/COOL/DIR/pubg-game1.pcap' | pino
const PacketByPacket = require('p-by-p')
const PacketParser = require('./backend/packetparser')
const utils = require('./backend/utils')
const logger = require('./backend/logger')
const CONSTS = require('./backend/constants')
const bunchStash = require('./backend/bunchstash')
const gamestate = require('./backend/gamestate')
const stringify = require("json-stringify-pretty-compact")
const parser = PacketParser()
const optionSummary = {}
function main () {
const args = process.argv.slice(2)
if (args.length !== 1 && args.length !== 2) {
console.log(`Usage: test-getcmd-pcapfile.js <pcapfile> <countToSendToGameState>`)
console.log(
` e.g.: test-getcmd-pcapfile.js /DIR/pubg-game1.pcap`
)
}
const pbyp = PacketByPacket(args[0])
const eventCountMap = new Map()
let bunchEvents = []
let errCount = 0
let packetNumber = 0
let allFinalEvents = []
const countType = (type) => {
const old = eventCountMap.get(type)
const newValue = old == null ? 1 : old + 1
eventCountMap.set(type, newValue)
}
pbyp.on('packet', packet => {
// packet contains { header, data }
packetNumber++
let result = null
try {
result = parser.parse(packet.data, packet.header.timestamp, packetNumber)
} catch (err) {
console.log('Parsing error:', err)
errCount++
}
if (result != null && result.type === CONSTS.EventTypes.UEBUNCHES && result.valid) {
bunchEvents.push(result)
}
})
pbyp.on('error', err => {
logger.error(err, 'Error happend')
})
pbyp.on('end', result => {
if (errCount) {
logger.error({ errCount }, 'During parse, got errors')
}
// got all the packet parsed into events, now feed them into stash
logger.warn({count: bunchEvents.length}, 'Got all the UEBunch events')
for (let evt of bunchEvents) {
const newEvts = bunchStash.feedEvent(evt)
if (newEvts) {
for (let newEvt of newEvts) {
if (newEvt.type === CONSTS.EventTypes.GAMESTART) {
gamestate.resetGameState()
}
countType(newEvt.type)
allFinalEvents.push(newEvt)
}
}
}
let printCount = 0
for (let evt of allFinalEvents) {
printCount++
printEvent(printCount, evt)
if (printCount > 200000) {
break
}
}
logger.warn({count: allFinalEvents.length}, 'Got all final events')
logger.warn('Count by result.type')
for (let pair of eventCountMap) {
console.log(pair)
}
if (args[1]) {
const countToSendToGameState = Math.min(parseInt(args[1]), allFinalEvents.length)
for (let i = 0; i < countToSendToGameState; i++) {
gamestate.processPUBGEvent(allFinalEvents[i])
}
logger.warn(`Feed ${countToSendToGameState} events into gamestate.`)
logger.warn(stringify(gamestate.dump()))
}
})
pbyp.resume()
}
function printEvent (count, evt) {
console.log(`[${utils.toLocalISOString(evt.time)} - ${evt.type.toString()} - guid:${evt.guid} -- ${JSON.stringify(evt.data)} --- (${count})`)
}
main()
>>>>>>> 提交文件