From a58514d4029f73bd693bda1c4da17fa619e6d44d Mon Sep 17 00:00:00 2001 From: copygirl Date: Tue, 27 Dec 2022 19:04:28 +0100 Subject: [PATCH] Add FlecsException and abort handling --- src/gaemstone.ECS/World.cs | 6 ++++++ src/gaemstone.Utility/FlecsException.cs | 26 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/gaemstone.Utility/FlecsException.cs diff --git a/src/gaemstone.ECS/World.cs b/src/gaemstone.ECS/World.cs index 05dd8b7..cd2b445 100644 --- a/src/gaemstone.ECS/World.cs +++ b/src/gaemstone.ECS/World.cs @@ -1,4 +1,5 @@ using System; +using gaemstone.Utility; using static flecs_hub.flecs; namespace gaemstone.ECS; @@ -17,6 +18,11 @@ public unsafe partial class World public World(params string[] args) { + ecs_os_set_api_defaults(); + var api = ecs_os_get_api(); + api.abort_ = new FnPtr_Void { Pointer = &FlecsAbortException.Callback }; + ecs_os_set_api(&api); + Handle = ecs_init_w_args(args.Length, null); ChildOf = LookupByPathOrThrow("/flecs/core/ChildOf"); diff --git a/src/gaemstone.Utility/FlecsException.cs b/src/gaemstone.Utility/FlecsException.cs new file mode 100644 index 0000000..fa16503 --- /dev/null +++ b/src/gaemstone.Utility/FlecsException.cs @@ -0,0 +1,26 @@ +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace gaemstone.Utility; + +public class FlecsException + : Exception +{ + public FlecsException() : base() { } + public FlecsException(string message) : base(message) { } +} + +public class FlecsAbortException + : FlecsException +{ + private readonly string _stackTrace = new StackTrace(2, true).ToString(); + public override string? StackTrace => _stackTrace; + + private FlecsAbortException() + : base("Abort was called by flecs") { } + + [UnmanagedCallersOnly] + internal static void Callback() + => throw new FlecsAbortException(); +}