Skip to content

Commit

Permalink
#5663: Throw an exception if the merged patch would exceed the maximu…
Browse files Browse the repository at this point in the history
…m dimensions
  • Loading branch information
codereader committed Aug 21, 2022
1 parent 8682e88 commit 2a83c7e
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 4 deletions.
6 changes: 6 additions & 0 deletions radiantcore/patch/algorithm/General.cpp
Expand Up @@ -305,6 +305,12 @@ scene::INodePtr createdMergedPatch(const PatchNodePtr& patchNode1, const PatchNo
auto& dimensionToExpand = patch1Edge.edgeType == EdgeType::Row ? numNewRows : numNewColumns;
dimensionToExpand += numNewEdges;

// Check if the new dimensions are out of bounds
if (numNewColumns > MAX_PATCH_WIDTH || numNewRows > MAX_PATCH_HEIGHT)
{
throw cmd::ExecutionFailure(_("Merged patch would have exceed maximum dimensions, cannot proceed."));
}

// Create the new patch
auto newPatchNode = GlobalPatchModule().createPatch(patch1.subdivisionsFixed() ? PatchDefType::Def3 : PatchDefType::Def2);
auto& newPatch = std::dynamic_pointer_cast<IPatchNode>(newPatchNode)->getPatch();
Expand Down
26 changes: 22 additions & 4 deletions test/PatchWelding.cpp
Expand Up @@ -78,11 +78,8 @@ void verifyMergedPatch(const scene::INodePtr& mergedPatchNode, int expectedRows,
EXPECT_EQ(merged->getPatch().getWidth(), expectedCols);
}

void assumePatchWeldingFails(const std::string& number1, const std::string& number2)
void assumePatchWeldingFails(const scene::INodePtr& firstPatch, const scene::INodePtr& secondPatch)
{
auto firstPatch = findPatchWithNumber(number1);
auto secondPatch = findPatchWithNumber(number2);

Node_setSelected(firstPatch, true);
Node_setSelected(secondPatch, true);

Expand All @@ -99,6 +96,14 @@ void assumePatchWeldingFails(const std::string& number1, const std::string& numb
EXPECT_TRUE(secondPatch->getParent());
}

void assumePatchWeldingFails(const std::string& number1, const std::string& number2)
{
auto firstPatch = findPatchWithNumber(number1);
auto secondPatch = findPatchWithNumber(number2);

assumePatchWeldingFails(firstPatch, secondPatch);
}

}

TEST_P(PatchWelding3x3, WeldWithOther3x3Patch)
Expand Down Expand Up @@ -157,6 +162,19 @@ INSTANTIATE_TEST_CASE_P(WeldPatch16WithOther3x3, PatchWelding3x3,
std::tuple{ "19", "16", 5, 3 },
std::tuple{ "20", "16", 5, 3 }));

TEST_F(PatchWeldingTest, WeldPatchExceedingMaximumDimensions)
{
loadMap("weld_patches_out_of_bounds.map");

auto firstPatch = algorithm::getNthChild(GlobalMapModule().findOrInsertWorldspawn(), 0);
auto secondPatch = algorithm::getNthChild(GlobalMapModule().findOrInsertWorldspawn(), 1);

EXPECT_TRUE(Node_isPatch(firstPatch)) << "Map should contain excactly two worldspawn patches";
EXPECT_TRUE(Node_isPatch(secondPatch)) << "Map should contain excactly two worldspawn patches";

assumePatchWeldingFails(firstPatch, secondPatch);
}

// Patch 4 = worldspawn, Patch 7 = func_static
TEST_F(PatchWeldingTest, TryToWeldPatchesOfDifferentParents)
{
Expand Down

0 comments on commit 2a83c7e

Please sign in to comment.