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"
33 #include <util/buffer.h>
35 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" );
55 m_eclassdef.scanFile = &Eclass_ScanFile;
56 m_eclassdef.getExtension = &EClass_GetExtension;
58 EntityClassScanner* getTable(){
63 typedef SingletonModule<EclassDefAPI, EntityClassDefDependencies> EclassDefModule;
64 typedef Static<EclassDefModule> StaticEclassDefModule;
65 StaticRegisterModule staticRegisterEclassDef( StaticEclassDefModule::instance() );
68 #include "string/string.h"
73 u::BufferVal<1024> com_token;
80 Parse a token out of a string
83 const char *COM_Parse( const char *data ){
96 while ( ( c = *data ) <= ' ' )
100 return 0; // end of file;
106 if ( c == '/' && data[1] == '/' ) {
107 while ( *data && *data != '\n' )
113 // handle quoted strings specially
128 // parse single characters
129 if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) {
136 // parse a regular word
143 if ( c == '{' || c == '}' || c == ')' || c == '(' || c == '\'' || c == ':' ) {
152 const char* Get_COM_Token(){
157 const char *debugname;
159 void setSpecialLoad( EntityClass *e, const char* pWhat, CopiedString& p ){
160 // Hydra: removed some amazingly bad cstring usage, whoever wrote that
161 // needs to be taken out and shot.
163 const char *pText = 0;
164 const char *where = 0;
166 where = strstr( e->comments(),pWhat );
171 pText = where + strlen( pWhat );
172 if ( *pText == '\"' ) {
176 where = strchr( pText,'\"' );
178 p = StringRange( pText, where );
186 #include "eclasslib.h"
190 the classname, color triple, and bounding box are parsed out of comments
191 A ? size means take the exact brush size.
193 / *QUAKED <classname> (0 0 0) ?
194 / *QUAKED <classname> (0 0 0) (-8 -8 -8) (8 8 8)
196 Flag names can follow the size description:
198 / *QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY
202 EntityClass *Eclass_InitFromText( const char *text ){
203 EntityClass* e = Eclass_Alloc();
204 e->free = &Eclass_Free;
207 text = COM_Parse( text );
208 e->m_name = Get_COM_Token();
209 debugname = e->name();
212 // grab the color, reformat as texture name
213 int r = sscanf( text," (%f %f %f)", &e->color[0], &e->color[1], &e->color[2] );
217 eclass_capture_state( e );
220 while ( *text != ')' )
230 text = COM_Parse( text );
231 if ( Get_COM_Token()[0] == '(' ) { // parse the size as two vectors
233 int r = sscanf( text,"%f %f %f) (%f %f %f)", &e->mins[0], &e->mins[1], &e->mins[2],
234 &e->maxs[0], &e->maxs[1], &e->maxs[2] );
239 for ( int i = 0 ; i < 2 ; i++ )
241 while ( *text != ')' )
252 auto parms = u::buffer<256>();
255 // copy to the first /n
257 while ( *text && *text != '\n' )
264 // any remaining words are parm flags
265 const char* p = parms;
266 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();
283 if ( !e->fixedsize ) {
284 EntityClass_insertAttribute( *e, "angle", EntityClassAttribute( "direction", "Direction", "0" ) );
288 EntityClass_insertAttribute( *e, "angle", EntityClassAttribute( "angle", "Yaw Angle", "0" ) );
290 EntityClass_insertAttribute( *e, "model", EntityClassAttribute( "model", "Model" ) );
291 EntityClass_insertAttribute( *e, "noise", EntityClassAttribute( "sound", "Sound" ) );
296 void Eclass_ScanFile( EntityClassCollector& collector, const char *filename ){
299 TextFileInputStream inputFile( filename );
300 if ( inputFile.failed() ) {
301 globalErrorStream() << "ScanFile: " << filename << " not found\n";
304 globalOutputStream() << "ScanFile: " << filename << "\n";
313 eParseEntityClassEnd,
314 } state = eParseDefault;
315 const char* quakeEd = "QUAKED";
318 SingleCharacterInputStream<TextFileInputStream> bufferedInput( inputFile );
322 if ( !bufferedInput.readChar( c ) ) {
330 state = eParseSolidus;
335 state = eParseComment;
337 else if ( c == '*' ) {
339 state = eParseQuakeED;
344 state = eParseDefault;
349 if ( *( ++p ) == '\0' ) {
350 state = eParseEntityClass;
355 state = eParseDefault;
358 case eParseEntityClass:
360 state = eParseEntityClassEnd;
364 buffer.push_back( c );
367 case eParseEntityClassEnd:
369 e = Eclass_InitFromText( buffer.c_str() );
370 state = eParseDefault;
372 collector.insert( e );
375 globalErrorStream() << "Error parsing: " << debugname << " in " << filename << "\n";
379 state = eParseDefault;
383 buffer.push_back( '*' );
384 buffer.push_back( c );
385 state = eParseEntityClass;