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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
|
use comparray;
use endian;
use mcproto;
use nbt;
use fmt;
use strings;
use trace;
fn decode_position(ctx: *mcproto::Context) (BlockPos | trace::failed) = {
const v = mcproto::decode_long(ctx)?: i64;
return BlockPos {
x = (v >> 38): i32,
y = (v << 52 >> 52): i16,
z = (v << 26 >> 38): i32,
};
};
fn decode_nbt(ctx: *mcproto::Context) ((str, nbt::Object) | trace::failed) = {
let in = ctx.dec.input[ctx.dec.pos..];
match (nbt::load(&in)) {
case let res: (str, nbt::Object) =>
ctx.dec.pos += len(ctx.dec.input) - ctx.dec.pos - len(in);
return res;
case nbt::Invalid =>
return mcproto::error(ctx, "Invalid NBT");
};
};
type Handler = fn(ctx: *mcproto::Context)
(void | trace::failed);
fn game_handle_packet(ctx: *mcproto::Context, packet_id: i32)
(void | trace::failed) = {
const handler = if (packet_id: size < len(PROTO_HANDLERS))
PROTO_HANDLERS[packet_id: size] else null;
match (handler) {
case let handler: *Handler =>
const ctx_ = mcproto::context(ctx, "0x{:02x}", packet_id);
return handler(&ctx_);
case null =>
// TODO: implement all the packets ._.
// erroring here would spam too much for now...
void;
};
};
const PROTO_HANDLERS: [_]nullable *Handler = [
null, // 0x00
null,
null,
null,
null,
null,
null,
null,
null,
&handle_block_update,
null,
null,
null,
null,
null,
null,
null, // 0x10
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
&handle_unload_chunk,
null,
null,
null,
null, // handle_keep_alive; handled in client_handle_packet.
&handle_chunk_data_and_update_light,
null,
null,
null,
null, // handle_login; handled in client_handle_packet.
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null, // 0x30
null,
null,
null,
&handle_combat_death,
null,
null,
null,
&handle_synchronize_player_position,
null,
null,
null,
null,
&handle_respawn,
null,
&handle_update_section_blocks,
null, // 0x40
null,
null,
null,
null,
null,
null,
null,
null,
null,
&handle_set_center_chunk,
null,
&handle_set_default_spawn_position,
null,
null,
null,
null, // 0x50
null,
null,
&handle_set_health,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null, // 0x60
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
];
fn handle_login(ctx: *mcproto::Context) (void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "player id");
const player_id = mcproto::decode_int(&ctx_)?;
const ctx_ = mcproto::context(ctx, "is hardcore");
const is_hardcore = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "gamemode");
const gamemode = mcproto::decode_byte(&ctx_)?: Gamemode;
if (gamemode >= Gamemode::COUNT) {
return mcproto::error(&ctx_, "Invalid gamemode 0x{:02x}",
gamemode: u8);
};
const ctx_ = mcproto::context(ctx, "previous gamemode");
const prev_gamemode = mcproto::decode_byte(&ctx_)?;
const ctx_ = mcproto::context(ctx, "dimension count");
const dim_count = mcproto::decode_varint(&ctx_)?;
for (let i = 0i32; i < dim_count; i += 1) {
const ctx_ = mcproto::context(ctx, "dimension name {}", i);
const dim_name = mcproto::decode_string(&ctx_, 0x7fff)?;
};
const ctx_ = mcproto::context(ctx, "registry");
const registry = decode_nbt(&ctx_)?;
free(registry.0);
const registry = registry.1;
defer nbt::finish(registry);
const ctx_ = mcproto::context(ctx, "dimension type");
const dim_type = mcproto::decode_string(&ctx_, 0x7fff)?;
const ctx_ = mcproto::context(ctx, "dimension name");
const dim_name = mcproto::decode_string(&ctx_, 0x7fff)?;
const ctx_ = mcproto::context(ctx, "hashed seed");
const hashed_seed = mcproto::decode_long(&ctx_)?;
const ctx_ = mcproto::context(ctx, "max players");
const max_players = mcproto::decode_varint(&ctx_)?;
if (max_players < 0) {
return mcproto::error(&ctx_, "Max players must not be negative");
};
const ctx_ = mcproto::context(ctx, "view distance");
const view_distance = mcproto::decode_varint(&ctx_)?;
if (view_distance <= 0) {
return mcproto::error(&ctx_, "View distance must be positive");
};
const ctx_ = mcproto::context(ctx, "simulation distance");
const sim_distance = mcproto::decode_varint(&ctx_)?;
if (sim_distance <= 0) {
return mcproto::error(&ctx_,
"Simulation distance must be positive");
};
const ctx_ = mcproto::context(ctx, "reduced debug info");
const reduced_debug_info = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "enable respawn screen");
const enable_respawn_screen = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "is debug");
const is_debug = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "is flat");
const is_flat = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "has death location");
if (mcproto::decode_bool(&ctx_)?) {
const ctx_ = mcproto::context(ctx, "death dimension");
const death_dim = mcproto::decode_string(&ctx_, 0x7fff)?;
const ctx_ = mcproto::context(ctx, "death position");
const death_pos = decode_position(&ctx_)?;
};
mcproto::expect_end(ctx)?;
GAMEMODE = gamemode;
trace::info(&trace::root, "gamemode {}", strgamemode(GAMEMODE));
assert(len(DIM_TYPES) == 0);
match (nbt_get(®istry, "minecraft:dimension_type")) {
case let reg: nbt::Object =>
match (nbt_get(®, "value")) {
case let reg: []nbt::Value =>
for (let i = 0z; i < len(reg); i += 1) {
const entry = match (reg[i]) {
case let entry: nbt::Object =>
yield entry;
case =>
return mcproto::error(ctx, "Invalid registry");
};
const name = match (nbt_get(&entry, "name")) {
case let name: str =>
yield strings::dup(name);
case =>
return mcproto::error(ctx, "Invalid registry");
};
const elem = match (nbt_get(&entry, "element")) {
case let elem: nbt::Object =>
yield elem;
case =>
return mcproto::error(ctx, "Invalid registry");
};
const min_y = match (nbt_get(&elem, "min_y")) {
case let min_y: i32 =>
if (min_y & 0xf != 0)
return mcproto::error(ctx, "Invalid registry");
min_y >>= 4;
if (min_y: i8 != min_y)
return mcproto::error(ctx, "Invalid registry");
yield min_y: i8;
case =>
return mcproto::error(ctx, "Invalid registry");
};
const height = match (nbt_get(&elem, "height")) {
case let height: i32 =>
if (height & 0xf != 0)
return mcproto::error(ctx, "Invalid registry");
height >>= 4;
if (height: u8 != height: u32)
return mcproto::error(ctx, "Invalid registry");
yield height: u8;
case =>
return mcproto::error(ctx, "Invalid registry");
};
if (min_y + height: i8 < min_y) {
return mcproto::error(ctx, "Invalid registry");
};
append(DIM_TYPES, DimType {
name = name,
min_y = min_y,
height = height,
});
};
case =>
return mcproto::error(ctx, "Invalid registry");
};
case =>
return mcproto::error(ctx, "Invalid registry");
};
let found = false;
for (let i = 0z; i < len(DIM_TYPES); i += 1) {
if (DIM_TYPES[i].name == dim_type) {
found = true;
DIM_TYPE = i;
};
};
if (!found) {
return mcproto::error(ctx, "Unknown dimension type {}", dim_type);
};
trace::info(&trace::root, "dimension type {}", DIM_TYPES[DIM_TYPE].name);
VIEW_DISTANCE = view_distance;
trace::info(&trace::root, "view distance {}", VIEW_DISTANCE);
game_init();
};
fn handle_respawn(ctx: *mcproto::Context) (void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "dimension type");
const dim_type = mcproto::decode_string(&ctx_, 0x7fff)?;
const ctx_ = mcproto::context(ctx, "dimension name");
const dim_name = mcproto::decode_string(&ctx_, 0x7fff)?;
const ctx_ = mcproto::context(ctx, "hashed seed");
const hashed_seed = mcproto::decode_long(&ctx_)?;
const ctx_ = mcproto::context(ctx, "gamemode");
const gamemode = mcproto::decode_byte(&ctx_)?: Gamemode;
if (gamemode >= Gamemode::COUNT) {
return mcproto::error(&ctx_, "Invalid gamemode 0x{:02x}",
gamemode: u8);
};
const ctx_ = mcproto::context(ctx, "previous gamemode");
const prev_gamemode = mcproto::decode_byte(&ctx_)?;
const ctx_ = mcproto::context(ctx, "is debug");
const is_debug = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "is flat");
const is_flat = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "copy metadata");
const copy_metadata = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "has death location");
if (mcproto::decode_bool(&ctx_)?) {
const ctx_ = mcproto::context(ctx, "death dimension");
const death_dim = mcproto::decode_string(&ctx_, 0x7fff)?;
const ctx_ = mcproto::context(ctx, "death position");
const death_pos = decode_position(&ctx_)?;
};
mcproto::expect_end(ctx)?;
game_despawn();
GAMEMODE = gamemode;
trace::info(&trace::root, "gamemode {}", strgamemode(GAMEMODE));
let found = false;
for (let i = 0z; i < len(DIM_TYPES); i += 1) {
if (DIM_TYPES[i].name == dim_type) {
found = true;
DIM_TYPE = i;
};
};
if (!found) {
return mcproto::error(ctx, "Unknown dimension type {}",
dim_type);
};
trace::info(&trace::root, "dimension type {}",
DIM_TYPES[DIM_TYPE].name);
game_respawn();
};
fn handle_keep_alive(ctx: *mcproto::Context) (void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "id");
const id = mcproto::decode_long(&ctx_)?;
mcproto::expect_end(ctx)?;
let out: []u8 = [];
defer free(out);
mcproto::encode_long(&out, id);
network_send(0x11, out);
};
fn handle_set_center_chunk(ctx: *mcproto::Context)
(void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "x");
const x = mcproto::decode_varint(&ctx_)?;
const ctx_ = mcproto::context(ctx, "z");
const z = mcproto::decode_varint(&ctx_)?;
mcproto::expect_end(ctx)?;
setview(ChunkPos { x = x, z = z });
};
type SyncPositionFlags = enum u8 {
X = 0x1,
Y = 0x2,
Z = 0x4,
// TODO: confirm that these are the correct way around;
// update wiki to clarify.
YAW = 0x8,
PITCH = 0xa,
};
fn handle_synchronize_player_position(ctx: *mcproto::Context)
(void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "x");
let x = mcproto::decode_double(&ctx_)?;
const ctx_ = mcproto::context(ctx, "y");
let y = mcproto::decode_double(&ctx_)?;
const ctx_ = mcproto::context(ctx, "z");
let z = mcproto::decode_double(&ctx_)?;
const ctx_ = mcproto::context(ctx, "yaw");
let yaw = mcproto::decode_float(&ctx_)?;
const ctx_ = mcproto::context(ctx, "pitch");
let pitch = mcproto::decode_float(&ctx_)?;
const ctx_ = mcproto::context(ctx, "flags");
const flags = mcproto::decode_byte(&ctx_)?;
const ctx_ = mcproto::context(ctx, "teleport id");
const id = mcproto::decode_varint(&ctx_)?;
const ctx_ = mcproto::context(ctx, "dismount");
const dismount = mcproto::decode_bool(&ctx_)?;
mcproto::expect_end(ctx)?;
if (flags & SyncPositionFlags::X != 0) x += PLAYER_POS[0];
if (flags & SyncPositionFlags::Y != 0) y += PLAYER_POS[1];
if (flags & SyncPositionFlags::Z != 0) z += PLAYER_POS[2];
if (flags & SyncPositionFlags::YAW != 0) yaw += PLAYER_YAW;
if (flags & SyncPositionFlags::PITCH != 0) pitch += PLAYER_PITCH;
PLAYER_POS = [x: f32, y: f32, z: f32];
PLAYER_YAW = yaw;
PLAYER_PITCH = pitch;
let out: []u8 = [];
defer free(out);
mcproto::encode_varint(&out, id);
network_send(0x00, out);
// TODO: is this necessary? (probably for anticheat reasons, at
// least...)
let out: []u8 = [];
defer free(out);
mcproto::encode_double(&out, PLAYER_POS[0]);
mcproto::encode_double(&out, PLAYER_POS[1]);
mcproto::encode_double(&out, PLAYER_POS[2]);
mcproto::encode_float(&out, PLAYER_YAW);
mcproto::encode_float(&out, PLAYER_PITCH);
mcproto::encode_bool(&out, true);
network_send(0x14, out);
};
fn handle_set_health(ctx: *mcproto::Context) (void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "health");
const health = mcproto::decode_float(&ctx_)?;
const ctx_ = mcproto::context(ctx, "food");
const food = mcproto::decode_varint(&ctx_)?;
const ctx_ = mcproto::context(ctx, "saturation");
const saturation = mcproto::decode_float(&ctx_)?;
mcproto::expect_end(ctx)?;
PLAYER_HEALTH = health;
PLAYER_FOOD = food;
PLAYER_SATURATION = saturation;
};
fn handle_combat_death(ctx: *mcproto::Context) (void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "player id");
const player_id = mcproto::decode_varint(&ctx_)?;
const ctx_ = mcproto::context(ctx, "entity id");
const entity_id = mcproto::decode_int(&ctx_)?;
const ctx_ = mcproto::context(ctx, "message");
const message = mcproto::decode_string(&ctx_, 0x7fff)?;
mcproto::expect_end(ctx)?;
trace::info(&trace::root, "death: {}", message);
death_show(message);
};
fn handle_set_default_spawn_position(ctx: *mcproto::Context)
(void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "position");
const pos = decode_position(&ctx_)?;
const ctx_ = mcproto::context(ctx, "yaw");
const yaw = mcproto::decode_float(&ctx_)?;
LOADING_RECEIVED_SPAWN_POSITION = true;
};
fn handle_block_update(ctx: *mcproto::Context) (void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "position");
const pos = decode_position(&ctx_)?;
const ctx_ = mcproto::context(ctx, "blockstate");
const bstate = mcproto::decode_varint(&ctx_)?;
if (bstate & ~0xffff != 0) {
return mcproto::error(&ctx_, "Blockstate id too large");
};
const bstate = bstate: u16;
mcproto::expect_end(&ctx_)?;
if (getsection(block_section(pos)) is null) {
mcproto::log(ctx, trace::level::WARN,
"Block update references nonexistent chunk: ( {} {} {} )",
pos.x, pos.y, pos.z);
return;
};
setblock(pos, bstate);
};
fn handle_update_section_blocks(ctx: *mcproto::Context)
(void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "section position");
const section_pos = mcproto::decode_long(&ctx_)?: i64;
const section_pos = SectionPos {
x = (section_pos >> 42): i32,
y = (section_pos << 44 >> 44): i8,
z = (section_pos << 22 >> 42): i32,
};
if (getsection(section_pos) is null) {
mcproto::log(ctx, trace::level::WARN,
"Section update references nonexistent chunk: [ {} {} {} ]",
section_pos.x, section_pos.y, section_pos.z);
return;
};
const ctx_ = mcproto::context(ctx, "suppress light updates");
const suppress_light_updates = mcproto::decode_bool(&ctx_)?;
const ctx_ = mcproto::context(ctx, "block count");
const nblocks = mcproto::decode_varint(&ctx_)?;
for (let i = 0i32; i < nblocks; i += 1) {
const ctx_ = mcproto::context(ctx, "entry {}", i);
// TODO: should be varlong
const entry = mcproto::decode_varint(&ctx_)?;
const pos = BlockPos {
x = (section_pos.x << 4) + (entry >> 8 & 0xf),
y = (section_pos.y: i16 << 4) + (entry & 0xf): i16,
z = (section_pos.z << 4) + (entry >> 4 & 0xf),
};
const bstate = entry >> 12;
if (bstate & ~0xffff != 0) {
return mcproto::error(&ctx_, "Blockstate id too large");
};
const bstate = bstate: u16;
setblock(pos, bstate);
};
mcproto::expect_end(ctx)?;
};
fn handle_chunk_data_and_update_light(ctx: *mcproto::Context)
(void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "x");
const x = mcproto::decode_int(&ctx_)?: i32;
const ctx_ = mcproto::context(ctx, "z");
const z = mcproto::decode_int(&ctx_)?: i32;
const chunk_pos = ChunkPos { x = x, z = z };
if (getchunk(chunk_pos) is null) {
return mcproto::error(ctx,
"Chunk out of range: [ {} {} ]",
chunk_pos.x, chunk_pos.z);
};
const ctx_ = mcproto::context(ctx, "heightmaps");
const heightmaps = decode_nbt(&ctx_)?;
free(heightmaps.0);
const heightmaps = heightmaps.1;
defer nbt::finish(heightmaps);
const ctx_ = mcproto::context(ctx, "data length");
const datalen = mcproto::decode_varint(&ctx_)?;
if (datalen < 0) {
return mcproto::error(&ctx_, "Data length must not be negative");
};
const datalen = datalen: u32;
const ctx_ = mcproto::context(ctx, "chunk data");
mcproto::decode_nbytes(&ctx_, datalen)?;
let cdec = *ctx.dec;
cdec.input = cdec.input[..cdec.pos];
cdec.pos -= datalen;
const cctx = mcproto::root(&cdec);
const chunk = chunk_init(chunk_pos);
for (let i = 0z; i < CHUNKS_HEIGHT; i += 1) {
const cctx_ = mcproto::context(&cctx, "block count");
const block_count = mcproto::decode_short(&cctx_)?;
const cctx_ = mcproto::context(&cctx, "bits per entry");
const bpe = mcproto::decode_byte(&cctx_)?;
const bstates = if (bpe == 0) { // constant
const cctx_ = mcproto::context(&cctx, "constant value");
const value = mcproto::decode_varint(&cctx_)?;
if (value > 0xffff) {
return mcproto::error(&cctx_, "Blockstate id too large");
};
const cctx_ = mcproto::context(&cctx, "array length");
const arraylen = mcproto::decode_varint(&cctx_)?;
if (arraylen != 0) {
return mcproto::error(&cctx_,
"Chunk data array must be empty when bpe = 0");
};
let array = comparray::new(1, 4096);
comparray::clear(&array, value: u16);
yield array;
} else {
let array = if (bpe >= 9) { // direct
yield comparray::new(0, 4096);
} else { // indirect
const cctx_ = mcproto::context(&cctx,
"palette length");
const palettelen =
mcproto::decode_varint(&cctx_)?: u32;
if (palettelen >= 256 || palettelen < 2) {
// = 256 is actually possible, but it's
// going to be a huge pain. oh well.
return mcproto::error(&cctx_, "Uh oh.");
};
let array =
comparray::new(palettelen: u8, 4096);
let palette = comparray::get_palette(&array);
for (let i = 0z; i < palettelen; i += 1) {
const cctx_ = mcproto::context(&cctx,
"palette entry {}", i);
const value =
mcproto::decode_varint(&cctx_)?;
if (value > 0xffff) {
return mcproto::error(&cctx_,
"Blockstate id too large");
};
palette[i] = value: u16;
};
yield array;
};
const bpe = if (bpe <= 4) 4u8
else if (bpe <= 8) bpe: u8
else 15u8;
const cctx_ = mcproto::context(&cctx, "array length");
const arraylen = mcproto::decode_varint(&cctx_)?;
const epl = 64 / bpe;
const arraylen_ = (4096z + epl - 1) / epl;
if (arraylen: size != arraylen_) {
return mcproto::error(&cctx_,
"Chunk data array with bpe = {} must have length {}, not {}",
bpe, arraylen_, arraylen);
};
const cctx_ = mcproto::context(&cctx, "array data");
let arraydata =
mcproto::decode_nbytes(&cctx_,
arraylen: size * 8)?;
let pos = 0z;
let long = 0u64;
let shift = 64u8;
const mask = (1u64 << bpe) - 1;
for (let i = 0z; i < 4096; i += 1) {
if (shift + bpe > 64) {
long = endian::begetu64(
arraydata[pos..pos + 8]);
pos += 8;
shift = 0;
};
const value = long >> shift & mask;
shift += bpe;
if (array.palette_size != 0
&& value > array.palette_size) {
return mcproto::error(&cctx_,
"Palette index out of range (entry {})",
i);
};
if (array.palette_size == 0 && value > 0xffff) {
return mcproto::error(&cctx_,
"Blockstate id too large (entry {})",
i);
};
comparray::set(&array, i, value: u16);
};
yield array;
};
const cctx_ = mcproto::context(&cctx, "bits per entry");
const bpe = mcproto::decode_byte(&cctx_)?;
if (bpe == 0) { // constant
const cctx_ = mcproto::context(&cctx, "constant value");
mcproto::decode_varint(&cctx_)?;
} else if (bpe < 4) { // indirect
const cctx_ = mcproto::context(&cctx, "palette length");
const palettelen = mcproto::decode_varint(&cctx)?: u32;
for (let i = 0z; i < palettelen; i += 1) {
const cctx_ = mcproto::context(&cctx,
"palette entry {}", i);
mcproto::decode_varint(&cctx_)?;
};
};
const cctx_ = mcproto::context(&cctx, "array length");
const arraylen = mcproto::decode_varint(&cctx_)?: u32;
const cctx_ = mcproto::context(&cctx, "array data");
mcproto::decode_nbytes(&cctx_, arraylen: size * 8)?;
const section = &(chunk.sections as *[*]Section)[i];
section.bstates = bstates;
section.dirty = true;
};
// other stuff we don't care about for now.
};
fn handle_unload_chunk(ctx: *mcproto::Context)
(void | trace::failed) = {
const ctx_ = mcproto::context(ctx, "x");
const x = mcproto::decode_int(&ctx_)?: i32;
const ctx_ = mcproto::context(ctx, "z");
const z = mcproto::decode_int(&ctx_)?: i32;
chunk_destroy(ChunkPos { x = x, z = z });
};
// this is really awkward, but i hear a "match overhaul" is coming that should
// improve things...
fn nbt_get(obj: *nbt::Object, key: str)
(...nbt::Value | void) = {
match (nbt::get(obj, key)) {
case let value: *nbt::Value =>
return *value;
case null =>
return void;
};
};
|