Преглед на файлове

Better validate CollisionShape config. warning after #37226

It could cause a crash when running the test for PlaneShape.
Fixes #42479.
Rémi Verschelde преди 5 години
родител
ревизия
10fa15a047
променени са 2 файла, в които са добавени 25 реда и са изтрити 22 реда
  1. 11 8
      scene/2d/collision_shape_2d.cpp
  2. 14 14
      scene/3d/collision_shape.cpp

+ 11 - 8
scene/2d/collision_shape_2d.cpp

@@ -181,27 +181,30 @@ bool CollisionShape2D::_edit_is_selected_on_click(const Point2 &p_point, double
 String CollisionShape2D::get_configuration_warning() const {
 
 	String warning = Node2D::get_configuration_warning();
+
 	if (!Object::cast_to<CollisionObject2D>(get_parent())) {
 		if (warning != String()) {
 			warning += "\n\n";
 		}
 		warning += TTR("CollisionShape2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidBody2D, KinematicBody2D, etc. to give them a shape.");
 	}
+
 	if (!shape.is_valid()) {
 		if (warning != String()) {
 			warning += "\n\n";
 		}
 		warning += TTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!");
-	}
-
-	Ref<ConvexPolygonShape2D> convex = shape;
-	Ref<ConcavePolygonShape2D> concave = shape;
-	if (convex.is_valid() || concave.is_valid()) {
-		if (warning != String()) {
-			warning += "\n\n";
+	} else {
+		Ref<ConvexPolygonShape2D> convex = shape;
+		Ref<ConcavePolygonShape2D> concave = shape;
+		if (convex.is_valid() || concave.is_valid()) {
+			if (warning != String()) {
+				warning += "\n\n";
+			}
+			warning += TTR("Polygon-based shapes are not meant be used nor edited directly through the CollisionShape2D node. Please use the CollisionPolygon2D node instead.");
 		}
-		warning += TTR("Polygon-based shapes are not meant be used nor edited directly through the CollisionShape2D node. Please use the CollisionPolygon2D node instead.");
 	}
+
 	return warning;
 }
 

+ 14 - 14
scene/3d/collision_shape.cpp

@@ -29,6 +29,7 @@
 /*************************************************************************/
 
 #include "collision_shape.h"
+
 #include "scene/resources/box_shape.h"
 #include "scene/resources/capsule_shape.h"
 #include "scene/resources/concave_polygon_shape.h"
@@ -116,6 +117,7 @@ void CollisionShape::resource_changed(RES res) {
 String CollisionShape::get_configuration_warning() const {
 
 	String warning = Spatial::get_configuration_warning();
+
 	if (!Object::cast_to<CollisionObject>(get_parent())) {
 		if (warning != String()) {
 			warning += "\n\n";
@@ -128,23 +130,21 @@ String CollisionShape::get_configuration_warning() const {
 			warning += "\n\n";
 		}
 		warning += TTR("A shape must be provided for CollisionShape to function. Please create a shape resource for it.");
-	}
-
-	if (shape->is_class("PlaneShape")) {
-		if (warning != String()) {
-			warning += "\n\n";
+	} else {
+		if (shape->is_class("PlaneShape")) {
+			if (warning != String()) {
+				warning += "\n\n";
+			}
+			warning += TTR("Plane shapes don't work well and will be removed in future versions. Please don't use them.");
 		}
-		warning += TTR("Plane shapes don't work well and will be removed in future versions. Please don't use them.");
-	}
 
-	if (Object::cast_to<RigidBody>(get_parent())) {
-		if (Object::cast_to<ConcavePolygonShape>(*shape)) {
-			if (Object::cast_to<RigidBody>(get_parent())->get_mode() != RigidBody::MODE_STATIC) {
-				if (warning != String()) {
-					warning += "\n\n";
-				}
-				warning += TTR("ConcavePolygonShape doesn't support RigidBody in another mode than static.");
+		if (Object::cast_to<RigidBody>(get_parent()) &&
+				Object::cast_to<ConcavePolygonShape>(*shape) &&
+				Object::cast_to<RigidBody>(get_parent())->get_mode() != RigidBody::MODE_STATIC) {
+			if (warning != String()) {
+				warning += "\n\n";
 			}
+			warning += TTR("ConcavePolygonShape doesn't support RigidBody in another mode than static.");
 		}
 	}