changeset 2475:a634985b1df3

Fix some issues with libretro serialization
author Michael Pavone <pavone@retrodev.com>
date Sat, 02 Mar 2024 15:33:14 -0800
parents b9bec5771993
children aaf7bb58ffca
files libblastem.c
diffstat 1 files changed, 13 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libblastem.c	Sat Mar 02 15:18:25 2024 -0800
+++ b/libblastem.c	Sat Mar 02 15:33:14 2024 -0800
@@ -194,6 +194,11 @@
 	if (!serialize_size_cache) {
 		uint8_t *tmp = current_system->serialize(current_system, &serialize_size_cache);
 		free(tmp);
+		//VDP serialization size can vary based on FIFO fullness
+		//add a little fudge factor here to ensure the returned size is always >= the actual size
+		serialize_size_cache += 64;
+		//We need to store the actual size saved too
+		serialize_size_cache += sizeof(size_t);
 	}
 	return serialize_size_cache;
 }
@@ -202,21 +207,23 @@
  * retro_serialize_size(), it should return false, true otherwise. */
 RETRO_API bool retro_serialize(void *data, size_t size)
 {
-	size_t actual_size;
-	uint8_t *tmp = current_system->serialize(current_system, &actual_size);
-	if (actual_size > size) {
+	size_t *buffer = data;
+	uint8_t *tmp = current_system->serialize(current_system, buffer);
+	if (*buffer > size) {
+		fprintf(stderr, "retro_serialize failed frontend size %d, actual size %d\n", (int)size, (int)*buffer);
 		free(tmp);
 		return 0;
 	}
-	memcpy(data, tmp, actual_size);
+	memcpy(buffer + 1, tmp, *buffer);
 	free(tmp);
 	return 1;
 }
 
 RETRO_API bool retro_unserialize(const void *data, size_t size)
 {
-	current_system->deserialize(current_system, (uint8_t *)data, size);
-	return 0;
+	const size_t *buffer = data;
+	current_system->deserialize(current_system, (uint8_t *)(buffer + 1), *buffer);
+	return 1;
 }
 
 RETRO_API void retro_cheat_reset(void)