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

lua: try to detect lightuserdata type

Daniele Bartolini 4 жил өмнө
parent
commit
3a873fd712

+ 1 - 0
docs/changelog.rst

@@ -30,6 +30,7 @@ Changelog
 * Added ``Math.obb_intersects_frustum()``
 * Removed ``DebugLine.add_unit()``
 * Fixed ``World.camera_screen_to_world()`` returning incorrect z-axis values on Windows/D3D.
+* ``print()`` will now try to detect the type of the lightuserdata and print it accordingly.
 
 0.42.0
 ------

+ 8 - 8
src/core/math/math.cpp

@@ -113,25 +113,25 @@ f32 catmull_rom(const f32 p0, const f32 p1, const f32 p2, const f32 p3, f32 t)
 	return tmp * 0.5f;
 }
 
-const char* to_string(const Vector3& v, char* buf, u32 buf_len)
+const char* to_string(char* buf, u32 buf_len, const Vector3& v)
 {
 	snprintf(buf, buf_len, "( %.4f, %.4f, %.4f )", v.x, v.y, v.z);
 	return buf;
 }
 
-const char* to_string(const Vector4& v, char* buf, u32 buf_len)
+const char* to_string(char* buf, u32 buf_len, const Vector4& v)
 {
 	snprintf(buf, buf_len, "( %.4f, %.4f, %.4f, %.4f )", v.x, v.y, v.z, v.w);
 	return buf;
 }
 
-const char* to_string(const Quaternion& q, char* buf, u32 buf_len)
+const char* to_string(char* buf, u32 buf_len, const Quaternion& q)
 {
 	snprintf(buf, buf_len, "( %.4f, %.4f, %.4f, %.4f )", q.x, q.y, q.z, q.w);
 	return buf;
 }
 
-const char* to_string(const Matrix4x4& m, char* buf, u32 buf_len)
+const char* to_string(char* buf, u32 buf_len, const Matrix4x4& m)
 {
 	char bufx[256];
 	char bufy[256];
@@ -139,10 +139,10 @@ const char* to_string(const Matrix4x4& m, char* buf, u32 buf_len)
 	char bufw[256];
 	snprintf(buf, buf_len,
 		"( %s, %s, %s, %s )"
-		, to_string(m.x, bufx, sizeof(bufx))
-		, to_string(m.y, bufy, sizeof(bufy))
-		, to_string(m.z, bufz, sizeof(bufz))
-		, to_string(m.t, bufw, sizeof(bufw))
+		, to_string(bufx, sizeof(bufx), m.x)
+		, to_string(bufy, sizeof(bufy), m.y)
+		, to_string(bufz, sizeof(bufz), m.z)
+		, to_string(bufw, sizeof(bufw), m.t)
 		);
 	return buf;
 }

+ 1 - 1
src/core/math/matrix4x4.inl

@@ -410,7 +410,7 @@ inline const f32* to_float_ptr(const Matrix4x4& m)
 /// Returns a string representing the matrix @m.
 /// @note This function is for debugging purposes only and doesn't
 /// output round-trip safe ASCII conversions. Do not use in production.
-const char* to_string(const Matrix4x4& m, char* buf, u32 buf_len);
+const char* to_string(char* buf, u32 buf_len, const Matrix4x4& m);
 
 /// @}
 

+ 1 - 1
src/core/math/quaternion.inl

@@ -212,7 +212,7 @@ inline Quaternion lerp(const Quaternion& a, const Quaternion& b, f32 t)
 /// Returns a string representing the quaternion @q.
 /// @note This function is for debugging purposes only and doesn't
 /// output round-trip safe ASCII conversions. Do not use in production.
-const char* to_string(const Quaternion& q, char* buf, u32 buf_len);
+const char* to_string(char* buf, u32 buf_len, const Quaternion& q);
 
 // @}
 

+ 1 - 1
src/core/math/vector3.inl

@@ -216,7 +216,7 @@ inline Vector2 to_vector2(const Vector3& a)
 /// Returns a string representing the vector @v.
 /// @note This function is for debugging purposes only and doesn't
 /// output round-trip safe ASCII conversions. Do not use in production.
-const char* to_string(const Vector3& v, char* buf, u32 buf_len);
+const char* to_string(char* buf, u32 buf_len, const Vector3& v);
 
 /// @}
 

+ 1 - 1
src/core/math/vector4.inl

@@ -219,7 +219,7 @@ inline Vector3 to_vector3(const Vector4& a)
 /// Returns a string representing the vector @v.
 /// @note This function is for debugging purposes only and doesn't
 /// output round-trip safe ASCII conversions. Do not use in production.
-const char* to_string(const Vector4& v, char* buf, u32 buf_len);
+const char* to_string(char* buf, u32 buf_len, const Vector4& v);
 
 /// @}
 

+ 60 - 3
src/lua/lua_api.cpp

@@ -186,7 +186,7 @@ static int quaternion_to_string(lua_State* L)
 {
 	LuaStack stack(L);
 	char buf[256];
-	stack.push_string(to_string(stack.get_quaternion(1), buf, sizeof(buf)));
+	stack.push_string(to_string(buf, sizeof(buf), stack.get_quaternion(1)));
 	return 1;
 }
 
@@ -760,7 +760,7 @@ void load_api(LuaEnvironment& env)
 		{
 			LuaStack stack(L);
 			char buf[256];
-			stack.push_string(to_string(stack.get_vector3(1), buf, sizeof(buf)));
+			stack.push_string(to_string(buf, sizeof(buf), stack.get_vector3(1)));
 			return 1;
 		});
 	env.add_module_metafunction("Vector3", "__call", [](lua_State* L)
@@ -962,7 +962,7 @@ void load_api(LuaEnvironment& env)
 		{
 			LuaStack stack(L);
 			char buf[1024];
-			stack.push_string(to_string(stack.get_matrix4x4(1), buf, sizeof(buf)));
+			stack.push_string(to_string(buf, sizeof(buf), stack.get_matrix4x4(1)));
 			return 1;
 		});
 	env.add_module_metafunction("Matrix4x4", "__call", [](lua_State* L)
@@ -1293,6 +1293,63 @@ void load_api(LuaEnvironment& env)
 
 			return 0;
 		});
+	env.add_module_metafunction("Lightuserdata_mt", "__tostring", [](lua_State* L)
+		{
+			LuaStack stack(L);
+			if (stack.is_vector3(1))
+			{
+				char buf[256];
+				to_string(buf, sizeof(buf), stack.get_vector3(1));
+				stack.push_fstring("Vector3: %s", buf);
+			}
+			else if (stack.is_quaternion(1))
+			{
+				char buf[256];
+				to_string(buf, sizeof(buf), stack.get_quaternion(1));
+				stack.push_fstring("Quaternion: %s", buf);
+			}
+			else if (stack.is_matrix4x4(1))
+			{
+				char buf[1024];
+				to_string(buf, sizeof(buf), stack.get_matrix4x4(1));
+				stack.push_fstring("Matrix4x4: %s", buf);
+			}
+			else if (stack.is_unit(1))
+			{
+				stack.push_fstring("UnitId: %u", stack.get_unit(1)._idx);
+			}
+			else
+			{
+				const void* ptr = stack.get_pointer(1);
+				const u32 marker = *(u32*)ptr;
+
+				if (marker == DEBUG_LINE_MARKER)
+					stack.push_fstring("DebugLine: %p", ptr);
+				else if (marker == DEBUG_GUI_MARKER)
+					stack.push_fstring("DebugGui: %p", ptr);
+				else if (marker == LEVEL_MARKER)
+					stack.push_fstring("Level: %p", ptr);
+				else if (marker == RENDER_WORLD_MARKER)
+					stack.push_fstring("RenderWorld: %p", ptr);
+				else if (marker == RESOURCE_PACKAGE_MARKER)
+					stack.push_fstring("ResourcePackage: %p", ptr);
+				else if (marker == SCENE_GRAPH_MARKER)
+					stack.push_fstring("SceneGraph: %p", ptr);
+				else if (marker == WORLD_MARKER)
+					stack.push_fstring("World: %p", ptr);
+				else if (marker == SCRIPT_WORLD_MARKER)
+					stack.push_fstring("ScriptWorld: %p", ptr);
+				else if (marker == SOUND_WORLD_MARKER)
+					stack.push_fstring("SoundWorld: %p", ptr);
+				else if (marker == PHYSICS_WORLD_MARKER)
+					stack.push_fstring("PhysicsWorld: %p", ptr);
+				else if (marker == ANIMATION_STATE_MACHINE_MARKER)
+					stack.push_fstring("AnimationStateMachine: %p", ptr);
+				else
+					stack.push_fstring("lightuserdata: %p", ptr);
+			}
+			return 1;
+		});
 
 #define KEYBOARD(name)                                                          \
 	[](lua_State* L)                                                            \

+ 3 - 0
src/lua/lua_stack.h

@@ -151,6 +151,9 @@ struct LuaStack
 	///
 	AnimationStateMachine* get_animation_state_machine(int i);
 
+	///
+	bool is_unit(int i);
+
 	///
 	UnitId get_unit(int i);
 

+ 6 - 0
src/lua/lua_stack.inl

@@ -237,6 +237,12 @@ inline AnimationStateMachine* LuaStack::get_animation_state_machine(int i)
 	return p;
 }
 
+inline bool LuaStack::is_unit(int i)
+{
+	uintptr_t enc = (uintptr_t)get_pointer(i);
+	return (enc & LIGHTDATA_TYPE_MASK) == LIGHTDATA_UNIT_MARKER;
+}
+
 inline UnitId LuaStack::get_unit(int i)
 {
 	uintptr_t enc = (uintptr_t)get_pointer(i);