Debian Codegraph @

tools/quake2/extra/qe4/eclass.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4
5 This file is part of Quake 2 Tools source code.
6
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "qe3.h"
24 #include "io.h"
25
26 eclass_t *eclass;
27 eclass_t *eclass_bad;
28 char eclass_directory[1024];
29
30 /*
31
32 the classname, color triple, and bounding box are parsed out of comments
33 A ? size means take the exact brush size.
34
35 /*QUAKED <classname> (0 0 0) ?
36 /*QUAKED <classname> (0 0 0) (-8 -8 -8) (8 8 8)
37
38 Flag names can follow the size description:
39
40 /*QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY
41
42 */
43 char *debugname;
44
45 eclass_t *Eclass_InitFromText (char *text)
46 {
47 char *t;
48 int len;
49 int r, i;
50 char parms[256], *p;
51 eclass_t *e;
52 char color[128];
53
54 e = qmalloc(sizeof(*e));
55 memset (e, 0, sizeof(*e));
56
57 text += strlen("/*QUAKED ");
58
59 // grab the name
60 text = COM_Parse (text);
61 e->name = qmalloc (strlen(com_token)+1);
62 strcpy (e->name, com_token);
63 debugname = e->name;
64
65 // grab the color, reformat as texture name
66 r = sscanf (text," (%f %f %f)", &e->color[0], &e->color[1], &e->color[2]);
67 if (r != 3)
68 return e;
69 sprintf (color, "(%f %f %f)", e->color[0], e->color[1], e->color[2]);
70 strcpy (e->texdef.name, color);
71
72 while (*text != ')')
73 {
74 if (!*text)
75 return e;
76 text++;
77 }
78 text++;
79
80 // get the size
81 text = COM_Parse (text);
82 if (com_token[0] == '(')
83 { // parse the size as two vectors
84 e->fixedsize = true;
85 r = sscanf (text,"%f %f %f) (%f %f %f)", &e->mins[0], &e->mins[1], &e->mins[2],
86 &e->maxs[0], &e->maxs[1], &e->maxs[2]);
87 if (r != 6)
88 return e;
89
90 for (i=0 ; i<2 ; i++)
91 {
92 while (*text != ')')
93 {
94 if (!*text)
95 return e;
96 text++;
97 }
98 text++;
99 }
100 }
101 else
102 { // use the brushes
103 }
104
105 // get the flags
106
107
108 // copy to the first /n
109 p = parms;
110 while (*text && *text != '\n')
111 *p++ = *text++;
112 *p = 0;
113 text++;
114
115 // any remaining words are parm flags
116 p = parms;
117 for (i=0 ; i<8 ; i++)
118 {
119 p = COM_Parse (p);
120 if (!p)
121 break;
122 strcpy (e->flagnames[i], com_token);
123 }
124
125 // find the length until close comment
126 for (t=text ; t[0] && !(t[0]=='*' && t[1]=='/') ; t++)
127 ;
128
129 // copy the comment block out
130 len = t-text;
131 e->comments = qmalloc (len+1);
132 memcpy (e->comments, text, len);
133 #if 0
134 for (i=0 ; i<len ; i++)
135 if (text[i] == '\n')
136 e->comments[i] = '\r';
137 else
138 e->comments[i] = text[i];
139 #endif
140 e->comments[len] = 0;
141
142 return e;
143 }
144
145
146 /*
147 =================
148 Eclass_InsertAlphabetized
149 =================
150 */
151 void Eclass_InsertAlphabetized (eclass_t *e)
152 {
153 eclass_t *s;
154
155 if (!eclass)
156 {
157 eclass = e;
158 return;
159 }
160
161
162 s = eclass;
163 if (stricmp (e->name, s->name) < 0)
164 {
165 e->next = s;
166 eclass = e;
167 return;
168 }
169
170 do
171 {
172 if (!s->next || stricmp (e->name, s->next->name) < 0)
173 {
174 e->next = s->next;
175 s->next = e;
176 return;
177 }
178 s=s->next;
179 } while (1);
180 }
181
182
183 /*
184 =================
185 Eclass_ScanFile
186 =================
187 */
188 void Eclass_ScanFile (char *filename)
189 {
190 int size;
191 char *data;
192 eclass_t *e;
193 int i;
194 char temp[1024];
195
196 QE_ConvertDOSToUnixName( temp, filename );
197
198 Sys_Printf ("ScanFile: %s\n", temp);
199
200 size = LoadFile (filename, (void *)&data);
201
202 for (i=0 ; i<size ; i++)
203 if (!strncmp(data+i, "/*QUAKED",8))
204 {
205 e = Eclass_InitFromText (data+i);
206 if (e)
207 Eclass_InsertAlphabetized (e);
208 else
209 printf ("Error parsing: %s in %s\n",debugname, filename);
210 }
211
212 free (data);
213 }
214
215
216
217 void Eclass_InitForSourceDirectory (char *path)
218 {
219 struct _finddata_t fileinfo;
220 int handle;
221 char filename[1024];
222 char filebase[1024];
223 char temp[1024];
224 char *s;
225
226 QE_ConvertDOSToUnixName( temp, path );
227
228 Sys_Printf ("Eclass_InitForSourceDirectory: %s\n", temp );
229
230 strcpy (filebase, path);
231 s = filebase + strlen(filebase)-1;
232 while (*s != '\\' && *s != '/' && s!=filebase)
233 s--;
234 *s = 0;
235
236 eclass = NULL;
237
238 handle = _findfirst (path, &fileinfo);
239 if (handle != -1)
240 {
241 do
242 {
243 sprintf (filename, "%s\\%s", filebase, fileinfo.name);
244 Eclass_ScanFile (filename);
245 } while (_findnext( handle, &fileinfo ) != -1);
246
247 _findclose (handle);
248 }
249
250 eclass_bad = Eclass_InitFromText ("/*QUAKED UNKNOWN_CLASS (0 0.5 0) ?");
251 }
252
253 eclass_t *Eclass_ForName (char *name, qboolean has_brushes)
254 {
255 eclass_t *e;
256 char init[1024];
257
258 if (!name)
259 return eclass_bad;
260
261 for (e=eclass ; e ; e=e->next)
262 if (!strcmp (name, e->name))
263 return e;
264
265 // create a new class for it
266 if (has_brushes)
267 {
268 sprintf (init, "/*QUAKED %s (0 0.5 0) ?\nNot found in source.\n", name);
269 e = Eclass_InitFromText (init);
270 }
271 else
272 {
273 sprintf (init, "/*QUAKED %s (0 0.5 0) (-8 -8 -8) (8 8 8)\nNot found in source.\n", name);
274 e = Eclass_InitFromText (init);
275 }
276
277 Eclass_InsertAlphabetized (e);
278
279 return e;
280 }
281