Skip to content

Commit

Permalink
Refactored code, Added Signature.Add and Signature.Remove.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Nov 4, 2024
1 parent 3874955 commit 8cf706d
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 399 deletions.
109 changes: 0 additions & 109 deletions src/Arch.SourceGen/Fundamentals/StructuralChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,115 +2,6 @@ namespace Arch.SourceGen;

public static class StructuralChangesExtensions
{
public static StringBuilder AppendWorldAdds(this StringBuilder sb, int amount)
{
for (var index = 1; index < amount; index++)
{
sb.AppendWorldAdd(index);
}

return sb;
}

public static StringBuilder AppendWorldAdd(this StringBuilder sb, int amount)
{
var generics = new StringBuilder().GenericWithoutBrackets(amount);
var parameters = new StringBuilder().GenericInDefaultParams(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++)
{
getIds.AppendLine($"var id{index} = Component<T{index}>.ComponentType.Id;");
setIds.AppendLine($"spanBitSet.SetBit(id{index});");
addEvents.AppendLine($"OnComponentAdded<T{index}>(entity);");
}

var template =
$$"""
[SkipLocalsInit]
[StructuralChange]
public void Add<{{generics}}>(Entity entity, {{parameters}})
{
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<uint> stack = stackalloc uint[BitSet.RequiredLength(ComponentRegistry.Size)];
oldArchetype.BitSet.AsSpan(stack);
// Create a span bitset, doing it local saves us headache and gargabe
var spanBitSet = new SpanBitSet(stack);
{{setIds}}
if (!TryGetArchetype(spanBitSet.GetHashCode(), out var newArchetype))
newArchetype = GetOrCreate(oldArchetype.Types.Add({{types}}));
Move(entity, oldArchetype, newArchetype, out var newSlot);
newArchetype.Set<{{generics}}>(ref newSlot, {{inParameters}});
{{addEvents}}
}
""";

return sb.AppendLine(template);
}

public static StringBuilder AppendWorldRemoves(this StringBuilder sb, int amount)
{
for (var index = 1; index < amount; index++)
{
sb.AppendWorldRemove(index);
}

return sb;
}

public static StringBuilder AppendWorldRemove(this StringBuilder sb, int amount)
{
var generics = new StringBuilder().GenericWithoutBrackets(amount);
var types = new StringBuilder().GenericTypeParams(amount);

var removes = new StringBuilder();
var events = new StringBuilder();
for (var index = 0; index <= amount; index++)
{
removes.AppendLine($"spanBitSet.ClearBit(Component<T{index}>.ComponentType.Id);");
events.AppendLine($"OnComponentRemoved<T{index}>(entity);");
}

var template =
$$"""
[SkipLocalsInit]
[StructuralChange]
public void Remove<{{generics}}>(Entity entity)
{
var oldArchetype = EntityInfo.GetArchetype(entity.Id);
// BitSet to stack/span bitset, size big enough to contain ALL registered components.
Span<uint> stack = stackalloc uint[oldArchetype.BitSet.Length];
oldArchetype.BitSet.AsSpan(stack);
// Create a span bitset, doing it local saves us headache and gargabe
var spanBitSet = new SpanBitSet(stack);
{{removes}}
if (!TryGetArchetype(spanBitSet.GetHashCode(), out var newArchetype))
newArchetype = GetOrCreate(oldArchetype.Types.Remove({{types}}));
{{events}}
Move(entity, oldArchetype, newArchetype, out _);
}
""";

return sb.AppendLine(template);
}

public static StringBuilder AppendEntityAdds(this StringBuilder sb, int amount)
{
Expand Down
98 changes: 0 additions & 98 deletions src/Arch.SourceGen/Queries/AddWithQueryDescription.cs

This file was deleted.

94 changes: 0 additions & 94 deletions src/Arch.SourceGen/Queries/RemoveWithQueryDescription.cs

This file was deleted.

4 changes: 0 additions & 4 deletions src/Arch.SourceGen/QueryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public partial class World
{{new StringBuilder().AppendWorldHases(25)}}
{{new StringBuilder().AppendWorldGets(25)}}
{{new StringBuilder().AppendWorldSets(25)}}
{{new StringBuilder().AppendWorldAdds(25)}}
{{new StringBuilder().AppendWorldRemoves(25)}}
{{new StringBuilder().AppendQueryMethods(25)}}
{{new StringBuilder().AppendEntityQueryMethods(25)}}
Expand All @@ -111,8 +109,6 @@ public partial class World
{{new StringBuilder().AppendHpeParallelQuerys(25)}}
{{new StringBuilder().AppendSetWithQueryDescriptions(25)}}
{{new StringBuilder().AppendAddWithQueryDescriptions(25)}}
{{new StringBuilder().AppendRemoveWithQueryDescriptions(25)}}
}
public partial struct QueryDescription
Expand Down
4 changes: 2 additions & 2 deletions src/Arch.Tests/ArchetypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ public void CopyToShift([Values(1111,2222,3333)] int sourceAmount, [Values(1111,
}

// Calculate their slots and position of copied entity.
var sourceSlot = source.LastSlot;
var destinationSlot = destination.LastSlot;
var sourceSlot = source.CurrentSlot;
var destinationSlot = destination.CurrentSlot;
destinationSlot++;
var resultSlot = Slot.Shift(sourceSlot, source.EntitiesPerChunk, destinationSlot, destination.EntitiesPerChunk);

Expand Down
36 changes: 36 additions & 0 deletions src/Arch/Arch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ Merged arrays in EntityInfo for increased performance when creating and destroyi
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>World.CreateBulk.cs</LastGenOutput>
</None>
<None Update="Templates\World.RemoveWithQueryDescription.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>World.RemoveWithQueryDescription.cs</LastGenOutput>
</None>
<None Update="Templates\World.AddWithQueryDescription.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>World.AddWithQueryDescription.cs</LastGenOutput>
</None>
<None Update="Templates\World.Add.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>World.Add.cs</LastGenOutput>
</None>
<None Update="Templates\World.Remove.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>World.Remove.cs</LastGenOutput>
</None>
</ItemGroup>

<ItemGroup>
Expand All @@ -156,6 +172,26 @@ Merged arrays in EntityInfo for increased performance when creating and destroyi
<DesignTime>True</DesignTime>
<DependentUpon>World.CreateBulk.tt</DependentUpon>
</Compile>
<Compile Update="Templates\World.Add.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>World.Add.tt</DependentUpon>
</Compile>
<Compile Update="Templates\World.RemoveWithQueryDescription.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>World.RemoveWithQueryDescription.tt</DependentUpon>
</Compile>
<Compile Update="Templates\World.AddWithQueryDescription.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>World.AddWithQueryDescription.tt</DependentUpon>
</Compile>
<Compile Update="Templates\World.Remove.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>World.Remove.tt</DependentUpon>
</Compile>
</ItemGroup>

</Project>
Loading

0 comments on commit 8cf706d

Please sign in to comment.