Simplify savestate code

This commit is contained in:
twinaphex 2017-12-20 05:47:53 +01:00
parent 181b8470fb
commit a0a6dad863
18 changed files with 75 additions and 93 deletions

View File

@ -958,7 +958,7 @@ int input_StateAction( StateMem* sm, const unsigned load, const bool data_only )
SFEND
};
success = MDFNSS_StateAction( sm, load, data_only, StateRegs, "LIBRETRO-INPUT" );
success = MDFNSS_StateAction( sm, load, data_only, StateRegs, "LIBRETRO-INPUT", false);
// ok?
return success;

View File

@ -1538,7 +1538,7 @@ MDFN_COLD int LibRetro_StateAction( StateMem* sm, const unsigned load, const boo
SOUND_StateAction(sm, load, data_only);
CART_StateAction(sm, load, data_only);
//
success = MDFNSS_StateAction(sm, load, data_only, StateRegs, "MAIN");
success = MDFNSS_StateAction(sm, load, data_only, StateRegs, "MAIN", false);
if ( success == 0 ) {
log_cb( RETRO_LOG_ERROR, "Failed to load MAIN state objects.\n" );
return 0;

View File

@ -103,7 +103,7 @@ void M68K::StateAction(StateMem* sm, const unsigned load, const bool data_only,
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, sname);
MDFNSS_StateAction(sm, load, data_only, StateRegs, sname, false);
}
void M68K::LoadOldState(const uint8* osm)

View File

@ -97,7 +97,7 @@ static MDFN_COLD void StateAction(StateMem* sm, const unsigned load, const bool
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_AR4MP");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_AR4MP", false);
if(load)
{

View File

@ -73,7 +73,7 @@ static MDFN_COLD void StateAction(StateMem* sm, const unsigned load, const bool
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_BACKUP");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_BACKUP", false);
if(load)
{

View File

@ -59,7 +59,7 @@ static MDFN_COLD void StateAction(StateMem* sm, const unsigned load, const bool
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_CS1RAM");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_CS1RAM", false);
}
void CART_CS1RAM_Init(CartInfo* c)

View File

@ -63,7 +63,7 @@ static MDFN_COLD void StateAction(StateMem* sm, const unsigned load, const bool
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_EXTRAM");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CART_EXTRAM", false);
}
void CART_ExtRAM_Init(CartInfo* c, bool R4MiB)

View File

@ -3884,7 +3884,7 @@ void CDB_StateAction(StateMem* sm, const unsigned load, const bool data_only)
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CDB");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "CDB", false);
if(load)
{

View File

@ -1596,7 +1596,7 @@ void SS_SCSP::StateAction(StateMem* sm, const unsigned load, const bool data_onl
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "SCSP");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "SCSP", false);
if(load)
{

View File

@ -2102,7 +2102,7 @@ static MDFN_COLD void SCU_StateAction(StateMem* sm, const unsigned load, const b
//
//
MDFNSS_StateAction(sm, load, data_only, StateRegs, "SCU");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "SCU", false);
if(load)
{

View File

@ -4925,7 +4925,7 @@ void SH7095::StateAction(StateMem* sm, const unsigned load, const bool data_only
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, sname);
MDFNSS_StateAction(sm, load, data_only, StateRegs, sname, false);
if(load)
{

View File

@ -574,7 +574,7 @@ void SMPC_StateAction(StateMem* sm, const unsigned load, const bool data_only)
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "SMPC");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "SMPC", false);
for(unsigned port = 0; port < 2; port++)
{

View File

@ -243,7 +243,7 @@ void SOUND_StateAction(StateMem *sm, const unsigned load, const bool data_only)
next_scsp_time -= SoundCPU.timestamp;
run_until_time -= (int64)SoundCPU.timestamp << 32;
MDFNSS_StateAction(sm, load, data_only, StateRegs, "SOUND");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "SOUND", false);
next_scsp_time += SoundCPU.timestamp;
run_until_time += (int64)SoundCPU.timestamp << 32;

View File

@ -944,7 +944,7 @@ void StateAction(StateMem* sm, const unsigned load, const bool data_only)
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "VDP1");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "VDP1", false);
if(load)
{

View File

@ -1062,7 +1062,7 @@ void StateAction(StateMem* sm, const unsigned load, const bool data_only)
SFEND
};
MDFNSS_StateAction(sm, load, data_only, StateRegs, "VDP2");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "VDP2", false);
if(load)
{

View File

@ -3488,7 +3488,7 @@ void VDP2REND_StateAction(StateMem* sm, const unsigned load, const bool data_onl
}
}
MDFNSS_StateAction(sm, load, data_only, StateRegs, "VDP2REND");
MDFNSS_StateAction(sm, load, data_only, StateRegs, "VDP2REND", false);
if(load)
{

View File

@ -342,87 +342,82 @@ static int WriteStateChunk(StateMem *st, const char *sname, SFORMAT *sf)
}
/* This function is called by the game driver(NES, GB, GBA) to save a state. */
int MDFNSS_StateAction( void *st_p, int load, int data_only, std::vector <SSDescriptor> &sections )
static int MDFNSS_StateAction_internal( void *st_p, int load, int data_only, SSDescriptor *section)
{
StateMem *st = (StateMem*)st_p;
std::vector<SSDescriptor>::iterator section;
if ( load )
{
char sname[32];
{
char sname[32];
// For each section ...
for( section = sections.begin(); section != sections.end(); section++ )
{
int found = 0;
uint32_t tmp_size;
uint32_t total = 0;
int found = 0;
uint32_t tmp_size;
uint32_t total = 0;
while ( smem_read(st, (uint8_t *)sname, 32 ) == 32 )
{
int where_are_we = st->loc - 32;
while ( smem_read(st, (uint8_t *)sname, 32 ) == 32 )
{
int where_are_we = st->loc - 32;
if(smem_read32le(st, &tmp_size) != 4)
return(0);
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 ) )
{
if(!ReadStateChunk(st, section->sf, tmp_size))
{
log_cb( RETRO_LOG_ERROR, "Error reading chunk: %s\n", section->name );
return(0);
}
// Yay, we found the section
if ( !strncmp(sname, section->name, 32 ) )
{
if(!ReadStateChunk(st, section->sf, tmp_size))
{
log_cb( RETRO_LOG_ERROR, "Error reading chunk: %s\n", section->name );
return(0);
}
found = 1;
break;
}
else
{
if ( smem_seek(st, tmp_size, SEEK_CUR ) < 0 )
{
log_cb( RETRO_LOG_ERROR, "Chunk seek failure.\n" );
return(0);
}
}
}
found = 1;
break;
}
else
{
if ( smem_seek(st, tmp_size, SEEK_CUR ) < 0 )
{
log_cb( RETRO_LOG_ERROR, "Chunk seek failure.\n" );
return(0);
}
}
}
if ( smem_seek(st, -total, SEEK_CUR) < 0 )
{
log_cb( RETRO_LOG_ERROR, "Reverse seek error.\n" );
return(0);
}
if ( smem_seek(st, -total, SEEK_CUR) < 0 )
{
log_cb( RETRO_LOG_ERROR, "Reverse seek error.\n" );
return(0);
}
if( !found && !section->optional ) // Not found. We are sad!
{
log_cb( RETRO_LOG_ERROR, "Section missing: %.32s\n", section->name);
return(0);
}
if( !found && !section->optional ) // Not found. We are sad!
{
log_cb( RETRO_LOG_ERROR, "Section missing: %.32s\n", section->name);
return(0);
}
}; // for each section.
}
}
else
{
// Write all the chunks.
for ( section = sections.begin(); section != sections.end(); section++ )
{
if ( !WriteStateChunk(st, section->name, section->sf ) )
return(0);
}
}
{
// Write all the chunks.
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)
{
SSDescriptor love;
StateMem *st = (StateMem*)st_p;
std::vector <SSDescriptor> love;
love.push_back(SSDescriptor(sf, name, optional));
return(MDFNSS_StateAction(st, load, 0, love));
love.sf = sf;
love.name = name;
love.optional = optional;
return(MDFNSS_StateAction_internal(st, load, 0, &love));
}
extern int LibRetro_StateAction( StateMem* sm, const unsigned load, const bool data_only );

View File

@ -155,26 +155,13 @@ static INLINE SFORMAT SFBASE_(T* const v, const uint32 count, const char* const
#include <vector>
// 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, std::vector <SSDescriptor> &sections);
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