From be2dcc52f0e4633a62705cedd44f010c1d242909 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Wed, 20 Dec 2017 06:11:59 +0100 Subject: [PATCH] Reduce savestate code --- mednafen/state.cpp | 91 ++++++++++++++++----------------- mednafen/state.h | 23 +++------ mednafen/wswan/eeprom.cpp | 2 +- mednafen/wswan/gfx.cpp | 2 +- mednafen/wswan/interrupt.cpp | 2 +- mednafen/wswan/rtc.cpp | 2 +- mednafen/wswan/sound.cpp | 2 +- mednafen/wswan/v30mz.cpp | 2 +- mednafen/wswan/wswan-memory.cpp | 2 +- 9 files changed, 56 insertions(+), 72 deletions(-) diff --git a/mednafen/state.cpp b/mednafen/state.cpp index 7fe54686..a76ac8d8 100644 --- a/mednafen/state.cpp +++ b/mednafen/state.cpp @@ -481,68 +481,60 @@ static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size) } static int MDFNSS_StateAction_internal(StateMem *st, int load, int data_only, - std::vector §ions) + SSDescriptor *section) { - std::vector::iterator section; - if(load) { char sname[32]; - for(section = sections.begin(); section != sections.end(); section++) - { - uint32_t tmp_size = 0; - int found = 0; - uint32_t total = 0; + uint32_t tmp_size = 0; + int found = 0; + uint32_t total = 0; - while(smem_read(st, (uint8_t *)sname, 32) == 32) - { - if(smem_read32le(st, &tmp_size) != 4) - return(0); + while(smem_read(st, (uint8_t *)sname, 32) == 32) + { + if(smem_read32le(st, &tmp_size) != 4) + return(0); - total += tmp_size + 32 + 4; + total += tmp_size + 32 + 4; - // Yay, we found the section - if(!strncmp(sname, section->name, 32)) + // Yay, we found the section + if(!strncmp(sname, section->name, 32)) + { + if(!ReadStateChunk(st, section->sf, tmp_size)) { - if(!ReadStateChunk(st, section->sf, tmp_size)) - { - printf("Error reading chunk: %s\n", section->name); - return(0); - } - found = 1; - break; - } - else + printf("Error reading chunk: %s\n", section->name); + return(0); + } + found = 1; + break; + } + else + { + if(smem_seek(st, tmp_size, SEEK_CUR) < 0) { - if(smem_seek(st, tmp_size, SEEK_CUR) < 0) - { - puts("Chunk seek failure"); - return(0); - } + puts("Chunk seek failure"); + return(0); } } + } - if(smem_seek(st, -total, SEEK_CUR) < 0) - { - puts("Reverse seek error"); - return(0); - } + if(smem_seek(st, -total, SEEK_CUR) < 0) + { + puts("Reverse seek error"); + return(0); + } - if(!found && !section->optional) // Not found. We are sad! - { - printf("Section missing: %.32s\n", section->name); - return(0); - } + if(!found && !section->optional) // Not found. We are sad! + { + printf("Section missing: %.32s\n", section->name); + return(0); } } else { - for(section = sections.begin(); section != sections.end(); section++) - { - if(!WriteStateChunk(st, section->name, section->sf)) - return(0); - } + if(!WriteStateChunk(st, section->name, section->sf)) + return(0); } return(1); @@ -550,11 +542,14 @@ static int MDFNSS_StateAction_internal(StateMem *st, int load, int data_only, int MDFNSS_StateAction(void *st_p, int load, int data_only, SFORMAT *sf, const char *name, bool optional) { - StateMem *st = (StateMem*)st_p; - std::vector love; + SSDescriptor love; + StateMem *st = (StateMem*)st_p; + + love.sf = sf; + love.name = name; + love.optional = optional; - love.push_back(SSDescriptor(sf, name, optional)); - return MDFNSS_StateAction_internal(st, load, 0, love); + return MDFNSS_StateAction_internal(st, load, 0, &love); } int MDFNSS_SaveSM(void *st_p, int, int, const void*, const void*, const void*) diff --git a/mednafen/state.h b/mednafen/state.h index e9d20f86..8b8b620b 100644 --- a/mednafen/state.h +++ b/mednafen/state.h @@ -90,25 +90,14 @@ INLINE uint32_t SF_FORCE_D(double *) { return(0); } #define SFEND { 0, 0, 0, 0 } /* State-Section Descriptor */ -class SSDescriptor +struct SSDescriptor { - public: - SSDescriptor(SFORMAT *n_sf, const char *n_name, bool n_optional = 0) - { - sf = n_sf; - name = n_name; - optional = n_optional; - } - ~SSDescriptor(void) - { - - } - - SFORMAT *sf; - const char *name; - bool optional; + + SFORMAT *sf; + const char *name; + bool optional; }; -int MDFNSS_StateAction(void *st, int load, int data_only, SFORMAT *sf, const char *name, bool optional = 0); +int MDFNSS_StateAction(void *st, int load, int data_only, SFORMAT *sf, const char *name, bool optional); #endif diff --git a/mednafen/wswan/eeprom.cpp b/mednafen/wswan/eeprom.cpp index bc14f711..7559efc7 100644 --- a/mednafen/wswan/eeprom.cpp +++ b/mednafen/wswan/eeprom.cpp @@ -250,7 +250,7 @@ int WSwan_EEPROMStateAction(StateMem *sm, int load, int data_only) SFEND }; - if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "EEPR")) + if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "EEPR", false)) return(0); return(1); diff --git a/mednafen/wswan/gfx.cpp b/mednafen/wswan/gfx.cpp index 9aa51697..98949e0c 100644 --- a/mednafen/wswan/gfx.cpp +++ b/mednafen/wswan/gfx.cpp @@ -721,7 +721,7 @@ int WSwan_GfxStateAction(StateMem *sm, int load, int data_only) SFEND }; - if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "GFX")) + if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "GFX", false)) return(0); if(load) diff --git a/mednafen/wswan/interrupt.cpp b/mednafen/wswan/interrupt.cpp index 734d8841..4f7e021b 100644 --- a/mednafen/wswan/interrupt.cpp +++ b/mednafen/wswan/interrupt.cpp @@ -138,7 +138,7 @@ int WSwan_InterruptStateAction(StateMem *sm, int load, int data_only) SFEND }; - if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "INTR")) + if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "INTR", false)) return(0); if(load) diff --git a/mednafen/wswan/rtc.cpp b/mednafen/wswan/rtc.cpp index 9716bed8..6bbbea82 100644 --- a/mednafen/wswan/rtc.cpp +++ b/mednafen/wswan/rtc.cpp @@ -118,7 +118,7 @@ int WSwan_RTCStateAction(StateMem *sm, int load, int data_only) SFEND }; - if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "RTC")) + if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "RTC", false)) return(0); return(1); diff --git a/mednafen/wswan/sound.cpp b/mednafen/wswan/sound.cpp index 31765db4..b27742d4 100644 --- a/mednafen/wswan/sound.cpp +++ b/mednafen/wswan/sound.cpp @@ -412,7 +412,7 @@ int WSwan_SoundStateAction(StateMem *sm, int load, int data_only) SFEND }; - if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "PSG")) + if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "PSG", false)) return(0); return(1); diff --git a/mednafen/wswan/v30mz.cpp b/mednafen/wswan/v30mz.cpp index ba3b7709..b38dae44 100644 --- a/mednafen/wswan/v30mz.cpp +++ b/mednafen/wswan/v30mz.cpp @@ -1160,7 +1160,7 @@ int v30mz_StateAction(StateMem *sm, int load, int data_only) PSW = CompressFlags(); - if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "V30")) + if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "V30", false)) return(0); if(load) diff --git a/mednafen/wswan/wswan-memory.cpp b/mednafen/wswan/wswan-memory.cpp index 9b003c3c..6cbe1f2a 100644 --- a/mednafen/wswan/wswan-memory.cpp +++ b/mednafen/wswan/wswan-memory.cpp @@ -595,7 +595,7 @@ int WSwan_MemoryStateAction(StateMem *sm, int load, int data_only) SFEND }; - if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "MEMR")) + if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "MEMR", false)) return(0); if(load)