Parcourir la source

Add Device.console_send()

Daniele Bartolini il y a 11 ans
Parent
commit
82f7c1609e
2 fichiers modifiés avec 40 ajouts et 1 suppressions
  1. 30 0
      engine/lua/lua_device.cpp
  2. 10 1
      engine/lua/lua_stack.h

+ 30 - 0
engine/lua/lua_device.cpp

@@ -32,6 +32,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "lua_stack.h"
 #include "temp_allocator.h"
 #include "array.h"
+#include "string_stream.h"
+#include "console_server.h"
 
 namespace crown
 {
@@ -127,6 +129,33 @@ static int device_destroy_resource_package(lua_State* L)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
+static int device_console_send(lua_State* L)
+{
+	using namespace string_stream;
+	LuaStack stack(L);
+
+	TempAllocator1024 alloc;
+	StringStream json(alloc);
+
+	json << "{";
+	/* table is in the stack at index 'i' */
+	stack.push_nil();  /* first key */
+	while (stack.next(1) != 0)
+	{
+		/* uses 'key' (at index -2) and 'value' (at index -1) */
+		json << "\"" << stack.get_string(-2) << "\":\"" << stack.get_string(-1) << "\",";
+		/* removes 'value'; keeps 'key' for next iteration */
+		stack.pop(1);
+	}
+	/* pop key */
+	stack.pop(1);
+	json << "}";
+
+	console_server_globals::console().send_to_all(c_str(json));
+	return 0;
+}
+
 //-----------------------------------------------------------------------------
 void load_device(LuaEnvironment& env)
 {
@@ -141,6 +170,7 @@ void load_device(LuaEnvironment& env)
 	env.load_module_function("Device", "render_world",             device_render_world);
 	env.load_module_function("Device", "create_resource_package",  device_create_resource_package);
 	env.load_module_function("Device", "destroy_resource_package", device_destroy_resource_package);
+	env.load_module_function("Device", "console_send",             device_console_send);
 }
 
 } // namespace crown

+ 10 - 1
engine/lua/lua_stack.h

@@ -35,7 +35,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "matrix4x4.h"
 #include "string_utils.h"
 #include "color4.h"
-#include "string_stream.h"
 
 //-----------------------------------------------------------------------------
 #if defined(CROWN_DEBUG)
@@ -126,6 +125,11 @@ struct LuaStack
 		lua_remove(_L, index);
 	}
 
+	/// Pops @a n elements from the stack.
+	void pop(int32_t n)
+	{
+		lua_pop(_L, n);
+	}
 	//-----------------------------------------------------------------------------
 	bool is_nil(int32_t index)
 	{
@@ -251,6 +255,11 @@ struct LuaStack
 		lua_settable(_L, -3);
 	}
 
+	int next(int32_t i)
+	{
+		return lua_next(_L, i);
+	}
+
 	//-----------------------------------------------------------------------------
 	void push_resource_package(ResourcePackage* package)
 	{