diff --git a/test/testmodules/test1/client_lua/init.lua b/test/testmodules/test1/client_lua/init.lua index e249ebe..daca6e3 100644 --- a/test/testmodules/test1/client_lua/init.lua +++ b/test/testmodules/test1/client_lua/init.lua @@ -24,16 +24,20 @@ local cereal = require("buildat/extension/cereal") local the_box = nil buildat.sub_packet("test1:add_box", function(data) - values = cereal.binary_input(data, { - "double", "double", "double", - "double", "double", "double" + local values = cereal.binary_input(data, {"object", + {"w", "double"}, + {"h", "double"}, + {"d", "double"}, + {"x", "double"}, + {"y", "double"}, + {"z", "double"}, }) - local w = values[1] - local h = values[2] - local d = values[3] - local x = values[4] - local y = values[5] - local z = values[6] + local w = values.w + local h = values.h + local d = values.d + local x = values.x + local y = values.y + local z = values.z log:info("values="..dump(values)) box = g3d.ScenePrimitive(g3d.ScenePrimitive.TYPE_BOX, w,h,d) box:loadTexture("test1/pink_texture.png") @@ -41,11 +45,39 @@ buildat.sub_packet("test1:add_box", function(data) scene:addEntity(box) the_box = box - data = cereal.binary_output( - {1, "Foo", "Bar"}, - {"byte", {"string", 2}} - ) + values = { + a = 128, + b = 1000, + c = 3.14, + d = "Foo", + e = {"Bar1", "Bar2"}, + f = {x=1, y=2}, + } + data = cereal.binary_output(values, {"object", + {"a", "byte"}, + {"b", "int32_t"}, + {"c", "double"}, + {"d", "string"}, + {"e", {"array", "string"}}, + {"f", {"object", {"x", "int32_t"}, {"y", "int32_t"}}} + }) buildat.send_packet("test1:box_added", data) + + -- Try deserializing it too and see if all is working + values = cereal.binary_input(data, {"object", + {"a", "byte"}, + {"b", "int32_t"}, + {"c", "double"}, + {"d", "string"}, + {"e", {"array", "string"}}, + {"f", {"object", {"x", "int32_t"}, {"y", "int32_t"}}} + }) + assert(values.a == 128) + assert(values.b == 1000) + assert(values.c == 3.14) + assert(values.d == "Foo") + assert(values.e[2] == "Bar2") + assert(values.f.y == 2) end) local keyinput = require("buildat/extension/keyinput") @@ -73,14 +105,10 @@ mouseinput.sub_up(function(button, x, y) end) buildat.sub_packet("test1:array", function(data) - values = cereal.binary_input(data, {"int32"}) - values = cereal.binary_input(data, {"int32", {"int32", values[1]}}) - log:info("test1:array: "..dump(values)) + local array = cereal.binary_input(data, {"array", "int32_t"}) + log:info("test1:array: "..dump(array)) - data = cereal.binary_output( - values, - {"int32", {"int32", values[1]}} - ) + data = cereal.binary_output(array, {"array", "int32_t"}) buildat.send_packet("test1:array_response", data) end) diff --git a/test/testmodules/test1/test1.cpp b/test/testmodules/test1/test1.cpp index 2fcb9ba..0f59efc 100644 --- a/test/testmodules/test1/test1.cpp +++ b/test/testmodules/test1/test1.cpp @@ -6,6 +6,7 @@ #include "network/api.h" #include "core/log.h" #include +#include using interface::Event; @@ -91,7 +92,8 @@ struct Module: public interface::Module std::ostringstream os(std::ios::binary); { cereal::PortableBinaryOutputArchive ar(os); - ar((int32_t)5, (int32_t)1, (int32_t)4, (int32_t)6, (int32_t)8, (int32_t)10); + sv_ array = {1, 2, 3, 4, 5}; + ar(array); } inetwork->send(event.recipient, "test1:array", os.str()); });