1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-21 00:19:29 +02:00

Fixed deserialization of new artifacts (and possibly some other objects)

Was broken in my previous PR, since pointer graph serialization was
enabled by default, leading to deserializationFix triggering on netpack
apply.

Cleaned up / clarified code
This commit is contained in:
Ivan Savenko
2024-07-27 16:51:23 +00:00
parent dac18ae35a
commit 48c92711f2
9 changed files with 15 additions and 26 deletions

View File

@ -53,7 +53,7 @@ public:
JsonNode toJsonNode() const; JsonNode toJsonNode() const;
}; };
#define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.smartPointerSerialization) deserializationFix(); #define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.loadingGamestate) deserializationFix();
/// Struct for handling bonuses of several types. Can be transferred to any hero /// Struct for handling bonuses of several types. Can be transferred to any hero
struct DLL_LINKAGE Bonus : public std::enable_shared_from_this<Bonus>, public Serializeable struct DLL_LINKAGE Bonus : public std::enable_shared_from_this<Bonus>, public Serializeable

View File

@ -155,9 +155,13 @@ struct DLL_LINKAGE LobbyStartGame : public CLobbyPackToPropagate
template <typename Handler> void serialize(Handler &h) template <typename Handler> void serialize(Handler &h)
{ {
if (!h.saving)
h.loadingGamestate = true;
h & clientId; h & clientId;
h & initializedStartInfo; h & initializedStartInfo;
h & initializedGameState; h & initializedGameState;
if (!h.saving)
h.loadingGamestate = false;
} }
}; };

View File

@ -15,9 +15,7 @@ VCMI_LIB_NAMESPACE_BEGIN
BinaryDeserializer::BinaryDeserializer(IBinaryReader * r): CLoaderBase(r) BinaryDeserializer::BinaryDeserializer(IBinaryReader * r): CLoaderBase(r)
{ {
saving = false;
version = Version::NONE; version = Version::NONE;
smartPointerSerialization = true;
reverseEndianness = false; reverseEndianness = false;
registerTypes(*this); registerTypes(*this);

View File

@ -165,8 +165,9 @@ public:
std::map<uint32_t, Serializeable*> loadedPointers; std::map<uint32_t, Serializeable*> loadedPointers;
std::map<const Serializeable*, std::shared_ptr<Serializeable>> loadedSharedPointers; std::map<const Serializeable*, std::shared_ptr<Serializeable>> loadedSharedPointers;
IGameCallback * cb = nullptr; IGameCallback * cb = nullptr;
bool smartPointerSerialization; static constexpr bool trackSerializedPointers = true;
bool saving; static constexpr bool saving = false;
bool loadingGamestate = false;
bool hasFeature(Version what) const bool hasFeature(Version what) const
{ {
@ -342,7 +343,7 @@ public:
} }
uint32_t pid = 0xffffffff; //pointer id (or maybe rather pointee id) uint32_t pid = 0xffffffff; //pointer id (or maybe rather pointee id)
if(smartPointerSerialization) if(trackSerializedPointers)
{ {
load( pid ); //get the id load( pid ); //get the id
auto i = loadedPointers.find(pid); //lookup auto i = loadedPointers.find(pid); //lookup
@ -383,7 +384,7 @@ public:
template <typename T> template <typename T>
void ptrAllocated(T *ptr, uint32_t pid) void ptrAllocated(T *ptr, uint32_t pid)
{ {
if(smartPointerSerialization && pid != 0xffffffff) if(trackSerializedPointers && pid != 0xffffffff)
loadedPointers[pid] = const_cast<Serializeable*>(dynamic_cast<const Serializeable*>(ptr)); //add loaded pointer to our lookup map; cast is to avoid errors with const T* pt loadedPointers[pid] = const_cast<Serializeable*>(dynamic_cast<const Serializeable*>(ptr)); //add loaded pointer to our lookup map; cast is to avoid errors with const T* pt
} }

View File

@ -15,8 +15,6 @@ VCMI_LIB_NAMESPACE_BEGIN
BinarySerializer::BinarySerializer(IBinaryWriter * w): CSaverBase(w) BinarySerializer::BinarySerializer(IBinaryWriter * w): CSaverBase(w)
{ {
saving=true;
smartPointerSerialization = true;
registerTypes(*this); registerTypes(*this);
} }

View File

@ -118,8 +118,9 @@ public:
std::map<const Serializeable*, uint32_t> savedPointers; std::map<const Serializeable*, uint32_t> savedPointers;
Version version = Version::CURRENT; Version version = Version::CURRENT;
bool smartPointerSerialization; static constexpr bool trackSerializedPointers = true;
bool saving; static constexpr bool saving = true;
bool loadingGamestate = false;
bool hasFeature(Version what) const bool hasFeature(Version what) const
{ {
@ -257,7 +258,7 @@ public:
return; return;
} }
if(smartPointerSerialization) if(trackSerializedPointers)
{ {
// We might have an object that has multiple inheritance and store it via the non-first base pointer. // We might have an object that has multiple inheritance and store it via the non-first base pointer.
// Therefore, all pointers need to be normalized to the actual object address. // Therefore, all pointers need to be normalized to the actual object address.

View File

@ -29,6 +29,7 @@ int CLoadFile::read(std::byte * data, unsigned size)
void CLoadFile::openNextFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion) void CLoadFile::openNextFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion)
{ {
serializer.loadingGamestate = true;
assert(!serializer.reverseEndianness); assert(!serializer.reverseEndianness);
assert(minimalVersion <= ESerializationVersion::CURRENT); assert(minimalVersion <= ESerializationVersion::CURRENT);

View File

@ -151,18 +151,6 @@ void CConnection::enterGameplayConnectionMode(CGameState * gs)
enableSmartVectorMemberSerializatoin(gs); enableSmartVectorMemberSerializatoin(gs);
} }
void CConnection::disableSmartPointerSerialization()
{
deserializer->smartPointerSerialization = false;
serializer->smartPointerSerialization = false;
}
void CConnection::enableSmartPointerSerialization()
{
deserializer->smartPointerSerialization = true;
serializer->smartPointerSerialization = true;
}
void CConnection::disableSmartVectorMemberSerialization() void CConnection::disableSmartVectorMemberSerialization()
{ {
packReader->smartVectorMembersSerialization = false; packReader->smartVectorMembersSerialization = false;

View File

@ -38,8 +38,6 @@ class DLL_LINKAGE CConnection : boost::noncopyable
void disableStackSendingByID(); void disableStackSendingByID();
void enableStackSendingByID(); void enableStackSendingByID();
void disableSmartPointerSerialization();
void enableSmartPointerSerialization();
void disableSmartVectorMemberSerialization(); void disableSmartVectorMemberSerialization();
void enableSmartVectorMemberSerializatoin(CGameState * gs); void enableSmartVectorMemberSerializatoin(CGameState * gs);