Subversion Repositories eduke32

Rev

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