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,97 +5187,23 @@ void Game::customloadquick(std::string savfile)
} }
void Game::loadsummary(void) struct Summary
{ {
tinyxml2::XMLDocument docTele; const char* summary;
if (!FILESYSTEM_loadTiXml2Document("saves/tsave.vvv", docTele)) int seconds;
{ int minutes;
telesummary = ""; int hours;
} int savex;
else int savey;
{ int trinkets;
tinyxml2::XMLHandle hDoc(&docTele); bool finalmode;
tinyxml2::XMLElement* pElem; bool crewstats[Game::numcrew];
tinyxml2::XMLHandle hRoot(NULL); };
static void loadthissummary(
{ struct Summary* summary,
pElem=hDoc.FirstChildElement().ToElement(); tinyxml2::XMLDocument& doc
// 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);
}
int l_minute, l_second, l_hours;
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))
{
quicksummary = "";
}
else
{
tinyxml2::XMLHandle hDoc(&doc); tinyxml2::XMLHandle hDoc(&doc);
tinyxml2::XMLElement* pElem; tinyxml2::XMLElement* pElem;
tinyxml2::XMLHandle hRoot(NULL); tinyxml2::XMLHandle hRoot(NULL);
@ -5294,10 +5220,6 @@ void Game::loadsummary(void)
// save this for later // save this for later
hRoot=tinyxml2::XMLHandle(pElem); hRoot=tinyxml2::XMLHandle(pElem);
} }
int l_minute, l_second, l_hours;
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()) for( pElem = hRoot.FirstChildElement( "Data" ).FirstChild().ToElement(); pElem; pElem=pElem->NextSiblingElement())
{ {
const char* pKey = pElem->Value(); const char* pKey = pElem->Value();
@ -5310,52 +5232,95 @@ void Game::loadsummary(void)
if (SDL_strcmp(pKey, "summary") == 0) if (SDL_strcmp(pKey, "summary") == 0)
{ {
quicksummary = pText; summary->summary = pText;
} }
else if (SDL_strcmp(pKey, "seconds") == 0) else if (SDL_strcmp(pKey, "seconds") == 0)
{ {
l_second = help.Int(pText); summary->seconds = help.Int(pText);
} }
else if (SDL_strcmp(pKey, "minutes") == 0) else if (SDL_strcmp(pKey, "minutes") == 0)
{ {
l_minute = help.Int(pText); summary->minutes = help.Int(pText);
} }
else if (SDL_strcmp(pKey, "hours") == 0) else if (SDL_strcmp(pKey, "hours") == 0)
{ {
l_hours = help.Int(pText); summary->hours = help.Int(pText);
}
else if (SDL_strcmp(pKey, "savery") == 0)
{
l_saveY = help.Int(pText);
} }
else if (SDL_strcmp(pKey, "saverx") == 0) else if (SDL_strcmp(pKey, "saverx") == 0)
{ {
l_saveX = help.Int(pText); summary->savex = help.Int(pText);
}
else if (SDL_strcmp(pKey, "savery") == 0)
{
summary->savey = help.Int(pText);
} }
else if (SDL_strcmp(pKey, "trinkets") == 0) else if (SDL_strcmp(pKey, "trinkets") == 0)
{ {
quick_trinkets = help.Int(pText); summary->trinkets = help.Int(pText);
} }
else if (SDL_strcmp(pKey, "finalmode") == 0) else if (SDL_strcmp(pKey, "finalmode") == 0)
{ {
map.finalmode = help.Int(pText); map.finalmode = help.Int(pText);
} }
else if (SDL_strcmp(pKey, "finalstretch") == 0)
LOAD_ARRAY_RENAME(crewstats, summary->crewstats)
}
}
void Game::loadsummary(void)
{ {
map.finalstretch = help.Int(pText); tinyxml2::XMLDocument doc;
if (!FILESYSTEM_loadTiXml2Document("saves/tsave.vvv", doc))
{
telesummary = "";
}
else
{
struct Summary summary;
SDL_zero(summary);
loadthissummary(&summary, doc);
telesummary = summary.summary;
tele_gametime = giventimestring(
summary.hours,
summary.minutes,
summary.seconds
);
map.finalmode = summary.finalmode;
tele_currentarea = map.currentarea(
map.area(summary.savex, summary.savey)
);
SDL_memcpy(tele_crewstats, summary.crewstats, sizeof(tele_crewstats));
tele_trinkets = summary.trinkets;
} }
LOAD_ARRAY_RENAME(crewstats, quick_crewstats) if (!FILESYSTEM_loadTiXml2Document("saves/qsave.vvv", doc))
{
quicksummary = "";
} }
else
{
struct Summary summary;
SDL_zero(summary);
quick_gametime = giventimestring(l_hours,l_minute, l_second); loadthissummary(&summary, doc);
quick_currentarea = map.currentarea(map.area(l_saveX, l_saveY));
quicksummary = summary.summary;
quick_gametime = giventimestring(
summary.hours,
summary.minutes,
summary.seconds
);
map.finalmode = summary.finalmode;
quick_currentarea = map.currentarea(
map.area(summary.savex, summary.savey)
);
SDL_memcpy(quick_crewstats, summary.crewstats, sizeof(quick_crewstats));
quick_trinkets = summary.trinkets;
} }
} }
void Game::initteleportermode(void) void Game::initteleportermode(void)