- added more error checking in scenarios

This commit is contained in:
Mark Vejvoda 2012-07-18 00:04:24 +00:00
parent a55397c5f6
commit 4d97c80561
2 changed files with 54 additions and 1 deletions

View File

@ -119,7 +119,7 @@ MenuStateScenario::MenuStateScenario(Program *program, MainMenu *mainMenu,
listBoxScenario.setItems(results);
try {
if(listBoxScenario.getSelectedItemIndex() > 0) {
if(listBoxScenario.getSelectedItemIndex() > 0 && listBoxScenario.getSelectedItemIndex() < scenarioFiles.size()) {
loadScenarioInfo(Scenario::getScenarioPath(dirList, scenarioFiles[listBoxScenario.getSelectedItemIndex()]), &scenarioInfo );
labelInfo.setText(scenarioInfo.desc);
}

View File

@ -162,7 +162,18 @@ void Scenario::loadScenarioInfo(string file, ScenarioInfo *scenarioInfo) {
xmlTree.load(file,Properties::getTagReplacementValues());
const XmlNode *scenarioNode= xmlTree.getRootNode();
if(scenarioNode == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"scenarioNode == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
const XmlNode *difficultyNode= scenarioNode->getChild("difficulty");
if(difficultyNode == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"difficultyNode == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
scenarioInfo->difficulty = difficultyNode->getAttribute("value")->getIntValue();
if( scenarioInfo->difficulty < dVeryEasy || scenarioInfo->difficulty > dInsane ) {
char szBuf[4096]="";
@ -171,6 +182,11 @@ void Scenario::loadScenarioInfo(string file, ScenarioInfo *scenarioInfo) {
}
const XmlNode *playersNode= scenarioNode->getChild("players");
if(playersNode == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"playersNode == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
for(int i= 0; i < GameConstants::maxPlayers; ++i) {
XmlNode* playerNode=NULL;
@ -179,6 +195,12 @@ void Scenario::loadScenarioInfo(string file, ScenarioInfo *scenarioInfo) {
if(playersNode->hasChildAtIndex("player",i)) {
playerNode = playersNode->getChild("player", i);
if(playerNode == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"playerNode == NULL for index = %d for file [%s]\n",i,file.c_str());
throw std::runtime_error(szBuf);
}
factionControl = strToControllerType( playerNode->getAttribute("control")->getValue() );
if(playerNode->getAttribute("resource_multiplier",false) != NULL) {
@ -223,6 +245,37 @@ void Scenario::loadScenarioInfo(string file, ScenarioInfo *scenarioInfo) {
scenarioInfo->factionTypeNames[i]= "";
}
if(scenarioNode->getChild("map") == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"map == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
if(scenarioNode->getChild("tileset") == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"tileset == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
if(scenarioNode->getChild("tech-tree") == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"tech-tree == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
if(scenarioNode->getChild("default-units") == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"default-units == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
if(scenarioNode->getChild("default-resources") == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"default-resources == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
if(scenarioNode->getChild("default-victory-conditions") == NULL) {
char szBuf[4096]="";
sprintf(szBuf,"default-victory-conditions == NULL for file [%s]\n",file.c_str());
throw std::runtime_error(szBuf);
}
scenarioInfo->mapName = scenarioNode->getChild("map")->getAttribute("value")->getValue();
scenarioInfo->tilesetName = scenarioNode->getChild("tileset")->getAttribute("value")->getValue();
scenarioInfo->techTreeName = scenarioNode->getChild("tech-tree")->getAttribute("value")->getValue();