mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 13:37:42 -05:00
impr: Cleanup .NET script library and API
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
using ImHex;
|
||||
|
||||
public class Library
|
||||
public static class Library
|
||||
{
|
||||
public static void Initialize()
|
||||
{
|
||||
Logger.RedirectConsole();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IScript {
|
||||
|
||||
static void Main() { }
|
||||
static void OnLoad() { }
|
||||
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using System.Text;
|
||||
|
||||
namespace ImHex
|
||||
{
|
||||
public partial class Logger
|
||||
public static partial class Logger
|
||||
{
|
||||
[LibraryImport("ImHex")]
|
||||
private static partial void logPrintV1(byte[] message);
|
||||
|
||||
@@ -33,8 +33,8 @@ namespace ImHex
|
||||
}
|
||||
public partial class Memory
|
||||
{
|
||||
private static List<IProvider> _registeredProviders = new();
|
||||
private static List<Delegate> _registeredDelegates = new();
|
||||
private static readonly List<IProvider> RegisteredProviders = new();
|
||||
private static readonly List<Delegate> RegisteredDelegates = new();
|
||||
|
||||
private delegate void DataAccessDelegate(UInt64 address, IntPtr buffer, UInt64 size);
|
||||
private delegate UInt64 GetSizeDelegate();
|
||||
@@ -95,20 +95,20 @@ namespace ImHex
|
||||
|
||||
public static void RegisterProvider<T>() where T : IProvider, new()
|
||||
{
|
||||
_registeredProviders.Add(new T());
|
||||
RegisteredProviders.Add(new T());
|
||||
|
||||
ref var provider = ref CollectionsMarshal.AsSpan(_registeredProviders)[^1];
|
||||
ref var provider = ref CollectionsMarshal.AsSpan(RegisteredProviders)[^1];
|
||||
|
||||
_registeredDelegates.Add(new DataAccessDelegate(provider.readRaw));
|
||||
_registeredDelegates.Add(new DataAccessDelegate(provider.writeRaw));
|
||||
_registeredDelegates.Add(new GetSizeDelegate(provider.getSize));
|
||||
RegisteredDelegates.Add(new DataAccessDelegate(provider.readRaw));
|
||||
RegisteredDelegates.Add(new DataAccessDelegate(provider.writeRaw));
|
||||
RegisteredDelegates.Add(new GetSizeDelegate(provider.getSize));
|
||||
|
||||
registerProviderV1(
|
||||
Encoding.UTF8.GetBytes(provider.getTypeName()),
|
||||
Encoding.UTF8.GetBytes(provider.getName()),
|
||||
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^3]),
|
||||
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^2]),
|
||||
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^1])
|
||||
Marshal.GetFunctionPointerForDelegate(RegisteredDelegates[^3]),
|
||||
Marshal.GetFunctionPointerForDelegate(RegisteredDelegates[^2]),
|
||||
Marshal.GetFunctionPointerForDelegate(RegisteredDelegates[^1])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace ImHex
|
||||
private delegate void DrawContentDelegate();
|
||||
private delegate void ActionDelegate();
|
||||
|
||||
private static List<Delegate> _registeredDelegates = new();
|
||||
private static readonly List<Delegate> RegisteredDelegates = new();
|
||||
|
||||
[LibraryImport("ImHex")]
|
||||
private static partial void showMessageBoxV1(byte[] message);
|
||||
@@ -48,22 +48,17 @@ namespace ImHex
|
||||
|
||||
public static string? ShowInputTextBox(string title, string message, int maxSize)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var buffer = new byte[maxSize];
|
||||
GCHandle pinnedArray = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
||||
showInputTextBoxV1(Encoding.UTF8.GetBytes(title), Encoding.UTF8.GetBytes(message), pinnedArray.AddrOfPinnedObject(), maxSize);
|
||||
pinnedArray.Free();
|
||||
var buffer = new byte[maxSize];
|
||||
GCHandle pinnedArray = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
||||
showInputTextBoxV1(Encoding.UTF8.GetBytes(title), Encoding.UTF8.GetBytes(message), pinnedArray.AddrOfPinnedObject(), maxSize);
|
||||
pinnedArray.Free();
|
||||
|
||||
if (buffer.Length == 0 || buffer[0] == '\x00')
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
}
|
||||
if (buffer.Length == 0 || buffer[0] == '\x00')
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
}
|
||||
|
||||
public enum ToastType
|
||||
@@ -85,22 +80,22 @@ namespace ImHex
|
||||
|
||||
public static void RegisterView(byte[] icon, string name, Action function)
|
||||
{
|
||||
_registeredDelegates.Add(new DrawContentDelegate(function));
|
||||
RegisteredDelegates.Add(new DrawContentDelegate(function));
|
||||
registerViewV1(
|
||||
icon,
|
||||
Encoding.UTF8.GetBytes(name),
|
||||
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^1])
|
||||
Marshal.GetFunctionPointerForDelegate(RegisteredDelegates[^1])
|
||||
);
|
||||
}
|
||||
|
||||
public static void AddMenuItem(byte[] icon, string menuName, string itemName, Action function)
|
||||
{
|
||||
_registeredDelegates.Add(new ActionDelegate(function));
|
||||
RegisteredDelegates.Add(new ActionDelegate(function));
|
||||
addMenuItemV1(
|
||||
icon,
|
||||
Encoding.UTF8.GetBytes(menuName),
|
||||
Encoding.UTF8.GetBytes(itemName),
|
||||
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^1])
|
||||
Marshal.GetFunctionPointerForDelegate(RegisteredDelegates[^1])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user