generated from PrismarineJS/prismarine-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdumpPackets.js
162 lines (144 loc) · 4.87 KB
/
dumpPackets.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
#!/usr/bin/env node
const minecraftWrap = require('minecraft-wrap')
const fs = require('fs')
const fsP = fs.promises
const path = require('path')
const util = require('util')
const { once } = require('events')
const MineflayerLog = require('../lib/mineflayer-log')
const { makeMarkdown, parsePacketCounter } = require('../lib/stats-helper')
const generatePackets = require('../lib/generate-packets')
const process = require('process')
const argv = require('yargs/yargs')(process.argv.slice(2))
.usage('Usage: $0 [options]')
.version(false) // disable default version command
.option('version', {
alias: ['v', 'ver'],
description: 'The mc version to dump',
string: true,
demandOption: true
})
.option('outputFolder', {
alias: ['o', 'output'],
description: 'Where to save the dumped packets',
default: 'output',
string: true,
coerce: path.resolve
})
.option('h', {
alias: 'help',
description: 'Display help message'
})
.option('dryrun', {
alias: 'd',
default: false,
boolean: true,
description: 'Run dumper but only save stats files'
})
.help('help')
// show examples of application in action.
// final message to display when successful.
.epilog('for more information visit https://discord.gg/tWaPBNtkaq')
// disable showing help on failures, provide a final message
// to display for errors.
.showHelpOnFail(false, 'whoops, something went wrong! get more info with --help')
.argv
const SERVER_DIRECTORY = path.resolve('temp')
const SERVER_PATH = path.join(SERVER_DIRECTORY, 'server.jar')
const downloadServer = util.promisify(minecraftWrap.download)
/**
* Stat stuff to see if it exists
* @param {string} path
*/
async function fileExists (path) {
try {
await fsP.stat(path)
return true
} catch (err) {
return false
}
}
async function deleteIfExists (path) {
if (!await fileExists(path)) return
await fsP.rm(path, { recursive: true, force: true })
}
async function setupDirectories () {
console.log('deleting old packets & metadata')
await deleteIfExists(argv.outputFolder)
await deleteIfExists(path.resolve('packets_info.json'))
await deleteIfExists(path.resolve('README.md'))
await fsP.mkdir(argv.outputFolder)
if (argv.dryrun) return // don't make packet dirs when no packets are generated
await fsP.mkdir(path.join(argv.outputFolder, 'from-server'))
await fsP.mkdir(path.join(argv.outputFolder, 'from-client'))
}
async function downloadMCServer (version) {
await deleteIfExists(SERVER_DIRECTORY)
await fsP.mkdir(SERVER_DIRECTORY, { recursive: true })
console.log('downloading server')
await downloadServer(version, SERVER_PATH)
console.log('done')
}
async function startServer () {
console.log('starting server')
const server = new minecraftWrap.WrapServer(SERVER_PATH, SERVER_DIRECTORY, {
maxMem: 2048
})
server.on('line', line => console.log(`server: ${line}`))
await util.promisify(server.startServer.bind(server))({
'online-mode': false,
difficulty: 'normal',
'spawn-protection': 'off'
})
console.log('server started')
return server
}
async function startMineflayer (version) {
const packetLogger = new MineflayerLog({ version, outputDirectory: argv.outputFolder, dryRun: argv.dryrun })
packetLogger.start('localhost', 25565)
return packetLogger
}
async function cleanup () {
await fsP.rm(SERVER_DIRECTORY, { recursive: true, force: true })
await fsP.rm(path.resolve('versions'), { recursive: true, force: true })
await fsP.rm(path.resolve('launcher_accounts.json'), { force: true })
}
async function makeStats (packetLogger, version) {
const { collectedPackets, allPackets } = parsePacketCounter(version, packetLogger.kindCounter)
// write packet data
const data = {
collected: collectedPackets,
missing: allPackets.filter(o => !collectedPackets.includes(o))
}
const metadataFolder = path.join(argv.outputFolder, 'metadata')
await deleteIfExists(metadataFolder)
await fsP.writeFile(path.join(argv.outputFolder, 'README.md'), makeMarkdown(data))
await fsP.mkdir(metadataFolder)
await fsP.writeFile(path.join(metadataFolder, 'packets_info.json'), JSON.stringify(data, null, 2))
}
function asyncStopServer (server) {
return new Promise((resolve, reject) => server.stopServer(resolve))
}
async function main () {
// setup
const version = argv.version
await setupDirectories() // delete old directories
await downloadMCServer(version) // download server
// start client/server
const server = await startServer() // start server
const packetLogger = await startMineflayer(version) // start mineflayer
// generate packets
const { bot } = packetLogger
await generatePackets(server, bot)
// stop client/server
const p = once(bot, 'end')
bot.quit()
await p
await asyncStopServer(server)
// make stats files
await makeStats(packetLogger, version)
// delete temp files
await cleanup()
process.exit()
}
main()