Skip to content

Commit

Permalink
Fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseTG committed Sep 3, 2024
1 parent 0986905 commit 10563be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/libretro/api/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def addressof_buffer(buffer: Buffer) -> int:


def memoryview_at(
address: c_char_p | c_void_p | int, size: c_ssize_t | int, readonly=False
address: c_char_p | c_void_p | int | bytes, size: c_ssize_t | int, readonly=False
) -> memoryview:
flags = c_int(0x100 if readonly else 0x200)
return pythonapi.PyMemoryView_FromMemory(cast(address, c_char_p), c_ssize_t(size), flags)
Expand Down
9 changes: 8 additions & 1 deletion src/libretro/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,13 @@ def unserialize(self, data: bytes | bytearray | memoryview | Buffer) -> bool:
"""
buf: memoryview
match data:
case bytes() | bytearray() | Buffer():
case bytes():
buf = memoryview_at(data, len(data), readonly=False)
# HACK! ctypes.Array.from_buffer requires a writable buffer,
# but bytes objects are read-only.
# retro_unserialize isn't supposed to modify the buffer,
# so we can blame undefined behavior if the core tries to write to it anyway.
case bytearray() | Buffer():
buf = memoryview(data)
case memoryview():
buf = data
Expand All @@ -525,6 +531,7 @@ def unserialize(self, data: bytes | bytearray | memoryview | Buffer) -> bool:
buflen = len(buf)
arraytype: Array = c_char * buflen

# TODO: Validate that the buffer wasn't written to, and raise a warning if it was. (Use zlib.crc32)
return self._core.retro_unserialize(byref(arraytype.from_buffer(buf)), buflen)

def cheat_reset(self):
Expand Down

0 comments on commit 10563be

Please sign in to comment.