From 334a02e69a23aed405c64e4ac2f937a98e8788e5 Mon Sep 17 00:00:00 2001 From: Soreepeong <3614868+Soreepeong@users.noreply.github.com> Date: Sat, 2 Aug 2025 20:35:05 +0900 Subject: [PATCH] Use LibraryImport for custom ImGuiNative functinos --- .../Custom/ImDrawCallbackEnum.cs | 4 +- .../Custom/ImGui.Manual.cs | 67 +++++++++---------- .../Custom/ImGui.Misc.cs | 3 +- .../Custom/ImGuiNative.Custom.cs | 39 ++++++++++- 4 files changed, 72 insertions(+), 41 deletions(-) diff --git a/imgui/Dalamud.Bindings.ImGui/Custom/ImDrawCallbackEnum.cs b/imgui/Dalamud.Bindings.ImGui/Custom/ImDrawCallbackEnum.cs index 4865d903c..732cd3c71 100644 --- a/imgui/Dalamud.Bindings.ImGui/Custom/ImDrawCallbackEnum.cs +++ b/imgui/Dalamud.Bindings.ImGui/Custom/ImDrawCallbackEnum.cs @@ -1,6 +1,6 @@ namespace Dalamud.Bindings.ImGui; -public enum ImDrawCallbackEnum +public enum ImDrawCallbackEnum : long { Empty, @@ -11,5 +11,5 @@ public enum ImDrawCallbackEnum /// state, and you want it to be restored. It is not done by default because they are many perfectly useful way of /// altering render state for imgui contents (e.g. changing shader/blending settings before an Image call). /// - ResetRenderState = -1, + ResetRenderState = ImGui.ImDrawCallbackResetRenderState, } diff --git a/imgui/Dalamud.Bindings.ImGui/Custom/ImGui.Manual.cs b/imgui/Dalamud.Bindings.ImGui/Custom/ImGui.Manual.cs index 376b5cafc..7b0aefe97 100644 --- a/imgui/Dalamud.Bindings.ImGui/Custom/ImGui.Manual.cs +++ b/imgui/Dalamud.Bindings.ImGui/Custom/ImGui.Manual.cs @@ -1,4 +1,5 @@ using System.Numerics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; @@ -160,17 +161,15 @@ public unsafe partial class ImGui { var dataBuffer = PointerTuple.Create(&callback); #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type - var r = - ((delegate* unmanaged[Cdecl], void*, byte>)ImGui.funcTable[1277])( - labelPtr, - hintPtr, - bufPtr, - buf.Length, - sizeArg, - flags, - callback == null ? null : &InputTextCallbackStatic, - callback == null ? null : &dataBuffer) != 0; + var r = ImGuiNative.InputTextEx( + labelPtr, + hintPtr, + bufPtr, + buf.Length, + sizeArg, + flags, + callback == null ? null : &InputTextCallbackStatic, + callback == null ? null : &dataBuffer) != 0; label.Dispose(); hint.Dispose(); return r; @@ -189,17 +188,15 @@ public unsafe partial class ImGui { var dataBuffer = PointerTuple.Create(&callback, contextPtr); #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type - var r = - ((delegate* unmanaged[Cdecl], void*, byte>)ImGui.funcTable[1277])( - labelPtr, - hintPtr, - bufPtr, - buf.Length, - sizeArg, - flags, - &InputTextCallbackRefContextStatic, - &dataBuffer) != 0; + var r = ImGuiNative.InputTextEx( + labelPtr, + hintPtr, + bufPtr, + buf.Length, + sizeArg, + flags, + &InputTextCallbackRefContextStatic, + &dataBuffer) != 0; label.Dispose(); hint.Dispose(); return r; @@ -218,17 +215,15 @@ public unsafe partial class ImGui { var dataBuffer = PointerTuple.Create(&callback, contextPtr); #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type - var r = - ((delegate* unmanaged[Cdecl], void*, byte>)ImGui.funcTable[1277])( - labelPtr, - hintPtr, - bufPtr, - buf.Length, - sizeArg, - flags, - &InputTextCallbackInContextStatic, - &dataBuffer) != 0; + var r = ImGuiNative.InputTextEx( + labelPtr, + hintPtr, + bufPtr, + buf.Length, + sizeArg, + flags, + &InputTextCallbackInContextStatic, + &dataBuffer) != 0; label.Dispose(); hint.Dispose(); return r; @@ -445,21 +440,21 @@ public unsafe partial class ImGui return r; } - [UnmanagedCallersOnly] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] private static int InputTextCallbackStatic(ImGuiInputTextCallbackData* data) { ref var dvps = ref PointerTuple.From(data->UserData); return dvps.Item1.Invoke(ref *data); } - [UnmanagedCallersOnly] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] private static int InputTextCallbackRefContextStatic(ImGuiInputTextCallbackData* data) { ref var dvps = ref PointerTuple.From, object>(data->UserData); return dvps.Item1.Invoke(ref *data, ref dvps.Item2); } - [UnmanagedCallersOnly] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])] private static int InputTextCallbackInContextStatic(ImGuiInputTextCallbackData* data) { ref var dvps = ref PointerTuple.From, object>(data->UserData); diff --git a/imgui/Dalamud.Bindings.ImGui/Custom/ImGui.Misc.cs b/imgui/Dalamud.Bindings.ImGui/Custom/ImGui.Misc.cs index 64629ccfc..3e1a3da3c 100644 --- a/imgui/Dalamud.Bindings.ImGui/Custom/ImGui.Misc.cs +++ b/imgui/Dalamud.Bindings.ImGui/Custom/ImGui.Misc.cs @@ -8,8 +8,7 @@ public static unsafe partial class ImGui { public static void AddCallback( ImDrawListPtr self, delegate* callback, void* callbackData = null) => - ((delegate* unmanaged[Cdecl], void*, void>)funcTable - [540])(self, callback, callbackData); + ImGuiNative.AddCallback(self, callback, callbackData); public static void AddCallback( ImDrawListPtr self, delegate* callback, void* callbackData = null) => diff --git a/imgui/Dalamud.Bindings.ImGui/Custom/ImGuiNative.Custom.cs b/imgui/Dalamud.Bindings.ImGui/Custom/ImGuiNative.Custom.cs index f3d0faa16..94d31e8ea 100644 --- a/imgui/Dalamud.Bindings.ImGui/Custom/ImGuiNative.Custom.cs +++ b/imgui/Dalamud.Bindings.ImGui/Custom/ImGuiNative.Custom.cs @@ -1,3 +1,40 @@ +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + namespace Dalamud.Bindings.ImGui; -public static partial class ImGuiNative; +public static unsafe partial class ImGuiNative +{ + private const string LibraryName = "cimgui"; + + static ImGuiNative() + { + if (LibraryName != ImGui.GetLibraryName()) + { + throw new( + $"{nameof(LibraryName)}(={LibraryName})" + + $" does not match " + + $"{nameof(ImGui)}.{nameof(ImGui.GetLibraryName)}(={ImGui.GetLibraryName()})"); + } + } + + [LibraryImport($"{LibraryName}.dll", EntryPoint = "ImDrawList_AddCallback")] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + public static partial void AddCallback( + ImDrawList* self, + delegate* callback, + void* callbackData = null); + + [LibraryImport($"{LibraryName}.dll", EntryPoint = "igInputTextEx")] + [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] + public static partial int InputTextEx( + byte* label, + byte* hint, + byte* buf, + int bufSize, + Vector2 sizeArg, + ImGuiInputTextFlags flags = ImGuiInputTextFlags.None, + delegate* unmanaged[Cdecl] callback = null, + void* userData = null); +}