feat: Added cross-platform .NET scripts support (#1185)

This PR intends to add support for .NET scripts that can extend ImHex's
functionality in a portable and cross-platform way.

---------

Co-authored-by: Justus Garbe <55301990+Nowilltolife@users.noreply.github.com>
This commit is contained in:
Nik
2023-07-15 14:29:14 +02:00
committed by GitHub
parent 4e3b8111fd
commit 5171bea0bf
36 changed files with 1219 additions and 34 deletions

View File

@@ -0,0 +1,22 @@
#pragma warning disable SYSLIB1054
using System.Drawing;
using System.Runtime.InteropServices;
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);
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);
}
}
}
}

View File

@@ -0,0 +1,7 @@
namespace ImHex
{
public static class Library
{
public const string Name = "script_loader.hexplug";
}
}

View File

@@ -0,0 +1,62 @@
#pragma warning disable SYSLIB1054
using System.Drawing;
using System.Runtime.InteropServices;
namespace ImHex
{
public class Memory
{
[DllImport(Library.Name)]
private static extern void readMemoryV1(UInt64 address, UInt64 size, IntPtr buffer);
[DllImport(Library.Name)]
private static extern void writeMemoryV1(UInt64 address, UInt64 size, IntPtr buffer);
[DllImport(Library.Name)]
private static extern bool getSelectionV1(IntPtr start, IntPtr end);
public static byte[] Read(ulong address, ulong size)
{
byte[] bytes = new byte[size];
unsafe
{
fixed (byte* buffer = bytes)
{
readMemoryV1(address, size, (IntPtr)buffer);
}
}
return bytes;
}
public static void Write(ulong address, byte[] bytes)
{
unsafe
{
fixed (byte* buffer = bytes)
{
writeMemoryV1(address, (UInt64)bytes.Length, (IntPtr)buffer);
}
}
}
public static (UInt64, UInt64)? GetSelection()
{
unsafe
{
UInt64 start = 0, end = 0;
if (!getSelectionV1((nint)(&start), (nint)(&end)))
{
return null;
}
return (start, end);
}
}
}
}

View File

@@ -0,0 +1,53 @@
#pragma warning disable SYSLIB1054
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace ImHex
{
public class UI
{
[DllImport(Library.Name)]
private static extern void showMessageBoxV1([MarshalAs(UnmanagedType.LPStr)] string message);
[DllImport(Library.Name)]
private static extern void showInputTextBoxV1([MarshalAs(UnmanagedType.LPStr)] string title, [MarshalAs(UnmanagedType.LPStr)] string message, StringBuilder buffer, int bufferSize);
[DllImport(Library.Name)]
private static extern unsafe void showYesNoQuestionBoxV1([MarshalAs(UnmanagedType.LPStr)] string title, [MarshalAs(UnmanagedType.LPStr)] string message, bool* result);
public static void ShowMessageBox(string message)
{
unsafe
{
showMessageBoxV1(message);
}
}
public static bool ShowYesNoQuestionBox(string title, string message)
{
unsafe
{
bool result = false;
showYesNoQuestionBoxV1(title, 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);
if (buffer.Length == 0 || buffer[0] == '\x00')
return null;
else
return buffer.ToString();
}
}
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>Main</AssemblyName>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<SelfContained>true</SelfContained>
</PropertyGroup>
<PropertyGroup>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,10 @@
using ImHex;
using System.Drawing;
class Script
{
public static void Main()
{
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net7.0\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<History>True|2023-06-16T15:24:52.9876162Z;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>