changeset 2404:6f8400ce7a0f

Fix reload of zipped and gzipped ROMS
author Michael Pavone <pavone@retrodev.com>
date Tue, 02 Jan 2024 18:14:28 -0800
parents 8171409b62ef
children b50fa7602e39
files blastem.c blastem.h render.h render_sdl.c system.h
diffstat 5 files changed, 21 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/blastem.c	Mon Jan 01 20:02:41 2024 -0800
+++ b/blastem.c	Tue Jan 02 18:14:28 2024 -0800
@@ -184,9 +184,10 @@
 	return 0;
 }
 
-uint32_t load_media(const char * filename, system_media *dst, system_type *stype)
+uint32_t load_media(char * filename, system_media *dst, system_type *stype)
 {
 	uint8_t header[10];
+	dst->orig_path = filename;
 	char *ext = path_extension(filename);
 	if (ext && !strcasecmp(ext, "zip")) {
 		free(ext);
@@ -388,7 +389,7 @@
 	}
 }
 
-static void on_drag_drop(const char *filename)
+static void on_drag_drop(char *filename)
 {
 	if (current_system) {
 		if (current_system->next_rom) {
@@ -421,30 +422,16 @@
 
 void reload_media(void)
 {
-	if (!current_system) {
+	if (!current_system || !cart.orig_path) {
 		return;
 	}
 	if (current_system->next_rom) {
 		free(current_system->next_rom);
 	}
-	char const *parts[] = {
-		cart.dir, PATH_SEP, cart.name, ".", cart.extension
-	};
-	char const **start = parts[0] ? parts : parts + 2;
-	int num_parts = parts[0] ? 5 : 3;
-	if (!parts[4]) {
-		num_parts -= 2;
-	}
-	current_system->next_rom = alloc_concat_m(num_parts, start);
+	current_system->next_rom = cart.orig_path;
+	cart.orig_path = NULL;
 	if (cart.chain) {
-		parts[0] = cart.chain->dir;
-		parts[2] = cart.chain->name;
-		parts[4] = cart.chain->extension;
-		start = parts[0] ? parts : parts + 2;
-		num_parts = parts[0] ? 5 : 3;
-		char *lock_on_path = alloc_concat_m(num_parts, start);
-		load_media(lock_on_path, cart.chain, NULL);
-		free(lock_on_path);
+		load_media(cart.chain->orig_path, cart.chain, NULL);
 	}
 	system_request_exit(current_system, 1);
 }
@@ -454,6 +441,7 @@
 	free(lock_on.dir);
 	free(lock_on.name);
 	free(lock_on.extension);
+	free(lock_on.orig_path);
 	if (lock_on_path) {
 		reload_media();
 		cart.chain = &lock_on;
@@ -462,13 +450,14 @@
 		lock_on.dir = NULL;
 		lock_on.name = NULL;
 		lock_on.extension = NULL;
+		lock_on.orig_path = NULL;
 		cart.chain = NULL;
 	}
 }
 
 static uint32_t opts = 0;
 static uint8_t force_region = 0;
-void init_system_with_media(const char *path, system_type force_stype)
+void init_system_with_media(char *path, system_type force_stype)
 {
 	if (game_system) {
 		if (game_system->persist_save) {
@@ -487,6 +476,7 @@
 	free(cart.dir);
 	free(cart.name);
 	free(cart.extension);
+	free(cart.orig_path);
 	system_type stype = SYSTEM_UNKNOWN;
 	if (!(cart.size = load_media(path, &cart, &stype))) {
 		fatal_error("Failed to open %s for reading\n", path);
@@ -681,11 +671,11 @@
 			if (reader_port) {
 				reader_addr = argv[i];
 			} else {
-			if (!load_media(argv[i], &cart, stype == SYSTEM_UNKNOWN ? &stype : NULL)) {
-				fatal_error("Failed to open %s for reading\n", argv[i]);
+				romfname = strdup(argv[i]);
+				if (!load_media(romfname, &cart, stype == SYSTEM_UNKNOWN ? &stype : NULL)) {
+					fatal_error("Failed to open %s for reading\n", argv[i]);
+				}
 			}
-			}
-			romfname = argv[i];
 			loaded = 1;
 		} else if (width < 0) {
 			width = atoi(argv[i]);
@@ -736,6 +726,7 @@
 		if (!romfname) {
 			romfname = "menu.bin";
 		}
+		romfname = strdup(romfname);
 		if (is_absolute_path(romfname)) {
 			if (!(cart.size = load_media(romfname, &cart, &stype))) {
 				fatal_error("Failed to open UI ROM %s for reading", romfname);
@@ -753,6 +744,7 @@
 			cart.dir = path_dirname(romfname);
 			cart.name = basename_no_extension(romfname);
 			cart.extension = path_extension(romfname);
+			cart.orig_path = romfname;
 		}
 		//force system detection, value on command line is only for games not the menu
 		stype = detect_system_type(&cart);
@@ -821,7 +813,6 @@
 			char *next_rom = current_system->next_rom;
 			current_system->next_rom = NULL;
 			init_system_with_media(next_rom, force_stype);
-			free(next_rom);
 			menu = 0;
 			current_system = game_system;
 			current_system->debugger_type = dtype;
--- a/blastem.h	Mon Jan 01 20:02:41 2024 -0800
+++ b/blastem.h	Tue Jan 02 18:14:28 2024 -0800
@@ -17,7 +17,7 @@
 extern uint8_t use_native_states;
 void reload_media(void);
 void lockon_media(char *lock_on_path);
-void init_system_with_media(const char *path, system_type force_stype);
+void init_system_with_media(char *path, system_type force_stype);
 void apply_updated_config(void);
 const system_media *current_media(void);
 
--- a/render.h	Mon Jan 01 20:02:41 2024 -0800
+++ b/render.h	Tue Jan 02 18:14:28 2024 -0800
@@ -94,7 +94,7 @@
 #define RENDER_NOT_MAPPED -2
 #define RENDER_NOT_PLUGGED_IN -3
 
-typedef void (*drop_handler)(const char *filename);
+typedef void (*drop_handler)(char *filename);
 typedef void (*window_close_handler)(uint8_t which);
 typedef void (*ui_render_fun)(void);
 typedef int (*render_thread_fun)(void*);
--- a/render_sdl.c	Mon Jan 01 20:02:41 2024 -0800
+++ b/render_sdl.c	Tue Jan 02 18:14:28 2024 -0800
@@ -958,7 +958,7 @@
 		break;
 	case SDL_DROPFILE:
 		if (drag_drop_handler) {
-			drag_drop_handler(event->drop.file);
+			drag_drop_handler(strdup(event->drop.file));
 		}
 		SDL_free(event->drop.file);
 		break;
--- a/system.h	Mon Jan 01 20:02:41 2024 -0800
+++ b/system.h	Tue Jan 02 18:14:28 2024 -0800
@@ -132,6 +132,7 @@
 	char         *dir;
 	char         *name;
 	char         *extension;
+	char         *orig_path; //Full path before splitting and any extension manipulation
 	system_media *chain;
 	track_info   *tracks;
 	uint8_t      *tmp_buffer;