Debian Codegraph @

include/imapformat.h

raw
1 #pragma once
2
3 #include "imodule.h"
4
5 namespace scene
6 {
7 class NodeVisitor;
8 class INode;
9 typedef std::shared_ptr<INode> INodePtr;
10 class IMapRootNode;
11 typedef std::shared_ptr<IMapRootNode> IMapRootNodePtr;
12 }
13
14 namespace parser { class DefTokeniser; }
15
16 class IPatchNode;
17 typedef std::shared_ptr<IPatchNode> IPatchNodePtr;
18 class IBrushNode;
19 typedef std::shared_ptr<IBrushNode> IBrushNodePtr;
20 class IEntityNode;
21 typedef std::shared_ptr<IEntityNode> IEntityNodePtr;
22
23 /** Callback function to control how the Walker traverses the scene graph. This function
24 * will be provided to the map export module by the Radiant map code.
25 */
26 typedef std::function<void(const scene::INodePtr& root, scene::NodeVisitor& nodeExporter)> GraphTraversalFunc;
27
28 namespace map
29 {
30
31 /**
32 * A Primitive parser is able to create a primitive (brush, patch) from a given token stream.
33 * The initial token, e.g. "brushDef3" is already parsed when the stream is passed to the
34 * parse method.
35 *
36 * Such a class should not change its "state" during the parse() calls - the map parser
37 * is calling the same instance of this PrimitiveParser over and over, one call for each
38 * primitive, so when returning from the parse() method the class should be ready
39 * to process the next primitive.
40 */
41 class PrimitiveParser
42 {
43 public:
44 virtual ~PrimitiveParser() {}
45
46 /**
47 * Returns the primitive keyword of this parser, e.g. "brushDef3". When the Map parser
48 * encounters this keyword, the stream is passed along to the parse() method to create
49 * a scene node from it.
50 */
51 virtual const std::string& getKeyword() const = 0;
52
53 /**
54 * Creates and returns a primitive node according to the encountered token.
55 */
56 virtual scene::INodePtr parse(parser::DefTokeniser& tok) const = 0;
57 };
58 typedef std::shared_ptr<PrimitiveParser> PrimitiveParserPtr;
59
60 /**
61 * An abstract map writer class used to write any map elements
62 * as string to the given output stream.
63 *
64 * The IMapWriter interface defines beginWrite/endWrite pairs for
65 * each scene element (Entity, primitives and the Map itself).
66 * These are called by the map saving algorithm when traversing
67 * the scene depth-first. The usual call order will look like this:
68 *
69 * beginWriteMap
70 * beginWriteEntity
71 * beginWriteBrush
72 * endWriteBrush
73 * beginWritePatch
74 * endWritePatch
75 * ....
76 * endWriteEntity
77 * ...
78 * endWriteMap
79 *
80 * Failure Handling: when the IMapWriter implementation encounters
81 * errors during write (e.g. a visited node is not exportable) a
82 * IMapWriter::FailureException will be thrown. The calling code
83 * is designed to catch this exception.
84 */
85 class IMapWriter
86 {
87 public:
88 // The generic exception type which is thrown by the IMapWriter methods
89 class FailureException :
90 public std::runtime_error
91 {
92 public:
93 FailureException(const std::string& what) :
94 std::runtime_error(what)
95 {}
96 };
97
98 // Destructor
99 virtual ~IMapWriter() {}
100
101 /**
102 * This is called before writing any nodes, to give an opportunity
103 * to write a map header and version info.
104 */
105 virtual void beginWriteMap(const scene::IMapRootNodePtr& root, std::ostream& stream) = 0;
106
107 /**
108 * Called after all nodes have been visited. Note that this method
109 * should NOT attempt to close the given stream.
110 */
111 virtual void endWriteMap(const scene::IMapRootNodePtr& root, std::ostream& stream) = 0;
112
113 // Entity export methods
114 virtual void beginWriteEntity(const IEntityNodePtr& entity, std::ostream& stream) = 0;
115 virtual void endWriteEntity(const IEntityNodePtr& entity, std::ostream& stream) = 0;
116
117 // Brush export methods
118 virtual void beginWriteBrush(const IBrushNodePtr& brush, std::ostream& stream) = 0;
119 virtual void endWriteBrush(const IBrushNodePtr& brush, std::ostream& stream) = 0;
120
121 // Patch export methods
122 virtual void beginWritePatch(const IPatchNodePtr& patch, std::ostream& stream) = 0;
123 virtual void endWritePatch(const IPatchNodePtr& patch, std::ostream& stream) = 0;
124 };
125 typedef std::shared_ptr<IMapWriter> IMapWriterPtr;
126
127 /**
128 * An abstract map reader class used to parse map elements
129 * from the given input (string) stream. The map reader instance
130 * is usually associated with an MapImportFilter class where the
131 * parsed elements are sent to.
132 */
133 class IMapReader
134 {
135 public:
136 // The generic exception type which is thrown by a map reader
137 class FailureException :
138 public std::runtime_error
139 {
140 public:
141 FailureException(const std::string& what) :
142 std::runtime_error(what)
143 {}
144 };
145
146 /**
147 * Read the contents of the given stream and send them through the given MapImportFilter.
148 * Whether the nodes are actually added to the map or not is something the
149 * ImportFilter can decide.
150 *
151 * throws: FailureException on any error.
152 */
153 virtual void readFromStream(std::istream& stream) = 0;
154 };
155
156 class IMapImportFilter
157 {
158 public:
159 /**
160 * Returns a reference to the root node.
161 */
162 virtual const scene::IMapRootNodePtr& getRootNode() const = 0;
163
164 /**
165 * Send an entity node to the import filter. In idTech4 maps all entities
166 * are immediate children of the root node in the scene, so this is where
167 * they usually end up after being added (unless they're filtered out).
168 *
169 * @returns: true if the entity got added, false otherwise.
170 */
171 virtual bool addEntity(const scene::INodePtr& entity) = 0;
172
173 /**
174 * Add an primitive node to the given entity.
175 *
176 * @returns: true if the primitive got added, false otherwise.
177 */
178 virtual bool addPrimitiveToEntity(const scene::INodePtr& primitive, const scene::INodePtr& entity) = 0;
179 };
180 typedef std::shared_ptr<IMapReader> IMapReaderPtr;
181
182 /**
183 * Map Format interface. Each map format is able to traverse the scene graph and write
184 * the contents into a mapfile, or to load a mapfile and populate a scene graph.
185 */
186 class MapFormat :
187 public RegisterableModule
188 {
189 public:
190 virtual ~MapFormat() {}
191
192 /**
193 * Get the display name of this map format, e.g. "Doom 3", "Quake 4", etc.
194 */
195 virtual const std::string& getMapFormatName() const = 0;
196
197 /**
198 * Each MapFormat can have a certain game type it is designed for,
199 * a value which conincides with the type attribute in the game tag
200 * found in the .game file, e.g. "doom3" or "quake4".
201 */
202 virtual const std::string& getGameType() const = 0;
203
204 /**
205 * Instantiate a new map reader, using the given ImportFilter
206 * which will be fed with nodes during the import.
207 */
208 virtual IMapReaderPtr getMapReader(IMapImportFilter& filter) const = 0;
209
210 /**
211 * Acquire a map writer instance, for exporting nodes to a stream.
212 */
213 virtual IMapWriterPtr getMapWriter() const = 0;
214
215 /**
216 * Returns true if this map format allows the .darkradiant file
217 * to be saved along the actual .map file. Some exporter modules
218 * might want to disable that (i.e. the prefab exporter)
219 */
220 virtual bool allowInfoFileCreation() const = 0;
221
222 /**
223 * greebo: Returns true if this map format is able to load
224 * the contents of this file. Usually this includes a version
225 * check of the file header.
226 */
227 virtual bool canLoad(std::istream& stream) const = 0;
228 };
229 typedef std::shared_ptr<MapFormat> MapFormatPtr;
230
231 /**
232 * greebo: This is the global map format manager. Use this class to
233 * register any parsers.
234 */
235 class IMapFormatManager :
236 public RegisterableModule
237 {
238 public:
239 /**
240 * Each MapFormat module should register itself here on startup.
241 */
242 virtual void registerMapFormat(const std::string& extension, const MapFormatPtr& mapFormat) = 0;
243
244 /**
245 * Proper MapFormat modules should unregister themselves at shutdown. This includes
246 * removal from all mapped extensions if the format was registered multiple times.
247 */
248 virtual void unregisterMapFormat(const MapFormatPtr& mapFormat) = 0;
249
250 /**
251 * Tries to look up the default map format for the given map format name.
252 */
253 virtual MapFormatPtr getMapFormatByName(const std::string& mapFormatName) = 0;
254
255 /**
256 * Tries to look up the default map format for the given game type (e.g. "doom3")
257 * associated with the given file extension.
258 */
259 virtual MapFormatPtr getMapFormatForGameType(const std::string& gameType,
260 const std::string& extension) = 0;
261
262 /**
263 * Returns the list of all registered map formats.
264 */
265 virtual std::set<MapFormatPtr> getAllMapFormats() = 0;
266
267 /**
268 * Returns the list of registered map formats matching the given extension.
269 */
270 virtual std::set<MapFormatPtr> getMapFormatList(const std::string& extension) = 0;
271
272 /**
273 * A matching map format is returned, based on the given filename's extension
274 * and the currently active game type.
275 * An empty pointer is returned if no format can be found.
276 */
277 virtual MapFormatPtr getMapFormatForFilename(const std::string& filename) = 0;
278 };
279 typedef std::shared_ptr<IMapFormatManager> IMapFormatManagerPtr;
280
281 // Portable Map Format Name is used across module boundaries
282 const char* const PORTABLE_MAP_FORMAT_NAME("Portable");
283
284 } // namespace map
285
286 const char* const MODULE_MAPFORMATMANAGER("MapFormatManager");
287
288 // Application-wide Accessor to the global map format manager
289 inline map::IMapFormatManager& GlobalMapFormatManager()
290 {
291 static module::InstanceReference<map::IMapFormatManager> _reference(MODULE_MAPFORMATMANAGER);
292 return _reference;
293 }