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(){
37 void Eclass_ScanFile( EntityClassCollector& collector, const char *filename );
40 #include "modulesystem/singletonmodule.h"
42 class EntityClassDefDependencies : public GlobalShaderCacheModuleRef, public GlobalScripLibModuleRef
48 EntityClassScanner m_eclassdef;
50 typedef EntityClassScanner Type;
51 STRING_CONSTANT( Name, "def" );
54 m_eclassdef.scanFile = &Eclass_ScanFile;
55 m_eclassdef.getExtension = &EClass_GetExtension;
57 EntityClassScanner* getTable(){
62 typedef SingletonModule<EclassDefAPI, EntityClassDefDependencies> EclassDefModule;
63 typedef Static<EclassDefModule> StaticEclassDefModule;
64 StaticRegisterModule staticRegisterEclassDef( StaticEclassDefModule::instance() );
67 #include "string/string.h"
79 Parse a token out of a string
82 const char *COM_Parse( const char *data ){
95 while ( ( c = *data ) <= ' ' )
99 return 0; // end of file;
105 if ( c == '/' && data[1] == '/' ) {
106 while ( *data && *data != '\n' )
112 // handle quoted strings specially
127 // parse single characters
128 if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) {
135 // parse a regular word
142 if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) {
151 const char* Get_COM_Token(){
156 const char *debugname;
158 void setSpecialLoad( EntityClass *e, const char* pWhat, CopiedString& p ){
159 // Hydra: removed some amazingly bad cstring usage, whoever wrote that
160 // needs to be taken out and shot.
162 const char *pText = 0;
163 const char *where = 0;
165 where = strstr( e->comments(),pWhat );
170 pText = where + strlen( pWhat );
171 if ( *pText == '\"' ) {
175 where = strchr( pText,'\"' );
177 p = StringRange( pText, where );
185 #include "eclasslib.h"
189 the classname, color triple, and bounding box are parsed out of comments
190 A ? size means take the exact brush size.
192 / *QUAKED <classname> (0 0 0) ?
193 / *QUAKED <classname> (0 0 0) (-8 -8 -8) (8 8 8)
195 Flag names can follow the size description:
197 / *QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY
201 EntityClass *Eclass_InitFromText( const char *text ){
202 EntityClass* e = Eclass_Alloc();
203 e->free = &Eclass_Free;
206 text = COM_Parse( text );
207 e->m_name = Get_COM_Token();
208 debugname = e->name();
211 // grab the color, reformat as texture name
212 int r = sscanf( text," (%f %f %f)", &e->color[0], &e->color[1], &e->color[2] );
216 eclass_capture_state( e );
219 while ( *text != ')' )
229 text = COM_Parse( text );
230 if ( Get_COM_Token()[0] == '(' ) { // parse the size as two vectors
232 int r = sscanf( text,"%f %f %f) (%f %f %f)", &e->mins[0], &e->mins[1], &e->mins[2],
233 &e->maxs[0], &e->maxs[1], &e->maxs[2] );
238 for ( int i = 0 ; i < 2 ; i++ )
240 while ( *text != ')' )
254 // copy to the first /n
256 while ( *text && *text != '\n' )
263 // any remaining words are parm flags
264 const char* p = parms;
265 for ( std::size_t i = 0 ; i < MAX_FLAGS ; i++ )
271 strcpy( e->flagnames[i], Get_COM_Token() );
275 e->m_comments = text;
277 setSpecialLoad( e, "model=", e->m_modelpath );
278 StringOutputStream buffer( string_length( e->m_modelpath.c_str() ) );
279 buffer << PathCleaned( e->m_modelpath.c_str() );
280 e->m_modelpath = buffer.c_str();
282 if ( !e->fixedsize ) {
283 EntityClass_insertAttribute( *e, "angle", EntityClassAttribute( "direction", "Direction", "0" ) );
287 EntityClass_insertAttribute( *e, "angle", EntityClassAttribute( "angle", "Yaw Angle", "0" ) );
289 EntityClass_insertAttribute( *e, "model", EntityClassAttribute( "model", "Model" ) );
290 EntityClass_insertAttribute( *e, "noise", EntityClassAttribute( "sound", "Sound" ) );
295 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";
312 eParseEntityClassEnd,
313 } state = eParseDefault;
314 const char* quakeEd = "QUAKED";
317 SingleCharacterInputStream<TextFileInputStream> bufferedInput( inputFile );
321 if ( !bufferedInput.readChar( c ) ) {
329 state = eParseSolidus;
334 state = eParseComment;
336 else if ( c == '*' ) {
338 state = eParseQuakeED;
343 state = eParseDefault;
348 if ( *( ++p ) == '\0' ) {
349 state = eParseEntityClass;
354 state = eParseDefault;
357 case eParseEntityClass:
359 state = eParseEntityClassEnd;
363 buffer.push_back( c );
366 case eParseEntityClassEnd:
368 e = Eclass_InitFromText( buffer.c_str() );
369 state = eParseDefault;
371 collector.insert( e );
374 globalErrorStream() << "Error parsing: " << debugname << " in " << filename << "\n";
378 state = eParseDefault;
382 buffer.push_back( '*' );
383 buffer.push_back( c );
384 state = eParseEntityClass;