2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 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.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "eclass_def.h"
24 #include "iscriplib.h"
25 #include "ifilesystem.h"
28 #include "eclasslib.h"
29 #include "stream/stringstream.h"
30 #include "stream/textfilestream.h"
31 #include "modulesystem/moduleregistry.h"
34 const char *EClass_GetExtension()
39 void Eclass_ScanFile(EntityClassCollector &collector, const char *filename);
42 #include "modulesystem/singletonmodule.h"
44 class EntityClassDefDependencies : public GlobalShaderCacheModuleRef, public GlobalScripLibModuleRef {
48 EntityClassScanner m_eclassdef;
50 typedef EntityClassScanner Type;
52 STRING_CONSTANT(Name, "def");
56 m_eclassdef.scanFile = &Eclass_ScanFile;
57 m_eclassdef.getExtension = &EClass_GetExtension;
60 EntityClassScanner *getTable()
66 typedef SingletonModule<EclassDefAPI, EntityClassDefDependencies> EclassDefModule;
67 typedef Static<EclassDefModule> StaticEclassDefModule;
68 StaticRegisterModule staticRegisterEclassDef(StaticEclassDefModule::instance());
71 #include "string/string.h"
83 Parse a token out of a string
86 const char *COM_Parse(const char *data)
100 while ((c = *data) <= ' ') {
103 return 0; // end of file;
109 if (c == '/' && data[1] == '/') {
110 while (*data && *data != '\n') {
117 // handle quoted strings specially
131 // parse single characters
132 if (c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':') {
139 // parse a regular word
145 if (c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':') {
154 const char *Get_COM_Token()
160 const char *debugname;
162 void setSpecialLoad(EntityClass *e, const char *pWhat, CopiedString &p)
164 // Hydra: removed some amazingly bad cstring usage, whoever wrote that
165 // needs to be taken out and shot.
167 const char *pText = 0;
168 const char *where = 0;
170 where = strstr(e->comments(), pWhat);
175 pText = where + strlen(pWhat);
176 if (*pText == '\"') {
180 where = strchr(pText, '\"');
182 p = StringRange(pText, where);
188 #include "eclasslib.h"
192 the classname, color triple, and bounding box are parsed out of comments
193 A ? size means take the exact brush size.
195 / *QUAKED <classname> (0 0 0) ?
196 / *QUAKED <classname> (0 0 0) (-8 -8 -8) (8 8 8)
198 Flag names can follow the size description:
200 / *QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY
204 EntityClass *Eclass_InitFromText(const char *text)
206 EntityClass *e = Eclass_Alloc();
207 e->free = &Eclass_Free;
210 text = COM_Parse(text);
211 e->m_name = Get_COM_Token();
212 debugname = e->name();
215 // grab the color, reformat as texture name
216 int r = sscanf(text, " (%f %f %f)", &e->color[0], &e->color[1], &e->color[2]);
220 eclass_capture_state(e);
223 while (*text != ')') {
232 text = COM_Parse(text);
233 if (Get_COM_Token()[0] == '(') { // parse the size as two vectors
235 int r = sscanf(text, "%f %f %f) (%f %f %f)", &e->mins[0], &e->mins[1], &e->mins[2],
236 &e->maxs[0], &e->maxs[1], &e->maxs[2]);
241 for (int i = 0; i < 2; i++) {
242 while (*text != ')') {
255 // copy to the first /n
257 while (*text && *text != '\n') {
265 // any remaining words are parm flags
266 const char *p = parms;
267 for (std::size_t i = 0; i < MAX_FLAGS; i++) {
272 strcpy(e->flagnames[i], Get_COM_Token());
276 e->m_comments = text;
278 setSpecialLoad(e, "model=", e->m_modelpath);
279 StringOutputStream buffer(string_length(e->m_modelpath.c_str()));
280 buffer << PathCleaned(e->m_modelpath.c_str());
281 e->m_modelpath = buffer.c_str();
284 EntityClass_insertAttribute(*e, "angle", EntityClassAttribute("direction", "Direction", "0"));
286 EntityClass_insertAttribute(*e, "angle", EntityClassAttribute("angle", "Yaw Angle", "0"));
288 EntityClass_insertAttribute(*e, "model", EntityClassAttribute("model", "Model"));
289 EntityClass_insertAttribute(*e, "noise", EntityClassAttribute("sound", "Sound"));
294 void Eclass_ScanFile(EntityClassCollector &collector, const char *filename)
298 TextFileInputStream inputFile(filename);
299 if (inputFile.failed()) {
300 globalErrorStream() << "ScanFile: " << filename << " not found\n";
303 globalOutputStream() << "ScanFile: " << filename << "\n";
311 eParseEntityClassEnd,
312 } state = eParseDefault;
313 const char *quakeEd = "QUAKED";
316 SingleCharacterInputStream<TextFileInputStream> bufferedInput(inputFile);
319 if (!bufferedInput.readChar(c)) {
326 state = eParseSolidus;
331 state = eParseComment;
332 } else if (c == '*') {
334 state = eParseQuakeED;
339 state = eParseDefault;
344 if (*(++p) == '\0') {
345 state = eParseEntityClass;
348 state = eParseDefault;
351 case eParseEntityClass:
353 state = eParseEntityClassEnd;
358 case eParseEntityClassEnd:
360 e = Eclass_InitFromText(buffer.c_str());
361 state = eParseDefault;
365 globalErrorStream() << "Error parsing: " << debugname << " in " << filename << "\n";
369 state = eParseDefault;
371 buffer.push_back('*');
373 state = eParseEntityClass;