-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.c
1034 lines (833 loc) · 26.8 KB
/
world.c
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdbool.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <zlib.h>
#include "config.h"
#include "types.h"
#include "platform.h"
#include "common.h"
#include "cmd.h"
#include "console.h"
#include "nbt.h"
#include "protocol.h"
#include "world.h"
#include "map.h"
static GHashTable *region_table = 0;
GHashTable *world_entities = 0;
G_LOCK_DEFINE(entity_mutex);
static jint entity_player = -1;
static jint entity_vehicle = -1;
int world_time = 0;
static jlong world_seed = 0;
coord3_t player_pos = { .x = 0, .y = 0, .z = 0 };
int player_yaw = 0;
jshort player_health = 0;
static bool spawn_known = false;
static jint spawn_x = 0, spawn_y = 0, spawn_z = 0;
static char *world_path = 0;
static char *region_path = 0;
static GAsyncQueue *worldq = 0;
static gpointer world_thread(gpointer data);
struct region_file
{
int fd;
unsigned nsect;
unsigned char *contents;
mmap_handle_t contents_map;
unsigned offsets[REGION_SIZE][REGION_SIZE];
uint8_t sects[REGION_SIZE][REGION_SIZE];
jint tstamps[REGION_SIZE][REGION_SIZE];
unsigned char dirty_chunks[REGION_SIZE][REGION_SIZE];
GByteArray *sect_bitmap;
};
static void region_free(gpointer gp)
{
struct region *region = gp;
for (size_t i = 0; i < NELEMS(region->chunks); i++)
for (size_t j = 0; j < NELEMS(region->chunks[i]); j++)
g_free(region->chunks[i][j]);
g_free(region);
}
static void entity_free(gpointer ep)
{
struct entity *e = ep;
g_free(e->name);
g_free(e);
}
void world_start(const char *path)
{
region_table = g_hash_table_new_full(coord_glib_hash, coord_glib_equal, 0, region_free);
world_entities = g_hash_table_new_full(g_int_hash, g_int_equal, 0, entity_free);
worldq = g_async_queue_new_full(packet_free);
g_thread_create(world_thread, 0, false, 0);
/* locate/create the world directory as required */
if (!path)
return;
world_path = g_strdup(path);
region_path = g_strdup_printf("%s/region", world_path);
if (g_file_test(world_path, G_FILE_TEST_EXISTS))
{
if (!g_file_test(world_path, G_FILE_TEST_IS_DIR))
dief("world directory not a directory: %s", world_path);
}
else
{
if (g_mkdir(world_path, 0777) != 0)
dief("unable to create world directory: %s", world_path);
if (g_mkdir(region_path, 0777) != 0)
dief("unable to create region directory: %s", region_path);
}
/* scan and index all existing region files */
/* TODO: maybe log something about this? shouldn't take long... */
GError *error = 0;
GDir *dir = g_dir_open(region_path, 0, &error);
if (!dir)
dief("unable to scan region directory contents: %s", error->message);
const char *region_file = 0;
while ((region_file = g_dir_read_name(dir)))
{
char xbuf[64], zbuf[64], *xend, *zend;
if (sscanf(region_file, "r.%63[^.].%63[^.].mcr", xbuf, zbuf) != 2)
continue; /* does not look like a proper region file */
jint x = strtol(xbuf, &xend, 36);
jint z = strtol(zbuf, &zend, 36);
if (!*xbuf || !*zbuf || *xend || *zend)
continue; /* x/z coords don't look like base36 numbers */
coord_t rc = COORD(x * REGION_XSIZE, z * REGION_ZSIZE);
struct region *region = world_region(rc, true);
char *region_file_path = g_strdup_printf("%s/%s", region_path, region_file);
region->file = world_regfile_open(region_file_path);
g_free(region_file_path);
/* TODO FIXME: debugging code: load all regions to memory at start */
world_regfile_load(region);
}
}
void world_push(struct directed_packet *dpacket)
{
struct directed_packet *dpacket_copy = g_new(struct directed_packet, 1);
dpacket_copy->from = dpacket->from;
dpacket_copy->p = packet_dup(dpacket->p);
g_async_queue_push(worldq, dpacket_copy);
}
struct region *world_region(coord_t cc, bool gen)
{
coord_t rc = COORD(REGION_XMASK(cc.x), REGION_ZMASK(cc.z));
struct region *region = g_hash_table_lookup(region_table, &rc);
if (region)
return region;
if (!gen)
return NULL;
region = g_new(struct region, 1);
region->key = rc;
/* Can't use g_malloc0; NULL might not be all-bits-zero */
for (size_t i = 0; i < NELEMS(region->chunks); i++)
for (size_t j = 0; j < NELEMS(region->chunks[i]); j++)
region->chunks[i][j] = NULL;
region->file = 0;
g_hash_table_insert(region_table, ®ion->key, region);
return region;
}
struct chunk *world_chunk(coord_t cc, bool gen)
{
struct region *region = world_region(cc, gen);
if (!region)
return 0;
jint xo = CHUNK_XIDX(REGION_XOFF(cc.x)), zo = CHUNK_ZIDX(REGION_ZOFF(cc.z));
if (gen && !region->chunks[xo][zo])
region->chunks[xo][zo] = g_malloc0(sizeof(struct chunk));
return region->chunks[xo][zo];
}
unsigned char *world_stack(coord_t cc, bool gen)
{
struct chunk *c = world_chunk(cc, gen);
if (!c)
return 0;
return c->blocks[CHUNK_XOFF(cc.x)][CHUNK_ZOFF(cc.z)];
}
jint world_getheight(coord_t cc)
{
struct chunk *c = world_chunk(cc, false);
if (!c)
return -1;
return c->height[CHUNK_XOFF(cc.x)][CHUNK_ZOFF(cc.z)];
}
static bool handle_compressed_chunk(jint x0, jint y0, jint z0,
jint xs, jint ys, jint zs,
unsigned zlen, unsigned char *zdata,
bool update_map)
{
static unsigned char zbuf[256*1024];
int err;
z_stream zstr = {
.next_in = zdata,
.avail_in = zlen,
.next_out = zbuf,
.avail_out = sizeof zbuf
};
if ((err = inflateInit(&zstr)) != Z_OK)
{
log_print("[WHAT] chunk update decompression: inflateInit: %s", zError(err));
return false;
}
while (zstr.avail_in)
{
err = inflate(&zstr, Z_PARTIAL_FLUSH);
if (err != Z_OK && err != Z_STREAM_END)
{
log_print("[WHAT] chunk update decompression: inflate: %s", zError(err));
return false;
}
if (err == Z_STREAM_END)
break;
}
int zbuf_len = (sizeof zbuf) - zstr.avail_out;
inflateEnd(&zstr);
if (zbuf_len < xs*ys*zs)
{
log_print("[WHAT] broken decompressed chunk length: %d != %d and < %d",
(int)zbuf_len, (int)(5*xs*ys*zs+1)/2, xs*ys*zs);
return false;
}
if (y0 > CHUNK_YSIZE)
{
log_print("[WHAT] too high chunk update: %d..%d", y0, y0+ys-1);
return false;
}
else if (y0 + ys > CHUNK_YSIZE)
ys = CHUNK_YSIZE - y0;
// FIXME: These lengths are too big, because they include the buffers after them.
// Not really a problem, though.
struct buffer zb = { zbuf_len, zbuf };
#ifdef FEAT_FULLCHUNK
struct buffer zb_meta = offset_buffer(zb, xs*ys*zs);
struct buffer zb_light_blocks = offset_buffer(zb_meta, (xs*ys*zs+1)/2);
struct buffer zb_light_sky = offset_buffer(zb_light_blocks, (xs*ys*zs+1)/2);
#else
struct buffer zb_meta = { 0, 0 };
struct buffer zb_light_blocks = { 0, 0 };
struct buffer zb_light_sky = { 0, 0 };
#endif
return world_handle_chunk(x0, y0, z0, xs, ys, zs, zb, zb_meta, zb_light_blocks, zb_light_sky, update_map);
}
bool world_handle_chunk(jint x0, jint y0, jint z0,
jint xs, jint ys, jint zs,
struct buffer zb, struct buffer zb_meta,
struct buffer zb_light_blocks, struct buffer zb_light_sky,
bool update_map)
{
bool set_chunk = false;
coord_t current_chunk = COORD(0, 0);
struct chunk *c = 0;
if (ys < 0)
{
log_print("[WARN] Invalid chunk size! Probably WorldEdit; go yell at the author.");
return false;
}
jint c_min_x = INT_MAX, c_min_z = INT_MAX;
jint c_max_x = INT_MIN, c_max_z = INT_MIN;
bool changed = false;
for (jint x = x0; x < x0+xs; x++)
{
for (jint z = z0; z < z0+zs; z++)
{
coord_t cc = COORD(CHUNK_XMASK(x), CHUNK_ZMASK(z));
if (!coord_equal(cc, current_chunk) || !set_chunk)
{
set_chunk = true;
c = world_chunk(cc, true);
current_chunk = cc;
/* TODO FIXME: marks dirty always, even when loading from disk; also unoptimal */
struct region *r = world_region(cc, false);
if (r && r->file)
r->file->dirty_chunks[CHUNK_ZIDX(REGION_ZOFF(cc.z))][CHUNK_XIDX(REGION_XOFF(cc.x))] = 1;
}
if (!changed && memcmp(&c->blocks[CHUNK_XOFF(x)][CHUNK_ZOFF(z)][y0], zb.data, ys) != 0)
changed = true;
memcpy(&c->blocks[CHUNK_XOFF(x)][CHUNK_ZOFF(z)][y0], zb.data, ys);
ADVANCE_BUFFER(zb, ys);
#ifdef FEAT_FULLCHUNK
size_t bytes = (ys+1)/2;
if (bytes <= zb_meta.len)
{
memcpy(&c->meta[(CHUNK_XOFF(x)*CHUNK_ZSIZE + CHUNK_ZOFF(z))*(CHUNK_YSIZE/2) + y0/2], zb_meta.data, bytes);
ADVANCE_BUFFER(zb_meta, bytes);
}
if (bytes <= zb_light_blocks.len)
{
memcpy(&c->light_blocks[(CHUNK_XOFF(x)*CHUNK_ZSIZE + CHUNK_ZOFF(z))*(CHUNK_YSIZE/2) + y0/2], zb_light_blocks.data, bytes);
ADVANCE_BUFFER(zb_light_blocks, (ys+1)/2);
}
if (bytes <= zb_light_sky.len)
{
memcpy(&c->light_sky[(CHUNK_XOFF(x)*CHUNK_ZSIZE + CHUNK_ZOFF(z))*(CHUNK_YSIZE/2) + y0/2], zb_light_sky.data, bytes);
ADVANCE_BUFFER(zb_light_sky, (ys+1)/2);
}
#endif
jint h = c->height[CHUNK_XOFF(x)][CHUNK_ZOFF(z)];
if (y0+ys >= h)
{
jint newh = y0 + ys;
if (newh >= CHUNK_YSIZE)
newh = CHUNK_YSIZE - 1;
unsigned char *stack = c->blocks[CHUNK_XOFF(x)][CHUNK_ZOFF(z)];
while (!stack[newh] && newh > 0)
newh--;
c->height[CHUNK_XOFF(x)][CHUNK_ZOFF(z)] = newh;
changed = true;
}
if (cc.x < c_min_x) c_min_x = cc.x;
if (cc.x > c_max_x) c_max_x = cc.x;
if (cc.z < c_min_z) c_min_z = cc.z;
if (cc.z > c_max_z) c_max_z = cc.z;
}
}
if (changed && update_map)
map_update(COORD(c_min_x, c_min_z), COORD(c_max_x, c_max_z));
return changed;
}
static inline int block_change(struct chunk *c, jint x, jint y, jint z, unsigned char type)
{
if (y < 0 || y >= CHUNK_YSIZE)
return 0; /* sometimes server sends Y=CHUNK_YSIZE block-to-air "updates" */
int changed = (c->blocks[x][z][y] != type);
c->blocks[x][z][y] = type;
if (y >= c->height[x][z])
{
jint newh = y;
if (!type)
while (!c->blocks[x][z][newh] && newh > 0)
newh--;
if (c->height[x][z] != newh)
changed = 1;
c->height[x][z] = newh;
}
return changed;
}
static void handle_multi_set_block(jint cx, jint cz, jint size, unsigned char *coord, unsigned char *type)
{
coord_t cc = COORD(cx, cz);
struct chunk *c = world_chunk(cc, false);
if (!c)
return; /* edit in an unloaded chunk */
int changed = 0;
while (size--)
{
int x = coord[0] >> 4, y = coord[1], z = coord[0] & 0x0f;
coord += 2;
if (block_change(c, x, y, z, *type++))
changed = 1;
}
if (changed)
map_update(cc, cc);
}
static void handle_set_block(jint x, jint y, jint z, jint type)
{
coord_t cc = COORD(x, z);
struct chunk *c = world_chunk(cc, false);
if (!c)
return; /* edit in an unloaded chunk */
if (block_change(c, CHUNK_XOFF(x), y, CHUNK_ZOFF(z), type))
map_update(cc, cc);
}
static void update_player_pos(double x, double y, double z)
{
coord3_t new_pos = COORD3(floor(x), floor(y), floor(z));
if (coord3_equal(player_pos, new_pos))
return;
player_pos = new_pos;
map_mode->update_player_pos(map_mode->data);
map_repaint();
}
static void update_player_dir(double yaw)
{
int new_yaw = 0;
yaw = fmod(yaw, 360.0);
if (yaw < 0.0) yaw += 360.0;
if (yaw > 360-22.5) yaw -= 360;
while (new_yaw < 7 && yaw > 22.5)
new_yaw++, yaw -= 45.0;
if (new_yaw == player_yaw)
return;
player_yaw = new_yaw;
map_repaint();
}
static void entity_add(jint id, enum entity_type type, jshort subtype,
unsigned char *name, jint x, jint y, jint z)
{
struct entity *e = g_malloc(sizeof *e);
e->id = id;
e->type = type;
e->subtype = subtype;
e->name = name;
e->ax = x;
e->ay = y;
e->az = z;
e->pos = COORD(x/32, z/32);
G_LOCK(entity_mutex);
g_hash_table_replace(world_entities, &e->id, e);
G_UNLOCK(entity_mutex);
if (name)
{
log_print("[INFO] Player appeared: %s", name);
map_repaint();
}
}
static void entity_del(jint id)
{
if (id == entity_vehicle)
{
entity_vehicle = -1;
log_print("[INFO] Unmounted vehicle %d by destroying", id);
}
struct entity *e = g_hash_table_lookup(world_entities, &id);
/* Notch sometimes lies I guess */
if (!e) return;
/* FIXME: This is ugly */
char *name = e->name ? g_strdup((char *) e->name) : 0;
G_LOCK(entity_mutex);
g_hash_table_remove(world_entities, &id);
if (name)
{
log_print("[INFO] Player disappeared: %s", name);
g_free(name);
map_repaint();
}
G_UNLOCK(entity_mutex);
}
static void entity_move(jint id, jint x, jint y, jint z, int relative)
{
struct entity *e;
e = g_hash_table_lookup(world_entities, &id);
if (!e)
return;
if (relative)
{
e->ax += x;
e->ay += y;
e->az += z;
}
else
{
e->ax = x;
e->ay = y;
e->az = z;
}
coord_t ep = COORD(e->ax/32, e->az/32);
if (coord_equal(e->pos, ep))
return;
e->pos = ep;
if (id == entity_vehicle)
update_player_pos(ep.x, e->ay/32, ep.z);
else
map_repaint();
}
static gpointer world_thread(gpointer data)
{
while (1)
{
struct directed_packet *dpacket = g_async_queue_pop(worldq);
enum packet_origin from = dpacket->from;
packet_t *packet = dpacket->p;
g_free(dpacket);
struct buffer msg;
unsigned char *p;
jint t;
jlong tl;
switch (packet->type)
{
case PACKET_MAP_CHUNK:
p = &packet->bytes[packet->field_offset[6]];
handle_compressed_chunk(packet_int(packet, 0), packet_int(packet, 1), packet_int(packet, 2),
packet_int(packet, 3)+1, packet_int(packet, 4)+1, packet_int(packet, 5)+1,
(p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3], &p[4], true);
break;
case PACKET_MULTI_BLOCK_CHANGE:
p = &packet->bytes[packet->field_offset[2]];
t = (p[0] << 8) | p[1];
handle_multi_set_block(packet_int(packet, 0), packet_int(packet, 1),
t, p+2, p+2+t*2);
break;
case PACKET_BLOCK_CHANGE:
handle_set_block(packet_int(packet, 0),
packet_int(packet, 1),
packet_int(packet, 2),
packet_int(packet, 3));
break;
case PACKET_LOGIN_REQUEST:
if (from == PACKET_FROM_SERVER)
{
entity_player = packet_int(packet, 0);
world_seed = packet_long(packet, 3);
}
break;
case PACKET_PLAYER_LOOK:
update_player_dir(packet_double(packet, 0));
break;
case PACKET_PLAYER_POSITION_AND_LOOK:
update_player_dir(packet_double(packet, 4));
/* fall-thru to PACKET_PLAYER_POSITION */
case PACKET_PLAYER_POSITION:
if (entity_vehicle < 0)
update_player_pos(packet_double(packet, 0),
packet_double(packet, 1),
packet_double(packet, 3));
if (from == PACKET_FROM_SERVER && !spawn_known)
{
spawn_known = true;
spawn_x = packet_double(packet, 0);
spawn_y = packet_double(packet, 1);
spawn_z = packet_double(packet, 3);
}
break;
case PACKET_NAMED_ENTITY_SPAWN:
entity_add(packet_int(packet, 0),
ENTITY_PLAYER,
0,
packet_string(packet, 1).data,
packet_int(packet, 2),
packet_int(packet, 3),
packet_int(packet, 4));
break;
case PACKET_PICKUP_SPAWN:
entity_add(packet_int(packet, 0),
ENTITY_PICKUP,
packet_int(packet, 1),
0,
packet_int(packet, 4),
packet_int(packet, 5),
packet_int(packet, 6));
break;
case PACKET_MOB_SPAWN:
entity_add(packet_int(packet, 0),
ENTITY_MOB,
packet_int(packet, 1),
0,
packet_int(packet, 2),
packet_int(packet, 3),
packet_int(packet, 4));
break;
case PACKET_DESTROY_ENTITY:
entity_del(packet_int(packet, 0));
break;
case PACKET_ENTITY_RELATIVE_MOVE:
case PACKET_ENTITY_LOOK_AND_RELATIVE_MOVE:
entity_move(packet_int(packet, 0),
packet_int(packet, 1),
packet_int(packet, 2),
packet_int(packet, 3),
1);
break;
case PACKET_ENTITY_TELEPORT:
entity_move(packet_int(packet, 0),
packet_int(packet, 1),
packet_int(packet, 2),
packet_int(packet, 3),
0);
break;
case PACKET_ATTACH_ENTITY:
if (packet_int(packet, 0) == entity_player)
{
jint new_vehicle = packet_int(packet, 1);
if (new_vehicle < 0)
log_print("[INFO] Unmounted vehicle %d normally", entity_vehicle);
else
log_print("[INFO] Mounted vehicle %d", new_vehicle);
entity_vehicle = packet_int(packet, 1);
}
break;
case PACKET_TIME_UPDATE:
tl = packet_long(packet, 0);
tl %= 24000;
world_time = tl;
map_mode->update_time(map_mode->data);
break;
case PACKET_UPDATE_HEALTH:
player_health = packet_int(packet, 0);
break;
case PACKET_CHAT_MESSAGE:
msg = packet_string(packet, 0);
if (msg.len >= 3 && msg.data[0] == '/' && msg.data[1] == '/')
{
struct buffer cmd = offset_buffer(msg, 2);
cmd_parse(cmd);
}
g_free(msg.data);
break;
}
packet_free(packet);
}
return NULL;
}
/* world file IO routines */
void world_regfile_sync_all(void)
{
/* FIXME testing code */
GHashTableIter region_iter;
struct region *region;
g_hash_table_iter_init(®ion_iter, region_table);
while (g_hash_table_iter_next(®ion_iter, NULL, (gpointer *)®ion))
{
world_regfile_sync(region);
}
}
#define SECTOR_SIZE 4096
static const char base36_chars[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
static char *base36_encode(jint value, char *buf, int bufsize)
{
buf[bufsize-1] = 0;
bufsize -= 2;
int neg = 0;
if (value < 0)
neg = 1, value = -value;
else if (value == 0)
{
buf[bufsize] = '0';
return buf+bufsize;
}
while (value && bufsize >= 0)
{
buf[bufsize--] = base36_chars[value % 36];
value /= 36;
}
if (neg && bufsize >= 0)
buf[bufsize--] = '-';
return buf + bufsize + 1;
}
static struct buffer compress_chunk(struct chunk *c)
{
/* dump the chunk data into compressed NBT */
struct nbt_tag *data = nbt_new_struct("Level");
nbt_struct_add(data, nbt_new_blob("Blocks", NBT_TAG_BLOB, c->blocks, CHUNK_NBLOCKS));
#ifdef FEAT_FULLCHUNK
nbt_struct_add(data, nbt_new_blob("Data", NBT_TAG_BLOB, c->meta, CHUNK_NBLOCKS/2));
nbt_struct_add(data, nbt_new_blob("BlockLight", NBT_TAG_BLOB, c->light_blocks, CHUNK_NBLOCKS/2));
nbt_struct_add(data, nbt_new_blob("SkyLight", NBT_TAG_BLOB, c->light_sky, CHUNK_NBLOCKS/2));
nbt_struct_add(data, nbt_new_blob("HeightMap", NBT_TAG_BLOB, c->height, CHUNK_XSIZE*CHUNK_ZSIZE)); /* TODO FIXME: indexing X/Z */
#endif /* FEAT_FULLCHUNK */
/* TODO: Entities, TileEntities */
nbt_struct_add(data, nbt_new_long("LastUpdate", 0));
nbt_struct_add(data, nbt_new_int("xPos", NBT_TAG_INT, c->key.x));
nbt_struct_add(data, nbt_new_int("zPos", NBT_TAG_INT, c->key.z));
nbt_struct_add(data, nbt_new_int("TerrainPopulated", NBT_TAG_BYTE, 1));
struct buffer buf = nbt_compress(data);
nbt_free(data);
return buf;
}
static void ensure_regfile(struct region *region)
{
if (region_path && !region->file)
{
/* not loaded from disk, so assume new file */
/* open/create, and flag all existing chunks in region as dirty */
char file_x_buf[16], file_z_buf[16];
char *file_x = base36_encode(region->key.x, file_x_buf, sizeof file_x_buf);
char *file_z = base36_encode(region->key.z, file_z_buf, sizeof file_z_buf);
char *file_path = g_strdup_printf("%s/r.%s.%s.mcr", region_path, file_x, file_z);
region->file = world_regfile_open(file_path);
for (jint cz = 0; cz < REGION_SIZE; cz++)
{
for (jint cx = 0; cx < REGION_SIZE; cx++)
{
/* NOTE: region->chunks in [cx][cz] order,
while file->dirty_chunks the opposite (in-disk-file) order */
if (region->chunks[cx][cz])
region->file->dirty_chunks[cz][cx] = 1;
}
}
}
}
struct region_file *world_regfile_open(const char *path)
{
/* open and/or create a new region file */
log_print("world_regfile_open: %s", path);
struct region_file *file = g_malloc0(sizeof *file);
file->fd = open(path, O_RDWR);
if (file->fd == -1)
{
if (errno != ENOENT)
dief("unable to read region file: %s: %s", path, g_strerror(errno));
/* the file does not seem to exist, so make a new empty one */
file->fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0666);
if (file->fd == -1)
dief("unable to create region file: %s: %s", path, g_strerror(errno));
if (ftruncate(file->fd, 2*SECTOR_SIZE) == -1)
dief("unable to prepare empty region file: %s: %s", path, g_strerror(errno));
file->nsect = 2;
file->sect_bitmap = g_byte_array_new();
g_byte_array_set_size(file->sect_bitmap, 1);
file->sect_bitmap->data[0] = 0x03; /* header sectors always taken */
}
else
{
/* file already existed, so scan index + length */
uint8_t header_loc[REGION_SIZE][REGION_SIZE][4];
uint8_t header_tstamp[REGION_SIZE][REGION_SIZE][4];
if (read(file->fd, header_loc, sizeof header_loc) != sizeof header_loc)
dief("unable to read region file header (loc): %s: %s", path, g_strerror(errno));
if (read(file->fd, header_tstamp, sizeof header_tstamp) != sizeof header_tstamp)
dief("unable to read region file header (tstamp): %s: %s", path, g_strerror(errno));
off_t len = lseek(file->fd, 0, SEEK_END);
if (len == (off_t)-1)
dief("unable to find size of region file: %s: %s", path, g_strerror(errno));
if (len < 2*SECTOR_SIZE)
dief("corrupted region file: %s: truncated header", path);
if (len % SECTOR_SIZE != 0)
{
/* not full multiple of sector size, round to avoid problems */
len += SECTOR_SIZE - (len%SECTOR_SIZE);
if (ftruncate(file->fd, len) == -1)
dief("unable to round region file to sector size: %s: %s", path, g_strerror(errno));
}
file->nsect = len / SECTOR_SIZE;
file->sect_bitmap = g_byte_array_sized_new((file->nsect + 7) / 8);
g_byte_array_set_size(file->sect_bitmap, (file->nsect + 7) / 8);
file->sect_bitmap->data[0] = 0x03;
/* parse the header and initialize the data structures */
for (jint z = 0; z < REGION_SIZE; z++)
{
for (jint x = 0; x < REGION_SIZE; x++)
{
file->offsets[z][x] = header_loc[z][x][0] << 16 | header_loc[z][x][1] << 8 | header_loc[z][x][2];
file->sects[z][x] = header_loc[z][x][3];
file->tstamps[z][x] = jint_read(header_tstamp[z][x]);
if (!file->offsets[z][x] || !file->sects[z][x])
continue; /* non-existing block, does not use sectors */
if (file->offsets[z][x] + file->sects[z][x] > file->nsect)
dief("corrupted region file: %s: chunk sectors beyond end of file", path);
for (unsigned s = file->offsets[z][x], sc = 0; sc < file->sects[z][x]; s++, sc++)
file->sect_bitmap->data[s/8] |= 1 << (s%8);
}
}
}
/* create shared memory mapping for file contents */
void *addr;
file->contents_map = make_mmap(file->fd, file->nsect*SECTOR_SIZE, &addr);
file->contents = addr;
if (!file->contents)
dief("unable to map region file to memory: %s: %s", path, g_strerror(errno));
return file;
}
void world_regfile_sync(struct region *region)
{
ensure_regfile(region);
struct region_file *file = region->file;
/* for each dirty chunk, serialize and try to put it into file */
unsigned old_nsect = file->nsect; /* used to notice size changes */
for (jint cz = 0; cz < REGION_SIZE; cz++)
{
for (jint cx = 0; cx < REGION_SIZE; cx++)
{
if (!file->dirty_chunks[cz][cx] || !region->chunks[cx][cz])
continue; /* not dirty */
struct buffer data = compress_chunk(region->chunks[cx][cz]);
/* write the data portion */
unsigned new_sects = (data.len + 5 + SECTOR_SIZE - 1) / SECTOR_SIZE;
if (new_sects <= file->sects[cz][cx])
{
/* fits in old slot; insert there */
unsigned char *bytes = &file->contents[file->offsets[cz][cx] * SECTOR_SIZE];
jint_write(bytes, data.len);
bytes[4] = 0x02; /* compression type: zlib */
memcpy(bytes + 5, data.data, data.len);
}
else
{
/* append to file */
uint8_t hdr[5];
jint_write(hdr, data.len);
hdr[4] = 0x02;
if (lseek(file->fd, 0, SEEK_END) == -1 ||
write(file->fd, hdr, 5) != 5 ||
write(file->fd, data.data, data.len) != data.len ||
ftruncate(file->fd, (file->nsect + new_sects)*SECTOR_SIZE) == -1)
dief("IO error when appending to region file: %s", g_strerror(errno));
/* update sector bitmap array size */
g_byte_array_set_size(file->sect_bitmap, (file->nsect + new_sects + 7) / 8);
}
/* alter the necessary header and other fields */
if (new_sects < file->sects[cz][cx])
{
for (unsigned s = file->offsets[cz][cx] + new_sects, sc = new_sects;
sc < file->sects[cz][cx];
s++, sc++)
file->sect_bitmap->data[s/8] &= ~(1 << (s%8));
file->sects[cz][cx] = new_sects;
file->contents[(cz*REGION_SIZE+cx)*4+3] = new_sects;
}
else if (new_sects > file->sects[cz][cx])
{
for (unsigned s = file->offsets[cz][cx], sc = 0; sc < file->sects[cz][cx]; s++, sc++)
file->sect_bitmap->data[s/8] &= ~(1 << (s%8));
file->offsets[cz][cx] = file->nsect;
file->sects[cz][cx] = new_sects;
unsigned char *bytes = &file->contents[(cz*REGION_SIZE+cx)*4];
bytes[0] = file->nsect >> 16;
bytes[1] = file->nsect >> 8;
bytes[2] = file->nsect;
bytes[3] = new_sects;
file->nsect += new_sects;
}
}
}
/* redo the file mapping if we have had to add new chunks;
otherwise just tell the system the file has changed */
if (file->nsect != old_nsect)
{
void *new_addr;
file->contents_map = resize_mmap(file->contents_map, file->contents, file->fd,
old_nsect*SECTOR_SIZE, file->nsect*SECTOR_SIZE, &new_addr);
if (!new_addr)
dief("unable to resize region file memory mapping: %s", g_strerror(errno));
file->contents = new_addr;
}
else
sync_mmap(file->contents, file->nsect*SECTOR_SIZE);
}
void world_regfile_load(struct region *region)
{
ensure_regfile(region);
struct region_file *file = region->file;
for (jint cz = 0; cz < REGION_SIZE; cz++)
{
for (jint cx = 0; cx < REGION_SIZE; cx++)
{
if (!file->offsets[cz][cx] || !file->sects[cz][cx])