Skip to content

Commit

Permalink
Reduce savestate code
Browse files Browse the repository at this point in the history
  • Loading branch information
inactive123 committed Dec 20, 2017
1 parent b5abaaa commit be2dcc5
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 72 deletions.
91 changes: 43 additions & 48 deletions mednafen/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,80 +481,75 @@ static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size)
}

static int MDFNSS_StateAction_internal(StateMem *st, int load, int data_only,
std::vector <SSDescriptor> &sections)
SSDescriptor *section)
{
std::vector<SSDescriptor>::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);
}

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 <SSDescriptor> 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*)
Expand Down
23 changes: 6 additions & 17 deletions mednafen/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion mednafen/wswan/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion mednafen/wswan/gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mednafen/wswan/interrupt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mednafen/wswan/rtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion mednafen/wswan/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion mednafen/wswan/v30mz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mednafen/wswan/wswan-memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit be2dcc5

Please sign in to comment.