feat: Added support for creating views and drawing ImGui elemts from C#

This commit is contained in:
WerWolv
2024-03-11 21:09:45 +01:00
parent a3f550c585
commit e3565d5bcb
8 changed files with 122 additions and 43 deletions

View File

@@ -2,20 +2,18 @@
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace ImHex
{
public class Bookmarks
{
[DllImport(Library.Name)]
private static extern void createBookmarkV1(UInt64 address, UInt64 size, UInt32 color, [MarshalAs(UnmanagedType.LPStr)] string name, [MarshalAs(UnmanagedType.LPStr)] string description);
private static extern void createBookmarkV1(UInt64 address, UInt64 size, UInt32 color, byte[] name, byte[] description);
public static void CreateBookmark(long address, long size, Color color, string name = "", string description = "")
{
unsafe
{
createBookmarkV1((UInt64)address, (UInt64)size, (UInt32)(0xA0 << 24 | color.B << 16 | color.G << 8 | color.R), name, description);
}
createBookmarkV1((UInt64)address, (UInt64)size, (UInt32)(0xA0 << 24 | color.B << 16 | color.G << 8 | color.R), Encoding.UTF8.GetBytes(name), Encoding.UTF8.GetBytes(description));
}
}

View File

@@ -2,6 +2,7 @@
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace ImHex
{
@@ -36,7 +37,7 @@ namespace ImHex
public class Memory
{
private static List<IProvider> _registeredProviders = new();
private static List<Delegate> _registeredProviderDelegates = new();
private static List<Delegate> _registeredDelegates = new();
private delegate void DataAccessDelegate(UInt64 address, IntPtr buffer, UInt64 size);
private delegate UInt64 GetSizeDelegate();
@@ -51,7 +52,7 @@ namespace ImHex
private static extern bool getSelectionV1(IntPtr start, IntPtr end);
[DllImport(Library.Name)]
private static extern int registerProviderV1([MarshalAs(UnmanagedType.LPStr)] string typeName, [MarshalAs(UnmanagedType.LPStr)] string name, IntPtr readFunction, IntPtr writeFunction, IntPtr getSizeFunction);
private static extern void registerProviderV1(byte[] typeName, byte[] name, IntPtr readFunction, IntPtr writeFunction, IntPtr getSizeFunction);
public static byte[] Read(ulong address, ulong size)
@@ -95,22 +96,22 @@ namespace ImHex
}
}
public static int RegisterProvider<T>() where T : IProvider, new()
public static void RegisterProvider<T>() where T : IProvider, new()
{
_registeredProviders.Add(new T());
ref var provider = ref CollectionsMarshal.AsSpan(_registeredProviders)[^1];
_registeredProviderDelegates.Add(new DataAccessDelegate(provider.readRaw));
_registeredProviderDelegates.Add(new DataAccessDelegate(provider.writeRaw));
_registeredProviderDelegates.Add(new GetSizeDelegate(provider.getSize));
_registeredDelegates.Add(new DataAccessDelegate(provider.readRaw));
_registeredDelegates.Add(new DataAccessDelegate(provider.writeRaw));
_registeredDelegates.Add(new GetSizeDelegate(provider.getSize));
return registerProviderV1(
_registeredProviders[^1].getTypeName(),
_registeredProviders[^1].getName(),
Marshal.GetFunctionPointerForDelegate(_registeredProviderDelegates[^3]),
Marshal.GetFunctionPointerForDelegate(_registeredProviderDelegates[^2]),
Marshal.GetFunctionPointerForDelegate(_registeredProviderDelegates[^1])
registerProviderV1(
Encoding.UTF8.GetBytes(provider.getTypeName()),
Encoding.UTF8.GetBytes(provider.getName()),
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^3]),
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^2]),
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^1])
);
}

View File

@@ -1,6 +1,5 @@
#pragma warning disable SYSLIB1054
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
@@ -8,21 +7,31 @@ namespace ImHex
{
public class UI
{
[DllImport(Library.Name)]
private static extern void showMessageBoxV1([MarshalAs(UnmanagedType.LPStr)] string message);
private delegate void DrawContentDelegate();
private static List<Delegate> _registeredDelegates = new();
[DllImport(Library.Name)]
private static extern void showInputTextBoxV1([MarshalAs(UnmanagedType.LPStr)] string title, [MarshalAs(UnmanagedType.LPStr)] string message, StringBuilder buffer, int bufferSize);
private static extern void showMessageBoxV1(byte[] message);
[DllImport(Library.Name)]
private static extern unsafe void showYesNoQuestionBoxV1([MarshalAs(UnmanagedType.LPStr)] string title, [MarshalAs(UnmanagedType.LPStr)] string message, bool* result);
private static extern void showInputTextBoxV1(byte[] title, byte[] message, StringBuilder buffer, int bufferSize);
[DllImport(Library.Name)]
private static extern unsafe void showYesNoQuestionBoxV1(byte[] title, byte[] message, bool* result);
[DllImport(Library.Name)]
private static extern void showToastV1(byte[] message, UInt32 type);
[DllImport(Library.Name)]
private static extern IntPtr getImGuiContextV1();
[DllImport(Library.Name)]
private static extern void registerViewV1(byte[] icon, byte[] name, IntPtr drawFunction);
public static void ShowMessageBox(string message)
{
unsafe
{
showMessageBoxV1(message);
}
showMessageBoxV1(Encoding.UTF8.GetBytes(message));
}
public static bool ShowYesNoQuestionBox(string title, string message)
@@ -30,23 +39,47 @@ namespace ImHex
unsafe
{
bool result = false;
showYesNoQuestionBoxV1(title, message, &result);
showYesNoQuestionBoxV1(Encoding.UTF8.GetBytes(title), Encoding.UTF8.GetBytes(message), &result);
return result;
}
}
public static string? ShowInputTextBox(string title, string message, int maxSize)
{
unsafe
{
StringBuilder buffer = new(maxSize);
showInputTextBoxV1(title, message, buffer, buffer.Capacity);
StringBuilder buffer = new(maxSize);
showInputTextBoxV1(Encoding.UTF8.GetBytes(title), Encoding.UTF8.GetBytes(message), buffer, buffer.Capacity);
if (buffer.Length == 0 || buffer[0] == '\x00')
return null;
else
return buffer.ToString();
}
if (buffer.Length == 0 || buffer[0] == '\x00')
return null;
else
return buffer.ToString();
}
public enum ToastType
{
Info = 0,
Warning = 1,
Error = 2
}
public static void ShowToast(string message, ToastType type = ToastType.Info)
{
showToastV1(Encoding.UTF8.GetBytes(message), (UInt32)type);
}
public static IntPtr GetImGuiContext()
{
return getImGuiContextV1();
}
public static void RegisterView(byte[] icon, string name, Action function)
{
_registeredDelegates.Add(new DrawContentDelegate(function));
registerViewV1(
icon,
Encoding.UTF8.GetBytes(name),
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^1])
);
}
}

View File

@@ -14,12 +14,12 @@
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.89.7.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ImHexLibrary\ImHexLibrary.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.90.1.1" />
</ItemGroup>
</Project>