Skip to content

Commit d2add0e

Browse files
Implement multi-threaded queries and rewrite invoker code
1 parent 092b151 commit d2add0e

File tree

794 files changed

+50684
-78555
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

794 files changed

+50684
-78555
lines changed

src/Flecs.NET.Codegen/Generators/Alert.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public override void Generate()
88
{
99
AddSource($"Alert.Id.g.cs", Id.GenerateExtensions(Type.Alert));
1010
AddSource($"Alert.Entity.g.cs", Entity.GenerateExtensions(Type.Alert));
11+
AddSource($"Alert.Entity.Observe.g.cs", Entity.GenerateObserveFunctions(Type.Alert));
1112
}
1213
}

src/Flecs.NET.Codegen/Generators/BindingContext.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,50 @@ public class BindingContext : GeneratorBase
88
{
99
public override void Generate()
1010
{
11-
for (int i = 0; i < Generator.GenericCount; i++)
11+
for (int i = -1; i < Generator.GenericCount; i++)
1212
{
13-
AddSource($"Functions/T{i}.g.cs", GenerateFunctions(i));
13+
AddSource($"Run/T{i + 1}.g.cs", GenerateRunFunctions(Generator.GetRunCallbacks(), i));
14+
AddSource($"Iter/T{i + 1}.g.cs", GenerateIteratorFunctions(Generator.GetIterCallbacks(i), i));
15+
AddSource($"Each/T{i + 1}.g.cs", GenerateIteratorFunctions(Generator.GetEachCallbacks(i), i));
1416
}
17+
18+
for (int i = -1; i < 1; i++)
19+
{
20+
AddSource($"Observe/T{i + 1}.g.cs", GenerateIteratorFunctions(Generator.GetObserveCallbacks(i), i));
21+
}
22+
}
23+
24+
public static string GenerateRunFunctions(Callback[] callbacks, int i)
25+
{
26+
IEnumerable<string> functions = callbacks.Select((Callback callback) => $$"""
27+
internal static void {{Generator.GetCallbackName(callback, i)}}(ecs_iter_t* iter)
28+
{
29+
RunContext* context = (RunContext*)iter->run_ctx;
30+
{{Generator.GetTypeName(Type.Invoker, i)}}.Run<{{Generator.GetCallbackName(callback)}}>(iter, ({{Generator.GetCallbackType(callback)}})context->Callback);
31+
}
32+
""");
33+
34+
return $$"""
35+
using System;
36+
37+
using static Flecs.NET.Bindings.flecs;
38+
39+
namespace Flecs.NET.Core.BindingContext;
40+
41+
internal static unsafe partial class Functions
42+
{
43+
{{string.Join(Separator.DoubleNewLine, functions)}}
44+
}
45+
""";
1546
}
1647

17-
public static string GenerateFunctions(int i)
48+
public static string GenerateIteratorFunctions(Callback[] callbacks, int i)
1849
{
19-
IEnumerable<string> functions = Generator.CallbacksRunAndIterAndEach.Select((Callback callback) => $$"""
20-
internal static void {{callback}}<{{Generator.TypeParameters[i]}}>(ecs_iter_t* iter)
50+
IEnumerable<string> functions = callbacks.Select((Callback callback) => $$"""
51+
internal static void {{Generator.GetCallbackName(callback, i)}}(ecs_iter_t* iter)
2152
{
22-
{{(Generator.GetCallbackIsRun(callback) ? "RunContext" : "IteratorContext")}}* context = ({{(Generator.GetCallbackIsRun(callback) ? "RunContext" : "IteratorContext")}}*)iter->{{(Generator.GetCallbackIsRun(callback) ? "run_ctx" : "callback_ctx")}};
23-
Invoker.{{Generator.GetInvokerName(callback)}}(iter, ({{Generator.GetCallbackType(callback, i)}}){{(Generator.GetCallbackIsDelegate(callback) ? "context->Callback.Delegate.Target!" : "context->Callback.Pointer")}});
53+
IteratorContext* context = (IteratorContext*)iter->callback_ctx;
54+
{{Generator.GetTypeName(Type.Invoker, i)}}.{{Generator.GetInvokerName(callback)}}<{{Generator.GetCallbackName(callback, i)}}>(iter, ({{Generator.GetCallbackType(callback, i)}})context->Callback);
2455
}
2556
""");
2657

src/Flecs.NET.Codegen/Generators/Component.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public override void Generate()
88
{
99
AddSource($"Component.Id.g.cs", Id.GenerateExtensions(Type.Component));
1010
AddSource($"Component.Entity.g.cs", Entity.GenerateExtensions(Type.Component));
11+
AddSource($"System.Entity.Observe.g.cs", Entity.GenerateObserveFunctions(Type.Component));
1112
}
1213
}

src/Flecs.NET.Codegen/Generators/Ecs.cs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ public class Ecs : GeneratorBase
77
public override void Generate()
88
{
99
for (int i = 0; i < Generator.GenericCount; i++)
10-
{
1110
AddSource($"Delegates/T{i + 1}.g.cs", GenerateDelegates(i));
12-
AddSource($"FetchPointers/T{i + 1}.g.cs", GenerateFetchPointers(i));
13-
}
1411
}
1512

1613
private static string GenerateDelegates(int i)
@@ -132,55 +129,4 @@ public static unsafe partial class Ecs
132129
}
133130
""";
134131
}
135-
136-
private static string GenerateFetchPointers(int i)
137-
{
138-
return $$"""
139-
using System;
140-
141-
using static Flecs.NET.Bindings.flecs;
142-
143-
namespace Flecs.NET.Core;
144-
145-
public static unsafe partial class Ecs
146-
{
147-
internal static bool EnsurePointers<{{Generator.TypeParameters[i]}}>(ecs_world_t* world, ulong e, void** ptrs)
148-
{
149-
{{Generator.EnsurePointers[i]}}
150-
return true;
151-
}
152-
153-
internal static bool GetPointers<{{Generator.TypeParameters[i]}}>(ecs_world_t* world, ulong e, ecs_record_t* r, ecs_table_t* table, void** ptrs)
154-
{
155-
Ecs.Assert(table != null, nameof(ECS_INTERNAL_ERROR));
156-
157-
if (ecs_table_column_count(table) == 0 && !ecs_table_has_flags(table, EcsTableHasSparse))
158-
return false;
159-
160-
ecs_world_t* realWorld = ecs_get_world(world);
161-
162-
{{Generator.IdsArray[i]}}
163-
{{Generator.ColumnsArray[i]}}
164-
165-
for (int i = 0; i < {{i + 1}}; i++)
166-
{
167-
if (columns[i] == -1)
168-
{
169-
void *ptr = ecs_get_mut_id(world, e, ids[i]);
170-
171-
if (ptr == null)
172-
return false;
173-
174-
ptrs[i] = ptr;
175-
continue;
176-
}
177-
178-
ptrs[i] = ecs_record_get_by_column(r, columns[i], 0);
179-
}
180-
181-
return true;
182-
}
183-
}
184-
""";
185-
}
186132
}

src/Flecs.NET.Codegen/Generators/Entity.cs

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,60 @@ public class Entity : GeneratorBase
88
{
99
public override void Generate()
1010
{
11+
AddSource("Entity.Id.g.cs", Id.GenerateExtensions(Type.Entity));
12+
AddSource("Entity.Observe.g.cs", GenerateObserveFunctions(Type.Entity));
13+
1114
for (int i = 0; i < Generator.GenericCount; i++)
1215
{
13-
AddSource($"Entity.Id.g.cs", Id.GenerateExtensions(Type.Entity));
1416
AddSource($"Entity.ComponentCallbacks/T{i + 1}.g.cs", GenerateComponentCallbacks(Type.Entity, i));
1517
}
1618
}
1719

20+
public static string GenerateObserveFunctions(Type type, int i = -1)
21+
{
22+
IEnumerable<string> untyped = Generator.GetObserveCallbacks()
23+
.Select((Callback callback) => $$"""
24+
/// <inheritdoc cref="IEntity{TEntity}.Observe(ulong, Ecs.ObserveEntityCallback)"/>
25+
public void Observe(ulong id, {{Generator.GetCallbackType(callback)}} callback)
26+
{
27+
Ecs.Observe(this, id, callback, &Functions.{{Generator.GetCallbackName(callback)}});
28+
}
29+
30+
/// <inheritdoc cref="IEntity{TEntity}.Observe{T}(Ecs.ObserveEntityCallback)"/>
31+
public void Observe<T0>({{Generator.GetCallbackType(callback)}} callback)
32+
{
33+
Ecs.Observe(this, Type<T0>.Id(World), callback, &Functions.{{Generator.GetCallbackName(callback)}});
34+
}
35+
""");
36+
37+
IEnumerable<string> typed = Generator.GetObserveCallbacks(0)
38+
.Select((Callback callback) => $$"""
39+
/// <inheritdoc cref="IEntity{TEntity}.Observe{T}(Ecs.ObserveEntityCallback)"/>
40+
public void Observe<T0>({{Generator.GetCallbackType(callback)}} callback)
41+
{
42+
Ecs.Observe(this, Type<T0>.Id(World), callback, &Functions.{{Generator.GetCallbackName(callback)}}<T0>);
43+
}
44+
""");
45+
46+
return $$"""
47+
#pragma warning disable CS0693 // Type parameter has the same name as the type parameter from outer type
48+
49+
using System;
50+
51+
namespace Flecs.NET.Core;
52+
53+
public unsafe partial struct {{Generator.GetTypeName(type, i)}}
54+
{
55+
{{string.Join(Separator.DoubleNewLine, untyped.Concat(typed))}}
56+
}
57+
""";
58+
}
59+
1860
public static string GenerateComponentCallbacks(Type type, int i)
1961
{
20-
IEnumerable<string> readAndWrite = Generator.CallbacksReadAndWrite.Select((Callback callback) => $$"""
62+
IEnumerable<string> readAndWrite = Generator.GetReadCallbacks()
63+
.Concat(Generator.GetWriteCallbacks())
64+
.Select((Callback callback) => $$"""
2165
/// <summary>
2266
/// {{Generator.GetInvokerName(callback)}} {{i + 1}} components using the provided callback. <br/><br/>
2367
///
@@ -39,11 +83,11 @@ public static string GenerateComponentCallbacks(Type type, int i)
3983
/// <returns>True if the entity has the specified components.</returns>
4084
public bool {{Generator.GetInvokerName(callback)}}<{{Generator.TypeParameters[i]}}>({{Generator.GetCallbackType(callback, i)}} callback)
4185
{
42-
return Invoker.{{Generator.GetInvokerName(callback)}}(World, Id, callback);
86+
return {{Generator.GetTypeName(Type.Invoker, i)}}.{{Generator.GetInvokerName(callback)}}<{{Generator.GetCallbackName(callback, i)}}>(new Entity(World, Id), callback);
4387
}
4488
""");
4589

46-
IEnumerable<string> insert = Generator.CallbacksInsert.Select((Callback callback) => $$"""
90+
IEnumerable<string> insert = Generator.GetInsertCallbacks().Select((Callback callback) => $$"""
4791
/// <summary>
4892
/// Ensures {{i + 1}} components using the provided callback.<br/><br/>
4993
///
@@ -62,9 +106,9 @@ public static string GenerateComponentCallbacks(Type type, int i)
62106
/// <param name="callback">The callback.</param>
63107
/// {{Generator.XmlTypeParameters[i]}}
64108
/// <returns>Reference to self.</returns>
65-
public ref {{type}} {{Generator.GetInvokerName(callback)}}<{{Generator.TypeParameters[i]}}>({{Generator.GetCallbackType(callback, i)}} callback)
109+
public ref {{Generator.GetTypeName(type)}} {{Generator.GetInvokerName(callback)}}<{{Generator.TypeParameters[i]}}>({{Generator.GetCallbackType(callback, i)}} callback)
66110
{
67-
Invoker.{{Generator.GetInvokerName(callback)}}(World, Id, callback);
111+
{{Generator.GetTypeName(Type.Invoker, i)}}.{{Generator.GetInvokerName(callback)}}<{{Generator.GetCallbackName(callback, i)}}>(new Entity(World, Id), callback);
68112
return ref this;
69113
}
70114
""");
@@ -74,7 +118,7 @@ public static string GenerateComponentCallbacks(Type type, int i)
74118
75119
namespace Flecs.NET.Core;
76120
77-
public unsafe partial struct {{type}}
121+
public unsafe partial struct {{Generator.GetTypeName(type)}}
78122
{
79123
{{string.Join(Separator.DoubleNewLine, readAndWrite.Concat(insert))}}
80124
}
@@ -1707,48 +1751,6 @@ public bool IsChildOf<T>(T value) where T : Enum
17071751
return ref this;
17081752
}
17091753
1710-
/// <inheritdoc cref="Entity.Observe(ulong, Action)"/>
1711-
public ref {{typeName}} Observe(ulong eventId, Action callback)
1712-
{
1713-
Entity.Observe(eventId, callback);
1714-
return ref this;
1715-
}
1716-
1717-
/// <inheritdoc cref="Entity.Observe(ulong, Ecs.ObserveEntityCallback)"/>
1718-
public ref {{typeName}} Observe(ulong eventId, Ecs.ObserveEntityCallback callback)
1719-
{
1720-
Entity.Observe(eventId, callback);
1721-
return ref this;
1722-
}
1723-
1724-
/// <inheritdoc cref="Entity.Observe{T}(Action)"/>
1725-
public ref {{typeName}} Observe<T>(Action callback)
1726-
{
1727-
Entity.Observe<T>(callback);
1728-
return ref this;
1729-
}
1730-
1731-
/// <inheritdoc cref="Entity.Observe{T}(Ecs.ObserveEntityCallback)"/>
1732-
public ref {{typeName}} Observe<T>(Ecs.ObserveEntityCallback callback)
1733-
{
1734-
Entity.Observe<T>(callback);
1735-
return ref this;
1736-
}
1737-
1738-
/// <inheritdoc cref="Entity.Observe{T}(Ecs.ObserveRefCallback{T})"/>
1739-
public ref {{typeName}} Observe<T>(Ecs.ObserveRefCallback<T> callback)
1740-
{
1741-
Entity.Observe(callback);
1742-
return ref this;
1743-
}
1744-
1745-
/// <inheritdoc cref="Entity.Observe{T}(Ecs.ObserveEntityRefCallback{T})"/>
1746-
public ref {{typeName}} Observe<T>(Ecs.ObserveEntityRefCallback<T> callback)
1747-
{
1748-
Entity.Observe(callback);
1749-
return ref this;
1750-
}
1751-
17521754
/// <inheritdoc cref="Entity.EnsurePtr(ulong)"/>
17531755
public void* EnsurePtr(ulong id)
17541756
{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Flecs.NET.Codegen.Helpers;
4+
5+
namespace Flecs.NET.Codegen.Generators;
6+
7+
public class IEntity : GeneratorBase
8+
{
9+
public override void Generate()
10+
{
11+
AddSource("IEntity.Observe.g.cs", GenerateObserveFunctions());
12+
}
13+
14+
public static string GenerateObserveFunctions()
15+
{
16+
IEnumerable<string> untyped = Generator.GetObserveCallbacks()
17+
.Select((Callback callback) => $$"""
18+
/// <summary>
19+
/// Registers a callback to be called when the provided event is signaled.
20+
/// </summary>
21+
/// <param name="id">The event id.</param>
22+
/// <param name="callback">The callback.</param>
23+
/// <returns>Reference to self.</returns>
24+
public void Observe(ulong id, {{Generator.GetCallbackType(callback)}} callback);
25+
26+
/// <summary>
27+
/// Registers a callback to be called when the provided event is signaled.
28+
/// </summary>
29+
/// <param name="callback">The callback.</param>
30+
/// <typeparam name="T0">The event type.</typeparam>
31+
/// <returns>Reference to self.</returns>
32+
public void Observe<T0>({{Generator.GetCallbackType(callback)}} callback);
33+
""");
34+
35+
IEnumerable<string> typed = Generator.GetObserveCallbacks(0)
36+
.Select((Callback callback) => $$"""
37+
/// <summary>
38+
/// Registers a callback to be called when the provided event is signaled.
39+
/// </summary>
40+
/// <param name="callback">The callback.</param>
41+
/// <typeparam name="T0">The event type.</typeparam>
42+
/// <returns>Reference to self.</returns>
43+
public void Observe<T0>({{Generator.GetCallbackType(callback)}} callback);
44+
""");
45+
46+
return $$"""
47+
namespace Flecs.NET.Core;
48+
49+
public unsafe partial interface IEntity<TEntity>
50+
{
51+
{{string.Join(Separator.DoubleNewLine, untyped.Concat(typed))}}
52+
}
53+
""";
54+
}
55+
56+
}

0 commit comments

Comments
 (0)