/
MapExport.cpp
254 lines (205 loc) · 9.56 KB
/
MapExport.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
#include "RadiantTest.h"
#include "imap.h"
#include "imapformat.h"
#include "ibrush.h"
#include "iselection.h"
#include "scenelib.h"
#include "os/path.h"
#include "string/predicate.h"
#include "xmlutil/Document.h"
#include "messages/MapFileOperation.h"
#include "algorithm/XmlUtils.h"
#include "algorithm/Primitives.h"
#include "testutil/FileSelectionHelper.h"
namespace test
{
namespace
{
void createAndSelectSingleBrush()
{
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
auto brushNode = GlobalBrushCreator().createBrush();
worldspawn->addChildNode(brushNode);
GlobalSelectionSystem().setSelectedAll(false);
Node_setSelected(brushNode, true);
}
}
using MapExportTest = RadiantTest;
TEST_F(MapExportTest, exportSelectedWithFormat)
{
createAndSelectSingleBrush();
auto format = GlobalMapFormatManager().getMapFormatByName(map::PORTABLE_MAP_FORMAT_NAME);
std::ostringstream output;
GlobalMapModule().exportSelected(output, format);
output.flush();
// Minimal assertion: we got a string that appears to start like an XML document
algorithm::assertStringIsMapxFile(output.str());
}
TEST_F(MapExportTest, exportSelectedDoesNotSendMessages)
{
createAndSelectSingleBrush();
auto format = GlobalMapFormatManager().getMapFormatByName(map::PORTABLE_MAP_FORMAT_NAME);
bool messageReceived = false;
// Subscribe to the messages potentially sent by the MapExporter
auto listener = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::MapFileOperation,
radiant::TypeListener<map::FileOperation>([&](map::FileOperation& msg)
{
messageReceived = true;
}));
std::ostringstream output;
GlobalMapModule().exportSelected(output, format);
output.flush();
GlobalRadiantCore().getMessageBus().removeListener(listener);
EXPECT_FALSE(messageReceived) << "Received a FileOperation message while exporting the selection";
}
// Exports a default cuboid brush with its texture fitted 1x1 to a temporary path
// using the format for the given game type. Returns the exported map text.
std::string exportDefaultBrushUsingFormat(const std::string& gameType, const std::string& exportPath)
{
auto brush = algorithm::createCuboidBrush(GlobalMapModule().findOrInsertWorldspawn(),
AABB(Vector3(0, 0, 0), Vector3(64, 128, 256)), "textures/darkmod/numbers/1");
for (auto i = 0; i < Node_getIBrush(brush)->getNumFaces(); ++i)
{
Node_getIBrush(brush)->getFace(i).fitTexture(1, 1);;
}
auto format = GlobalMapFormatManager().getMapFormatForGameType(gameType, os::getExtension(exportPath));
EXPECT_FALSE(fs::exists(exportPath)) << "File already exists";
FileSelectionHelper helper(exportPath, format);
GlobalCommandSystem().executeCommand("ExportMap");
EXPECT_TRUE(fs::exists(exportPath)) << "File still doesn't exist";
std::ifstream tempFile(exportPath);
std::stringstream content;
content << tempFile.rdbuf();
return content.str();
}
TEST_F(MapExportTest, exportDoom3Brush)
{
fs::path tempPath = _context.getTemporaryDataPath();
tempPath /= "brushexport.map";
auto text = exportDefaultBrushUsingFormat("doom3", tempPath.string());
auto brushTextIndex = text.find(R"BRUSH(// primitive 0
{
brushDef3
{
( 1 0 0 -64 ) ( ( 0.00390625 0 0.5 ) ( 0 0.001953125 0.5 ) ) "textures/darkmod/numbers/1" 0 0 0
( -1 0 0 -64 ) ( ( 0.00390625 0 0.5 ) ( 0 0.001953125 0.5 ) ) "textures/darkmod/numbers/1" 0 0 0
( 0 1 0 -128 ) ( ( 0.0078125 0 0.5 ) ( 0 0.001953125 0.5 ) ) "textures/darkmod/numbers/1" 0 0 0
( 0 -1 0 -128 ) ( ( 0.0078125 0 0.5 ) ( 0 0.001953125 0.5 ) ) "textures/darkmod/numbers/1" 0 0 0
( 0 0 1 -256 ) ( ( 0.00390625 0 0.5 ) ( 0 0.0078125 0.5 ) ) "textures/darkmod/numbers/1" 0 0 0
( 0 0 -1 -256 ) ( ( 0.00390625 0 0.5 ) ( 0 0.0078125 0.5 ) ) "textures/darkmod/numbers/1" 0 0 0
}
})BRUSH");
EXPECT_NE(brushTextIndex, std::string::npos) << "Could not locate the exported brush in the expected format";
}
TEST_F(MapExportTest, exportDoom3BrushPortable)
{
fs::path tempPath = _context.getTemporaryDataPath();
tempPath /= "brushexport.mapx";
auto text = exportDefaultBrushUsingFormat("doom3", tempPath.string());
auto brushTextIndex = text.find(R"BRUSH(<brush number="0">
<faces>
<face>
<plane x="1.000000" y="0" z="0" d="-64.000000"/>
<textureProjection xx="0.003906" yx="0" tx="0.500000" xy="0" yy="0.001953" ty="0.500000"/>
<material name="textures/darkmod/numbers/1"/>
<contentsFlag value="0"/>
</face>
<face>
<plane x="-1.000000" y="0" z="0" d="-64.000000"/>
<textureProjection xx="0.003906" yx="0" tx="0.500000" xy="0" yy="0.001953" ty="0.500000"/>
<material name="textures/darkmod/numbers/1"/>
<contentsFlag value="0"/>
</face>
<face>
<plane x="0" y="1.000000" z="0" d="-128.000000"/>
<textureProjection xx="0.007813" yx="0" tx="0.500000" xy="0" yy="0.001953" ty="0.500000"/>
<material name="textures/darkmod/numbers/1"/>
<contentsFlag value="0"/>
</face>
<face>
<plane x="0" y="-1.000000" z="0" d="-128.000000"/>
<textureProjection xx="0.007813" yx="0" tx="0.500000" xy="0" yy="0.001953" ty="0.500000"/>
<material name="textures/darkmod/numbers/1"/>
<contentsFlag value="0"/>
</face>
<face>
<plane x="0" y="0" z="1.000000" d="-256.000000"/>
<textureProjection xx="0.003906" yx="0" tx="0.500000" xy="0" yy="0.007813" ty="0.500000"/>
<material name="textures/darkmod/numbers/1"/>
<contentsFlag value="0"/>
</face>
<face>
<plane x="0" y="0" z="-1.000000" d="-256.000000"/>
<textureProjection xx="0.003906" yx="0" tx="0.500000" xy="0" yy="0.007813" ty="0.500000"/>
<material name="textures/darkmod/numbers/1"/>
<contentsFlag value="0"/>
</face>
</faces>
<layers>
<layer id="0"/>
</layers>
<selectionGroups/>
<selectionSets/>
</brush>)BRUSH");
EXPECT_NE(brushTextIndex, std::string::npos) << "Could not locate the exported brush in the expected format";
}
TEST_F(MapExportTest, exportQuake3Brush)
{
fs::path tempPath = _context.getTemporaryDataPath();
tempPath /= "brushexport.map";
auto text = exportDefaultBrushUsingFormat("quake3", tempPath.string());
// Quake 3 format (after #5711) should be writing the old brushDef format without
// brushDef header, using the shift/scale/rotation tex def syntax
auto brushTextIndex = text.find(R"BRUSH(// brush 0
{
( 64 128 -256 ) ( 64 -128 256 ) ( 64 128 256 ) darkmod/numbers/1 32 32 -180 -4 -8 0 0 0
( -64 -128 -256 ) ( -64 128 256 ) ( -64 -128 256 ) darkmod/numbers/1 32 32 0 -4 8 0 0 0
( -64 128 -256 ) ( 64 128 256 ) ( -64 128 256 ) darkmod/numbers/1 32 32 0 -2 8 0 0 0
( 64 -128 -256 ) ( -64 -128 256 ) ( 64 -128 256 ) darkmod/numbers/1 32 32 -180 -2 -8 0 0 0
( -64 128 256 ) ( 64 -128 256 ) ( -64 -128 256 ) darkmod/numbers/1 32 32 90 4 2 0 0 0
( -64 128 -256 ) ( 64 -128 -256 ) ( 64 128 -256 ) darkmod/numbers/1 32 32 90 4 -2 0 0 0
})BRUSH");
EXPECT_NE(brushTextIndex, std::string::npos) << "Could not locate the exported brush in the expected format";
}
TEST_F(MapExportTest, exportQuake3AlternateBrush)
{
fs::path tempPath = _context.getTemporaryDataPath();
tempPath /= "brushexport.map";
auto text = exportDefaultBrushUsingFormat("quake3alternate", tempPath.string());
// Quake 3 alternate format is writing the newer brushDef format including the brushDef keyword
auto brushTextIndex = text.find(R"BRUSH(// brush 0
{
brushDef
{
( 64 128 -256 ) ( 64 -128 256 ) ( 64 128 256 ) ( ( 0.00390625 0 0.5 ) ( 0 0.001953125 0.5 ) ) darkmod/numbers/1 0 0 0
( -64 -128 -256 ) ( -64 128 256 ) ( -64 -128 256 ) ( ( 0.00390625 0 0.5 ) ( 0 0.001953125 0.5 ) ) darkmod/numbers/1 0 0 0
( -64 128 -256 ) ( 64 128 256 ) ( -64 128 256 ) ( ( 0.0078125 0 0.5 ) ( 0 0.001953125 0.5 ) ) darkmod/numbers/1 0 0 0
( 64 -128 -256 ) ( -64 -128 256 ) ( 64 -128 256 ) ( ( 0.0078125 0 0.5 ) ( 0 0.001953125 0.5 ) ) darkmod/numbers/1 0 0 0
( -64 128 256 ) ( 64 -128 256 ) ( -64 -128 256 ) ( ( 0.00390625 0 0.5 ) ( 0 0.0078125 0.5 ) ) darkmod/numbers/1 0 0 0
( -64 128 -256 ) ( 64 -128 -256 ) ( 64 128 -256 ) ( ( 0.00390625 0 0.5 ) ( 0 0.0078125 0.5 ) ) darkmod/numbers/1 0 0 0
}
})BRUSH");
EXPECT_NE(brushTextIndex, std::string::npos) << "Could not locate the exported brush in the expected format";
}
TEST_F(MapExportTest, exportQuake4Brush)
{
fs::path tempPath = _context.getTemporaryDataPath();
tempPath /= "brushexport.map";
auto text = exportDefaultBrushUsingFormat("quake4", tempPath.string());
// Quake 4 format is like Doom 3, just without the additional flag numbers at the end of each face
auto brushTextIndex = text.find(R"BRUSH(// primitive 0
{
brushDef3
{
( 1 0 0 -64 ) ( ( 0.00390625 0 0.5 ) ( 0 0.001953125 0.5 ) ) "textures/darkmod/numbers/1"
( -1 0 0 -64 ) ( ( 0.00390625 0 0.5 ) ( 0 0.001953125 0.5 ) ) "textures/darkmod/numbers/1"
( 0 1 0 -128 ) ( ( 0.0078125 0 0.5 ) ( 0 0.001953125 0.5 ) ) "textures/darkmod/numbers/1"
( 0 -1 0 -128 ) ( ( 0.0078125 0 0.5 ) ( 0 0.001953125 0.5 ) ) "textures/darkmod/numbers/1"
( 0 0 1 -256 ) ( ( 0.00390625 0 0.5 ) ( 0 0.0078125 0.5 ) ) "textures/darkmod/numbers/1"
( 0 0 -1 -256 ) ( ( 0.00390625 0 0.5 ) ( 0 0.0078125 0.5 ) ) "textures/darkmod/numbers/1"
}
})BRUSH");
EXPECT_NE(brushTextIndex, std::string::npos) << "Could not locate the exported brush in the expected format";
}
}