/
sys_reslocator.c
1486 lines (1292 loc) · 42.5 KB
/
sys_reslocator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file sys_reslocator.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2011 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Routines for locating resources.
*/
// HEADER FILES ------------------------------------------------------------
#ifdef WIN32
# include <direct.h>
#endif
#ifdef UNIX
# include <pwd.h>
# include <limits.h>
# include <unistd.h>
#endif
#include "de_base.h"
#include "de_console.h"
#include "m_args.h"
#include "m_misc.h"
#include "filedirectory.h"
#include "resourcerecord.h"
#include "resourcenamespace.h"
// MACROS ------------------------------------------------------------------
#define PATH_DELIMIT_CHAR ';'
#define PATH_DELIMIT_STR ";"
// TYPES -------------------------------------------------------------------
#define MAX_EXTENSIONS (3)
typedef struct {
/// Default class attributed to resources of this type.
resourceclass_t defaultClass;
char* knownFileNameExtensions[MAX_EXTENSIONS];
} resourcetypeinfo_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean inited = false;
static const resourcetypeinfo_t typeInfo[NUM_RESOURCE_TYPES] = {
/* RT_ZIP */ { RC_PACKAGE, {"pk3", "zip", 0} },
/* RT_WAD */ { RC_PACKAGE, {"wad", 0} },
/* RT_DED */ { RC_DEFINITION, {"ded", 0} },
/* RT_PNG */ { RC_GRAPHIC, {"png", 0} },
/* RT_TGA */ { RC_GRAPHIC, {"tga", 0} },
/* RT_PCX */ { RC_GRAPHIC, {"pcx", 0} },
/* RT_DMD */ { RC_MODEL, {"dmd", 0} },
/* RT_MD2 */ { RC_MODEL, {"md2", 0} },
/* RT_WAV */ { RC_SOUND, {"wav", 0} },
/* RT_OGG */ { RC_MUSIC, {"ogg", 0} },
/* RT_MP3 */ { RC_MUSIC, {"mp3", 0} },
/* RT_MOD */ { RC_MUSIC, {"mod", 0} },
/* RT_MID */ { RC_MUSIC, {"mid", 0} },
/* RT_DEH */ { RC_UNKNOWN, {"deh", 0} },
/* RT_DFN */ { RC_FONT, {"dfn", 0} }
};
// Recognized resource types (in order of importance, left to right).
#define MAX_TYPEORDER 6
static const resourcetype_t searchTypeOrder[RESOURCECLASS_COUNT][MAX_TYPEORDER] = {
/* RC_PACKAGE */ { RT_ZIP, RT_WAD, 0 }, // Favor ZIP over WAD.
/* RC_DEFINITION */ { RT_DED, 0 }, // Only DED files.
/* RC_GRAPHIC */ { RT_PNG, RT_TGA, RT_PCX, 0 }, // Favour quality.
/* RC_MODEL */ { RT_DMD, RT_MD2, 0 }, // Favour DMD over MD2.
/* RC_SOUND */ { RT_WAV, 0 }, // Only WAV files.
/* RC_MUSIC */ { RT_OGG, RT_MP3, RT_WAV, RT_MOD, RT_MID, 0 },
/* RC_FONT */ { RT_DFN, 0 } // Only DFN fonts.
};
static const ddstring_t defaultNamespaceForClass[RESOURCECLASS_COUNT] = {
/* RC_PACKAGE */ { PACKAGES_RESOURCE_NAMESPACE_NAME },
/* RC_DEFINITION */ { DEFINITIONS_RESOURCE_NAMESPACE_NAME },
/* RC_GRAPHIC */ { GRAPHICS_RESOURCE_NAMESPACE_NAME },
/* RC_MODEL */ { MODELS_RESOURCE_NAMESPACE_NAME },
/* RC_SOUND */ { SOUNDS_RESOURCE_NAMESPACE_NAME },
/* RC_MUSIC */ { MUSIC_RESOURCE_NAMESPACE_NAME },
/* RC_FONT */ { FONTS_RESOURCE_NAMESPACE_NAME }
};
static resourcenamespace_t** namespaces = 0;
static uint numNamespaces = 0;
// CODE --------------------------------------------------------------------
static __inline const resourcetypeinfo_t* getInfoForResourceType(resourcetype_t type)
{
assert(VALID_RESOURCE_TYPE(type));
return &typeInfo[((uint)type)-1];
}
static __inline resourcenamespace_t* getNamespaceForId(resourcenamespaceid_t rni)
{
if(!F_IsValidResourceNamespaceId(rni))
Con_Error("getNamespaceForId: Invalid namespace id %i.", (int)rni);
return namespaces[((uint)rni)-1];
}
static resourcenamespaceid_t findNamespaceForName(const char* name)
{
if(name && name[0])
{
uint i;
for(i = 0; i < numNamespaces; ++i)
{
resourcenamespace_t* rnamespace = namespaces[i];
if(!stricmp(Str_Text(ResourceNamespace_Name(rnamespace)), name))
return (resourcenamespaceid_t)(i+1);
}
}
return 0;
}
static void destroyAllNamespaces(void)
{
if(numNamespaces == 0)
return;
{ uint i;
for(i = 0; i < numNamespaces; ++i)
{
resourcenamespace_t* rnamespace = namespaces[i];
if(NULL != ResourceNamespace_Directory(rnamespace))
{
FileDirectory_Destruct(ResourceNamespace_Directory(rnamespace));
}
ResourceNamespace_Destruct(rnamespace);
}}
free(namespaces);
namespaces = 0;
}
static void resetAllNamespaces(void)
{
uint i;
for(i = 0; i < numNamespaces; ++i)
ResourceNamespace_Reset(namespaces[i]);
}
static void clearNamespaceSearchPaths(resourcenamespaceid_t rni)
{
if(rni == 0)
{
uint i;
for(i = 0; i < numNamespaces; ++i)
ResourceNamespace_ClearExtraSearchPaths(namespaces[i]);
return;
}
ResourceNamespace_ClearExtraSearchPaths(getNamespaceForId(rni));
}
static boolean tryFindResource2(resourceclass_t rclass, const ddstring_t* searchPath,
ddstring_t* foundPath, resourcenamespace_t* rnamespace)
{
assert(inited && searchPath && !Str_IsEmpty(searchPath));
// Is there a namespace we should use?
if(rnamespace && ResourceNamespace_Find2(rnamespace, searchPath, foundPath))
{
if(foundPath)
F_PrependBasePath(foundPath, foundPath);
return true;
}
if(0 != F_Access(Str_Text(searchPath)))
{
if(foundPath)
F_PrependBasePath(foundPath, searchPath);
return true;
}
return false;
}
static boolean tryFindResource(resourceclass_t rclass, const ddstring_t* searchPath,
ddstring_t* foundPath, resourcenamespace_t* rnamespace)
{
assert(inited && searchPath && !Str_IsEmpty(searchPath));
{
boolean found = false;
const char* ptr;
// Has an extension been specified?
ptr = F_FindFileExtension(Str_Text(searchPath));
if(ptr && *ptr != '*') // Try this first.
found = tryFindResource2(rclass, searchPath, foundPath, rnamespace);
if(!found)
{
ddstring_t path2;
Str_Init(&path2);
// Create a copy of the searchPath minus file extension.
if(ptr)
{
Str_PartAppend(&path2, Str_Text(searchPath), 0, ptr - Str_Text(searchPath));
}
else
{
Str_Copy(&path2, searchPath);
Str_AppendChar(&path2, '.');
}
if(VALID_RESOURCE_CLASS(rclass) && searchTypeOrder[rclass][0] != RT_NONE)
{
const resourcetype_t* type = searchTypeOrder[rclass];
ddstring_t tmp;
Str_Init(&tmp);
do
{
const resourcetypeinfo_t* info = getInfoForResourceType(*type);
if(info->knownFileNameExtensions[0])
{
char* const* ext = info->knownFileNameExtensions;
do
{
Str_Clear(&tmp);
Str_Appendf(&tmp, "%s%s", Str_Text(&path2), *ext);
found = tryFindResource2(rclass, &tmp, foundPath, rnamespace);
} while(!found && *(++ext));
}
} while(!found && *(++type) != RT_NONE);
Str_Free(&tmp);
}
Str_Free(&path2);
}
return found;
}
}
static boolean findResource2(resourceclass_t rclass, const ddstring_t* searchPath,
const ddstring_t* optionalSuffix, ddstring_t* foundPath, resourcenamespace_t* rnamespace)
{
assert(inited && searchPath && !Str_IsEmpty(searchPath));
{
boolean found = false;
#if _DEBUG
VERBOSE2( Con_Message("Using rnamespace '%s'...\n", rnamespace? Str_Text(ResourceNamespace_Name(rnamespace)) : "None") )
#endif
// First try with the optional suffix.
if(optionalSuffix)
{
ddstring_t fn;
Str_Init(&fn);
// Has an extension been specified?
{ const char* ptr = F_FindFileExtension(Str_Text(searchPath));
if(ptr && *ptr != '*')
{
char ext[10];
strncpy(ext, ptr, 10);
Str_PartAppend(&fn, Str_Text(searchPath), 0, ptr - 1 - Str_Text(searchPath));
Str_Appendf(&fn, "%s%s", Str_Text(optionalSuffix), ext);
}
else
{
Str_Appendf(&fn, "%s%s", Str_Text(searchPath), Str_Text(optionalSuffix));
}}
found = tryFindResource(rclass, &fn, foundPath, rnamespace);
Str_Free(&fn);
}
// Try without a suffix.
if(!found)
found = tryFindResource(rclass, searchPath, foundPath, rnamespace);
return found;
}
}
static int findResource(resourceclass_t rclass, const dduri_t* const* list,
const ddstring_t* optionalSuffix, ddstring_t* foundPath)
{
assert(inited && list && (rclass == RC_UNKNOWN || VALID_RESOURCE_CLASS(rclass)));
{
uint result = 0, n = 1;
const dduri_t* const* ptr;
for(ptr = list; *ptr; ptr++, n++)
{
const dduri_t* searchPath = *ptr;
ddstring_t* resolvedPath;
if((resolvedPath = Uri_Resolved(searchPath)) == 0)
continue; // Incomplete path; ignore it.
// If this is an absolute path, locate using it.
if(F_IsAbsolute(resolvedPath))
{
if(findResource2(rclass, resolvedPath, optionalSuffix, foundPath, 0))
result = n;
}
else
{ // Probably a relative path.
// Has a namespace identifier been included?
if(!Str_IsEmpty(Uri_Scheme(searchPath)))
{
resourcenamespaceid_t rni;
if((rni = F_SafeResourceNamespaceForName(Str_Text(Uri_Scheme(searchPath)))) != 0)
{
if(findResource2(rclass, resolvedPath, optionalSuffix, foundPath, getNamespaceForId(rni)))
result = n;
}
else
{
ddstring_t* rawPath = Uri_ToString(searchPath);
Con_Message("tryLocateResource: Unknown rnamespace in searchPath \"%s\", "
"using default for %s.\n", Str_Text(rawPath), F_ResourceClassStr(rclass));
Str_Delete(rawPath);
}
}
}
Str_Delete(resolvedPath);
if(result != 0)
break;
}
return result;
}
}
ddstring_t* F_ComposeHashNameForFilePath(const ddstring_t* filePath)
{
ddstring_t* hashName = Str_New();
F_FileName(hashName, Str_Text(filePath));
return hashName;
}
resourcenamespace_namehash_key_t F_HashKeyForAlphaNumericNameIgnoreCase(const ddstring_t* name)
{
assert(name);
{
resourcenamespace_namehash_key_t key = 0;
byte op = 0;
const char* c;
for(c = Str_Text(name); *c; c++)
{
switch(op)
{
case 0: key ^= tolower(*c); ++op; break;
case 1: key *= tolower(*c); ++op; break;
case 2: key -= tolower(*c); op=0; break;
}
}
return key % RESOURCENAMESPACE_HASHSIZE;
}
}
static void createPackagesResourceNamespace(void)
{
ddstring_t** doomWadPaths = 0, *doomWadDir = 0;
uint doomWadPathsCount = 0, searchPathsCount, idx;
filedirectory_t* directory;
dduri_t** searchPaths;
// Is the DOOMWADPATH environment variable in use?
if(!ArgCheck("-nodoomwadpath") && getenv("DOOMWADPATH"))
{
#if WIN32
# define PATH_DELIMITER_CHAR ';'
#else
# define PATH_DELIMITER_CHAR ':'
#endif
#define ADDDOOMWADPATH(path) \
{ \
Str_Strip(path); \
if(!Str_IsEmpty(path) && F_IsAbsolute(path)) \
{ \
char last = Str_RAt(path, 0); \
ddstring_t* pathCopy = Str_New(); \
if(last != DIR_SEP_CHAR && last != DIR_WRONG_SEP_CHAR) \
Str_Appendf(doomWadDir, "%s"DIR_SEP_STR, Str_Text(path)); \
else \
Str_Copy(pathCopy, (path)); \
doomWadPaths = realloc(doomWadPaths, sizeof(*doomWadPaths) * ++doomWadPathsCount); \
doomWadPaths[doomWadPathsCount-1] = pathCopy; \
} \
}
ddstring_t fullString; Str_Init(&fullString);
Str_Set(&fullString, getenv("DOOMWADPATH"));
Str_Strip(&fullString);
if(!Str_IsEmpty(&fullString))
{
ddstring_t path; Str_Init(&path);
// Split into paths.
{ const char* c = Str_Text(&fullString);
while((c = Str_CopyDelim2(&path, c, PATH_DELIMITER_CHAR, CDF_OMIT_DELIMITER))) // Get the next path.
{
ADDDOOMWADPATH(&path)
}}
// Add the last path.
if(!Str_IsEmpty(&path))
{
ADDDOOMWADPATH(&path)
}
Str_Free(&path);
}
Str_Free(&fullString);
#undef ADDDOOMWADPATH
#undef PATH_DELIMITER_CHAR
}
// Is the DOOMWADDIR environment variable in use?
if(!ArgCheck("-nodoomwaddir") && getenv("DOOMWADDIR"))
{
boolean pathIsGood = false;
doomWadDir = Str_New(); Str_Set(doomWadDir, getenv("DOOMWADDIR"));
Str_Strip(doomWadDir);
if(Str_IsEmpty(doomWadDir) || !F_IsAbsolute(doomWadDir))
{
Str_Delete(doomWadDir);
doomWadDir = 0;
}
else
{
char last = Str_RAt(doomWadDir, 0);
if(last != DIR_SEP_CHAR && last != DIR_WRONG_SEP_CHAR)
Str_AppendChar(doomWadDir, DIR_SEP_CHAR);
}
}
// Construct the search path list.
searchPathsCount = 2 + doomWadPathsCount + (doomWadDir != 0? 1 : 0);
if((searchPaths = malloc(sizeof(*searchPaths) * searchPathsCount)) == 0)
Con_Error("createPackagesResourceNamespace: Failed on allocation of %lu bytes.",
(unsigned long) (sizeof(*searchPaths) * searchPathsCount));
idx = 0;
// Add the default paths.
searchPaths[idx++] = Uri_Construct2("$(GameInfo.DataPath)/", RC_NULL);
searchPaths[idx++] = Uri_Construct2("$(App.DataPath)/", RC_NULL);
// Add any paths from the DOOMWADPATH environment variable.
if(doomWadPaths != 0)
{
uint i;
for(i = 0; i < doomWadPathsCount; ++i)
{
searchPaths[idx++] = Uri_Construct2(Str_Text(doomWadPaths[i]), RC_NULL);
Str_Delete(doomWadPaths[i]);
}
free(doomWadPaths);
}
// Add the path from the DOOMWADDIR environment variable.
if(doomWadDir != 0)
{
searchPaths[idx++] = Uri_Construct2(Str_Text(doomWadDir), RC_NULL);
Str_Delete(doomWadDir);
}
directory = FileDirectory_ConstructDefault();
F_CreateResourceNamespace(PACKAGES_RESOURCE_NAMESPACE_NAME, directory, F_ComposeHashNameForFilePath,
F_HashKeyForFilePathHashName, (const dduri_t**)searchPaths, searchPathsCount, 0, 0, 0);
for(idx = 0; idx < searchPathsCount; ++idx)
Uri_Destruct(searchPaths[idx]);
free(searchPaths);
}
void F_CreateNamespacesForFileResourcePaths(void)
{
#define NAMESPACEDEF_MAX_SEARCHPATHS 5
struct namespacedef_s {
const char* name;
const char* searchPaths[NAMESPACEDEF_MAX_SEARCHPATHS];
byte flags;
const char* overrideName;
const char* overrideName2;
} defs[] = {
{ DEFINITIONS_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DefsPath)/$(GameInfo.IdentityKey)/", "$(GameInfo.DefsPath)/", "$(App.DefsPath)/" } },
{ GRAPHICS_RESOURCE_NAMESPACE_NAME, { "$(App.DataPath)/graphics/" }, 0, "-gfxdir", "-gfxdir2" },
{ MODELS_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DataPath)/models/$(GameInfo.IdentityKey)/", "$(GameInfo.DataPath)/models/" }, RNF_USE_VMAP, "-modeldir", "-modeldir2" },
{ SOUNDS_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DataPath)/sfx/$(GameInfo.IdentityKey)/", "$(GameInfo.DataPath)/sfx/" }, RNF_USE_VMAP, "-sfxdir", "-sfxdir2" },
{ MUSIC_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DataPath)/music/$(GameInfo.IdentityKey)/", "$(GameInfo.DataPath)/music/" }, RNF_USE_VMAP, "-musdir", "-musdir2" },
{ TEXTURES_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DataPath)/textures/$(GameInfo.IdentityKey)/", "$(GameInfo.DataPath)/textures/" }, RNF_USE_VMAP, "-texdir", "-texdir2" },
{ FLATS_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DataPath)/flats/$(GameInfo.IdentityKey)/", "$(GameInfo.DataPath)/flats/" }, RNF_USE_VMAP, "-flatdir", "-flatdir2" },
{ PATCHES_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DataPath)/patches/$(GameInfo.IdentityKey)/", "$(GameInfo.DataPath)/patches/" }, RNF_USE_VMAP, "-patdir", "-patdir2" },
{ LIGHTMAPS_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DataPath)/lightmaps/$(GameInfo.IdentityKey)/", "$(GameInfo.DataPath)/lightmaps/" }, RNF_USE_VMAP, "-lmdir", "-lmdir2" },
{ FONTS_RESOURCE_NAMESPACE_NAME, { "$(GameInfo.DataPath)/fonts/$(GameInfo.IdentityKey)/", "$(GameInfo.DataPath)/fonts/", "$(App.DataPath)/fonts/" }, RNF_USE_VMAP, "-fontdir", "-fontdir2" },
{ NULL }
};
// Setup of the Packages namespace is somewhat more involved...
createPackagesResourceNamespace();
// Setup the rest...
{ size_t i;
for(i = 0; defs[i].name; ++i)
{
struct namespacedef_s* def = &defs[i];
dduri_t** searchPaths = 0;
uint j, searchPathsCount = 0;
filedirectory_t* directory;
for(searchPathsCount = 0; searchPathsCount < NAMESPACEDEF_MAX_SEARCHPATHS; ++searchPathsCount)
if(!def->searchPaths[searchPathsCount])
break;
if((searchPaths = malloc(sizeof(*searchPaths) * searchPathsCount)) == 0)
Con_Error("createResourceNamespaces: Failed on allocation of %lu bytes.",
(unsigned long) (sizeof(*searchPaths) * searchPathsCount));
for(j = 0; j < searchPathsCount; ++j)
searchPaths[j] = Uri_Construct2(def->searchPaths[j], RC_NULL);
directory = FileDirectory_ConstructDefault();
F_CreateResourceNamespace(def->name, directory, F_ComposeHashNameForFilePath, F_HashKeyForFilePathHashName,
(const dduri_t**)searchPaths, searchPathsCount, def->flags, def->overrideName, def->overrideName2);
for(j = 0; j < searchPathsCount; ++j)
Uri_Destruct(searchPaths[j]);
free(searchPaths);
}}
#undef NAMESPACEDEF_MAX_SEARCHPATHS
}
void F_InitResourceLocator(void)
{
// Allow re-init.
inited = true;
}
void F_ShutdownResourceLocator(void)
{
if(!inited)
return;
destroyAllNamespaces();
inited = false;
}
void F_ResetAllResourceNamespaces(void)
{
if(!inited)
return;
resetAllNamespaces();
}
struct resourcenamespace_s* F_ToResourceNamespace(resourcenamespaceid_t rni)
{
assert(inited);
return getNamespaceForId(rni);
}
resourcenamespaceid_t F_SafeResourceNamespaceForName(const char* name)
{
assert(inited);
return findNamespaceForName(name);
}
resourcenamespaceid_t F_ResourceNamespaceForName(const char* name)
{
assert(inited);
{
resourcenamespaceid_t result;
if((result = F_SafeResourceNamespaceForName(name)) == 0)
Con_Error("resourceNamespaceForName: Failed to locate resource namespace \"%s\".", name);
return result;
}
}
uint F_NumResourceNamespaces(void)
{
assert(inited);
return numNamespaces;
}
boolean F_IsValidResourceNamespaceId(int val)
{
assert(inited);
return (boolean)(val>0 && (unsigned)val < (F_NumResourceNamespaces()+1)? 1 : 0);
}
resourcenamespace_t* F_CreateResourceNamespace(const char* name,
filedirectory_t* directory, ddstring_t* (*composeHashNameFunc) (const ddstring_t* path),
resourcenamespace_namehash_key_t (*hashNameFunc) (const ddstring_t* name),
const dduri_t* const* searchPaths, int numSearchPaths, byte flags, const char* overrideName,
const char* overrideName2)
{
assert(inited && name);
{
resourcenamespace_t* rn = ResourceNamespace_Construct5(name, directory, composeHashNameFunc,
hashNameFunc, searchPaths, numSearchPaths, flags, overrideName, overrideName2);
namespaces = (resourcenamespace_t**) realloc(namespaces, sizeof(*namespaces) * ++numNamespaces);
if(namespaces == NULL)
Con_Error("F_CreateResourceNamespace: Failed on (re)allocation of %lu bytes for new resource namespace\n",
(unsigned long) (sizeof(*namespaces) * numNamespaces));
namespaces[numNamespaces-1] = rn;
return rn;
}
}
dduri_t** F_CreateUriList2(resourceclass_t rclass, const char* searchPaths,
size_t* count)
{
#define FIXEDSIZE (8)
dduri_t** list = 0, *localFixedList[FIXEDSIZE];
size_t numPaths = 0, n = 0;
ddstring_t buf;
const char* p;
if(!searchPaths || !searchPaths[0])
{
if(count)
*count = 0;
return 0;
}
Str_Init(&buf);
numPaths = n = 0;
p = searchPaths;
do
{
if(0 != numPaths)
{ // Prepare for another round.
Str_Clear(&buf);
}
for(; *p && *p != PATH_DELIMIT_CHAR; ++p)
{
Str_PartAppend(&buf, p, 0, 1);
}
// Skip past the delimiter if present.
if(*p) ++p;
if(0 != Str_Length(&buf))
{ // A new path was parsed; add it to the list.
if(n == FIXEDSIZE)
{
list = realloc(list, sizeof(*list) * (numPaths + 1));
memcpy(list + (numPaths - FIXEDSIZE), localFixedList, sizeof(*list) * FIXEDSIZE);
n = 0;
}
localFixedList[n++] = Uri_Construct2(Str_Text(&buf), rclass);
++numPaths;
}
} while(*p);
if(numPaths <= FIXEDSIZE)
{
list = malloc(sizeof(*list) * (numPaths + 1));
memcpy(list, localFixedList, sizeof(*list) * numPaths);
}
else if(n > 1)
{
list = realloc(list, sizeof(*list) * (numPaths + 1));
memcpy(list + numPaths - n, localFixedList, sizeof(*list) * n);
}
else
{
list[numPaths-1] = localFixedList[0];
}
list[numPaths] = 0; // Terminate.
Str_Free(&buf);
if(count)
*count = numPaths;
return list;
#undef FIXEDSIZE
}
dduri_t** F_CreateUriList(resourceclass_t rclass, const char* searchPaths)
{
return F_CreateUriList2(rclass, searchPaths, 0);
}
dduri_t** F_CreateUriListStr2(resourceclass_t rclass, const ddstring_t* searchPaths,
size_t* count)
{
if(!searchPaths)
{
if(count)
*count = 0;
return 0;
}
return F_CreateUriList2(rclass, Str_Text(searchPaths), count);
}
dduri_t** F_CreateUriListStr(resourceclass_t rclass, const ddstring_t* searchPaths)
{
return F_CreateUriListStr2(rclass, searchPaths, 0);
}
void F_DestroyUriList(dduri_t** list)
{
if(list)
{
dduri_t** ptr;
for(ptr = list; *ptr; ptr++)
Uri_Destruct(*ptr);
free(list);
}
}
ddstring_t** F_ResolvePathList2(resourceclass_t defaultResourceClass,
const ddstring_t* pathList, size_t* count, char delimiter)
{
{dduri_t** uris;
if((uris = F_CreateUriListStr(RC_NULL, pathList)) != 0)
{
ddstring_t** paths = 0;
size_t numResolvedPaths = 0;
{ dduri_t** ptr;
for(ptr = uris; *ptr; ++ptr)
{
if(Uri_Resolved(*ptr) != 0) // Ignore incomplete paths.
++numResolvedPaths;
}}
if(numResolvedPaths != 0)
{
uint n = 0;
paths = malloc(sizeof(*paths) * (numResolvedPaths+1));
{ dduri_t** ptr;
for(ptr = uris; *ptr; ++ptr)
{
ddstring_t* resolvedPath;
if((resolvedPath = Uri_Resolved(*ptr)) == 0)
continue; // Incomplete path; ignore it.
// Let's try to make it a relative path.
F_RemoveBasePath(resolvedPath, resolvedPath);
paths[n++] = resolvedPath;
}}
paths[n] = 0; // Terminate.
}
F_DestroyUriList(uris);
if(count)
*count = numResolvedPaths;
return paths;
}}
if(count)
*count = 0;
return 0;
}
ddstring_t** F_ResolvePathList(resourceclass_t defaultResourceClass,
const ddstring_t* pathList, size_t* count)
{
return F_ResolvePathList2(defaultResourceClass, pathList, count, PATH_DELIMIT_CHAR);
}
void F_DestroyStringList(ddstring_t** list)
{
if(list)
{
ddstring_t** ptr;
for(ptr = list; *ptr; ptr++)
Str_Delete(*ptr);
free(list);
}
}
#if _DEBUG
void F_PrintStringList(const ddstring_t** strings, size_t stringsCount)
{
if(!strings || stringsCount == 0)
return;
{ const ddstring_t** ptr = strings;
size_t i;
for(i = 0; i < stringsCount && *ptr; ++i, ptr++)
Con_Printf(" \"%s\"\n", Str_Text(*ptr));
}
}
#endif
uint F_FindResource4(resourceclass_t rclass, const dduri_t** searchPaths,
ddstring_t* foundPath, const ddstring_t* optionalSuffix)
{
assert(inited);
if(rclass != RC_UNKNOWN && !VALID_RESOURCE_CLASS(rclass))
Con_Error("F_FindResource: Invalid resource class %i.\n", rclass);
if(!searchPaths)
return 0;
return findResource(rclass, searchPaths, optionalSuffix, foundPath);
}
uint F_FindResourceStr3(resourceclass_t rclass, const ddstring_t* searchPaths,
ddstring_t* foundPath, const ddstring_t* optionalSuffix)
{
assert(inited);
{
dduri_t** list;
int result = 0;
if(rclass != RC_UNKNOWN && !VALID_RESOURCE_CLASS(rclass))
Con_Error("F_FindResource: Invalid resource class %i.\n", rclass);
if(!searchPaths || Str_IsEmpty(searchPaths))
{
#if _DEBUG
Con_Message("F_FindResource: Invalid (NULL) search path, returning not-found.\n");
#endif
return 0;
}
if((list = F_CreateUriListStr(rclass, searchPaths)) != 0)
{
result = findResource(rclass, (const dduri_t**)list, optionalSuffix, foundPath);
F_DestroyUriList(list);
}
return result;
}
}
uint F_FindResourceForRecord(resourcerecord_t* rec, ddstring_t* foundPath)
{
return findResource(ResourceRecord_ResourceClass(rec), ResourceRecord_SearchPaths(rec), 0, foundPath);
}
uint F_FindResourceStr2(resourceclass_t rclass, const ddstring_t* searchPath,
ddstring_t* foundPath)
{
return F_FindResourceStr3(rclass, searchPath, foundPath, 0);
}
uint F_FindResourceStr(resourceclass_t rclass, const ddstring_t* searchPath)
{
return F_FindResourceStr2(rclass, searchPath, 0);
}
uint F_FindResource3(resourceclass_t rclass, const char* _searchPaths,
ddstring_t* foundPath, const char* _optionalSuffix)
{
ddstring_t searchPaths, optionalSuffix;
boolean hasOptionalSuffix = false;
uint result;
Str_Init(&searchPaths); Str_Set(&searchPaths, _searchPaths);
if(_optionalSuffix && _optionalSuffix[0])
{
Str_Init(&optionalSuffix); Str_Set(&optionalSuffix, _optionalSuffix);
hasOptionalSuffix = true;
}
result = F_FindResourceStr3(rclass, &searchPaths, foundPath, hasOptionalSuffix? &optionalSuffix : 0);
if(hasOptionalSuffix)
Str_Free(&optionalSuffix);
Str_Free(&searchPaths);
return result;
}
uint F_FindResource2(resourceclass_t rclass, const char* searchPaths, ddstring_t* foundPath)
{
return F_FindResource3(rclass, searchPaths, foundPath, 0);
}
uint F_FindResource(resourceclass_t rclass, const char* searchPaths)
{
return F_FindResource2(rclass, searchPaths, 0);
}
resourceclass_t F_DefaultResourceClassForType(resourcetype_t type)
{
assert(inited);
if(type == RT_NONE)
return RC_UNKNOWN;
return getInfoForResourceType(type)->defaultClass;
}
resourcenamespaceid_t F_DefaultResourceNamespaceForClass(resourceclass_t rclass)
{
assert(inited && VALID_RESOURCE_CLASS(rclass));
return F_ResourceNamespaceForName(Str_Text(&defaultNamespaceForClass[rclass]));
}
resourcetype_t F_GuessResourceTypeByName(const char* path)
{
assert(inited);
if(!path || !path[0])
return RT_NONE; // Unrecognizable.
// We require a file extension for this.
{ char* ext = strrchr(path, '.');
if(NULL != ext)
{
uint i;
++ext;
for(i = RT_FIRST; i < NUM_RESOURCE_TYPES; ++i)
{
const resourcetypeinfo_t* info = getInfoForResourceType((resourcetype_t)i);
// Check the extension.
if(info->knownFileNameExtensions[0])
{
char* const* cand = info->knownFileNameExtensions;
do
{
if(!stricmp(*cand, ext))
return (resourcetype_t)i;
} while(*(++cand));
}
}
}}
return RT_NONE; // Unrecognizable.
}
boolean F_ApplyPathMapping(ddstring_t* path)
{
assert(inited && path);
{ uint i = 0;
boolean result = false;
while(i < numNamespaces && !(result = ResourceNamespace_MapPath(namespaces[i++], path)));
return result;
}
}
const char* F_ResourceClassStr(resourceclass_t rclass)
{
assert(VALID_RESOURCE_CLASS(rclass));
{
static const char* resourceClassNames[RESOURCECLASS_COUNT] = {
"RC_PACKAGE",
"RC_DEFINITION",
"RC_GRAPHIC",
"RC_MODEL",
"RC_SOUND",
"RC_MUSIC",
"RC_FONT"
};
return resourceClassNames[(int)rclass];
}
}
void F_FileDir(ddstring_t* dst, const ddstring_t* src)
{
assert(NULL != dst && NULL != src);
{
/// \fixme Potentially truncates @a src to FILENAME_T_MAXLEN
directory_t* dir = Dir_ConstructFromPathDir(Str_Text(src));
Str_Set(dst, Dir_Path(dir));
Dir_Destruct(dir);
}
}
void F_FileName(ddstring_t* dst, const char* src)
{
#ifdef WIN32
char name[_MAX_FNAME];
#else
char name[NAME_MAX];
#endif
_splitpath(src, 0, 0, name, 0);
Str_Set(dst, name);
}
void F_FileNameAndExtension(ddstring_t* dst, const char* src)
{
#ifdef WIN32