From 531aebb54fc78fd88fbed76c83ec5313f4de2e46 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 4 Dec 2022 09:42:35 +1000 Subject: [PATCH] Expose `MemoryDomain.ReadByteRange`'s `Span` overload via `IMemoryApi` --- .../Api/Classes/MemoryApi.cs | 20 +++++++++++++++++++ .../Api/Interfaces/IMemoryApi.cs | 9 +++++++++ 2 files changed, 29 insertions(+) diff --git a/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs b/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs index 3c6725d42d..d8d0ebe100 100644 --- a/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs +++ b/src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs @@ -260,6 +260,26 @@ namespace BizHawk.Client.Common return newBytes; } + public bool ReadByteRange(ulong srcStartOffset, Span dstBuffer, string domain = null) + { + var d = NamedDomainOrCurrent(domain); + if (srcStartOffset >= (ulong) d.Size) + { + LogCallback($"Warning: attempted read of {srcStartOffset} outside the memory size of {d.Size}"); + return false; + } + try + { + d.BulkPeekByte(srcStartOffset, dstBuffer); + return true; + } + catch (Exception e) + { + LogCallback($"bulk-peek threw {e.GetType().Name}: {e.Message}"); + return false; + } + } + public void WriteByteRange(long addr, IReadOnlyList memoryblock, string domain = null) { var d = NamedDomainOrCurrent(domain); diff --git a/src/BizHawk.Client.Common/Api/Interfaces/IMemoryApi.cs b/src/BizHawk.Client.Common/Api/Interfaces/IMemoryApi.cs index ae644937a0..08a082c3c3 100644 --- a/src/BizHawk.Client.Common/Api/Interfaces/IMemoryApi.cs +++ b/src/BizHawk.Client.Common/Api/Interfaces/IMemoryApi.cs @@ -17,6 +17,15 @@ namespace BizHawk.Client.Common uint ReadByte(long addr, string domain = null); IReadOnlyList ReadByteRange(long addr, int length, string domain = null); + + /// copies a region of memory into , starting at and taking as many bytes as will fit in the buffer + /// + /// iff the internal API call threw an exception (for example, if you request bytes beyond the end of the domain).
+ /// If this happens, the state of is undefined. Anywhere from 0 bytes to all of them could have been copied. + /// The exception is when falls outside the domain, as that is checked first and results in a no-op. + ///
+ bool ReadByteRange(ulong srcStartOffset, Span dstBuffer, string domain = null); + float ReadFloat(long addr, string domain = null); int ReadS8(long addr, string domain = null);