Subversion Repositories eduke32

Rev

Rev 2554 | Rev 2560 | 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"
2549 helixhorne 6
#include "scriptfile.h"
2554 helixhorne 7
#include "cache1d.h"
8
#include "kplib.h"
9
#include "baselayer.h"
2542 helixhorne 10
 
11
#include "common.h"
12
 
13
 
14
struct strllist *CommandPaths, *CommandGrps;
15
 
16
void G_AddGroup(const char *buffer)
17
{
18
    char buf[BMAX_PATH];
19
 
20
    struct strllist *s = Bcalloc(1,sizeof(struct strllist));
21
 
22
    Bstrcpy(buf, buffer);
23
 
24
    if (Bstrchr(buf,'.') == 0)
25
        Bstrcat(buf,".grp");
26
 
27
    s->str = Bstrdup(buf);
28
 
29
    if (CommandGrps)
30
    {
31
        struct strllist *t;
32
        for (t = CommandGrps; t->next; t=t->next) ;
33
        t->next = s;
34
        return;
35
    }
36
    CommandGrps = s;
37
}
38
 
39
void G_AddPath(const char *buffer)
40
{
41
    struct strllist *s = Bcalloc(1,sizeof(struct strllist));
42
    s->str = Bstrdup(buffer);
43
 
44
    if (CommandPaths)
45
    {
46
        struct strllist *t;
47
        for (t = CommandPaths; t->next; t=t->next) ;
48
        t->next = s;
49
        return;
50
    }
51
    CommandPaths = s;
52
}
2549 helixhorne 53
 
54
//////////
55
 
56
int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens)
57
{
58
    char *tok;
59
    int32_t i;
60
 
61
    if (!sf) return T_ERROR;
62
    tok = scriptfile_gettoken(sf);
63
    if (!tok) return T_EOF;
64
 
65
    for (i=ntokens-1; i>=0; i--)
66
    {
67
        if (!Bstrcasecmp(tok, tl[i].text))
68
            return tl[i].tokenid;
69
    }
70
    return T_ERROR;
71
}
2554 helixhorne 72
 
73
//////////
74
 
75
// checks from path and in ZIPs, returns 1 if NOT found
76
int32_t check_file_exist(const char *fn)
77
{
78
    int32_t opsm = pathsearchmode;
79
    char *tfn;
80
 
81
    pathsearchmode = 1;
82
    if (findfrompath(fn,&tfn) < 0)
83
    {
84
        char buf[BMAX_PATH];
85
 
86
        Bstrcpy(buf,fn);
87
        kzfindfilestart(buf);
88
        if (!kzfindfile(buf))
89
        {
90
            initprintf("Error: file \"%s\" does not exist\n",fn);
91
            pathsearchmode = opsm;
92
            return 1;
93
        }
94
    }
95
    else Bfree(tfn);
96
    pathsearchmode = opsm;
97
 
98
    return 0;
99
}
2555 helixhorne 100
 
101
 
102
//// FILE NAME / DIRECTORY LISTS ////
103
void fnlist_clearnames(fnlist_t *fnl)
104
{
105
    klistfree(fnl->finddirs);
106
    klistfree(fnl->findfiles);
107
 
108
    fnl->finddirs = fnl->findfiles = NULL;
109
    fnl->numfiles = fnl->numdirs = 0;
110
}
111
 
112
// dirflags, fileflags:
113
//  -1 means "don't get dirs/files",
114
//  otherwise ORed to flags for respective klistpath
115
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
116
                        int32_t dirflags, int32_t fileflags)
117
{
118
    CACHE1D_FIND_REC *r;
119
 
120
    fnlist_clearnames(fnl);
121
 
122
    if (dirflags != -1)
123
        fnl->finddirs = klistpath(dirname, "*", CACHE1D_FIND_DIR|dirflags);
124
    if (fileflags != -1)
125
        fnl->findfiles = klistpath(dirname, pattern, CACHE1D_FIND_FILE|fileflags);
126
 
127
    for (r=fnl->finddirs; r; r=r->next)
128
        fnl->numdirs++;
129
    for (r=fnl->findfiles; r; r=r->next)
130
        fnl->numfiles++;
131
 
132
    return(0);
133
}
134
 
135
 
136
// loads all group (grp, zip, pk3) files in the given directory
137
void G_LoadGroupsInDir(const char *dirname)
138
{
139
    static const char *extensions[3] = { "*.grp", "*.zip", "*.pk3" };
140
 
141
    char buf[BMAX_PATH];
142
    int32_t i;
143
 
144
    fnlist_t fnlist = FNLIST_INITIALIZER;
145
 
146
    for (i=0; i<3; i++)
147
    {
148
        CACHE1D_FIND_REC *rec;
149
 
150
        fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
151
 
152
        for (rec=fnlist.findfiles; rec; rec=rec->next)
153
        {
154
            Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
155
            initprintf("Using group file \"%s\".\n", buf);
156
            initgroupfile(buf);
157
        }
158
 
159
        fnlist_clearnames(&fnlist);
160
    }
161
}
162
 
163
void G_DoAutoload(const char *dirname)
164
{
165
    char buf[BMAX_PATH];
166
 
167
    Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
168
    G_LoadGroupsInDir(buf);
169
}