Ticket #4771: kyra-recover_from_invalid_savegame_slot.patch

File kyra-recover_from_invalid_savegame_slot.patch, 5.5 KB (added by Templier, 14 years ago)

2 - Check for proper savegame loading and fallback to normal path if it fails

  • engines/kyra/kyra_hof.cpp

     
    438438        runStartScript(1, 0);
    439439        loadNPCScript();
    440440
    441         if (_gameToLoad == -1) {
     441        // Try loading a saved game and fallback to normal game init if it fails
     442        if (_gameToLoad == -1 || (_gameToLoad != -1 && !loadGameStateCheck(_gameToLoad))) {
    442443                snd_playWanderScoreViaMap(52, 1);
    443444                enterNewScene(_mainCharacter.sceneId, _mainCharacter.facing, 0, 0, 1);
    444445                saveGameState(0, "New Game", 0);
    445         } else {
    446                 loadGameStateCheck(_gameToLoad);
    447         }
     446        }
    448447
    449448        _screen->showMouse();
    450449
  • engines/kyra/kyra_lok.cpp

     
    409409                error("Could not load \"_NPC.EMC\" script");
    410410
    411411        snd_playTheme(1, -1);
    412         if (_gameToLoad == -1) {
    413                 enterNewScene(_currentCharacter->sceneId, _currentCharacter->facing, 0, 0, 1);
    414                 if (_abortIntroFlag && _skipIntroFlag && saveFileLoadable(0)) {
    415                         _menuDirectlyToLoad = true;
    416                         _screen->setMouseCursor(1, 1, _shapes[0]);
    417                         _screen->showMouse();
    418                         _gui->buttonMenuCallback(0);
    419                         _menuDirectlyToLoad = false;
    420                 } else if (!shouldQuit()) {
    421                         saveGameState(0, "New game", 0);
     412
     413        // Try loading a saved game and fall back to normal path if it fails
     414        if (_gameToLoad != -1) {
     415                Screen::FontId previousFont = _screen->setFont(_flags.lang == Common::JA_JPN ? Screen::FID_SJIS_FNT : Screen::FID_8_FNT);
     416
     417                if (loadGameStateCheck(_gameToLoad)) {
     418                        _gameToLoad = -1;
     419                        return;
    422420                }
    423         } else {
    424                 _screen->setFont(_flags.lang == Common::JA_JPN ? Screen::FID_SJIS_FNT : Screen::FID_8_FNT);
    425                 loadGameStateCheck(_gameToLoad);
     421
     422                // Restore screen font and continue
     423                _screen->setFont(previousFont);
    426424                _gameToLoad = -1;
    427425        }
     426
     427        enterNewScene(_currentCharacter->sceneId, _currentCharacter->facing, 0, 0, 1);
     428        if (_abortIntroFlag && _skipIntroFlag && saveFileLoadable(0)) {
     429                _menuDirectlyToLoad = true;
     430                _screen->setMouseCursor(1, 1, _shapes[0]);
     431                _screen->showMouse();
     432                _gui->buttonMenuCallback(0);
     433                _menuDirectlyToLoad = false;
     434        } else if (!shouldQuit()) {
     435                saveGameState(0, "New game", 0);
     436        }
    428437}
    429438
    430439void KyraEngine_LoK::mainLoop() {
  • engines/kyra/kyra_mr.cpp

     
    658658        saveGameState(0, "New Game", 0);
    659659        _soundDigital->beginFadeOut(_musicSoundChannel, 60);
    660660        delayWithTicks(60);
    661         if (_gameToLoad == -1)
     661
     662        // Try loading a saved game and fall back to normal scene if it fails
     663        if (_gameToLoad == -1 || (_gameToLoad != -1 && loadGameStateCheck(_gameToLoad)))
    662664                enterNewScene(_mainCharacter.sceneId, _mainCharacter.facing, 0, 0, 1);
    663         else
    664                 loadGameStateCheck(_gameToLoad);
    665665
    666666        if (_menuDirectlyToLoad)
    667667                (*_mainButtonData[0].buttonCallback)(&_mainButtonData[0]);
  • engines/kyra/kyra_v1.cpp

     
    266266                                int saveLoadSlot = 9 - (event.kbd.keycode - Common::KEYCODE_0) + 990;
    267267
    268268                                if (event.kbd.flags == Common::KBD_CTRL) {
    269                                         loadGameStateCheck(saveLoadSlot);
    270                                         _eventList.clear();
    271                                         breakLoop = true;
     269                                        if (loadGameStateCheck(saveLoadSlot)) {
     270                                                _eventList.clear();
     271                                                breakLoop = true;
     272                                        }
    272273                                } else {
    273274                                        char savegameName[14];
    274275                                        sprintf(savegameName, "Quicksave %d", event.kbd.keycode - Common::KEYCODE_0);
  • engines/kyra/kyra_v1.h

     
    409409
    410410        static kReadSaveHeaderError readSaveHeader(Common::SeekableReadStream *file, bool loadThumbnail, SaveHeader &header);
    411411
    412         void loadGameStateCheck(int slot);
     412        bool loadGameStateCheck(int slot);
    413413        virtual Common::Error loadGameState(int slot) = 0;
    414414        Common::Error saveGameState(int slot, const char *saveName) { return saveGameState(slot, saveName, 0); }
    415415        virtual Common::Error saveGameState(int slot, const char *saveName, const Graphics::Surface *thumbnail) = 0;
  • engines/kyra/saveload.cpp

     
    256256        }
    257257}
    258258
    259 void KyraEngine_v1::loadGameStateCheck(int slot) {
    260         if (loadGameState(slot) != Common::kNoError) {
     259bool KyraEngine_v1::loadGameStateCheck(int slot) {
     260        Common::Error ret = loadGameState(slot);
     261
     262        if (ret != Common::kNoError) {
    261263                const char *filename = getSavegameFilename(slot);
    262264                Common::String errorMessage = "Could not load savegame: '";
    263265                errorMessage += filename;
    264266                errorMessage += "'";
    265267
    266268                GUIErrorMessage(errorMessage);
     269
     270                // In case the file does not exist, we get a reading failed error and we can recover from it
     271                if (ret == Common::kReadingFailed) {
     272                        warning("%s", errorMessage.c_str());
     273                        return false;
     274                }
     275
     276                // The loading failed and the engine is in an unknown state, so we throw an error
    267277                error("%s", errorMessage.c_str());
     278                return false;
    268279        }
     280
     281        return true;
    269282}
    270283
    271284} // End of namespace Kyra