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")
60 BrushCreator& getBrushDoom3()
62 return GlobalBrushModule::getTable();
64 PatchCreator& getPatchDoom3()
66 return *m_patchDoom3Module.getTable();
68 PatchCreator& getPatchDef2Doom3()
70 return *m_patchDef2Doom3Module.getTable();
74 class MapDoom3API : public TypeSystemRef, public MapFormat, public PrimitiveParser
76 MapDoom3Dependencies& m_dependencies;
78 typedef MapFormat Type;
79 STRING_CONSTANT(Name, "mapdoom3");
80 INTEGER_CONSTANT(MapVersion, 2);
82 MapDoom3API(MapDoom3Dependencies& dependencies) : m_dependencies(dependencies)
84 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("doom3 maps", "*.map"));
85 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("doom3 region", "*.reg"));
92 scene::Node& parsePrimitive(Tokeniser& tokeniser) const
94 const char* primitive = tokeniser.getToken();
97 if(string_equal(primitive, "patchDef3"))
99 return m_dependencies.getPatchDoom3().createPatch();
101 else if(string_equal(primitive, "patchDef2"))
103 return m_dependencies.getPatchDef2Doom3().createPatch();
105 else if(string_equal(primitive, "brushDef3"))
107 return m_dependencies.getBrushDoom3().createBrush();
111 Tokeniser_unexpectedError(tokeniser, primitive, "#doom3-primitive");
114 void readGraph(scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable) const
116 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser(inputStream);
117 tokeniser.nextLine();
118 if(!Tokeniser_parseToken(tokeniser, "Version"))
123 if(!Tokeniser_getSize(tokeniser, version))
127 if(version != MapVersion())
129 globalErrorStream() << "Doom 3 map version " << MapVersion() << " supported, version is " << Unsigned(version) << "\n";
132 tokeniser.nextLine();
133 Map_Read(root, tokeniser, entityTable, *this);
136 void writeGraph(scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream) const
138 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter(outputStream);
139 writer.writeToken("Version");
140 writer.writeInteger(MapVersion());
142 Map_Write(root, traverse, writer, false);
147 typedef SingletonModule<
149 MapDoom3Dependencies,
150 DependenciesAPIConstructor<MapDoom3API, MapDoom3Dependencies>
154 MapDoom3Module g_MapDoom3Module;
157 class MapQuake4API : public TypeSystemRef, public MapFormat, public PrimitiveParser
159 MapDoom3Dependencies& m_dependencies;
161 typedef MapFormat Type;
162 STRING_CONSTANT(Name, "mapquake4");
163 INTEGER_CONSTANT(MapVersion, 3);
165 MapQuake4API(MapDoom3Dependencies& dependencies) : m_dependencies(dependencies)
167 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake4 maps", "*.map"));
168 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake4 region", "*.reg"));
170 MapFormat* getTable()
175 scene::Node& parsePrimitive(Tokeniser& tokeniser) const
177 const char* primitive = tokeniser.getToken();
180 if(string_equal(primitive, "patchDef3"))
182 return m_dependencies.getPatchDoom3().createPatch();
184 else if(string_equal(primitive, "patchDef2"))
186 return m_dependencies.getPatchDef2Doom3().createPatch();
188 else if(string_equal(primitive, "brushDef3"))
190 return m_dependencies.getBrushDoom3().createBrush();
194 Tokeniser_unexpectedError(tokeniser, primitive, "#quake4-primitive");
197 void readGraph(scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable) const
199 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser(inputStream);
200 tokeniser.nextLine();
201 if(!Tokeniser_parseToken(tokeniser, "Version"))
206 if(!Tokeniser_getSize(tokeniser, version))
210 if(version != MapVersion())
212 globalErrorStream() << "Quake 4 map version " << MapVersion() << " supported, version is " << Unsigned(version) << "\n";
215 tokeniser.nextLine();
216 Map_Read(root, tokeniser, entityTable, *this);
219 void writeGraph(scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream) const
221 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter(outputStream);
222 writer.writeToken("Version");
223 writer.writeInteger(MapVersion());
225 Map_Write(root, traverse, writer, false);
230 typedef SingletonModule<
232 MapDoom3Dependencies,
233 DependenciesAPIConstructor<MapQuake4API, MapDoom3Dependencies>
237 MapQuake4Module g_MapQuake4Module;
240 class MapDependencies :
241 public GlobalRadiantModuleRef,
242 public GlobalBrushModuleRef,
243 public GlobalPatchModuleRef,
244 public GlobalFiletypesModuleRef,
245 public GlobalScripLibModuleRef,
246 public GlobalEntityClassManagerModuleRef,
247 public GlobalSceneGraphModuleRef
251 GlobalBrushModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("brushtypes")),
252 GlobalPatchModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("patchtypes")),
253 GlobalEntityClassManagerModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entityclass"))
258 class MapQ3API : public TypeSystemRef, public MapFormat, public PrimitiveParser
260 mutable bool detectedFormat;
262 typedef MapFormat Type;
263 STRING_CONSTANT(Name, "mapq3");
267 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake3 maps", "*.map", true, true, true));
268 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake3 region", "*.reg", true, true, true));
269 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake3 compiled maps", "*.bsp", false, true, false));
271 MapFormat* getTable()
276 scene::Node& parsePrimitive(Tokeniser& tokeniser) const
278 const char* primitive = tokeniser.getToken();
281 if(string_equal(primitive, "patchDef2"))
283 return GlobalPatchModule::getTable().createPatch();
285 if(GlobalBrushModule::getTable().useAlternativeTextureProjection())
287 if(string_equal(primitive, "brushDef"))
289 detectedFormat = true;
290 return GlobalBrushModule::getTable().createBrush();
292 else if(!detectedFormat && string_equal(primitive, "("))
294 detectedFormat = true;
296 Tokeniser_unexpectedError(tokeniser, primitive, "#quake3-switch-to-texdef");
302 if(string_equal(primitive, "("))
304 detectedFormat = true;
305 tokeniser.ungetToken(); // (
306 return GlobalBrushModule::getTable().createBrush();
308 else if(!detectedFormat && string_equal(primitive, "brushDef"))
310 detectedFormat = true;
312 Tokeniser_unexpectedError(tokeniser, primitive, "#quake3-switch-to-brush-primitives");
318 Tokeniser_unexpectedError(tokeniser, primitive, "#quake3-primitive");
322 void readGraph(scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable) const
324 detectedFormat = false;
326 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser(inputStream);
327 Map_Read(root, tokeniser, entityTable, *this);
330 void writeGraph(scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream) const
332 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter(outputStream);
333 Map_Write(root, traverse, writer, false);
338 typedef SingletonModule<MapQ3API, MapDependencies> MapQ3Module;
340 MapQ3Module g_MapQ3Module;
343 class MapQ1API : public TypeSystemRef, public MapFormat, public PrimitiveParser
346 typedef MapFormat Type;
347 STRING_CONSTANT(Name, "mapq1");
351 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake maps", "*.map"));
352 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake region", "*.reg"));
354 MapFormat* getTable()
359 scene::Node& parsePrimitive(Tokeniser& tokeniser) const
361 const char* primitive = tokeniser.getToken();
364 if(string_equal(primitive, "("))
366 tokeniser.ungetToken(); // (
367 return GlobalBrushModule::getTable().createBrush();
371 Tokeniser_unexpectedError(tokeniser, primitive, "#quake-primitive");
374 void readGraph(scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable) const
376 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser(inputStream);
377 Map_Read(root, tokeniser, entityTable, *this);
380 void writeGraph(scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream) const
382 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter(outputStream);
383 Map_Write(root, traverse, writer, true);
388 typedef SingletonModule<MapQ1API, MapDependencies> MapQ1Module;
390 MapQ1Module g_MapQ1Module;
393 class MapHalfLifeAPI : public TypeSystemRef, public MapFormat, public PrimitiveParser
396 typedef MapFormat Type;
397 STRING_CONSTANT(Name, "maphl");
401 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("half-life maps", "*.map"));
402 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("half-life region", "*.reg"));
404 MapFormat* getTable()
409 scene::Node& parsePrimitive(Tokeniser& tokeniser) const
411 const char* primitive = tokeniser.getToken();
414 if(string_equal(primitive, "("))
416 tokeniser.ungetToken(); // (
417 return GlobalBrushModule::getTable().createBrush();
421 Tokeniser_unexpectedError(tokeniser, primitive, "#halflife-primitive");
424 void readGraph(scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable) const
426 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser(inputStream);
427 Map_Read(root, tokeniser, entityTable, *this);
430 void writeGraph(scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream) const
432 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter(outputStream);
433 Map_Write(root, traverse, writer, true);
438 typedef SingletonModule<MapHalfLifeAPI, MapDependencies> MapHalfLifeModule;
440 MapHalfLifeModule g_MapHalfLifeModule;
443 class MapQ2API : public TypeSystemRef, public MapFormat, public PrimitiveParser
446 typedef MapFormat Type;
447 STRING_CONSTANT(Name, "mapq2");
451 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake2 maps", "*.map"));
452 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("quake2 region", "*.reg"));
454 MapFormat* getTable()
458 scene::Node& parsePrimitive(Tokeniser& tokeniser) const
460 const char* primitive = tokeniser.getToken();
463 if(string_equal(primitive, "("))
465 tokeniser.ungetToken(); // (
466 return GlobalBrushModule::getTable().createBrush();
470 Tokeniser_unexpectedError(tokeniser, primitive, "#quake2-primitive");
473 void readGraph(scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable) const
475 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser(inputStream);
476 Map_Read(root, tokeniser, entityTable, *this);
479 void writeGraph(scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream) const
481 TokenWriter& writer = GlobalScripLibModule::getTable().m_pfnNewSimpleTokenWriter(outputStream);
482 Map_Write(root, traverse, writer, true);
487 typedef SingletonModule<MapQ2API, MapDependencies> MapQ2Module;
489 MapQ2Module g_MapQ2Module;
493 #define PARSE_ERROR "error parsing VMF"
495 inline void parseToken(Tokeniser& tokeniser, const char* token)
497 ASSERT_MESSAGE(Tokeniser_parseToken(tokeniser, token), "error parsing vmf: token not found: " << makeQuoted(token));
500 #include "generic/arrayrange.h"
503 typedef ArrayConstRange<VMFBlock> VMFBlockArrayRange;
510 VMFBlockArrayRange m_children;
511 typedef const VMFBlock Value;
513 VMFBlock(const char* name, VMFBlockArrayRange children = VMFBlockArrayRange(0, 0)) : m_name(name), m_children(children)
516 const char* name() const
520 typedef Value* const_iterator;
521 const_iterator begin() const
523 return m_children.first;
525 const_iterator end() const
527 return m_children.last;
531 const VMFBlock c_vmfNormals("normals");
532 const VMFBlock c_vmfDistances("distances");
533 const VMFBlock c_vmfOffsets("offsets");
534 const VMFBlock c_vmfOffsetNormals("offset_normals");
535 const VMFBlock c_vmfAlphas("alphas");
536 const VMFBlock c_vmfTriangleTags("triangle_tags");
537 const VMFBlock c_vmfAllowedVerts("allowed_verts");
538 const VMFBlock c_vmfDispInfoChildren[] = { c_vmfNormals, c_vmfDistances, c_vmfOffsets, c_vmfOffsetNormals, c_vmfAlphas, c_vmfTriangleTags, c_vmfAllowedVerts };
539 const VMFBlock c_vmfDispInfo("dispinfo", ARRAY_RANGE(c_vmfDispInfoChildren));
540 const VMFBlock c_vmfSideChildren[] = { c_vmfDispInfo };
541 const VMFBlock c_vmfSide("side", ARRAY_RANGE(c_vmfSideChildren));
542 const VMFBlock c_vmfEditor("editor");
543 const VMFBlock c_vmfVersionInfo("versioninfo");
544 const VMFBlock c_vmfViewSettings("viewsettings");
545 const VMFBlock c_vmfCordon("cordon");
546 const VMFBlock c_vmfGroupChildren[] = { c_vmfEditor };
547 const VMFBlock c_vmfGroup("group", ARRAY_RANGE(c_vmfGroupChildren));
548 const VMFBlock c_vmfCamera("camera");
549 const VMFBlock c_vmfCamerasChildren[] = { c_vmfCamera };
550 const VMFBlock c_vmfCameras("cameras", ARRAY_RANGE(c_vmfCamerasChildren));
551 VMFBlock c_vmfVisGroup("visgroup");
552 VMFBlock c_vmfVisGroups("visgroups", VMFBlockArrayRange(&c_vmfVisGroup, &c_vmfVisGroup+1));
553 const VMFBlock c_vmfSolidChildren[] = { c_vmfSide, c_vmfEditor };
554 const VMFBlock c_vmfSolid("solid", ARRAY_RANGE(c_vmfSolidChildren));
555 const VMFBlock c_vmfConnections("connections");
556 const VMFBlock c_vmfEntityChildren[] = { c_vmfEditor, c_vmfSolid, c_vmfGroup, c_vmfConnections };
557 const VMFBlock c_vmfEntity("entity", ARRAY_RANGE(c_vmfEntityChildren));
558 const VMFBlock c_vmfWorldChildren[] = { c_vmfEditor, c_vmfSolid, c_vmfGroup };
559 const VMFBlock c_vmfWorld("world", ARRAY_RANGE(c_vmfWorldChildren));
560 const VMFBlock c_vmfRootChildren[] = { c_vmfVersionInfo, c_vmfViewSettings, c_vmfVisGroups, c_vmfWorld, c_vmfEntity, c_vmfCameras, c_vmfCordon };
561 const VMFBlock c_vmfRoot("", ARRAY_RANGE(c_vmfRootChildren));
568 c_vmfVisGroup.m_children = VMFBlockArrayRange(&c_vmfVisGroup, &c_vmfVisGroup+1);
577 inline VMFBlock::const_iterator VMFBlock_find(const VMFBlock& block, const char* name)
579 for(VMFBlock::const_iterator i = block.begin(); i != block.end(); ++i)
581 if(string_equal(name, (*i).name()))
589 void VMF_parseBlock(Tokeniser& tokeniser, const VMFBlock& block)
593 const char* key = tokeniser.getToken();
594 if(key == 0 || string_equal(key, "}"))
596 tokeniser.ungetToken();
599 CopiedString tmp(key);
600 tokeniser.nextLine();
601 const char* value = tokeniser.getToken();
602 tokeniser.nextLine();
603 if(string_equal(value, "{"))
605 VMFBlock::const_iterator i = VMFBlock_find(block, tmp.c_str());
606 ASSERT_MESSAGE(i != block.end(), "error parsing vmf block " << makeQuoted(block.name()) << ": unknown block: " << makeQuoted(tmp.c_str()));
607 if(string_equal(tmp.c_str(), "solid"))
611 else if(string_equal(tmp.c_str(), "entity") || string_equal(tmp.c_str(), "world"))
615 VMF_parseBlock(tokeniser, *i);
616 parseToken(tokeniser, "}");
617 tokeniser.nextLine();
626 void VMF_Read(scene::Node& root, Tokeniser& tokeniser, EntityCreator& entityTable)
628 g_vmf_entities = g_vmf_brushes = 0;
629 VMF_parseBlock(tokeniser, c_vmfRoot);
630 globalOutputStream() << g_vmf_entities << " entities\n";
631 globalOutputStream() << g_vmf_brushes << " brushes\n";
634 class MapVMFAPI : public TypeSystemRef, public MapFormat
637 typedef MapFormat Type;
638 STRING_CONSTANT(Name, "mapvmf");
642 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("vmf maps", "*.vmf"));
643 GlobalFiletypesModule::getTable().addType(Type::Name(), Name(), filetype_t("vmf region", "*.reg"));
645 MapFormat* getTable()
650 void readGraph(scene::Node& root, TextInputStream& inputStream, EntityCreator& entityTable) const
652 Tokeniser& tokeniser = GlobalScripLibModule::getTable().m_pfnNewSimpleTokeniser(inputStream);
653 VMF_Read(root, tokeniser, entityTable);
656 void writeGraph(scene::Node& root, GraphTraversalFunc traverse, TextOutputStream& outputStream) const
661 typedef SingletonModule<MapVMFAPI, MapDependencies> MapVMFModule;
663 MapVMFModule g_MapVMFModule;
667 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer& server)
669 initialiseModule(server);
671 g_MapDoom3Module.selfRegister();
672 g_MapQuake4Module.selfRegister();
673 g_MapQ3Module.selfRegister();
674 g_MapQ1Module.selfRegister();
675 g_MapQ2Module.selfRegister();
676 g_MapHalfLifeModule.selfRegister();
677 g_MapVMFModule.selfRegister();