Subversion Repositories eduke32

Rev

Rev 2726 | Rev 2789 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2542 helixhorne 1
//
2
// Common non-engine code/data for EDuke32 and Mapster32
3
//
4
 
5
#include "compat.h"
2726 hendricks2 6
#include "build.h"
2549 helixhorne 7
#include "scriptfile.h"
2554 helixhorne 8
#include "cache1d.h"
9
#include "kplib.h"
10
#include "baselayer.h"
2542 helixhorne 11
 
12
#include "common.h"
2726 hendricks2 13
#include "common_game.h"
2542 helixhorne 14
 
2726 hendricks2 15
int32_t g_gameType = 0;
2542 helixhorne 16
 
2726 hendricks2 17
// grp/con/def handling
18
 
19
char *defaultgamegrp[GAMECOUNT] = { "DUKE3D.GRP", "NAM.GRP", "NAPALM.GRP", "WW2GI.GRP" };
20
char *defaultdeffilename[GAMECOUNT] = { "duke3d.def", "nam.def", "napalm.def", "ww2gi.def" };
21
char *defaultconfilename = "GAME.CON";
22
char *defaultgameconfilename[GAMECOUNT] = { "EDUKE.CON", "NAM.CON", "NAPALM.CON", "WW2GI.CON" };
23
 
24
// g_grpNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
25
char *g_grpNamePtr = NULL;
26
// g_defNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
27
char *g_defNamePtr = NULL;
28
// g_scriptNamePtr can ONLY point to a malloc'd block (length BMAX_PATH)
29
char *g_scriptNamePtr = NULL;
30
 
31
void clearGrpNamePtr(void)
32
{
33
    if (g_grpNamePtr != NULL)
34
        Bfree(g_grpNamePtr);
35
    // g_grpNamePtr assumed to be assigned to right after
36
}
37
 
38
void clearDefNamePtr(void)
39
{
40
    if (g_defNamePtr != NULL)
41
        Bfree(g_defNamePtr);
42
    // g_defNamePtr assumed to be assigned to right after
43
}
44
 
45
void clearScriptNamePtr(void)
46
{
47
    if (g_scriptNamePtr != NULL)
48
        Bfree(g_scriptNamePtr);
49
    // g_scriptNamePtr assumed to be assigned to right after
50
}
51
 
52
char *G_DefaultGrpFile(void)
53
{
54
    if (DUKE)
55
        return defaultgamegrp[GAME_DUKE];
56
    // order is important for the following three because GAMEFLAG_NAM overlaps all
57
    else if (NAPALM)
58
        return defaultgamegrp[GAME_NAPALM];
59
    else if (WW2GI)
60
        return defaultgamegrp[GAME_WW2GI];
61
    else if (NAM)
62
        return defaultgamegrp[GAME_NAM];
63
 
64
    return defaultgamegrp[0];
65
}
66
char *G_DefaultDefFile(void)
67
{
68
    if (DUKE)
69
        return defaultdeffilename[GAME_DUKE];
70
    else if (WW2GI)
71
        return defaultdeffilename[GAME_WW2GI];
72
    else if (NAPALM)
73
    {
2752 helixhorne 74
        if (!testkopen(defaultdeffilename[GAME_NAPALM],0) && testkopen(defaultdeffilename[GAME_NAM],0))
2726 hendricks2 75
            return defaultdeffilename[GAME_NAM]; // NAM/Napalm Sharing
76
        else
77
            return defaultdeffilename[GAME_NAPALM];
78
    }
79
    else if (NAM)
80
    {
2752 helixhorne 81
        if (!testkopen(defaultdeffilename[GAME_NAM],0) && testkopen(defaultdeffilename[GAME_NAPALM],0))
2726 hendricks2 82
            return defaultdeffilename[GAME_NAPALM]; // NAM/Napalm Sharing
83
        else
84
            return defaultdeffilename[GAME_NAM];
85
    }
86
 
87
    return defaultdeffilename[0];
88
}
89
char *G_DefaultConFile(void)
90
{
2752 helixhorne 91
    if (DUKE && testkopen(defaultgameconfilename[GAME_DUKE],0))
2726 hendricks2 92
        return defaultgameconfilename[GAME_DUKE];
2752 helixhorne 93
    else if (WW2GI && testkopen(defaultgameconfilename[GAME_WW2GI],0))
2726 hendricks2 94
        return defaultgameconfilename[GAME_WW2GI];
95
    else if (NAPALM)
96
    {
2752 helixhorne 97
        if (!testkopen(defaultgameconfilename[GAME_NAPALM],0))
2726 hendricks2 98
        {
2752 helixhorne 99
            if (testkopen(defaultgameconfilename[GAME_NAM],0))
2726 hendricks2 100
                return defaultgameconfilename[GAME_NAM]; // NAM/Napalm Sharing
101
        }
102
        else
103
            return defaultgameconfilename[GAME_NAPALM];
104
    }
105
    else if (NAM)
106
    {
2752 helixhorne 107
        if (!testkopen(defaultgameconfilename[GAME_NAM],0))
2726 hendricks2 108
        {
2752 helixhorne 109
            if (testkopen(defaultgameconfilename[GAME_NAPALM],0))
2726 hendricks2 110
                return defaultgameconfilename[GAME_NAPALM]; // NAM/Napalm Sharing
111
        }
112
        else
113
            return defaultgameconfilename[GAME_NAM];
114
    }
115
 
116
    return defaultconfilename;
117
}
118
 
119
char *G_GrpFile(void)
120
{
121
    if (g_grpNamePtr == NULL)
122
        return G_DefaultGrpFile();
123
    else
124
        return g_grpNamePtr;
125
}
126
char *G_DefFile(void)
127
{
128
    if (g_defNamePtr == NULL)
129
        return G_DefaultDefFile();
130
    else
131
        return g_defNamePtr;
132
}
133
char *G_ConFile(void)
134
{
135
    if (g_scriptNamePtr == NULL)
136
        return G_DefaultConFile();
137
    else
138
        return g_scriptNamePtr;
139
}
140
 
141
//////////
142
 
2542 helixhorne 143
struct strllist *CommandPaths, *CommandGrps;
144
 
145
void G_AddGroup(const char *buffer)
146
{
147
    char buf[BMAX_PATH];
148
 
149
    struct strllist *s = Bcalloc(1,sizeof(struct strllist));
150
 
151
    Bstrcpy(buf, buffer);
152
 
153
    if (Bstrchr(buf,'.') == 0)
154
        Bstrcat(buf,".grp");
155
 
156
    s->str = Bstrdup(buf);
157
 
158
    if (CommandGrps)
159
    {
160
        struct strllist *t;
161
        for (t = CommandGrps; t->next; t=t->next) ;
162
        t->next = s;
163
        return;
164
    }
165
    CommandGrps = s;
166
}
167
 
168
void G_AddPath(const char *buffer)
169
{
170
    struct strllist *s = Bcalloc(1,sizeof(struct strllist));
171
    s->str = Bstrdup(buffer);
172
 
173
    if (CommandPaths)
174
    {
175
        struct strllist *t;
176
        for (t = CommandPaths; t->next; t=t->next) ;
177
        t->next = s;
178
        return;
179
    }
180
    CommandPaths = s;
181
}
2549 helixhorne 182
 
183
//////////
184
 
185
int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens)
186
{
187
    char *tok;
188
    int32_t i;
189
 
190
    if (!sf) return T_ERROR;
191
    tok = scriptfile_gettoken(sf);
192
    if (!tok) return T_EOF;
193
 
194
    for (i=ntokens-1; i>=0; i--)
195
    {
196
        if (!Bstrcasecmp(tok, tl[i].text))
197
            return tl[i].tokenid;
198
    }
199
    return T_ERROR;
200
}
2554 helixhorne 201
 
202
//////////
203
 
2752 helixhorne 204
// returns: 1 if file could be opened, 0 else
205
int32_t testkopen(const char *filename, char searchfirst)
206
{
207
    int32_t fd = kopen4load(filename, searchfirst);
208
    if (fd >= 0)
209
        kclose(fd);
210
    return (fd >= 0);
211
}
212
 
2554 helixhorne 213
// checks from path and in ZIPs, returns 1 if NOT found
214
int32_t check_file_exist(const char *fn)
215
{
216
    int32_t opsm = pathsearchmode;
217
    char *tfn;
218
 
219
    pathsearchmode = 1;
220
    if (findfrompath(fn,&tfn) < 0)
221
    {
222
        char buf[BMAX_PATH];
223
 
224
        Bstrcpy(buf,fn);
225
        kzfindfilestart(buf);
226
        if (!kzfindfile(buf))
227
        {
228
            initprintf("Error: file \"%s\" does not exist\n",fn);
229
            pathsearchmode = opsm;
230
            return 1;
231
        }
232
    }
233
    else Bfree(tfn);
234
    pathsearchmode = opsm;
235
 
236
    return 0;
237
}
2555 helixhorne 238
 
239
 
240
//// FILE NAME / DIRECTORY LISTS ////
241
void fnlist_clearnames(fnlist_t *fnl)
242
{
243
    klistfree(fnl->finddirs);
244
    klistfree(fnl->findfiles);
245
 
246
    fnl->finddirs = fnl->findfiles = NULL;
247
    fnl->numfiles = fnl->numdirs = 0;
248
}
249
 
250
// dirflags, fileflags:
251
//  -1 means "don't get dirs/files",
252
//  otherwise ORed to flags for respective klistpath
253
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
254
                        int32_t dirflags, int32_t fileflags)
255
{
256
    CACHE1D_FIND_REC *r;
257
 
258
    fnlist_clearnames(fnl);
259
 
260
    if (dirflags != -1)
261
        fnl->finddirs = klistpath(dirname, "*", CACHE1D_FIND_DIR|dirflags);
262
    if (fileflags != -1)
263
        fnl->findfiles = klistpath(dirname, pattern, CACHE1D_FIND_FILE|fileflags);
264
 
265
    for (r=fnl->finddirs; r; r=r->next)
266
        fnl->numdirs++;
267
    for (r=fnl->findfiles; r; r=r->next)
268
        fnl->numfiles++;
269
 
270
    return(0);
271
}
272
 
273
 
274
// loads all group (grp, zip, pk3) files in the given directory
275
void G_LoadGroupsInDir(const char *dirname)
276
{
277
    static const char *extensions[3] = { "*.grp", "*.zip", "*.pk3" };
278
 
279
    char buf[BMAX_PATH];
280
    int32_t i;
281
 
282
    fnlist_t fnlist = FNLIST_INITIALIZER;
283
 
284
    for (i=0; i<3; i++)
285
    {
286
        CACHE1D_FIND_REC *rec;
287
 
288
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
289
 
290
        for (rec=fnlist.findfiles; rec; rec=rec->next)
291
        {
292
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
293
            initprintf("Using group file \"%s\".\n", buf);
294
            initgroupfile(buf);
295
        }
296
 
297
        fnlist_clearnames(&fnlist);
298
    }
299
}
300
 
301
void G_DoAutoload(const char *dirname)
302
{
303
    char buf[BMAX_PATH];
304
 
305
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
306
    G_LoadGroupsInDir(buf);
307
}
2560 helixhorne 308
 
309
////
310
 
311
// returns a buffer of size BMAX_PATH
312
char *dup_filename(const char *fn)
313
{
314
    char *buf = Bmalloc(BMAX_PATH);
315
 
316
    return Bstrncpyz(buf, fn, BMAX_PATH);
317
}