Subversion Repositories eduke32

Rev

Rev 5037 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2542 helixhorne 1
//
2
// Common non-engine code/data for EDuke32 and Mapster32
3
//
4
 
5
#include "compat.h"
2726 hendricks2 6
#include "build.h"
4584 helixhorne 7
#include "baselayer.h"
4559 hendricks2 8
 
4557 hendricks2 9
#include "grpscan.h"
2542 helixhorne 10
 
3581 hendricks2 11
#ifdef _WIN32
12
# include "winbits.h"
3671 hendricks2 13
# include <shlwapi.h>
14
# include <winnt.h>
15
# ifndef KEY_WOW64_32KEY
16
#  define KEY_WOW64_32KEY 0x0200
17
# endif
4801 hendricks2 18
#elif defined __APPLE__
19
# include "osxbits.h"
3581 hendricks2 20
#endif
21
 
2542 helixhorne 22
#include "common.h"
2726 hendricks2 23
#include "common_game.h"
2542 helixhorne 24
 
2789 hendricks2 25
int32_t g_gameType = GAMEFLAG_DUKE;
2542 helixhorne 26
 
4557 hendricks2 27
int32_t g_dependencyCRC = 0;
28
int32_t g_usingAddon = 0;
5062 hendricks2 29
void (*g_postprocessing)(void);
4557 hendricks2 30
 
31
// g_gameNamePtr can point to one of: grpfiles[].name (string literal), string
32
// literal, malloc'd block (XXX: possible leak)
33
const char *g_gameNamePtr = NULL;
34
 
4559 hendricks2 35
// grp/con handling
2726 hendricks2 36
 
4789 hendricks2 37
static const char *defaultgamegrp[GAMECOUNT] = { "DUKE3D.GRP", "NAM.GRP", "NAPALM.GRP", "WW2GI.GRP" };
38
static const char *defaultdeffilename[GAMECOUNT] = { "duke3d.def", "nam.def", "napalm.def", "ww2gi.def" };
39
static const char *defaultconfilename = "GAME.CON";
40
static const char *defaultgameconfilename[GAMECOUNT] = { "EDUKE.CON", "NAM.CON", "NAPALM.CON", "WW2GI.CON" };
2726 hendricks2 41
 
42
// g_grpNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
43
char *g_grpNamePtr = NULL;
44
// g_scriptNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
45
char *g_scriptNamePtr = NULL;
46
 
47
void clearGrpNamePtr(void)
48
{
49
    if (g_grpNamePtr != NULL)
50
        Bfree(g_grpNamePtr);
51
    // g_grpNamePtr assumed to be assigned to right after
52
}
53
 
54
void clearScriptNamePtr(void)
55
{
56
    if (g_scriptNamePtr != NULL)
57
        Bfree(g_scriptNamePtr);
58
    // g_scriptNamePtr assumed to be assigned to right after
59
}
60
 
2796 helixhorne 61
const char *G_DefaultGrpFile(void)
2726 hendricks2 62
{
63
    if (DUKE)
64
        return defaultgamegrp[GAME_DUKE];
65
    // order is important for the following three because GAMEFLAG_NAM overlaps all
66
    else if (NAPALM)
67
        return defaultgamegrp[GAME_NAPALM];
68
    else if (WW2GI)
69
        return defaultgamegrp[GAME_WW2GI];
70
    else if (NAM)
71
        return defaultgamegrp[GAME_NAM];
72
 
73
    return defaultgamegrp[0];
74
}
2796 helixhorne 75
const char *G_DefaultDefFile(void)
2726 hendricks2 76
{
77
    if (DUKE)
78
        return defaultdeffilename[GAME_DUKE];
79
    else if (WW2GI)
80
        return defaultdeffilename[GAME_WW2GI];
81
    else if (NAPALM)
82
    {
2752 helixhorne 83
        if (!testkopen(defaultdeffilename[GAME_NAPALM],0) && testkopen(defaultdeffilename[GAME_NAM],0))
3618 hendricks2 84
            return defaultdeffilename[GAME_NAM]; // NAM/NAPALM Sharing
2726 hendricks2 85
        else
86
            return defaultdeffilename[GAME_NAPALM];
87
    }
88
    else if (NAM)
89
    {
2752 helixhorne 90
        if (!testkopen(defaultdeffilename[GAME_NAM],0) && testkopen(defaultdeffilename[GAME_NAPALM],0))
3618 hendricks2 91
            return defaultdeffilename[GAME_NAPALM]; // NAM/NAPALM Sharing
2726 hendricks2 92
        else
93
            return defaultdeffilename[GAME_NAM];
94
    }
95
 
96
    return defaultdeffilename[0];
97
}
2796 helixhorne 98
const char *G_DefaultConFile(void)
2726 hendricks2 99
{
2752 helixhorne 100
    if (DUKE && testkopen(defaultgameconfilename[GAME_DUKE],0))
2726 hendricks2 101
        return defaultgameconfilename[GAME_DUKE];
2752 helixhorne 102
    else if (WW2GI && testkopen(defaultgameconfilename[GAME_WW2GI],0))
2726 hendricks2 103
        return defaultgameconfilename[GAME_WW2GI];
104
    else if (NAPALM)
105
    {
2752 helixhorne 106
        if (!testkopen(defaultgameconfilename[GAME_NAPALM],0))
2726 hendricks2 107
        {
2752 helixhorne 108
            if (testkopen(defaultgameconfilename[GAME_NAM],0))
3618 hendricks2 109
                return defaultgameconfilename[GAME_NAM]; // NAM/NAPALM Sharing
2726 hendricks2 110
        }
111
        else
112
            return defaultgameconfilename[GAME_NAPALM];
113
    }
114
    else if (NAM)
115
    {
2752 helixhorne 116
        if (!testkopen(defaultgameconfilename[GAME_NAM],0))
2726 hendricks2 117
        {
2752 helixhorne 118
            if (testkopen(defaultgameconfilename[GAME_NAPALM],0))
3618 hendricks2 119
                return defaultgameconfilename[GAME_NAPALM]; // NAM/NAPALM Sharing
2726 hendricks2 120
        }
121
        else
122
            return defaultgameconfilename[GAME_NAM];
123
    }
124
 
125
    return defaultconfilename;
126
}
127
 
2796 helixhorne 128
const char *G_GrpFile(void)
2726 hendricks2 129
{
130
    if (g_grpNamePtr == NULL)
131
        return G_DefaultGrpFile();
132
    else
133
        return g_grpNamePtr;
134
}
3654 terminx 135
 
2796 helixhorne 136
const char *G_DefFile(void)
2726 hendricks2 137
{
138
    if (g_defNamePtr == NULL)
139
        return G_DefaultDefFile();
140
    else
141
        return g_defNamePtr;
142
}
3654 terminx 143
 
2796 helixhorne 144
const char *G_ConFile(void)
2726 hendricks2 145
{
146
    if (g_scriptNamePtr == NULL)
147
        return G_DefaultConFile();
148
    else
149
        return g_scriptNamePtr;
150
}
151
 
152
//////////
153
 
3976 helixhorne 154
#define NUMPSKYMULTIS 5
155
EDUKE32_STATIC_ASSERT(NUMPSKYMULTIS <= MAXPSKYMULTIS);
156
EDUKE32_STATIC_ASSERT(PSKYOFF_MAX <= MAXPSKYTILES);
157
 
3975 helixhorne 158
// Set up new-style multi-psky handling.
3976 helixhorne 159
void G_InitMultiPsky(int32_t CLOUDYOCEAN__DYN, int32_t MOONSKY1__DYN, int32_t BIGORBIT1__DYN, int32_t LA__DYN)
3220 hendricks2 160
{
161
    int32_t i;
162
 
3976 helixhorne 163
    psky_t *defaultsky = &multipsky[0];
164
    psky_t *oceansky = &multipsky[1];
165
    psky_t *moonsky = &multipsky[2];
166
    psky_t *spacesky = &multipsky[3];
167
    psky_t *citysky = &multipsky[4];
3220 hendricks2 168
 
3975 helixhorne 169
    static int32_t inited;
170
    if (inited)
171
        return;
172
    inited = 1;
3220 hendricks2 173
 
3976 helixhorne 174
    multipskytile[0] = -1;
175
    multipskytile[1] = CLOUDYOCEAN__DYN;
176
    multipskytile[2] = MOONSKY1__DYN;
177
    multipskytile[3] = BIGORBIT1__DYN;
178
    multipskytile[4] = LA__DYN;
3220 hendricks2 179
 
3976 helixhorne 180
    pskynummultis = NUMPSKYMULTIS;
3220 hendricks2 181
 
3975 helixhorne 182
    // When adding other multi-skies, take care that the tileofs[] values are
183
    // <= PSKYOFF_MAX. (It can be increased up to MAXPSKYTILES, but should be
184
    // set as tight as possible.)
185
 
3976 helixhorne 186
    // The default sky properties (all others are implicitly zero):
187
    defaultsky->lognumtiles = 3;
188
    defaultsky->horizfrac = 32768;
189
 
190
    // CLOUDYOCEAN
191
    // Aligns with the drawn scene horizon because it has one itself.
192
    oceansky->lognumtiles = 3;
193
    oceansky->horizfrac = 65536;
194
 
3220 hendricks2 195
    // MOONSKY1
196
    //        earth          mountain   mountain         sun
3975 helixhorne 197
    moonsky->lognumtiles = 3;
198
    moonsky->horizfrac = 32768;
199
    moonsky->tileofs[6] = 1;
200
    moonsky->tileofs[1] = 2;
201
    moonsky->tileofs[4] = 2;
202
    moonsky->tileofs[2] = 3;
3220 hendricks2 203
 
204
    // BIGORBIT1   // orbit
205
    //       earth1         2           3           moon/sun
3975 helixhorne 206
    spacesky->lognumtiles = 3;
207
    spacesky->horizfrac = 32768;
208
    spacesky->tileofs[5] = 1;
209
    spacesky->tileofs[6] = 2;
210
    spacesky->tileofs[7] = 3;
211
    spacesky->tileofs[2] = 4;
3220 hendricks2 212
 
213
    // LA // la city
214
    //       earth1         2           3           moon/sun
3975 helixhorne 215
    citysky->lognumtiles = 3;
216
    citysky->horizfrac = 16384+1024;
217
    citysky->tileofs[0] = 1;
218
    citysky->tileofs[1] = 2;
219
    citysky->tileofs[2] = 1;
220
    citysky->tileofs[3] = 3;
221
    citysky->tileofs[4] = 4;
222
    citysky->tileofs[5] = 0;
223
    citysky->tileofs[6] = 2;
224
    citysky->tileofs[7] = 3;
3220 hendricks2 225
 
3975 helixhorne 226
    for (i=0; i<pskynummultis; ++i)
227
    {
228
        int32_t j;
229
        for (j=0; j<(1<<multipsky[i].lognumtiles); ++j)
230
            Bassert(multipsky[i].tileofs[j] <= PSKYOFF_MAX);
231
    }
3976 helixhorne 232
}
3220 hendricks2 233
 
3976 helixhorne 234
void G_SetupGlobalPsky(void)
235
{
236
    int32_t i, mskyidx=0;
237
 
238
    // NOTE: Loop must be running backwards for the same behavior as the game
239
    // (greatest sector index with matching parallaxed sky takes precedence).
240
    for (i=numsectors-1; i>=0; i--)
241
    {
242
        if (sector[i].ceilingstat & 1)
243
        {
4006 helixhorne 244
            mskyidx = getpskyidx(sector[i].ceilingpicnum);
3976 helixhorne 245
            if (mskyidx > 0)
246
                break;
247
        }
248
    }
249
 
250
    g_pskyidx = mskyidx;
3220 hendricks2 251
}
252
 
253
//////////
254
 
4557 hendricks2 255
static char g_rootDir[BMAX_PATH];
256
char g_modDir[BMAX_PATH] = "/";
257
 
258
int32_t kopen4loadfrommod(const char *filename, char searchfirst)
259
{
260
    int32_t r=-1;
261
 
262
    if (g_modDir[0]!='/' || g_modDir[1]!=0)
263
    {
264
        static char fn[BMAX_PATH];
265
 
266
        Bsnprintf(fn, sizeof(fn), "%s/%s",g_modDir,filename);
267
        r = kopen4load(fn,searchfirst);
268
    }
269
 
270
    if (r < 0)
271
        r = kopen4load(filename,searchfirst);
272
 
273
    return r;
274
}
275
 
276
int32_t usecwd;
277
static void G_LoadAddon(void);
278
int32_t g_groupFileHandle;
279
 
280
void G_ExtPreInit(int32_t argc,const char **argv)
281
{
282
    usecwd = G_CheckCmdSwitch(argc, argv, "-usecwd");
283
 
284
#ifdef _WIN32
285
    GetModuleFileName(NULL,g_rootDir,BMAX_PATH);
286
    Bcorrectfilename(g_rootDir,1);
287
    //chdir(g_rootDir);
288
#else
289
    getcwd(g_rootDir,BMAX_PATH);
290
    strcat(g_rootDir,"/");
3582 hendricks2 291
#endif
4557 hendricks2 292
}
3582 hendricks2 293
 
4557 hendricks2 294
void G_ExtInit(void)
3582 hendricks2 295
{
4557 hendricks2 296
    char cwd[BMAX_PATH];
297
 
5005 hendricks2 298
#ifdef EDUKE32_OSX
4801 hendricks2 299
    char *appdir = Bgetappdir();
300
    addsearchpath(appdir);
301
    Bfree(appdir);
3582 hendricks2 302
#endif
4801 hendricks2 303
 
304
    if (getcwd(cwd,BMAX_PATH) && Bstrcmp(cwd,"/") != 0)
4557 hendricks2 305
        addsearchpath(cwd);
306
 
307
    if (CommandPaths)
308
    {
309
        int32_t i;
310
        struct strllist *s;
311
        while (CommandPaths)
312
        {
313
            s = CommandPaths->next;
314
            i = addsearchpath(CommandPaths->str);
315
            if (i < 0)
316
            {
317
                initprintf("Failed adding %s for game data: %s\n", CommandPaths->str,
318
                           i==-1 ? "not a directory" : "no such directory");
319
            }
320
 
321
            Bfree(CommandPaths->str);
322
            Bfree(CommandPaths);
323
            CommandPaths = s;
324
        }
325
    }
326
 
327
#if defined(_WIN32)
328
    if (!access("user_profiles_enabled", F_OK))
329
#else
330
    if (usecwd == 0 && access("user_profiles_disabled", F_OK))
331
#endif
332
    {
333
        char *homedir;
334
        int32_t asperr;
335
 
336
        if ((homedir = Bgethomedir()))
337
        {
338
            Bsnprintf(cwd,sizeof(cwd),"%s/"
339
#if defined(_WIN32)
340
                      "EDuke32 Settings"
341
#elif defined(GEKKO)
342
                      "apps/eduke32"
343
#else
344
                      ".eduke32"
345
#endif
346
                      ,homedir);
347
            asperr = addsearchpath(cwd);
348
            if (asperr == -2)
349
            {
350
                if (Bmkdir(cwd,S_IRWXU) == 0) asperr = addsearchpath(cwd);
351
                else asperr = -1;
352
            }
353
            if (asperr == 0)
4697 terminx 354
                Bchdir(cwd);
4557 hendricks2 355
            Bfree(homedir);
356
        }
357
    }
358
 
359
    // JBF 20031220: Because it's annoying renaming GRP files whenever I want to test different game data
360
    if (g_grpNamePtr == NULL)
361
    {
362
        const char *cp = getenv("DUKE3DGRP");
363
        if (cp)
364
        {
365
            clearGrpNamePtr();
366
            g_grpNamePtr = dup_filename(cp);
367
            initprintf("Using \"%s\" as main GRP file\n", g_grpNamePtr);
368
        }
369
    }
3582 hendricks2 370
}
371
 
4989 terminx 372
void G_ScanGroups(void)
4557 hendricks2 373
{
374
    ScanGroups();
4989 terminx 375
 
376
    // try and identify the 'defaultgamegrp' in the set of GRPs.
377
    // if it is found, set up the environment accordingly for the game it represents.
378
    // if it is not found, choose the first GRP from the list
379
    struct grpfile *fg, *first = NULL;
380
 
381
    for (fg = foundgrps; fg; fg=fg->next)
4557 hendricks2 382
    {
4989 terminx 383
        struct grpfile *grp;
384
        for (grp = listgrps; grp; grp=grp->next)
385
            if (fg->crcval == grp->crcval) break;
4557 hendricks2 386
 
4989 terminx 387
        if (grp == NULL)
388
            continue;
389
 
390
        fg->game = grp->game;
391
        if (!first) first = fg;
392
        if (!Bstrcasecmp(fg->name, G_DefaultGrpFile()))
4557 hendricks2 393
        {
4989 terminx 394
            g_gameType = grp->game;
395
            g_gameNamePtr = grp->name;
396
            break;
4557 hendricks2 397
        }
4989 terminx 398
    }
399
    if (!fg && first)
400
    {
401
        if (g_grpNamePtr == NULL)
4557 hendricks2 402
        {
4989 terminx 403
            clearGrpNamePtr();
404
            g_grpNamePtr = dup_filename(first->name);
4557 hendricks2 405
        }
4989 terminx 406
        g_gameType = first->game;
407
        g_gameNamePtr = listgrps->name;
4557 hendricks2 408
    }
4989 terminx 409
    else if (!fg) g_gameNamePtr = NULL;
4557 hendricks2 410
}
411
 
4989 terminx 412
void G_LoadGroups(int32_t autoload)
4557 hendricks2 413
{
414
    if (g_modDir[0] != '/')
415
    {
416
        char cwd[BMAX_PATH];
417
 
4989 terminx 418
        Bstrcat(g_rootDir, g_modDir);
4557 hendricks2 419
        addsearchpath(g_rootDir);
4989 terminx 420
        //        addsearchpath(mod_dir);
4557 hendricks2 421
 
4989 terminx 422
        if (getcwd(cwd, BMAX_PATH))
4557 hendricks2 423
        {
4989 terminx 424
            Bsprintf(cwd, "%s/%s", cwd, g_modDir);
4557 hendricks2 425
            if (!Bstrcmp(g_rootDir, cwd))
426
            {
427
                if (addsearchpath(cwd) == -2)
4989 terminx 428
                    if (Bmkdir(cwd, S_IRWXU) == 0)
429
                        addsearchpath(cwd);
4557 hendricks2 430
            }
431
        }
432
 
433
#ifdef USE_OPENGL
4989 terminx 434
        Bsprintf(cwd, "%s/%s", g_modDir, TEXCACHEFILE);
435
        Bstrcpy(TEXCACHEFILE, cwd);
4557 hendricks2 436
#endif
437
    }
438
 
439
    if (g_usingAddon)
440
        G_LoadAddon();
441
 
4989 terminx 442
    int32_t i;
443
    const char *grpfile = G_GrpFile();
444
 
445
    if (g_dependencyCRC)
4557 hendricks2 446
    {
4989 terminx 447
        struct grpfile *grp = FindGroup(g_dependencyCRC);
448
        if (grp)
4557 hendricks2 449
        {
4989 terminx 450
            if ((i = initgroupfile(grp->name)) == -1)
451
                initprintf("Warning: could not find main data file \"%s\"!\n", grp->name);
452
            else
453
                initprintf("Using \"%s\" as main game data file.\n", grp->name);
4557 hendricks2 454
        }
4989 terminx 455
    }
4557 hendricks2 456
 
4989 terminx 457
    if ((i = initgroupfile(grpfile)) == -1)
458
        initprintf("Warning: could not find main data file \"%s\"!\n", grpfile);
459
    else
460
        initprintf("Using \"%s\" as main game data file.\n", grpfile);
4557 hendricks2 461
 
5062 hendricks2 462
    if (g_postprocessing)
463
        g_postprocessing();
464
 
4989 terminx 465
    if (autoload)
466
    {
467
        G_LoadGroupsInDir("autoload");
4557 hendricks2 468
 
4989 terminx 469
        if (i != -1)
470
            G_DoAutoload(grpfile);
4557 hendricks2 471
    }
472
 
473
    if (g_modDir[0] != '/')
474
        G_LoadGroupsInDir(g_modDir);
475
 
476
    if (g_defNamePtr == NULL)
477
    {
478
        const char *tmpptr = getenv("DUKE3DDEF");
479
        if (tmpptr)
480
        {
481
            clearDefNamePtr();
482
            g_defNamePtr = dup_filename(tmpptr);
483
            initprintf("Using \"%s\" as definitions file\n", g_defNamePtr);
484
        }
485
    }
486
 
487
    loaddefinitions_game(G_DefFile(), TRUE);
488
 
4989 terminx 489
    struct strllist *s;
490
 
491
    pathsearchmode = 1;
492
    while (CommandGrps)
4557 hendricks2 493
    {
4989 terminx 494
        int32_t j;
4557 hendricks2 495
 
4989 terminx 496
        s = CommandGrps->next;
497
 
498
        if ((j = initgroupfile(CommandGrps->str)) == -1)
499
            initprintf("Could not find file \"%s\".\n", CommandGrps->str);
500
        else
4557 hendricks2 501
        {
4989 terminx 502
            g_groupFileHandle = j;
503
            initprintf("Using file \"%s\" as game data.\n", CommandGrps->str);
504
            if (autoload)
505
                G_DoAutoload(CommandGrps->str);
506
        }
4557 hendricks2 507
 
4989 terminx 508
        Bfree(CommandGrps->str);
509
        Bfree(CommandGrps);
510
        CommandGrps = s;
4557 hendricks2 511
    }
4989 terminx 512
    pathsearchmode = 0;
4557 hendricks2 513
}
514
 
3622 terminx 515
#ifdef _WIN32
3637 terminx 516
const char * G_GetInstallPath(int32_t insttype)
3622 terminx 517
{
3637 terminx 518
    static char spath[NUMINSTPATHS][BMAX_PATH];
4790 hendricks2 519
    static int32_t success[NUMINSTPATHS] = { -1, -1, -1, -1, -1 };
3622 terminx 520
    int32_t siz = BMAX_PATH;
521
 
3637 terminx 522
    if (success[insttype] == -1)
523
    {
3671 hendricks2 524
        HKEY HKLM32;
525
        LONG keygood = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NULL, 0, KEY_READ | KEY_WOW64_32KEY, &HKLM32);
526
        // KEY_WOW64_32KEY gets us around Wow6432Node on 64-bit builds
527
 
528
        if (keygood == ERROR_SUCCESS)
4127 hendricks2 529
        {
3671 hendricks2 530
            switch (insttype)
531
            {
4790 hendricks2 532
            case INSTPATH_STEAM_DUKE3D:
3671 hendricks2 533
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 225140", "InstallLocation", NULL, spath[insttype], (LPDWORD)&siz);
534
                break;
4790 hendricks2 535
            case INSTPATH_GOG_DUKE3D:
3671 hendricks2 536
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\GOG.com\\GOGDUKE3D", "PATH", NULL, spath[insttype], (LPDWORD)&siz);
537
                break;
4790 hendricks2 538
            case INSTPATH_3DR_DUKE3D:
539
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\3DRealms\\Duke Nukem 3D", NULL, NULL, spath[insttype], (LPDWORD)&siz);
540
                break;
541
            case INSTPATH_3DR_ANTH:
542
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\3DRealms\\Anthology", NULL, NULL, spath[insttype], (LPDWORD)&siz);
543
                break;
544
            case INSTPATH_STEAM_NAM:
545
                success[insttype] = SHGetValueA(HKLM32, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Steam App 329650", "InstallLocation", NULL, spath[insttype], (LPDWORD)&siz);
546
                break;
3671 hendricks2 547
            }
4127 hendricks2 548
 
549
            RegCloseKey(HKLM32);
550
        }
3637 terminx 551
    }
3622 terminx 552
 
3637 terminx 553
    if (success[insttype] == ERROR_SUCCESS)
554
        return spath[insttype];
3622 terminx 555
 
556
    return NULL;
557
}
558
#endif
559
 
4557 hendricks2 560
static void G_LoadAddon(void)
561
{
562
    struct grpfile * grp;
563
    int32_t crc = 0;  // compiler-happy
564
 
565
    switch (g_usingAddon)
566
    {
567
    case ADDON_DUKEDC:
568
        crc = DUKEDC_CRC;
569
        break;
570
    case ADDON_NWINTER:
571
        crc = DUKENW_CRC;
572
        break;
573
    case ADDON_CARIBBEAN:
574
        crc = DUKECB_CRC;
575
        break;
576
    }
577
 
578
    if (!crc) return;
579
 
580
    grp = FindGroup(crc);
581
 
582
    if (grp && FindGroup(DUKE15_CRC))
583
    {
584
        clearGrpNamePtr();
585
        g_grpNamePtr = dup_filename(FindGroup(DUKE15_CRC)->name);
586
 
587
        G_AddGroup(grp->name);
588
 
589
        for (grp = listgrps; grp; grp=grp->next)
590
            if (crc == grp->crcval) break;
591
 
592
        if (grp != NULL && grp->scriptname)
593
        {
594
            clearScriptNamePtr();
595
            g_scriptNamePtr = dup_filename(grp->scriptname);
596
        }
597
 
598
        if (grp != NULL && grp->defname)
599
        {
600
            clearDefNamePtr();
601
            g_defNamePtr = dup_filename(grp->defname);
602
        }
603
    }
604
}
605
 
5037 hendricks2 606
#if defined EDUKE32_OSX || defined __linux__ || defined EDUKE32_BSD
4804 hendricks2 607
static void G_AddSteamPaths(const char *basepath)
4801 hendricks2 608
{
609
    char buf[BMAX_PATH];
610
 
611
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot", basepath);
4886 hendricks2 612
    addsearchpath_user(buf, SEARCHPATH_REMOVE);
4801 hendricks2 613
 
614
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/dc", basepath);
4886 hendricks2 615
    addsearchpath_user(buf, SEARCHPATH_REMOVE);
4801 hendricks2 616
 
617
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/nw", basepath);
4886 hendricks2 618
    addsearchpath_user(buf, SEARCHPATH_REMOVE);
4801 hendricks2 619
 
620
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/vacation", basepath);
4886 hendricks2 621
    addsearchpath_user(buf, SEARCHPATH_REMOVE);
4801 hendricks2 622
 
4887 hendricks2 623
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music", basepath);
624
    addsearchpath(buf);
625
 
626
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music/nwinter", basepath);
627
    addsearchpath_user(buf, SEARCHPATH_NWINTER);
628
 
629
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/music/vacation", basepath);
630
    addsearchpath(buf);
631
 
5005 hendricks2 632
#if defined EDUKE32_OSX
4801 hendricks2 633
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Nam/Nam.app/Contents/Resources/Nam.boxer/C.harddisk/NAM", basepath);
4804 hendricks2 634
#else
635
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Nam/NAM", basepath);
636
#endif
4886 hendricks2 637
    addsearchpath_user(buf, SEARCHPATH_NAM);
4801 hendricks2 638
}
639
 
640
// A bare-bones "parser" for Valve's KeyValues VDF format.
641
// There is no guarantee this will function properly with ill-formed files.
642
static void KeyValues_SkipWhitespace(char **vdfbuf, char * const vdfbufend)
643
{
644
    while (((*vdfbuf)[0] == ' ' || (*vdfbuf)[0] == '\n' || (*vdfbuf)[0] == '\r' || (*vdfbuf)[0] == '\t' || (*vdfbuf)[0] == '\0') && *vdfbuf < vdfbufend)
645
        (*vdfbuf)++;
646
 
647
    // comments
648
    if ((*vdfbuf) + 2 < vdfbufend && (*vdfbuf)[0] == '/' && (*vdfbuf)[1] == '/')
649
    {
650
        while ((*vdfbuf)[0] != '\n' && (*vdfbuf)[0] != '\r' && *vdfbuf < vdfbufend)
651
            (*vdfbuf)++;
652
 
653
        KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
654
    }
655
}
656
static void KeyValues_SkipToEndOfQuotedToken(char **vdfbuf, char * const vdfbufend)
657
{
658
    (*vdfbuf)++;
659
    while ((*vdfbuf)[0] != '\"' && (*vdfbuf)[-1] != '\\' && *vdfbuf < vdfbufend)
660
        (*vdfbuf)++;
661
}
662
static void KeyValues_SkipToEndOfUnquotedToken(char **vdfbuf, char * const vdfbufend)
663
{
664
    while ((*vdfbuf)[0] != ' ' && (*vdfbuf)[0] != '\n' && (*vdfbuf)[0] != '\r' && (*vdfbuf)[0] != '\t' && (*vdfbuf)[0] != '\0' && *vdfbuf < vdfbufend)
665
        (*vdfbuf)++;
666
}
667
static void KeyValues_SkipNextWhatever(char **vdfbuf, char * const vdfbufend)
668
{
669
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
670
 
671
    if (*vdfbuf == vdfbufend)
672
        return;
673
 
674
    if ((*vdfbuf)[0] == '{')
675
    {
676
        (*vdfbuf)++;
677
        do
678
        {
679
            KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
680
        }
681
        while ((*vdfbuf)[0] != '}');
682
        (*vdfbuf)++;
683
    }
684
    else if ((*vdfbuf)[0] == '\"')
685
        KeyValues_SkipToEndOfQuotedToken(vdfbuf, vdfbufend);
686
    else if ((*vdfbuf)[0] != '}')
687
        KeyValues_SkipToEndOfUnquotedToken(vdfbuf, vdfbufend);
688
 
689
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
690
}
691
static char* KeyValues_NormalizeToken(char **vdfbuf, char * const vdfbufend)
692
{
693
    char *token = *vdfbuf;
694
 
695
    if ((*vdfbuf)[0] == '\"' && *vdfbuf < vdfbufend)
696
    {
697
        token++;
698
 
699
        KeyValues_SkipToEndOfQuotedToken(vdfbuf, vdfbufend);
700
        (*vdfbuf)[0] = '\0';
701
 
702
        // account for escape sequences
703
        char *writeseeker = token, *readseeker = token;
704
        while (readseeker <= *vdfbuf)
705
        {
706
            if (readseeker[0] == '\\')
707
                readseeker++;
708
 
709
            writeseeker[0] = readseeker[0];
710
 
711
            writeseeker++;
712
            readseeker++;
713
        }
714
 
715
        return token;
716
    }
717
 
718
    KeyValues_SkipToEndOfUnquotedToken(vdfbuf, vdfbufend);
719
    (*vdfbuf)[0] = '\0';
720
 
721
    return token;
722
}
723
static void KeyValues_FindKey(char **vdfbuf, char * const vdfbufend, const char *token)
724
{
725
    char *ParentKey = KeyValues_NormalizeToken(vdfbuf, vdfbufend);
726
    if (token != NULL) // pass in NULL to find the next key instead of a specific one
727
        while (Bstrcmp(ParentKey, token) != 0 && *vdfbuf < vdfbufend)
728
        {
729
            KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
730
            ParentKey = KeyValues_NormalizeToken(vdfbuf, vdfbufend);
731
        }
732
 
733
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
734
}
735
static int32_t KeyValues_FindParentKey(char **vdfbuf, char * const vdfbufend, const char *token)
736
{
737
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
738
 
739
    // end of scope
740
    if ((*vdfbuf)[0] == '}')
741
        return 0;
742
 
743
    KeyValues_FindKey(vdfbuf, vdfbufend, token);
744
 
745
    // ignore the wrong type
746
    while ((*vdfbuf)[0] != '{' && *vdfbuf < vdfbufend)
747
    {
748
        KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
749
        KeyValues_FindKey(vdfbuf, vdfbufend, token);
750
    }
751
 
752
    if (*vdfbuf == vdfbufend)
753
        return 0;
754
 
755
    return 1;
756
}
757
static char* KeyValues_FindKeyValue(char **vdfbuf, char * const vdfbufend, const char *token)
758
{
759
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
760
 
761
    // end of scope
762
    if ((*vdfbuf)[0] == '}')
763
        return NULL;
764
 
765
    KeyValues_FindKey(vdfbuf, vdfbufend, token);
766
 
767
    // ignore the wrong type
768
    while ((*vdfbuf)[0] == '{' && *vdfbuf < vdfbufend)
769
    {
770
        KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
771
        KeyValues_FindKey(vdfbuf, vdfbufend, token);
772
    }
773
 
774
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
775
 
776
    if (*vdfbuf == vdfbufend)
777
        return NULL;
778
 
779
    return KeyValues_NormalizeToken(vdfbuf, vdfbufend);
780
}
781
 
782
static void G_ParseSteamKeyValuesForPaths(const char *vdf)
783
{
784
    int32_t fd = Bopen(vdf, BO_RDONLY);
785
    int32_t size = Bfilelength(fd);
786
    char *vdfbufstart, *vdfbuf, *vdfbufend;
787
 
788
    if (size <= 0)
789
        return;
790
 
791
    vdfbufstart = vdfbuf = (char*)Bmalloc(size);
792
    size = (int32_t)Bread(fd, vdfbuf, size);
793
    Bclose(fd);
794
    vdfbufend = vdfbuf + size;
795
 
796
    if (KeyValues_FindParentKey(&vdfbuf, vdfbufend, "LibraryFolders"))
797
    {
798
        char *result;
799
        vdfbuf++;
800
        while ((result = KeyValues_FindKeyValue(&vdfbuf, vdfbufend, NULL)) != NULL)
4804 hendricks2 801
            G_AddSteamPaths(result);
4801 hendricks2 802
    }
803
 
804
    Bfree(vdfbufstart);
805
}
806
#endif
807
 
3581 hendricks2 808
void G_AddSearchPaths(void)
809
{
5037 hendricks2 810
#if defined __linux__ || defined EDUKE32_BSD
4804 hendricks2 811
    char buf[BMAX_PATH];
812
    char *homepath = Bgethomedir();
813
 
814
    Bsnprintf(buf, sizeof(buf), "%s/.steam/steam", homepath);
815
    G_AddSteamPaths(buf);
816
 
817
    Bsnprintf(buf, sizeof(buf), "%s/.steam/steam/steamapps/libraryfolders.vdf", homepath);
818
    G_ParseSteamKeyValuesForPaths(buf);
819
 
820
    Bfree(homepath);
821
 
3581 hendricks2 822
    addsearchpath("/usr/share/games/jfduke3d");
823
    addsearchpath("/usr/local/share/games/jfduke3d");
824
    addsearchpath("/usr/share/games/eduke32");
825
    addsearchpath("/usr/local/share/games/eduke32");
5005 hendricks2 826
#elif defined EDUKE32_OSX
4801 hendricks2 827
    char buf[BMAX_PATH];
4802 hendricks2 828
    int32_t i;
829
    char *applications[] = { osx_getapplicationsdir(0), osx_getapplicationsdir(1) };
830
    char *support[] = { osx_getsupportdir(0), osx_getsupportdir(1) };
4801 hendricks2 831
 
4802 hendricks2 832
    for (i = 0; i < 2; i++)
833
    {
834
        Bsnprintf(buf, sizeof(buf), "%s/Steam", support[i]);
4804 hendricks2 835
        G_AddSteamPaths(buf);
4801 hendricks2 836
 
4802 hendricks2 837
        Bsnprintf(buf, sizeof(buf), "%s/Steam/steamapps/libraryfolders.vdf", support[i]);
838
        G_ParseSteamKeyValuesForPaths(buf);
4801 hendricks2 839
 
4802 hendricks2 840
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D.app/Contents/Resources/Duke Nukem 3D.boxer/C.harddisk", applications[i]);
4886 hendricks2 841
        addsearchpath_user(buf, SEARCHPATH_REMOVE);
4802 hendricks2 842
    }
4801 hendricks2 843
 
4802 hendricks2 844
    for (i = 0; i < 2; i++)
845
    {
846
        Bsnprintf(buf, sizeof(buf), "%s/JFDuke3D", support[i]);
847
        addsearchpath(buf);
848
        Bsnprintf(buf, sizeof(buf), "%s/EDuke32", support[i]);
849
        addsearchpath(buf);
850
    }
4801 hendricks2 851
 
4802 hendricks2 852
    for (i = 0; i < 2; i++)
853
    {
854
        Bfree(applications[i]);
855
        Bfree(support[i]);
856
    }
3581 hendricks2 857
#elif defined (_WIN32)
3615 terminx 858
    char buf[BMAX_PATH];
3671 hendricks2 859
    const char* instpath;
3615 terminx 860
 
4790 hendricks2 861
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM_DUKE3D)))
3622 terminx 862
    {
4557 hendricks2 863
        Bsnprintf(buf, sizeof(buf), "%s/gameroot", instpath);
4886 hendricks2 864
        addsearchpath_user(buf, SEARCHPATH_REMOVE);
3615 terminx 865
 
4594 hendricks2 866
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/dc", instpath);
4886 hendricks2 867
        addsearchpath_user(buf, SEARCHPATH_REMOVE);
4594 hendricks2 868
 
869
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/nw", instpath);
4886 hendricks2 870
        addsearchpath_user(buf, SEARCHPATH_REMOVE);
4594 hendricks2 871
 
872
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/vacation", instpath);
4886 hendricks2 873
        addsearchpath_user(buf, SEARCHPATH_REMOVE);
4887 hendricks2 874
 
875
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/music", instpath);
876
        addsearchpath(buf);
877
 
878
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/music/nwinter", instpath);
879
        addsearchpath_user(buf, SEARCHPATH_NWINTER);
880
 
881
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/music/vacation", instpath);
882
        addsearchpath(buf);
3615 terminx 883
    }
884
 
4790 hendricks2 885
    if ((instpath = G_GetInstallPath(INSTPATH_GOG_DUKE3D)))
4886 hendricks2 886
        addsearchpath_user(instpath, SEARCHPATH_REMOVE);
4790 hendricks2 887
 
888
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_DUKE3D)))
889
    {
890
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
4886 hendricks2 891
        addsearchpath_user(buf, SEARCHPATH_REMOVE);
4790 hendricks2 892
    }
893
 
894
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_ANTH)))
895
    {
896
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
4886 hendricks2 897
        addsearchpath_user(buf, SEARCHPATH_REMOVE);
4790 hendricks2 898
    }
899
 
900
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM_NAM)))
901
    {
902
        Bsnprintf(buf, sizeof(buf), "%s/NAM", instpath);
4886 hendricks2 903
        addsearchpath_user(buf, SEARCHPATH_NAM);
4790 hendricks2 904
    }
3581 hendricks2 905
#endif
906
}
907
 
3654 terminx 908
void G_CleanupSearchPaths(void)
909
{
4886 hendricks2 910
    removesearchpaths_withuser(SEARCHPATH_REMOVE);
3654 terminx 911
 
4886 hendricks2 912
    if (!(NAM || NAPALM))
913
        removesearchpaths_withuser(SEARCHPATH_NAM);
4887 hendricks2 914
 
915
    if (!(NWINTER))
916
        removesearchpaths_withuser(SEARCHPATH_NWINTER);
3654 terminx 917
}
918
 
3581 hendricks2 919
//////////
920
 
2542 helixhorne 921
struct strllist *CommandPaths, *CommandGrps;
922
 
4128 hendricks2 923
char **g_scriptModules = NULL;
924
int32_t g_scriptModulesNum = 0;
925
 
2542 helixhorne 926
void G_AddGroup(const char *buffer)
927
{
928
    char buf[BMAX_PATH];
929
 
4491 helixhorne 930
    struct strllist *s = (struct strllist *)Xcalloc(1,sizeof(struct strllist));
2542 helixhorne 931
 
932
    Bstrcpy(buf, buffer);
933
 
934
    if (Bstrchr(buf,'.') == 0)
935
        Bstrcat(buf,".grp");
936
 
4491 helixhorne 937
    s->str = Xstrdup(buf);
2542 helixhorne 938
 
939
    if (CommandGrps)
940
    {
941
        struct strllist *t;
942
        for (t = CommandGrps; t->next; t=t->next) ;
943
        t->next = s;
944
        return;
945
    }
946
    CommandGrps = s;
947
}
948
 
949
void G_AddPath(const char *buffer)
950
{
4491 helixhorne 951
    struct strllist *s = (struct strllist *)Xcalloc(1,sizeof(struct strllist));
952
    s->str = Xstrdup(buffer);
2542 helixhorne 953
 
954
    if (CommandPaths)
955
    {
956
        struct strllist *t;
957
        for (t = CommandPaths; t->next; t=t->next) ;
958
        t->next = s;
959
        return;
960
    }
961
    CommandPaths = s;
962
}
2549 helixhorne 963
 
4128 hendricks2 964
void G_AddCon(const char *buffer)
965
{
966
    clearScriptNamePtr();
967
    g_scriptNamePtr = dup_filename(buffer);
968
    initprintf("Using CON file \"%s\".\n",g_scriptNamePtr);
969
}
970
 
971
void G_AddConModule(const char *buffer)
972
{
4491 helixhorne 973
    g_scriptModules = (char **) Xrealloc (g_scriptModules, (g_scriptModulesNum+1) * sizeof(char *));
974
    g_scriptModules[g_scriptModulesNum] = Xstrdup(buffer);
4128 hendricks2 975
    ++g_scriptModulesNum;
976
}
977
 
2549 helixhorne 978
//////////
979
 
3269 terminx 980
// loads all group (grp, zip, pk3/4) files in the given directory
2555 helixhorne 981
void G_LoadGroupsInDir(const char *dirname)
982
{
3269 terminx 983
    static const char *extensions[4] = { "*.grp", "*.zip", "*.pk3", "*.pk4" };
2555 helixhorne 984
 
985
    char buf[BMAX_PATH];
986
    int32_t i;
987
 
988
    fnlist_t fnlist = FNLIST_INITIALIZER;
989
 
3269 terminx 990
    for (i=0; i<4; i++)
2555 helixhorne 991
    {
992
        CACHE1D_FIND_REC *rec;
993
 
994
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
995
 
996
        for (rec=fnlist.findfiles; rec; rec=rec->next)
997
        {
998
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
999
            initprintf("Using group file \"%s\".\n", buf);
1000
            initgroupfile(buf);
1001
        }
1002
 
1003
        fnlist_clearnames(&fnlist);
1004
    }
1005
}
1006
 
1007
void G_DoAutoload(const char *dirname)
1008
{
1009
    char buf[BMAX_PATH];
1010
 
1011
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
1012
    G_LoadGroupsInDir(buf);
1013
}
4564 hendricks2 1014
 
1015
//////////
1016
 
1017
static uint8_t water_pal[768], slime_pal[768], title_pal[768], dre_alms[768], ending_pal[768];
1018
 
1019
uint8_t *basepaltable[BASEPALCOUNT] = {
1020
    palette, water_pal, slime_pal,
1021
    dre_alms, title_pal, ending_pal,
1022
    NULL /*anim_pal*/
1023
};
1024
 
1025
int32_t G_LoadLookups(void)
1026
{
1027
    int32_t fp, j;
1028
 
1029
    if ((fp=kopen4loadfrommod("lookup.dat",0)) == -1)
1030
    {
1031
        if ((fp=kopen4loadfrommod("lookup.dat",1)) == -1)
1032
        {
1033
            initprintf("ERROR: File \"lookup.dat\" not found.\n");
1034
            return 1;
1035
        }
1036
    }
1037
 
1038
    j = loadlookups(fp);
1039
 
1040
    if (j < 0)
1041
    {
1042
        if (j == -1)
1043
            initprintf("ERROR loading \"lookup.dat\": failed reading enough data.\n");
1044
        return 1;
1045
    }
1046
 
1047
    for (j=1; j<=5; j++)
1048
    {
1049
        // Account for TITLE and REALMS swap between basepal number and on-disk order.
1050
        // XXX: this reordering is better off as an argument to us.
1051
        int32_t basepalnum = (j == 3 || j == 4) ? 4+3-j : j;
1052
 
1053
        if (kread(fp, basepaltable[basepalnum], 768) != 768)
1054
            return -1;
1055
    }
1056
 
1057
    kclose(fp);
1058
 
4811 helixhorne 1059
    generatefogpals();
4564 hendricks2 1060
 
1061
    fillemptylookups();
1062
 
1063
    return 0;
1064
}
4948 hendricks2 1065
 
1066
#if defined HAVE_FLAC || defined HAVE_VORBIS
1067
int32_t S_UpgradeFormat(const char *fn, char searchfirst)
1068
{
1069
    char *testfn, *extension;
1070
    int32_t fp = -1;
1071
 
1072
    testfn = (char *)Xmalloc(Bstrlen(fn) + 6);
1073
    Bstrcpy(testfn, fn);
1074
    extension = Bstrrchr(testfn, '.');
1075
 
1076
    if (extension)
1077
    {
1078
#ifdef HAVE_FLAC
1079
        Bstrcpy(extension, ".flac");
1080
        fp = kopen4loadfrommod(testfn, searchfirst);
1081
        if (fp >= 0)
1082
        {
1083
            Bfree(testfn);
1084
            return fp;
1085
        }
1086
#endif
1087
 
1088
#ifdef HAVE_VORBIS
1089
        Bstrcpy(extension, ".ogg");
1090
        fp = kopen4loadfrommod(testfn, searchfirst);
1091
        if (fp >= 0)
1092
        {
1093
            Bfree(testfn);
1094
            return fp;
1095
        }
1096
#endif
1097
    }
1098
 
1099
    Bfree(testfn);
1100
    return -1;
1101
}
1102
#endif