From b137a7a1d6458edb475760416f0bfa495def2155 Mon Sep 17 00:00:00 2001 From: Patrick Dawson Date: Mon, 26 Aug 2024 22:00:59 +0200 Subject: [PATCH] Add AOT-compatible implementation of ConstructRid --- .../imgui-godot/ImGuiGodot/Internal/Util.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/addons/imgui-godot/ImGuiGodot/Internal/Util.cs b/addons/imgui-godot/ImGuiGodot/Internal/Util.cs index 690069e9..b588876c 100644 --- a/addons/imgui-godot/ImGuiGodot/Internal/Util.cs +++ b/addons/imgui-godot/ImGuiGodot/Internal/Util.cs @@ -1,13 +1,20 @@ -#if GODOT_PC using Godot; using System; +#if GODOT_PC using System.Reflection; using System.Reflection.Emit; +#else +using System.Diagnostics; +using System.Runtime.InteropServices; +#endif namespace ImGuiGodot.Internal; internal static class Util { +#if GODOT_PC + // this is ~15x faster, so keep it for non-AOT platforms + public static readonly Func ConstructRid; static Util() @@ -23,5 +30,16 @@ static Util() il.Emit(OpCodes.Ret); ConstructRid = dm.CreateDelegate>(); } -} +#else + public static Rid ConstructRid(ulong id) + { + Debug.Assert(Marshal.SizeOf() == sizeof(ulong)); + nint ptr = Marshal.AllocHGlobal(Marshal.SizeOf()); + byte[] bytes = BitConverter.GetBytes(id); + Marshal.Copy(bytes, 0, ptr, bytes.Length); + Rid rv = Marshal.PtrToStructure(ptr); + Marshal.FreeHGlobal(ptr); + return rv; + } #endif +}