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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
#include "RadiantTest.h"
#include "imap.h"
#include "imapformat.h"
#include "ibrush.h"
#include "math/Plane3.h"
#include "math/Matrix3.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";
return algorithm::loadFileToString(exportPath);
}
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";
}
struct XmlFace
{
Plane3 plane;
Matrix3 textureMatrix;
std::string material;
unsigned int contentsFlag;
static XmlFace ParseFromNode(const xml::Node& node)
{
XmlFace face;
face.material = node.getNamedChildren("material")[0].getAttributeValue("name");
auto planeNode = node.getNamedChildren("plane")[0];
face.plane.normal().x() = string::to_float(planeNode.getAttributeValue("x"));
face.plane.normal().y() = string::to_float(planeNode.getAttributeValue("y"));
face.plane.normal().z() = string::to_float(planeNode.getAttributeValue("z"));
face.plane.dist() = -string::to_float(planeNode.getAttributeValue("d")); // negate d
auto texProjTag = node.getNamedChildren("textureProjection")[0];
face.textureMatrix.xx() = string::to_float(texProjTag.getAttributeValue("xx"));
face.textureMatrix.yx() = string::to_float(texProjTag.getAttributeValue("yx"));
face.textureMatrix.zx() = string::to_float(texProjTag.getAttributeValue("tx"));
face.textureMatrix.xy() = string::to_float(texProjTag.getAttributeValue("xy"));
face.textureMatrix.yy() = string::to_float(texProjTag.getAttributeValue("yy"));
face.textureMatrix.zy() = string::to_float(texProjTag.getAttributeValue("ty"));
face.contentsFlag = string::convert<unsigned int>(node.getNamedChildren("contentsFlag")[0].getAttributeValue("value"));
return face;
}
};
inline bool hasFaceWithProperties(const std::vector<XmlFace>& faces, const std::string& material, unsigned int contentsFlag,
const Plane3& plane, double xx, double yx, double tx, double xy, double yy, double ty)
{
constexpr double Epsilon = 0.001;
for (const auto& face : faces)
{
if (face.material != material) continue;
if (face.contentsFlag != contentsFlag) continue;
if (!math::isNear(face.plane.normal(), plane.normal(), Epsilon)) continue;
if (std::abs(face.plane.dist() - plane.dist()) > Epsilon) continue;
if (std::abs(face.textureMatrix.xx() - xx) > Epsilon) continue;
if (std::abs(face.textureMatrix.yx() - yx) > Epsilon) continue;
if (std::abs(face.textureMatrix.zx() - tx) > Epsilon) continue;
if (std::abs(face.textureMatrix.xy() - xy) > Epsilon) continue;
if (std::abs(face.textureMatrix.yy() - yy) > Epsilon) continue;
if (std::abs(face.textureMatrix.zy() - ty) > Epsilon) continue;
return true;
}
return false;
}
TEST_F(MapExportTest, exportDoom3BrushPortable)
{
fs::path tempPath = _context.getTemporaryDataPath();
tempPath /= "brushexport.mapx";
auto text = exportDefaultBrushUsingFormat("doom3", tempPath.string());
std::stringstream stream(text);
xml::Document doc(stream);
auto brush = doc.findXPath("//brush[@number='0']");
EXPECT_EQ(brush.size(), 1) << "More than 1 brush found in XML";
auto faceNodes = doc.findXPath("//brush[@number='0']/faces//face");
EXPECT_EQ(faceNodes.size(), 6) << "Other than 6 faces brush found in XML";
std::vector<XmlFace> faces;
for (const auto& faceNode : faceNodes)
{
faces.emplace_back(XmlFace::ParseFromNode(faceNode));
}
EXPECT_TRUE(hasFaceWithProperties(faces, "textures/darkmod/numbers/1", 0, Plane3(1.0, 0.0, 0.0, 64), 0.003906, 0, 0.5, 0, 0.001953, 0.5));
EXPECT_TRUE(hasFaceWithProperties(faces, "textures/darkmod/numbers/1", 0, Plane3(-1.0, 0.0, 0.0, 64), 0.003906, 0, 0.5, 0, 0.001953, 0.5));
EXPECT_TRUE(hasFaceWithProperties(faces, "textures/darkmod/numbers/1", 0, Plane3(0, 1.0, 0.0, 128), 0.007813, 0, 0.5, 0, 0.001953, 0.5));
EXPECT_TRUE(hasFaceWithProperties(faces, "textures/darkmod/numbers/1", 0, Plane3(0, -1.0, 0.0, 128), 0.007813, 0, 0.5, 0, 0.001953, 0.5));
EXPECT_TRUE(hasFaceWithProperties(faces, "textures/darkmod/numbers/1", 0, Plane3(0, 0, 1.0, 256), 0.003906, 0, 0.5, 0, 0.007813, 0.5));
EXPECT_TRUE(hasFaceWithProperties(faces, "textures/darkmod/numbers/1", 0, Plane3(0, 0, -1.0, 256), 0.003906, 0, 0.5, 0, 0.007813, 0.5));
}
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";
}
namespace
{
void runExportWithEmptyFileExtension(const std::string& temporaryDataPath,const std::string& command)
{
auto brush = algorithm::createCuboidBrush(GlobalMapModule().findOrInsertWorldspawn(),
AABB(Vector3(0, 0, 0), Vector3(64, 128, 256)), "textures/darkmod/numbers/1");
Node_setSelected(brush, true);
fs::path tempPath = temporaryDataPath;
tempPath /= "export_empty_file_extension";
EXPECT_FALSE(fs::exists(tempPath)) << "File already exists";
// Subscribe to the event asking for the target path
auto msgSubscription = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::FileSelectionRequest,
radiant::TypeListener<radiant::FileSelectionRequest>(
[&](radiant::FileSelectionRequest& msg)
{
msg.setHandled(true);
msg.setResult(radiant::FileSelectionRequest::Result
{
tempPath.string(),
"" // this can happen if e.g. the *.* filter is active
});
}));
GlobalCommandSystem().executeCommand(command);
EXPECT_TRUE(fs::exists(tempPath)) << "File still doesn't exist";
auto content = algorithm::loadFileToString(tempPath);
EXPECT_NE(content.find("brushDef3"), std::string::npos) << "Couldn't find the brush keyword in the export";
}
}
TEST_F(MapExportTest, ExportSelectedWithEmptyFileExtension)
{
runExportWithEmptyFileExtension(_context.getTemporaryDataPath(), "SaveSelected");
}
TEST_F(MapExportTest, ExportPrefabWithEmptyFileExtension)
{
runExportWithEmptyFileExtension(_context.getTemporaryDataPath(), "SaveSelectedAsPrefab");
}
}
|