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()
38 void Eclass_ScanFile (EntityClassCollector& collector, const char *filename);
41 #include "modulesystem/singletonmodule.h"
43 class EntityClassDefDependencies : public GlobalShaderCacheModuleRef, public GlobalScripLibModuleRef
49 EntityClassScanner m_eclassdef;
51 typedef EntityClassScanner Type;
52 STRING_CONSTANT(Name, "def");
56 m_eclassdef.scanFile = &Eclass_ScanFile;
57 m_eclassdef.getExtension = &EClass_GetExtension;
59 EntityClassScanner* getTable()
65 typedef SingletonModule<EclassDefAPI, EntityClassDefDependencies> EclassDefModule;
66 typedef Static<EclassDefModule> StaticEclassDefModule;
67 StaticRegisterModule staticRegisterEclassDef(StaticEclassDefModule::instance());
70 #include "string/string.h"
82 Parse a token out of a string
85 const char *COM_Parse (const char *data)
98 while ( (c = *data) <= ' ')
103 return 0; // end of file;
109 if (c=='/' && data[1] == '/')
111 while (*data && *data != '\n')
117 // handle quoted strings specially
134 // parse single characters
135 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
143 // parse a regular word
150 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
158 const char* Get_COM_Token()
164 const char *debugname;
166 void setSpecialLoad(EntityClass *e, const char* pWhat, CopiedString& p)
168 // Hydra: removed some amazingly bad cstring usage, whoever wrote that
169 // needs to be taken out and shot.
171 const char *pText = 0;
172 const char *where = 0;
174 where = strstr(e->comments(),pWhat);
178 pText = where + strlen(pWhat);
182 where = strchr(pText,'\"');
185 p = StringRange(pText, where);
193 #include "eclasslib.h"
197 the classname, color triple, and bounding box are parsed out of comments
198 A ? size means take the exact brush size.
200 / *QUAKED <classname> (0 0 0) ?
201 / *QUAKED <classname> (0 0 0) (-8 -8 -8) (8 8 8)
203 Flag names can follow the size description:
205 / *QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY
209 EntityClass *Eclass_InitFromText (const char *text)
211 EntityClass* e = Eclass_Alloc();
212 e->free = &Eclass_Free;
215 text = COM_Parse (text);
216 e->m_name = Get_COM_Token();
217 debugname = e->name();
220 // grab the color, reformat as texture name
221 int r = sscanf (text," (%f %f %f)", &e->color[0], &e->color[1], &e->color[2]);
224 eclass_capture_state(e);
237 text = COM_Parse (text);
238 if (Get_COM_Token()[0] == '(')
239 { // parse the size as two vectors
241 int r = sscanf (text,"%f %f %f) (%f %f %f)", &e->mins[0], &e->mins[1], &e->mins[2],
242 &e->maxs[0], &e->maxs[1], &e->maxs[2]);
247 for (int i=0 ; i<2 ; i++)
263 // copy to the first /n
265 while (*text && *text != '\n')
272 // any remaining words are parm flags
273 const char* p = parms;
274 for (std::size_t i=0 ; i<MAX_FLAGS ; i++)
279 strcpy (e->flagnames[i], Get_COM_Token());
283 e->m_comments = text;
285 setSpecialLoad(e, "model=", e->m_modelpath);
286 StringOutputStream buffer(string_length(e->m_modelpath.c_str()));
287 buffer << PathCleaned(e->m_modelpath.c_str());
288 e->m_modelpath = buffer.c_str();
292 EntityClass_insertAttribute(*e, "angle", EntityClassAttribute("direction", "Direction", "0"));
296 EntityClass_insertAttribute(*e, "angle", EntityClassAttribute("angle", "Yaw Angle", "0"));
298 EntityClass_insertAttribute(*e, "model", EntityClassAttribute("model", "Model"));
299 EntityClass_insertAttribute(*e, "noise", EntityClassAttribute("sound", "Sound"));
304 void Eclass_ScanFile (EntityClassCollector& collector, const char *filename)
308 TextFileInputStream inputFile(filename);
309 if(inputFile.failed())
311 globalErrorStream() << "ScanFile: " << filename << " not found\n";
314 globalOutputStream() << "ScanFile: " << filename << "\n";
323 eParseEntityClassEnd,
324 } state = eParseDefault;
325 const char* quakeEd = "QUAKED";
328 SingleCharacterInputStream<TextFileInputStream> bufferedInput(inputFile);
332 if(!bufferedInput.readChar(c))
342 state = eParseSolidus;
348 state = eParseComment;
353 state = eParseQuakeED;
359 state = eParseDefault;
367 state = eParseEntityClass;
372 state = eParseDefault;
375 case eParseEntityClass:
378 state = eParseEntityClassEnd;
385 case eParseEntityClassEnd:
388 e = Eclass_InitFromText(buffer.c_str());
389 state = eParseDefault;
393 globalErrorStream() << "Error parsing: " << debugname << " in " << filename << "\n";
396 state = eParseDefault;
400 buffer.push_back('*');
402 state = eParseEntityClass;