Open gocha opened 2 years ago
For now, we can use the workaround with table.concat()
, although it may be a bit slow.
function byte_array_to_string(a)
t = {}
for _, v in ipairs(a) do
table.insert(t, string.char(v))
end
return table.concat(t)
end
name = "MainRAM"
size = memory.getmemorydomainsize(name)
data_array = memory.read_bytes_as_array(0, size, name)
data = byte_array_to_string(data_array)
file, errmsg = io.open(name .. ".bin", "wb")
if file then
file:write(data)
file:close()
else
print(errmsg)
end
I'm going to suggest that your use case would be better served by an external tool.
I won't drop my request for Lua improvements, but the information on ExternalTools looks useful to me. Thank you for letting me know. ♥
Need to revisit this for KeraLua.
I need a lua function that reads binary data from memory as a string. I would like to get the entire memory into a variable and do some complex pattern matching and file extraction.
BizHawk provides
memory.read_bytes_as_array()
, but it does not allow me to use the convenience functions in string library to the result. Also, if I try to convert a 2MB table to a string, likestring.char(unpack(tbl))
, it will fail because it has too many arguments.Lua string can handle data containing
\x00
. The standardfile:read()
andfile:write()
also handle strings when reading or writing binary data. I think the behavior of returning a sequence of bytes as a string makes sense.I'm using BizHawk 2.7.
How to implement this?
Actually, I tried to implement this myself and failed. Since the return value of the function is not text, I think I should construct and return a Lua string without converting it to a .NET string. However, since I am not familiar with NLua, I could not figure out how to define such a function.
With your help, I may be able to create a pull request myself.
Thanks for reading.