Update read and write streaming api to use the newest format

This commit is contained in:
Wojtek Figat
2024-12-29 23:00:40 +01:00
parent fee0ab74ff
commit 668f3fa68d
29 changed files with 444 additions and 378 deletions

View File

@@ -167,9 +167,9 @@ public:
}
/// <summary>
/// Read data array
/// Reads array.
/// </summary>
/// <param name="data">Array to read</param>
/// <param name="data">Array to read.</param>
template<typename T, typename AllocationType = HeapAllocation>
void Read(Array<T, AllocationType>& data)
{
@@ -189,9 +189,9 @@ public:
}
/// <summary>
/// Read data dictionary
/// Reads dictionary.
/// </summary>
/// <param name="data">Dictionary to read</param>
/// <param name="data">Dictionary to read.</param>
template<typename KeyType, typename ValueType, typename AllocationType = HeapAllocation>
void Read(Dictionary<KeyType, ValueType, AllocationType>& data)
{
@@ -214,34 +214,41 @@ public:
/// <param name="obj">The object to deserialize.</param>
void ReadJson(ISerializable* obj);
// Deserialization of math types with float or double depending on the context (must match serialization)
// Set useDouble=true to explicitly use 64-bit precision for serialized data
void Read(BoundingBox& box, bool useDouble = false);
void Read(BoundingSphere& sphere, bool useDouble = false);
void Read(Transform& transform, bool useDouble = false);
void Read(Ray& ray, bool useDouble = false);
public:
// Reads StringAnsi from the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
void ReadStringAnsi(StringAnsi* data);
DEPRECATED("Use Read method") void ReadStringAnsi(StringAnsi* data);
// Reads StringAnsi from the stream with a key
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
void ReadStringAnsi(StringAnsi* data, int8 lock);
DEPRECATED("Use Read method") void ReadStringAnsi(StringAnsi* data, int8 lock);
// Reads String from the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
void ReadString(String* data);
DEPRECATED("Use Read method") void ReadString(String* data);
// Reads String from the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
void ReadString(String* data, int16 lock);
DEPRECATED("Use Read method") void ReadString(String* data, int16 lock);
// Reads CommonValue from the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
void ReadCommonValue(CommonValue* data);
DEPRECATED("Use Read method") void ReadCommonValue(CommonValue* data);
// Reads VariantType from the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
void ReadVariantType(VariantType* data);
DEPRECATED("Use Read method") void ReadVariantType(VariantType* data);
// Reads Variant from the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
void ReadVariant(Variant* data);
DEPRECATED("Use Read method") void ReadVariant(Variant* data);
/// <summary>
/// Read data array
@@ -249,18 +256,21 @@ public:
/// </summary>
/// <param name="data">Array to read</param>
template<typename T, typename AllocationType = HeapAllocation>
void ReadArray(Array<T, AllocationType>* data)
DEPRECATED("Use Read method") void ReadArray(Array<T, AllocationType>* data)
{
Read(*data);
}
public:
// Deserialization of math types with float or double depending on the context (must match serialization)
// Set useDouble=true to explicitly use 64-bit precision for serialized data
void ReadBoundingBox(BoundingBox* box, bool useDouble = false);
void ReadBoundingSphere(BoundingSphere* sphere, bool useDouble = false);
void ReadTransform(Transform* transform, bool useDouble = false);
void ReadRay(Ray* ray, bool useDouble = false);
// [Deprecated in v1.10]
DEPRECATED("Use Read method") void ReadBoundingBox(BoundingBox* box, bool useDouble = false);
// [Deprecated in v1.10]
DEPRECATED("Use Read method") void ReadBoundingSphere(BoundingSphere* sphere, bool useDouble = false);
// [Deprecated in v1.10]
DEPRECATED("Use Read method") void ReadTransform(Transform* transform, bool useDouble = false);
// [Deprecated in v1.10]
DEPRECATED("Use Read method") void ReadRay(Ray* ray, bool useDouble = false);
public:
// [Stream]

View File

@@ -167,14 +167,14 @@ void ReadStream::Read(CommonValue& data)
case CommonType::String:
{
String v;
ReadString(&v, 953);
Read(v, 953);
data.Set(v);
}
break;
case CommonType::Box:
{
BoundingBox v;
ReadBoundingBox(&v);
Read(v);
data.Set(v);
}
break;
@@ -188,14 +188,14 @@ void ReadStream::Read(CommonValue& data)
case CommonType::Transform:
{
Transform v;
ReadTransform(&v);
Read(v);
data.Set(v);
}
break;
case CommonType::Sphere:
{
BoundingSphere v;
ReadBoundingSphere(&v);
Read(v);
data.Set(v);
}
break;
@@ -208,7 +208,7 @@ void ReadStream::Read(CommonValue& data)
case CommonType::Ray:
{
Ray v;
ReadRay(&v);
Read(v);
data.Set(v);
}
break;
@@ -277,7 +277,7 @@ void ReadStream::Read(VariantType& data)
void ReadStream::Read(Variant& data)
{
VariantType type;
ReadVariantType(&type);
Read(type);
data.SetType(MoveTemp(type));
switch (data.Type.Type)
{
@@ -360,7 +360,7 @@ void ReadStream::Read(Variant& data)
{
// Json
StringAnsi json;
ReadStringAnsi(&json, -71);
Read(json, -71);
#if USE_CSHARP
MCore::Thread::Attach();
MClass* klass = MUtils::GetClass(data.Type);
@@ -430,22 +430,22 @@ void ReadStream::Read(Variant& data)
ReadBytes(&data.AsData, sizeof(Guid));
break;
case VariantType::BoundingBox:
ReadBoundingBox(&data.AsBoundingBox());
Read(data.AsBoundingBox());
break;
case VariantType::BoundingSphere:
ReadBoundingSphere(&data.AsBoundingSphere());
Read(data.AsBoundingSphere());
break;
case VariantType::Quaternion:
ReadBytes(&data.AsData, sizeof(Quaternion));
break;
case VariantType::Transform:
ReadTransform(&data.AsTransform());
Read(data.AsTransform());
break;
case VariantType::Rectangle:
ReadBytes(&data.AsData, sizeof(Rectangle));
break;
case VariantType::Ray:
ReadRay(&data.AsRay());
Read(data.AsRay());
break;
case VariantType::Matrix:
ReadBytes(data.AsBlob.Data, sizeof(Matrix));
@@ -453,25 +453,25 @@ void ReadStream::Read(Variant& data)
case VariantType::Array:
{
int32 count;
ReadInt32(&count);
Read(count);
auto& array = *(Array<Variant>*)data.AsData;
array.Resize(count);
for (int32 i = 0; i < count; i++)
ReadVariant(&array[i]);
Read(array[i]);
break;
}
case VariantType::Dictionary:
{
int32 count;
ReadInt32(&count);
Read(count);
auto& dictionary = *data.AsDictionary;
dictionary.Clear();
dictionary.EnsureCapacity(count);
for (int32 i = 0; i < count; i++)
{
Variant key;
ReadVariant(&key);
ReadVariant(&dictionary[MoveTemp(key)]);
Read(key);
Read(dictionary[MoveTemp(key)]);
}
break;
}
@@ -562,18 +562,18 @@ void ReadStream::ReadVariant(Variant* data)
Read(*data);
}
void ReadStream::ReadBoundingBox(BoundingBox* box, bool useDouble)
void ReadStream::Read(BoundingBox& box, bool useDouble)
{
#if USE_LARGE_WORLDS
if (useDouble)
Read(box);
ReadBytes(&box, sizeof(BoundingBox));
else
{
Float3 min, max;
Read(min);
Read(max);
box->Minimum = min;
box->Maximum = max;
box.Minimum = min;
box.Maximum = max;
}
#else
if (useDouble)
@@ -581,27 +581,27 @@ void ReadStream::ReadBoundingBox(BoundingBox* box, bool useDouble)
Double3 min, max;
Read(min);
Read(max);
box->Minimum = min;
box->Maximum = max;
box.Minimum = min;
box.Maximum = max;
}
else
Read(*box);
ReadBytes(&box, sizeof(BoundingBox));
#endif
}
void ReadStream::ReadBoundingSphere(BoundingSphere* sphere, bool useDouble)
void ReadStream::Read(BoundingSphere& sphere, bool useDouble)
{
#if USE_LARGE_WORLDS
if (useDouble)
Read(*sphere);
ReadBytes(&sphere, sizeof(BoundingSphere));
else
{
Float3 center;
float radius;
Read(center);
Read(radius);
sphere->Center = center;
sphere->Radius = radius;
sphere.Center = center;
sphere.Radius = radius;
}
#else
if (useDouble)
@@ -610,53 +610,53 @@ void ReadStream::ReadBoundingSphere(BoundingSphere* sphere, bool useDouble)
double radius;
Read(center);
Read(radius);
sphere->Center = center;
sphere->Radius = (float)radius;
sphere.Center = center;
sphere.Radius = (float)radius;
}
else
Read(*sphere);
ReadBytes(&sphere, sizeof(BoundingSphere));
#endif
}
void ReadStream::ReadTransform(Transform* transform, bool useDouble)
void ReadStream::Read(Transform& transform, bool useDouble)
{
#if USE_LARGE_WORLDS
if (useDouble)
Read(*transform);
ReadBytes(&transform, sizeof(Transform));
else
{
Float3 translation;
Read(translation);
Read(transform->Orientation);
Read(transform->Scale);
transform->Translation = translation;
Read(transform.Orientation);
Read(transform.Scale);
transform.Translation = translation;
}
#else
if (useDouble)
{
Double3 translation;
Read(translation);
Read(transform->Orientation);
Read(transform->Scale);
transform->Translation = translation;
Read(transform.Orientation);
Read(transform.Scale);
transform.Translation = translation;
}
else
Read(*transform);
ReadBytes(&transform, sizeof(Transform));
#endif
}
void ReadStream::ReadRay(Ray* ray, bool useDouble)
void ReadStream::Read(Ray& ray, bool useDouble)
{
#if USE_LARGE_WORLDS
if (useDouble)
Read(*ray);
ReadBytes(&ray, sizeof(Ray));
else
{
Float3 position, direction;
Read(position);
Read(direction);
ray->Position = position;
ray->Direction = direction;
ray.Position = position;
ray.Direction = direction;
}
#else
if (useDouble)
@@ -664,14 +664,34 @@ void ReadStream::ReadRay(Ray* ray, bool useDouble)
Double3 position, direction;
Read(position);
Read(direction);
ray->Position = position;
ray->Direction = direction;
ray.Position = position;
ray.Direction = direction;
}
else
Read(*ray);
ReadBytes(&ray, sizeof(Ray));
#endif
}
void ReadStream::ReadBoundingBox(BoundingBox* box, bool useDouble)
{
Read(*box);
}
void ReadStream::ReadBoundingSphere(BoundingSphere* sphere, bool useDouble)
{
Read(*sphere);
}
void ReadStream::ReadTransform(Transform* transform, bool useDouble)
{
Read(*transform);
}
void ReadStream::ReadRay(Ray* ray, bool useDouble)
{
Read(*ray);
}
void WriteStream::WriteText(const StringView& text)
{
WriteBytes(text.Get(), sizeof(Char) * text.Length());
@@ -722,13 +742,13 @@ void WriteStream::Write(const CommonValue& data)
switch (data.Type)
{
case CommonType::Bool:
WriteBool(data.AsBool);
Write(data.AsBool);
break;
case CommonType::Integer:
WriteInt32(data.AsInteger);
Write(data.AsInteger);
break;
case CommonType::Float:
WriteFloat(data.AsFloat);
Write(data.AsFloat);
break;
case CommonType::Vector2:
Write(data.AsVector2);
@@ -746,25 +766,25 @@ void WriteStream::Write(const CommonValue& data)
Write(data.AsGuid);
break;
case CommonType::String:
WriteString(data.AsString, 953);
Write(data.AsString, 953);
break;
case CommonType::Box:
WriteBoundingBox(data.AsBox);
Write(data.AsBox);
break;
case CommonType::Rotation:
Write(data.AsRotation);
break;
case CommonType::Transform:
WriteTransform(data.AsTransform);
Write(data.AsTransform);
break;
case CommonType::Sphere:
WriteBoundingSphere(data.AsSphere);
Write(data.AsSphere);
break;
case CommonType::Rectangle:
Write(data.AsRectangle);
break;
case CommonType::Ray:
WriteRay(data.AsRay);
Write(data.AsRay);
break;
case CommonType::Matrix:
Write(data.AsMatrix);
@@ -782,12 +802,12 @@ void WriteStream::Write(const VariantType& data)
{
WriteByte((byte)data.Type);
WriteInt32(MAX_int32);
WriteStringAnsi(StringAnsiView(data.TypeName), 77);
Write(StringAnsiView(data.TypeName), 77);
}
void WriteStream::Write(const Variant& data)
{
WriteVariantType(data.Type);
Write(data.Type);
Guid id;
switch (data.Type.Type)
{
@@ -826,7 +846,7 @@ void WriteStream::Write(const Variant& data)
WriteUint64((uint64)(uintptr)data.AsPointer);
break;
case VariantType::String:
WriteString((StringView)data, -14);
Write((StringView)data, -14);
break;
case VariantType::Object:
id = data.AsObject ? data.AsObject->GetID() : Guid::Empty;
@@ -837,13 +857,13 @@ void WriteStream::Write(const Variant& data)
WriteBytes(data.AsBlob.Data, data.AsBlob.Length);
break;
case VariantType::BoundingBox:
WriteBoundingBox(data.AsBoundingBox());
Write(data.AsBoundingBox());
break;
case VariantType::Transform:
WriteTransform(data.AsTransform());
Write(data.AsTransform());
break;
case VariantType::Ray:
WriteRay(data.AsRay());
Write(data.AsRay());
break;
case VariantType::Matrix:
WriteBytes(data.AsBlob.Data, sizeof(Matrix));
@@ -883,24 +903,24 @@ void WriteStream::Write(const Variant& data)
WriteBytes(data.AsData, sizeof(Rectangle));
break;
case VariantType::BoundingSphere:
WriteBoundingSphere(data.AsBoundingSphere());
Write(data.AsBoundingSphere());
break;
case VariantType::Array:
id.A = ((Array<Variant>*)data.AsData)->Count();
WriteInt32(id.A);
for (uint32 i = 0; i < id.A; i++)
WriteVariant(((Array<Variant>*)data.AsData)->At(i));
Write(((Array<Variant>*)data.AsData)->At(i));
break;
case VariantType::Dictionary:
WriteInt32(data.AsDictionary->Count());
for (auto i = data.AsDictionary->Begin(); i.IsNotEnd(); ++i)
{
WriteVariant(i->Key);
WriteVariant(i->Value);
Write(i->Key);
Write(i->Value);
}
break;
case VariantType::Typename:
WriteStringAnsi((StringAnsiView)data, -14);
Write((StringAnsiView)data, -14);
break;
case VariantType::ManagedObject:
case VariantType::Structure:
@@ -918,7 +938,7 @@ void WriteStream::Write(const Variant& data)
CompactJsonWriter writerObj(json);
MCore::Thread::Attach();
ManagedSerialization::Serialize(writerObj, obj);
WriteStringAnsi(StringAnsiView(json.GetString(), (int32)json.GetSize()), -71);
Write(StringAnsiView(json.GetString(), (int32)json.GetSize()), -71);
}
else
#endif
@@ -992,11 +1012,11 @@ void WriteStream::WriteVariant(const Variant& data)
Write(data);
}
void WriteStream::WriteBoundingBox(const BoundingBox& box, bool useDouble)
void WriteStream::Write(const BoundingBox& box, bool useDouble)
{
#if USE_LARGE_WORLDS
if (useDouble)
Write(box);
WriteBytes(&box, sizeof(BoundingBox));
else
{
Float3 min = box.Minimum, max = box.Maximum;
@@ -1011,15 +1031,15 @@ void WriteStream::WriteBoundingBox(const BoundingBox& box, bool useDouble)
Write(max);
}
else
Write(box);
WriteBytes(&box, sizeof(BoundingBox));
#endif
}
void WriteStream::WriteBoundingSphere(const BoundingSphere& sphere, bool useDouble)
void WriteStream::Write(const BoundingSphere& sphere, bool useDouble)
{
#if USE_LARGE_WORLDS
if (useDouble)
Write(sphere);
WriteBytes(&sphere, sizeof(BoundingSphere));
else
{
Float3 center = sphere.Center;
@@ -1036,15 +1056,15 @@ void WriteStream::WriteBoundingSphere(const BoundingSphere& sphere, bool useDoub
Write(radius);
}
else
Write(sphere);
WriteBytes(&sphere, sizeof(BoundingSphere));
#endif
}
void WriteStream::WriteTransform(const Transform& transform, bool useDouble)
void WriteStream::Write(const Transform& transform, bool useDouble)
{
#if USE_LARGE_WORLDS
if (useDouble)
Write(transform);
WriteBytes(&transform, sizeof(Transform));
else
{
Float3 translation = transform.Translation;
@@ -1061,15 +1081,15 @@ void WriteStream::WriteTransform(const Transform& transform, bool useDouble)
Write(transform.Scale);
}
else
Write(transform);
WriteBytes(&transform, sizeof(Transform));
#endif
}
void WriteStream::WriteRay(const Ray& ray, bool useDouble)
void WriteStream::Write(const Ray& ray, bool useDouble)
{
#if USE_LARGE_WORLDS
if (useDouble)
Write(ray);
WriteBytes(&ray, sizeof(Ray));
else
{
Float3 position = ray.Position, direction = ray.Direction;
@@ -1084,10 +1104,30 @@ void WriteStream::WriteRay(const Ray& ray, bool useDouble)
Write(direction);
}
else
Write(ray);
WriteBytes(&ray, sizeof(Ray));
#endif
}
void WriteStream::WriteBoundingBox(const BoundingBox& box, bool useDouble)
{
Write(box, useDouble);
}
void WriteStream::WriteBoundingSphere(const BoundingSphere& sphere, bool useDouble)
{
Write(sphere, useDouble);
}
void WriteStream::WriteTransform(const Transform& transform, bool useDouble)
{
Write(transform, useDouble);
}
void WriteStream::WriteRay(const Ray& ray, bool useDouble)
{
Write(ray, useDouble);
}
Array<byte> JsonSerializer::SaveToBytes(ISerializable* obj)
{
Array<byte> result;

View File

@@ -178,6 +178,23 @@ public:
Write(v.Get());
}
template<typename T>
void Write(const Span<T>& data)
{
const int32 size = data.Length();
WriteInt32(size);
if (size > 0)
{
if (TIsPODType<T>::Value && !TIsPointer<T>::Value)
WriteBytes(data.Get(), size * sizeof(T));
else
{
for (int32 i = 0; i < size; i++)
Write(data[i]);
}
}
}
template<typename T, typename AllocationType = HeapAllocation>
void Write(const Array<T, AllocationType>& data)
{
@@ -216,42 +233,49 @@ public:
void WriteJson(ISerializable* obj, const void* otherObj = nullptr);
void WriteJson(const StringAnsiView& json);
// Serialization of math types with float or double depending on the context (must match deserialization)
// Set useDouble=true to explicitly use 64-bit precision for serialized data
void Write(const BoundingBox& box, bool useDouble = false);
void Write(const BoundingSphere& sphere, bool useDouble = false);
void Write(const Transform& transform, bool useDouble = false);
void Write(const Ray& ray, bool useDouble = false);
public:
// Writes String to the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
// @param data Data to write
void WriteString(const StringView& data);
DEPRECATED("Use Write method") void WriteString(const StringView& data);
// Writes String to the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
// @param data Data to write
// @param lock Characters pass in the stream
void WriteString(const StringView& data, int16 lock);
DEPRECATED("Use Write method") void WriteString(const StringView& data, int16 lock);
// Writes Ansi String to the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
void WriteStringAnsi(const StringAnsiView& data);
DEPRECATED("Use Write method") void WriteStringAnsi(const StringAnsiView& data);
// Writes Ansi String to the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
// @param data Data to write
// @param lock Characters pass in the stream
void WriteStringAnsi(const StringAnsiView& data, int8 lock);
DEPRECATED("Use Write method") void WriteStringAnsi(const StringAnsiView& data, int8 lock);
// Writes CommonValue to the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
// @param data Data to write
void WriteCommonValue(const CommonValue& data);
DEPRECATED("Use Write method") void WriteCommonValue(const CommonValue& data);
// Writes VariantType to the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
// @param data Data to write
void WriteVariantType(const VariantType& data);
DEPRECATED("Use Write method") void WriteVariantType(const VariantType& data);
// Writes Variant to the stream
/// [Deprecated on 11.10.2022, expires on 11.10.2024]
// @param data Data to write
void WriteVariant(const Variant& data);
DEPRECATED("Use Write method") void WriteVariant(const Variant& data);
/// <summary>
/// Write data array
@@ -259,18 +283,21 @@ public:
/// </summary>
/// <param name="data">Array to write</param>
template<typename T, typename AllocationType = HeapAllocation>
FORCE_INLINE void WriteArray(const Array<T, AllocationType>& data)
DEPRECATED("Use Write method") FORCE_INLINE void WriteArray(const Array<T, AllocationType>& data)
{
Write(data);
}
public:
// Serialization of math types with float or double depending on the context (must match deserialization)
// Set useDouble=true to explicitly use 64-bit precision for serialized data
void WriteBoundingBox(const BoundingBox& box, bool useDouble = false);
void WriteBoundingSphere(const BoundingSphere& sphere, bool useDouble = false);
void WriteTransform(const Transform& transform, bool useDouble = false);
void WriteRay(const Ray& ray, bool useDouble = false);
// [Deprecated in v1.10]
DEPRECATED("Use Write method") void WriteBoundingBox(const BoundingBox& box, bool useDouble = false);
// [Deprecated in v1.10]
DEPRECATED("Use Write method") void WriteBoundingSphere(const BoundingSphere& sphere, bool useDouble = false);
// [Deprecated in v1.10]
DEPRECATED("Use Write method") void WriteTransform(const Transform& transform, bool useDouble = false);
// [Deprecated in v1.10]
DEPRECATED("Use Write method") void WriteRay(const Ray& ray, bool useDouble = false);
public:
// [Stream]