From 20bebd60252958c8e617eedf49bd0851460dfa0e Mon Sep 17 00:00:00 2001 From: libertyernie Date: Tue, 29 Aug 2017 18:00:17 -0500 Subject: [PATCH] Fixes to get it working (I think) --- source/fceuram.cpp | 41 +++++++++++++++++++++++++--------- source/pocketnes/goombasav.cpp | 20 ++++++++++++----- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/source/fceuram.cpp b/source/fceuram.cpp index 3b7371e..7ad152f 100644 --- a/source/fceuram.cpp +++ b/source/fceuram.cpp @@ -87,13 +87,25 @@ bool SaveRAM (char * filepath, bool silent) if (goomba_is_sram(&tag)) { - goomba_cleanup(savebuffer); + void* gba_data = malloc(GOOMBA_COLOR_SRAM_SIZE); + + file = fopen(filepath, "rb"); + fread(gba_data, 1, GOOMBA_COLOR_SRAM_SIZE, file); + fclose(file); + + void* cleaned = goomba_cleanup(gba_data); + if (!cleaned) { + ErrorPrompt(goomba_last_error()); + } else if (cleaned != gba_data) { + memcpy(gba_data, cleaned, GOOMBA_COLOR_SRAM_SIZE); + free(cleaned); + } // Look for just one save file. If there aren't any, or there is more than one, don't read any data. stateheader* sh1 = NULL; stateheader* sh2 = NULL; - stateheader* sh = (stateheader*)(savebuffer + 4); + stateheader* sh = (stateheader*)(gba_data + 4); while (sh && stateheader_plausible(sh)) { if (little_endian_conv_16(sh->type) != GOOMBA_SRAMSAVE) {} else if (sh1 == NULL) { @@ -118,10 +130,15 @@ bool SaveRAM (char * filepath, bool silent) } else { - char* newdata = goomba_new_sav(savebuffer, sh1, savebuffer, datasize); - memcpy(savebuffer, newdata, GOOMBA_COLOR_SRAM_SIZE); - datasize = GOOMBA_COLOR_SRAM_SIZE; - free(newdata); + char* newdata = goomba_new_sav(gba_data, sh1, savebuffer, datasize); + if (!newdata) { + ErrorPrompt(goomba_last_error()); + datasize = 0; + } else { + memcpy(savebuffer, newdata, GOOMBA_COLOR_SRAM_SIZE); + datasize = GOOMBA_COLOR_SRAM_SIZE; + free(newdata); + } } } } @@ -177,7 +194,13 @@ bool LoadRAM (char * filepath, bool silent) // Check to see if this is a PocketNES save file if (goomba_is_sram(savebuffer)) { - goomba_cleanup(savebuffer); + void* cleaned = goomba_cleanup(savebuffer); + if (!cleaned) { + ErrorPrompt(goomba_last_error()); + } else if (cleaned != savebuffer) { + memcpy(savebuffer, cleaned, GOOMBA_COLOR_SRAM_SIZE); + free(cleaned); + } // Look for just one save file. If there aren't any, or there is more than one, don't read any data. stateheader* sh1 = NULL; @@ -185,9 +208,7 @@ bool LoadRAM (char * filepath, bool silent) stateheader* sh = (stateheader*)(savebuffer + 4); while (sh && stateheader_plausible(sh)) { - if (little_endian_conv_16(sh->type) != GOOMBA_SRAMSAVE) { - InfoPrompt("Not sram"); - } + if (little_endian_conv_16(sh->type) != GOOMBA_SRAMSAVE) { } else if (sh1 == NULL) { sh1 = sh; } diff --git a/source/pocketnes/goombasav.cpp b/source/pocketnes/goombasav.cpp index 40a36aa..4ee6a11 100644 --- a/source/pocketnes/goombasav.cpp +++ b/source/pocketnes/goombasav.cpp @@ -191,9 +191,12 @@ const stateheader* stateheader_first(const void* gba_data) { else if (check_le == POCKETNES_STATEID2) check++; else if (check_le == SMSADVANCE_STATEID) check++; - return stateheader_plausible(check) - ? (stateheader*)check - : NULL; + if (stateheader_plausible(check)) { + return (stateheader*)check; + } else { + goomba_error("sh at %p not plausible - value: %08X", gba_data, *(uint32_t*)gba_data); + return NULL; + } } const stateheader** stateheader_scan(const void* gba_data) { @@ -202,6 +205,10 @@ const stateheader** stateheader_scan(const void* gba_data) { memset(headers, 0, psize * 64); const stateheader* sh = stateheader_first(gba_data); + if (sh == NULL) { + free(headers); + return NULL; + } int i = 0; while (stateheader_plausible(sh) && i < 63) { headers[i] = sh; @@ -289,12 +296,15 @@ char* goomba_cleanup(const void* gba_data_param) { goomba_configdata* gcd = NULL; smsadvance_configdata* scd = NULL; - if (cd->size == sizeof(goomba_configdata)) { + if (F16(cd->size) == sizeof(goomba_configdata)) { gcd = (goomba_configdata*)cd; checksum = F32(gcd->sram_checksum); // 0 = clean, postitive = unclean - } else if (cd->size == sizeof(smsadvance_configdata)) { + } else if (F16(cd->size) == sizeof(smsadvance_configdata)) { scd = (smsadvance_configdata*)cd; checksum = F32(scd->sram_checksum); // 0 = clean, postitive = unclean + } else { + goomba_error("Unrecognized size of configdata, cannot clean"); + return NULL; } for (j = 0; headers[j] != NULL; j++) {