Add several comm MMF LUA functions

This commit is contained in:
Mickael Laurent 2021-07-16 23:33:06 +02:00 committed by James Groom
parent 457ff87481
commit e79d33bcfd
2 changed files with 33 additions and 2 deletions

View File

@ -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()

View File

@ -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<double>(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<byte>(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")]