Subversion Repositories eduke32

Rev

Rev 4805 | Rev 4886 | 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
 
4557 hendricks2 371
void G_ExtPreStartupWindow(void)
372
{
373
    ScanGroups();
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)
381
        {
382
            struct grpfile *grp;
383
            for (grp = listgrps; grp; grp=grp->next)
384
                if (fg->crcval == grp->crcval) break;
385
 
386
            if (grp == NULL)
387
                continue;
388
 
389
            fg->game = grp->game;
390
            if (!first) first = fg;
391
            if (!Bstrcasecmp(fg->name, G_DefaultGrpFile()))
392
            {
393
                g_gameType = grp->game;
394
                g_gameNamePtr = grp->name;
395
                break;
396
            }
397
        }
398
        if (!fg && first)
399
        {
400
            if (g_grpNamePtr == NULL)
401
            {
402
                clearGrpNamePtr();
403
                g_grpNamePtr = dup_filename(first->name);
404
            }
405
            g_gameType = first->game;
406
            g_gameNamePtr = listgrps->name;
407
        }
408
        else if (!fg) g_gameNamePtr = NULL;
409
    }
410
}
411
 
412
void G_ExtPostStartupWindow(int32_t autoload)
413
{
414
    if (g_modDir[0] != '/')
415
    {
416
        char cwd[BMAX_PATH];
417
 
418
        Bstrcat(g_rootDir,g_modDir);
419
        addsearchpath(g_rootDir);
420
//        addsearchpath(mod_dir);
421
 
422
        if (getcwd(cwd,BMAX_PATH))
423
        {
424
            Bsprintf(cwd,"%s/%s",cwd,g_modDir);
425
            if (!Bstrcmp(g_rootDir, cwd))
426
            {
427
                if (addsearchpath(cwd) == -2)
428
                    if (Bmkdir(cwd,S_IRWXU) == 0) addsearchpath(cwd);
429
            }
430
        }
431
 
432
#ifdef USE_OPENGL
433
        Bsprintf(cwd,"%s/%s",g_modDir,TEXCACHEFILE);
434
        Bstrcpy(TEXCACHEFILE,cwd);
435
#endif
436
    }
437
 
438
    if (g_usingAddon)
439
        G_LoadAddon();
440
 
441
    {
442
        int32_t i;
443
        const char *grpfile = G_GrpFile();
444
 
445
        if (g_dependencyCRC)
446
        {
447
            struct grpfile * grp = FindGroup(g_dependencyCRC);
448
            if (grp)
449
            {
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);
454
            }
455
        }
456
 
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);
461
 
462
        if (autoload)
463
        {
464
            G_LoadGroupsInDir("autoload");
465
 
466
            if (i != -1)
467
                G_DoAutoload(grpfile);
468
        }
469
    }
470
 
471
    if (g_modDir[0] != '/')
472
        G_LoadGroupsInDir(g_modDir);
473
 
474
    if (g_defNamePtr == NULL)
475
    {
476
        const char *tmpptr = getenv("DUKE3DDEF");
477
        if (tmpptr)
478
        {
479
            clearDefNamePtr();
480
            g_defNamePtr = dup_filename(tmpptr);
481
            initprintf("Using \"%s\" as definitions file\n", g_defNamePtr);
482
        }
483
    }
484
 
485
    loaddefinitions_game(G_DefFile(), TRUE);
486
 
487
    {
488
        struct strllist *s;
489
 
490
        pathsearchmode = 1;
491
        while (CommandGrps)
492
        {
493
            int32_t j;
494
 
495
            s = CommandGrps->next;
496
 
497
            if ((j = initgroupfile(CommandGrps->str)) == -1)
498
                initprintf("Could not find file \"%s\".\n",CommandGrps->str);
499
            else
500
            {
501
                g_groupFileHandle = j;
502
                initprintf("Using file \"%s\" as game data.\n",CommandGrps->str);
503
                if (autoload)
504
                    G_DoAutoload(CommandGrps->str);
505
            }
506
 
507
            Bfree(CommandGrps->str);
508
            Bfree(CommandGrps);
509
            CommandGrps = s;
510
        }
511
        pathsearchmode = 0;
512
    }
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
 
4805 hendricks2 606
#if defined __APPLE__ || defined __linux__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
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);
612
    addsearchpath(buf);
613
 
614
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/dc", basepath);
615
    addsearchpath(buf);
616
 
617
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/nw", basepath);
618
    addsearchpath(buf);
619
 
620
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Duke Nukem 3D/gameroot/addons/vacation", basepath);
621
    addsearchpath(buf);
622
 
4804 hendricks2 623
#if defined __APPLE__
4801 hendricks2 624
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Nam/Nam.app/Contents/Resources/Nam.boxer/C.harddisk/NAM", basepath);
4804 hendricks2 625
#else
626
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Nam/NAM", basepath);
627
#endif
4801 hendricks2 628
    addsearchpath(buf);
629
}
630
 
631
// A bare-bones "parser" for Valve's KeyValues VDF format.
632
// There is no guarantee this will function properly with ill-formed files.
633
static void KeyValues_SkipWhitespace(char **vdfbuf, char * const vdfbufend)
634
{
635
    while (((*vdfbuf)[0] == ' ' || (*vdfbuf)[0] == '\n' || (*vdfbuf)[0] == '\r' || (*vdfbuf)[0] == '\t' || (*vdfbuf)[0] == '\0') && *vdfbuf < vdfbufend)
636
        (*vdfbuf)++;
637
 
638
    // comments
639
    if ((*vdfbuf) + 2 < vdfbufend && (*vdfbuf)[0] == '/' && (*vdfbuf)[1] == '/')
640
    {
641
        while ((*vdfbuf)[0] != '\n' && (*vdfbuf)[0] != '\r' && *vdfbuf < vdfbufend)
642
            (*vdfbuf)++;
643
 
644
        KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
645
    }
646
}
647
static void KeyValues_SkipToEndOfQuotedToken(char **vdfbuf, char * const vdfbufend)
648
{
649
    (*vdfbuf)++;
650
    while ((*vdfbuf)[0] != '\"' && (*vdfbuf)[-1] != '\\' && *vdfbuf < vdfbufend)
651
        (*vdfbuf)++;
652
}
653
static void KeyValues_SkipToEndOfUnquotedToken(char **vdfbuf, char * const vdfbufend)
654
{
655
    while ((*vdfbuf)[0] != ' ' && (*vdfbuf)[0] != '\n' && (*vdfbuf)[0] != '\r' && (*vdfbuf)[0] != '\t' && (*vdfbuf)[0] != '\0' && *vdfbuf < vdfbufend)
656
        (*vdfbuf)++;
657
}
658
static void KeyValues_SkipNextWhatever(char **vdfbuf, char * const vdfbufend)
659
{
660
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
661
 
662
    if (*vdfbuf == vdfbufend)
663
        return;
664
 
665
    if ((*vdfbuf)[0] == '{')
666
    {
667
        (*vdfbuf)++;
668
        do
669
        {
670
            KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
671
        }
672
        while ((*vdfbuf)[0] != '}');
673
        (*vdfbuf)++;
674
    }
675
    else if ((*vdfbuf)[0] == '\"')
676
        KeyValues_SkipToEndOfQuotedToken(vdfbuf, vdfbufend);
677
    else if ((*vdfbuf)[0] != '}')
678
        KeyValues_SkipToEndOfUnquotedToken(vdfbuf, vdfbufend);
679
 
680
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
681
}
682
static char* KeyValues_NormalizeToken(char **vdfbuf, char * const vdfbufend)
683
{
684
    char *token = *vdfbuf;
685
 
686
    if ((*vdfbuf)[0] == '\"' && *vdfbuf < vdfbufend)
687
    {
688
        token++;
689
 
690
        KeyValues_SkipToEndOfQuotedToken(vdfbuf, vdfbufend);
691
        (*vdfbuf)[0] = '\0';
692
 
693
        // account for escape sequences
694
        char *writeseeker = token, *readseeker = token;
695
        while (readseeker <= *vdfbuf)
696
        {
697
            if (readseeker[0] == '\\')
698
                readseeker++;
699
 
700
            writeseeker[0] = readseeker[0];
701
 
702
            writeseeker++;
703
            readseeker++;
704
        }
705
 
706
        return token;
707
    }
708
 
709
    KeyValues_SkipToEndOfUnquotedToken(vdfbuf, vdfbufend);
710
    (*vdfbuf)[0] = '\0';
711
 
712
    return token;
713
}
714
static void KeyValues_FindKey(char **vdfbuf, char * const vdfbufend, const char *token)
715
{
716
    char *ParentKey = KeyValues_NormalizeToken(vdfbuf, vdfbufend);
717
    if (token != NULL) // pass in NULL to find the next key instead of a specific one
718
        while (Bstrcmp(ParentKey, token) != 0 && *vdfbuf < vdfbufend)
719
        {
720
            KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
721
            ParentKey = KeyValues_NormalizeToken(vdfbuf, vdfbufend);
722
        }
723
 
724
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
725
}
726
static int32_t KeyValues_FindParentKey(char **vdfbuf, char * const vdfbufend, const char *token)
727
{
728
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
729
 
730
    // end of scope
731
    if ((*vdfbuf)[0] == '}')
732
        return 0;
733
 
734
    KeyValues_FindKey(vdfbuf, vdfbufend, token);
735
 
736
    // ignore the wrong type
737
    while ((*vdfbuf)[0] != '{' && *vdfbuf < vdfbufend)
738
    {
739
        KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
740
        KeyValues_FindKey(vdfbuf, vdfbufend, token);
741
    }
742
 
743
    if (*vdfbuf == vdfbufend)
744
        return 0;
745
 
746
    return 1;
747
}
748
static char* KeyValues_FindKeyValue(char **vdfbuf, char * const vdfbufend, const char *token)
749
{
750
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
751
 
752
    // end of scope
753
    if ((*vdfbuf)[0] == '}')
754
        return NULL;
755
 
756
    KeyValues_FindKey(vdfbuf, vdfbufend, token);
757
 
758
    // ignore the wrong type
759
    while ((*vdfbuf)[0] == '{' && *vdfbuf < vdfbufend)
760
    {
761
        KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
762
        KeyValues_FindKey(vdfbuf, vdfbufend, token);
763
    }
764
 
765
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
766
 
767
    if (*vdfbuf == vdfbufend)
768
        return NULL;
769
 
770
    return KeyValues_NormalizeToken(vdfbuf, vdfbufend);
771
}
772
 
773
static void G_ParseSteamKeyValuesForPaths(const char *vdf)
774
{
775
    int32_t fd = Bopen(vdf, BO_RDONLY);
776
    int32_t size = Bfilelength(fd);
777
    char *vdfbufstart, *vdfbuf, *vdfbufend;
778
 
779
    if (size <= 0)
780
        return;
781
 
782
    vdfbufstart = vdfbuf = (char*)Bmalloc(size);
783
    size = (int32_t)Bread(fd, vdfbuf, size);
784
    Bclose(fd);
785
    vdfbufend = vdfbuf + size;
786
 
787
    if (KeyValues_FindParentKey(&vdfbuf, vdfbufend, "LibraryFolders"))
788
    {
789
        char *result;
790
        vdfbuf++;
791
        while ((result = KeyValues_FindKeyValue(&vdfbuf, vdfbufend, NULL)) != NULL)
4804 hendricks2 792
            G_AddSteamPaths(result);
4801 hendricks2 793
    }
794
 
795
    Bfree(vdfbufstart);
796
}
797
#endif
798
 
3581 hendricks2 799
void G_AddSearchPaths(void)
800
{
801
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
4804 hendricks2 802
    char buf[BMAX_PATH];
803
    char *homepath = Bgethomedir();
804
 
805
    Bsnprintf(buf, sizeof(buf), "%s/.steam/steam", homepath);
806
    G_AddSteamPaths(buf);
807
 
808
    Bsnprintf(buf, sizeof(buf), "%s/.steam/steam/steamapps/libraryfolders.vdf", homepath);
809
    G_ParseSteamKeyValuesForPaths(buf);
810
 
811
    Bfree(homepath);
812
 
3581 hendricks2 813
    addsearchpath("/usr/share/games/jfduke3d");
814
    addsearchpath("/usr/local/share/games/jfduke3d");
815
    addsearchpath("/usr/share/games/eduke32");
816
    addsearchpath("/usr/local/share/games/eduke32");
817
#elif defined(__APPLE__)
4801 hendricks2 818
    char buf[BMAX_PATH];
4802 hendricks2 819
    int32_t i;
820
    char *applications[] = { osx_getapplicationsdir(0), osx_getapplicationsdir(1) };
821
    char *support[] = { osx_getsupportdir(0), osx_getsupportdir(1) };
4801 hendricks2 822
 
4802 hendricks2 823
    for (i = 0; i < 2; i++)
824
    {
825
        Bsnprintf(buf, sizeof(buf), "%s/Steam", support[i]);
4804 hendricks2 826
        G_AddSteamPaths(buf);
4801 hendricks2 827
 
4802 hendricks2 828
        Bsnprintf(buf, sizeof(buf), "%s/Steam/steamapps/libraryfolders.vdf", support[i]);
829
        G_ParseSteamKeyValuesForPaths(buf);
4801 hendricks2 830
 
4802 hendricks2 831
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D.app/Contents/Resources/Duke Nukem 3D.boxer/C.harddisk", applications[i]);
832
        addsearchpath(buf);
833
    }
4801 hendricks2 834
 
4802 hendricks2 835
    for (i = 0; i < 2; i++)
836
    {
837
        Bsnprintf(buf, sizeof(buf), "%s/JFDuke3D", support[i]);
838
        addsearchpath(buf);
839
        Bsnprintf(buf, sizeof(buf), "%s/EDuke32", support[i]);
840
        addsearchpath(buf);
841
    }
4801 hendricks2 842
 
4802 hendricks2 843
    for (i = 0; i < 2; i++)
844
    {
845
        Bfree(applications[i]);
846
        Bfree(support[i]);
847
    }
3581 hendricks2 848
#elif defined (_WIN32)
3615 terminx 849
    char buf[BMAX_PATH];
3671 hendricks2 850
    const char* instpath;
3615 terminx 851
 
4790 hendricks2 852
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM_DUKE3D)))
3622 terminx 853
    {
4557 hendricks2 854
        Bsnprintf(buf, sizeof(buf), "%s/gameroot", instpath);
3622 terminx 855
        addsearchpath(buf);
3615 terminx 856
 
4594 hendricks2 857
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/dc", instpath);
3622 terminx 858
        addsearchpath(buf);
4594 hendricks2 859
 
860
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/nw", instpath);
861
        addsearchpath(buf);
862
 
863
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/vacation", instpath);
864
        addsearchpath(buf);
3615 terminx 865
    }
866
 
4790 hendricks2 867
    if ((instpath = G_GetInstallPath(INSTPATH_GOG_DUKE3D)))
3671 hendricks2 868
        addsearchpath(instpath);
4790 hendricks2 869
 
870
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_DUKE3D)))
871
    {
872
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
873
        addsearchpath(buf);
874
    }
875
 
876
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_ANTH)))
877
    {
878
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
879
        addsearchpath(buf);
880
    }
881
 
882
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM_NAM)))
883
    {
884
        Bsnprintf(buf, sizeof(buf), "%s/NAM", instpath);
885
        addsearchpath(buf);
886
    }
3581 hendricks2 887
#endif
888
}
889
 
3654 terminx 890
void G_CleanupSearchPaths(void)
891
{
892
#ifdef _WIN32
893
    char buf[BMAX_PATH];
3671 hendricks2 894
    const char* instpath;
3654 terminx 895
 
4790 hendricks2 896
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM_DUKE3D)))
3654 terminx 897
    {
4557 hendricks2 898
        Bsnprintf(buf, sizeof(buf), "%s/gameroot", instpath);
3654 terminx 899
        removesearchpath(buf);
900
 
4594 hendricks2 901
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/dc", instpath);
3654 terminx 902
        removesearchpath(buf);
4594 hendricks2 903
 
904
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/nw", instpath);
905
        removesearchpath(buf);
906
 
907
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/vacation", instpath);
908
        removesearchpath(buf);
3654 terminx 909
    }
910
 
4790 hendricks2 911
    if ((instpath = G_GetInstallPath(INSTPATH_GOG_DUKE3D)))
3671 hendricks2 912
        removesearchpath(instpath);
4790 hendricks2 913
 
914
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_DUKE3D)))
915
    {
916
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
917
        removesearchpath(buf);
918
    }
919
 
920
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_ANTH)))
921
    {
922
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
923
        removesearchpath(buf);
924
    }
925
 
926
    if (g_gameType != GAMEFLAG_NAM && (instpath = G_GetInstallPath(INSTPATH_STEAM_NAM)))
927
    {
928
        Bsnprintf(buf, sizeof(buf), "%s/NAM", instpath);
929
        removesearchpath(buf);
930
    }
3654 terminx 931
#endif
932
}
933
 
3581 hendricks2 934
//////////
935
 
2542 helixhorne 936
struct strllist *CommandPaths, *CommandGrps;
937
 
4128 hendricks2 938
char **g_scriptModules = NULL;
939
int32_t g_scriptModulesNum = 0;
940
 
2542 helixhorne 941
void G_AddGroup(const char *buffer)
942
{
943
    char buf[BMAX_PATH];
944
 
4491 helixhorne 945
    struct strllist *s = (struct strllist *)Xcalloc(1,sizeof(struct strllist));
2542 helixhorne 946
 
947
    Bstrcpy(buf, buffer);
948
 
949
    if (Bstrchr(buf,'.') == 0)
950
        Bstrcat(buf,".grp");
951
 
4491 helixhorne 952
    s->str = Xstrdup(buf);
2542 helixhorne 953
 
954
    if (CommandGrps)
955
    {
956
        struct strllist *t;
957
        for (t = CommandGrps; t->next; t=t->next) ;
958
        t->next = s;
959
        return;
960
    }
961
    CommandGrps = s;
962
}
963
 
964
void G_AddPath(const char *buffer)
965
{
4491 helixhorne 966
    struct strllist *s = (struct strllist *)Xcalloc(1,sizeof(struct strllist));
967
    s->str = Xstrdup(buffer);
2542 helixhorne 968
 
969
    if (CommandPaths)
970
    {
971
        struct strllist *t;
972
        for (t = CommandPaths; t->next; t=t->next) ;
973
        t->next = s;
974
        return;
975
    }
976
    CommandPaths = s;
977
}
2549 helixhorne 978
 
4128 hendricks2 979
void G_AddCon(const char *buffer)
980
{
981
    clearScriptNamePtr();
982
    g_scriptNamePtr = dup_filename(buffer);
983
    initprintf("Using CON file \"%s\".\n",g_scriptNamePtr);
984
}
985
 
986
void G_AddConModule(const char *buffer)
987
{
4491 helixhorne 988
    g_scriptModules = (char **) Xrealloc (g_scriptModules, (g_scriptModulesNum+1) * sizeof(char *));
989
    g_scriptModules[g_scriptModulesNum] = Xstrdup(buffer);
4128 hendricks2 990
    ++g_scriptModulesNum;
991
}
992
 
2549 helixhorne 993
//////////
994
 
3269 terminx 995
// loads all group (grp, zip, pk3/4) files in the given directory
2555 helixhorne 996
void G_LoadGroupsInDir(const char *dirname)
997
{
3269 terminx 998
    static const char *extensions[4] = { "*.grp", "*.zip", "*.pk3", "*.pk4" };
2555 helixhorne 999
 
1000
    char buf[BMAX_PATH];
1001
    int32_t i;
1002
 
1003
    fnlist_t fnlist = FNLIST_INITIALIZER;
1004
 
3269 terminx 1005
    for (i=0; i<4; i++)
2555 helixhorne 1006
    {
1007
        CACHE1D_FIND_REC *rec;
1008
 
1009
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
1010
 
1011
        for (rec=fnlist.findfiles; rec; rec=rec->next)
1012
        {
1013
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
1014
            initprintf("Using group file \"%s\".\n", buf);
1015
            initgroupfile(buf);
1016
        }
1017
 
1018
        fnlist_clearnames(&fnlist);
1019
    }
1020
}
1021
 
1022
void G_DoAutoload(const char *dirname)
1023
{
1024
    char buf[BMAX_PATH];
1025
 
1026
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
1027
    G_LoadGroupsInDir(buf);
1028
}
4564 hendricks2 1029
 
1030
//////////
1031
 
1032
static uint8_t water_pal[768], slime_pal[768], title_pal[768], dre_alms[768], ending_pal[768];
1033
 
1034
uint8_t *basepaltable[BASEPALCOUNT] = {
1035
    palette, water_pal, slime_pal,
1036
    dre_alms, title_pal, ending_pal,
1037
    NULL /*anim_pal*/
1038
};
1039
 
1040
int32_t G_LoadLookups(void)
1041
{
1042
    int32_t fp, j;
1043
 
1044
    if ((fp=kopen4loadfrommod("lookup.dat",0)) == -1)
1045
    {
1046
        if ((fp=kopen4loadfrommod("lookup.dat",1)) == -1)
1047
        {
1048
            initprintf("ERROR: File \"lookup.dat\" not found.\n");
1049
            return 1;
1050
        }
1051
    }
1052
 
1053
    j = loadlookups(fp);
1054
 
1055
    if (j < 0)
1056
    {
1057
        if (j == -1)
1058
            initprintf("ERROR loading \"lookup.dat\": failed reading enough data.\n");
1059
        return 1;
1060
    }
1061
 
1062
    for (j=1; j<=5; j++)
1063
    {
1064
        // Account for TITLE and REALMS swap between basepal number and on-disk order.
1065
        // XXX: this reordering is better off as an argument to us.
1066
        int32_t basepalnum = (j == 3 || j == 4) ? 4+3-j : j;
1067
 
1068
        if (kread(fp, basepaltable[basepalnum], 768) != 768)
1069
            return -1;
1070
    }
1071
 
1072
    kclose(fp);
1073
 
4811 helixhorne 1074
    generatefogpals();
4564 hendricks2 1075
 
1076
    fillemptylookups();
1077
 
1078
    return 0;
1079
}