1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-26 14:38:30 +02:00

Unindent loadcustomlevelstats from previous commit

Done in a separate commit to reduce diff noise.
This commit is contained in:
Misa 2020-06-29 18:47:45 -07:00
parent be9c405ddd
commit b9bf2cc1c5

View File

@ -585,113 +585,113 @@ void Game::loadcustomlevelstats()
return; return;
} }
tinyxml2::XMLDocument doc; tinyxml2::XMLDocument doc;
if (!FILESYSTEM_loadTiXml2Document("saves/levelstats.vvv", doc)) if (!FILESYSTEM_loadTiXml2Document("saves/levelstats.vvv", doc))
{
//No levelstats file exists; start new
customlevelstats.clear();
savecustomlevelstats();
return;
}
// Old system
std::vector<std::string> customlevelnames;
std::vector<int> customlevelscores;
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)
{ {
//No levelstats file exists; start new printf("Error: Levelstats file corrupted\n");
customlevelstats.clear();
savecustomlevelstats();
return;
} }
// Old system // save this for later
std::vector<std::string> customlevelnames; hRoot=tinyxml2::XMLHandle(pElem);
std::vector<int> customlevelscores; }
tinyxml2::XMLHandle hDoc(&doc); // First pass, look for the new system of storing stats
tinyxml2::XMLElement* pElem; // If they don't exist, then fall back to the old system
tinyxml2::XMLHandle hRoot(NULL); for (pElem = hRoot.FirstChildElement("Data").FirstChild().ToElement(); pElem; pElem = pElem->NextSiblingElement())
{
std::string pKey(pElem->Value());
const char* pText = pElem->GetText();
if (pText == NULL)
{
pText = "";
}
if (pKey == "stats")
{
for (tinyxml2::XMLElement* stat_el = pElem->FirstChildElement(); stat_el; stat_el = stat_el->NextSiblingElement())
{ {
pElem=hDoc.FirstChildElement().ToElement(); CustomLevelStat stat = {};
// should always have a valid root but handle gracefully if it does
if (!pElem) if (stat_el->GetText() != NULL)
{ {
printf("Error: Levelstats file corrupted\n"); stat.score = atoi(stat_el->GetText());
} }
// save this for later if (stat_el->Attribute("name"))
hRoot=tinyxml2::XMLHandle(pElem);
}
// First pass, look for the new system of storing stats
// If they don't exist, then fall back to the old system
for (pElem = hRoot.FirstChildElement("Data").FirstChild().ToElement(); pElem; pElem = pElem->NextSiblingElement())
{
std::string pKey(pElem->Value());
const char* pText = pElem->GetText();
if (pText == NULL)
{ {
pText = ""; stat.name = stat_el->Attribute("name");
} }
if (pKey == "stats")
{
for (tinyxml2::XMLElement* stat_el = pElem->FirstChildElement(); stat_el; stat_el = stat_el->NextSiblingElement())
{
CustomLevelStat stat = {};
if (stat_el->GetText() != NULL)
{
stat.score = atoi(stat_el->GetText());
}
if (stat_el->Attribute("name"))
{
stat.name = stat_el->Attribute("name");
}
customlevelstats.push_back(stat);
}
return;
}
}
// Since we're still here, we must be on the old system
for( pElem = hRoot.FirstChildElement( "Data" ).FirstChild().ToElement(); pElem; pElem=pElem->NextSiblingElement())
{
std::string pKey(pElem->Value());
const char* pText = pElem->GetText() ;
if(pText == NULL)
{
pText = "";
}
if (pKey == "customlevelscore")
{
std::string TextString = (pText);
if(TextString.length())
{
std::vector<std::string> values = split(TextString,',');
for(size_t i = 0; i < values.size(); i++)
{
customlevelscores.push_back(atoi(values[i].c_str()));
}
}
}
if (pKey == "customlevelstats")
{
std::string TextString = (pText);
if(TextString.length())
{
std::vector<std::string> values = split(TextString,'|');
for(size_t i = 0; i < values.size(); i++)
{
customlevelnames.push_back(values[i]);
}
}
}
}
// If the two arrays happen to differ in length, just go with the smallest one
for (size_t i = 0; i < std::min(customlevelnames.size(), customlevelscores.size()); i++)
{
CustomLevelStat stat = {customlevelnames[i], customlevelscores[i]};
customlevelstats.push_back(stat); customlevelstats.push_back(stat);
} }
return;
}
}
// Since we're still here, we must be on the old system
for( pElem = hRoot.FirstChildElement( "Data" ).FirstChild().ToElement(); pElem; pElem=pElem->NextSiblingElement())
{
std::string pKey(pElem->Value());
const char* pText = pElem->GetText() ;
if(pText == NULL)
{
pText = "";
}
if (pKey == "customlevelscore")
{
std::string TextString = (pText);
if(TextString.length())
{
std::vector<std::string> values = split(TextString,',');
for(size_t i = 0; i < values.size(); i++)
{
customlevelscores.push_back(atoi(values[i].c_str()));
}
}
}
if (pKey == "customlevelstats")
{
std::string TextString = (pText);
if(TextString.length())
{
std::vector<std::string> values = split(TextString,'|');
for(size_t i = 0; i < values.size(); i++)
{
customlevelnames.push_back(values[i]);
}
}
}
}
// If the two arrays happen to differ in length, just go with the smallest one
for (size_t i = 0; i < std::min(customlevelnames.size(), customlevelscores.size()); i++)
{
CustomLevelStat stat = {customlevelnames[i], customlevelscores[i]};
customlevelstats.push_back(stat);
}
} }
void Game::savecustomlevelstats() void Game::savecustomlevelstats()