Subversion Repositories eduke32

Rev

Rev 4887 | Rev 4989 | 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);
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
 
4804 hendricks2 632
#if defined __APPLE__
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
{
810
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
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");
826
#elif defined(__APPLE__)
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