From 831bbda6c9769813678d4e0c8a6b45623667f596 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 28 May 2013 15:42:54 +0000 Subject: [PATCH] better error handling for game / scenario loading problems (like bad xml) --- source/glest_game/main/main.cpp | 29 ++++++++++---- .../glest_game/menu/menu_state_scenario.cpp | 40 ++++++++++++------- source/glest_game/world/world.cpp | 10 ++--- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 54928827b..56fe13fda 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -628,7 +628,8 @@ void stackdumper(unsigned int type, EXCEPTION_POINTERS *ep, bool fatalExit) { static bool inErrorNow = false; if(inErrorNow == true) { - printf("\n** Already in error handler, msg [%s]\n",msg); + printf("\n=====================================\n"); + printf("\n** Already in error handler aborting, msg [%s]\n",msg); fflush(stdout); abort(); return; @@ -678,9 +679,16 @@ void stackdumper(unsigned int type, EXCEPTION_POINTERS *ep, bool fatalExit) { for(;GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false && mainProgram->isMessageShowing();) { //program->getState()->render(); Shared::Platform::Window::handleEvent(); - mainProgram->loop(); - - //printf("\nhandle error #1\n"); + try { + mainProgram->loop(); + } + catch(const exception &e) { + printf("\n=====================================\n"); + printf("\n** Already in error handler exiting errror rendering, msg [%s]\n",e.what()); + fflush(stdout); + //abort(); + break; + } } } } @@ -693,9 +701,16 @@ void stackdumper(unsigned int type, EXCEPTION_POINTERS *ep, bool fatalExit) { for(;GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false && mainProgram->isMessageShowing();) { //program->renderProgramMsgBox(); Shared::Platform::Window::handleEvent(); - mainProgram->loop(); - - //printf("\nhandle error #2\n"); + try { + mainProgram->loop(); + } + catch(const exception &e) { + printf("\n=====================================\n"); + printf("\n** Already in error handler exiting errror rendering, msg [%s]\n",e.what()); + fflush(stdout); + //abort(); + break; + } } } } diff --git a/source/glest_game/menu/menu_state_scenario.cpp b/source/glest_game/menu/menu_state_scenario.cpp index 94a1b79d3..1d5d33fb2 100644 --- a/source/glest_game/menu/menu_state_scenario.cpp +++ b/source/glest_game/menu/menu_state_scenario.cpp @@ -50,7 +50,7 @@ MenuStateScenario::MenuStateScenario(Program *program, MainMenu *mainMenu, } catch(const std::exception &ex) { char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); + snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); @@ -139,7 +139,7 @@ MenuStateScenario::MenuStateScenario(Program *program, MainMenu *mainMenu, } catch(const std::exception &ex) { char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); + snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); @@ -160,7 +160,7 @@ MenuStateScenario::MenuStateScenario(Program *program, MainMenu *mainMenu, } catch(const std::exception &ex) { char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); + snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); @@ -201,7 +201,7 @@ MenuStateScenario::~MenuStateScenario() { } void MenuStateScenario::cleanupPreviewTexture() { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] scenarioLogoTexture [%p]\n",__FILE__,__FUNCTION__,__LINE__,scenarioLogoTexture); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] scenarioLogoTexture [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,scenarioLogoTexture); if(scenarioLogoTexture != NULL) { Renderer::getInstance().endTexture(rsGlobal, scenarioLogoTexture, false); @@ -248,7 +248,7 @@ void MenuStateScenario::mouseClick(int x, int y, MouseButton mouseButton) { } catch(const std::exception &ex) { char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); + snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); @@ -308,15 +308,27 @@ void MenuStateScenario::update() { this->autoloadScenarioName = ""; } else { - if(listBoxScenario.getItemCount() > 0 && listBoxScenario.getSelectedItemIndex() >= 0 && listBoxScenario.getSelectedItemIndex() < scenarioFiles.size()) { - loadScenarioInfo(Scenario::getScenarioPath(dirList, scenarioFiles[listBoxScenario.getSelectedItemIndex()]), &scenarioInfo); - labelInfo.setText(scenarioInfo.desc); + try { + this->autoloadScenarioName = ""; + if(listBoxScenario.getItemCount() > 0 && listBoxScenario.getSelectedItemIndex() >= 0 && listBoxScenario.getSelectedItemIndex() < scenarioFiles.size()) { + loadScenarioInfo(Scenario::getScenarioPath(dirList, scenarioFiles[listBoxScenario.getSelectedItemIndex()]), &scenarioInfo); + labelInfo.setText(scenarioInfo.desc); - SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - CoreData &coreData= CoreData::getInstance(); - soundRenderer.playFx(coreData.getClickSoundC()); - launchGame(); - return; + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + CoreData &coreData= CoreData::getInstance(); + soundRenderer.playFx(coreData.getClickSoundC()); + launchGame(); + return; + } + } + catch(const std::exception &ex) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"In [%s::%s %d] Error detected:\n%s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); + + mainMessageBoxState=1; + showMessageBox( "Error: " + string(ex.what()), "Error detected", false); } } } @@ -367,7 +379,7 @@ void MenuStateScenario::loadScenarioPreviewTexture(){ Game::extractScenarioLogoFile(&gameSettings, scenarioLogo, loadingImageUsed); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] scenarioLogo [%s]\n",__FILE__,__FUNCTION__,__LINE__,scenarioLogo.c_str()); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] scenarioLogo [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,scenarioLogo.c_str()); if(scenarioLogo != "") { cleanupPreviewTexture(); diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index dd8580ba3..ec27a22ca 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -456,7 +456,7 @@ Checksum World::loadScenario(const string &path, Checksum *checksum, bool resetC if(resetCurrentScenario == true) { scenario = Scenario(); - scriptManager->init(this, this->getGame()->getGameCameraPtr(),rootNode); + if(scriptManager) scriptManager->init(this, this->getGame()->getGameCameraPtr(),rootNode); } scenarioChecksum = scenario.load(path); @@ -492,7 +492,7 @@ void World::updateAllFactionUnits() { char perfBuf[8096]=""; std::vector perfList; - scriptManager->onTimerTriggerEvent(); + if(scriptManager) scriptManager->onTimerTriggerEvent(); // Prioritize grouped command units so closest units to target go first // units @@ -754,7 +754,7 @@ void World::update() { //time timeFlow.update(); - scriptManager->onDayNightTriggerEvent(); + if(scriptManager) scriptManager->onDayNightTriggerEvent(); if(showPerfStats) { sprintf(perfBuf,"In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chronoPerf.getMillis()); @@ -1076,7 +1076,7 @@ void World::moveUnitCells(Unit *unit) { } } - scriptManager->onCellTriggerEvent(unit); + if(scriptManager) scriptManager->onCellTriggerEvent(unit); } void World::addAttackEffects(const Unit *unit) { @@ -1223,7 +1223,7 @@ void World::createUnit(const string &unitName, int factionIndex, const Vec2i &po if(placeUnit(pos, generationArea, unit, spaciated)) { unit->create(true); unit->born(NULL); - scriptManager->onUnitCreated(unit); + if(scriptManager) scriptManager->onUnitCreated(unit); } else { delete unit;