-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCA-NBT-EDIT.CPP
2587 lines (2201 loc) · 110 KB
/
MCA-NBT-EDIT.CPP
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 "MCA-NBT-EDIT.HPP"
#include <winsock2.h>
WORLD_STRUCT world;
int RESET_TIME=1;
size_t tot_chunks_written_to_regionfiles=0;
size_t tot_chunks_written=0;
size_t tot_chunks_appended=0;
int SILENT=0;
extern double tied_total_total;
int MCA_MODE=0;
size_t XALLOC_MEM_START;
extern double PCFreq;
extern LARGE_INTEGER sys_freq;
extern int MC_running;
void test_tha_shit2();
void test_tha_shit(int n) {
PCFreq = 0.0; QueryPerformanceFrequency(&sys_freq);StartCounter();
system("cls");
for (int t=0; t<n; t++) test_tha_shit2();
}
void test_tha_shit2() {
tied=0.0;tied_total=0.0;
tot_chunks_written_to_regionfiles=0;
tot_chunks_written=0;
#ifdef SFML_ONBOARD
// for (int t=0; t<30; t++) {
// kiki();
// monitor_off=1;
// sf::sleep(sf::seconds(0.1));
// }
#endif
// xalloc_stats_quiet=1;
// xalloc_stats();
xalloc_get_tot_mem();
XALLOC_MEM_START=memory_total;
printf("memory_total=%u\n",memory_total);
// MCA_MODE|=LOAD_CHUNKS;
// MCA_MODE|=LOAD_BINARY;
// MCA_MODE|=LOAD_NBT;
/*
for (int n=0; n<7; n++) {
if (n==0) {
MCA_MODE=LOAD_BINARY;
} else if (n==1) {
MCA_MODE=LOAD_CHUNKS;
} else if (n==2) {
MCA_MODE=LOAD_NBT;
} else if (n==3) {
MCA_MODE=LOAD_BINARY|LOAD_CHUNKS;
} else if (n==4) {
MCA_MODE=LOAD_BINARY|LOAD_NBT;
} else if (n==5) {
MCA_MODE=LOAD_NBT|LOAD_CHUNKS;
} else if (n==6) {
MCA_MODE=LOAD_BINARY|LOAD_NBT|LOAD_CHUNKS;
}
*/
printf("___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________\n");
printf("LOADING: ");
if ((MCA_MODE&LOAD_BINARY)==LOAD_BINARY) printf("MCA ");
if ((MCA_MODE&LOAD_CHUNKS)==LOAD_CHUNKS) printf("CHUNKS ");
if ((MCA_MODE&LOAD_NBT)==LOAD_NBT) printf("NBT ");
xalloc_get_tot_mem();
printf("Memory start=%s ",pretty_number(XALLOC_MEM_START));
printf("Memory used=%s\n",pretty_number(memory_total-XALLOC_MEM_START)); //must be 0
printf("___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________\n");
// do_something_with_all("Synra/region");
// void test1(); test1();
void test2(); test2();
void test1(); test1();
/*
do_something_with_all("Synra/region");
erase_regions();
destroy(&world);
list_world(world,0);
*/
/*
MCA_MODE=LOAD_CHUNKS;
append_regions("Synra/region");
chunks_to_mca();
erase_chunks();
save_mcas();
erase_regions();
destroy(&world);
append_regions("Synra/region/new");
chunks_to_mca();
save_mcas();
erase_regions();
destroy(&world);
append_regions("Synra/region/new/new");
chunks_to_mca();
erase_chunks();
save_mcas();
erase_regions();
destroy(&world);
*/
/*
MCA_MODE=LOAD_CHUNKS;
append_regions("Synra/region");
print_chunks("txt");
erase_chunks();
destroy(&world);
MCA_MODE=LOAD_CHUNKS;
append_regions("Synra/region");
chunks_to_tags_erase_chunks_tags_to_chunks();
chunks_to_mca();
erase_chunks();
save_mcas();
erase_regions();
destroy(&world);
append_regions("Synra/region/new");
print_chunks("txt");
compare("Synra/region/new/txt","Synra/region/txt"); //proof of same nbt tags
erase_chunks();
destroy(&world);
*/
/* append and erase and destroy all. program.exe shit <#runs>
append_regions();
list_world(world,0);
system("timeout 2 >nul");
erase_regions();
list_world(world,0);
system("timeout 2 >nul");
append_regions();
list_world(world,0);
system("timeout 2 >nul");
erase_regions();
list_world(world,0);
system("timeout 2 >nul");
*/
/*
MCA_MODE=LOAD_NBT|LOAD_BINARY;
append_regions("Synra/region");
print_tags("txt");
save_mcas();
// destroy(&world);
erase_regions(); //erase whatever
MCA_MODE=LOAD_CHUNKS;
append_regions("Synra/region/new");
print_chunks("txt");
compare("Synra/region/new/txt","Synra/region/txt"); //proof of same nbt tags, NEEDS WORLD.REGIONS[] FILLED FOR FILENAMES.... :P REMOVE FRONT PATH... :P
erase_regions(); //erase whatever
destroy(&world);
*/
//save original chunks extracted from region binaries and unzipped from chunks to nbt tags to region nbt tag text file to disk.
/*
list_world(world,0);
MCA_MODE=LOAD_NBT;
append_regions("Synra/region");
print_tags("txt");
erase_tags();
destroy(&world);
//now flip flop little bit around.
//1) First load binary region .mca files into binary mem, with gaps between chunks.
//2) Extract chunks from .mca region binaries. (Can also been done while append_regions(), then or (|) MCA_MODE with flags, LOAD_NBT for nbt tags, LOAD_CHUNKS for chunks, LOAD_MCA for keeping binary, otherwise it will be deleted after extracting chunks and/or nbt's
//3) Erase the mca binaries (so we need the chunks)
//4) Extract nbt tags from chunks. Same story as above...
//5) Erase the chunks.
//6) Zip nbt tags back to chunks, when all deleted, all will be rezipped. If chunks are still there, and nbt not changed (see struct.), no update... for performance. But need to set change flags correctly when changing nbt tags
//7) Erase the tags.
//8) Pack chunks back into mca region binaries.
//9) Save .mca files back to disk, (into dir directory_path/new, next to region dir.)
//10) Erase the mca region binaries.
//11) Append saved mca region binaries back to nbt tags for printing
//12) Print them.
//13) Delete all region compounds, nbt's, chunks, mca's (only nbt's left...)
//14) Destroy world
list_world(world,0);
MCA_MODE=LOAD_BINARY;
append_regions("Synra/region");
mca_to_chunks();
erase_mcas();
chunks_to_tags();
erase_chunks();
tags_to_chunks();
erase_tags();
chunks_to_mca();
erase_chunks();
save_mcas();
erase_mcas();
destroy(&world); //clean for sure. No left pointers etc.
MCA_MODE=LOAD_CHUNKS;
append_regions("Synra/region/new"); //test, load mingled region files. (Mostly smaller, sometimes some large gaps in .mca files gone...)
print_chunks("txt"); //only chunks loaded this time, convert the to region nbt tags for printing. (also testing other print routine).
//will end up in Sunra/region/new/txt/r.*.*.txt ???
compare("Synra/region/new/txt","Synra/region/txt"); //proof of same nbt tags
erase_regions(); //erase whatever
list_world(world,0); //show erased
destroy(&world);
list_world(world,0); //show destroyed. (whole pointer index freed again.) (erase leaves empty region_struct for re-use) (need code). (many...)
*/
return; //use program.exe test <#loops>
}
int save_region_binary_to_file(REGION_STRUCT* region_struct) {
if (region_struct==0) { printf("Pointer to region_struct=0\n"); return 0; }
if (region_struct->free==1) { printf("Pointer to not used, but empty, reseted, and free\n"); return 0; }
if (region_struct->region_file_header==0) { printf("Pointer to region_file_header=0 file: %s\n",region_struct->filename); return 0; }
char* filename=region_struct->filename;
char directory_path[1024];
strcpy(directory_path,region_struct->directory_path);
strcat(directory_path,"../region.new");
if (mkdir(directory_path) != 0)
if (errno != EEXIST) { printf("Directory does not exist or can not create: %s\n",directory_path); return 0; }
char filename_write[1024];
sprintf(filename_write,"%s/%s",directory_path,filename);
if (!SILENT) printf("\r%6.1f %6.3f Saving: %s",tied_total,tied,filename_write);
size_t size=region_struct->size;
FILE *file;
if ((file = fopen(filename_write, "wb")) == NULL) { fprintf(stderr, "FATAL ERROR: unable to open file \"%s\" for writing region\n", filename_write); return 0; }
if (fwrite(region_struct->region_file_header, size, 1, file) != 1) { fprintf(stderr, "FATAL ERROR: unable to write region to file \"%s\"\n", filename_write); return 0; }
fclose(file);
strcpy(region_struct->status,"Saved");
list_region(region_struct);
return 1;
}
int print_region_from_chunks(REGION_STRUCT* region_struct,char* print_directory) {
if (region_struct==0) { printf("Pointer to region_struct=0\n"); return 0; }
if (region_struct->free==1) { printf("Pointer to not used, but empty, reseted, and free\n"); return 0; }
char* filename=region_struct->filename;
int ok=0;
for (int i = 0; i < CHUNKS_PER_REGION; i++) {
if (!(region_struct->columns_binary[i]==0)) { ok=1; break; }
}
if (ok==0) { printf("No binary chunks detected\n"); return 0; }
nbt_tag_t* tag_region=NULL;
tag_region = nbt_new_tag_compound();
nbt_set_tag_name(tag_region,region_struct->filename,strlen(region_struct->filename));
int32_t index[1024]={0};
nbt_tag_t* tag_index=NULL;
tag_index = nbt_new_tag_int_array(index,1024);
nbt_set_tag_name(tag_index,(char*)"index",strlen("index"));
nbt_tag_compound_append(tag_region,tag_index);
char name[100];
int chunk_count=0;
for (int i = 0; i < CHUNKS_PER_REGION; i++) {
if (region_struct->columns_binary[i]==0) {
tag_index->tag_int_array.value[i]=-1;
} else {
char *chunk;
tag_index->tag_int_array.value[i]=chunk_count+1;
chunk_count++;
RegionFileHeader* region_header=&(region_struct->region_header);
ChunkDataHeader* header_chunk=region_struct->header_chunk;
chunk=(char*)region_struct->columns_binary[i];
size_t length=region_struct->columns_binary_lenght[i];
unsigned char compression_type=header_chunk[i].compression_type;
nbt_tag_t* tag_chunk;
tag_chunk=NULL;
if (compression_type == COMPRESSION_TYPE_ZLIB) {
tag_chunk = read_nbt_from_memory((uint8_t*) chunk, length, NBT_PARSE_FLAG_USE_ZLIB);
} else if (compression_type == COMPRESSION_TYPE_GZIP) {
tag_chunk = read_nbt_from_memory((uint8_t*) chunk, length, NBT_PARSE_FLAG_USE_GZIP);
} else {
tag_chunk = read_nbt_from_memory((uint8_t*) chunk, length, NBT_PARSE_FLAG_USE_RAW);
}
nbt_tag_t* tag_timestamp = nbt_new_tag_long((int64_t)region_header->timestamps[i].timestamp);
nbt_set_tag_name(tag_timestamp, "timestamp" , strlen("timestamp"));
nbt_tag_compound_append(tag_chunk,tag_timestamp);
int regx,regz;
sscanf(region_struct->filename,"r.%d.%d.mca",®x,®z);
sprintf(name,"Chunk [%d, %d] in world at (%d, %d)",i%32,(int)(i/32), regx*32+i%32, regz*32+(int)(i/32));
nbt_set_tag_name(tag_chunk, (char*)name, strlen(name));
nbt_tag_compound_append(tag_region, tag_chunk);
tot_chunks_written++;
tied=GetCounterNanoseconds();
sprintf(name,"%s%s: Chunk [%2d, %2d] in world at (%5d, %5d)",region_struct->directory_path,filename,i%32,(int)(i/32), regx*32+i%32, regz*32+(int)(i/32));
printf("\r%6.1f %6.3f %s",tied_total,tied,name);
}
}
char directory_path[1024];
strcpy(directory_path,region_struct->directory_path);
strcat(directory_path,print_directory);
if (mkdir(directory_path) != 0)
if (errno != EEXIST) { printf("Directory does not exist or can not create: %s\n",directory_path); return 0; }
char filename_print[1024];
sprintf(filename_print,"%s/%s.txt",directory_path,region_struct->filename);
printf("\r%6.1f %6.3f Printing: %s ",tied_total,tied,filename_print);
printf("\r%6.1f %6.3f Printing: %s",tied_total,tied,filename_print);
print_nbt(tag_region,filename_print);
nbt_free_tag(tag_region);
strcpy(region_struct->status,"Printed");
list_region(region_struct);
return 1;
}
int print_region_from_tags(REGION_STRUCT* region_struct,char* print_directory) {
if (region_struct==0) { printf("Pointer to region_struct=0\n"); return 0; }
if (region_struct->free==1) { printf("Pointer to not used, but empty, reseted, and free\n"); return 0; }
if (region_struct->tag_region==0) {
{ printf("Tag region not loaded?\n"); return 0; }
}
char* filename=region_struct->filename;
char directory_path[1024];
strcpy(directory_path,region_struct->directory_path);
strcat(directory_path,print_directory);
if (mkdir(directory_path) != 0)
if (errno != EEXIST) { printf("Directory does not exist or can not create: %s\n",directory_path); return 0; }
char filename_print[1024];
sprintf(filename_print,"%s/%s.txt",directory_path,region_struct->filename);
printf("\r%6.1f %6.3f Printing: %s ",tied_total,tied,filename_print);
printf("\r%6.1f %6.3f Printing: %s",tied_total,tied,filename_print);
print_nbt(region_struct->tag_region,filename_print);
strcpy(region_struct->status,"Printed");
list_region(region_struct);
return 1;
}
void print_nbt(nbt_tag_t* tag,char* filename) {
FILE *textfile=fopen(filename,"wb");
if (!textfile==NULL) {
print_nbt_tree_to_file(tag, 0, textfile);
fclose(textfile);
} else { printf("print_nbt_to_file(): Can not open %s for writing\n"); }
return;
}
uint8_t* load_region_file_to_memory(char* filename, size_t* size) {
struct stat filestatus;
stat(filename, &filestatus );
size_t file_size=filestatus.st_size;
*size=file_size;
FILE* file=fopen(filename,"rb");
uint8_t* pointer_to_mem;
if (filename!=NULL) {
// pointer_to_mem=new uint8_t [file_size];
//hoppa
pointer_to_mem=(uint8_t*) NBT_MALLOC(file_size);
if (fread(pointer_to_mem, file_size, 1, file) != 1) { printf("FATAL ERROR: unable to open file \"%s\" for reading\n", filename); return 0; }
fclose(file);
return pointer_to_mem;
} else { printf("FATAL ERROR: unable to open file \"%s\" for reading\n", filename); return 0;}
}
REGION_STRUCT* append_mca_file_to_world(char* directorypath, char* file_name) {
char region_file_name[1024]="";
strcat(region_file_name,directorypath); //directory_path ... directorypath... otherway around as filename... but hey!, there are comments.
strcat(region_file_name,"/");
strcat(region_file_name,file_name); // filename ... file_name fix when get back from vacation...
REGION_STRUCT* region_struct;
RegionFileHeader* region_file_header;
// uint8_t* region_file_header;
size_t size;
if ((strstr(region_file_name, ".mca")) != NULL) {}
else { printf("No valid region filename (t.x.z.mca): %s\n" , region_file_name); return 0; }
if ((strstr(region_file_name, "r.")) != NULL) {}
else { printf("No valid region filename (r.x.z.mca): %s\n" , region_file_name); return 0; }
if (file_exists(region_file_name)) {}
else { printf("Region file %s does not exist\n",region_file_name); return 0; }
char* p_file;
char filename[100];
char directory_path[1024];
p_file=strstr(region_file_name, "r.");
strcpy(filename,p_file);
strcpy(directory_path,region_file_name);
p_file=strstr(directory_path, "r.");
*p_file=0;
int new_region=1;
int load_region=1;
//changed.....
int l;
for (l=0; l<world.number_of_regions; l++) {
if (!(world.regions[l]==0)) {
if ( strcmp(world.regions[l]->filename,filename)==0 && strcmp(world.regions[l]->directory_path,directory_path)==0 ) {
if (world.regions[l]->free==0) { //not free, in use, maybe erased, re-use
if (world.regions[l]->region_file_header==0) { load_region=1; } //load again....no binary.
else { load_region=0; } //still active, test if region binary exists, when so, re-use it. (not erased...),
} else { //freed, empty, erased, but structure still there, not freed, reuse
load_region=1;
}
new_region=0; //found region structure matching old used one and/or still filled one....
break;
} //otherwise make new region structure inside world
} else { //world.regions[l] freed (destroyed, not erased). new_region==1
}
}
xalloc_get_tot_mem();
long long int md=0;
if (load_region==1) {
if ( (region_file_header=(RegionFileHeader*) load_region_file_to_memory(region_file_name , &size)) !=0 ) {
xalloc_get_tot_mem();
md=memory_difference;
}
else { printf("Can not read region file %s\n",region_file_name); return 0; }
}
if (new_region==1) {
//KOEKOEK
// region_struct = NBT_MALLOC(sizeof(REGION_STRUCT*));
region_struct = (REGION_STRUCT*)NBT_MALLOC(sizeof(REGION_STRUCT));
for (uint8_t* m=(uint8_t*)region_struct; m<((uint8_t*)region_struct+sizeof(REGION_STRUCT)); m++) *m=0; //erase the memory???? what does the allocaoto do? Erase mem? So functions can steal re-used allocated data used by other functions/pointers....security thingy when used.
// region_struct = new REGION_STRUCT;
region_struct->free=0;
strcpy(region_struct->status,"Loaded");
xalloc_get_tot_mem();
world.world_total_memory_alloc+=memory_difference;
} else {
region_struct = world.regions[l];
if (region_struct==0) { // can not be 0 ... but...used, but now history...changed code above...
printf("Error\n");
region_struct = (REGION_STRUCT*)NBT_MALLOC(sizeof(REGION_STRUCT)); //nbt_freed when destroyed.....not when erased....struct then exists.
for (uint8_t* m=(uint8_t*)region_struct; m<((uint8_t*)region_struct+sizeof(REGION_STRUCT)); m++) *m=0; //erase the memory???? what does the allocaoto do? Erase mem? So functions can steal re-used allocated data used by other functions/pointers....security thingy when used.
xalloc_get_tot_mem();
world.world_total_memory_alloc+=memory_difference;
}
strcpy(region_struct->status,"Reloaded");
}
region_struct->binary_memory_alloc=md;
region_struct->binary_memory_freed=0;
region_struct->columns_total_binary_memory_freed=0;
region_struct->binary_changed=0;
if (load_region==1) {
region_struct->region_file_header=region_file_header;
region_struct->size=size;
strcpy(region_struct->filename,filename);
strcpy(region_struct->directory_path,directory_path);
}
for (int c=0; c<1024; c++) {
region_struct->columns_binary[c]=0;
region_struct->columns_binary_lenght[c]=0;
region_struct->header_chunk[c].compression_type=0;
region_struct->header_chunk[c].length[0]=0;
region_struct->header_chunk[c].length[1]=0;
region_struct->header_chunk[c].length[2]=0;
region_struct->header_chunk[c].length[3]=0;
region_struct->region_header.timestamps[c].timestamp=0;
region_struct->region_header.locations[c].offset[0]=0;
region_struct->region_header.locations[c].offset[1]=0;
region_struct->region_header.locations[c].offset[2]=0;
region_struct->region_header.locations[c].sector_count=0;
}
if ( ((MCA_MODE&LOAD_NBT)==LOAD_NBT) || ((MCA_MODE&LOAD_CHUNKS)==LOAD_CHUNKS) ) {
nbt_tag_t* tag_region;
// xalloc_get_tot_mem();
if ((tag_region=memory_mca_to_region_tag(region_struct)) != 0 ) {}
else {
if (!((MCA_MODE&LOAD_CHUNKS)==LOAD_CHUNKS)) {
printf("Can not convert region file %s to nbt\n",region_file_name); //error....
//hoppa
// delete[] region_file_header;
NBT_FREE(region_file_header);
region_file_header=0;
delete region_struct;
region_struct=0;
// NBT_FREE(region_struct);
return 0;
}
}
// xalloc_get_tot_mem();
if ((MCA_MODE&LOAD_NBT)==LOAD_NBT) {
region_struct->tag_region=tag_region;
nbt_set_tag_name(tag_region,region_struct->filename,strlen(region_struct->filename));
// region_struct->nbt_tag_memory_alloc=memory_difference;
}
}
if (!((MCA_MODE&LOAD_BINARY)==LOAD_BINARY)) {
xalloc_get_tot_mem();
//hoppa
// delete[] region_struct->region_file_header;
NBT_FREE(region_struct->region_file_header);
region_struct->region_file_header=0;
xalloc_get_tot_mem();
region_struct->binary_memory_freed=-memory_difference;
// region_struct->binary_memory_alloc=0;
region_struct->size=0;
}
if (new_region==1) {
xalloc_get_tot_mem();
world.regions=(REGION_STRUCT**)NBT_REALLOC( world.regions , (sizeof(REGION_STRUCT*) * (world.number_of_regions + 1) ) );
world.regions[world.number_of_regions]=region_struct;
world.regions[world.number_of_regions]->free=0;
world.regions[world.number_of_regions]->index_num=world.number_of_regions;
world.number_of_regions++;
xalloc_get_tot_mem();
world.world_total_memory_alloc+=memory_difference;
}
if (!SILENT) tied_total=tied_total+tied;
if (!SILENT) list_world(world,1);
// list_region(region_struct);
return region_struct;
}
REGION_STRUCT* list_struct_pointer=0;
REGION_STRUCT* erase_struct_pointer=0;
void list_region(REGION_STRUCT* region_struct) {
if (region_struct==0) { printf("list_region(): region_struct=0\n"); return; }
list_struct_pointer=region_struct;
list_world(world,-3);
}
int print_header=1;
void list_world(WORLD_STRUCT world, int last) {
int setting=0;
if (last==-5) {
last=0;
setting=1;
}
if (SILENT) return;
// tied=0.0; //tied_total=0.0;//print_header=1;
if (!setting) printf("\r");
tied=GetCounterNanoseconds();
// tied_total+=tied;
StartCounter();
size_t allocated_total=0;
size_t binary_size_total=0;
size_t binary_malloc_total=0;
size_t binary_freed_total=0;
size_t nbt_malloc_total=0;
size_t nbt_freed_total=0;
size_t chunks_total=0;
size_t chunks_size_total=0;
size_t chunks_freed_total=0;
size_t chunks_malloc_total=0;
if (last==0 ) {
printf("%6.1f %6.3f ",tied_total,tied);
printf("LISTING WORLD: %d entries",world.number_of_regions);
}
if (world.regions==0) {
printf(" No world, must be destroyed.\n");
if (last==0) printf("___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________\n");
return;
}
if (last==0) {
printf("\n");
}
if (last!=-3 || print_header==1 && setting==0) {
if (last==0 || print_header) {
print_header=0;
printf("%6.1f %6.3f ",tied_total,tied);
printf("Index: Free: Address binary: Binary size: Binary malloc: Binary free: Address nbt: Nbt malloc: Nbt free: Chunks: Memory chunks: Malloc chunks: Chunks free: Total malloc: Filename:\n");
}
}
int number_of_regions=world.number_of_regions;
for (int i=0; i<number_of_regions; i++) {
int num_columns;
num_columns=0;
size_t allocated_total_columns; allocated_total_columns=0;
size_t allocated_region; allocated_region=0;
size_t chunks_freed; chunks_freed=0;
size_t chunks_malloc; chunks_malloc=0;
REGION_STRUCT* region_struct;
if (last!=-3) region_struct=world.regions[i];
else region_struct=list_struct_pointer;
if ( ((!(last<0) ) && region_struct->free==0 ) || last==0 || last==-3 || last==-1) {
// if (!(last<0 && region_struct->free==1) || last==-3 || last==0) {
if (region_struct!=0) {
if (!(region_struct->region_file_header==0)) {
allocated_region+=region_struct->binary_memory_alloc;
allocated_total+= region_struct->binary_memory_alloc;
binary_size_total+=region_struct->size;
}
binary_malloc_total+=region_struct->binary_memory_alloc;
binary_freed_total+=region_struct->binary_memory_freed;
if (!(region_struct->tag_region==0)) {
allocated_region+=region_struct->nbt_tag_memory_alloc;
allocated_total+= region_struct->nbt_tag_memory_alloc;
}
nbt_malloc_total+=region_struct->nbt_tag_memory_alloc;
nbt_freed_total+=region_struct->nbt_tag_memory_freed;
chunks_freed=region_struct->columns_total_binary_memory_freed;
chunks_malloc=region_struct->columns_total_binary_memory_alloc;
for (int c=0; c<1024; c++) {
allocated_total_columns+=(size_t) region_struct->columns_binary_lenght[c];
if (region_struct->columns_binary_lenght[c]>0) { num_columns++; chunks_total++; }
}
if (num_columns>0) { // must be all allocated....
allocated_region+=chunks_malloc;
allocated_total+=chunks_malloc;
chunks_size_total+=allocated_total_columns;
}
chunks_freed_total+=chunks_freed;
chunks_malloc_total+=chunks_malloc;
}
// allocated_region+=allocated_total_columns;
// allocated_total+=allocated_total_columns;
if ( (last==1 && i==world.number_of_regions - 1) || last==0 || last==-2 || last==-3) {
printf("\r%6.1f %6.3f ",tied_total,tied);
printf("%6d ", region_struct->index_num );
printf("%5d ", region_struct->free );
printf("%16p", region_struct->region_file_header ); if (region_struct->binary_changed) printf("* "); else printf(" ");
printf("%16s ", pretty_number( region_struct->size ) );
printf("%16s ", pretty_number( region_struct->binary_memory_alloc) );
printf("%16s ", pretty_number( region_struct->binary_memory_freed) );
printf("%16p", region_struct->tag_region ); if (region_struct->one_or_more_columns_tag_changed) printf("* "); else printf(" ");
printf("%16s ", pretty_number( region_struct->nbt_tag_memory_alloc ) );
printf("%16s ", pretty_number( region_struct->nbt_tag_memory_freed ) );
printf(" %4d", num_columns ); if (region_struct->one_or_more_columns_binary_changed) printf("* "); else printf(" ");
printf("%16s ", pretty_number( allocated_total_columns ) );
printf("%16s ", pretty_number( chunks_malloc ) );
printf("%16s ", pretty_number( chunks_freed ) );
printf("%16s ", pretty_number( allocated_region ) );
if (strlen(region_struct->filename)==0 && strlen(region_struct->directory_path)==0)
printf("%s\n" , region_struct->status); //saving one space and 2 printing conversions, but costs two compares by pointer...
else
printf("%s%s %s\n", region_struct->directory_path , region_struct->filename , region_struct->status);
printf("%6.1f %6.3f ",tied_total,tied);
}
if (last==-3) break;
}
}
if (last==-3) { return; }
// printf("\nIndex: Free: Address binary: Binary size: Binary malloc: Binary free: Address nbt: Nbt malloc: Nbt free: Chunks: Memory chunks: Malloc chunks: Chunks free: Total malloc: Filename:\n");
if (last!=0) printf(" ");
if (last<=0) printf(" ---------------- ---------------- ---------------- ---------------- ---------------- ------- ---------------- ---------------- ---------------- ----------------\n");
// else printf(" \r");
// printf("%6.3f %6.3f\n",tied_total,tied);
// if (last==1)
// printf("%6.3f %6.3f ",tied_total,tied);
// if (last!=0) printf("");
if (last!=1){
printf(" ");
}
printf(" "); printf(" ");
printf("%16s ", pretty_number( binary_size_total ) );
printf("%16s ", pretty_number( binary_malloc_total) );
printf("%16s ", pretty_number( binary_freed_total) );
printf(" ");
printf("%16s ", pretty_number( nbt_malloc_total ) );
printf("%16s ", pretty_number( nbt_freed_total ) );
printf("%7d ", chunks_total );
printf("%16s ", pretty_number( chunks_size_total ) );
printf("%16s ", pretty_number( chunks_malloc_total ) );
printf("%16s ", pretty_number( chunks_freed_total ) );
printf("%16s" , pretty_number( allocated_total ) );
if (last!=1 ) printf(" (=TOTAL REGIONS MEMORY ALLOCATED)" );
if (last==0 || last==-1) printf("\n"); else printf("\r");
xalloc_get_tot_mem();
if (last==0 || last==-1) {
char world_used[100]; strcpy(world_used , pretty_number(world.world_total_memory_alloc-world.world_total_memory_freed) );
char regions_used[100]; strcpy(regions_used, pretty_number(memory_total - XALLOC_MEM_START - (size_t)world.world_total_memory_alloc + (size_t)world.world_total_memory_freed ) );
char start_used[100]; strcpy(start_used, pretty_number(XALLOC_MEM_START ) );
char total_used[100]; strcpy(total_used, pretty_number(memory_total ) );
// printf(" ");
char tot_string[200];
if (allocated_total==memory_total - XALLOC_MEM_START - (size_t)world.world_total_memory_alloc + (size_t)world.world_total_memory_freed)
sprintf(tot_string,"MATCH! <IDidMakeThat> %s (TOTAL) - %s (START) - %s (WORLD) = %s (REGIONS)", total_used, start_used, world_used, regions_used );
else
sprintf(tot_string,"!!! MEMORY LEAK !!! %s (TOTAL) - %s (START) - %s (WORLD) = %s (REGIONS)", total_used, start_used, world_used, regions_used );
int len=245-strlen(tot_string);
for (int stripe=0; stripe<len; stripe++) printf(" ");
printf(tot_string); // printf("\n");
// printf(" ");
// printf("%16s (REGIONS USED)\n", regions_used );
// printf(" ");
// printf("%16s (WORLD USED)\n", world_used );
}
int xsqo;
if (last==0 || last==-1) { xsqo=xalloc_stats_quiet; xalloc_stats_quiet=1; }
if (last==0 || last==-1) xalloc_stats();
if (last==0 || last==-1) xalloc_stats_quiet=xsqo;
// if (last==0 || last==-1) printf(" %16s (XALLOC NOW)\n",
// pretty_number( xalloc_total_memory ) );
if (last<=0) {
print_header=1;
}
if (last==0) printf("\n___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________\n");
return;
}
void erase_tag(REGION_STRUCT* region_struct) {
if (region_struct==0) { printf("erase_region(): region_struct=0\n"); return; }
erase_struct_pointer=region_struct;
strcpy(region_struct->status,"Erased");
erase_all_tags(world);
erase_struct_pointer=0;
}
void erase_all_tags(WORLD_STRUCT world) {
for (int i=0; i<world.number_of_regions; i++) {
REGION_STRUCT* region_struct;
if (!(erase_struct_pointer==0)) region_struct=erase_struct_pointer;
else region_struct=world.regions[i];
if (region_struct->free==0) {
region_struct->nbt_tag_memory_alloc=0;
if (region_struct->tag_region!=0) {
xalloc_get_tot_mem();
nbt_free_tag(region_struct->tag_region);
region_struct->tag_region=0;
xalloc_get_tot_mem();
region_struct->nbt_tag_memory_freed=-memory_difference;
for (int c=0; c<1024; c++) {
region_struct->nbt_columns_tag_changed[c]=0;
}
region_struct->one_or_more_columns_tag_changed=0;
}
tied_total=tied_total+tied;
}
if (erase_struct_pointer!=0) break;
}
return;
}
void erase_chunk(REGION_STRUCT* region_struct) {
if (region_struct==0) { printf("erase_region(): region_struct=0\n"); return; }
erase_struct_pointer=region_struct;
erase_all_chunks(world);
erase_struct_pointer=0;
}
void erase_all_chunks(WORLD_STRUCT world) {
for (int i=0; i<world.number_of_regions; i++) {
REGION_STRUCT* region_struct;
if (!(erase_struct_pointer==0)) region_struct=erase_struct_pointer;
else region_struct=world.regions[i];
if (region_struct!=0) {
if (region_struct->free==0) {
xalloc_get_tot_mem();
region_struct->columns_total_binary_memory_alloc=0;
int hit=0;
for (int c=0; c<1024; c++) {
if (!(region_struct->columns_binary[c]==0)) { hit=1; NBT_FREE(region_struct->columns_binary[c]); }
region_struct->columns_binary[c]=0;
region_struct->columns_binary_lenght[c]=0;
region_struct->header_chunk[c].length[0]=0;
region_struct->header_chunk[c].length[1]=0;
region_struct->header_chunk[c].length[2]=0;
region_struct->header_chunk[c].length[3]=0;
region_struct->header_chunk[c].compression_type=0;
region_struct->columns_binary_changed[c]=0;
region_struct->region_header.locations[c].offset[0]=0;
region_struct->region_header.locations[c].offset[1]=0;
region_struct->region_header.locations[c].offset[2]=0;
region_struct->region_header.locations[c].sector_count=0;
region_struct->region_header.timestamps[c].timestamp=0;
region_struct->columns_binary_changed[c]=0;
}
region_struct->one_or_more_columns_binary_changed;
if (hit==1) {
xalloc_get_tot_mem();
region_struct->columns_total_binary_memory_freed=-memory_difference;
}
}
tied_total=tied_total+tied;
}
if (erase_struct_pointer!=0) break;
}
return;
}
void erase_mca(REGION_STRUCT* region_struct) {
if (region_struct==0) { printf("erase_region(): region_struct=0\n"); return; }
erase_struct_pointer=region_struct;
erase_all_mcas(world);
erase_struct_pointer=0;
}
void erase_all_mcas(WORLD_STRUCT world) {
for (int i=0; i<world.number_of_regions; i++) {
REGION_STRUCT* region_struct;
if (!(erase_struct_pointer==0)) region_struct=erase_struct_pointer;
else region_struct=world.regions[i];
if (region_struct->free==0) {
region_struct->binary_memory_alloc=0;
if (region_struct->region_file_header!=0) {
xalloc_get_tot_mem();
NBT_FREE(region_struct->region_file_header);
region_struct->region_file_header=0;
xalloc_get_tot_mem();
region_struct->binary_memory_freed=-memory_difference;
region_struct->size=0;
region_struct->binary_changed=0;
}
tied_total=tied_total+tied;
}
if (erase_struct_pointer!=0) break;
}
return;
}
void erase_region(REGION_STRUCT* region_struct) {
if (region_struct==0) { printf("erase_region(): region_struct=0\n"); return; }
erase_struct_pointer=region_struct;
erase_world(world);
erase_struct_pointer=0;
}
void erase_world(WORLD_STRUCT world) {
for (int i=0; i<world.number_of_regions; i++) {
REGION_STRUCT* region_struct;
if (!(erase_struct_pointer==0)) region_struct=erase_struct_pointer;
else region_struct=world.regions[i];
if (region_struct!=0) {
if (region_struct->free==0) {
region_struct->binary_memory_alloc=0;
if (!(region_struct->region_file_header==0)) {
NBT_FREE(region_struct->region_file_header);
region_struct->region_file_header=0;
xalloc_get_tot_mem();
region_struct->binary_memory_freed=-memory_difference;
region_struct->size=0;
region_struct->binary_changed=0;
}
xalloc_get_tot_mem();
region_struct->columns_total_binary_memory_alloc=0;
int hit=0;
for (int c=0; c<1024; c++) {
if (!(region_struct->columns_binary[c]==0)) { hit=1; NBT_FREE(region_struct->columns_binary[c]); }
region_struct->columns_binary[c]=0;
region_struct->columns_binary_lenght[c]=0;
region_struct->header_chunk[c].length[0]=0;
region_struct->header_chunk[c].length[1]=0;
region_struct->header_chunk[c].length[2]=0;
region_struct->header_chunk[c].length[3]=0;
region_struct->header_chunk[c].compression_type=0;
region_struct->columns_binary_changed[c]=0;
region_struct->region_header.locations[c].offset[0]=0;
region_struct->region_header.locations[c].offset[1]=0;
region_struct->region_header.locations[c].offset[2]=0;
region_struct->region_header.locations[c].sector_count=0;
region_struct->region_header.timestamps[c].timestamp=0;
region_struct->columns_binary_changed[c]=0;
}
region_struct->one_or_more_columns_binary_changed;
if (hit==1) {
xalloc_get_tot_mem();
region_struct->columns_total_binary_memory_freed=-memory_difference;
}
region_struct->nbt_tag_memory_alloc=0;
if (region_struct->tag_region!=0) {
xalloc_get_tot_mem();
nbt_free_tag(region_struct->tag_region);
region_struct->tag_region=0;
xalloc_get_tot_mem();
region_struct->nbt_tag_memory_freed=-memory_difference;
for (int c=0; c<1024; c++) {
region_struct->nbt_columns_tag_changed[c]=0;
}
region_struct->one_or_more_columns_tag_changed=0;
}
}
region_struct->free=1;
tied_total=tied_total+tied;
}
// for (uint8_t* m=(uint8_t*)region_struct; m<((uint8_t*)region_struct+sizeof(REGION_STRUCT)); m++) *m=0; // kill it. For later use....
if (erase_struct_pointer!=0) break;
}
return;
}
nbt_tag_t* memory_mca_to_region_tag(REGION_STRUCT* region_struct)
{
if (region_struct==0) { printf("Pointer to region_struct=0\n"); return 0; }
// if (region_struct->free==1) { printf("Pointer to not used, but empty, reseted, and free\n"); return 0; }
if (region_struct->tag_region!=0) {
{ printf("Nbt tag not empty. Delete first\n"); return 0; }
}
char* filename=region_struct->filename;
RegionFileHeader* region_file_header=(RegionFileHeader*) region_struct->region_file_header;
RegionFileHeader* region_header=&(region_struct->region_header);
if (region_file_header==0) { printf("Pointer to region_file_header=0 file: %s\n",filename); return 0; }
for (int i = 0; i < CHUNKS_PER_REGION; i++) {
if (region_struct->columns_binary[i]!=0) { printf("Columns allocated. Not empty. Delete 'chunks' first\n"); return 0; }
}
int i;
int32_t index[1024]={0};
nbt_tag_t* tag_region;
nbt_tag_t* tag_index;
if ((MCA_MODE&LOAD_NBT)==LOAD_NBT) {
xalloc_get_tot_mem();
region_struct->nbt_tag_memory_alloc=0;