diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs index 16ff4745f2..73e42a788d 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/MemoryLuaLibrary.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Linq; using NLua; @@ -46,24 +47,26 @@ namespace BizHawk.Client.Common [LuaMethod("writebyte", "Writes the given value to the given address as an unsigned byte")] public void WriteByte(long addr, uint value, string domain = null) => APIs.Memory.WriteByte(addr, value, domain); - [LuaMethodExample("local nlmemrea = memory.readbyterange( 0x100, 30, mainmemory.getname( ) );")] + [LuaDeprecatedMethod] [LuaMethod("readbyterange", "Reads the address range that starts from address, and is length long. Returns a zero-indexed table containing the read values (an array of bytes.)")] public LuaTable ReadByteRange(long addr, int length, string domain = null) => _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, domain)); - [LuaMethodExample("local nlmemrea = memory.readbyterangetable( 0x100, 30, 0x100, mainmemory.getname( ) );")] - [LuaMethod("readbyterangetable", "Reads the address range that starts from address, and is length long. Returns the result into a table of key value pairs (where the index is the first key.)")] - public LuaTable ReadByteRangeTable(long addr, int length, long index, string domain = null) => _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, domain), index); + [LuaMethodExample("local bytes = memory.read_bytes_as_array(0x100, 30, \"WRAM\");")] + [LuaMethod("read_bytes_as_array", "Reads length bytes starting at addr into an array-like table (1-indexed).")] + public LuaTable ReadBytesAsArray(long addr, int length, string domain = null) + => _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, domain), indexFrom: 1); - /// TODO C# version requires a contiguous address range - [LuaMethodExample("")] + [LuaMethodExample("local bytes = memory.read_bytes_as_dict(0x100, 30, \"WRAM\");")] + [LuaMethod("read_bytes_as_dict", "Reads length bytes starting at addr into a dict-like table (where the keys are the addresses, relative to the start of the domain).")] + public LuaTable ReadBytesAsDict(long addr, int length, string domain = null) + => _th.MemoryBlockToTable(APIs.Memory.ReadByteRange(addr, length, domain), addr); + + [LuaDeprecatedMethod] [LuaMethod("writebyterange", "Writes the given values to the given addresses as unsigned bytes")] public void WriteByteRange(LuaTable memoryblock, string domain = null) { #if true - foreach (var (addr, v) in _th.EnumerateEntries(memoryblock)) - { - APIs.Memory.WriteByte(LuaInt(addr), (uint) v, domain); - } + WriteBytesAsDict(memoryblock, domain); #else var d = string.IsNullOrEmpty(domain) ? Domain : DomainList[VerifyMemoryDomain(domain)]; if (d.CanPoke()) @@ -88,6 +91,21 @@ namespace BizHawk.Client.Common #endif } + [LuaMethodExample("memory.write_bytes_as_array(0x100, { 0xAB, 0x12, 0xCD, 0x34 });")] + [LuaMethod("write_bytes_as_array", "Writes sequential bytes starting at addr.")] + public void WriteBytesAsArray(long addr, LuaTable bytes, string domain = null) + => APIs.Memory.WriteByteRange(addr, _th.EnumerateValues(bytes).Select(d => (byte) d).ToList(), domain); + + [LuaMethodExample("memory.write_bytes_as_dict({ [0x100] = 0xAB, [0x104] = 0xCD, [0x106] = 0x12, [0x107] = 0x34, [0x108] = 0xEF });")] + [LuaMethod("write_bytes_as_dict", "Writes bytes at arbitrary addresses (the keys of the given table are the addresses, relative to the start of the domain).")] + public void WriteBytesAsDict(LuaTable addrMap, string domain = null) + { + foreach (var (addr, v) in _th.EnumerateEntries(addrMap)) + { + APIs.Memory.WriteByte(LuaInt(addr), (uint) v, domain); + } + } + [LuaMethodExample("local simemrea = memory.readfloat( 0x100, false, mainmemory.getname( ) );")] [LuaMethod("readfloat", "Reads the given address as a 32-bit float value from the main memory domain with th e given endian")] public float ReadFloat(long addr, bool bigendian, string domain = null) diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs index 4d6768036a..6bd84727c1 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/MainMemoryLuaLibrary.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Linq; using BizHawk.Emulation.Common; @@ -43,24 +44,26 @@ namespace BizHawk.Client.Common [LuaMethod("writebyte", "Writes the given value to the given address as an unsigned byte")] public void WriteByte(long addr, uint value) => APIs.Memory.WriteByte(addr, value, MainMemName); - [LuaMethodExample("local nlmairea = mainmemory.readbyterange( 0x100, 64 );")] + [LuaDeprecatedMethod] [LuaMethod("readbyterange", "Reads the address range that starts from address, and is length long. Returns a zero-indexed table containing the read values (an array of bytes.)")] public LuaTable ReadByteRange(long addr, int length) => _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, MainMemName)); - [LuaMethodExample("local nlmairea = mainmemory.readbyterangetable( 0x100, 64, 0x100 );")] - [LuaMethod("readbyterangetable", "Reads the address range that starts from address, and is length long. Returns the result into a table of key value pairs (where the index is the first key.)")] - public LuaTable ReadByteRangeTable(long addr, int length, long index) => _th.ListToTable(APIs.Memory.ReadByteRange(addr, length), index); + [LuaMethodExample("local bytes = mainmemory.read_bytes_as_array(0x100, 30);")] + [LuaMethod("read_bytes_as_array", "Reads length bytes starting at addr into an array-like table (1-indexed).")] + public LuaTable ReadBytesAsArray(long addr, int length) + => _th.ListToTable(APIs.Memory.ReadByteRange(addr, length, MainMemName), indexFrom: 1); - /// TODO C# version requires a contiguous address range - [LuaMethodExample("")] + [LuaMethodExample("local bytes = mainmemory.read_bytes_as_dict(0x100, 30);")] + [LuaMethod("read_bytes_as_dict", "Reads length bytes starting at addr into a dict-like table (where the keys are the addresses, relative to the start of the main memory).")] + public LuaTable ReadBytesAsDict(long addr, int length) + => _th.MemoryBlockToTable(APIs.Memory.ReadByteRange(addr, length, MainMemName), addr); + + [LuaDeprecatedMethod] [LuaMethod("writebyterange", "Writes the given values to the given addresses as unsigned bytes")] public void WriteByteRange(LuaTable memoryblock) { #if true - foreach (var (addr, v) in _th.EnumerateEntries(memoryblock)) - { - APIs.Memory.WriteByte(LuaInt(addr), (uint) v, MainMemName); - } + WriteBytesAsDict(memoryblock); #else var d = Domain; if (d.CanPoke()) @@ -85,6 +88,21 @@ namespace BizHawk.Client.Common #endif } + [LuaMethodExample("mainmemory.write_bytes_as_array(0x100, { 0xAB, 0x12, 0xCD, 0x34 });")] + [LuaMethod("write_bytes_as_array", "Writes sequential bytes starting at addr.")] + public void WriteBytesAsArray(long addr, LuaTable bytes) + => APIs.Memory.WriteByteRange(addr, _th.EnumerateValues(bytes).Select(d => (byte) d).ToList(), MainMemName); + + [LuaMethodExample("mainmemory.write_bytes_as_dict({ [0x100] = 0xAB, [0x104] = 0xCD, [0x106] = 0x12, [0x107] = 0x34, [0x108] = 0xEF });")] + [LuaMethod("write_bytes_as_dict", "Writes bytes at arbitrary addresses (the keys of the given table are the addresses, relative to the start of the main memory).")] + public void WriteBytesAsDict(LuaTable addrMap) + { + foreach (var (addr, v) in _th.EnumerateEntries(addrMap)) + { + APIs.Memory.WriteByte(LuaInt(addr), (uint) v, MainMemName); + } + } + [LuaMethodExample("local simairea = mainmemory.readfloat(0x100, false);")] [LuaMethod("readfloat", "Reads the given address as a 32-bit float value from the main memory domain with th e given endian")] public float ReadFloat(long addr, bool bigendian) diff --git a/src/BizHawk.Client.Common/lua/NLuaTableHelper.cs b/src/BizHawk.Client.Common/lua/NLuaTableHelper.cs index 637225d6d8..0ad95fd7a4 100644 --- a/src/BizHawk.Client.Common/lua/NLuaTableHelper.cs +++ b/src/BizHawk.Client.Common/lua/NLuaTableHelper.cs @@ -39,13 +39,23 @@ namespace BizHawk.Client.Common public IEnumerable EnumerateValues(LuaTable table) => table.Values.Cast(); - public LuaTable ListToTable(IList list, long indexFrom = 0) + public LuaTable ListToTable(IList list, int indexFrom = 0) { var table = _lua.NewTable(); for (int i = 0, l = list.Count; i != l; i++) table[indexFrom + i] = list[i]; return table; } + public LuaTable MemoryBlockToTable(IReadOnlyList bytes, long startAddr) + { + var length = bytes.Count; + var table = CreateTable(); + var iArray = 0; + var iDict = startAddr; + while (iArray < length) table[(double) iDict++] = bytes[iArray++]; + return table; + } + public LuaTable ObjectToTable(object obj) { var table = _lua.NewTable();