Subversion Repositories eduke32

Rev

Rev 5037 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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