Skip to content

Commit

Permalink
Fixes persistence.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Mar 26, 2024
1 parent d25d006 commit 9874569
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Arch.Persistence.Tests/Arch.Persistence.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Arch" Version="1.2.7" />
<PackageReference Include="Arch" Version="1.2.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
Expand Down
8 changes: 4 additions & 4 deletions Arch.Persistence.Tests/PersistenceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public void BinaryWorldSerialization()
var archetype = _world.Archetypes[index];
var newArchetype = newWorld.Archetypes[index];

That(archetype.Capacity, Is.EqualTo(newArchetype.Capacity));
That(archetype.Entities, Is.EqualTo(newArchetype.Entities));
That(archetype.ChunkCapacity, Is.EqualTo(newArchetype.ChunkCapacity));
That(archetype.EntityCount, Is.EqualTo(newArchetype.EntityCount));
}

// Are entities equal?
Expand Down Expand Up @@ -123,8 +123,8 @@ public void JsonWorldSerialization()
var archetype = _world.Archetypes[index];
var newArchetype = newWorld.Archetypes[index];

That(archetype.Capacity, Is.EqualTo(newArchetype.Capacity));
That(archetype.Entities, Is.EqualTo(newArchetype.Entities));
That(archetype.ChunkCapacity, Is.EqualTo(newArchetype.ChunkCapacity));
That(archetype.EntityCount, Is.EqualTo(newArchetype.EntityCount));
}

// Are entities equal?
Expand Down
40 changes: 34 additions & 6 deletions Arch.Persistence/Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,32 @@ public ComponentType Deserialize(ref MessagePackReader reader, MessagePackSerial
}
}

/// <summary>
/// The <see cref="ComponentTypeFormatter"/> class
/// is a <see cref="IJsonFormatter{ComponentType}"/> to (de)serialize <see cref="ComponentType"/>s to or from json.
/// </summary>
public partial class EntitySlotFormatter : IMessagePackFormatter<(Archetype, (int,int))>
{
public void Serialize(ref MessagePackWriter writer, (Archetype, (int, int)) value, MessagePackSerializerOptions options)
{
// Write chunk index
writer.WriteUInt32((uint)value.Item2.Item1);

// Write entity index
writer.WriteUInt32((uint)value.Item2.Item2);
}

public (Archetype, (int, int)) Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{

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

return (null, ((int)chunkIndex, (int)entityIndex));
}
}


/// <summary>
/// The <see cref="WorldFormatter"/> class
Expand Down Expand Up @@ -247,6 +273,12 @@ public World Deserialize(ref MessagePackReader reader, MessagePackSerializerOpti
//Read recycled entity ids
var recycledEntityIDs = MessagePackSerializer.Deserialize<List<(int, int)>>(ref reader, options);

// Forward values to the world
world.SetVersions(versions);
world.SetRecycledEntityIds(recycledEntityIDs);
world.SetSlots(slots);
world.EnsureCapacity(versions.Capacity);

// Read archetypes
var size = reader.ReadInt32();
List<Archetype> archetypes = new();
Expand All @@ -256,13 +288,9 @@ public World Deserialize(ref MessagePackReader reader, MessagePackSerializerOpti
var archetype = archetypeFormatter.Deserialize(ref reader, options);
archetypes.Add(archetype);
}

// Forward values to the world
// Set archetypes
world.SetArchetypes(archetypes);
world.SetVersions(versions);
world.SetRecycledEntityIds(recycledEntityIDs);
world.SetSlots(slots);
world.EnsureCapacity(versions.Capacity);
return world;
}
}
Expand Down
67 changes: 54 additions & 13 deletions Arch.Persistence/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ public void Serialize(ref JsonWriter writer, JaggedArray<T> value, IJsonFormatte
writer.WritePropertyName("items");
writer.WriteBeginArray();

for (int index = 0; index < value.Capacity; index++)
for (var index = 0; index < value.Capacity; index++)
{
T? item = value[index];
var item = value[index];
JsonSerializer.Serialize(ref writer, item, formatterResolver);
writer.WriteValueSeparator();
}
Expand Down Expand Up @@ -329,6 +329,46 @@ public ComponentType Deserialize(ref JsonReader reader, IJsonFormatterResolver f
}
}

/// <summary>
/// The <see cref="ComponentTypeFormatter"/> class
/// is a <see cref="IJsonFormatter{ComponentType}"/> to (de)serialize <see cref="ComponentType"/>s to or from json.
/// </summary>
public partial class EntitySlotFormatter : IJsonFormatter<(Archetype, (int,int))>
{
public void Serialize(ref JsonWriter writer, (Archetype, (int, int)) value, IJsonFormatterResolver options)
{
writer.WriteBeginObject();

// Write chunk index
writer.WritePropertyName("chunkIndex");
writer.WriteUInt32((uint)value.Item2.Item1);
writer.WriteValueSeparator();

// Write entity index
writer.WritePropertyName("entityIndex");
writer.WriteUInt32((uint)value.Item2.Item2);

writer.WriteEndObject();
}

public (Archetype, (int, int)) Deserialize(ref JsonReader reader, IJsonFormatterResolver options)
{
reader.ReadIsBeginObject();

// Read chunk index
reader.ReadPropertyName();
var chunkIndex = reader.ReadUInt32();
reader.ReadIsValueSeparator();

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

reader.ReadIsEndObject();
return (null, ((int)chunkIndex, (int)entityIndex));
}
}

/// <summary>
/// The <see cref="WorldFormatter"/> class
/// is a <see cref="IJsonFormatter{World}"/> to (de)serialize <see cref="World"/>s to or from json.
Expand Down Expand Up @@ -356,8 +396,8 @@ public void Serialize(ref JsonWriter writer, World value, IJsonFormatterResolver
//Write recycled entity ids
writer.WritePropertyName("recycledEntityIDs");
writer.WriteBeginArray();
List<(int, int)> recycledEntityIDs = value.GetRecycledEntityIds();
foreach ((int, int) recycledId in recycledEntityIDs)
var recycledEntityIDs = value.GetRecycledEntityIds();
foreach (var recycledId in recycledEntityIDs)
{
writer.WriteBeginObject();
writer.WritePropertyName("id");
Expand All @@ -381,7 +421,7 @@ public void Serialize(ref JsonWriter writer, World value, IJsonFormatterResolver
//Write archetypes
writer.WritePropertyName("archetypes");
writer.WriteBeginArray();
foreach (Archetype archetype in value)
foreach (var archetype in value)
{
JsonSerializer.Serialize(ref writer, archetype, formatterResolver);
writer.WriteValueSeparator();
Expand Down Expand Up @@ -428,10 +468,10 @@ public World Deserialize(ref JsonReader reader, IJsonFormatterResolver formatter
{
reader.ReadIsBeginObject();
reader.ReadPropertyName();
int id = reader.ReadInt32();
var id = reader.ReadInt32();
reader.ReadIsValueSeparator();
reader.ReadPropertyName();
int value = reader.ReadInt32();
var value = reader.ReadInt32();
reader.ReadIsEndObject();

(int, int) recycledId = new(id, value);
Expand All @@ -440,6 +480,12 @@ public World Deserialize(ref JsonReader reader, IJsonFormatterResolver formatter
}

reader.ReadIsValueSeparator();

// Forward values to the world
world.SetVersions(versions);
world.SetRecycledEntityIds(recycledIds);
world.SetSlots(slots);
world.EnsureCapacity(versions.Capacity);

// Read archetypes
count = 0;
Expand All @@ -452,13 +498,8 @@ public World Deserialize(ref JsonReader reader, IJsonFormatterResolver formatter
archetypes.Add(archetype);
}

// Forward values to the world
// Set archetypes
world.SetArchetypes(archetypes);
world.SetVersions(versions);
world.SetRecycledEntityIds(recycledIds);
world.SetSlots(slots);
world.EnsureCapacity(versions.Capacity);

reader.ReadIsEndObject();
return world;
}
Expand Down
6 changes: 5 additions & 1 deletion Arch.Persistence/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ public class ArchBinarySerializer : IArchSerializer
new ChunkFormatter(),
new ArrayFormatter(),
new ComponentTypeFormatter(),
new EntitySlotFormatter(),
new EntityFormatter(),
new JaggedArrayFormatter<int>(-1),
new JaggedArrayFormatter<(int,int)>((-1,-1))
new JaggedArrayFormatter<(int,int)>((-1,-1)),
new JaggedArrayFormatter<(Archetype,(int,int))>((null, (-1,-1)))
};

/// <summary>
Expand Down Expand Up @@ -243,9 +245,11 @@ public class ArchJsonSerializer : IArchSerializer
new ChunkFormatter(),
new ArrayFormatter(),
new ComponentTypeFormatter(),
new EntitySlotFormatter(),
new EntityFormatter(),
new JaggedArrayFormatter<int>(-1),
new JaggedArrayFormatter<(int,int)>((-1,-1)),
new JaggedArrayFormatter<(Archetype,(int,int))>((null, (-1,-1))),
new DateTimeFormatter("yyyy-MM-dd HH:mm:ss"),
new NullableDateTimeFormatter("yyyy-MM-dd HH:mm:ss")
};
Expand Down

0 comments on commit 9874569

Please sign in to comment.