-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCFile.cpp
More file actions
1504 lines (1264 loc) · 46.7 KB
/
Copy pathCFile.cpp
File metadata and controls
1504 lines (1264 loc) · 46.7 KB
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
// Copyright (C) 2009 - 2021 Marc Vester (XaserLE)
// Copyright (C) 2009 - 2024 Settlers Freaks <sf-team at siedler25.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "CFile.h"
#include "../CSurface.h"
#include "../globals.h"
#include "libendian/libendian.h"
#include "s25util/file_handle.h"
#include <boost/endian/conversion.hpp>
#include <boost/nowide/cstdio.hpp>
#include <iostream>
#include <memory>
#include <stdexcept>
//-V:fseek:303
//-V:ftell:303
bobBMP* CFile::bmpArray = nullptr;
bobSHADOW* CFile::shadowArray = nullptr;
bobPAL* CFile::palArray = nullptr;
bobPAL* CFile::palActual = nullptr;
bool CFile::loadPAL = false;
#define STRINGIZE(x) STRINGIZE2(x)
#define STRINGIZE2(x) #x
#define LINE_STRING STRINGIZE(__LINE__)
#define CHECK_READ(readCmd) \
if(!(readCmd)) \
throw std::runtime_error("Read failed at line " LINE_STRING)
void CFile::init()
{
bmpArray = global::bmpArray.data();
shadowArray = global::shadowArray.data();
palArray = global::palArray.data();
palActual = global::palArray.data();
loadPAL = false;
}
void* CFile::open_file(const boost::filesystem::path& filepath, char filetype, bool only_loadPAL)
{
void* return_value = nullptr;
if(filepath.empty() || !bmpArray || !shadowArray || !palArray || !palActual)
return nullptr;
s25util::file_handle fh(boost::nowide::fopen(filepath.string().c_str(), "rb"));
if(!fh)
return nullptr;
if(only_loadPAL)
loadPAL = true;
try
{
switch(filetype)
{
case LST:
if(read_lst(*fh))
return_value = (void*)-1;
break;
case BOB:
if(read_bob(*fh))
return_value = (void*)-1;
break;
case IDX:
if(open_idx(filepath))
return_value = (void*)-1;
break;
case BBM:
if(read_bbm(*fh))
return_value = (void*)-1;
break;
case LBM:
if(read_lbm(*fh, filepath))
return_value = (void*)-1;
break;
case GOU:
if(read_gou(*fh))
return_value = (void*)-1;
break;
case WLD: return_value = read_wld(*fh); break;
case SWD: return_value = read_swd(*fh); break;
default: // no valid data type
break;
}
} catch(const std::exception& e)
{
std::cerr << "Error while reading " << filepath << ": " << e.what() << std::endl;
}
loadPAL = false;
return return_value;
}
bool CFile::read_lst(FILE* fp)
{
// type of entry (used or unused entry)
Uint16 entrytype;
// bobtype of the entry
Uint16 bobtype;
// skip: id (2x 1 Bytes) + count (1x 4 Bytes) = 6 Bytes
fseek(fp, 6, SEEK_SET);
// main loop for reading entries
while(!feof(fp))
{
// entry type (2 Bytes) - unused (=0x0000) or used (=0x0001) -
if(!libendian::le_read_us(&entrytype, fp))
{
if(feof(fp))
break;
CHECK_READ(false);
}
// if entry is unused, go back to 'while' --- and by the way: after the last entry there are always zeros in the
// file, so the following case will happen till we have reached the end of the file and the 'while' will break -
// PERFECT!
if(entrytype == 0x0000)
continue;
// bobtype (2 Bytes)
CHECK_READ(libendian::le_read_us(&bobtype, fp));
switch(bobtype)
{
case BOBTYPE01:
if(!read_bob01(fp))
return false;
break;
case BOBTYPE02:
if(!read_bob02(fp))
return false;
break;
case BOBTYPE03:
if(!read_bob03(fp))
return false;
break;
case BOBTYPE04:
if(!read_bob04(fp, PLAYER_BLUE))
return false;
break;
case BOBTYPE05:
if(!read_bob05(fp))
return false;
break;
case BOBTYPE07:
if(!read_bob07(fp))
return false;
break;
case BOBTYPE14:
if(!read_bob14(fp))
return false;
break;
default: // Something is wrong? Maybe the last entry was really the LAST, so we should not return false
break;
}
}
return true;
}
bool CFile::read_bob(FILE*)
{
return false;
}
bool CFile::open_idx(const boost::filesystem::path& filepath)
{
// starting adress of data in the corresponding '******.DAT'-File
Uint32 offset;
// bobtype of the entry
Uint16 bobtype;
// bobtype is repeated in the corresponding '******.DAT'-File, so we have to check this is correct
Uint16 bobtype_check;
// '******.IDX'-File
const s25util::file_handle fh_idx(boost::nowide::fopen(filepath.string().c_str(), "rb"));
// corresponding '******.DAT'-File
auto filename_dat = filepath;
filename_dat.replace_extension(".DAT");
const s25util::file_handle fh_dat(boost::nowide::fopen(filename_dat.string().c_str(), "rb"));
if(!fh_idx || !fh_dat)
return false;
// we are finished opening the files now we can handle the content
// skip: unknown data (1x 4 Bytes) at the beginning of the file
fseek(*fh_idx, 4, SEEK_SET);
// main loop for reading entries
while(!feof(*fh_idx) && !feof(*fh_dat))
{
// skip: name (1x 16 Bytes)
fseek(*fh_idx, 16, SEEK_CUR);
// offset (4 Bytes)
if(!libendian::le_read_ui(&offset, *fh_idx))
{
if(feof(*fh_idx))
break;
CHECK_READ(false);
}
// skip unknown data (6x 1 Byte)
fseek(*fh_idx, 6, SEEK_CUR);
// bobtype (2 Bytes)
CHECK_READ(libendian::le_read_us(&bobtype, *fh_idx));
// set fp_dat to the position in 'offset'
fseek(*fh_dat, offset, SEEK_SET);
// read bobtype again, now from 'DAT'-File
CHECK_READ(libendian::le_read_us(&bobtype_check, *fh_dat));
// check if data in 'DAT'-File is the data that it should be (bobtypes are equal)
if(bobtype != bobtype_check)
return false;
switch(bobtype)
{
case BOBTYPE01:
if(!read_bob01(*fh_dat))
return false;
break;
case BOBTYPE02:
if(!read_bob02(*fh_dat))
return false;
break;
case BOBTYPE03:
if(!read_bob03(*fh_dat))
return false;
break;
case BOBTYPE04:
if(!read_bob04(*fh_dat))
return false;
break;
case BOBTYPE05:
if(!read_bob05(*fh_dat))
return false;
break;
case BOBTYPE07:
if(!read_bob07(*fh_dat))
return false;
break;
case BOBTYPE14:
if(!read_bob14(*fh_dat))
return false;
break;
default: // Something is wrong? Maybe the last entry was really the LAST, so we should not return false
break;
}
}
return true;
}
bool CFile::read_bbm(FILE* fp)
{
// skip header (48 Bytes)
fseek(fp, 48, SEEK_CUR);
for(auto& color : palArray->colors)
{
CHECK_READ(libendian::read(&(color.r), 1, fp));
CHECK_READ(libendian::read(&(color.g), 1, fp));
CHECK_READ(libendian::read(&(color.b), 1, fp));
color.a = 255;
}
palArray++;
return true;
}
bool CFile::read_lbm(FILE* fp, const boost::filesystem::path& filepath)
{
// IMPORTANT NOTE: LBM (also ILBM) is the Interchange File Format (IFF) and the 4-byte-blocks are originally
// organized as Big Endian, so a convertion is needed
// identifier for the kind of data follows (FORM, BMHD, CMAP, BODY)
std::array<char, 5> chunk_identifier;
// length of data block
Uint32 length;
// color depth of the picture
Uint16 color_depth;
// is the picture rle-compressed?
Uint8 compression_flag;
// compression type
Sint8 ctype;
// array for palette colors
std::array<SDL_Color, 256> colors;
// color value for read pixel
Uint8 color_value;
// skip File-Identifier "FORM" (1x 4 Bytes) + unknown data (4x 1 Byte) + Header-Identifier "PBM " (1x 4 Bytes) = 12
// Bytes
fseek(fp, 12, SEEK_CUR);
/* READ FIRST CHUNK "BMHD" */
// chunk-identifier (4 Bytes)
CHECK_READ(libendian::read(chunk_identifier.data(), 4, fp));
chunk_identifier[4] = '\0';
// should be "BMHD" at this time
if(strcmp(chunk_identifier.data(), "BMHD") != 0)
return false;
// length of data block
CHECK_READ(libendian::be_read_ui(&length, fp));
// width of picture (2 Bytes)
CHECK_READ(libendian::be_read_us(&(bmpArray->w), fp));
// heigth of picture (2 Bytes)
CHECK_READ(libendian::be_read_us(&(bmpArray->h), fp));
// skip unknown data (4x 1 Bytes)
fseek(fp, 4, SEEK_CUR);
// color depth of the picture (1x 2 Bytes)
CHECK_READ(libendian::be_read_us(&color_depth, fp));
// compression_flag (1x 1 Bytes)
CHECK_READ(libendian::read(&compression_flag, 1, fp));
fseek(fp, 9, SEEK_CUR);
// skip unknown data (length - 20 x 1 Byte)
// fseek(fp, length-20, SEEK_CUR);
const Uint16 tilesetSlot = static_cast<Uint16>(bmpArray - global::bmpArray.data());
/* READ SECOND CHUNK "CMAP" */
// chunk-identifier (4 Bytes)
// search for the "CMAP" and skip other chunk-types
while(!feof(fp))
{
CHECK_READ(libendian::read(chunk_identifier.data(), 4, fp));
if(strcmp(chunk_identifier.data(), "CMAP") == 0)
break;
else
{
Uint32 chunkLen;
CHECK_READ(libendian::be_read_ui(&chunkLen, fp));
if(chunkLen & 1)
chunkLen++;
fseek(fp, chunkLen, SEEK_CUR);
}
}
if(feof(fp))
return false;
// length of data block
CHECK_READ(libendian::be_read_ui(&length, fp));
// must be 768 (RGB = 3 Byte x 256 Colors)
if(length != 3u * 256u)
return false;
// palette
for(auto& color : colors)
{
CHECK_READ(libendian::read(&color.r, 1, fp));
CHECK_READ(libendian::read(&color.g, 1, fp));
CHECK_READ(libendian::read(&color.b, 1, fp));
}
/* READ THIRD CHUNK "BODY" */
// chunk-identifier (4 Bytes)
// search for the "BODY" and skip other chunk-types
while(!feof(fp))
{
CHECK_READ(libendian::read(chunk_identifier.data(), 4, fp));
if(strcmp(chunk_identifier.data(), "BODY") == 0)
break;
else if(strcmp(chunk_identifier.data(), "CRNG") == 0)
{
Uint32 chunkLen;
CHECK_READ(libendian::be_read_ui(&chunkLen, fp));
if(chunkLen >= 8)
{
uint16_t padding, rate, flags;
CHECK_READ(libendian::be_read_us(&padding, fp));
CHECK_READ(libendian::be_read_us(&rate, fp));
CHECK_READ(libendian::be_read_us(&flags, fp));
uint8_t firstClr, lastClr;
CHECK_READ(libendian::read(&firstClr, 1, fp));
CHECK_READ(libendian::read(&lastClr, 1, fp));
// Only register valid animations: at least 2 colors and non-zero rate
if(rate > 0 && lastClr > firstClr)
{
PaletteAnimation anim;
anim.moveUp = (flags & 2) != 0;
if(rate)
anim.moveUp = true;
anim.rate = rate;
anim.firstClr = firstClr;
anim.lastClr = lastClr;
anim.currentOffset = 0;
anim.lastAppliedOffset = 0;
anim.lastUpdateTime = SDL_GetTicks();
switch(tilesetSlot)
{
case TILESET_GREENLAND_8BPP: global::paletteAnimations[MAP_GREENLAND].push_back(anim); break;
case TILESET_WASTELAND_8BPP: global::paletteAnimations[MAP_WASTELAND].push_back(anim); break;
case TILESET_WINTERLAND_8BPP: global::paletteAnimations[MAP_WINTERLAND].push_back(anim); break;
default: break;
}
}
if(chunkLen > 8)
{
uint32_t remaining = chunkLen - 8;
if(remaining & 1)
remaining++;
fseek(fp, remaining, SEEK_CUR);
} else if(chunkLen & 1)
fseek(fp, 1, SEEK_CUR);
} else
{
if(chunkLen & 1)
chunkLen++;
fseek(fp, chunkLen, SEEK_CUR);
}
} else
{
Uint32 chunkLen;
CHECK_READ(libendian::be_read_ui(&chunkLen, fp));
if(chunkLen & 1)
chunkLen++;
fseek(fp, chunkLen, SEEK_CUR);
}
}
if(feof(fp))
return false;
// length of data block
CHECK_READ(libendian::be_read_ui(&length, fp));
// now we are ready to read the picture lines and fill the surface, so lets create one
if(!(bmpArray->surface = makePalSurface(bmpArray->w, bmpArray->h, colors)))
{
std::cerr << "Failed to create surface: " << SDL_GetError() << std::endl;
return false;
}
if(compression_flag == 0)
{
// picture uncompressed
// main loop for reading picture lines
for(Position pos{0, 0}; pos.y < bmpArray->h; pos.y++)
{
// loop for reading pixels of the actual picture line
for(pos.x = 0; pos.x < bmpArray->w; pos.x++)
{
// read color value (1 Byte)
CHECK_READ(libendian::read(&color_value, 1, fp));
// draw
CSurface::DrawPixel_Color(bmpArray->surface.get(), pos, (Uint32)color_value);
}
}
} else if(compression_flag == 1)
{
// picture compressed
// main loop for reading picture lines
for(Position pos{0, 0}; pos.y < bmpArray->h; pos.y++)
{
// loop for reading pixels of the actual picture line
//(cause of a kind of RLE-Compression we cannot read the pixels sequentially)
//'x' will be incremented WITHIN the loop
for(pos.x = 0; pos.x < bmpArray->w;)
{
// read compression type
CHECK_READ(libendian::read(&ctype, 1, fp));
if(ctype >= 0)
{
// following 'ctype + 1' pixels are uncompressed
for(int k = 0; k < ctype + 1; k++, pos.x++)
{
// read color value (1 Byte)
CHECK_READ(libendian::read(&color_value, 1, fp));
// draw
CSurface::DrawPixel_Color(bmpArray->surface.get(), pos, (Uint32)color_value);
}
} else if(ctype >= -127)
{
// draw the following byte '-ctype + 1' times to the surface
CHECK_READ(libendian::read(&color_value, 1, fp));
for(int k = 0; k < -ctype + 1; k++, pos.x++)
CSurface::DrawPixel_Color(bmpArray->surface.get(), pos, (Uint32)color_value);
} else // if (ctype == -128)
{
// ignore
}
}
}
} else
return false;
// if this is a texture file, we need a secondary 32-bit surface for SGE and we set a color key at both surfaces
if(filepath.filename() == "TEX5.LBM" || filepath.filename() == "TEX6.LBM" || filepath.filename() == "TEX7.LBM"
|| filepath.filename() == "TEXTUR_0.LBM" || filepath.filename() == "TEXTUR_3.LBM")
{
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
bmpArray++;
if((bmpArray->surface = makeRGBSurface((bmpArray - 1)->w, (bmpArray - 1)->h)))
{
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
CSurface::Draw(bmpArray->surface, (bmpArray - 1)->surface);
} else
bmpArray--;
}
// we are finished, the surface is filled
// increment bmpArray for the next picture
bmpArray++;
return true;
}
bool CFile::read_gou(FILE* fp)
{
static int internalArrayCtr = 0; // maximum is two cause there are only 3 GOUx.DAT-Files
if(internalArrayCtr > 2)
return false;
CHECK_READ(libendian::read(gouData[internalArrayCtr][0], sizeof(gouData[internalArrayCtr]), fp));
internalArrayCtr++;
return true;
}
bobMAP* CFile::read_wld(FILE* fp)
{
auto myMap = std::make_unique<bobMAP>();
std::array<char, 20> tmpNameAuthor;
fseek(fp, 10, SEEK_SET);
tmpNameAuthor.fill('\0');
CHECK_READ(libendian::read(tmpNameAuthor, fp));
myMap->setName(tmpNameAuthor.data());
CHECK_READ(libendian::le_read_us(&myMap->width_old, fp));
CHECK_READ(libendian::le_read_us(&myMap->height_old, fp));
uint8_t mapType;
CHECK_READ(libendian::read(&mapType, 1, fp));
myMap->type = MapType(mapType);
CHECK_READ(libendian::read(&myMap->player, 1, fp));
tmpNameAuthor.fill('\0');
CHECK_READ(libendian::read(tmpNameAuthor, fp));
myMap->setAuthor(tmpNameAuthor.data());
for(unsigned short& i : myMap->HQx)
CHECK_READ(libendian::le_read_us(&i, fp));
for(unsigned short& i : myMap->HQy)
CHECK_READ(libendian::le_read_us(&i, fp));
// go to big map header and read it
fseek(fp, 92, SEEK_SET);
for(auto& i : myMap->header)
{
CHECK_READ(libendian::read(&i.type, 1, fp));
CHECK_READ(libendian::le_read_us(&i.x, fp));
CHECK_READ(libendian::le_read_us(&i.y, fp));
CHECK_READ(libendian::le_read_ui(&i.area, fp));
}
// go to real map height and width
fseek(fp, 2348, SEEK_SET);
CHECK_READ(libendian::le_read_us(&myMap->width, fp));
CHECK_READ(libendian::le_read_us(&myMap->height, fp));
myMap->vertex.resize(myMap->width * myMap->height);
// go to altitude information (we skip the 16 bytes long map data header that each block has)
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
{
Uint8 heightFactor;
CHECK_READ(libendian::read(&heightFactor, 1, fp));
myMap->getVertex(i, j).h = heightFactor; //-V807
}
}
myMap->initVertexCoords();
// go to texture information for RightSideUp-Triangles
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).rsuTexture, 1, fp));
}
// go to texture information for UpSideDown-Triangles
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).usdTexture, 1, fp));
}
// go to road data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).road, 1, fp));
}
// go to object type data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).objectType, 1, fp));
}
// go to object info data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).objectInfo, 1, fp));
}
// go to animal data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).animal, 1, fp));
}
// go to unknown1 data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).unknown1, 1, fp));
}
// go to build data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).build, 1, fp));
}
// go to unknown2 data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).unknown2, 1, fp));
}
// go to unknown3 data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).unknown3, 1, fp));
}
// go to resource data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).resource, 1, fp));
}
// go to shading data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).shading, 1, fp));
}
// go to unknown5 data
fseek(fp, 16, SEEK_CUR);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
CHECK_READ(libendian::read(&myMap->getVertex(i, j).unknown5, 1, fp));
}
return myMap.release();
}
bobMAP* CFile::read_swd(FILE* fp)
{
return read_wld(fp);
}
bool CFile::save_file(const boost::filesystem::path& filepath, char filetype, void* data)
{
if(filepath.empty() || !data)
return false;
s25util::file_handle fh(boost::nowide::fopen(filepath.string().c_str(), "wb"));
if(!fh)
return false;
switch(filetype)
{
case LST: return save_lst(*fh, data);
case BOB: return save_bob(*fh, data);
case IDX: return save_idx(*fh, data);
case BBM: return save_bbm(*fh, data);
case LBM: return save_lbm(*fh, data);
case WLD: return save_wld(*fh, data);
case SWD: return save_swd(*fh, data);
default: // no valid data type
return false;
}
}
bool CFile::save_lst(FILE*, void*)
{
return false;
}
bool CFile::save_bob(FILE*, void*)
{
return false;
}
bool CFile::save_idx(FILE*, void*)
{
return false;
}
bool CFile::save_bbm(FILE*, void*)
{
return false;
}
bool CFile::save_lbm(FILE*, void*)
{
return false;
}
bool CFile::save_wld(FILE* fp, void* data)
{
char zero = 0; // to fill bytes
char temp = 0; // to fill bytes
auto* myMap = (bobMAP*)data;
char map_version[11] = "WORLD_V1.0";
std::array<char, 16> map_data_header;
// prepare map data header
map_data_header[0] = 0x10;
map_data_header[1] = 0x27;
map_data_header[2] = 0x00;
map_data_header[3] = 0x00;
map_data_header[4] = 0x00;
map_data_header[5] = 0x00;
*((Uint16*)(&map_data_header[6])) = boost::endian::native_to_little(myMap->width);
*((Uint16*)(&map_data_header[8])) = boost::endian::native_to_little(myMap->height);
map_data_header[10] = 0x01;
map_data_header[11] = 0x00;
*((Uint32*)(&map_data_header[12])) = boost::endian::native_to_little(myMap->width * myMap->height);
// begin writing data to file
// first of all the map header
// WORLD_V1.0
libendian::write(map_version, 10, fp);
auto makeNameArray = [](const std::string& name) {
std::array<char, 20> ar;
auto size = std::min(name.size(), ar.size());
ar.fill(0);
std::copy_n(name.begin(), size, ar.begin());
return ar;
};
// name
libendian::write(makeNameArray(myMap->getName()), fp);
// old width
libendian::le_write_us(myMap->width_old, fp);
// old height
libendian::le_write_us(myMap->height_old, fp);
// type
auto mapType = uint8_t(myMap->type);
libendian::write(&mapType, 1, fp);
// players
libendian::write(&myMap->player, 1, fp);
// author
libendian::write(makeNameArray(myMap->getAuthor()), fp);
// headquarters x
for(unsigned short i : myMap->HQx)
libendian::le_write_us(i, fp);
// headquarters y
for(unsigned short i : myMap->HQy)
libendian::le_write_us(i, fp);
// unknown data (8 Bytes)
for(int i = 0; i < 8; i++)
libendian::write(&zero, 1, fp);
// big map header with area information
for(auto& i : myMap->header)
{
libendian::write(&i.type, 1, fp);
libendian::le_write_us(i.x, fp);
libendian::le_write_us(i.y, fp);
libendian::le_write_ui(i.area, fp);
}
// 0x11 0x27
temp = 0x11;
libendian::write(&temp, 1, fp);
temp = 0x27;
libendian::write(&temp, 1, fp);
// unknown data (always null, 4 Bytes)
for(int i = 0; i < 4; i++)
libendian::write(&zero, 1, fp);
// width
libendian::le_write_us(myMap->width, fp);
// height
libendian::le_write_us(myMap->height, fp);
// now begin writing the real map data
// altitude information
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
{
temp = myMap->getVertex(i, j).z / 5 + 0x0A; //-V807
libendian::write(&temp, 1, fp);
}
}
// texture information for RightSideUp-Triangles
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).rsuTexture, 1, fp);
}
// go to texture information for UpSideDown-Triangles
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).usdTexture, 1, fp);
}
// go to road data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).road, 1, fp);
}
// go to object type data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).objectType, 1, fp);
}
// go to object info data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).objectInfo, 1, fp);
}
// go to animal data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).animal, 1, fp);
}
// go to unknown1 data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).unknown1, 1, fp);
}
// go to build data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).build, 1, fp);
}
// go to unknown2 data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).unknown2, 1, fp);
}
// go to unknown3 data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).unknown3, 1, fp);
}
// go to resource data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).resource, 1, fp);
}
// go to shading data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).shading, 1, fp);
}
// go to unknown5 data
libendian::write(map_data_header, fp);
for(int j = 0; j < myMap->height; j++)
{
for(int i = 0; i < myMap->width; i++)
libendian::write(&myMap->getVertex(i, j).unknown5, 1, fp);
}
// at least write the map footer (ends in 0xFF)
temp = char(0xFF);
libendian::write(&temp, 1, fp);
return true;
}
bool CFile::save_swd(FILE* fp, void* data)
{