From 84d5851aec4e1a58ea270f2fb4a009ca78645737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Sat, 28 Jun 2025 11:43:18 -0400 Subject: [PATCH] Serialize WASI file descriptor positions in libretro save states --- binding-sandbox/wasi.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/binding-sandbox/wasi.cpp b/binding-sandbox/wasi.cpp index 9bb8f0b4..1944ffee 100644 --- a/binding-sandbox/wasi.cpp +++ b/binding-sandbox/wasi.cpp @@ -189,6 +189,14 @@ bool wasi_t::sandbox_serialize(void *&data, mkxp_sandbox::wasm_size_t &max_size) if (!::sandbox_serialize((uint8_t)2, data, max_size)) return false; if (!::sandbox_serialize(entry.file_handle()->root, data, max_size)) return false; if (!::sandbox_serialize(entry.file_handle()->file.path(), data, max_size)) return false; + { + PHYSFS_File *file = entry.file_handle()->file.get(); + if (!::sandbox_serialize(file == nullptr ? (int64_t)0 : std::max((int64_t)0, (int64_t)PHYSFS_tell(file)), data, max_size)) return false; + } + { + PHYSFS_File *file = entry.file_handle()->file.get_write(); + if (!::sandbox_serialize(file == nullptr ? (int64_t)0 : std::max((int64_t)0, (int64_t)PHYSFS_tell(file)), data, max_size)) return false; + } } else { ++num_free_handles; } @@ -274,6 +282,22 @@ bool wasi_t::sandbox_deserialize(const void *&data, mkxp_sandbox::wasm_size_t &m return false; } fdtable[i] = {handle, wasi_fd_type::FSFILE}; + { + PHYSFS_File *file = handle->file.get(); + int64_t pos; + if (!::sandbox_deserialize(pos, data, max_size)) return false; + if (pos > 0) { + PHYSFS_seek(file, pos); + } + } + { + PHYSFS_File *file = handle->file.get_write(); + int64_t pos; + if (!::sandbox_deserialize(pos, data, max_size)) return false; + if (file != nullptr && pos > 0) { + PHYSFS_seek(file, pos); + } + } } else { return false; }