-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpak_map.c
970 lines (798 loc) · 23.8 KB
/
pak_map.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
/*
hypov8
===========
pack map file into .pk3
generate a shader file that includes non kpq3 assets
these shaders will be renamed to prevent conflicts (eg maps using the same map models)
shader names are textures/kmap2_<mapname>/<index>
also .bsp internal shader names will be re-named (.aas crc updated to suit)
usage:
kmap2.exe -pakmap -game kingpinq3 -fs_game basekpq3 -fs_basepath D:/kpq3_beta2/ D:/kpq3_beta2/basekpq3/maps/fortknox.map
output:
basekpq3/map-<mapname>-<date>.pk3
maps/<mapname>.bsp (updated shaders)
materials/kmap2_<mapname>.mtr (generated)
maps/<mapname>.aas (must exist)
levelshots/<mapname>.jpg (must exist)
video/<mapname>.ogv (must exist)
scripts/<mapname>.arena (must exist)
<map assets>
todo:
deal with tables?
fix kmap image files
cubemap images.
change pk3 file name/date?
change compresion for other file types. eg png is uncompresed in zip.
imagefile names are not renamed and will cause duplicates. should these be unique to?
handle mod folder/s. this is also not loading global paths properly(local to vfs.c)
check for missing -fs_path switches.
optional include <filename>.map
*/
#ifdef KMAP2
#include "kmap2.h"
#include "zip.h" //compress
// dupe "../code/qcommon/md4.c"
//copied to kmap
unsigned Com_BlockChecksum(const void *buffer, int length);
typedef enum{
SHADER_NONE,
SHADER_STORED,
SHADER_FOUND
}storeState_t;
typedef struct
{
char newName[MAX_QPATH];
char oldName[MAX_QPATH];
char *script; //complete script
storeState_t storeState; //1=needs shader, -1=match found
} shaderStruct;
shaderStruct pakMapShaders[MAX_SHADER_INFO];
static int isPakMap = 0; //todo: move to global?
static int isCaseSens = 0; //find linux texture issues?
static int numMapTextures = 0;
static int numShaderScripts = 0;
static char bspFilename[1024], bspTmpFilename[1024];
static char aasFilename[1024], aasTmpFilename[1024];
static char textureNames[MAX_SHADER_INFO][MAX_QPATH]; //image files size ok?
#define fExCount 4
static const char *fExt[fExCount] = {".png", ".tga", ".jpg", ".pcx"};
static qboolean ReadTexturesAdvancedModes(char *shaderText);
static int bspChecksum = 0;
/*
==============
PakMap_Cleanup
if error is found, fix file names then print Error
==============
*/
static void PakMap_Cleanup(qboolean restoreOrigMap, qboolean printError, char *str)
{
int i;
for (i = 0; i < numBSPShaders; i++)
{
if (pakMapShaders[i].storeState == SHADER_STORED)
free(pakMapShaders[i].script);
}
//delete shader file
remove(mapShaderFile);
//delete edited files. restore originals
if (restoreOrigMap)
{
if (FileExists(bspTmpFilename))
{
remove(bspFilename);
rename(bspTmpFilename, bspFilename);
}
if (FileExists(aasTmpFilename))
{
remove(aasFilename);
rename(aasTmpFilename, aasFilename);
}
}
Sys_Printf("=========================\n");
if (printError)
Error(str);
}
static void PakMap_ReNameShader()
{
int i, j;
char *c, newShaderName[256];
if (mapName == NULL || mapName == '\0')
PakMap_Cleanup(qtrue, qtrue, "mapname failed");
for(i = 0; i < numBSPShaders; i++)
{
j = 0;
//skip rename
if (pakMapShaders[i].storeState != SHADER_STORED)
continue;
c = &bspShaders[i].shader[0];
//find first '/'
while (j< 64 && c[0] != '/' && c[0] != '\\') {
j++; c++;
}
j++; c++;
//copy shader root folder
Q_strncpyz(newShaderName, bspShaders[i].shader, j+1);
sprintf(newShaderName, "%skmap2_%s", newShaderName, mapName);
//find next '/'
while (j< 64 && c[0] != '/' && c[0] != '\\') {
j++; c++;
}
#if 0 //copy the end of shader.
Q_strcat(newShaderName, 64, c);
#else
//todo: add index as name instead?
Q_strcat(newShaderName, 64, va("/%.3i", i));
#endif
//copy new shader name to .bsp and .mtr
Q_strncpyz(bspShaders[i].shader, newShaderName, 64); //rename bsp shader
Q_strncpyz(pakMapShaders[i].newName, newShaderName, 64); //rename .mtr name
}
}
static qboolean PakMap_IgnoredFile(char *texName)
{
int i;
//ignore internal and base images
if (texName[0] == '*' || texName[0] == '$' || texName[0] == '_' ||
/* skip known base textures */
!Q_strncasecmp(texName, "textures/skies_kpq3/", 20) || //moved from textures/skies/
!Q_strncasecmp(texName, "textures/strombine", 18) ||
!Q_strncasecmp(texName, "textures/decals/", 16) ||
!Q_strncasecmp(texName, "textures/method/", 16) ||
!Q_strncasecmp(texName, "textures/common/", 16) || //textures not used in kpq3
!Q_strncasecmp(texName, "textures/color/", 15) || //move to common?
!Q_strncasecmp(texName, "textures/kpq3_", 14) ||
!Q_strncasecmp(texName, "textures/misc_", 14) ||
!Q_strncasecmp(texName, "textures/al0/", 13) || //contains flags, safe etc..
!Q_strncasecmp(texName, "lights/kpq3/", 12) ||
//Q_strncasecmp(texName, "cubemaps/", 9) || //todo: move default?
!Q_strncasecmp(texName, "sprites/", 8) ||
!Q_strncasecmp(texName, "gfx/", 4) ||
!Q_strncasecmp(texName, "ui/", 3))
{
return qtrue;
}
//check if we have the image allready
for (i = 0; i < numMapTextures; i++)
{
if (textureNames[i][0] != '\0' && !Q_stricmp(texName, textureNames[i]))
return qtrue;
}
return qfalse;
}
static int PakMap_Shader_CheckForMatchingInBsp(char *shaderName)
{
int i, ret;
//check if shader is used
for(i = 0; i < numBSPShaders; i++)
{
if (isCaseSens)
ret = strcmp(shaderName, bspShaders[i].shader);
else
ret = Q_stricmp(shaderName, bspShaders[i].shader);
//found match?
if (!ret)
{
if (pakMapShaders[i].storeState == SHADER_NONE)
pakMapShaders[i].storeState = SHADER_FOUND; //mark as found
return i;
}
}
return -1;
}
static qboolean PakMap_SetTextureLists_global(char *shaderText/*, char *shaderName*/)
{
int i;
char *list1[8] = {"colorMap", "diffuseMap", "bumpMap", "normalMap", "specularMap", "glowMap", "reflectionMap", "reflectionMapBlended"};
/// match kmap_
if (!Q_strncasecmp(token, "kmap_", PREFIX_LEN) || !Q_strncasecmp(token, "xmap_", PREFIX_LEN))
{ //kmap_cloneShader, kmap_remapShader, kmap_globaltexture
if (!Q_stricmp(token + PREFIX_LEN, "backshader... cloneShader...remapShader...")) //todo: real names!!
{
ReadTexturesAdvancedModes(shaderText);
}
}
else if (!Q_stricmp(token, "qer_editorimage"))
{
// skip texture
GetTokenAppend(shaderText, qfalse);
}
else
{
for (i = 0; i < 8; i++)
{
if (!Q_stricmp(token, list1[i]))
{
ReadTexturesAdvancedModes(shaderText);
break;
}
}
}
//ignore all other tokens on the line.
while(TokenAvailable() && GetTokenAppend(shaderText, qfalse));
return qtrue;
}
static qboolean PakMap_SetTextureLists_stage(char *shaderText)
{
int i;
if (!Q_stricmp(token, "map") || !Q_stricmp(token, "clampMap") || !Q_stricmp(token, "videoMap"))
{
ReadTexturesAdvancedModes(shaderText);
}
/*else if (!Q_stricmp(token, "cubeMap")) //todo:
{
readCubemapTextures();
}*/
else if (!Q_stricmp(token, "animMap"))
{
//skip fps number
GetTokenAppend(shaderText, qfalse);
//try max count of shaders
for (i = 0; i < 24; i++)
{
if (TokenAvailable() && GetTokenAppend(shaderText, qfalse))
{
if (!PakMap_IgnoredFile(token))
{
Q_strncpyz(textureNames[numMapTextures], token, MAX_QPATH);
numMapTextures++;
}
}
else
break;
}
return qtrue;
}
/* ignore all other tokens on the line */
while(TokenAvailable() && GetTokenAppend(shaderText, qfalse));
return qtrue;
}
int PakMap_ReadShaderFile()
{
int bspIndex;
char shaderText[MAX_SHADER_INFO];
int depth = 0;
if (!isPakMap)
return qfalse;
bspIndex = PakMap_Shader_CheckForMatchingInBsp(token);
if (bspIndex == -1 || PakMap_IgnoredFile(token))
{ //skip whole shader
SkipBracedSection();
}
else
{
Q_strncpyz(pakMapShaders[bspIndex].oldName, token, MAX_QPATH); //copy shader name
shaderText[0] = '\0';
if (!GetTokenAppend(shaderText, qtrue))
PakMap_Cleanup(qtrue, qtrue, "invalid token in shader");
if (strcmp(token, "{"))
PakMap_Cleanup(qtrue, qtrue, va("brace { missing in shader %s", pakMapShaders[bspIndex].oldName));
while (1)
{
if (!GetTokenAppend(shaderText, qtrue))
break;
if (!strcmp(token, "}"))
break;
depth = 0;
/* parse stage directives */
if (!strcmp(token, "{"))
{
depth++;
while (1)
{
if (!GetTokenAppend(shaderText, qtrue))
break;
if (!strcmp(token, "}"))
break;
//skip blend/stage
PakMap_SetTextureLists_stage(shaderText);
}
}
PakMap_SetTextureLists_global(shaderText);
}
//store shader
pakMapShaders[bspIndex].script = safe_malloc(strlen(shaderText) + 1);
Q_strncpyz(pakMapShaders[bspIndex].script, shaderText, strlen(shaderText)+1); //copy all shader text
numShaderScripts++;
pakMapShaders[bspIndex].storeState = SHADER_STORED;
}
return qtrue;
}
/*
write .bsp in the original file name
*/
void PakMap_SaveBspFile()
{
Sys_Printf ("%-23s (maps/%s.bsp)\n", "Write .bsp file.", mapName);
WriteBSPFile(bspFilename);
}
//read file. get checksum
static int PakMap_GetFileChecksum(char * fileName)
{
//FILE *f;
unsigned int len;
int retChecksum;
byte *buffer;
len = LoadFile(fileName, (void **)&buffer);
retChecksum = LittleLong(Com_BlockChecksum(buffer, len));
free(buffer);
return retChecksum;
}
//be_aas_file
void PakMap_AAS_DDATA(unsigned char *data, int size)
{
int i;
for (i = 0; i < size; i++)
{
data[i] ^= (unsigned char) i * 119;
}
}
/*
update aas file checksum
*/
void PakMap_UpdateAASChecksum()
{
byte *buffer;
long len;
int newBspChecksum, cSum;
int *hdr;
//make sure original aas and modded bsp exists
if (!FileExists(aasFilename) || !FileExists(bspFilename))
{
Sys_Printf ("%-23s (%s)\n", "WARNING: File missing", aasFilename);
return;
}
//remove any failed attemps
remove(aasTmpFilename);
//rename original aas to tmp
if (rename(aasFilename, aasTmpFilename) != 0)
{
Sys_Printf ("%-23s (%s)\n", "WARNING: Cant rename", aasFilename);
return;
}
//new bsp checksum
newBspChecksum = PakMap_GetFileChecksum(bspFilename);
if (newBspChecksum == 0 || bspChecksum == 0)
{
Sys_Printf ("%-23s (%s)\n", "WARNING: bsp/aas", "checksum failed");
return;
}
//read .aas file to buffer
len = LoadFile(aasTmpFilename, (void **)&buffer);
//get header value
PakMap_AAS_DDATA(buffer + 8, 4);
hdr = (int*)buffer;
cSum = LittleLong(hdr[2]);
if (cSum != bspChecksum)
{
rename(aasTmpFilename, aasFilename);
free(buffer);
Sys_Printf ("%-23s (%s)\n", "WARNING: Checksum missmatch", "aasFilename");
return;
}
//set header value
hdr[2] = LittleLong(newBspChecksum);
PakMap_AAS_DDATA(buffer + 8, 4);
//save buffer to new file
SaveFile(aasFilename, buffer, len);
free(buffer);
}
static void PakMap_GetTime(char * timeString)
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
sprintf(timeString, va("%d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday));
}
/*
============
PakMap_CompType
zip compresion types
dont compress already compress files. store
============
*/
int PakMap_CompType(const char *fileName)
{
char str[256];
ExtractFileExtension(fileName, str);
if (!Q_strncasecmp(str, ".png", 4) || !Q_strncasecmp(str, ".jpg", 4))
return 0; //store
//else if (!Q_strncasecmp(str, ".tga", 4))
//default compress
return Z_DEFLATED;
}
void PakMap_ZipTexture(zipFile pk3File, char *texName, byte *buffer, int size)
{
const zip_fileinfo zfi = {0};
int ret, compType = PakMap_CompType(texName);
zipOpenNewFileInZip(pk3File, texName, &zfi, NULL, 0, NULL, 0, NULL, compType, Z_DEFAULT_COMPRESSION);
ret = zipWriteInFileInZip(pk3File, buffer, size); //-1?
zipCloseFileInZip(pk3File);
free(buffer);
}
void PakMap_AddFileToPk3(zipFile pk3File, char *texName)
{
byte *buffer = NULL;
int i, size; // , ret, compType;
//search for file in virtual file system
size = vfsLoadFile((const char *)texName, (void **)&buffer, 0);
if (size > 0 )
{
PakMap_ZipTexture(pk3File,texName, buffer, size);
}
else
{
char tmp[256];
for (i = 0; i < fExCount; i++)
{ //add new file extension
Q_strncpyz(tmp, texName, 256);
StripExtension(tmp);
Q_strcat(tmp,256, fExt[i]);
//search for file in virtual file system
size = vfsLoadFile((const char *)tmp, (void **)&buffer, 0);
if (size > 0 )
{
PakMap_ZipTexture(pk3File, tmp, buffer, size);
break;
}
}
if (i==4)
Sys_Printf("%-23s (%s)\n", "WARNING: can't read.", texName);
}
}
/*
===============
PakMap_GenerateShader
found texture but shader is missing.
this generates a new shader for image
note: some models have image name set. not using shader
===============
*/
static void PakMap_GenerateShader(char *filePath,int bspIndex)
{
char shaderText[2048];
if (PakMap_IgnoredFile(filePath))
return;
//add the image file it list
Q_strncpyz(textureNames[numMapTextures], filePath, MAX_QPATH);
numMapTextures++;
//generate the shader script
sprintf(shaderText, "\n{\ndiffusemap %s\n}\n", filePath);
pakMapShaders[bspIndex].script = safe_malloc(strlen(shaderText) + 1);
Q_strncpyz(pakMapShaders[bspIndex].script, shaderText, strlen(shaderText)+1); //copy all shader text
numShaderScripts++;
Q_strncpyz(pakMapShaders[bspIndex].oldName, bspShaders[bspIndex].shader, 64); //add materal name
pakMapShaders[bspIndex].storeState = SHADER_STORED; //marked save
//warn user
//Sys_Printf("Note: Generated missing shader for \"%s\"\n", filePath);
//Sys_Printf("%-23s (%s)\n", "Note: Generated shader", filePath);
Sys_Printf("%-23s (%s)\n", "Make shader for image", filePath);
}
static void PakMap_CheckMissingShaderIsTexture()
{
byte *buffer = NULL;
char baseDir[256], tmp1[256];
int i, j;
char *ptrBase = strstr(bspFilename, game->gamePath); //todo: check multiple mod names?
char *bspPath = &bspFilename[0];
Sys_Printf(
"=========================\n"
"= pak map =\n"
"=========================\n");
if (ptrBase)
{
size_t size = ptrBase - bspPath;
Q_strncpyz(baseDir, bspFilename, size + strlen(game->gamePath) + 1);
for (i = 0; i < numBSPShaders; i++)
{
if (pakMapShaders[i].storeState == SHADER_NONE)
{
Q_strncpyz(tmp1, bspShaders[i].shader, 256);
for (j = 0; j < fExCount; j++)
{ //add new file extension
StripExtension(tmp1);
Q_strcat(tmp1, 256, fExt[j]);
if (vfsLoadFile((const char *)tmp1, (void **)&buffer, 0) > 0)
{
PakMap_GenerateShader(tmp1, i); //generate new shader
free(buffer);
break;
}
}
}
}
}
else
{
//warn, no base
PakMap_Cleanup(qtrue, qtrue, va("GamePath game not found in filename. <%s> <%s>", game->gamePath, bspFilename));
}
}
static void PakMap_SavePk3File()
{
FILE *fileAsset = NULL;
char pakFilename[256], curTime[256];
char baseDir[256], tmpPath[256];
int i;
char *ptrBase = strstr(bspFilename, game->gamePath); //todo: check multiple mod names?
char *bspPath = &bspFilename[0];
if (ptrBase)
{
zipFile pk3File;
size_t size = ptrBase - bspPath;
PakMap_GetTime(curTime);
Q_strncpyz(baseDir, bspFilename, size +strlen(game->gamePath) + 1);
sprintf(pakFilename, "%s/map-%s-%s.pk3", baseDir, mapName, curTime);
remove(pakFilename); //delete .pk3 if it exists
Sys_Printf ("%-23s (map-%s-%s.pk3)\n", "Saving .pk3 file.", mapName, curTime);
//open new .pk3 file
pk3File = zipOpen(pakFilename, 0);
if (pk3File == NULL)
PakMap_Cleanup(qtrue, qtrue, va("Can't create new pk3 file: %s", pakFilename));
//loop through all map assets
for(i = 0; i < numMapTextures; i++)
{
//error?
if (textureNames[i] == NULL || textureNames[i][0] =='\0')
continue;
PakMap_AddFileToPk3(pk3File, textureNames[i]);
}
Sys_Printf ("%-23s (%d)\n", "Adding shader images.", numMapTextures);
//add default files that should exist
PakMap_AddFileToPk3(pk3File, va("maps/%s.bsp", mapName));
PakMap_AddFileToPk3(pk3File, va("maps/%s.aas", mapName));
PakMap_AddFileToPk3(pk3File, va("levelshots/%s.jpg", mapName));
PakMap_AddFileToPk3(pk3File, va("video/%s.ogv", mapName)); //menu play video
PakMap_AddFileToPk3(pk3File, va( "%s/kmap2_%s.mtr", game->shaderPath, mapName)); //add new shader file. note: mapShaderFile full path fails
PakMap_AddFileToPk3(pk3File, va("scripts/%s.arena", mapName)); //list game types <mapname>.arena
i = 0;
//add lightmaps
while (FileExists(va("%s/maps/%s/lm_%04d.tga", baseDir, mapName, i)))
{
sprintf(tmpPath, "maps/%s/lm_%04d.tga", mapName, i++);
PakMap_AddFileToPk3(pk3File, tmpPath);
}
if (zipClose(pk3File, "closing pk3"))
PakMap_Cleanup(qtrue, qtrue, "can't close pk3 file");
}
}
/*
========
PakMap_ReadBspFile
rename ".bsp" to ".bsp.pakmap"
then load renamed file
========
*/
void PakMap_ReadBspFile()
{
//set old/new map file names
sprintf(bspFilename, "%s.bsp", source);
sprintf(bspTmpFilename, "%s.bsp.pakmap", source);
sprintf(aasFilename, "%s.aas", source);
sprintf(aasTmpFilename, "%s.aas.pakmap", source);
//make sure map exists
if (source[0] == '\0' || !FileExists(bspFilename))
PakMap_Cleanup(qfalse, qtrue, va("Can't open bsp file: %s", bspFilename));
//remove any failed attemps
remove(bspTmpFilename);
if (rename(bspFilename, bspTmpFilename) != 0)
PakMap_Cleanup(qfalse, qtrue, va("Can't rename bsp file: %s", bspFilename));
Sys_Printf("Reading bsp: %s\n", bspFilename);
LoadBSPFile(bspTmpFilename);
//get bsp checksum for aas comparison
bspChecksum = PakMap_GetFileChecksum(bspTmpFilename);
//set mapShaderFile and delete file if it exists
BeginMapShaderFile(source);
//read all shaders from shaderlist.txt
LoadShaderInfo();
}
/*
============
PakMap_SaveShaderFile
clone all used shaders.
All comments removed.
todo: add tables if required
===========
*/
static void PakMap_SaveShaderFile()
{
FILE *file;
int i, custFound = qfalse;
/* dummy check */
if(mapShaderFile[0] == '\0')
return;
/* open shader file */
file = fopen(mapShaderFile, "w");
if(file == NULL)
{
Sys_Printf("WARNING: Unable to open map shader file %s for writing\n", mapShaderFile);
return;
}
/* print header */
fprintf(file,
"// Custom shader file for %s.bsp\n"
"// Generated by KMap2\n" "// This file is overwritten on -pakmap recompiles.\n\n", mapName);
Sys_FPrintf(SYS_VRB, //verbose
//"-------------------------\n"
"---- bsp shader list ----\n");
/* walk the shader list */
for(i = 0; i < numBSPShaders; i++)
{
// skip?
if (pakMapShaders[i].storeState != SHADER_STORED || pakMapShaders[i].oldName == NULL || pakMapShaders[i].oldName[0] == '\0')
{
// print all bsp shaders. verbose
Sys_FPrintf(SYS_VRB, " %s\n", bspShaders[i].shader);
// dont save custom shader
continue;
}
//print custom shader. verbose
Sys_FPrintf(SYS_VRB, "Stored %s\n", pakMapShaders[i].oldName);
// write custom shader to .mtr file
fprintf(file, "//%s\n%s%s\n", pakMapShaders[i].oldName, pakMapShaders[i].newName, pakMapShaders[i].script);
}
Sys_FPrintf(SYS_VRB, //verbose
"--- end bsp shader list ---\n"
"---------------------------\n");
// print missing shaders
for(i = 0; i < numBSPShaders; i++)
{
if (pakMapShaders[i].storeState == SHADER_NONE)
Sys_Printf ("%-23s (%s)\n", "WARNING: Missing shader", bspShaders[i].shader);
}
//close the shader
fflush(file);
fclose(file);
//Sys_FPrintf(SYS_VRB, "\n");
/* print some stats */
Sys_Printf ("%-23s (%d)\n", "Shaders in bsp", numBSPShaders);
Sys_Printf ("%-23s (%d)\n", "Shaders added to .mtr", numShaderScripts);
Sys_Printf ("%-23s (scripts/kmap_%s.mtr)\n", "Write shader file.", mapName);
}
static qboolean ParseHeightMap(char *shaderText)
{
//"heightMap"(<map>, <float>)
GetTokenAppend(shaderText, qfalse);
if (token[0] != '(')
return qfalse;
if (!ReadTexturesAdvancedModes(shaderText)) //<map>
return qfalse;
GetTokenAppend(shaderText, qfalse);
if (token[0] != ',')
return qfalse;
GetTokenAppend(shaderText, qfalse); //float value
GetTokenAppend(shaderText, qfalse); //')'
if (token[0] != ')')
return qfalse;
return qtrue;
}
static qboolean ParseAddNormals(char *shaderText)
{
//"addNormals"(<map>, <map>)
GetTokenAppend(shaderText, qfalse);
if (token[0] != '(')
return qfalse;
if (!ReadTexturesAdvancedModes(shaderText))
return qfalse;
GetTokenAppend(shaderText, qfalse);
if (token[0] != ',')
return qfalse;
if (!ReadTexturesAdvancedModes(shaderText))
return qfalse;
GetTokenAppend(shaderText, qfalse);
if (token[0] != ')')
return qfalse;
return qtrue;
}
static qboolean ParseMakeAlpha(char *shaderText)
{
//"smoothNormals"(<map>)
GetTokenAppend(shaderText, qfalse);
if (token[0] != '(')
return qfalse;
if (!ReadTexturesAdvancedModes(shaderText))
return qfalse;
GetTokenAppend(shaderText, qfalse);
if (token[0] != ')')
return qfalse;
return qtrue;
}
/*
============
"heightMap"(<map>, <float>)
"scale"(<map>, <float> [,float] [,float] [,float])
"displaceMap"(<map>, <map>)
"addNormals"(<map>, <map>)
"add"(<map>, <map>)
"smoothNormals"(<map>)
"invertAlpha"(<map>)
"invertColor"(<map>)
"makeIntensity"(<map>)
"makeAlpha"(<map>)
some textures are combined. parse them out
=========
*/
static qboolean ReadTexturesAdvancedModes(char *shaderText)
{
GetTokenAppend(shaderText, qfalse);
//(<map>, <float>)
if (!Q_stricmp(token, "heightMap"))
{
if (!ParseHeightMap(shaderText))
return qfalse; //error
}
else if (!Q_stricmp(token, "scale"))
{
//"not supported";
return qfalse;
}
//(<map>, <map>)
else if (!Q_stricmp(token, "displaceMap") || !Q_stricmp(token, "addNormals") || !Q_stricmp(token, "add"))
{
if (!ParseAddNormals(shaderText))
return qfalse; //error
}
//(<map>)
else if (!Q_stricmp(token, "smoothNormals")|| !Q_stricmp(token, "invertAlpha")||
!Q_stricmp(token, "invertColor")|| !Q_stricmp(token, "makeIntensity")|| !Q_stricmp(token, "makeAlpha"))
{
if (!ParseMakeAlpha(shaderText))
return qfalse; //error
}
//add image
else
{
if (!PakMap_IgnoredFile(token))
{
Q_strncpyz(textureNames[numMapTextures], token, MAX_QPATH);
numMapTextures++;
}
return qtrue;
}
return qtrue; //check this...
}
int PackMapAssets(int argc, char **argv)
{
//dupe bspMain
int i;
char tempSource[1024];
Sys_Printf("--- PACKMAP ---\n");
SetDrawSurfacesBuffer();
mapDrawSurfs = safe_malloc(sizeof(mapDrawSurface_t) * MAX_MAP_DRAW_SURFS);
memset(mapDrawSurfs, 0, sizeof(mapDrawSurface_t) * MAX_MAP_DRAW_SURFS);
numMapDrawSurfs = 0;
tempSource[0] = '\0';
/* set standard game flags */
maxSurfaceVerts = game->maxSurfaceVerts;
maxLMSurfaceVerts = game->maxLMSurfaceVerts;
maxSurfaceIndexes = game->maxSurfaceIndexes;
emitFlares = game->emitFlares;
/* process arguments */
for (i = 1; i < (argc - 1); i++)
{
//todo: do we need any arguments?
if(!strcmp(argv[i], "-forcecase"))
{
Sys_Printf("Force case sensitive search\n");
isCaseSens = qtrue;
}
}
/* copy source name */
Q_strncpyz(source, ExpandArg(argv[argc - 1]), 1024); //get last arg
StripExtension(source);
Q_strncpyz(name, ExpandArg(argv[argc - 1]), 1024);
//end bspMain
//start -pakmap
isPakMap = 1;
//check base paths are ok?
memset(&pakMapShaders, 0, sizeof(shaderStruct) * MAX_SHADER_INFO);
//todo: use allocate each shader?
PakMap_ReadBspFile();
PakMap_CheckMissingShaderIsTexture(); //check missing shaders for actual file
PakMap_ReNameShader();
PakMap_SaveShaderFile();
PakMap_SaveBspFile();
PakMap_UpdateAASChecksum(); //aas relies on bsp checksum to load
PakMap_SavePk3File();
PakMap_Cleanup(qtrue, qfalse, NULL);
return 0;
}
#endif