From b30d53e71df679ba83c62b6bc607682797a09962 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 27 Apr 2022 12:42:43 -0700 Subject: [PATCH] resize component storage if necessary --- src/ComponentStorage.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ComponentStorage.cs b/src/ComponentStorage.cs index 51d5750..3020bb1 100644 --- a/src/ComponentStorage.cs +++ b/src/ComponentStorage.cs @@ -118,9 +118,18 @@ namespace MoonTools.ECS public override void Serialize(ComponentStorageState serializedComponentStorage) { ReadOnlySpan entityIDBytes = MemoryMarshal.Cast(new ReadOnlySpan(entityIDs, 0, nextID)); + + if (entityIDBytes.Length > serializedComponentStorage.EntityIDs.Length) + { + Array.Resize(ref serializedComponentStorage.EntityIDs, entityIDBytes.Length); + } entityIDBytes.CopyTo(serializedComponentStorage.EntityIDs); ReadOnlySpan componentBytes = MemoryMarshal.Cast(AllComponents()); + if (componentBytes.Length > serializedComponentStorage.Components.Length) + { + Array.Resize(ref serializedComponentStorage.Components, componentBytes.Length); + } componentBytes.CopyTo(serializedComponentStorage.Components); serializedComponentStorage.EntityIdToStorageIndex.Clear();