Skip to content

Commit

Permalink
Add AOT-compatible implementation of ConstructRid
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Aug 26, 2024
1 parent 1f6b8fc commit b137a7a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/Util.cs
Original file line number Diff line number Diff line change
@@ -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<ulong, Rid> ConstructRid;

static Util()
Expand All @@ -23,5 +30,16 @@ static Util()
il.Emit(OpCodes.Ret);
ConstructRid = dm.CreateDelegate<Func<ulong, Rid>>();
}
}
#else
public static Rid ConstructRid(ulong id)
{
Debug.Assert(Marshal.SizeOf<Rid>() == sizeof(ulong));
nint ptr = Marshal.AllocHGlobal(Marshal.SizeOf<Rid>());
byte[] bytes = BitConverter.GetBytes(id);
Marshal.Copy(bytes, 0, ptr, bytes.Length);
Rid rv = Marshal.PtrToStructure<Rid>(ptr);
Marshal.FreeHGlobal(ptr);
return rv;
}
#endif
}

0 comments on commit b137a7a

Please sign in to comment.