1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-18 04:10:06 +00:00
OpenMW/apps/openmw/mwlua/userdataserializer.cpp

64 lines
2.3 KiB
C++
Raw Normal View History

2020-12-18 22:21:10 +00:00
#include "userdataserializer.hpp"
#include <components/lua/serialization.hpp>
#include <components/misc/endianness.hpp>
#include "object.hpp"
namespace MWLua
{
class Serializer final : public LuaUtil::UserdataSerializer
{
public:
explicit Serializer(bool localSerializer, ObjectRegistry* registry, std::map<int, int>* contentFileMapping)
: mLocalSerializer(localSerializer), mObjectRegistry(registry), mContentFileMapping(contentFileMapping) {}
2020-12-18 22:21:10 +00:00
private:
// Appends serialized sol::userdata to the end of BinaryData.
// Returns false if this type of userdata is not supported by this serializer.
bool serialize(LuaUtil::BinaryData& out, const sol::userdata& data) const override
{
if (data.is<GObject>() || data.is<LObject>())
{
2022-05-20 19:47:13 +00:00
appendRefNum(out, data.as<Object>().id());
2020-12-18 22:21:10 +00:00
return true;
}
return false;
}
// Deserializes userdata of type "typeName" from binaryData. Should push the result on stack using sol::stack::push.
// Returns false if this type is not supported by this serializer.
bool deserialize(std::string_view typeName, std::string_view binaryData, lua_State* lua) const override
2020-12-18 22:21:10 +00:00
{
2022-05-20 19:47:13 +00:00
if (typeName == sRefNumTypeName)
2020-12-18 22:21:10 +00:00
{
2022-05-20 19:47:13 +00:00
ObjectId id = loadRefNum(binaryData);
if (id.hasContentFile() && mContentFileMapping)
{
auto iter = mContentFileMapping->find(id.mContentFile);
if (iter != mContentFileMapping->end())
id.mContentFile = iter->second;
}
2020-12-18 22:21:10 +00:00
if (mLocalSerializer)
sol::stack::push<LObject>(lua, LObject(id, mObjectRegistry));
else
sol::stack::push<GObject>(lua, GObject(id, mObjectRegistry));
return true;
}
return false;
}
bool mLocalSerializer;
ObjectRegistry* mObjectRegistry;
std::map<int, int>* mContentFileMapping;
2020-12-18 22:21:10 +00:00
};
std::unique_ptr<LuaUtil::UserdataSerializer> createUserdataSerializer(
bool local, ObjectRegistry* registry, std::map<int, int>* contentFileMapping)
2020-12-18 22:21:10 +00:00
{
return std::make_unique<Serializer>(local, registry, contentFileMapping);
2020-12-18 22:21:10 +00:00
}
}