Debian Codegraph @

include/imapinfofile.h

raw
1 #pragma once
2
3 #include <string>
4 #include <map>
5 #include "imodule.h"
6 #include "imap.h"
7 #include <functional>
8
9 namespace parser { class DefTokeniser; }
10 namespace scene { class INode; typedef std::shared_ptr<INode> INodePtr; }
11
12 namespace map
13 {
14
15 typedef std::pair<std::size_t, std::size_t> NodeIndexPair;
16 typedef std::map<NodeIndexPair, scene::INodePtr> NodeIndexMap;
17
18 /**
19 * An info file module is allowed to write text-based information
20 * to the auxiliary .darkradiant file that is written alongside to the
21 * game-compatible .map file. Things like layer or selection set/group
22 * information can be stored persistently between mapping sessions this way.
23 *
24 * The module should write its information in named blocks, like
25 *
26 * MyModuleInfo
27 * {
28 * // arbitrary parseable info here
29 * }
30 *
31 * Later, when the info file is parsed after map load, the module will be asked
32 * to parse the blocks it's responsible for, and apply its information to the map.
33 */
34 class IMapInfoFileModule
35 {
36 public:
37 virtual ~IMapInfoFileModule() {}
38
39 // Info File Saving / Exporting
40
41 // The name of this info file module, mainly for internal book-keeping
42 virtual std::string getName() = 0;
43
44 /**
45 * Called before any node is written to the .map file. Use this
46 * to prepare the internal structures for exporting.
47 */
48 virtual void onInfoFileSaveStart() = 0;
49
50 /**
51 * Called before starting the actual node traversal,
52 * the info file exporter gets a chance to look at the map root.
53 */
54 virtual void onBeginSaveMap(const scene::IMapRootNodePtr& root) = 0;
55
56 /**
57 * Called during map export traversal when a single
58 * primitive is about to be written to the .map file.
59 * Assemble information about the primitive and save it
60 * internally, until the writeBlocks() method is called.
61 */
62 virtual void onSavePrimitive(const scene::INodePtr& node,
63 std::size_t entityNum, std::size_t primitiveNum) = 0;
64
65 /**
66 * Called during map export traversal when a single
67 * entity is about to be written to the .map file.
68 * Assemble information about the entity and save it
69 * internally, until the writeBlocks() method is called.
70 */
71 virtual void onSaveEntity(const scene::INodePtr& node, std::size_t entityNum) = 0;
72
73 /**
74 * Called after node traversal.
75 */
76 virtual void onFinishSaveMap(const scene::IMapRootNodePtr& root) = 0;
77
78 /**
79 * Final export function, write the assembled data to the
80 * info file stream. This method should include the block file name
81 * and the opening and closing braces in its write operation.
82 */
83 virtual void writeBlocks(std::ostream& stream) = 0;
84
85 /**
86 * Called before the info file stream is closed, time for cleanup.
87 */
88 virtual void onInfoFileSaveFinished() = 0;
89
90 // Info File Loading / Parsing
91
92 /**
93 * Called before the info file is loaded, so take tihs opportunity to
94 * clear internal structures that are going to be filled during the parse process.
95 */
96 virtual void onInfoFileLoadStart() = 0;
97
98 /**
99 * The info file parser will ask this module when a named block is encountered.
100 * A module which returns true on a block will sign up for parsing the block
101 * and a subsequent call to parseBlock() is imminent.
102 */
103 virtual bool canParseBlock(const std::string& blockName) = 0;
104
105 /**
106 * Parse a block as found in the info file. The block name as passed to this method
107 * needs to be registered in the IMapInfoFileManager class before.
108 *
109 * Regarding the state of the tokeniser: the block name will already have been parsed
110 * by the time this method is called, so expect the opening brace { as first token.
111 */
112 virtual void parseBlock(const std::string& blockName, parser::DefTokeniser& tok) = 0;
113
114 /**
115 * Invoked by the map parsing code when the info file has been fully loaded,
116 * so modules should now apply the loaded information to the map.
117 * The info file is always loaded after the actual .map file, so this method
118 * can assume that the scene graph is already in place.
119 * For convenience, a NodeMap is passed to this method, mapping
120 * the entity/primitive number combination to scene::INodes.
121 */
122 virtual void applyInfoToScene(const scene::IMapRootNodePtr& root, const NodeIndexMap& nodeMap) = 0;
123
124 /**
125 * Post-parsing cleanup routine, called after applyInfoToScene().
126 */
127 virtual void onInfoFileLoadFinished() = 0;
128 };
129 typedef std::shared_ptr<IMapInfoFileModule> IMapInfoFileModulePtr;
130
131 class IMapInfoFileManager :
132 public RegisterableModule
133 {
134 public:
135 virtual ~IMapInfoFileManager()
136 {}
137
138 /**
139 * Add an info file module to the global list. The module will be considered
140 * during info file export/import.
141 */
142 virtual void registerInfoFileModule(const IMapInfoFileModulePtr& module) = 0;
143
144 /**
145 * Unregister a previouly registered info file module.
146 */
147 virtual void unregisterInfoFileModule(const IMapInfoFileModulePtr& module) = 0;
148
149 /**
150 * Call the functor for each registered module.
151 */
152 virtual void foreachModule(const std::function<void(IMapInfoFileModule&)>& functor) = 0;
153 };
154
155 }
156
157 const char* const MODULE_MAPINFOFILEMANAGER("MapInfoFileManager");
158
159 // Application-wide Accessor to the global map info file manager
160 inline map::IMapInfoFileManager& GlobalMapInfoFileManager()
161 {
162 static module::InstanceReference<map::IMapInfoFileManager> _reference(MODULE_MAPINFOFILEMANAGER);
163 return _reference;
164 }