mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-06 00:55:50 +00:00
Improve error messages in components/lua/serialization.cpp
This commit is contained in:
parent
09705260e8
commit
3ce5e9e680
@ -193,9 +193,9 @@ namespace
|
|||||||
table["y"] = TestStruct2{4, 3};
|
table["y"] = TestStruct2{4, 3};
|
||||||
TestSerializer serializer;
|
TestSerializer serializer;
|
||||||
|
|
||||||
EXPECT_ERROR(LuaUtil::serialize(table), "Unknown userdata");
|
EXPECT_ERROR(LuaUtil::serialize(table), "Value is not serializable.");
|
||||||
std::string serialized = LuaUtil::serialize(table, &serializer);
|
std::string serialized = LuaUtil::serialize(table, &serializer);
|
||||||
EXPECT_ERROR(LuaUtil::deserialize(lua, serialized), "Unknown type:");
|
EXPECT_ERROR(LuaUtil::deserialize(lua, serialized), "Unknown type in serialized data:");
|
||||||
sol::table res = LuaUtil::deserialize(lua, serialized, &serializer);
|
sol::table res = LuaUtil::deserialize(lua, serialized, &serializer);
|
||||||
|
|
||||||
TestStruct1 rx = res.get<TestStruct1>("x");
|
TestStruct1 rx = res.get<TestStruct1>("x");
|
||||||
|
@ -43,7 +43,7 @@ namespace LuaUtil
|
|||||||
static T getValue(std::string_view& binaryData)
|
static T getValue(std::string_view& binaryData)
|
||||||
{
|
{
|
||||||
if (binaryData.size() < sizeof(T))
|
if (binaryData.size() < sizeof(T))
|
||||||
throw std::runtime_error("Unexpected end");
|
throw std::runtime_error("Unexpected end of serialized data.");
|
||||||
T v;
|
T v;
|
||||||
std::memcpy(&v, binaryData.data(), sizeof(T));
|
std::memcpy(&v, binaryData.data(), sizeof(T));
|
||||||
binaryData = binaryData.substr(sizeof(T));
|
binaryData = binaryData.substr(sizeof(T));
|
||||||
@ -107,15 +107,15 @@ namespace LuaUtil
|
|||||||
if (customSerializer && customSerializer->serialize(out, data))
|
if (customSerializer && customSerializer->serialize(out, data))
|
||||||
return;
|
return;
|
||||||
else
|
else
|
||||||
throw std::runtime_error("Unknown userdata");
|
throw std::runtime_error("Value is not serializable.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void serialize(BinaryData& out, const sol::object& obj, const UserdataSerializer* customSerializer, int recursionCounter)
|
static void serialize(BinaryData& out, const sol::object& obj, const UserdataSerializer* customSerializer, int recursionCounter)
|
||||||
{
|
{
|
||||||
if (obj.get_type() == sol::type::lightuserdata)
|
if (obj.get_type() == sol::type::lightuserdata)
|
||||||
throw std::runtime_error("light userdata is not allowed to be serialized");
|
throw std::runtime_error("Light userdata is not allowed to be serialized.");
|
||||||
if (obj.is<sol::function>())
|
if (obj.is<sol::function>())
|
||||||
throw std::runtime_error("functions are not allowed to be serialized");
|
throw std::runtime_error("Functions are not allowed to be serialized.");
|
||||||
else if (obj.is<sol::userdata>())
|
else if (obj.is<sol::userdata>())
|
||||||
serializeUserdata(out, obj, customSerializer);
|
serializeUserdata(out, obj, customSerializer);
|
||||||
else if (obj.is<sol::lua_table>())
|
else if (obj.is<sol::lua_table>())
|
||||||
@ -144,13 +144,13 @@ namespace LuaUtil
|
|||||||
appendType(out, SerializedType::BOOLEAN);
|
appendType(out, SerializedType::BOOLEAN);
|
||||||
out.push_back(v);
|
out.push_back(v);
|
||||||
} else
|
} else
|
||||||
throw std::runtime_error("Unknown lua type");
|
throw std::runtime_error("Unknown Lua type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deserializeImpl(sol::state& lua, std::string_view& binaryData, const UserdataSerializer* customSerializer)
|
static void deserializeImpl(sol::state& lua, std::string_view& binaryData, const UserdataSerializer* customSerializer)
|
||||||
{
|
{
|
||||||
if (binaryData.empty())
|
if (binaryData.empty())
|
||||||
throw std::runtime_error("Unexpected end");
|
throw std::runtime_error("Unexpected end of serialized data.");
|
||||||
unsigned char type = binaryData[0];
|
unsigned char type = binaryData[0];
|
||||||
binaryData = binaryData.substr(1);
|
binaryData = binaryData.substr(1);
|
||||||
if (type & (CUSTOM_COMPACT_FLAG | CUSTOM_FULL_FLAG))
|
if (type & (CUSTOM_COMPACT_FLAG | CUSTOM_FULL_FLAG))
|
||||||
@ -170,7 +170,7 @@ namespace LuaUtil
|
|||||||
std::string_view data = binaryData.substr(typeNameSize, dataSize);
|
std::string_view data = binaryData.substr(typeNameSize, dataSize);
|
||||||
binaryData = binaryData.substr(typeNameSize + dataSize);
|
binaryData = binaryData.substr(typeNameSize + dataSize);
|
||||||
if (!customSerializer || !customSerializer->deserialize(typeName, data, lua))
|
if (!customSerializer || !customSerializer->deserialize(typeName, data, lua))
|
||||||
throw std::runtime_error("Unknown type: " + std::string(typeName));
|
throw std::runtime_error("Unknown type in serialized data: " + std::string(typeName));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (type & SHORT_STRING_FLAG)
|
if (type & SHORT_STRING_FLAG)
|
||||||
@ -205,12 +205,12 @@ namespace LuaUtil
|
|||||||
lua_settable(lua, -3);
|
lua_settable(lua, -3);
|
||||||
}
|
}
|
||||||
if (binaryData.empty())
|
if (binaryData.empty())
|
||||||
throw std::runtime_error("Unexpected end");
|
throw std::runtime_error("Unexpected end of serialized data.");
|
||||||
binaryData = binaryData.substr(1);
|
binaryData = binaryData.substr(1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case SerializedType::TABLE_END:
|
case SerializedType::TABLE_END:
|
||||||
throw std::runtime_error("Unexpected table end");
|
throw std::runtime_error("Unexpected end of table during deserialization.");
|
||||||
case SerializedType::VEC2:
|
case SerializedType::VEC2:
|
||||||
{
|
{
|
||||||
float x = getValue<float>(binaryData);
|
float x = getValue<float>(binaryData);
|
||||||
@ -227,7 +227,7 @@ namespace LuaUtil
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw std::runtime_error("Unknown type: " + std::to_string(type));
|
throw std::runtime_error("Unknown type in serialized data: " + std::to_string(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
BinaryData serialize(const sol::object& obj, const UserdataSerializer* customSerializer)
|
BinaryData serialize(const sol::object& obj, const UserdataSerializer* customSerializer)
|
||||||
|
Loading…
Reference in New Issue
Block a user