Subversion Repositories eduke32

Rev

Rev 4801 | Rev 4804 | 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
 
4801 hendricks2 606
#ifdef __APPLE__
607
static void G_AddSteamPathsApple(const char *basepath)
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
 
623
    Bsnprintf(buf, sizeof(buf), "%s/steamapps/common/Nam/Nam.app/Contents/Resources/Nam.boxer/C.harddisk/NAM", basepath);
624
    addsearchpath(buf);
625
}
626
 
627
// A bare-bones "parser" for Valve's KeyValues VDF format.
628
// There is no guarantee this will function properly with ill-formed files.
629
static void KeyValues_SkipWhitespace(char **vdfbuf, char * const vdfbufend)
630
{
631
    while (((*vdfbuf)[0] == ' ' || (*vdfbuf)[0] == '\n' || (*vdfbuf)[0] == '\r' || (*vdfbuf)[0] == '\t' || (*vdfbuf)[0] == '\0') && *vdfbuf < vdfbufend)
632
        (*vdfbuf)++;
633
 
634
    // comments
635
    if ((*vdfbuf) + 2 < vdfbufend && (*vdfbuf)[0] == '/' && (*vdfbuf)[1] == '/')
636
    {
637
        while ((*vdfbuf)[0] != '\n' && (*vdfbuf)[0] != '\r' && *vdfbuf < vdfbufend)
638
            (*vdfbuf)++;
639
 
640
        KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
641
    }
642
}
643
static void KeyValues_SkipToEndOfQuotedToken(char **vdfbuf, char * const vdfbufend)
644
{
645
    (*vdfbuf)++;
646
    while ((*vdfbuf)[0] != '\"' && (*vdfbuf)[-1] != '\\' && *vdfbuf < vdfbufend)
647
        (*vdfbuf)++;
648
}
649
static void KeyValues_SkipToEndOfUnquotedToken(char **vdfbuf, char * const vdfbufend)
650
{
651
    while ((*vdfbuf)[0] != ' ' && (*vdfbuf)[0] != '\n' && (*vdfbuf)[0] != '\r' && (*vdfbuf)[0] != '\t' && (*vdfbuf)[0] != '\0' && *vdfbuf < vdfbufend)
652
        (*vdfbuf)++;
653
}
654
static void KeyValues_SkipNextWhatever(char **vdfbuf, char * const vdfbufend)
655
{
656
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
657
 
658
    if (*vdfbuf == vdfbufend)
659
        return;
660
 
661
    if ((*vdfbuf)[0] == '{')
662
    {
663
        (*vdfbuf)++;
664
        do
665
        {
666
            KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
667
        }
668
        while ((*vdfbuf)[0] != '}');
669
        (*vdfbuf)++;
670
    }
671
    else if ((*vdfbuf)[0] == '\"')
672
        KeyValues_SkipToEndOfQuotedToken(vdfbuf, vdfbufend);
673
    else if ((*vdfbuf)[0] != '}')
674
        KeyValues_SkipToEndOfUnquotedToken(vdfbuf, vdfbufend);
675
 
676
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
677
}
678
static char* KeyValues_NormalizeToken(char **vdfbuf, char * const vdfbufend)
679
{
680
    char *token = *vdfbuf;
681
 
682
    if ((*vdfbuf)[0] == '\"' && *vdfbuf < vdfbufend)
683
    {
684
        token++;
685
 
686
        KeyValues_SkipToEndOfQuotedToken(vdfbuf, vdfbufend);
687
        (*vdfbuf)[0] = '\0';
688
 
689
        // account for escape sequences
690
        char *writeseeker = token, *readseeker = token;
691
        while (readseeker <= *vdfbuf)
692
        {
693
            if (readseeker[0] == '\\')
694
                readseeker++;
695
 
696
            writeseeker[0] = readseeker[0];
697
 
698
            writeseeker++;
699
            readseeker++;
700
        }
701
 
702
        return token;
703
    }
704
 
705
    KeyValues_SkipToEndOfUnquotedToken(vdfbuf, vdfbufend);
706
    (*vdfbuf)[0] = '\0';
707
 
708
    return token;
709
}
710
static void KeyValues_FindKey(char **vdfbuf, char * const vdfbufend, const char *token)
711
{
712
    char *ParentKey = KeyValues_NormalizeToken(vdfbuf, vdfbufend);
713
    if (token != NULL) // pass in NULL to find the next key instead of a specific one
714
        while (Bstrcmp(ParentKey, token) != 0 && *vdfbuf < vdfbufend)
715
        {
716
            KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
717
            ParentKey = KeyValues_NormalizeToken(vdfbuf, vdfbufend);
718
        }
719
 
720
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
721
}
722
static int32_t KeyValues_FindParentKey(char **vdfbuf, char * const vdfbufend, const char *token)
723
{
724
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
725
 
726
    // end of scope
727
    if ((*vdfbuf)[0] == '}')
728
        return 0;
729
 
730
    KeyValues_FindKey(vdfbuf, vdfbufend, token);
731
 
732
    // ignore the wrong type
733
    while ((*vdfbuf)[0] != '{' && *vdfbuf < vdfbufend)
734
    {
735
        KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
736
        KeyValues_FindKey(vdfbuf, vdfbufend, token);
737
    }
738
 
739
    if (*vdfbuf == vdfbufend)
740
        return 0;
741
 
742
    return 1;
743
}
744
static char* KeyValues_FindKeyValue(char **vdfbuf, char * const vdfbufend, const char *token)
745
{
746
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
747
 
748
    // end of scope
749
    if ((*vdfbuf)[0] == '}')
750
        return NULL;
751
 
752
    KeyValues_FindKey(vdfbuf, vdfbufend, token);
753
 
754
    // ignore the wrong type
755
    while ((*vdfbuf)[0] == '{' && *vdfbuf < vdfbufend)
756
    {
757
        KeyValues_SkipNextWhatever(vdfbuf, vdfbufend);
758
        KeyValues_FindKey(vdfbuf, vdfbufend, token);
759
    }
760
 
761
    KeyValues_SkipWhitespace(vdfbuf, vdfbufend);
762
 
763
    if (*vdfbuf == vdfbufend)
764
        return NULL;
765
 
766
    return KeyValues_NormalizeToken(vdfbuf, vdfbufend);
767
}
768
 
769
static void G_ParseSteamKeyValuesForPaths(const char *vdf)
770
{
771
    int32_t fd = Bopen(vdf, BO_RDONLY);
772
    int32_t size = Bfilelength(fd);
773
    char *vdfbufstart, *vdfbuf, *vdfbufend;
774
 
775
    if (size <= 0)
776
        return;
777
 
778
    vdfbufstart = vdfbuf = (char*)Bmalloc(size);
779
    size = (int32_t)Bread(fd, vdfbuf, size);
780
    Bclose(fd);
781
    vdfbufend = vdfbuf + size;
782
 
783
    if (KeyValues_FindParentKey(&vdfbuf, vdfbufend, "LibraryFolders"))
784
    {
785
        char *result;
786
        vdfbuf++;
787
        while ((result = KeyValues_FindKeyValue(&vdfbuf, vdfbufend, NULL)) != NULL)
788
            G_AddSteamPathsApple(result);
789
    }
790
 
791
    Bfree(vdfbufstart);
792
}
793
#endif
794
 
3581 hendricks2 795
void G_AddSearchPaths(void)
796
{
797
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
798
    addsearchpath("/usr/share/games/jfduke3d");
799
    addsearchpath("/usr/local/share/games/jfduke3d");
800
    addsearchpath("/usr/share/games/eduke32");
801
    addsearchpath("/usr/local/share/games/eduke32");
802
#elif defined(__APPLE__)
4801 hendricks2 803
    char buf[BMAX_PATH];
4802 hendricks2 804
    int32_t i;
805
    char *applications[] = { osx_getapplicationsdir(0), osx_getapplicationsdir(1) };
806
    char *support[] = { osx_getsupportdir(0), osx_getsupportdir(1) };
4801 hendricks2 807
 
4802 hendricks2 808
    for (i = 0; i < 2; i++)
809
    {
810
        Bsnprintf(buf, sizeof(buf), "%s/Steam", support[i]);
811
        G_AddSteamPathsApple(buf);
4801 hendricks2 812
 
4802 hendricks2 813
        Bsnprintf(buf, sizeof(buf), "%s/Steam/steamapps/libraryfolders.vdf", support[i]);
814
        G_ParseSteamKeyValuesForPaths(buf);
4801 hendricks2 815
 
4802 hendricks2 816
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D.app/Contents/Resources/Duke Nukem 3D.boxer/C.harddisk", applications[i]);
817
        addsearchpath(buf);
818
    }
4801 hendricks2 819
 
4802 hendricks2 820
    for (i = 0; i < 2; i++)
821
    {
822
        Bsnprintf(buf, sizeof(buf), "%s/JFDuke3D", support[i]);
823
        addsearchpath(buf);
824
        Bsnprintf(buf, sizeof(buf), "%s/EDuke32", support[i]);
825
        addsearchpath(buf);
826
    }
4801 hendricks2 827
 
4802 hendricks2 828
    for (i = 0; i < 2; i++)
829
    {
830
        Bfree(applications[i]);
831
        Bfree(support[i]);
832
    }
3581 hendricks2 833
#elif defined (_WIN32)
3615 terminx 834
    char buf[BMAX_PATH];
3671 hendricks2 835
    const char* instpath;
3615 terminx 836
 
4790 hendricks2 837
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM_DUKE3D)))
3622 terminx 838
    {
4557 hendricks2 839
        Bsnprintf(buf, sizeof(buf), "%s/gameroot", instpath);
3622 terminx 840
        addsearchpath(buf);
3615 terminx 841
 
4594 hendricks2 842
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/dc", instpath);
3622 terminx 843
        addsearchpath(buf);
4594 hendricks2 844
 
845
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/nw", instpath);
846
        addsearchpath(buf);
847
 
848
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/vacation", instpath);
849
        addsearchpath(buf);
3615 terminx 850
    }
851
 
4790 hendricks2 852
    if ((instpath = G_GetInstallPath(INSTPATH_GOG_DUKE3D)))
3671 hendricks2 853
        addsearchpath(instpath);
4790 hendricks2 854
 
855
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_DUKE3D)))
856
    {
857
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
858
        addsearchpath(buf);
859
    }
860
 
861
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_ANTH)))
862
    {
863
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
864
        addsearchpath(buf);
865
    }
866
 
867
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM_NAM)))
868
    {
869
        Bsnprintf(buf, sizeof(buf), "%s/NAM", instpath);
870
        addsearchpath(buf);
871
    }
3581 hendricks2 872
#endif
873
}
874
 
3654 terminx 875
void G_CleanupSearchPaths(void)
876
{
877
#ifdef _WIN32
878
    char buf[BMAX_PATH];
3671 hendricks2 879
    const char* instpath;
3654 terminx 880
 
4790 hendricks2 881
    if ((instpath = G_GetInstallPath(INSTPATH_STEAM_DUKE3D)))
3654 terminx 882
    {
4557 hendricks2 883
        Bsnprintf(buf, sizeof(buf), "%s/gameroot", instpath);
3654 terminx 884
        removesearchpath(buf);
885
 
4594 hendricks2 886
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/dc", instpath);
3654 terminx 887
        removesearchpath(buf);
4594 hendricks2 888
 
889
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/nw", instpath);
890
        removesearchpath(buf);
891
 
892
        Bsnprintf(buf, sizeof(buf), "%s/gameroot/addons/vacation", instpath);
893
        removesearchpath(buf);
3654 terminx 894
    }
895
 
4790 hendricks2 896
    if ((instpath = G_GetInstallPath(INSTPATH_GOG_DUKE3D)))
3671 hendricks2 897
        removesearchpath(instpath);
4790 hendricks2 898
 
899
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_DUKE3D)))
900
    {
901
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
902
        removesearchpath(buf);
903
    }
904
 
905
    if ((instpath = G_GetInstallPath(INSTPATH_3DR_ANTH)))
906
    {
907
        Bsnprintf(buf, sizeof(buf), "%s/Duke Nukem 3D", instpath);
908
        removesearchpath(buf);
909
    }
910
 
911
    if (g_gameType != GAMEFLAG_NAM && (instpath = G_GetInstallPath(INSTPATH_STEAM_NAM)))
912
    {
913
        Bsnprintf(buf, sizeof(buf), "%s/NAM", instpath);
914
        removesearchpath(buf);
915
    }
3654 terminx 916
#endif
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_firstFogPal;
1026
 
1027
int32_t G_LoadLookups(void)
1028
{
1029
    int32_t fp, j;
1030
 
1031
    if ((fp=kopen4loadfrommod("lookup.dat",0)) == -1)
1032
    {
1033
        if ((fp=kopen4loadfrommod("lookup.dat",1)) == -1)
1034
        {
1035
            initprintf("ERROR: File \"lookup.dat\" not found.\n");
1036
            return 1;
1037
        }
1038
    }
1039
 
1040
    j = loadlookups(fp);
1041
 
1042
    if (j < 0)
1043
    {
1044
        if (j == -1)
1045
            initprintf("ERROR loading \"lookup.dat\": failed reading enough data.\n");
1046
        return 1;
1047
    }
1048
 
1049
    for (j=1; j<=5; j++)
1050
    {
1051
        // Account for TITLE and REALMS swap between basepal number and on-disk order.
1052
        // XXX: this reordering is better off as an argument to us.
1053
        int32_t basepalnum = (j == 3 || j == 4) ? 4+3-j : j;
1054
 
1055
        if (kread(fp, basepaltable[basepalnum], 768) != 768)
1056
            return -1;
1057
    }
1058
 
1059
    kclose(fp);
1060
 
1061
    g_firstFogPal = generatefogpals();
1062
 
1063
    fillemptylookups();
1064
 
1065
    return 0;
1066
}