/
MapExporter.cpp
311 lines (246 loc) · 6.71 KB
/
MapExporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "MapExporter.h"
#include <ostream>
#include "i18n.h"
#include "itextstream.h"
#include "ibrush.h"
#include "ipatch.h"
#include "ientity.h"
#include "imap.h"
#include "igroupnode.h"
#include "imainframe.h"
#include "../../brush/Brush.h"
#include "registry/registry.h"
#include "string/string.h"
#include "ChildPrimitives.h"
namespace map
{
namespace
{
const char* const RKEY_FLOAT_PRECISION = "/mapFormat/floatPrecision";
const char* const RKEY_MAP_SAVE_STATUS_INTERLEAVE = "user/ui/map/saveStatusInterleave";
}
MapExporter::MapExportEvent MapExporter::_preExportSignal;
MapExporter::MapExportEvent MapExporter::_postExportSignal;
MapExporter::MapExporter(IMapWriter& writer, const scene::IMapRootNodePtr& root, std::ostream& mapStream, std::size_t nodeCount) :
_writer(writer),
_mapStream(mapStream),
_root(root),
_dialogEventLimiter(registry::getValue<int>(RKEY_MAP_SAVE_STATUS_INTERLEAVE)),
_totalNodeCount(nodeCount),
_curNodeCount(0),
_entityNum(0),
_primitiveNum(0)
{
construct();
}
MapExporter::MapExporter(IMapWriter& writer, const scene::IMapRootNodePtr& root,
std::ostream& mapStream, std::ostream& auxStream, std::size_t nodeCount) :
_writer(writer),
_mapStream(mapStream),
_infoFileExporter(new InfoFileExporter(auxStream)),
_root(root),
_dialogEventLimiter(registry::getValue<int>(RKEY_MAP_SAVE_STATUS_INTERLEAVE)),
_totalNodeCount(nodeCount),
_curNodeCount(0),
_entityNum(0),
_primitiveNum(0)
{
construct();
}
MapExporter::~MapExporter()
{
// Close any info file stream
_infoFileExporter.reset();
// The finish() call is placed in the destructor to make sure that
// even on unhandled exceptions the map is left in a working state
finishScene();
}
void MapExporter::construct()
{
if (_totalNodeCount > 0 && GlobalMainFrame().isActiveApp())
{
enableProgressDialog();
}
// Prepare the output stream
game::IGamePtr curGame = GlobalGameManager().currentGame();
assert(curGame != NULL);
xml::NodeList nodes = curGame->getLocalXPath(RKEY_FLOAT_PRECISION);
assert(!nodes.empty());
int precision = string::convert<int>(nodes[0].getAttributeValue("value"));
_mapStream.precision(precision);
// Add origin to func_* children before writing
prepareScene();
}
MapExporter::MapExportEvent& MapExporter::signal_preExport()
{
return _preExportSignal;
}
MapExporter::MapExportEvent& MapExporter::signal_postExport()
{
return _postExportSignal;
}
void MapExporter::exportMap(const scene::INodePtr& root, const GraphTraversalFunc& traverse)
{
try
{
auto mapRoot = std::dynamic_pointer_cast<scene::IMapRootNode>(root);
if (!mapRoot)
{
throw std::logic_error("Map node is not a scene::IMapRootNode");
}
_writer.beginWriteMap(mapRoot, _mapStream);
if (_infoFileExporter)
{
_infoFileExporter->beginSaveMap(mapRoot);
}
}
catch (IMapWriter::FailureException& ex)
{
rError() << "Failure exporting a node (pre): " << ex.what() << std::endl;
}
// Perform the actual map traversal
traverse(root, *this);
try
{
auto mapRoot = std::dynamic_pointer_cast<scene::IMapRootNode>(root);
if (!mapRoot)
{
throw std::logic_error("Map node is not a scene::IMapRootNode");
}
_writer.endWriteMap(mapRoot, _mapStream);
if (_infoFileExporter)
{
_infoFileExporter->finishSaveMap(mapRoot);
}
}
catch (IMapWriter::FailureException& ex)
{
rError() << "Failure exporting a node (pre): " << ex.what() << std::endl;
}
// finishScene() is handled through the destructor
}
void MapExporter::enableProgressDialog()
{
_dialog.reset(new wxutil::ModalProgressDialog(_("Writing map")));
}
void MapExporter::disableProgressDialog()
{
_dialog.reset();
}
bool MapExporter::pre(const scene::INodePtr& node)
{
try
{
auto entity = std::dynamic_pointer_cast<IEntityNode>(node);
if (entity)
{
// Progress dialog handling
onNodeProgress();
_writer.beginWriteEntity(entity, _mapStream);
if (_infoFileExporter) _infoFileExporter->visitEntity(node, _entityNum);
return true;
}
auto brush = std::dynamic_pointer_cast<IBrushNode>(node);
if (brush && brush->getIBrush().hasContributingFaces())
{
// Progress dialog handling
onNodeProgress();
_writer.beginWriteBrush(brush, _mapStream);
if (_infoFileExporter) _infoFileExporter->visitPrimitive(node, _entityNum, _primitiveNum);
return true;
}
auto patch = std::dynamic_pointer_cast<IPatchNode>(node);
if (patch)
{
// Progress dialog handling
onNodeProgress();
_writer.beginWritePatch(patch, _mapStream);
if (_infoFileExporter) _infoFileExporter->visitPrimitive(node, _entityNum, _primitiveNum);
return true;
}
}
catch (IMapWriter::FailureException& ex)
{
rError() << "Failure exporting a node (pre): " << ex.what() << std::endl;
}
return true; // full traversal
}
void MapExporter::post(const scene::INodePtr& node)
{
try
{
auto entity = std::dynamic_pointer_cast<IEntityNode>(node);
if (entity)
{
_writer.endWriteEntity(entity, _mapStream);
_entityNum++;
return;
}
auto brush = std::dynamic_pointer_cast<IBrushNode>(node);
if (brush && brush->getIBrush().hasContributingFaces())
{
_writer.endWriteBrush(brush, _mapStream);
_primitiveNum++;
return;
}
auto patch = std::dynamic_pointer_cast<IPatchNode>(node);
if (patch)
{
_writer.endWritePatch(patch, _mapStream);
_primitiveNum++;
return;
}
}
catch (IMapWriter::FailureException& ex)
{
rError() << "Failure exporting a node (post): " << ex.what() << std::endl;
}
}
void MapExporter::onNodeProgress()
{
_curNodeCount++;
// Update the dialog text. This will throw an exception if the cancel
// button is clicked, which we must catch and handle.
if (_dialog && _dialogEventLimiter.readyForEvent())
{
std::string text = fmt::format(_("Writing node {0:d}"), _curNodeCount);
_dialog->setTextAndFraction(
text,
static_cast<double>(_curNodeCount) / static_cast<double>(_totalNodeCount)
);
}
}
void MapExporter::prepareScene()
{
removeOriginFromChildPrimitives(_root);
// Re-evaluate all brushes, to update the Winding calculations
recalculateBrushWindings();
// Emit the pre-export event to given notifiers a chance to prepare the scene
_preExportSignal.emit(_root);
}
void MapExporter::finishScene()
{
// Emit the post-export event to given notifiers a chance to cleanup the scene
_postExportSignal.emit(_root);
addOriginToChildPrimitives(_root);
// Re-evaluate all brushes, to update the Winding calculations
recalculateBrushWindings();
}
void MapExporter::recalculateBrushWindings()
{
_root->foreachNode([] (const scene::INodePtr& child)->bool
{
Brush* brush = Node_getBrush(child);
if (brush != NULL)
{
brush->evaluateBRep();
}
return true;
});
}
void MapExporter::cleanupEvents()
{
_preExportSignal.clear();
_postExportSignal.clear();
}
} // namespace