Subversion Repositories eduke32

Rev

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