Rework Serialization functions
add name and handle more types
This commit is contained in:
188
src/Nazara/Core/BinarySerialization.cpp
Normal file
188
src/Nazara/Core/BinarySerialization.cpp
Normal file
@@ -0,0 +1,188 @@
|
||||
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Export.hpp
|
||||
|
||||
#include <Nazara/Core/BinarySerialization.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \brief Reset the current read bit cursor
|
||||
*/
|
||||
void BinarySerializationContext::ResetReadBitPosition()
|
||||
{
|
||||
readBitPos = 8;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Reset the current read bit cursor
|
||||
|
||||
* \remark This function only reset the cursor position, it doesn't do any writing
|
||||
if you wish to write all bits and reset bit position, call FlushBits
|
||||
|
||||
\see FlushBits
|
||||
*/
|
||||
void BinarySerializationContext::ResetWriteBitPosition()
|
||||
{
|
||||
writeBitPos = 8;
|
||||
}
|
||||
|
||||
bool BinarySerializationContext::PushObject(std::string_view name)
|
||||
{
|
||||
// do nothing
|
||||
NazaraUnused(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BinarySerializationContext::PopObject()
|
||||
{
|
||||
// do nothing
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BinarySerializationContext::PushArray(std::string_view name)
|
||||
{
|
||||
// do nothing
|
||||
NazaraUnused(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BinarySerializationContext::PopArray()
|
||||
{
|
||||
// do nothing
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup core
|
||||
* \class Nz::SerializationContext
|
||||
* \brief Structure containing a serialization/unserialization context states
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Write bits to the stream (if any) and reset the current bit cursor
|
||||
|
||||
* \see ResetWriteBitPosition
|
||||
*/
|
||||
void BinarySerializationContext::FlushBits()
|
||||
{
|
||||
if (writeBitPos != 8)
|
||||
{
|
||||
ResetWriteBitPosition();
|
||||
|
||||
// Serialize will reset the bit position
|
||||
if (!Serialize(*this, "", writeByte))
|
||||
NazaraWarning("Failed to flush bits");
|
||||
}
|
||||
}
|
||||
|
||||
bool BinarySerializationContext::Write(std::string_view name, bool value)
|
||||
{
|
||||
if (writeBitPos == 8)
|
||||
{
|
||||
writeBitPos = 0;
|
||||
writeByte = 0;
|
||||
}
|
||||
|
||||
if (value)
|
||||
writeByte |= 1 << writeBitPos;
|
||||
|
||||
if (++writeBitPos >= 8)
|
||||
return Serialize(*this, name, writeByte, TypeTag<UInt8>());
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
#define WRITE_NUMERIC(Type) \
|
||||
bool BinarySerializationContext::Write(std::string_view name, Type value) \
|
||||
{ \
|
||||
NazaraUnused(name); \
|
||||
/* Flush bits in case a writing is in progress */ \
|
||||
FlushBits(); \
|
||||
\
|
||||
if (endianness != Endianness::Unknown && endianness != PlatformEndianness) \
|
||||
value = ByteSwap(value); \
|
||||
\
|
||||
return stream->Write(&value, sizeof(Type)) == sizeof(Type); \
|
||||
}
|
||||
|
||||
WRITE_NUMERIC(Nz::Int8);
|
||||
WRITE_NUMERIC(Nz::Int16);
|
||||
WRITE_NUMERIC(Nz::Int32);
|
||||
WRITE_NUMERIC(Nz::Int64);
|
||||
WRITE_NUMERIC(Nz::UInt8);
|
||||
WRITE_NUMERIC(Nz::UInt16);
|
||||
WRITE_NUMERIC(Nz::UInt32);
|
||||
WRITE_NUMERIC(Nz::UInt64);
|
||||
WRITE_NUMERIC(float);
|
||||
WRITE_NUMERIC(double);
|
||||
|
||||
#undef WRITE_NUMERIC
|
||||
|
||||
bool BinarySerializationContext::Write(std::string_view name, const std::string& value)
|
||||
{
|
||||
NazaraUnused(name);
|
||||
if (!Serialize(*this, "", SafeCast<UInt32>(value.size()), TypeTag<UInt32>()))
|
||||
return false;
|
||||
|
||||
return stream->Write(value.data(), value.size()) == value.size();
|
||||
}
|
||||
|
||||
bool BinarySerializationContext::Read(std::string_view name, bool* value)
|
||||
{
|
||||
if (readBitPos == 8)
|
||||
{
|
||||
if (!Unserialize(*this, name, &readByte, TypeTag<UInt8>()))
|
||||
return false;
|
||||
|
||||
readBitPos = 0;
|
||||
}
|
||||
|
||||
if (value)
|
||||
*value = (readByte & (1 << readBitPos)) != 0;
|
||||
|
||||
readBitPos++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#define READ_NUMERIC(Type) \
|
||||
bool BinarySerializationContext::Read(std::string_view name, Type* value) \
|
||||
{ \
|
||||
NazaraUnused(name); \
|
||||
NazaraAssert(value, "Invalid data pointer"); \
|
||||
ResetReadBitPosition(); \
|
||||
if (stream->Read(value, sizeof(Type)) == sizeof(Type)) \
|
||||
{ \
|
||||
if (endianness != Endianness::Unknown && endianness != PlatformEndianness) \
|
||||
*value = ByteSwap(*value); \
|
||||
return true; \
|
||||
} \
|
||||
return false; \
|
||||
}
|
||||
|
||||
READ_NUMERIC(Nz::Int8);
|
||||
READ_NUMERIC(Nz::Int16);
|
||||
READ_NUMERIC(Nz::Int32);
|
||||
READ_NUMERIC(Nz::Int64);
|
||||
READ_NUMERIC(Nz::UInt8);
|
||||
READ_NUMERIC(Nz::UInt16);
|
||||
READ_NUMERIC(Nz::UInt32);
|
||||
READ_NUMERIC(Nz::UInt64);
|
||||
READ_NUMERIC(float);
|
||||
READ_NUMERIC(double);
|
||||
|
||||
#undef READ_NUMERIC
|
||||
|
||||
bool BinarySerializationContext::Read(std::string_view name, std::string* value)
|
||||
{
|
||||
NazaraUnused(name);
|
||||
UInt32 size;
|
||||
if (!Unserialize(*this, "", &size, TypeTag<UInt32>()))
|
||||
return false;
|
||||
|
||||
value->resize(size);
|
||||
return stream->Read(&(*value)[0], size) == size;
|
||||
}
|
||||
}
|
||||
@@ -8,37 +8,30 @@ namespace Nz
|
||||
{
|
||||
bool Unserialize(SerializationContext& context, DDSHeader* header)
|
||||
{
|
||||
if (!Unserialize(context, &header->size))
|
||||
if (!Unserialize(context, "size", &header->size))
|
||||
return false;
|
||||
if (!Unserialize(context, &header->flags))
|
||||
if (!Unserialize(context, "flags", &header->flags))
|
||||
return false;
|
||||
if (!Unserialize(context, &header->height))
|
||||
if (!Unserialize(context, "height", &header->height))
|
||||
return false;
|
||||
if (!Unserialize(context, &header->width))
|
||||
if (!Unserialize(context, "width", &header->width))
|
||||
return false;
|
||||
if (!Unserialize(context, &header->pitch))
|
||||
if (!Unserialize(context, "pitch", &header->pitch))
|
||||
return false;
|
||||
if (!Unserialize(context, &header->depth))
|
||||
if (!Unserialize(context, "depth", &header->depth))
|
||||
return false;
|
||||
if (!Unserialize(context, &header->levelCount))
|
||||
if (!Unserialize(context, "levels", &header->levelCount))
|
||||
return false;
|
||||
if (!Unserialize(context, "reserved1", &header->reserved1))
|
||||
return false;
|
||||
|
||||
for (unsigned int i = 0; i < CountOf(header->reserved1); ++i)
|
||||
{
|
||||
if (!Unserialize(context, &header->reserved1[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Unserialize(context, &header->format))
|
||||
if (!Unserialize(context, "format", &header->format))
|
||||
return false;
|
||||
|
||||
for (unsigned int i = 0; i < CountOf(header->ddsCaps); ++i)
|
||||
{
|
||||
if (!Unserialize(context, &header->ddsCaps[i]))
|
||||
return false;
|
||||
}
|
||||
if (!Unserialize(context, "caps", &header->ddsCaps))
|
||||
return false;
|
||||
|
||||
if (!Unserialize(context, &header->reserved2))
|
||||
if (!Unserialize(context, "reserved2", &header->reserved2))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -48,19 +41,19 @@ namespace Nz
|
||||
{
|
||||
UInt32 enumValue;
|
||||
|
||||
if (!Unserialize(context, &enumValue))
|
||||
if (!Unserialize(context, "format", &enumValue))
|
||||
return false;
|
||||
header->dxgiFormat = static_cast<DXGI_FORMAT>(enumValue);
|
||||
|
||||
if (!Unserialize(context, &enumValue))
|
||||
if (!Unserialize(context, "dimension", &enumValue))
|
||||
return false;
|
||||
header->resourceDimension = static_cast<D3D10_RESOURCE_DIMENSION>(enumValue);
|
||||
|
||||
if (!Unserialize(context, &header->miscFlag))
|
||||
if (!Unserialize(context, "miscFlag", &header->miscFlag))
|
||||
return false;
|
||||
if (!Unserialize(context, &header->arraySize))
|
||||
if (!Unserialize(context, "size", &header->arraySize))
|
||||
return false;
|
||||
if (!Unserialize(context, &header->reserved))
|
||||
if (!Unserialize(context, "reserved", &header->reserved))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -68,21 +61,21 @@ namespace Nz
|
||||
|
||||
bool Unserialize(SerializationContext& context, DDSPixelFormat* pixelFormat)
|
||||
{
|
||||
if (!Unserialize(context, &pixelFormat->size))
|
||||
if (!Unserialize(context, "size", &pixelFormat->size))
|
||||
return false;
|
||||
if (!Unserialize(context, &pixelFormat->flags))
|
||||
if (!Unserialize(context, "flags", &pixelFormat->flags))
|
||||
return false;
|
||||
if (!Unserialize(context, &pixelFormat->fourCC))
|
||||
if (!Unserialize(context, "CC", &pixelFormat->fourCC))
|
||||
return false;
|
||||
if (!Unserialize(context, &pixelFormat->bpp))
|
||||
if (!Unserialize(context, "bpp", &pixelFormat->bpp))
|
||||
return false;
|
||||
if (!Unserialize(context, &pixelFormat->redMask))
|
||||
if (!Unserialize(context, "redMask", &pixelFormat->redMask))
|
||||
return false;
|
||||
if (!Unserialize(context, &pixelFormat->greenMask))
|
||||
if (!Unserialize(context, "greenMask", &pixelFormat->greenMask))
|
||||
return false;
|
||||
if (!Unserialize(context, &pixelFormat->blueMask))
|
||||
if (!Unserialize(context, "blueMask", &pixelFormat->blueMask))
|
||||
return false;
|
||||
if (!Unserialize(context, &pixelFormat->alphaMask))
|
||||
if (!Unserialize(context, "alphaMask", &pixelFormat->alphaMask))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Export.hpp
|
||||
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
|
||||
/*!
|
||||
* \ingroup core
|
||||
* \class Nz::SerializationContext
|
||||
* \brief Structure containing a serialization/unserialization context states
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Write bits to the stream (if any) and reset the current bit cursor
|
||||
|
||||
* \see ResetWriteBitPosition
|
||||
*/
|
||||
void SerializationContext::FlushBits()
|
||||
{
|
||||
if (writeBitPos != 8)
|
||||
{
|
||||
ResetWriteBitPosition();
|
||||
|
||||
// Serialize will reset the bit position
|
||||
if (!Serialize(*this, writeByte))
|
||||
NazaraWarning("Failed to flush bits");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user