2 Copyright (C) 2001-2006, William Joseph.
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
24 #include "iscriplib.h"
27 #include "ifiletypes.h"
29 #include "qerplugin.h"
32 #include "string/string.h"
34 #include "generic/constant.h"
36 #include "modulesystem/singletonmodule.h"
42 class MapDoom3Dependencies :
43 public GlobalRadiantModuleRef,
44 public GlobalFiletypesModuleRef,
45 public GlobalScripLibModuleRef,
46 public GlobalEntityClassManagerModuleRef,
47 public GlobalSceneGraphModuleRef,
48 public GlobalBrushModuleRef
50 PatchModuleRef m_patchDef2Doom3Module;
51 PatchModuleRef m_patchDoom3Module;
53 MapDoom3Dependencies() :
54 GlobalEntityClassManagerModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entityclass" ) ),
55 GlobalBrushModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "brushtypes" ) ),
56 m_patchDef2Doom3Module( "def2doom3" ),
57 m_patchDoom3Module( "doom3" ){
59 BrushCreator& getBrushDoom3(){
60 return GlobalBrushModule::getTable();
62 PatchCreator& getPatchDoom3(){
63 return *m_patchDoom3Module.getTable();
65 PatchCreator& getPatchDef2Doom3(){
66 return *m_patchDef2Doom3Module.getTable();
70 class MapDoom3API : public TypeSystemRef, public MapFormat, public PrimitiveParser
72 MapDoom3Dependencies& m_dependencies;
74 typedef MapFormat Type;
75 STRING_CONSTANT( Name, "mapdoom3" );
76 INTEGER_CONSTANT( MapVersion, 2 );
78 MapDoom3API( MapDoom3Dependencies& dependencies ) : m_dependencies( dependencies ){
79 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "doom3 maps", "*.map" ) );
80 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "doom3 region", "*.reg" ) );
82 MapFormat* getTable(){
86 scene::Node& parsePrimitive( Tokeniser& tokeniser ) const {
87 const char* primitive = tokeniser.getToken();
88 if ( primitive != 0 ) {
89 if ( string_equal( primitive, "patchDef3" ) ) {
90 return m_dependencies.getPatchDoom3().createPatch();
92 else if ( string_equal( primitive, "patchDef2" ) ) {
93 return m_dependencies.getPatchDef2Doom3().createPatch();
95 else if ( string_equal( primitive, "brushDef3" ) ) {
96 return m_dependencies.getBrushDoom3().createBrush();
100 Tokeniser_unexpectedError( tokeniser, primitive, "#doom3-primitive" );
103 void readGraph( scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable ) const {
104 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser( inputStream );
105 tokeniser.nextLine();
106 if ( !Tokeniser_parseToken( tokeniser, "Version" ) ) {
110 if ( !Tokeniser_getSize( tokeniser, version ) ) {
113 if ( version != std::size_t(MapVersion()) ) {
114 globalErrorStream() << "Doom 3 map version " << MapVersion() << " supported, version is " << Unsigned( version ) << "\n";
117 tokeniser.nextLine();
118 Map_Read( root, tokeniser, entityTable, *this );
121 void writeGraph( scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream ) const {
122 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter( outputStream );
123 writer.writeToken( "Version" );
124 writer.writeInteger( MapVersion() );
126 Map_Write( root, traverse, writer, false );
131 typedef SingletonModule<
133 MapDoom3Dependencies,
134 DependenciesAPIConstructor<MapDoom3API, MapDoom3Dependencies>
138 MapDoom3Module g_MapDoom3Module;
141 class MapQuake4API : public TypeSystemRef, public MapFormat, public PrimitiveParser
143 MapDoom3Dependencies& m_dependencies;
145 typedef MapFormat Type;
146 STRING_CONSTANT( Name, "mapquake4" );
147 INTEGER_CONSTANT( MapVersion, 3 );
149 MapQuake4API( MapDoom3Dependencies& dependencies ) : m_dependencies( dependencies ){
150 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake4 maps", "*.map" ) );
151 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake4 region", "*.reg" ) );
153 MapFormat* getTable(){
157 scene::Node& parsePrimitive( Tokeniser& tokeniser ) const {
158 const char* primitive = tokeniser.getToken();
159 if ( primitive != 0 ) {
160 if ( string_equal( primitive, "patchDef3" ) ) {
161 return m_dependencies.getPatchDoom3().createPatch();
163 else if ( string_equal( primitive, "patchDef2" ) ) {
164 return m_dependencies.getPatchDef2Doom3().createPatch();
166 else if ( string_equal( primitive, "brushDef3" ) ) {
167 return m_dependencies.getBrushDoom3().createBrush();
171 Tokeniser_unexpectedError( tokeniser, primitive, "#quake4-primitive" );
174 void readGraph( scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable ) const {
175 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser( inputStream );
176 tokeniser.nextLine();
177 if ( !Tokeniser_parseToken( tokeniser, "Version" ) ) {
181 if ( !Tokeniser_getSize( tokeniser, version ) ) {
184 if ( version != std::size_t(MapVersion()) ) {
185 globalErrorStream() << "Quake 4 map version " << MapVersion() << " supported, version is " << Unsigned( version ) << "\n";
188 tokeniser.nextLine();
189 Map_Read( root, tokeniser, entityTable, *this );
192 void writeGraph( scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream ) const {
193 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter( outputStream );
194 writer.writeToken( "Version" );
195 writer.writeInteger( MapVersion() );
197 Map_Write( root, traverse, writer, false );
202 typedef SingletonModule<
204 MapDoom3Dependencies,
205 DependenciesAPIConstructor<MapQuake4API, MapDoom3Dependencies>
209 MapQuake4Module g_MapQuake4Module;
212 class MapDependencies :
213 public GlobalRadiantModuleRef,
214 public GlobalBrushModuleRef,
215 public GlobalPatchModuleRef,
216 public GlobalFiletypesModuleRef,
217 public GlobalScripLibModuleRef,
218 public GlobalEntityClassManagerModuleRef,
219 public GlobalSceneGraphModuleRef
223 GlobalBrushModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "brushtypes" ) ),
224 GlobalPatchModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "patchtypes" ) ),
225 GlobalEntityClassManagerModuleRef( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entityclass" ) ){
229 class MapQ3API : public TypeSystemRef, public MapFormat, public PrimitiveParser
231 mutable bool detectedFormat;
233 typedef MapFormat Type;
234 STRING_CONSTANT( Name, "mapq3" );
237 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake3 maps", "*.map", true, true, true ) );
238 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake3 region", "*.reg", true, true, true ) );
239 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake3 compiled maps", "*.bsp", false, true, false ) );
241 MapFormat* getTable(){
245 scene::Node& parsePrimitive( Tokeniser& tokeniser ) const {
246 const char* primitive = tokeniser.getToken();
247 if ( primitive != 0 ) {
248 if ( string_equal( primitive, "patchDef2" ) ) {
249 return GlobalPatchModule::getTable().createPatch();
251 if ( GlobalBrushModule::getTable().useAlternativeTextureProjection() ) {
252 if ( string_equal( primitive, "brushDef" ) ) {
253 detectedFormat = true;
254 return GlobalBrushModule::getTable().createBrush();
256 else if ( !detectedFormat && string_equal( primitive, "(" ) ) {
257 detectedFormat = true;
259 Tokeniser_unexpectedError( tokeniser, primitive, "#quake3-switch-to-texdef" );
265 if ( string_equal( primitive, "(" ) ) {
266 detectedFormat = true;
267 tokeniser.ungetToken(); // (
268 return GlobalBrushModule::getTable().createBrush();
270 else if ( !detectedFormat && string_equal( primitive, "brushDef" ) ) {
271 detectedFormat = true;
273 Tokeniser_unexpectedError( tokeniser, primitive, "#quake3-switch-to-brush-primitives" );
279 Tokeniser_unexpectedError( tokeniser, primitive, "#quake3-primitive" );
283 void readGraph( scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable ) const {
284 detectedFormat = false;
286 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser( inputStream );
287 Map_Read( root, tokeniser, entityTable, *this );
290 void writeGraph( scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream ) const {
291 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter( outputStream );
292 Map_Write( root, traverse, writer, false );
297 typedef SingletonModule<MapQ3API, MapDependencies> MapQ3Module;
299 MapQ3Module g_MapQ3Module;
302 class MapQ1API : public TypeSystemRef, public MapFormat, public PrimitiveParser
305 typedef MapFormat Type;
306 STRING_CONSTANT( Name, "mapq1" );
309 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake maps", "*.map" ) );
310 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake region", "*.reg" ) );
312 MapFormat* getTable(){
316 scene::Node& parsePrimitive( Tokeniser& tokeniser ) const {
317 const char* primitive = tokeniser.getToken();
318 if ( primitive != 0 ) {
319 if ( string_equal( primitive, "(" ) ) {
320 tokeniser.ungetToken(); // (
321 return GlobalBrushModule::getTable().createBrush();
325 Tokeniser_unexpectedError( tokeniser, primitive, "#quake-primitive" );
328 void readGraph( scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable ) const {
329 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser( inputStream );
330 Map_Read( root, tokeniser, entityTable, *this );
333 void writeGraph( scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream ) const {
334 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter( outputStream );
335 Map_Write( root, traverse, writer, true );
340 typedef SingletonModule<MapQ1API, MapDependencies> MapQ1Module;
342 MapQ1Module g_MapQ1Module;
345 class MapHalfLifeAPI : public TypeSystemRef, public MapFormat, public PrimitiveParser
348 typedef MapFormat Type;
349 STRING_CONSTANT( Name, "maphl" );
352 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "half-life maps", "*.map" ) );
353 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "half-life region", "*.reg" ) );
355 MapFormat* getTable(){
359 scene::Node& parsePrimitive( Tokeniser& tokeniser ) const {
360 const char* primitive = tokeniser.getToken();
361 if ( primitive != 0 ) {
362 if ( string_equal( primitive, "(" ) ) {
363 tokeniser.ungetToken(); // (
364 return GlobalBrushModule::getTable().createBrush();
368 Tokeniser_unexpectedError( tokeniser, primitive, "#halflife-primitive" );
371 void readGraph( scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable ) const {
372 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser( inputStream );
373 Map_Read( root, tokeniser, entityTable, *this );
376 void writeGraph( scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream ) const {
377 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter( outputStream );
378 Map_Write( root, traverse, writer, true );
383 typedef SingletonModule<MapHalfLifeAPI, MapDependencies> MapHalfLifeModule;
385 MapHalfLifeModule g_MapHalfLifeModule;
388 class MapQ2API : public TypeSystemRef, public MapFormat, public PrimitiveParser
391 typedef MapFormat Type;
392 STRING_CONSTANT( Name, "mapq2" );
395 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake2 maps", "*.map" ) );
396 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "quake2 region", "*.reg" ) );
398 MapFormat* getTable(){
401 scene::Node& parsePrimitive( Tokeniser& tokeniser ) const {
402 const char* primitive = tokeniser.getToken();
403 if ( primitive != 0 ) {
404 if ( string_equal( primitive, "(" ) ) {
405 tokeniser.ungetToken(); // (
406 return GlobalBrushModule::getTable().createBrush();
410 Tokeniser_unexpectedError( tokeniser, primitive, "#quake2-primitive" );
413 void readGraph( scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable ) const {
414 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser( inputStream );
415 Map_Read( root, tokeniser, entityTable, *this );
418 void writeGraph( scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream ) const {
419 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter( outputStream );
420 Map_Write( root, traverse, writer, true );
425 typedef SingletonModule<MapQ2API, MapDependencies> MapQ2Module;
427 MapQ2Module g_MapQ2Module;
431 #define PARSE_ERROR "error parsing VMF"
433 inline void parseToken( Tokeniser& tokeniser, const char* token ){
434 ASSERT_MESSAGE( Tokeniser_parseToken( tokeniser, token ), "error parsing vmf: token not found: " << makeQuoted( token ) );
437 #include "generic/arrayrange.h"
440 typedef ArrayConstRange<VMFBlock> VMFBlockArrayRange;
447 VMFBlockArrayRange m_children;
448 typedef const VMFBlock Value;
450 VMFBlock( const char* name, VMFBlockArrayRange children = VMFBlockArrayRange( 0, 0 ) ) : m_name( name ), m_children( children ){
452 const char* name() const {
455 typedef Value* const_iterator;
456 const_iterator begin() const {
457 return m_children.first;
459 const_iterator end() const {
460 return m_children.last;
464 const VMFBlock c_vmfNormals( "normals" );
465 const VMFBlock c_vmfDistances( "distances" );
466 const VMFBlock c_vmfOffsets( "offsets" );
467 const VMFBlock c_vmfOffsetNormals( "offset_normals" );
468 const VMFBlock c_vmfAlphas( "alphas" );
469 const VMFBlock c_vmfTriangleTags( "triangle_tags" );
470 const VMFBlock c_vmfAllowedVerts( "allowed_verts" );
471 const VMFBlock c_vmfDispInfoChildren[] = { c_vmfNormals, c_vmfDistances, c_vmfOffsets, c_vmfOffsetNormals, c_vmfAlphas, c_vmfTriangleTags, c_vmfAllowedVerts };
472 const VMFBlock c_vmfDispInfo( "dispinfo", ARRAY_RANGE( c_vmfDispInfoChildren ) );
473 const VMFBlock c_vmfSideChildren[] = { c_vmfDispInfo };
474 const VMFBlock c_vmfSide( "side", ARRAY_RANGE( c_vmfSideChildren ) );
475 const VMFBlock c_vmfEditor( "editor" );
476 const VMFBlock c_vmfVersionInfo( "versioninfo" );
477 const VMFBlock c_vmfViewSettings( "viewsettings" );
478 const VMFBlock c_vmfCordon( "cordon" );
479 const VMFBlock c_vmfGroupChildren[] = { c_vmfEditor };
480 const VMFBlock c_vmfGroup( "group", ARRAY_RANGE( c_vmfGroupChildren ) );
481 const VMFBlock c_vmfCamera( "camera" );
482 const VMFBlock c_vmfCamerasChildren[] = { c_vmfCamera };
483 const VMFBlock c_vmfCameras( "cameras", ARRAY_RANGE( c_vmfCamerasChildren ) );
484 VMFBlock c_vmfVisGroup( "visgroup" );
485 VMFBlock c_vmfVisGroups( "visgroups", VMFBlockArrayRange( &c_vmfVisGroup, &c_vmfVisGroup + 1 ) );
486 const VMFBlock c_vmfSolidChildren[] = { c_vmfSide, c_vmfEditor };
487 const VMFBlock c_vmfSolid( "solid", ARRAY_RANGE( c_vmfSolidChildren ) );
488 const VMFBlock c_vmfConnections( "connections" );
489 const VMFBlock c_vmfEntityChildren[] = { c_vmfEditor, c_vmfSolid, c_vmfGroup, c_vmfConnections };
490 const VMFBlock c_vmfEntity( "entity", ARRAY_RANGE( c_vmfEntityChildren ) );
491 const VMFBlock c_vmfWorldChildren[] = { c_vmfEditor, c_vmfSolid, c_vmfGroup };
492 const VMFBlock c_vmfWorld( "world", ARRAY_RANGE( c_vmfWorldChildren ) );
493 const VMFBlock c_vmfRootChildren[] = { c_vmfVersionInfo, c_vmfViewSettings, c_vmfVisGroups, c_vmfWorld, c_vmfEntity, c_vmfCameras, c_vmfCordon };
494 const VMFBlock c_vmfRoot( "", ARRAY_RANGE( c_vmfRootChildren ) );
500 c_vmfVisGroup.m_children = VMFBlockArrayRange( &c_vmfVisGroup, &c_vmfVisGroup + 1 );
509 inline VMFBlock::const_iterator VMFBlock_find( const VMFBlock& block, const char* name ){
510 for ( VMFBlock::const_iterator i = block.begin(); i != block.end(); ++i )
512 if ( string_equal( name, ( *i ).name() ) ) {
519 void VMF_parseBlock( Tokeniser& tokeniser, const VMFBlock& block ){
522 const char* key = tokeniser.getToken();
523 if ( key == 0 || string_equal( key, "}" ) ) {
524 tokeniser.ungetToken();
527 std::string tmp( key );
528 tokeniser.nextLine();
529 const char* value = tokeniser.getToken();
530 tokeniser.nextLine();
531 if ( string_equal( value, "{" ) ) {
532 VMFBlock::const_iterator i = VMFBlock_find( block, tmp.c_str() );
533 ASSERT_MESSAGE( i != block.end(), "error parsing vmf block " << makeQuoted( block.name() ) << ": unknown block: " << makeQuoted( tmp.c_str() ) );
534 if ( string_equal( tmp.c_str(), "solid" ) ) {
537 else if ( string_equal( tmp.c_str(), "entity" ) || string_equal( tmp.c_str(), "world" ) ) {
540 VMF_parseBlock( tokeniser, *i );
541 parseToken( tokeniser, "}" );
542 tokeniser.nextLine();
551 void VMF_Read( scene::Node& root, Tokeniser& tokeniser, EntityCreator& entityTable ){
552 g_vmf_entities = g_vmf_brushes = 0;
553 VMF_parseBlock( tokeniser, c_vmfRoot );
554 globalOutputStream() << g_vmf_entities << " entities\n";
555 globalOutputStream() << g_vmf_brushes << " brushes\n";
558 class MapVMFAPI : public TypeSystemRef, public MapFormat
561 typedef MapFormat Type;
562 STRING_CONSTANT( Name, "mapvmf" );
565 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "vmf maps", "*.vmf" ) );
566 GlobalFiletypesModule::getTable().addType( Type::Name(), Name(), filetype_t( "vmf region", "*.reg" ) );
568 MapFormat* getTable(){
572 void readGraph( scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable ) const {
573 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser( inputStream );
574 VMF_Read( root, tokeniser, entityTable );
577 void writeGraph( scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream ) const {
581 typedef SingletonModule<MapVMFAPI, MapDependencies> MapVMFModule;
583 MapVMFModule g_MapVMFModule;
587 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){
588 initialiseModule( server );
590 g_MapDoom3Module.selfRegister();
591 g_MapQuake4Module.selfRegister();
592 g_MapQ3Module.selfRegister();
593 g_MapQ1Module.selfRegister();
594 g_MapQ2Module.selfRegister();
595 g_MapHalfLifeModule.selfRegister();
596 g_MapVMFModule.selfRegister();