forked from aresrpg/minecraft-dissector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
304 lines (269 loc) · 7.9 KB
/
generate.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const dataPath = require("./minecraft-data/data/dataPaths.json")
const data = require(`./minecraft-data/data/${dataPath.pc["1.16.3"].protocol}/protocol.json`)
const assert = require("assert")
function indent(code, indent = " ") {
return code
.split("\n")
.map((line) => indent + line)
.join("\n")
}
function unsnake(s) {
return s
.split("_")
.map((e) => e[0].toUpperCase() + e.slice(1).toLowerCase())
.join(" ")
}
function uncamel(s) {
return s
.replace(/_\w/g, (m) => m[1].toUpperCase())
.split(/(?=[A-Z0-9])/)
.map((e) => e[0].toUpperCase() + e.slice(1).toLowerCase())
.join(" ")
}
const natives = {
container(args, { path, read }) {
return {
code: args
.map(({ type, name, anon }) => {
if (name === 'item') console.error(type)
const new_path = `${path}_${name}`
const { code, hf_type } = read(type, undefined, {
path: new_path,
read,
name
})
hf.push({
name,
path: new_path,
type: hf_type,
})
return code
})
.join("\n"),
}
},
varint(args, { path }) {
return {
code: `minecraft_add_varint(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_UINT32",
return_type: "guint32"
}
},
string(args, { path }) {
return {
code: `minecraft_add_string(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_STRING",
}
},
bool(args, { path }) {
return {
code: `minecraft_add_bool(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_BOOLEAN",
}
},
u8(args, { path }) {
return {
code: `minecraft_add_u8(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_UINT8",
}
},
i8(args, { path }) {
return {
code: `minecraft_add_i8(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_INT8",
}
},
u16(args, { path }) {
return {
code: `minecraft_add_u16(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_UINT16",
}
},
i16(args, { path }) {
return {
code: `minecraft_add_i16(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_INT16",
}
},
i32(args, { path }) {
return {
code: `minecraft_add_i32(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_INT32",
}
},
i64(args, { path }) {
return {
code: `minecraft_add_i64(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_INT64",
}
},
f32(args, { path }) {
return {
code: `minecraft_add_f32(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_FLOAT",
}
},
f64(args, { path }) {
return {
code: `minecraft_add_f64(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_DOUBLE",
}
},
UUID(args, { path }) {
return {
code: `minecraft_add_UUID(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_BYTES",
}
},
restBuffer(args, { path }) {
return {
code: `minecraft_add_restbuffer(tree, hf_${path}, tvb, &offset);`,
hf_type: "FT_BYTES",
}
},
buffer({ countType }, { read, path, name }) {
const count = read(countType, undefined, { read, path: `${path}_len`})
hf.push({
name: name ? `${name}Length` : 'Length',
path: `${path}_len`,
type: count.hf_type,
})
return {
code:
`${count.return_type} ${path}_len = ${count.code}
minecraft_add_buffer(tree, hf_${path}, tvb, &offset, ${path}_len);`,
hf_type: "FT_BYTES",
}
},
option(args, ctx) {
const inner = ctx.read(args, undefined, ctx)
return {
code:
`if (tvb_get_guint8(tvb, offset) == 1) {
offset += 1;
${inner.code}
}`,
hf_type: inner.hf_type,
}
},
}
const functions = []
const hf = []
function generate(namespace, skip = []) {
const types = {}
const res = namespace.reduce((c, v) => {
if (c.types) {
Object.assign(types, c.types)
}
return c[v]
}, data)
if (res.types) {
Object.assign(types, res.types)
}
Object.assign(types, natives)
function read(type, args, ctx = {}) {
let fieldInfo
if (typeof type === "string") {
if (!(type in types)) throw new Error("unknown type " + type)
fieldInfo = types[type]
} else fieldInfo = type
if (typeof fieldInfo === "function") {
return fieldInfo(args, ctx)
}
if (typeof fieldInfo === "string") {
if (fieldInfo === "native") {
throw new Error("unknown native " + type)
}
return read(fieldInfo, args, ctx)
} else if (Array.isArray(fieldInfo)) {
return read(fieldInfo[0], fieldInfo[1], ctx)
} else throw new Error("Invalid type " + type)
}
const { packet } = types
assert(Array.isArray(packet) && packet.length == 2)
assert(packet[0] === "container")
assert(packet[1][0].name === "name" && packet[1][0].type[0] === "mapper")
assert(packet[1][1].name === "params" && packet[1][1].type[0] === "switch")
assert(packet[1][0].type[1].type === "varint")
const names = packet[1][0].type[1].mappings
const names_to_types = packet[1][1].type[1].fields
const path = namespace.join("_")
functions.push(
`void ${path}(guint32 packet_id, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, guint offset, guint32 len) {
switch (packet_id) {
${indent(
Object.entries(names)
.map(([id, name]) => {
if (skip.includes(name)) {
return `case ${id}:
col_set_str(pinfo->cinfo, COL_INFO, "${unsnake(name)} [${namespace[0]}] (${namespace[1]})");
break;`
}
const { code } = read(names_to_types[name], undefined, {
path: `${path}_${name}`,
read,
})
return `case ${id}:
col_set_str(pinfo->cinfo, COL_INFO, "${unsnake(name)} [${namespace[0]}] (${namespace[1]})");
${indent(code)}
break;`
}, " ")
.join("\n")
)}
}
}`
)
}
generate(["handshaking", "toServer"])
generate(["handshaking", "toClient"])
generate(["status", "toServer"])
generate(["status", "toClient"])
generate(["login", "toServer"])
generate(["login", "toClient"])
generate(["play", "toServer"], [
// need to support bitfield and switch
'query_block_nbt', 'use_entity', 'generate_structure', 'block_dig', 'advancement_tab', 'update_command_block',
'update_jigsaw_block', 'update_structure_block', 'update_sign', 'block_place',
'window_click', 'edit_book', 'set_creative_slot'
])
const use_array = [
'statistics', 'multi_block_change', 'tab_complete', 'declare_commands', 'window_items',
'explosion', 'login', 'map', 'trade_list', 'player_info', 'unlock_recipes', 'entity_destroy',
'set_passengers', 'advancements', 'entity_update_attributes', 'declare_recipes', 'tags',
]
const use_optionalNbt = ['tile_entity_data']
const use_nbt = ['map_chunk', 'nbt_query_response', 'respawn']
const use_entityMetadataLoop = ['entity_metadata']
const use_topBitSetTerminatedArray = ['entity_equipment']
const use_bitfield = [
'spawn_entity_painting', 'acknowledge_player_digging', 'block_break_animation', 'block_action', 'block_change',
'world_event', 'open_sign_entity', 'spawn_position'
]
const use_switch = [
'boss_bar', 'world_particles', 'combat_event', 'face_player', 'world_border', 'scoreboard_objective', 'teams',
'scoreboard_score', 'title', 'stop_sound', 'set_slot'
]
generate(["play", "toClient"], [
...use_array,...use_optionalNbt, ...use_nbt, ...use_entityMetadataLoop,
...use_topBitSetTerminatedArray, ...use_bitfield, ...use_switch
])
console.log(hf.map(({ path }) => `static int hf_${path} = -1;`).join("\n"))
console.log("")
console.log(`static hf_register_info hf_generated[] = {`)
const NONE_TYPES = ["FT_STRING", "FT_BYTES", "FT_FLOAT", "FT_DOUBLE"]
console.log(
indent(
hf
.map(({ name, path, type }) => {
return `{ &hf_${path},
{ "${uncamel(name)}", "minecraft.${path.replace(/_/g, ".")}", ${type}, ${
NONE_TYPES.includes(type) ? "BASE_NONE" : "BASE_DEC"
}, NULL,
0x0, "${uncamel(name)}", HFILL }},`
})
.join("\n\n")
)
)
console.log("};")
console.log("")
console.log(functions.join("\n\n"))