From 2d8c5a77472159229d920041878e09c003c84ecd Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 4 May 2013 21:39:43 +0100 Subject: [PATCH] TPT Blue screen of death - try its best to catch some signals (SIGSEGV) and unhandled exception in the game loop --- src/PowderToySDL.cpp | 250 ++++++++++++++++++++++++++++--------------- 1 file changed, 166 insertions(+), 84 deletions(-) diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 9c175f4c9..f6ef881dc 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -624,6 +624,66 @@ bool SaveWindowPosition() #endif +void BlueScreen(char * detailMessage){ + ui::Engine * engine = &ui::Engine::Ref(); + engine->g->fillrect(0, 0, engine->GetWidth(), engine->GetHeight(), 17, 114, 169, 210); + + std::string errorTitle = "ERROR"; + std::string errorDetails = "Details: " + std::string(detailMessage); + std::string errorHelp = "An unrecoverable fault has occured, please report the error by visiting the website below\n" + "http://" SERVER; + int currentY = 0, width, height; + int errorWidth = 0; + Graphics::textsize(errorHelp.c_str(), errorWidth, height); + + engine->g->drawtext((engine->GetWidth()/2)-(errorWidth/2), ((engine->GetHeight()/2)-100) + currentY, errorTitle.c_str(), 255, 255, 255, 255); + Graphics::textsize(errorTitle.c_str(), width, height); + currentY += height + 4; + + engine->g->drawtext((engine->GetWidth()/2)-(errorWidth/2), ((engine->GetHeight()/2)-100) + currentY, errorDetails.c_str(), 255, 255, 255, 255); + Graphics::textsize(errorTitle.c_str(), width, height); + currentY += height + 4; + + engine->g->drawtext((engine->GetWidth()/2)-(errorWidth/2), ((engine->GetHeight()/2)-100) + currentY, errorHelp.c_str(), 255, 255, 255, 255); + Graphics::textsize(errorTitle.c_str(), width, height); + currentY += height + 4; + + //Death loop + SDL_Event event; + while(true) + { + while (SDL_PollEvent(&event)) + if(event.type == SDL_QUIT) + exit(-1); +#ifdef OGLI + blit(); +#else + if(engine->Scale==2) + blit2(engine->g->vid, engine->Scale); + else + blit(engine->g->vid); +#endif + } +} + +void SigHandler(int signal) +{ + switch(signal){ + case SIGSEGV: + BlueScreen("Memory read/write error"); + break; + case SIGFPE: + BlueScreen("Floating point exception"); + break; + case SIGILL: + BlueScreen("Program execution exception"); + break; + case SIGABRT: + BlueScreen("Unexpected program abort"); + break; + } +} + int main(int argc, char * argv[]) { currentWidth = XRES+BARSIZE; @@ -719,108 +779,130 @@ int main(int argc, char * argv[]) engine->Begin(XRES+BARSIZE, YRES+MENUSIZE); engine->SetFastQuit(Client::Ref().GetPrefBool("FastQuit", true)); - GameController * gameController = new GameController(); - engine->ShowWindow(gameController->GetView()); +#ifndef DEBUG + //Get ready to catch any dodgy errors + signal(SIGSEGV, SigHandler); + signal(SIGFPE, SigHandler); + signal(SIGILL, SigHandler); + signal(SIGABRT, SigHandler); - if(arguments["open"].length()) - { -#ifdef DEBUG - std::cout << "Loading " << arguments["open"] << std::endl; #endif - if(Client::Ref().FileExists(arguments["open"])) + +#ifndef DEBUG + GameController * gameController = NULL; + try { +#endif + + gameController = new GameController(); + engine->ShowWindow(gameController->GetView()); + + if(arguments["open"].length()) { + #ifdef DEBUG + std::cout << "Loading " << arguments["open"] << std::endl; + #endif + if(Client::Ref().FileExists(arguments["open"])) + { + try + { + std::vector gameSaveData = Client::Ref().ReadFile(arguments["open"]); + if(!gameSaveData.size()) + { + new ErrorMessage("Error", "Could not read file"); + } + else + { + SaveFile * newFile = new SaveFile(arguments["open"]); + GameSave * newSave = new GameSave(gameSaveData); + newFile->SetGameSave(newSave); + gameController->LoadSaveFile(newFile); + delete newFile; + } + + } + catch(std::exception & e) + { + new ErrorMessage("Error", "Could not open save file:\n"+std::string(e.what())) ; + } + } + else + { + new ErrorMessage("Error", "Could not open file"); + } + } + + if(arguments["ptsave"].length()) + { + engine->g->fillrect((engine->GetWidth()/2)-101, (engine->GetHeight()/2)-26, 202, 52, 0, 0, 0, 210); + engine->g->drawrect((engine->GetWidth()/2)-100, (engine->GetHeight()/2)-25, 200, 50, 255, 255, 255, 180); + engine->g->drawtext((engine->GetWidth()/2)-(Graphics::textwidth("Loading save...")/2), (engine->GetHeight()/2)-5, "Loading save...", style::Colour::InformationTitle.Red, style::Colour::InformationTitle.Green, style::Colour::InformationTitle.Blue, 255); + + #ifdef OGLI + blit(); + #else + if(engine->Scale==2) + blit2(engine->g->vid, engine->Scale); + else + blit(engine->g->vid); + #endif + std::string ptsaveArg = arguments["ptsave"]; try { - std::vector gameSaveData = Client::Ref().ReadFile(arguments["open"]); - if(!gameSaveData.size()) + if(!ptsaveArg.find("ptsave:")) + { + std::string saveIdPart = ""; + int saveId; + int hashPos = ptsaveArg.find('#'); + if(hashPos != std::string::npos) { - new ErrorMessage("Error", "Could not read file"); + saveIdPart = ptsaveArg.substr(7, hashPos-7); } else { - SaveFile * newFile = new SaveFile(arguments["open"]); - GameSave * newSave = new GameSave(gameSaveData); - newFile->SetGameSave(newSave); - gameController->LoadSaveFile(newFile); - delete newFile; + saveIdPart = ptsaveArg.substr(7); } + if(saveIdPart.length()) + { + #ifdef DEBUG + std::cout << "Got Ptsave: id: " << saveIdPart << std::endl; + #endif + saveId = format::StringToNumber(saveIdPart); + if(!saveId) + throw std::runtime_error("Invalid Save ID"); + SaveInfo * newSave = Client::Ref().GetSave(saveId, 0); + GameSave * newGameSave = new GameSave(Client::Ref().GetSaveData(saveId, 0)); + newSave->SetGameSave(newGameSave); + if(!newSave) + throw std::runtime_error("Could not load save"); + + gameController->LoadSave(newSave); + delete newSave; + } + else + { + throw std::runtime_error("No Save ID"); + } } - catch(std::exception & e) + } + catch (std::exception & e) { - new ErrorMessage("Error", "Could not open save file:\n"+std::string(e.what())) ; + new ErrorMessage("Error", "Invalid save link"); } } - else - { - new ErrorMessage("Error", "Could not open file"); - } - } - if(arguments["ptsave"].length()) + EngineProcess(); + + #ifdef WIN + SaveWindowPosition(); + #endif + +#ifndef DEBUG + } + catch(...) { - engine->g->fillrect((engine->GetWidth()/2)-101, (engine->GetHeight()/2)-26, 202, 52, 0, 0, 0, 210); - engine->g->drawrect((engine->GetWidth()/2)-100, (engine->GetHeight()/2)-25, 200, 50, 255, 255, 255, 180); - engine->g->drawtext((engine->GetWidth()/2)-(Graphics::textwidth("Loading save...")/2), (engine->GetHeight()/2)-5, "Loading save...", style::Colour::InformationTitle.Red, style::Colour::InformationTitle.Green, style::Colour::InformationTitle.Blue, 255); - -#ifdef OGLI - blit(); -#else - if(engine->Scale==2) - blit2(engine->g->vid, engine->Scale); - else - blit(engine->g->vid); -#endif - std::string ptsaveArg = arguments["ptsave"]; - try - { - if(!ptsaveArg.find("ptsave:")) - { - std::string saveIdPart = ""; - int saveId; - int hashPos = ptsaveArg.find('#'); - if(hashPos != std::string::npos) - { - saveIdPart = ptsaveArg.substr(7, hashPos-7); - } - else - { - saveIdPart = ptsaveArg.substr(7); - } - if(saveIdPart.length()) - { -#ifdef DEBUG - std::cout << "Got Ptsave: id: " << saveIdPart << std::endl; -#endif - saveId = format::StringToNumber(saveIdPart); - if(!saveId) - throw std::runtime_error("Invalid Save ID"); - - SaveInfo * newSave = Client::Ref().GetSave(saveId, 0); - GameSave * newGameSave = new GameSave(Client::Ref().GetSaveData(saveId, 0)); - newSave->SetGameSave(newGameSave); - if(!newSave) - throw std::runtime_error("Could not load save"); - - gameController->LoadSave(newSave); - delete newSave; - } - else - { - throw std::runtime_error("No Save ID"); - } - } - } - catch (std::exception & e) - { - new ErrorMessage("Error", "Invalid save link"); - } + BlueScreen("Unhandled exception"); } - - EngineProcess(); - -#ifdef WIN - SaveWindowPosition(); #endif ui::Engine::Ref().CloseWindow();