Skip to content

Commit

Permalink
Fix incorrect Run callback handler pointer and reformat iterator methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BeanCheeseBurrito committed Aug 4, 2024
1 parent ae715a1 commit 1e2f40b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 128 deletions.
139 changes: 69 additions & 70 deletions src/Flecs.NET/Core/ObserverBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using Flecs.NET.Core.BindingContext;
using Flecs.NET.Utilities;
using static Flecs.NET.Bindings.flecs;
Expand All @@ -14,7 +13,7 @@ public unsafe partial struct ObserverBuilder : IDisposable, IEquatable<ObserverB
private ecs_world_t* _world;
private ecs_observer_desc_t _desc;
private QueryBuilder _queryBuilder;
internal int EventCount;
private int _eventCount;

/// <summary>
/// A reference to the world.
Expand All @@ -39,7 +38,7 @@ public ObserverBuilder(ecs_world_t* world)
{
_world = world;
_desc = default;
EventCount = default;
_eventCount = default;
_queryBuilder = new QueryBuilder(world);
}

Expand Down Expand Up @@ -78,10 +77,10 @@ public void Dispose()
/// <returns></returns>
public ref ObserverBuilder Event(ulong @event)
{
if (EventCount >= 8)
if (_eventCount >= 8)
Ecs.Error("Can't create an observer with more than 8 events.");

Desc.events[EventCount++] = @event;
Desc.events[_eventCount++] = @event;
return ref this;
}

Expand Down Expand Up @@ -118,183 +117,183 @@ public ref ObserverBuilder Ctx(void* data)
}

/// <summary>
/// Creates a observer with the provided Iter callback.
/// Creates an observer with the provided Run callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Iter(Action callback)
public Observer Run(Action callback)
{
return SetCallback(callback, Pointers.ActionCallbackDelegate).Build();
}

/// <summary>
/// Creates a observer with the provided Iter callback.
/// Creates an observer with the provided Run callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Iter(Ecs.IterCallback callback)
public Observer Run(delegate*<void> callback)
{
return SetCallback(callback, Pointers.IterCallbackDelegate).Build();
return SetCallback((IntPtr)callback, Pointers.ActionCallbackPointer).Build();
}

/// <summary>
/// Creates a routine with the provided Each callback.
/// Creates an observer with the provided Run callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created routine.</returns>
public Observer Each(Action callback)
/// <param name="run">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Run(Ecs.RunCallback run)
{
return SetCallback(callback, Pointers.ActionCallbackDelegate).Build();
return SetRun(run, Pointers.RunCallbackDelegate).Build();
}

/// <summary>
/// Creates a observer with the provided Each callback.
/// Creates an observer with the provided Run callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Each(Ecs.EachEntityCallback callback)
public Observer Run(delegate*<Iter, void> callback)
{
return SetCallback(callback, Pointers.EachEntityCallbackDelegate).Build();
return SetRun((IntPtr)callback, Pointers.RunCallbackPointer).Build();
}

/// <summary>
/// Creates a observer with the provided Each callback.
/// Sets a run callback. .Iter() or .Each() must be called after this to build the observer.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Each(Ecs.EachIterCallback callback)
/// <param name="run">The callback.</param>
/// <returns>Reference to self.</returns>
public ref ObserverBuilder Run(Ecs.RunDelegateCallback run)
{
return SetCallback(callback, Pointers.EachIterCallbackDelegate).Build();
return ref SetRun(run, Pointers.RunDelegateCallbackDelegate);
}

/// <summary>
/// Creates a routine with the provided Run callback.
/// Sets a run callback. .Iter() or .Each() must be called after this to build the observer.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created routine.</returns>
public Observer Run(Action callback)
/// <returns>Reference to self.</returns>
public ref ObserverBuilder Run(delegate*<Iter, Action<Iter>, void> callback)
{
return SetCallback(callback, Pointers.ActionCallbackDelegate).Build();
return ref SetRun((IntPtr)callback, Pointers.RunDelegateCallbackPointer);
}

/// <summary>
/// Creates a observer with the provided Run callback.
/// Sets a run callback. .Iter() or .Each() must be called after this to build the observer.
/// </summary>
/// <param name="run">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Run(Ecs.RunCallback run)
/// <param name="callback">The callback.</param>
/// <returns>Reference to self.</returns>
public ref ObserverBuilder Run(Ecs.RunPointerCallback callback)
{
return SetRun(run, Pointers.RunCallbackDelegate).Build();
return ref SetRun(callback, Pointers.RunPointerCallbackDelegate);
}

/// <summary>
/// Sets a run callback. .Iter() or .Each() must be called after this to build the observer.
/// </summary>
/// <param name="run">The callback.</param>
/// <param name="callback">The callback.</param>
/// <returns>Reference to self.</returns>
public ref ObserverBuilder Run(Ecs.RunDelegateCallback run)
public ref ObserverBuilder Run(delegate*<Iter, delegate*<Iter, void>, void> callback)
{
return ref SetRun(run, Pointers.RunDelegateCallbackDelegate);
return ref SetRun((IntPtr)callback, Pointers.RunPointerCallbackPointer);
}

/// <summary>
/// Creates a observer with the provided Iter callback.
/// Creates an observer with the provided Iter callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Iter(delegate*<void> callback)
public Observer Iter(Action callback)
{
return SetCallback((IntPtr)callback, Pointers.ActionCallbackPointer).Build();
return SetCallback(callback, Pointers.ActionCallbackDelegate).Build();
}

/// <summary>
/// Creates a observer with the provided Iter callback.
/// Creates an observer with the provided Iter callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Iter(delegate*<Iter, void> callback)
public Observer Iter(delegate*<void> callback)
{
return SetCallback((IntPtr)callback, Pointers.IterCallbackPointer).Build();
return SetCallback((IntPtr)callback, Pointers.ActionCallbackPointer).Build();
}

/// <summary>
/// Creates a routine with the provided Each callback.
/// Creates an observer with the provided Iter callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created routine.</returns>
public Observer Each(delegate*<void> callback)
/// <returns>The created observer.</returns>
public Observer Iter(Ecs.IterCallback callback)
{
return SetCallback((IntPtr)callback, Pointers.ActionCallbackPointer).Build();
return SetCallback(callback, Pointers.IterCallbackDelegate).Build();
}

/// <summary>
/// Creates a observer with the provided Each callback.
/// Creates an observer with the provided Iter callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Each(delegate*<Entity, void> callback)
public Observer Iter(delegate*<Iter, void> callback)
{
return SetCallback((IntPtr)callback, Pointers.EachEntityCallbackPointer).Build();
return SetCallback((IntPtr)callback, Pointers.IterCallbackPointer).Build();
}

/// <summary>
/// Creates a observer with the provided Each callback.
/// Creates an observer with the provided Each callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Each(delegate*<Iter, int, void> callback)
public Observer Each(Action callback)
{
return SetCallback((IntPtr)callback, Pointers.EachIterCallbackPointer).Build();
return SetCallback(callback, Pointers.ActionCallbackDelegate).Build();
}

/// <summary>
/// Creates a routine with the provided Run callback.
/// Creates an observer with the provided Each callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created routine.</returns>
public Observer Run(delegate*<void> callback)
/// <returns>The created observer.</returns>
public Observer Each(delegate*<void> callback)
{
return SetCallback((IntPtr)callback, Pointers.ActionCallbackPointer).Build();
}

/// <summary>
/// Creates a observer with the provided Run callback.
/// Creates an observer with the provided Each callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>The created observer.</returns>
public Observer Run(delegate*<Iter, void> callback)
public Observer Each(Ecs.EachEntityCallback callback)
{
return SetRun((IntPtr)callback, Pointers.RunCallbackPointer).Build();
return SetCallback(callback, Pointers.EachEntityCallbackDelegate).Build();
}

/// <summary>
/// Sets a run callback. .Iter() or .Each() must be called after this to build the observer.
/// Creates an observer with the provided Each callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>Reference to self.</returns>
public ref ObserverBuilder Run(delegate*<Iter, Action<Iter>, void> callback)
/// <returns>The created observer.</returns>
public Observer Each(delegate*<Entity, void> callback)
{
return ref SetRun((IntPtr)callback, Pointers.RunDelegateCallbackPointer);
return SetCallback((IntPtr)callback, Pointers.EachEntityCallbackPointer).Build();
}

/// <summary>
/// Sets a run callback. .Iter() or .Each() must be called after this to build the observer.
/// Creates an observer with the provided Each callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>Reference to self.</returns>
public ref ObserverBuilder Run(delegate*<Iter, delegate*<Iter, void>, void> callback)
/// <returns>The created observer.</returns>
public Observer Each(Ecs.EachIterCallback callback)
{
return ref SetRun((IntPtr)callback, Pointers.RunPointerCallbackPointer);
return SetCallback(callback, Pointers.EachIterCallbackDelegate).Build();
}

/// <summary>
/// Sets a run callback. .Iter() or .Each() must be called after this to build the observer.
/// Creates an observer with the provided Each callback.
/// </summary>
/// <param name="callback">The callback.</param>
/// <returns>Reference to self.</returns>
public ref ObserverBuilder Run(Ecs.RunPointerCallback callback)
/// <returns>The created observer.</returns>
public Observer Each(delegate*<Iter, int, void> callback)
{
return ref SetRun(callback, Pointers.RunPointerCallbackPointer);
return SetCallback((IntPtr)callback, Pointers.EachIterCallbackPointer).Build();
}

private ref ObserverBuilder SetCallback<T>(T callback, IntPtr invoker) where T : Delegate
Expand Down Expand Up @@ -349,7 +348,7 @@ private Observer Build()

fixed (ecs_observer_desc_t* ptr = &Desc)
{
Ecs.Assert(EventCount != 0, "Observer cannot have zero events. Use ObserverBuilder.Event() to add events.");
Ecs.Assert(_eventCount != 0, "Observer cannot have zero events. Use ObserverBuilder.Event() to add events.");
Ecs.Assert(ptr->query.terms[0] != default || ptr->query.expr != null, "Observers require at least 1 term.");
return new Observer(World, ecs_observer_init(World, ptr));
}
Expand Down
Loading

0 comments on commit 1e2f40b

Please sign in to comment.