From 12ddc8214779af06028961c340e12ff27da99e1a Mon Sep 17 00:00:00 2001 From: Roycon Date: Tue, 28 Nov 2023 09:05:52 +0000 Subject: [PATCH] Maybe better --- src/Arch.SourceGen/Fundamentals/StructuralChanges.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Arch.SourceGen/Fundamentals/StructuralChanges.cs b/src/Arch.SourceGen/Fundamentals/StructuralChanges.cs index d75a848..a087391 100644 --- a/src/Arch.SourceGen/Fundamentals/StructuralChanges.cs +++ b/src/Arch.SourceGen/Fundamentals/StructuralChanges.cs @@ -19,11 +19,13 @@ public static StringBuilder AppendWorldAdd(this StringBuilder sb, int amount) var inParameters = new StringBuilder().InsertGenericInParams(amount); var types = new StringBuilder().GenericTypeParams(amount); + var getIds = new StringBuilder(); var setIds = new StringBuilder(); var addEvents = new StringBuilder(); for (var index = 0; index <= amount; index++) { - setIds.AppendLine($"spanBitSet.SetBit(Component.ComponentType.Id);"); + getIds.AppendLine($"var id{index} = Component.ComponentType.Id;"); + setIds.AppendLine($"spanBitSet.SetBit(id{index});"); addEvents.AppendLine($"OnComponentAdded(entity);"); } @@ -36,8 +38,11 @@ public static StringBuilder AppendWorldAdd(this StringBuilder sb, int amount) { var oldArchetype = EntityInfo.GetArchetype(entity.Id); + // Get all the ids here just in case we are adding a new component as this will grow the ComponentRegistry.Size + {{getIds}} + // BitSet to stack/span bitset, size big enough to contain ALL registered components. - Span stack = stackalloc uint[BitSet.RequiredLength(ComponentRegistry.Size + {{amount + 1}})]; + Span stack = stackalloc uint[BitSet.RequiredLength(ComponentRegistry.Size)]; oldArchetype.BitSet.AsSpan(stack); // Create a span bitset, doing it local saves us headache and gargabe