Skip to content

Commit

Permalink
fixed serialisation of World component
Browse files Browse the repository at this point in the history
  • Loading branch information
Doraku committed Jun 5, 2021
1 parent 3998c61 commit ab85d19
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 45 deletions.
30 changes: 28 additions & 2 deletions source/DefaultEcs.Test/Serialization/ISerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ public void Serialize_Should_serialize_World(Type serializerType)
{
using World world = new(42);

world.Set("hello");

world.SetMaxCapacity<int>(13);
world.SetMaxCapacity<float>(60);

Expand Down Expand Up @@ -238,12 +240,15 @@ public void Serialize_Should_serialize_World(Type serializerType)
entities[0].Set(new ClassTest { Id = 12345, Inner = new Test(66), Test = new InnerTest2() });
entities[2].Set(new InnerTest { Lol = 313 });
entities[1].SetSameAs<InnerTest>(entities[2]);
entities[1].SetSameAsWorld<string>();
entities[1].Set(new Test(42));
entities[2].SetSameAs<Test>(entities[1]);
entities[2].SetSameAs<bool>(entities[0]);
entities[2].Disable<bool>();
entities[2].Set<sbyte>(42);
entities[2].Disable<sbyte>();
entities[2].SetSameAsWorld<string>();
entities[2].Disable<string>();

entities[0].Set<InnerClass>();
entities[0].Set<IEnumerable<int>>(new int[] { 1, 2, 3 });
Expand All @@ -270,6 +275,8 @@ public void Serialize_Should_serialize_World(Type serializerType)
{
Check.That(copyWorld.MaxCapacity).IsEqualTo(world.MaxCapacity);

Check.That(copyWorld.Get<string>()).IsEqualTo(world.Get<string>());

Entity[] entitiesCopy = copyWorld.ToArray();

Check.That(entitiesCopy[0].Has<Int32>());
Expand All @@ -292,6 +299,7 @@ public void Serialize_Should_serialize_World(Type serializerType)
Check.That(entitiesCopy[0].Get<Test>()).IsEqualTo(entities[0].Get<Test>());

Check.That(entitiesCopy[1].Get<Test>()).IsEqualTo(entities[1].Get<Test>());
Check.That(entitiesCopy[1].Get<string>()).IsEqualTo(entities[1].Get<string>());
Check.That(entitiesCopy[1].Get<InnerTest>()).IsEqualTo(entities[1].Get<InnerTest>());

Check.That(entitiesCopy[1].Get<Test>()).IsEqualTo(entitiesCopy[2].Get<Test>());
Expand All @@ -306,6 +314,8 @@ public void Serialize_Should_serialize_World(Type serializerType)
Check.That(entitiesCopy[2].IsEnabled<bool>()).IsFalse();
Check.That(entitiesCopy[2].Get<sbyte>()).IsEqualTo(entities[2].Get<sbyte>());
Check.That(entitiesCopy[2].IsEnabled<sbyte>()).IsFalse();
Check.That(entitiesCopy[2].Get<string>()).IsEqualTo(entities[2].Get<string>());
Check.That(entitiesCopy[2].IsEnabled<string>()).IsFalse();
}
}
finally
Expand Down Expand Up @@ -539,9 +549,11 @@ public void Should_use_context_marshalling(ISerializer serializer, IDisposable c
{
using World world = new();

world.SetMaxCapacity<uint>(10);
world.Set<uint>(42);

Entity entity0 = world.CreateEntity();

world.SetMaxCapacity<uint>(10);
entity0.Set<uint>(42);
entity0.Set<double>(1);

Expand All @@ -559,6 +571,15 @@ public void Should_use_context_marshalling(ISerializer serializer, IDisposable c
entity3.SetSameAs<uint>(entity0);
entity3.Disable<uint>();

Entity entity4 = world.CreateEntity();

entity4.SetSameAsWorld<uint>();

Entity entity5 = world.CreateEntity();

entity5.SetSameAsWorld<uint>();
entity5.Disable<uint>();

using Stream stream = new MemoryStream();

serializer.Serialize(stream, world);
Expand All @@ -567,9 +588,11 @@ public void Should_use_context_marshalling(ISerializer serializer, IDisposable c

using World copy = serializer.Deserialize(stream);

Check.That(copy.Get<string>()).IsEqualTo("42");

Entity[] entities = copy.ToArray();
Check.That(copy.GetMaxCapacity<string>()).IsEqualTo(10);
Check.That(entities.Length).IsEqualTo(4);
Check.That(entities.Length).IsEqualTo(6);
Check.That(entities[0].Get<double>()).IsEqualTo(entity0.Get<double>());
Check.That(entities[0].Has<int>()).IsFalse();
Check.That(entities[0].Has<uint>()).IsFalse();
Expand All @@ -591,6 +614,9 @@ public void Should_use_context_marshalling(ISerializer serializer, IDisposable c
Check.That(entities[3].Has<string>()).IsTrue();
Check.That(entities[3].IsEnabled<string>()).IsFalse();
Check.That(entities[3].Get<string>()).IsEqualTo("42");
Check.That(entities[4].Get<string>()).IsEqualTo("42");
Check.That(entities[5].Get<string>()).IsEqualTo("42");
Check.That(entities[5].IsEnabled<string>()).IsFalse();
}
}

Expand Down
14 changes: 12 additions & 2 deletions source/DefaultEcs/Serialization/BinarySerializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public MarshalComponentOperation(Func<TIn, TOut> converter)

public void SetMaxCapacity(World world, int maxCapacity) => world.SetMaxCapacity<TOut>(maxCapacity);

public void Set(World world, in StreamReaderWrapper reader) => world.Set(_converter(Converter<TIn>.Read(reader)));

public void Set(in Entity entity, in StreamReaderWrapper reader) => entity.Set(_converter(Converter<TIn>.Read(reader)));

public void SetSameAs(in Entity entity, in Entity reference) => entity.SetSameAs<TOut>(reference);

public void SetSameAsWorld(in Entity entity) => entity.SetSameAsWorld<TOut>();

public void SetDisabled(in Entity entity, in StreamReaderWrapper reader)
{
Set(entity, reader);
Expand All @@ -50,6 +54,12 @@ public void SetDisabledSameAs(in Entity entity, in Entity reference)
entity.Disable<TOut>();
}

public void SetDisabledSameAsWorld(in Entity entity)
{
SetSameAsWorld(entity);
entity.Disable<TOut>();
}

public BinarySerializer.IComponentOperation ApplyContext(BinarySerializationContext context) => this;

#endregion
Expand Down Expand Up @@ -79,7 +89,7 @@ public BinarySerializationContext()

#region Methods

internal Action<ComponentTypeWriter, int> GetWorldWrite<T>() => _id < SerializationContext<T>.Actions.Length ? SerializationContext<T>.Actions[_id].WorldWrite as Action<ComponentTypeWriter, int> : null;
internal Action<ComponentTypeWriter, int, bool> GetWorldWrite<T>() => _id < SerializationContext<T>.Actions.Length ? SerializationContext<T>.Actions[_id].WorldWrite as Action<ComponentTypeWriter, int, bool> : null;

internal Action<EntityWriter, T, Entity, bool> GetEntityWrite<T>() => _id < SerializationContext<T>.Actions.Length ? SerializationContext<T>.Actions[_id].EntityWrite as Action<EntityWriter, T, Entity, bool> : null;

Expand All @@ -106,7 +116,7 @@ public BinarySerializationContext Marshal<TIn, TOut>(Func<TIn, TOut> converter)
{
SerializationContext<TIn>.SetWriteActions(
_id,
new Action<ComponentTypeWriter, int>((writer, maxCapacity) => writer.WriteComponent<TOut>(maxCapacity)),
new Action<ComponentTypeWriter, int, bool>((writer, maxCapacity, hasComponent) => writer.WriteComponent(maxCapacity, hasComponent, w => converter(w.Get<TIn>()))),
new Action<EntityWriter, TIn, Entity, bool>((writer, value, owner, isEnabled) => writer.WriteComponent(converter(value), owner, isEnabled)),
new WriteAction<TIn>((in StreamWriterWrapper writer, in TIn value) =>
{
Expand Down
38 changes: 36 additions & 2 deletions source/DefaultEcs/Serialization/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public sealed class BinarySerializer : ISerializer
internal interface IComponentOperation
{
void SetMaxCapacity(World world, int maxCapacity);
void Set(World world, in StreamReaderWrapper reader);
void Set(in Entity entity, in StreamReaderWrapper reader);
void SetSameAs(in Entity entity, in Entity reference);
void SetSameAsWorld(in Entity entity);
void SetDisabled(in Entity entity, in StreamReaderWrapper reader);
void SetDisabledSameAs(in Entity entity, in Entity reference);
void SetDisabledSameAsWorld(in Entity entity);
IComponentOperation ApplyContext(BinarySerializationContext context);
}

Expand All @@ -30,10 +33,14 @@ private sealed class ComponentOperation<T> : IComponentOperation

public void SetMaxCapacity(World world, int maxCapacity) => world.SetMaxCapacity<T>(maxCapacity);

public void Set(World world, in StreamReaderWrapper reader) => world.Set(Converter<T>.Read(reader));

public void Set(in Entity entity, in StreamReaderWrapper reader) => entity.Set(Converter<T>.Read(reader));

public void SetSameAs(in Entity entity, in Entity reference) => entity.SetSameAs<T>(reference);

public void SetSameAsWorld(in Entity entity) => entity.SetSameAsWorld<T>();

public void SetDisabled(in Entity entity, in StreamReaderWrapper reader)
{
Set(entity, reader);
Expand All @@ -46,6 +53,12 @@ public void SetDisabledSameAs(in Entity entity, in Entity reference)
entity.Disable<T>();
}

public void SetDisabledSameAsWorld(in Entity entity)
{
SetSameAsWorld(entity);
entity.Disable<T>();
}

public IComponentOperation ApplyContext(BinarySerializationContext context) => context?.GetComponentOperation<T>() ?? this;

#endregion
Expand All @@ -57,14 +70,20 @@ private sealed class IgnoreComponentOperation<T> : IComponentOperation

public void SetMaxCapacity(World world, int maxCapacity) { }

public void Set(World world, in StreamReaderWrapper reader) => Converter<T>.Read(reader);

public void Set(in Entity entity, in StreamReaderWrapper reader) => Converter<T>.Read(reader);

public void SetSameAs(in Entity entity, in Entity reference) { }

public void SetSameAsWorld(in Entity entity) { }

public void SetDisabled(in Entity entity, in StreamReaderWrapper reader) => Set(entity, reader);

public void SetDisabledSameAs(in Entity entity, in Entity reference) { }

public void SetDisabledSameAsWorld(in Entity entity) { }

public IComponentOperation ApplyContext(BinarySerializationContext context) => this;

#endregion
Expand Down Expand Up @@ -164,13 +183,24 @@ private ICollection<Entity> Deserialize(Stream stream, ref World world)
break;

case EntryType.Component:
componentOperations[reader.Read<ushort>()].Set(currentEntity, reader);
if (currentEntity == default)
{
componentOperations[reader.Read<ushort>()].Set(world, reader);
}
else
{
componentOperations[reader.Read<ushort>()].Set(currentEntity, reader);
}
break;

case EntryType.ComponentSameAs:
componentOperations[reader.Read<ushort>()].SetSameAs(currentEntity, entities[reader.Read<int>()]);
break;

case EntryType.ComponentSameAsWorld:
componentOperations[reader.Read<ushort>()].SetSameAsWorld(currentEntity);
break;

case EntryType.DisabledEntity:
currentEntity = world.CreateEntity();
currentEntity.Disable();
Expand All @@ -184,6 +214,10 @@ private ICollection<Entity> Deserialize(Stream stream, ref World world)
case EntryType.DisabledComponentSameAs:
componentOperations[reader.Read<ushort>()].SetDisabledSameAs(currentEntity, entities[reader.Read<int>()]);
break;

case EntryType.DisabledComponentSameAsWorld:
componentOperations[reader.Read<ushort>()].SetDisabledSameAsWorld(currentEntity);
break;
}
}

Expand Down Expand Up @@ -281,7 +315,7 @@ public void Serialize(Stream stream, World world)

Dictionary<Type, ushort> types = new();

world.ReadAllComponentTypes(new ComponentTypeWriter(writer, types, world.MaxCapacity, _componentFilter));
world.ReadAllComponentTypes(new ComponentTypeWriter(world, writer, types, world.MaxCapacity, _componentFilter));

new EntityWriter(writer, types, _componentFilter).Write(world);
}
Expand Down
14 changes: 12 additions & 2 deletions source/DefaultEcs/Serialization/TextSerializationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ public MarshalComponentOperation(Func<TIn, TOut> converter)

public void SetMaxCapacity(World world, int maxCapacity) => world.SetMaxCapacity<TOut>(maxCapacity);

public void Set(World world, StreamReaderWrapper reader) => world.Set(_converter(Converter<TIn>.Read(reader)));

public void Set(in Entity entity, StreamReaderWrapper reader) => entity.Set(_converter(Converter<TIn>.Read(reader)));

public void SetSameAs(in Entity entity, in Entity reference) => entity.SetSameAs<TOut>(reference);

public void SetSameAsWorld(in Entity entity) => entity.SetSameAsWorld<TOut>();

public void SetDisabled(in Entity entity, StreamReaderWrapper reader)
{
Set(entity, reader);
Expand All @@ -49,6 +53,12 @@ public void SetDisabledSameAs(in Entity entity, in Entity reference)
entity.Disable<TOut>();
}

public void SetDisabledSameAsWorld(in Entity entity)
{
SetSameAsWorld(entity);
entity.Disable<TOut>();
}

public TextSerializer.IComponentOperation ApplyContext(TextSerializationContext context) => this;

#endregion
Expand Down Expand Up @@ -78,7 +88,7 @@ public TextSerializationContext()

#region Methods

internal Action<ComponentTypeWriter, int> GetWorldWrite<T>() => _id < SerializationContext<T>.Actions.Length ? SerializationContext<T>.Actions[_id].WorldWrite as Action<ComponentTypeWriter, int> : null;
internal Action<ComponentTypeWriter, int, bool> GetWorldWrite<T>() => _id < SerializationContext<T>.Actions.Length ? SerializationContext<T>.Actions[_id].WorldWrite as Action<ComponentTypeWriter, int, bool> : null;

internal Action<EntityWriter, T, Entity, bool> GetEntityWrite<T>() => _id < SerializationContext<T>.Actions.Length ? SerializationContext<T>.Actions[_id].EntityWrite as Action<EntityWriter, T, Entity, bool> : null;

Expand All @@ -105,7 +115,7 @@ public TextSerializationContext Marshal<TIn, TOut>(Func<TIn, TOut> converter)
{
SerializationContext<TIn>.SetWriteActions(
_id,
new Action<ComponentTypeWriter, int>((writer, maxCapacity) => writer.WriteComponent<TOut>(maxCapacity)),
new Action<ComponentTypeWriter, int, bool>((writer, maxCapacity, hasComponent) => writer.WriteComponent(maxCapacity, hasComponent, w => converter(w.Get<TIn>()))),
new Action<EntityWriter, TIn, Entity, bool>((writer, value, owner, isEnabled) => writer.WriteComponent(converter(value), owner, isEnabled)),
new WriteAction<TIn>((StreamWriterWrapper writer, in TIn value) =>
{
Expand Down
Loading

0 comments on commit ab85d19

Please sign in to comment.