diff options
author | Josquin Frei <josquin@genevastage.ch> | 2022-01-10 12:42:03 +0000 |
---|---|---|
committer | psi29a <psi29a@gmail.com> | 2022-01-10 12:42:03 +0000 |
commit | d9672f7d468338cb2e96e775b8494dc85131a546 (patch) | |
tree | 7d15065a3344b432c9fa65d3c66a2c47b69385e1 | |
parent | a1f8db760070b3b4b3b36f7765c8069c946f3413 (diff) |
Add serialization for TransformM and TransformQ
-rw-r--r-- | AUTHORS.md | 1 | ||||
-rw-r--r-- | apps/openmw_test_suite/lua/test_serialization.cpp | 30 | ||||
-rw-r--r-- | components/lua/serialization.cpp | 40 |
3 files changed, 71 insertions, 0 deletions
diff --git a/AUTHORS.md b/AUTHORS.md index 67be756f33..2ddaa03cf6 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -115,6 +115,7 @@ Programmers John Blomberg (fstp) Jordan Ayers Jordan Milne + Josquin Frei Josua Grawitter Jules Blok (Armada651) julianko diff --git a/apps/openmw_test_suite/lua/test_serialization.cpp b/apps/openmw_test_suite/lua/test_serialization.cpp index 9300e4571b..57a1070c83 100644 --- a/apps/openmw_test_suite/lua/test_serialization.cpp +++ b/apps/openmw_test_suite/lua/test_serialization.cpp @@ -1,10 +1,13 @@ #include "gmock/gmock.h" #include <gtest/gtest.h> +#include <osg/Matrixf> +#include <osg/Quat> #include <osg/Vec2f> #include <osg/Vec3f> #include <components/lua/serialization.hpp> +#include <components/lua/utilpackage.hpp> #include <components/misc/endianness.hpp> @@ -104,6 +107,33 @@ namespace } } + TEST(LuaSerializationTest, Transform) { + sol::state lua; + osg::Matrixf matrix(1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16); + LuaUtil::TransformM transM = LuaUtil::asTransform(matrix); + osg::Quat quat(1, 2, 3, 4); + LuaUtil::TransformQ transQ = LuaUtil::asTransform(quat); + + { + std::string serialized = LuaUtil::serialize(sol::make_object(lua, transM)); + EXPECT_EQ(serialized.size(), 130); // version, type, 16x double + sol::object value = LuaUtil::deserialize(lua, serialized); + ASSERT_TRUE(value.is<LuaUtil::TransformM>()); + EXPECT_EQ(value.as<LuaUtil::TransformM>().mM, transM.mM); + } + { + std::string serialized = LuaUtil::serialize(sol::make_object(lua, transQ)); + EXPECT_EQ(serialized.size(), 34); // version, type, 4x double + sol::object value = LuaUtil::deserialize(lua, serialized); + ASSERT_TRUE(value.is<LuaUtil::TransformQ>()); + EXPECT_EQ(value.as<LuaUtil::TransformQ>().mQ, transQ.mQ); + } + + } + TEST(LuaSerializationTest, Table) { sol::state lua; diff --git a/components/lua/serialization.cpp b/components/lua/serialization.cpp index f9b8951e4e..8b18294449 100644 --- a/components/lua/serialization.cpp +++ b/components/lua/serialization.cpp @@ -1,11 +1,15 @@ #include "serialization.hpp" +#include <osg/Matrixf> +#include <osg/Quat> #include <osg/Vec2f> #include <osg/Vec3f> +#include <osg/Vec4f> #include <components/misc/endianness.hpp> #include "luastate.hpp" +#include "utilpackage.hpp" namespace LuaUtil { @@ -22,6 +26,8 @@ namespace LuaUtil VEC2 = 0x10, VEC3 = 0x11, + TRANSFORM_M = 0x12, + TRANSFORM_Q = 0x13, // All values should be lesser than 0x20 (SHORT_STRING_FLAG). }; @@ -106,6 +112,23 @@ namespace LuaUtil appendValue<float>(out, v.z()); return; } + if (data.is<TransformM>()) + { + appendType(out, SerializedType::TRANSFORM_M); + osg::Matrixf matrix = data.as<TransformM>().mM; + for (size_t i = 0; i < 4; i++) + for (size_t j = 0; j < 4; j++) + appendValue<double>(out, matrix(i,j)); + return; + } + if (data.is<TransformQ>()) + { + appendType(out, SerializedType::TRANSFORM_Q); + osg::Quat quat = data.as<TransformQ>().mQ; + for(size_t i = 0; i < 4; i++) + appendValue<double>(out, quat[i]); + return; + } if (customSerializer && customSerializer->serialize(out, data)) return; else @@ -231,6 +254,23 @@ namespace LuaUtil sol::stack::push<osg::Vec3f>(lua, osg::Vec3f(x, y, z)); return; } + case SerializedType::TRANSFORM_M: + { + osg::Matrixf mat; + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + mat(i, j) = getValue<double>(binaryData); + sol::stack::push<TransformM>(lua, asTransform(mat)); + return; + } + case SerializedType::TRANSFORM_Q: + { + osg::Quat q; + for (int i = 0; i < 4; i++) + q[i] = getValue<double>(binaryData); + sol::stack::push<TransformQ>(lua, asTransform(q)); + return; + } } throw std::runtime_error("Unknown type in serialized data: " + std::to_string(type)); } |