From e79d33bcfdd9dac596dce0d60bf7c8621d92ce62 Mon Sep 17 00:00:00 2001 From: Mickael Laurent Date: Fri, 16 Jul 2021 23:33:06 +0200 Subject: [PATCH] Add several comm MMF LUA functions --- .../Api/MemoryMappedFiles.cs | 15 ++++++++++++-- .../lua/CommonLibs/CommLuaLibrary.cs | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/BizHawk.Client.Common/Api/MemoryMappedFiles.cs b/src/BizHawk.Client.Common/Api/MemoryMappedFiles.cs index c41f35000b..e3815279c4 100644 --- a/src/BizHawk.Client.Common/Api/MemoryMappedFiles.cs +++ b/src/BizHawk.Client.Common/Api/MemoryMappedFiles.cs @@ -21,10 +21,21 @@ namespace BizHawk.Client.Common public string ReadFromFile(string filename, int expectedSize) { - using var viewAccessor = MemoryMappedFile.OpenExisting(filename).CreateViewAccessor(); + var bytes = ReadBytesFromFile(filename, expectedSize); + return Encoding.UTF8.GetString(bytes); + } + + public byte[] ReadBytesFromFile(string filename, int expectedSize) + { + if (!_mmfFiles.TryGetValue(filename, out var mmfFile)) + { + mmfFile = _mmfFiles[filename] = MemoryMappedFile.OpenExisting(filename); + } + + using var viewAccessor = mmfFile.CreateViewAccessor(0, expectedSize, MemoryMappedFileAccess.Read); var bytes = new byte[expectedSize]; viewAccessor.ReadArray(0, bytes, 0, expectedSize); - return Encoding.UTF8.GetString(bytes); + return bytes; } public int ScreenShotToFile() diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs index 78d1de1af0..93bfd34e1c 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs @@ -153,11 +153,31 @@ namespace BizHawk.Client.Common { return APIs.Comm.MMF.WriteToFile(mmf_filename, outputString); } + [LuaMethod("mmfWriteBytes", "Write bytes to a memory mapped file")] + public int MmfWriteBytes(string mmf_filename, LuaTable byteArray) + { + return APIs.Comm.MMF.WriteToFile(mmf_filename, _th.EnumerateValues(byteArray).Select(d => (byte)d).ToArray()); + } + [LuaMethod("mmfCopyFromMemory", "Copy a section of the memory to a memory mapped file")] + public int MmfCopyFromMemory(string mmf_filename, long addr, int length, string domain) + { + return APIs.Comm.MMF.WriteToFile(mmf_filename, APIs.Memory.ReadByteRange(addr, length, domain).ToArray()); + } + [LuaMethod("mmfCopyToMemory", "Copy a memory mapped file to a section of the memory")] + public void MmfCopyToMemory(string mmf_filename, long addr, int length, string domain) + { + APIs.Memory.WriteByteRange(addr, new List(APIs.Comm.MMF.ReadBytesFromFile(mmf_filename, length)), domain); + } [LuaMethod("mmfRead", "Reads a string from a memory mapped file")] public string MmfRead(string mmf_filename, int expectedSize) { return APIs.Comm.MMF.ReadFromFile(mmf_filename, expectedSize); } + [LuaMethod("mmfReadBytes", "Reads bytes from a memory mapped file")] + public LuaTable MmfReadBytes(string mmf_filename, int expectedSize) + { + return _th.ListToTable(APIs.Comm.MMF.ReadBytesFromFile(mmf_filename, expectedSize)); + } // All HTTP related methods [LuaMethod("httpTest", "tests HTTP connections")]