resize component storage if necessary

pull/2/head
cosmonaut 2022-04-27 12:42:43 -07:00
parent 2fc1b68f41
commit b30d53e71d
1 changed files with 9 additions and 0 deletions

View File

@ -118,9 +118,18 @@ namespace MoonTools.ECS
public override void Serialize(ComponentStorageState serializedComponentStorage) public override void Serialize(ComponentStorageState serializedComponentStorage)
{ {
ReadOnlySpan<byte> entityIDBytes = MemoryMarshal.Cast<int, byte>(new ReadOnlySpan<int>(entityIDs, 0, nextID)); ReadOnlySpan<byte> entityIDBytes = MemoryMarshal.Cast<int, byte>(new ReadOnlySpan<int>(entityIDs, 0, nextID));
if (entityIDBytes.Length > serializedComponentStorage.EntityIDs.Length)
{
Array.Resize(ref serializedComponentStorage.EntityIDs, entityIDBytes.Length);
}
entityIDBytes.CopyTo(serializedComponentStorage.EntityIDs); entityIDBytes.CopyTo(serializedComponentStorage.EntityIDs);
ReadOnlySpan<byte> componentBytes = MemoryMarshal.Cast<TComponent, byte>(AllComponents()); ReadOnlySpan<byte> componentBytes = MemoryMarshal.Cast<TComponent, byte>(AllComponents());
if (componentBytes.Length > serializedComponentStorage.Components.Length)
{
Array.Resize(ref serializedComponentStorage.Components, componentBytes.Length);
}
componentBytes.CopyTo(serializedComponentStorage.Components); componentBytes.CopyTo(serializedComponentStorage.Components);
serializedComponentStorage.EntityIdToStorageIndex.Clear(); serializedComponentStorage.EntityIdToStorageIndex.Clear();