Subversion Repositories eduke32

Rev

Rev 5005 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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