Răsfoiți Sursa

Do not crash on bad input

Daniele Bartolini 11 ani în urmă
părinte
comite
cbce78e6f8

+ 3 - 5
engine/lua/lua_world.cpp

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "gui.h"
 #include "temp_allocator.h"
 #include "array.h"
+#include "lua_assert.h"
 
 namespace crown
 {
@@ -38,15 +39,13 @@ namespace crown
 static int world_spawn_unit(lua_State* L)
 {
 	LuaStack stack(L);
-
 	World* world = stack.get_world(1);
 	const char* name = stack.get_string(2);
-
 	const Vector3& pos = stack.num_args() > 2 ? stack.get_vector3(3) : vector3::ZERO;
 	const Quaternion& rot = stack.num_args() > 3 ? stack.get_quaternion(4) : quaternion::IDENTITY;
 
+	LUA_ASSERT(device()->resource_manager()->can_get(UNIT_EXTENSION, name), stack, "Unit '%s' not found", name);
 	UnitId unit = world->spawn_unit(name, pos, rot);
-
 	stack.push_unit(world->get_unit(unit));
 	return 1;
 }
@@ -116,16 +115,15 @@ static int world_update(lua_State* L)
 static int world_play_sound(lua_State* L)
 {
 	LuaStack stack(L);
-
 	World* world = stack.get_world(1);
 	const char* name = stack.get_string(2);
 	int32_t nargs = stack.num_args();
-
 	const bool loop = nargs > 2 ? stack.get_bool(3) : false;
 	const float volume = nargs > 3 ? stack.get_float(4) : 1.0f;
 	const Vector3& pos = nargs > 4 ? stack.get_vector3(5) : vector3::ZERO;
 	const float range = nargs > 5 ? stack.get_float(6) : 1000.0f;
 
+	LUA_ASSERT(device()->resource_manager()->can_get(SOUND_EXTENSION, name), stack, "Sound '%s' not found", name);
 	stack.push_sound_instance_id(world->play_sound(name, loop, volume, pos, range));
 	return 1;
 }

+ 6 - 0
engine/resource/resource_manager.cpp

@@ -91,6 +91,12 @@ void ResourceManager::unload(ResourceId id, bool force)
 	}
 }
 
+//-----------------------------------------------------------------------------
+bool ResourceManager::can_get(const char* type, const char* name)
+{
+	return can_get(ResourceId(type, name));
+}
+
 //-----------------------------------------------------------------------------
 bool ResourceManager::can_get(ResourceId id) const
 {

+ 3 - 0
engine/resource/resource_manager.h

@@ -70,6 +70,9 @@ public:
 	/// Use @a force option only if you know *exactly* what you are doing.
 	void unload(ResourceId id, bool force = false);
 
+	/// Returns whether the manager has the given resource. 
+	bool can_get(const char* type, const char* name);
+
 	/// Returns whether the manager has the resource @a id.
 	bool can_get(ResourceId id) const;