refactor: new code style in SafeMemory.cs

This commit is contained in:
goat 2021-03-31 03:08:15 +02:00
parent 8d0baaddb5
commit 679d9b3d6a

View file

@ -1,16 +1,14 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
namespace Dalamud namespace Dalamud
{ {
/// <summary> /// <summary>
/// Class facilitating safe memory access /// Class facilitating safe memory access.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Attention! The performance of these methods is severely worse than regular <see cref="Marshal"/> calls. /// Attention! The performance of these methods is severely worse than regular <see cref="Marshal"/> calls.
@ -52,14 +50,16 @@ namespace Dalamud
} }
/// <summary> /// <summary>
/// Read an object of the specified struct from the current process. /// Read an object of the specified struct from the current process.
/// </summary> /// </summary>
/// <typeparam name="T">The type of the struct.</typeparam> /// <typeparam name="T">The type of the struct.</typeparam>
/// <param name="address">The address to read from.</param> /// <param name="address">The address to read from.</param>
/// <param name="result">The resulting object.</param> /// <param name="result">The resulting object.</param>
/// <returns>Whether or not the read succeeded.</returns> /// <returns>Whether or not the read succeeded.</returns>
public static bool Read<T>(IntPtr address, out T result) where T : struct { public static bool Read<T>(IntPtr address, out T result) where T : struct
if (!ReadBytes(address, SizeCache<T>.Size, out var buffer)) { {
if (!ReadBytes(address, SizeCache<T>.Size, out var buffer))
{
result = default; result = default;
return false; return false;
} }
@ -203,7 +203,7 @@ namespace Dalamud
/// </summary> /// </summary>
/// <typeparam name="T">The type to inspect.</typeparam> /// <typeparam name="T">The type to inspect.</typeparam>
/// <returns>The size of the passed type.</returns> /// <returns>The size of the passed type.</returns>
public static int SizeOf<T>() where T: struct public static int SizeOf<T>() where T : struct
{ {
return SizeCache<T>.Size; return SizeCache<T>.Size;
} }
@ -236,15 +236,18 @@ namespace Dalamud
private sealed class LocalMemory : IDisposable private sealed class LocalMemory : IDisposable
{ {
private IntPtr hGlobal;
private readonly int size; private readonly int size;
private IntPtr hGlobal;
public LocalMemory(int size) public LocalMemory(int size)
{ {
this.size = size; this.size = size;
this.hGlobal = Marshal.AllocHGlobal(this.size); this.hGlobal = Marshal.AllocHGlobal(this.size);
} }
~LocalMemory() => this.Dispose();
public byte[] Read() public byte[] Read()
{ {
var bytes = new byte[this.size]; var bytes = new byte[this.size];
@ -253,9 +256,10 @@ namespace Dalamud
} }
public T Read<T>(int offset = 0) => (T)Marshal.PtrToStructure(this.hGlobal + offset, typeof(T)); public T Read<T>(int offset = 0) => (T)Marshal.PtrToStructure(this.hGlobal + offset, typeof(T));
public void Write(byte[] data, int index = 0) => Marshal.Copy(data, index, this.hGlobal, this.size); public void Write(byte[] data, int index = 0) => Marshal.Copy(data, index, this.hGlobal, this.size);
public void Write<T>(T data, int offset = 0) => Marshal.StructureToPtr(data, this.hGlobal + offset, false); public void Write<T>(T data, int offset = 0) => Marshal.StructureToPtr(data, this.hGlobal + offset, false);
~LocalMemory() => Dispose();
public void Dispose() public void Dispose()
{ {