2024-03-13 19:49:48 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
2024-03-11 21:09:45 +01:00
|
|
|
|
using System.Text;
|
2023-07-15 14:29:14 +02:00
|
|
|
|
|
|
|
|
|
namespace ImHex
|
|
|
|
|
{
|
2024-03-10 22:05:26 +01:00
|
|
|
|
public interface IProvider
|
|
|
|
|
{
|
|
|
|
|
void readRaw(UInt64 address, IntPtr buffer, UInt64 size)
|
|
|
|
|
{
|
|
|
|
|
unsafe
|
|
|
|
|
{
|
|
|
|
|
Span<byte> data = new(buffer.ToPointer(), (int)size);
|
|
|
|
|
read(address, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void writeRaw(UInt64 address, IntPtr buffer, UInt64 size)
|
|
|
|
|
{
|
|
|
|
|
unsafe
|
|
|
|
|
{
|
|
|
|
|
ReadOnlySpan<byte> data = new(buffer.ToPointer(), (int)size);
|
|
|
|
|
write(address, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void read(UInt64 address, Span<byte> data);
|
|
|
|
|
void write(UInt64 address, ReadOnlySpan<byte> data);
|
|
|
|
|
|
|
|
|
|
UInt64 getSize();
|
|
|
|
|
|
|
|
|
|
string getTypeName();
|
|
|
|
|
string getName();
|
|
|
|
|
}
|
2024-03-12 23:17:49 +01:00
|
|
|
|
public partial class Memory
|
2023-07-15 14:29:14 +02:00
|
|
|
|
{
|
2024-03-10 22:05:26 +01:00
|
|
|
|
private static List<IProvider> _registeredProviders = new();
|
2024-03-11 21:09:45 +01:00
|
|
|
|
private static List<Delegate> _registeredDelegates = new();
|
2024-03-10 22:05:26 +01:00
|
|
|
|
|
|
|
|
|
private delegate void DataAccessDelegate(UInt64 address, IntPtr buffer, UInt64 size);
|
|
|
|
|
private delegate UInt64 GetSizeDelegate();
|
|
|
|
|
|
2024-03-12 23:17:49 +01:00
|
|
|
|
[LibraryImport("ImHex")]
|
|
|
|
|
private static partial void readMemoryV1(UInt64 address, UInt64 size, IntPtr buffer);
|
2023-07-15 14:29:14 +02:00
|
|
|
|
|
2024-03-12 23:17:49 +01:00
|
|
|
|
[LibraryImport("ImHex")]
|
|
|
|
|
private static partial void writeMemoryV1(UInt64 address, UInt64 size, IntPtr buffer);
|
2023-07-15 14:29:14 +02:00
|
|
|
|
|
2024-03-12 23:17:49 +01:00
|
|
|
|
[LibraryImport("ImHex")]
|
|
|
|
|
private static partial int getSelectionV1(IntPtr start, IntPtr end);
|
2024-03-10 22:05:26 +01:00
|
|
|
|
|
2024-03-12 23:17:49 +01:00
|
|
|
|
[LibraryImport("ImHex")]
|
|
|
|
|
private static partial void registerProviderV1(byte[] typeName, byte[] name, IntPtr readFunction, IntPtr writeFunction, IntPtr getSizeFunction);
|
2023-07-15 14:29:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2024-03-12 23:17:49 +01:00
|
|
|
|
if (getSelectionV1((nint)(&start), (nint)(&end)) == 0)
|
2023-07-15 14:29:14 +02:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (start, end);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-10 22:05:26 +01:00
|
|
|
|
|
2024-03-11 21:09:45 +01:00
|
|
|
|
public static void RegisterProvider<T>() where T : IProvider, new()
|
2024-03-10 22:05:26 +01:00
|
|
|
|
{
|
|
|
|
|
_registeredProviders.Add(new T());
|
|
|
|
|
|
|
|
|
|
ref var provider = ref CollectionsMarshal.AsSpan(_registeredProviders)[^1];
|
|
|
|
|
|
2024-03-11 21:09:45 +01:00
|
|
|
|
_registeredDelegates.Add(new DataAccessDelegate(provider.readRaw));
|
|
|
|
|
_registeredDelegates.Add(new DataAccessDelegate(provider.writeRaw));
|
|
|
|
|
_registeredDelegates.Add(new GetSizeDelegate(provider.getSize));
|
2024-03-10 22:05:26 +01:00
|
|
|
|
|
2024-03-11 21:09:45 +01:00
|
|
|
|
registerProviderV1(
|
|
|
|
|
Encoding.UTF8.GetBytes(provider.getTypeName()),
|
|
|
|
|
Encoding.UTF8.GetBytes(provider.getName()),
|
|
|
|
|
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^3]),
|
|
|
|
|
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^2]),
|
|
|
|
|
Marshal.GetFunctionPointerForDelegate(_registeredDelegates[^1])
|
2024-03-10 22:05:26 +01:00
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-15 14:29:14 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|