diff --git a/src/Flecs.NET/Core/BindingContext/Callback.cs b/src/Flecs.NET/Core/BindingContext/Callback.cs index 490a8d23..1630757a 100644 --- a/src/Flecs.NET/Core/BindingContext/Callback.cs +++ b/src/Flecs.NET/Core/BindingContext/Callback.cs @@ -47,24 +47,6 @@ internal void Set(T callback, nint invoker) where T : Delegate Delegate = GCHandle.Alloc(callback); } - internal static void Set(ref Callback dest, nint callback, nint invoker) - { - if (dest != default) - dest.Dispose(); - - dest.Invoker = invoker; - dest.Pointer = callback; - } - - internal static void Set(ref Callback dest, T callback, nint invoker) where T : Delegate - { - if (dest != default) - dest.Dispose(); - - dest.Invoker = invoker; - dest.Delegate = GCHandle.Alloc(callback); - } - public bool Equals(Callback other) { return Delegate.Equals(other.Delegate) && Pointer == other.Pointer && Invoker == other.Invoker; diff --git a/src/Flecs.NET/Core/Ecs/OsApi.cs b/src/Flecs.NET/Core/Ecs/OsApi.cs index 5010ee3d..66f05b9a 100644 --- a/src/Flecs.NET/Core/Ecs/OsApi.cs +++ b/src/Flecs.NET/Core/Ecs/OsApi.cs @@ -59,7 +59,7 @@ internal static void OverrideOsApi() /// The callback. public static void SetAbort(Action callback) { - Callback.Set(ref Context.Abort, callback, Pointers.AbortCallbackDelegate); + Context.Abort.Set(callback, Pointers.AbortCallbackDelegate); } /// @@ -68,7 +68,7 @@ public static void SetAbort(Action callback) /// The callback. public static void SetAbort(delegate* callback) { - Callback.Set(ref Context.Abort, (IntPtr)callback, Pointers.AbortCallbackPointer); + Context.Abort.Set((IntPtr)callback, Pointers.AbortCallbackPointer); } /// @@ -77,7 +77,7 @@ public static void SetAbort(delegate* callback) /// The callback. public static void SetLog(LogCallback callback) { - Callback.Set(ref Context.Log, callback, Pointers.LogCallbackDelegate); + Context.Log.Set(callback, Pointers.LogCallbackDelegate); } /// @@ -86,7 +86,7 @@ public static void SetLog(LogCallback callback) /// The callback. public static void SetLog(delegate* callback) { - Callback.Set(ref Context.Log, (IntPtr)callback, Pointers.LogCallbackPointer); + Context.Log.Set((IntPtr)callback, Pointers.LogCallbackPointer); } private static void DefaultAbort() diff --git a/src/Flecs.NET/Core/Entity.cs b/src/Flecs.NET/Core/Entity.cs index d2bcd871..6fbdb5b7 100644 --- a/src/Flecs.NET/Core/Entity.cs +++ b/src/Flecs.NET/Core/Entity.cs @@ -3797,7 +3797,7 @@ private ref Entity SetInternal(ulong id, T* component) private ref Entity ObserveInternal(ulong eventId, T callback, nint invoker) where T : Delegate { IteratorContext* iteratorContext = Memory.AllocZeroed(1); - Callback.Set(ref iteratorContext->Callback, callback, invoker); + iteratorContext->Callback.Set(callback, invoker); ecs_observer_desc_t desc = default; desc.events[0] = eventId; @@ -3816,7 +3816,7 @@ private ref Entity ObserveInternal(ulong eventId, T callback, nint invoker) w private ref Entity ObserveInternal(ulong eventId, IntPtr callback, nint invoker) where T : Delegate { IteratorContext* iteratorContext = Memory.AllocZeroed(1); - Callback.Set(ref iteratorContext->Callback, callback, invoker); + iteratorContext->Callback.Set(callback, invoker); ecs_observer_desc_t desc = default; desc.events[0] = eventId; diff --git a/src/Flecs.NET/Core/ObserverBuilder.cs b/src/Flecs.NET/Core/ObserverBuilder.cs index 135cd5e4..b30ad94a 100644 --- a/src/Flecs.NET/Core/ObserverBuilder.cs +++ b/src/Flecs.NET/Core/ObserverBuilder.cs @@ -300,7 +300,7 @@ internal ref ObserverBuilder SetCallback(T callback, nint invoker) where T : { FreeCallback(); IteratorContext context = default; - Callback.Set(ref context.Callback, callback, invoker); + context.Callback.Set(callback, invoker); Desc.callback = Pointers.IteratorCallback; Desc.callback_ctx = Memory.Alloc(context); Desc.callback_ctx_free = Pointers.IteratorContextFree; @@ -311,7 +311,7 @@ internal ref ObserverBuilder SetCallback(nint callback, nint invoker) { FreeCallback(); IteratorContext context = default; - Callback.Set(ref context.Callback, callback, invoker); + context.Callback.Set(callback, invoker); Desc.callback = Pointers.IteratorCallback; Desc.callback_ctx = Memory.Alloc(context); Desc.callback_ctx_free = Pointers.IteratorContextFree; @@ -322,7 +322,7 @@ internal ref ObserverBuilder SetRun(T callback, nint invoker) where T : Deleg { FreeRun(); RunContext context = default; - Callback.Set(ref context.Callback, callback, invoker); + context.Callback.Set(callback, invoker); Desc.run = Pointers.RunCallback; Desc.run_ctx = Memory.Alloc(context); Desc.run_ctx_free = Pointers.RunContextFree; @@ -333,7 +333,7 @@ internal ref ObserverBuilder SetRun(nint callback, nint invoker) { FreeRun(); RunContext context = default; - Callback.Set(ref context.Callback, callback, invoker); + context.Callback.Set(callback, invoker); Desc.run = Pointers.RunCallback; Desc.run_ctx = Memory.Alloc(context); Desc.run_ctx_free = Pointers.RunContextFree; diff --git a/src/Flecs.NET/Core/QueryBuilder.cs b/src/Flecs.NET/Core/QueryBuilder.cs index c74990c9..ea887a3b 100644 --- a/src/Flecs.NET/Core/QueryBuilder.cs +++ b/src/Flecs.NET/Core/QueryBuilder.cs @@ -1641,7 +1641,7 @@ public ref QueryBuilder TermAt(int termIndex) /// public ref QueryBuilder OrderBy(ulong component, Ecs.OrderByCallback callback) { - Callback.Set(ref QueryContext.OrderBy, callback, 0); + QueryContext.OrderBy.Set(callback, 0); Desc.order_by_callback = Marshal.GetFunctionPointerForDelegate(callback); Desc.order_by = component; return ref this; @@ -1688,7 +1688,7 @@ public ref QueryBuilder GroupBy() /// public ref QueryBuilder GroupBy(ulong component, Ecs.GroupByCallback callback) { - Callback.Set(ref GroupByContext.GroupBy, callback, Pointers.GroupByCallbackDelegate); + GroupByContext.GroupBy.Set(callback, Pointers.GroupByCallbackDelegate); Desc.group_by_callback = Pointers.GroupByCallback; Desc.group_by = component; return ref this; @@ -1712,7 +1712,7 @@ public ref QueryBuilder GroupBy(Ecs.GroupByCallback callback) /// public ref QueryBuilder OnGroupCreate(Ecs.GroupCreateCallback callback) { - Callback.Set(ref GroupByContext.GroupCreate, callback, Pointers.GroupCreateCallbackDelegate); + GroupByContext.GroupCreate.Set(callback, Pointers.GroupCreateCallbackDelegate); Desc.on_group_create = Pointers.GroupCreateCallback; return ref this; } @@ -1725,7 +1725,7 @@ public ref QueryBuilder OnGroupCreate(Ecs.GroupCreateCallback callback) /// public ref QueryBuilder OnGroupCreate(Ecs.GroupCreateCallback callback) { - Callback.Set(ref GroupByContext.GroupCreate, callback, Pointers.GroupCreateCallbackDelegate); + GroupByContext.GroupCreate.Set(callback, Pointers.GroupCreateCallbackDelegate); Desc.on_group_create = Pointers.GroupCreateCallback; return ref this; } @@ -1737,7 +1737,7 @@ public ref QueryBuilder OnGroupCreate(Ecs.GroupCreateCallback callback) /// public ref QueryBuilder OnGroupDelete(Ecs.GroupDeleteCallback callback) { - Callback.Set(ref GroupByContext.GroupDelete, callback, Pointers.GroupDeleteCallbackDelegate); + GroupByContext.GroupDelete.Set(callback, Pointers.GroupDeleteCallbackDelegate); Desc.on_group_delete = Pointers.GroupDeleteCallback; return ref this; } @@ -1750,7 +1750,7 @@ public ref QueryBuilder OnGroupDelete(Ecs.GroupDeleteCallback callback) /// public ref QueryBuilder OnGroupDelete(Ecs.GroupDeleteCallback callback) { - Callback.Set(ref GroupByContext.GroupDelete, callback, Pointers.GroupDeleteCallbackDelegate); + GroupByContext.GroupDelete.Set(callback, Pointers.GroupDeleteCallbackDelegate); Desc.on_group_delete = Pointers.GroupDeleteCallback; return ref this; } diff --git a/src/Flecs.NET/Core/SystemBuilder.cs b/src/Flecs.NET/Core/SystemBuilder.cs index 83377557..2a72d84b 100644 --- a/src/Flecs.NET/Core/SystemBuilder.cs +++ b/src/Flecs.NET/Core/SystemBuilder.cs @@ -398,7 +398,7 @@ internal ref SystemBuilder SetCallback(T callback, nint invoker) where T : De { FreeCallback(); IteratorContext context = default; - Callback.Set(ref context.Callback, callback, invoker); + context.Callback.Set(callback, invoker); Desc.callback = Pointers.IteratorCallback; Desc.callback_ctx = Memory.Alloc(context); Desc.callback_ctx_free = Pointers.IteratorContextFree; @@ -409,7 +409,7 @@ internal ref SystemBuilder SetCallback(nint callback, nint invoker) { FreeCallback(); IteratorContext context = default; - Callback.Set(ref context.Callback, callback, invoker); + context.Callback.Set(callback, invoker); Desc.callback = Pointers.IteratorCallback; Desc.callback_ctx = Memory.Alloc(context); Desc.callback_ctx_free = Pointers.IteratorContextFree; @@ -421,7 +421,7 @@ internal ref SystemBuilder SetRun(T callback, nint invoker) where T : Delegat { FreeRun(); RunContext context = default; - Callback.Set(ref context.Callback, callback, invoker); + context.Callback.Set(callback, invoker); Desc.run = Pointers.RunCallback; Desc.run_ctx = Memory.Alloc(context); Desc.run_ctx_free = Pointers.RunContextFree; @@ -432,7 +432,7 @@ internal ref SystemBuilder SetRun(nint callback, nint invoker) { FreeRun(); RunContext context = default; - Callback.Set(ref context.Callback, callback, invoker); + context.Callback.Set(callback, invoker); Desc.run = Pointers.RunCallback; Desc.run_ctx = Memory.Alloc(context); Desc.run_ctx_free = Pointers.RunContextFree; diff --git a/src/Flecs.NET/Core/World.cs b/src/Flecs.NET/Core/World.cs index bd518636..07133a0a 100644 --- a/src/Flecs.NET/Core/World.cs +++ b/src/Flecs.NET/Core/World.cs @@ -126,7 +126,7 @@ public void Quit() public void AtFini(Ecs.WorldFinishCallback callback) { WorldFinishContext* context = AllocateWorldFinishContext(); - Callback.Set(ref context->Callback, callback, Pointers.WorldFinishCallbackDelegate); + context->Callback.Set(callback, Pointers.WorldFinishCallbackDelegate); ecs_atfini(Handle, Pointers.WorldFinishCallback, context); } @@ -137,7 +137,7 @@ public void AtFini(Ecs.WorldFinishCallback callback) public void AtFini(delegate* callback) { WorldFinishContext* context = AllocateWorldFinishContext(); - Callback.Set(ref context->Callback, (IntPtr)callback, Pointers.WorldFinishCallbackPointer); + context->Callback.Set((IntPtr)callback, Pointers.WorldFinishCallbackPointer); ecs_atfini(Handle, Pointers.WorldFinishCallback, context); } @@ -2356,7 +2356,7 @@ public Entity MakeAlive(ulong entity) public void RunPostFrame(Ecs.PostFrameCallback callback) { PostFrameContext* postFrameContext = AllocatePostFrameContext(); - Callback.Set(ref postFrameContext->Callback, callback, Pointers.PostFrameCallbackDelegate); + postFrameContext->Callback.Set(callback, Pointers.PostFrameCallbackDelegate); ecs_run_post_frame(Handle, Pointers.PostFrameCallback, postFrameContext); } @@ -2367,7 +2367,7 @@ public void RunPostFrame(Ecs.PostFrameCallback callback) public void RunPostFrame(delegate* callback) { PostFrameContext* postFrameContext = AllocatePostFrameContext(); - Callback.Set(ref postFrameContext->Callback, (nint)callback, Pointers.PostFrameCallbackPointer); + postFrameContext->Callback.Set((nint)callback, Pointers.PostFrameCallbackPointer); ecs_run_post_frame(Handle, Pointers.PostFrameCallback, postFrameContext); }