/
dd_main.c
2698 lines (2248 loc) · 71 KB
/
dd_main.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 dd_main.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006-2007 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
*/
/**
* Engine Core
*/
// HEADER FILES ------------------------------------------------------------
#ifdef WIN32
# define _WIN32_DCOM
# include <objbase.h>
#endif
#include "de_platform.h"
#ifdef UNIX
# include <ctype.h>
#endif
#include "de_base.h"
#include "de_console.h"
#include "de_filesys.h"
#include "de_network.h"
#include "de_refresh.h"
#include "de_graphics.h"
#include "de_render.h"
#include "de_audio.h"
#include "de_ui.h"
#include "de_bsp.h"
#include "de_edit.h"
#include "filedirectory.h"
#include "abstractresource.h"
#include "resourcenamespace.h"
#include "m_misc.h"
#include "texture.h"
#include "displaymode.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
typedef struct ddvalue_s {
int* readPtr;
int* writePtr;
} ddvalue_t;
typedef struct autoload_s {
boolean loadFiles; // Should files be loaded right away.
int count; // Number of files loaded successfully.
} autoload_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static int DD_StartupWorker(void* paramaters);
static int DD_DummyWorker(void* paramaters);
static void DD_AutoLoad(void);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern int renderTextures;
extern int gotFrame;
extern int monochrome;
extern int gameDataFormat;
extern int gameDrawHUD;
extern int symbolicEchoMode;
// PUBLIC DATA DEFINITIONS -------------------------------------------------
filename_t ddBasePath = ""; // Doomsday root directory is at...?
filename_t ddRuntimePath, ddBinPath;
int isDedicated = false;
int verbose = 0; // For debug messages (-verbose).
//FILE* outFile; // Output file for console messages.
// List of file names, whitespace seperating (written to .cfg).
char* gameStartupFiles = "";
// Id of the currently running title finale if playing, else zero.
finaleid_t titleFinale = 0;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// List of game data files (specified via the command line or in a cfg, or
// found using the default search algorithm (e.g., /auto and DOOMWADDIR)).
static ddstring_t** gameResourceFileList = 0;
static size_t numGameResourceFileList = 0;
Game* theGame = NULL; // Currently active game.
// Game collection.
static Game** games = 0;
static int gamesCount = 0;
static Game* nullGame; // Special "null-game" object.
// CODE --------------------------------------------------------------------
/**
* Register the engine commands and variables.
*/
void DD_Register(void)
{
DD_RegisterLoop();
DD_RegisterInput();
F_Register();
B_Register(); // for control bindings
Con_Register();
DH_Register();
R_Register();
S_Register();
SBE_Register(); // for bias editor
Rend_Register();
GL_Register();
Net_Register();
I_Register();
H_Register();
DAM_Register();
BspBuilder_Register();
UI_Register();
Demo_Register();
P_ControlRegister();
FI_Register();
}
static int gameIndex(const Game* game)
{
int i;
if(game && !DD_IsNullGame(game))
{
for(i = 0; i < gamesCount; ++i)
{
if(game == games[i])
return i+1;
}
}
return 0;
}
static Game* findGameForId(gameid_t gameId)
{
if(gameId > 0 && gameId <= gamesCount)
return games[gameId-1];
return NULL; // Not found.
}
static Game* findGameForIdentityKey(const char* identityKey)
{
int i;
assert(identityKey && identityKey[0]);
for(i = 0; i < gamesCount; ++i)
{
Game* game = games[i];
if(!stricmp(Str_Text(Game_IdentityKey(game)), identityKey))
return game;
}
return NULL; // Not found.
}
static void addToPathList(ddstring_t*** list, size_t* listSize, const char* rawPath)
{
ddstring_t* newPath = Str_New();
assert(list && listSize && rawPath && rawPath[0]);
Str_Set(newPath, rawPath);
F_FixSlashes(newPath, newPath);
F_ExpandBasePath(newPath, newPath);
*list = realloc(*list, sizeof(**list) * ++(*listSize));
(*list)[(*listSize)-1] = newPath;
}
static void parseStartupFilePathsAndAddFiles(const char* pathString)
{
#define ATWSEPS ",; \t"
char* token, *buffer;
size_t len;
if(!pathString || !pathString[0]) return;
len = strlen(pathString);
buffer = (char*)malloc(len + 1);
if(!buffer) Con_Error("parseStartupFilePathsAndAddFiles: Failed on allocation of %lu bytes for parse buffer.", (unsigned long) (len+1));
strcpy(buffer, pathString);
token = strtok(buffer, ATWSEPS);
while(token)
{
F_AddFile(token, 0, false);
token = strtok(NULL, ATWSEPS);
}
free(buffer);
#undef ATWSEPS
}
static void destroyPathList(ddstring_t*** list, size_t* listSize)
{
assert(list && listSize);
if(*list)
{
size_t i;
for(i = 0; i < *listSize; ++i)
Str_Delete((*list)[i]);
free(*list); *list = 0;
}
*listSize = 0;
}
static Game* addGame(Game* game)
{
if(game)
{
games = (Game**)realloc(games, sizeof(*games) * ++gamesCount);
if(!games) Con_Error("addGame: Failed on allocation of %lu bytes enlarging Game list.", (unsigned long) (sizeof(*games) * gamesCount));
games[gamesCount-1] = game;
}
return game;
}
boolean DD_GameLoaded(void)
{
return !DD_IsNullGame(theGame);
}
int DD_GameCount(void)
{
return gamesCount;
}
Game* DD_GameByIndex(int idx)
{
if(idx > 0 && idx <= gamesCount)
return games[idx-1];
return NULL;
}
Game* DD_GameByIdentityKey(const char* identityKey)
{
if(identityKey && identityKey[0])
return findGameForIdentityKey(identityKey);
return NULL;
}
gameid_t DD_GameId(Game* game)
{
if(!game || game == nullGame) return 0; // Invalid id.
return (gameid_t)gameIndex(game);
}
boolean DD_IsNullGame(const Game* game)
{
if(!game) return false;
return game == nullGame;
}
static void populateGameInfo(GameInfo* info, Game* game)
{
info->identityKey = Str_Text(Game_IdentityKey(game));
info->title = Str_Text(Game_Title(game));
info->author = Str_Text(Game_Author(game));
}
boolean DD_GameInfo(GameInfo* info)
{
if(!info)
{
#if _DEBUG
Con_Message("Warning:DD_GameInfo: Received invalid info (=NULL), ignoring.");
#endif
return false;
}
memset(info, 0, sizeof(*info));
if(DD_GameLoaded())
{
populateGameInfo(info, theGame);
return true;
}
#if _DEBUG
Con_Message("DD_GameInfo: Warning, no game currently loaded - returning false.\n");
#endif
return false;
}
void DD_AddGameResource(gameid_t gameId, resourceclass_t rclass, int rflags,
const char* _names, void* params)
{
Game* game = findGameForId(gameId);
AbstractResource* rec;
ddstring_t name;
ddstring_t str;
const char* p;
if(!game)
Con_Error("DD_AddGameResource: Error, unknown game id %i.", gameId);
if(!VALID_RESOURCE_CLASS(rclass))
Con_Error("DD_AddGameResource: Unknown resource class %i.", (int)rclass);
if(!_names || !_names[0] || !strcmp(_names, ";"))
Con_Error("DD_AddGameResource: Invalid name argument.");
if(0 == (rec = AbstractResource_New(rclass, rflags)))
Con_Error("DD_AddGameResource: Unknown error occured during AbstractResource::Construct.");
// Add a name list to the game record.
Str_Init(&str);
Str_Set(&str, _names);
// Ensure the name list has the required terminating semicolon.
if(Str_RAt(&str, 0) != ';')
Str_Append(&str, ";");
p = Str_Text(&str);
Str_Init(&name);
while((p = Str_CopyDelim2(&name, p, ';', CDF_OMIT_DELIMITER)))
{
AbstractResource_AddName(rec, &name);
}
Str_Free(&name);
if(params)
switch(rclass)
{
case RC_PACKAGE: {
// Add an auto-identification file identityKey list to the game record.
ddstring_t identityKey;
const char* p;
// Ensure the identityKey list has the required terminating semicolon.
Str_Set(&str, (const char*) params);
if(Str_RAt(&str, 0) != ';')
Str_Append(&str, ";");
Str_Init(&identityKey);
p = Str_Text(&str);
while((p = Str_CopyDelim2(&identityKey, p, ';', CDF_OMIT_DELIMITER)))
{
AbstractResource_AddIdentityKey(rec, &identityKey);
}
Str_Free(&identityKey);
break;
}
default: break;
}
Game_AddResource(game, rclass, rec);
Str_Free(&str);
}
gameid_t DD_DefineGame(const GameDef* def)
{
Game* game;
if(!def)
{
#if _DEBUG
Con_Message("Warning:DD_DefineGame: Received invalid GameDef (=NULL), ignoring.");
#endif
return 0; // Invalid id.
}
// Game mode identity keys must be unique. Ensure that is the case.
if(findGameForIdentityKey(def->identityKey))
{
#if _DEBUG
Con_Message("Warning:DD_DefineGame: Failed adding game \"%s\", identity key '%s' already in use, ignoring.\n", def->defaultTitle, def->identityKey);
#endif
return 0; // Invalid id.
}
// Add this game to our records.
game = addGame(Game_FromDef(def));
if(game)
{
Game_SetPluginId(game, DD_PluginIdForActiveHook());
return DD_GameId(game);
}
return 0; // Invalid id.
}
gameid_t DD_GameIdForKey(const char* identityKey)
{
Game* game = findGameForIdentityKey(identityKey);
if(game)
{
return DD_GameId(game);
}
DEBUG_Message(("Warning:DD_GameIdForKey: Game \"%s\" not defined.\n", identityKey));
return 0; // Invalid id.
}
void DD_DestroyGames(void)
{
destroyPathList(&gameResourceFileList, &numGameResourceFileList);
if(games)
{
int i;
for(i = 0; i < gamesCount; ++i)
{
Game_Delete(games[i]);
}
free(games); games = 0;
gamesCount = 0;
}
if(nullGame)
{
Game_Delete(nullGame);
nullGame = NULL;
}
theGame = NULL;
}
/**
* Begin the Doomsday title animation sequence.
*/
void DD_StartTitle(void)
{
ddstring_t setupCmds;
const char* fontName;
ddfinale_t fin;
int i;
if(isDedicated || !Def_Get(DD_DEF_FINALE, "background", &fin)) return;
Str_Init(&setupCmds);
// Configure the predefined fonts (all normal, variable width).
fontName = R_ChooseVariableFont(FS_NORMAL, Window_Width(theWindow),
Window_Height(theWindow));
for(i = 1; i <= FIPAGE_NUM_PREDEFINED_FONTS; ++i)
{
Str_Appendf(&setupCmds, "prefont %i "FN_SYSTEM_NAME":%s\n", i, fontName);
}
// Configure the predefined colors.
for(i = 1; i <= MIN_OF(NUM_UI_COLORS, FIPAGE_NUM_PREDEFINED_FONTS); ++i)
{
ui_color_t* color = UI_Color(i-1);
Str_Appendf(&setupCmds, "precolor %i %f %f %f\n", i, color->red, color->green, color->blue);
}
titleFinale = FI_Execute2(fin.script, FF_LOCAL, Str_Text(&setupCmds));
Str_Free(&setupCmds);
}
/// @return @c true, iff the resource appears to be what we think it is.
static boolean recognizeWAD(const char* filePath, void* data)
{
lumpnum_t auxLumpBase = F_OpenAuxiliary3(filePath, 0, true);
boolean result = false;
if(auxLumpBase >= 0)
{
// Ensure all identity lumps are present.
if(data)
{
const ddstring_t* const* lumpNames = (const ddstring_t* const*) data;
result = true;
for(; result && *lumpNames; lumpNames++)
{
lumpnum_t lumpNum = F_CheckLumpNumForName2(Str_Text(*lumpNames), true);
if(lumpNum < 0)
{
result = false;
}
}
}
else
{
// Matched.
result = true;
}
F_CloseAuxiliary();
}
return result;
}
/// @return @c true, iff the resource appears to be what we think it is.
static boolean recognizeZIP(const char* filePath, void* data)
{
DFile* dfile = F_Open(filePath, "bf");
boolean result = false;
if(dfile)
{
result = ZipFile_Recognise(dfile);
/// @todo Check files. We should implement an auxiliary zip lumpdirectory...
F_Close(dfile);
}
return result;
}
/// @todo This logic should be encapsulated by AbstractResource.
static int validateResource(AbstractResource* rec, void* paramaters)
{
int validated = false;
if(AbstractResource_ResourceClass(rec) == RC_PACKAGE)
{
Uri* const* uriList = AbstractResource_SearchPaths(rec);
Uri* const* ptr;
int i = 0;
for(ptr = uriList; *ptr; ptr++, i++)
{
const ddstring_t* path;
path = AbstractResource_ResolvedPathWithIndex(rec, i, true/*locate resources*/);
if(!path) continue;
if(recognizeWAD(Str_Text(path), (void*)AbstractResource_IdentityKeys(rec)))
{
validated = true;
break;
}
else if(recognizeZIP(Str_Text(path), (void*)AbstractResource_IdentityKeys(rec)))
{
validated = true;
break;
}
}
}
else
{
// Other resource types are not validated.
validated = true;
}
AbstractResource_MarkAsFound(rec, validated);
return validated;
}
static void locateGameStartupResources(Game* game)
{
Game* oldGame = theGame;
uint i;
if(!game) return;
if(theGame != game)
{
/// \kludge Temporarily switch Game.
theGame = game;
// Re-init the resource locator using the search paths of this Game.
F_ResetAllResourceNamespaces();
}
for(i = RESOURCECLASS_FIRST; i < RESOURCECLASS_COUNT; ++i)
{
AbstractResource* const* records = Game_Resources(game, (resourceclass_t)i, 0);
AbstractResource* const* recordIt;
if(records)
for(recordIt = records; *recordIt; recordIt++)
{
AbstractResource* rec = *recordIt;
// We are only interested in startup resources at this time.
if(!(AbstractResource_ResourceFlags(rec) & RF_STARTUP)) continue;
validateResource(rec, 0);
}
}
if(theGame != oldGame)
{
/// \kludge Restore the old Game.
theGame = oldGame;
// Re-init the resource locator using the search paths of this Game.
F_ResetAllResourceNamespaces();
}
}
static boolean allGameStartupResourcesFound(Game* game)
{
if(!DD_IsNullGame(game))
{
uint i;
for(i = 0; i < RESOURCECLASS_COUNT; ++i)
{
AbstractResource* const* records = Game_Resources(game, (resourceclass_t)i, 0);
AbstractResource* const* recordIt;
if(records)
for(recordIt = records; *recordIt; recordIt++)
{
AbstractResource* rec = *recordIt;
const int flags = AbstractResource_ResourceFlags(rec);
if((flags & RF_STARTUP) && !(flags & RF_FOUND))
return false;
}
}
}
return true;
}
/**
* Print a game mode banner with rulers.
* @todo This has been moved here so that strings like the game
* title and author can be overridden (e.g., via DEHACKED). Make it so!
*/
static void printGameBanner(Game* game)
{
assert(game);
Con_PrintRuler();
Con_FPrintf(CPF_WHITE | CPF_CENTER, "%s\n", Str_Text(Game_Title(game)));
Con_PrintRuler();
}
static void printGameResources(Game* game, boolean printStatus, int rflags)
{
size_t count = 0;
uint i;
if(!game) return;
for(i = 0; i < RESOURCECLASS_COUNT; ++i)
{
AbstractResource* const* records = Game_Resources(game, (resourceclass_t)i, 0);
AbstractResource* const* recordIt;
if(records)
for(recordIt = records; *recordIt; recordIt++)
{
AbstractResource* rec = *recordIt;
if((rflags & RF_STARTUP) == (AbstractResource_ResourceFlags(rec) & RF_STARTUP))
{
AbstractResource_Print(rec, printStatus);
count += 1;
}
}
}
if(count == 0)
Con_Printf(" None\n");
}
void DD_PrintGame(Game* game, int flags)
{
if(DD_IsNullGame(game))
flags &= ~PGF_BANNER;
#if _DEBUG
Con_Printf("pluginid:%i data:\"%s\" defs:\"%s\"\n", (int)Game_PluginId(game),
F_PrettyPath(Str_Text(Game_DataPath(game))),
F_PrettyPath(Str_Text(Game_DefsPath(game))));
#endif
if(flags & PGF_BANNER)
printGameBanner(game);
if(!(flags & PGF_BANNER))
Con_Printf("Game: %s - ", Str_Text(Game_Title(game)));
else
Con_Printf("Author: ");
Con_Printf("%s\n", Str_Text(Game_Author(game)));
Con_Printf("IdentityKey: %s\n", Str_Text(Game_IdentityKey(game)));
if(flags & PGF_LIST_STARTUP_RESOURCES)
{
Con_Printf("Startup resources:\n");
printGameResources(game, (flags & PGF_STATUS) != 0, RF_STARTUP);
}
if(flags & PGF_LIST_OTHER_RESOURCES)
{
Con_Printf("Other resources:\n");
Con_Printf(" ");
printGameResources(game, /*(flags & PGF_STATUS) != 0*/false, 0);
}
if(flags & PGF_STATUS)
Con_Printf("Status: %s\n", theGame == game? "Loaded" :
allGameStartupResourcesFound(game)? "Complete/Playable" :
"Incomplete/Not playable");
}
/**
* (f_allresourcepaths_callback_t)
*/
static int autoDataAdder(const ddstring_t* fileName, pathdirectorynode_type_t type, void* paramaters)
{
assert(fileName && paramaters);
// We are only interested in files.
if(type == PT_LEAF)
{
autoload_t* data = (autoload_t*)paramaters;
if(data->loadFiles)
{
if(F_AddFile(Str_Text(fileName), 0, false))
++data->count;
}
else
{
addToPathList(&gameResourceFileList, &numGameResourceFileList, Str_Text(fileName));
}
}
return 0; // Continue searching.
}
/**
* Files with the extensions wad, lmp, pk3, zip and deh in the automatical data
* directory are added to gameResourceFileList.
*
* @return Number of new files that were loaded.
*/
static int addFilesFromAutoData(boolean loadFiles)
{
static const char* extensions[] = {
"wad", "lmp", "pk3", "zip", "deh",
#ifdef UNIX
"WAD", "LMP", "PK3", "ZIP", "DEH", // upper case alternatives
#endif
0
};
ddstring_t pattern;
autoload_t data;
uint i;
data.loadFiles = loadFiles;
data.count = 0;
Str_Init(&pattern);
for(i = 0; extensions[i]; ++i)
{
Str_Clear(&pattern);
Str_Appendf(&pattern, "%sauto/*.%s", Str_Text(Game_DataPath(theGame)), extensions[i]);
F_AllResourcePaths2(Str_Text(&pattern), 0, autoDataAdder, (void*)&data);
}
Str_Free(&pattern);
return data.count;
}
static boolean exchangeEntryPoints(pluginid_t pluginId)
{
if(pluginId != 0)
{
// Do the API transfer.
GETGAMEAPI fptAdr;
if(!(fptAdr = (GETGAMEAPI) DD_FindEntryPoint(pluginId, "GetGameAPI")))
return false;
app.GetGameAPI = fptAdr;
DD_InitAPI();
Def_GetGameClasses();
}
else
{
app.GetGameAPI = 0;
DD_InitAPI();
Def_GetGameClasses();
}
return true;
}
static void loadResource(AbstractResource* res)
{
if(!res) return;
switch(AbstractResource_ResourceClass(res))
{
case RC_PACKAGE: {
const ddstring_t* path = AbstractResource_ResolvedPath(res, false/*do not locate resource*/);
if(path)
{
DFile* file = F_AddFile(Str_Text(path), 0, false);
if(file)
{
// Mark this as an original game resource.
AbstractFile_SetCustom(DFile_File(file), false);
// Print the 'CRC' number of IWADs, so they can be identified.
if(FT_WADFILE == AbstractFile_Type(DFile_File(file)))
Con_Message(" IWAD identification: %08x\n", WadFile_CalculateCRC((WadFile*)DFile_File(file)));
}
}
break; }
default: Con_Error("loadGameResource: No resource loader found for %s.",
F_ResourceClassStr(AbstractResource_ResourceClass(res)));
}
}
typedef struct {
/// @c true iff caller (i.e., DD_ChangeGame) initiated busy mode.
boolean initiatedBusyMode;
} ddgamechange_paramaters_t;
static int DD_BeginGameChangeWorker(void* paramaters)
{
ddgamechange_paramaters_t* p = (ddgamechange_paramaters_t*)paramaters;
assert(p);
P_InitMapUpdate();
P_InitGameMapObjDefs();
if(p->initiatedBusyMode)
Con_SetProgress(100);
DAM_Init();
if(p->initiatedBusyMode)
{
Con_SetProgress(200);
Con_BusyWorkerEnd();
}
return 0;
}
static int DD_LoadGameStartupResourcesWorker(void* paramaters)
{
ddgamechange_paramaters_t* p = (ddgamechange_paramaters_t*)paramaters;
assert(p);
// Reset file Ids so previously seen files can be processed again.
F_ResetFileIds();
F_ResetAllResourceNamespaces();
F_InitVirtualDirectoryMappings();
if(p->initiatedBusyMode)
Con_SetProgress(50);
if(!DD_IsNullGame(theGame))
{
ddstring_t temp;
// Create default Auto mappings in the runtime directory.
// Data class resources.
Str_Init(&temp);
Str_Appendf(&temp, "%sauto", Str_Text(Game_DataPath(theGame)));
F_AddVirtualDirectoryMapping("auto", Str_Text(&temp));
// Definition class resources.
Str_Clear(&temp);
Str_Appendf(&temp, "%sauto", Str_Text(Game_DefsPath(theGame)));
F_AddVirtualDirectoryMapping("auto", Str_Text(&temp));
Str_Free(&temp);
}
/**
* Open all the files, load headers, count lumps, etc, etc...
* @note Duplicate processing of the same file is automatically guarded
* against by the virtual file system layer.
*/
Con_Message("Loading game resources%s\n", verbose >= 1? ":" : "...");
{ int numResources;
AbstractResource* const* resources;
if((resources = Game_Resources(theGame, RC_PACKAGE, &numResources)))
{
AbstractResource* const* resIt;
for(resIt = resources; *resIt; resIt++)
{
loadResource(*resIt);
// Update our progress.
if(p->initiatedBusyMode)
{
Con_SetProgress(((resIt - resources)+1) * (200-50)/numResources -1);
}
}
}}
if(p->initiatedBusyMode)
{
Con_SetProgress(200);
Con_BusyWorkerEnd();
}
return 0;
}
static int DD_LoadAddonResourcesWorker(void* paramaters)
{
ddgamechange_paramaters_t* p = (ddgamechange_paramaters_t*)paramaters;
assert(p);
/**
* Add additional game-startup files.
* \note These must take precedence over Auto but not game-resource files.
*/
if(gameStartupFiles && gameStartupFiles[0])
parseStartupFilePathsAndAddFiles(gameStartupFiles);
if(p->initiatedBusyMode)
Con_SetProgress(50);
if(!DD_IsNullGame(theGame))
{
/**
* Phase 3: Add real files from the Auto directory.
* First ZIPs then WADs (they may contain WAD files).
*/
addFilesFromAutoData(false);
if(numGameResourceFileList > 0)
{
size_t i, pass;
for(pass = 0; pass < 2; ++pass)
for(i = 0; i < numGameResourceFileList; ++i)
{
resourcetype_t resType = F_GuessResourceTypeByName(Str_Text(gameResourceFileList[i]));
if((pass == 0 && resType == RT_ZIP) ||
(pass == 1 && resType == RT_WAD))
F_AddFile(Str_Text(gameResourceFileList[i]), 0, false);
}
}
// Final autoload round.
DD_AutoLoad();
}
if(p->initiatedBusyMode)
Con_SetProgress(180);
// Re-initialize the resource locator as there are now new resources to be found
// on existing search paths (probably that is).
F_InitLumpDirectoryMappings();
F_ResetAllResourceNamespaces();
if(p->initiatedBusyMode)
{
Con_SetProgress(200);
Con_BusyWorkerEnd();
}
return 0;
}
static int DD_ActivateGameWorker(void* paramaters)
{
ddgamechange_paramaters_t* p = (ddgamechange_paramaters_t*)paramaters;
uint i;
assert(p);
// Texture resources are located now, prior to initializing the game.
R_InitPatchComposites();
R_InitFlatTextures();
R_InitSpriteTextures();
if(p->initiatedBusyMode)
Con_SetProgress(50);
// Now that resources have been located we can begin to initialize the game.
if(!DD_IsNullGame(theGame) && gx.PreInit)
gx.PreInit(DD_GameId(theGame));
if(p->initiatedBusyMode)
Con_SetProgress(100);
/**
* Parse the game's main config file.
* If a custom top-level config is specified; let it override.
*/
{ const ddstring_t* configFileName = 0;
ddstring_t tmp;
if(CommandLine_CheckWith("-config", 1))
{
Str_Init(&tmp); Str_Set(&tmp, CommandLine_Next());
F_FixSlashes(&tmp, &tmp);
configFileName = &tmp;
}
else
{
configFileName = Game_MainConfig(theGame);
}
Con_Message("Parsing primary config \"%s\"...\n", F_PrettyPath(Str_Text(configFileName)));