forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.cpp
1179 lines (953 loc) · 27.4 KB
/
data.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
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file data.c
*
* Data loading functions used by the framework resource module.
*
*/
#include <physfs.h>
#include "lib/framework/frame.h"
#include "lib/framework/frameresource.h"
#include "lib/framework/strres.h"
#include "lib/framework/crc.h"
#include "lib/framework/resly.h"
#include "lib/gamelib/parser.h"
#include "lib/ivis_opengl/bitimage.h"
#include "lib/ivis_opengl/tex.h"
#include "lib/script/script.h"
#include "lib/sound/audio.h"
#include "qtscript.h"
#include "data.h"
#include "droid.h"
#include "feature.h"
#include "function.h"
#include "mechanics.h"
#include "message.h"
#include "multiplay.h"
#include "research.h"
#include "scriptvals.h"
#include "stats.h"
#include "template.h"
#include "text.h"
#include "texture.h"
#define DT_TEXPAGE "TEXPAGE"
#define DT_TCMASK "TCMASK"
// whether a save game is currently being loaded
static bool saveFlag = false;
uint32_t DataHash[DATA_MAXDATA]= {0};
/**
* hashBuffer()
* \param pData pointer to our buffer (always a text file)
* \param size the size of the buffer
* \return hash calculated from the buffer.
*
* Note, this is obviously not a very complex hash routine. This most likely has many collisions possible.
* This is almost the same routine that Pumpkin had, minus the ugly bug :)
* And minus the old algorithm and debugging trace, replaced with a simple CRC...
*/
static UDWORD hashBuffer(uint8_t *pData, uint32_t size)
{
char nl = '\n';
uint32_t crc = 0;
uint32_t i, j;
uint32_t lines = 0;
uint32_t bytes = 0;
for (i = 0; i < size; i = j + 1)
{
for (j = i; j < size && pData[j] != '\n' && pData[j] != '\r'; ++j)
{}
if (i != j) // CRC non-empty lines only.
{
crc = crcSum(crc, pData + i, j - i); // CRC the line.
crc = crcSum(crc, &nl, 1); // CRC the line ending.
++lines;
bytes += j - i + 1;
}
}
debug(LOG_NET, "The size of the old buffer (%u bytes - %d stripped), New buffer size of %u bytes, %u non-empty lines.", size, size - bytes, bytes, lines);
return ~crc;
}
// create the hash for that data block.
// Data should be converted to Network byte order
static void calcDataHash(uint8_t *pBuffer, uint32_t size, uint32_t index)
{
const uint32_t oldHash = DataHash[index];
if (!bMultiPlayer)
{
return;
}
DataHash[index] += hashBuffer(pBuffer, size);
if (!DataHash[index] && oldHash)
{
debug(LOG_NET, "The new hash is 0, the old hash was %u. We added the negated value!", oldHash);
}
debug(LOG_NET, "DataHash[%2u] = %08x", index, DataHash[index]);
return;
}
void resetDataHash(void)
{
UDWORD i;
for (i = 0; i < DATA_MAXDATA; i++)
{
DataHash[i] = 0;
}
debug(LOG_NET, "== Hash is reset ==");
}
/**********************************************************/
void dataSetSaveFlag(void)
{
saveFlag = true;
}
void dataClearSaveFlag(void)
{
saveFlag = false;
}
/* Load the body stats */
static bool bufferSBODYLoad(const char* fileName, void** ppData)
{
if (!loadBodyStats(fileName) || !allocComponentList(COMP_BODY, numBodyStats))
{
return false;
}
*ppData = (void *)1;
return true;
}
static void dataReleaseStats(WZ_DECL_UNUSED void *pData)
{
freeComponentLists();
statsShutDown();
}
/* Load the weapon stats */
static bool bufferSWEAPONLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SWEAPON);
if (!loadWeaponStats(pBuffer, size)
|| !allocComponentList(COMP_WEAPON, numWeaponStats))
{
return false;
}
// not interested in this value
*ppData = NULL;
return true;
}
/* Load the constructor stats */
static bool bufferSCONSTRLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SCONSTR);
if (!loadConstructStats(pBuffer, size)
|| !allocComponentList(COMP_CONSTRUCT, numConstructStats))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the ECM stats */
static bool bufferSECMLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SECM);
if (!loadECMStats(pBuffer, size)
|| !allocComponentList(COMP_ECM, numECMStats))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Propulsion stats */
static bool bufferSPROPLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SPROP);
if (!loadPropulsionStats(pBuffer, size)
|| !allocComponentList(COMP_PROPULSION, numPropulsionStats))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Sensor stats */
static bool bufferSSENSORLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SSENSOR);
if (!loadSensorStats(pBuffer, size)
|| !allocComponentList(COMP_SENSOR, numSensorStats))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Repair stats */
static bool bufferSREPAIRLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SREPAIR);
if (!loadRepairStats(pBuffer, size)
|| !allocComponentList(COMP_REPAIRUNIT, numRepairStats))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Brain stats */
static bool bufferSBRAINLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SBRAIN);
if (!loadBrainStats(pBuffer, size)
|| !allocComponentList(COMP_BRAIN, numBrainStats))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the PropulsionType stats */
static bool bufferSPROPTYPESLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SPROPTY);
if (!loadPropulsionTypes(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the propulsion type sound stats */
static bool bufferSPROPSNDLoad(const char *pBuffer, UDWORD size, void **ppData)
{
if (!loadPropulsionSounds(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the STERRTABLE stats */
static bool bufferSTERRTABLELoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_STERRT);
if (!loadTerrainTable(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the body/propulsion IMDs stats */
static bool bufferSBPIMDLoad(const char *pBuffer, UDWORD size, void **ppData)
{
if (!loadBodyPropulsionIMDs(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the weapon sound stats */
static bool bufferSWEAPSNDLoad(const char *pBuffer, UDWORD size, void **ppData)
{
if (!loadWeaponSounds(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Weapon Effect modifier stats */
static bool bufferSWEAPMODLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SWEAPMOD);
if (!loadWeaponModifiers(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Template stats */
static bool bufferSTEMPLLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_STEMP);
if (!loadDroidTemplates(pBuffer, size))
{
return false;
}
// set a dummy value so the release function gets called
*ppData = (void *)1;
return true;
}
// release the templates
static void dataSTEMPLRelease(WZ_DECL_UNUSED void *pData)
{
//free the storage allocated to the droid templates
droidTemplateShutDown();
}
/* Load the Template weapons stats */
static bool bufferSTEMPWEAPLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_STEMPWEAP);
if (!loadDroidWeapons(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Structure stats */
static bool bufferSSTRUCTLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SSTRUCT);
if (!loadStructureStats(pBuffer, size)
|| !allocStructLists())
{
return false;
}
// set a dummy value so the release function gets called
*ppData = (void *)1;
return true;
}
// release the structure stats
static void dataSSTRUCTRelease(WZ_DECL_UNUSED void *pData)
{
freeStructureLists();
structureStatsShutDown();
}
/* Load the Structure Weapons stats */
static bool bufferSSTRWEAPLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SSTRWEAP);
if (!loadStructureWeapons(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Structure Functions stats */
static bool bufferSSTRFUNCLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_STRFUNC);
if (!loadStructureFunctions(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Structure strength modifier stats */
static bool bufferSSTRMODLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SSTRMOD);
if (!loadStructureStrengthModifiers(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the Feature stats */
static bool bufferSFEATLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SFEAT);
if (!loadFeatureStats(pBuffer, size))
{
return false;
}
// set a dummy value so the release function gets called
*ppData = (void *)1;
return true;
}
// free the feature stats
static void dataSFEATRelease(WZ_DECL_UNUSED void *pData)
{
featureStatsShutDown();
}
/* Load the Functions stats */
static bool bufferSFUNCLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_SFUNC);
if (!loadFunctionStats(pBuffer, size))
{
return false;
}
//adjust max values of stats used in the design screen due to any possible upgrades
adjustMaxDesignStats();
// set a dummy value so the release function gets called
*ppData = (void *)1;
return true;
}
// release the function stats
static void dataSFUNCRelease(WZ_DECL_UNUSED void *pData)
{
FunctionShutDown();
}
// release the research stats
static void dataRESCHRelease(WZ_DECL_UNUSED void *pData)
{
//free the storage allocated to the stats
ResearchShutDown();
}
/* Load the Research stats */
static bool bufferRESCHLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_RESCH);
//check to see if already loaded
if (asResearch.size() > 0)
{
//release previous data before loading in the new
dataRESCHRelease(NULL);
}
if (!loadResearch(pBuffer, size))
{
return false;
}
/* set a dummy value so the release function gets called - the Release
* function is now called when load up the next set
// *ppData = (void *)1;
* pass back NULL so that can load the same name file for the next campaign*/
*ppData = NULL;
return true;
}
/* Load the research pre-requisites */
static bool bufferRPREREQLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_RPREREQ);
if (!loadResearchPR(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the research components made redundant */
static bool bufferRCOMPREDLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_RCOMPRED);
if (!loadResearchArtefacts(pBuffer, size, RED_LIST))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the research component results */
static bool bufferRCOMPRESLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_RCOMPRES);
if (!loadResearchArtefacts(pBuffer, size, RES_LIST))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the research structures required */
static bool bufferRSTRREQLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_RSTRREQ);
if (!loadResearchStructures(pBuffer, size, REQ_LIST))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the research structures made redundant */
static bool bufferRSTRREDLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_RSTRRED);
if (!loadResearchStructures(pBuffer, size, RED_LIST))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the research structure results */
static bool bufferRSTRRESLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_RSTRRES);
if (!loadResearchStructures(pBuffer, size, RES_LIST))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the research functions */
static bool bufferRFUNCLoad(const char *pBuffer, UDWORD size, void **ppData)
{
calcDataHash((uint8_t *)pBuffer, size, DATA_RFUNC);
if (!loadResearchFunctions(pBuffer, size))
{
return false;
}
//not interested in this value
*ppData = NULL;
return true;
}
/* Load the message viewdata */
static bool bufferSMSGLoad(const char *pBuffer, UDWORD size, void **ppData)
{
const char *ptr;
ptr = loadViewData(pBuffer, size);
if (!ptr)
{
return false;
}
// set the pointer so the release function gets called with it
*ppData = (void *)ptr;
return true;
}
/* Load research message viewdata */
static bool dataResearchMsgLoad(const char* fileName, void** ppData)
{
const char *ptr = loadResearchViewData(fileName);
if (!ptr)
{
return false;
}
// set the pointer so the release function gets called with it
*ppData = (void *)ptr;
return true;
}
// release the message viewdata
static void dataSMSGRelease(void *pData)
{
viewDataShutDown((const char *)pData);
}
/* Load an imd */
static bool dataIMDBufferLoad(const char *pBuffer, UDWORD size, void **ppData)
{
iIMDShape *psIMD;
const char *pBufferPosition = pBuffer;
psIMD = iV_ProcessIMD( &pBufferPosition, pBufferPosition + size );
if (psIMD == NULL) {
debug( LOG_ERROR, "IMD load failed - %s", GetLastResourceFilename() );
return false;
}
*ppData = psIMD;
return true;
}
/*!
* Load an image from file
*/
static bool dataImageLoad(const char *fileName, void **ppData)
{
iV_Image *psSprite = (iV_Image *)malloc(sizeof(iV_Image));
if (!psSprite)
{
return false;
}
if (!iV_loadImage_PNG(fileName, psSprite))
{
debug( LOG_ERROR, "IMGPAGE load failed" );
return false;
}
*ppData = psSprite;
return true;
}
// Tertiles (terrain tiles) loader.
static bool dataTERTILESLoad(const char *fileName, void **ppData)
{
bool status;
status = texLoad(fileName);
ASSERT_OR_RETURN(false, status, "Error loading tertiles!");
debug(LOG_TEXTURE, "HW Tiles loaded");
*ppData = NULL; // don't bother calling cleanup
return true;
}
static bool dataIMGLoad(const char *fileName, void **ppData)
{
*ppData = iV_LoadImageFile(fileName);
if(*ppData == NULL)
{
return false;
}
return true;
}
static void dataIMGRelease(void *pData)
{
iV_FreeImageFile((IMAGEFILE*)pData);
}
/* Load a texturepage into memory */
static bool dataTexPageLoad(const char *fileName, void **ppData)
{
char texpage[PATH_MAX] = {'\0'};
// This hackery is needed, because fileName will include the directory name, whilst the LastResourceFilename will not, and we need a short name to identify the texpage
sstrcpy(texpage, GetLastResourceFilename());
pie_MakeTexPageName(texpage);
if (!dataImageLoad(fileName, ppData))
{
return false;
}
// see if this texture page has already been loaded
if (resPresent(DT_TEXPAGE, texpage))
{
// replace the old texture page with the new one
debug(LOG_TEXTURE, "replacing %s with new texture %s", texpage, fileName);
pie_ReplaceTexPage((iV_Image *)*ppData, texpage, getTextureSize(), true);
}
else
{
debug(LOG_TEXTURE, "adding page %s with texture %s", texpage, fileName);
SetLastResourceFilename(texpage);
pie_AddTexPage((iV_Image *)*ppData, texpage, 0, getTextureSize(), true);
}
return true;
}
/* Load a team colour mask texturepage into memory */
static bool dataTexPageTCMaskLoad(const char *fileName, void **ppData)
{
char texpage[PATH_MAX] = {'\0'};
// This hackery is needed, because fileName will include the directory name, whilst the LastResourceFilename will not, and we need a short name to identify the texpage
sstrcpy(texpage, GetLastResourceFilename());
// Check if a corresponding texpage exists, exit if no
pie_MakeTexPageName(texpage);
ASSERT_OR_RETURN(false, resPresent(DT_TEXPAGE, texpage), "Corresponding texpage %s doesn't exists!", texpage);
pie_MakeTexPageTCMaskName(texpage);
if (!dataImageLoad(fileName, ppData))
{
return false;
}
// see if this texture page has already been loaded
if (resPresent(DT_TCMASK, texpage))
{
// replace the old texture page with the new one
debug(LOG_TEXTURE, "replacing %s with new tcmask %s", texpage, fileName);
pie_ReplaceTexPage((iV_Image *)*ppData, texpage, getTextureSize(), false);
}
else
{
debug(LOG_TEXTURE, "adding page %s with tcmask %s", texpage, fileName);
SetLastResourceFilename(texpage);
pie_AddTexPage((iV_Image *)*ppData, texpage, 0, getTextureSize(), false);
}
return true;
}
/*!
* Release an Image
*/
static void dataImageRelease(void *pData)
{
iV_Image *psSprite = (iV_Image*) pData;
if( psSprite )
{
free(psSprite);
}
}
/* Load an audio file */
static bool dataAudioLoad(const char* fileName, void **ppData)
{
if ( audio_Disabled() == true )
{
*ppData = NULL;
// No error occurred (sound is just disabled), so we return true
return true;
}
// Load the track from a file
*ppData = sound_LoadTrackFromFile( fileName );
return *ppData != NULL;
}
/* Load an audio file */
static bool dataAudioCfgLoad(const char* fileName, void **ppData)
{
bool success;
PHYSFS_file* fileHandle;
*ppData = NULL;
if (audio_Disabled())
{
return true;
}
debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName);
fileHandle = PHYSFS_openRead(fileName);
if (fileHandle == NULL)
{
return false;
}
success = ParseResourceFile(fileHandle);
PHYSFS_close(fileHandle);
return success;
}
/* Load an anim file */
static bool dataAnimLoad(const char *fileName, void **ppData)
{
PHYSFS_file* fileHandle = PHYSFS_openRead(fileName);
debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName);
if (fileHandle == NULL)
{
*ppData = NULL;
return false;
}
*ppData = anim_LoadFromFile(fileHandle);
PHYSFS_close(fileHandle);
return *ppData != NULL;
}
/* Load an audio config file */
static bool dataAnimCfgLoad(const char *fileName, void **ppData)
{
bool success;
PHYSFS_file* fileHandle = PHYSFS_openRead(fileName);
*ppData = NULL;
debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName);
if (fileHandle == NULL)
{
return false;
}
success = ParseResourceFile(fileHandle);
PHYSFS_close(fileHandle);
return success;
}
static void dataAnimRelease( void *pData )
{
anim_ReleaseAnim((BASEANIM*)pData);
}
/* Load a string resource file */
static bool dataStrResLoad(const char* fileName, void** ppData)
{
// recreate the string resource if it was freed by a WRF release
if (psStringRes == NULL)
{
if (!stringsInitialise())
{
return false;
}
}
if (!strresLoad(psStringRes, fileName))
{
return false;
}
*ppData = psStringRes;
return true;
}
static void dataStrResRelease(WZ_DECL_UNUSED void *pData)
{
if (psStringRes != NULL)
{
strresDestroy(psStringRes);
psStringRes = NULL;
}
}
/* Load a script file */
// All scripts, binary or otherwise are now passed through this routine
static bool dataScriptLoad(const char* fileName, void **ppData)
{
static const bool printHack = false;
SCRIPT_CODE** psProg = (SCRIPT_CODE**)ppData;
PHYSFS_file* fileHandle;
uint8_t *pBuffer;
PHYSFS_sint64 fileSize = 0;
debug(LOG_WZ, "COMPILING SCRIPT ...%s", GetLastResourceFilename());
fileHandle = PHYSFS_openRead(fileName);
debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName);
if (fileHandle == NULL)
{
return false;
}
// due to the changes in r2531 we must do this routine a bit different.
fileSize = PHYSFS_fileLength(fileHandle);
pBuffer = (uint8_t *)malloc(fileSize * sizeof(uint8_t));
ASSERT_OR_RETURN(false, pBuffer, "Out of memory");
PHYSFS_read(fileHandle, pBuffer, 1, fileSize);
calcDataHash(pBuffer, fileSize, DATA_SCRIPT);
free(pBuffer);
PHYSFS_seek(fileHandle, 0); //reset position
*psProg = scriptCompile(fileHandle, SCRIPTTYPE);
PHYSFS_close(fileHandle);
if (!*psProg) // see script.h
{
debug(LOG_ERROR, "Script %s did not compile", GetLastResourceFilename());
return false;
}
if (printHack)
{
cpPrintProgram(*psProg);