2
0
Эх сурвалжийг харах

Be more gentle on bad inputs

Daniele Bartolini 9 жил өмнө
parent
commit
6b2b91ef24

+ 22 - 14
src/device/input_device.cpp

@@ -35,14 +35,18 @@ u8 InputDevice::num_axes() const
 
 bool InputDevice::pressed(u8 id) const
 {
-	CE_ASSERT(id < _num_buttons, "Index out of bounds");
-	return (~_last_state[id] & _state[id]) != 0;
+	return id < _num_buttons
+		? (~_last_state[id] & _state[id]) != 0
+		: false
+		;
 }
 
 bool InputDevice::released(u8 id) const
 {
-	CE_ASSERT(id < _num_buttons, "Index out of bounds");
-	return (_last_state[id] & ~_state[id]) != 0;
+	return id < _num_buttons
+		? (_last_state[id] & ~_state[id]) != 0
+		: false
+		;
 }
 
 bool InputDevice::any_pressed() const
@@ -57,20 +61,26 @@ bool InputDevice::any_released() const
 
 Vector3 InputDevice::axis(u8 id) const
 {
-	CE_ASSERT(id < _num_axes, "Index out of bounds");
-	return _axis[id];
+	return id < _num_axes
+		? _axis[id]
+		: VECTOR3_ZERO
+		;
 }
 
 const char* InputDevice::button_name(u8 id)
 {
-	CE_ASSERT(id < _num_buttons, "Index out of bounds");
-	return _button_name[id];
+	return id < _num_buttons
+		? _button_name[id]
+		: NULL
+		;
 }
 
 const char* InputDevice::axis_name(u8 id)
 {
-	CE_ASSERT(id < _num_axes, "Index out of bounds");
-	return _axis_name[id];
+	return id < _num_axes
+		? _axis_name[id]
+		: NULL
+		;
 }
 
 u8 InputDevice::button_id(StringId32 name)
@@ -81,8 +91,7 @@ u8 InputDevice::button_id(StringId32 name)
 			return i;
 	}
 
-	CE_FATAL("Unknown button name");
-	return 0;
+	return UINT8_MAX;
 }
 
 u8 InputDevice::axis_id(StringId32 name)
@@ -93,8 +102,7 @@ u8 InputDevice::axis_id(StringId32 name)
 			return i;
 	}
 
-	CE_FATAL("Unknown axis name");
-	return 0;
+	return UINT8_MAX;
 }
 
 void InputDevice::set_connected(bool connected)

+ 4 - 4
src/device/input_device.h

@@ -58,16 +58,16 @@ struct InputDevice
 	/// Returns the value of the axis @a id.
 	Vector3 axis(u8 id) const;
 
-	/// Returns the name of the button @a id.
+	/// Returns the name of the button @a id or NULL if no matching button is found.
 	const char* button_name(u8 id);
 
-	/// Returns the name of the axis @a id.
+	/// Returns the name of the axis @a id of NULL if no matching axis is found.
 	const char* axis_name(u8 id);
 
-	/// Returns the id of the button @a name.
+	/// Returns the id of the button @a name or UINT8_MAX if no matching button is found.
 	u8 button_id(StringId32 name);
 
-	/// Returns the id of the axis @a name.
+	/// Returns the id of the axis @a name of UINT8_MAX if no matching axis is found.
 	u8 axis_id(StringId32 name);
 
 	void set_connected(bool connected);

+ 50 - 26
src/lua/lua_api.cpp

@@ -1056,94 +1056,118 @@ static int lightuserdata_newindex(lua_State* L)
 	return 0;
 }
 
-static int input_device_name(lua_State* L, InputDevice& id)
+static int input_device_name(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_string(id.name());
+	stack.push_string(dev.name());
 	return 1;
 }
 
-static int input_device_connected(lua_State* L, InputDevice& id)
+static int input_device_connected(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_bool(id.connected());
+	stack.push_bool(dev.connected());
 	return 1;
 }
 
-static int input_device_num_buttons(lua_State* L, InputDevice& id)
+static int input_device_num_buttons(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_int(id.num_buttons());
+	stack.push_int(dev.num_buttons());
 	return 1;
 }
 
-static int input_device_num_axes(lua_State* L, InputDevice& id)
+static int input_device_num_axes(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_int(id.num_axes());
+	stack.push_int(dev.num_axes());
 	return 1;
 }
 
-static int input_device_pressed(lua_State* L, InputDevice& id)
+static int input_device_pressed(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_bool(id.pressed(stack.get_int(1)));
+	stack.push_bool(dev.pressed(stack.get_int(1)));
 	return 1;
 }
 
-static int input_device_released(lua_State* L, InputDevice& id)
+static int input_device_released(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_bool(id.released(stack.get_int(1)));
+	stack.push_bool(dev.released(stack.get_int(1)));
 	return 1;
 }
 
-static int input_device_any_pressed(lua_State* L, InputDevice& id)
+static int input_device_any_pressed(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_bool(id.any_pressed());
+	stack.push_bool(dev.any_pressed());
 	return 1;
 }
 
-static int input_device_any_released(lua_State* L, InputDevice& id)
+static int input_device_any_released(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_bool(id.any_released());
+	stack.push_bool(dev.any_released());
 	return 1;
 }
 
-static int input_device_axis(lua_State* L, InputDevice& id)
+static int input_device_axis(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_vector3(id.axis(stack.get_int(1)));
+	stack.push_vector3(dev.axis(stack.get_int(1)));
 	return 1;
 }
 
-static int input_device_button_name(lua_State* L, InputDevice& id)
+static int input_device_button_name(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_string(id.button_name(stack.get_int(1)));
+	const char* name = dev.button_name(stack.get_int(1));
+
+	if (name != NULL)
+		stack.push_string(name);
+	else
+		stack.push_nil();
+
 	return 1;
 }
 
-static int input_device_axis_name(lua_State* L, InputDevice& id)
+static int input_device_axis_name(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_string(id.axis_name(stack.get_int(1)));
+	const char* name = dev.axis_name(stack.get_int(1));
+
+	if (name != NULL)
+		stack.push_string(name);
+	else
+		stack.push_nil();
+
 	return 1;
 }
 
-static int input_device_button_id(lua_State* L, InputDevice& id)
+static int input_device_button_id(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_int(id.button_id(stack.get_string_id_32(1)));
+	const u8 id = dev.button_id(stack.get_string_id_32(1));
+
+	if (id != UINT8_MAX)
+		stack.push_int(id);
+	else
+		stack.push_nil();
+
 	return 1;
 }
 
-static int input_device_axis_id(lua_State* L, InputDevice& id)
+static int input_device_axis_id(lua_State* L, InputDevice& dev)
 {
 	LuaStack stack(L);
-	stack.push_int(id.axis_id(stack.get_string_id_32(1)));
+	const u8 id = dev.axis_id(stack.get_string_id_32(1));
+
+	if (id != UINT8_MAX)
+		stack.push_int(id);
+	else
+		stack.push_nil();
+
 	return 1;
 }