1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 01:59:43 +01:00

De-duplicate loadsummary parsing and move to different function

I will need to be able to return from this function if there's an XML
error, otherwise writing out the control flow manually gets really
nasty. And while I'm at it, it's some a nice de-duplication as well.

To do this, we create a temporary struct that bundles up all the
information we want for the summary, and pass it in to the intermediate
load function.

Furthermore, we can get rid of reading map.finalstretch - it affects
nothing. map.finalmode is still needed, however, because of the usage of
map.area().
This commit is contained in:
Misa 2021-03-23 16:43:02 -07:00 committed by Misa Elizabeth Kai
parent bbc2f06d81
commit 73535b4342

View file

@ -5187,175 +5187,140 @@ void Game::customloadquick(std::string savfile)
} }
struct Summary
{
const char* summary;
int seconds;
int minutes;
int hours;
int savex;
int savey;
int trinkets;
bool finalmode;
bool crewstats[Game::numcrew];
};
static void loadthissummary(
struct Summary* summary,
tinyxml2::XMLDocument& doc
) {
tinyxml2::XMLHandle hDoc(&doc);
tinyxml2::XMLElement* pElem;
tinyxml2::XMLHandle hRoot(NULL);
{
pElem=hDoc.FirstChildElement().ToElement();
// should always have a valid root but handle gracefully if it does
if (!pElem)
{
vlog_error("Save Not Found");
}
// save this for later
hRoot=tinyxml2::XMLHandle(pElem);
}
for( pElem = hRoot.FirstChildElement( "Data" ).FirstChild().ToElement(); pElem; pElem=pElem->NextSiblingElement())
{
const char* pKey = pElem->Value();
const char* pText = pElem->GetText() ;
if (pText == NULL)
{
pText = "";
}
if (SDL_strcmp(pKey, "summary") == 0)
{
summary->summary = pText;
}
else if (SDL_strcmp(pKey, "seconds") == 0)
{
summary->seconds = help.Int(pText);
}
else if (SDL_strcmp(pKey, "minutes") == 0)
{
summary->minutes = help.Int(pText);
}
else if (SDL_strcmp(pKey, "hours") == 0)
{
summary->hours = help.Int(pText);
}
else if (SDL_strcmp(pKey, "saverx") == 0)
{
summary->savex = help.Int(pText);
}
else if (SDL_strcmp(pKey, "savery") == 0)
{
summary->savey = help.Int(pText);
}
else if (SDL_strcmp(pKey, "trinkets") == 0)
{
summary->trinkets = help.Int(pText);
}
else if (SDL_strcmp(pKey, "finalmode") == 0)
{
map.finalmode = help.Int(pText);
}
LOAD_ARRAY_RENAME(crewstats, summary->crewstats)
}
}
void Game::loadsummary(void) void Game::loadsummary(void)
{ {
tinyxml2::XMLDocument docTele; tinyxml2::XMLDocument doc;
if (!FILESYSTEM_loadTiXml2Document("saves/tsave.vvv", docTele))
if (!FILESYSTEM_loadTiXml2Document("saves/tsave.vvv", doc))
{ {
telesummary = ""; telesummary = "";
} }
else else
{ {
tinyxml2::XMLHandle hDoc(&docTele); struct Summary summary;
tinyxml2::XMLElement* pElem; SDL_zero(summary);
tinyxml2::XMLHandle hRoot(NULL);
loadthissummary(&summary, doc);
{ telesummary = summary.summary;
pElem=hDoc.FirstChildElement().ToElement(); tele_gametime = giventimestring(
// should always have a valid root but handle gracefully if it does summary.hours,
if (!pElem) summary.minutes,
{ summary.seconds
vlog_error("Save Not Found"); );
} map.finalmode = summary.finalmode;
tele_currentarea = map.currentarea(
// save this for later map.area(summary.savex, summary.savey)
hRoot=tinyxml2::XMLHandle(pElem); );
} SDL_memcpy(tele_crewstats, summary.crewstats, sizeof(tele_crewstats));
int l_minute, l_second, l_hours; tele_trinkets = summary.trinkets;
l_minute = l_second= l_hours = 0;
int l_saveX = 0;
int l_saveY = 0;
for( pElem = hRoot.FirstChildElement( "Data" ).FirstChild().ToElement(); pElem; pElem=pElem->NextSiblingElement())
{
const char* pKey = pElem->Value();
const char* pText = pElem->GetText() ;
if (pText == NULL)
{
pText = "";
}
if (SDL_strcmp(pKey, "summary") == 0)
{
telesummary = pText;
}
else if (SDL_strcmp(pKey, "seconds") == 0)
{
l_second = help.Int(pText);
}
else if (SDL_strcmp(pKey, "minutes") == 0)
{
l_minute = help.Int(pText);
}
else if (SDL_strcmp(pKey, "hours") == 0)
{
l_hours = help.Int(pText);
}
else if (SDL_strcmp(pKey, "savery") == 0)
{
l_saveY = help.Int(pText);
}
else if (SDL_strcmp(pKey, "saverx") == 0)
{
l_saveX = help.Int(pText);
}
else if (SDL_strcmp(pKey, "trinkets") == 0)
{
tele_trinkets = help.Int(pText);
}
else if (SDL_strcmp(pKey, "finalmode") == 0)
{
map.finalmode = help.Int(pText);
}
else if (SDL_strcmp(pKey, "finalstretch") == 0)
{
map.finalstretch = help.Int(pText);
}
LOAD_ARRAY_RENAME(crewstats, tele_crewstats)
}
tele_gametime = giventimestring(l_hours,l_minute, l_second);
tele_currentarea = map.currentarea(map.area(l_saveX, l_saveY));
} }
tinyxml2::XMLDocument doc;
if (!FILESYSTEM_loadTiXml2Document("saves/qsave.vvv", doc)) if (!FILESYSTEM_loadTiXml2Document("saves/qsave.vvv", doc))
{ {
quicksummary = ""; quicksummary = "";
} }
else else
{ {
tinyxml2::XMLHandle hDoc(&doc); struct Summary summary;
tinyxml2::XMLElement* pElem; SDL_zero(summary);
tinyxml2::XMLHandle hRoot(NULL);
loadthissummary(&summary, doc);
{ quicksummary = summary.summary;
pElem=hDoc.FirstChildElement().ToElement(); quick_gametime = giventimestring(
// should always have a valid root but handle gracefully if it does summary.hours,
if (!pElem) summary.minutes,
{ summary.seconds
vlog_error("Save Not Found"); );
} map.finalmode = summary.finalmode;
quick_currentarea = map.currentarea(
// save this for later map.area(summary.savex, summary.savey)
hRoot=tinyxml2::XMLHandle(pElem); );
} SDL_memcpy(quick_crewstats, summary.crewstats, sizeof(quick_crewstats));
int l_minute, l_second, l_hours; quick_trinkets = summary.trinkets;
l_minute = l_second= l_hours = 0;
int l_saveX = 0;
int l_saveY = 0;
for( pElem = hRoot.FirstChildElement( "Data" ).FirstChild().ToElement(); pElem; pElem=pElem->NextSiblingElement())
{
const char* pKey = pElem->Value();
const char* pText = pElem->GetText() ;
if (pText == NULL)
{
pText = "";
}
if (SDL_strcmp(pKey, "summary") == 0)
{
quicksummary = pText;
}
else if (SDL_strcmp(pKey, "seconds") == 0)
{
l_second = help.Int(pText);
}
else if (SDL_strcmp(pKey, "minutes") == 0)
{
l_minute = help.Int(pText);
}
else if (SDL_strcmp(pKey, "hours") == 0)
{
l_hours = help.Int(pText);
}
else if (SDL_strcmp(pKey, "savery") == 0)
{
l_saveY = help.Int(pText);
}
else if (SDL_strcmp(pKey, "saverx") == 0)
{
l_saveX = help.Int(pText);
}
else if (SDL_strcmp(pKey, "trinkets") == 0)
{
quick_trinkets = help.Int(pText);
}
else if (SDL_strcmp(pKey, "finalmode") == 0)
{
map.finalmode = help.Int(pText);
}
else if (SDL_strcmp(pKey, "finalstretch") == 0)
{
map.finalstretch = help.Int(pText);
}
LOAD_ARRAY_RENAME(crewstats, quick_crewstats)
}
quick_gametime = giventimestring(l_hours,l_minute, l_second);
quick_currentarea = map.currentarea(map.area(l_saveX, l_saveY));
} }
} }
void Game::initteleportermode(void) void Game::initteleportermode(void)