Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion Arch.Persistence.Tests/PersistenceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,50 @@ public void JsonEntitySerialization()

var newWorld = World.Create();
var newEntity = _jsonSerializer.Deserialize(newWorld, bytes);

That(newEntity.Get<Transform>(), Is.EqualTo(entity.Get<Transform>()));
That(newEntity.Get<MetaData>(), Is.EqualTo(entity.Get<MetaData>()));
}

/// <summary>
/// Checks if entities are alive after binary world deserialization.
/// This verifies that EntityData.Version is correctly preserved.
/// </summary>
[Test]
public void BinaryWorldSerialization_EntitiesAreAlive()
{
var bytes = _binarySerializer.Serialize(_world);
var newWorld = _binarySerializer.Deserialize(bytes);

var newEntities = new Entity[newWorld.Size];
newWorld.GetEntities(new QueryDescription(), newEntities.AsSpan());

// All entities should be alive after deserialization
for (var index = 0; index < newEntities.Length; index++)
{
var entity = newEntities[index];
That(newWorld.IsAlive(entity), Is.True, $"Entity {entity.Id} with version {entity.Version} should be alive");
}
}

/// <summary>
/// Checks if entities are alive after JSON world deserialization.
/// This verifies that EntityData.Version is correctly preserved.
/// </summary>
[Test]
public void JsonWorldSerialization_EntitiesAreAlive()
{
var bytes = _jsonSerializer.Serialize(_world);
var newWorld = _jsonSerializer.Deserialize(bytes);

var newEntities = new Entity[newWorld.Size];
newWorld.GetEntities(new QueryDescription(), newEntities.AsSpan());

// All entities should be alive after deserialization
for (var index = 0; index < newEntities.Length; index++)
{
var entity = newEntities[index];
That(newWorld.IsAlive(entity), Is.True, $"Entity {entity.Id} with version {entity.Version} should be alive");
}
}
}
10 changes: 8 additions & 2 deletions Arch.Persistence/Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,23 @@ public void Serialize(ref MessagePackWriter writer, EntityData value, MessagePac

// Write entity index
writer.WriteUInt32((uint)value.Slot.Index);

// Write version (required for IsAlive checks after deserialization)
writer.WriteInt32(value.Version);
}

/// <inheritdoc cref="IMessagePackFormatter{T}.Deserialize"/>
public EntityData Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{

// Read chunk index and entity index
var chunkIndex = reader.ReadUInt32();
var entityIndex = reader.ReadUInt32();

return new EntityData(null!, new Slot((int)entityIndex, (int)chunkIndex), 0);
// Read version (required for IsAlive checks after deserialization)
var version = reader.ReadInt32();

return new EntityData(null!, new Slot((int)entityIndex, (int)chunkIndex), version);
}
}

Expand Down
18 changes: 14 additions & 4 deletions Arch.Persistence/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public partial class EntitySlotFormatter : IJsonFormatter<EntityData>
public void Serialize(ref JsonWriter writer, EntityData value, IJsonFormatterResolver options)
{
writer.WriteBeginObject();

// Write chunk index
writer.WritePropertyName("chunkIndex");
writer.WriteUInt32((uint)value.Slot.ChunkIndex);
Expand All @@ -412,7 +412,12 @@ public void Serialize(ref JsonWriter writer, EntityData value, IJsonFormatterRes
// Write entity index
writer.WritePropertyName("index");
writer.WriteUInt32((uint)value.Slot.Index);

writer.WriteValueSeparator();

// Write version (required for IsAlive checks after deserialization)
writer.WritePropertyName("version");
writer.WriteInt32(value.Version);

writer.WriteEndObject();
}

Expand All @@ -425,13 +430,18 @@ public EntityData Deserialize(ref JsonReader reader, IJsonFormatterResolver opti
reader.ReadPropertyName();
var chunkIndex = reader.ReadUInt32();
reader.ReadIsValueSeparator();

// Read entity index
reader.ReadPropertyName();
var entityIndex = reader.ReadUInt32();
reader.ReadIsValueSeparator();

// Read version (required for IsAlive checks after deserialization)
reader.ReadPropertyName();
var version = reader.ReadInt32();

reader.ReadIsEndObject();
return new EntityData(null!, new Slot((int)entityIndex, (int)chunkIndex), 0);
return new EntityData(null!, new Slot((int)entityIndex, (int)chunkIndex), version);
}
}

Expand Down