From 00e59a312211c0b49433ad3e9923460ed85edc50 Mon Sep 17 00:00:00 2001 From: Adrian Frühwirth Date: Fri, 6 Apr 2018 00:06:38 +0200 Subject: ALL: Load savegame thumbnail only when necessary This commit introduces the following changes: 1. Graphics::loadThumbnail() Now returns a boolean and takes a new argument skipThumbnail which defaults to false. In case of true, loadThumbnail() reads past the thumbnail data in the input stream instead of actually loading the thumbnail. This simplifies savegame handling where, up until now, many engines always read the whole savegame metadata (including the thumbnail) and then threw away the thumbnail when not needed (which is in almost all cases, the most common exception being MetaEngine::querySaveMetaInfos() which is responsible for loading savegame metadata for displaying it in the GUI launcher. 2. readSavegameHeader() Engines which already implement such a method (name varies) now take a new argument skipThumbnail (default: true) which is passed through to loadThumbnail(). This means that the default case for readSavegameHeader() is now _not_ loading the thumbnail from a savegame and just reading past it. In those cases, e.g. querySaveMetaInfos(), where we actually are interested in loading the thumbnail readSavegameHeader() needs to explicitely be called with skipThumbnail == false. Engines whose readSavegameHeader() (name varies) already takes an argument loadThumbnail have been adapted to have a similar prototype and semantics. I.e. readSaveHeader(in, loadThumbnail, header) now is readSaveHeader(in, header, skipThumbnail). 3. Error handling Engines which previously did not check the return value of readSavegameHeader() (name varies) now do so ensuring that possibly broken savegames (be it a broken thumbnail or something else) don't make it into the GUI launcher list in the first place. --- engines/mads/detection.cpp | 12 ++++++------ engines/mads/game.cpp | 12 +++--------- engines/mads/game.h | 2 +- 3 files changed, 10 insertions(+), 16 deletions(-) (limited to 'engines/mads') diff --git a/engines/mads/detection.cpp b/engines/mads/detection.cpp index 4fb8b82eb3..8eb3b4eee9 100644 --- a/engines/mads/detection.cpp +++ b/engines/mads/detection.cpp @@ -203,11 +203,8 @@ SaveStateList MADSMetaEngine::listSaves(const char *target) const { Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(*file); if (in) { - MADS::Game::readSavegameHeader(in, header); - saveList.push_back(SaveStateDescriptor(slot, header._saveName)); - - header._thumbnail->free(); - delete header._thumbnail; + if (MADS::Game::readSavegameHeader(in, header)) + saveList.push_back(SaveStateDescriptor(slot, header._saveName)); delete in; } } @@ -233,7 +230,10 @@ SaveStateDescriptor MADSMetaEngine::querySaveMetaInfos(const char *target, int s if (f) { MADS::MADSSavegameHeader header; - MADS::Game::readSavegameHeader(f, header); + if (!MADS::Game::readSavegameHeader(f, header, false)) { + delete f; + return SaveStateDescriptor(); + } delete f; // Create the return descriptor diff --git a/engines/mads/game.cpp b/engines/mads/game.cpp index 0a6741ba7a..bea0ea3bb4 100644 --- a/engines/mads/game.cpp +++ b/engines/mads/game.cpp @@ -485,11 +485,6 @@ void Game::loadGame(int slotNumber) { if (!readSavegameHeader(_saveFile, header)) error("Invalid savegame"); - if (header._thumbnail) { - header._thumbnail->free(); - delete header._thumbnail; - } - // Load most of the savegame data with the exception of scene specific info synchronize(s, true); @@ -527,9 +522,8 @@ void Game::saveGame(int slotNumber, const Common::String &saveName) { const char *const SAVEGAME_STR = "MADS"; #define SAVEGAME_STR_SIZE 4 -bool Game::readSavegameHeader(Common::InSaveFile *in, MADSSavegameHeader &header) { +WARN_UNUSED_RESULT bool Game::readSavegameHeader(Common::InSaveFile *in, MADSSavegameHeader &header, bool skipThumbnail) { char saveIdentBuffer[SAVEGAME_STR_SIZE + 1]; - header._thumbnail = nullptr; // Validate the header Id in->read(saveIdentBuffer, SAVEGAME_STR_SIZE + 1); @@ -546,9 +540,9 @@ bool Game::readSavegameHeader(Common::InSaveFile *in, MADSSavegameHeader &header while ((ch = (char)in->readByte()) != '\0') header._saveName += ch; // Get the thumbnail - header._thumbnail = Graphics::loadThumbnail(*in); - if (!header._thumbnail) + if (!Graphics::loadThumbnail(*in, header._thumbnail, skipThumbnail)) { return false; + } // Read in save date/time header._year = in->readSint16LE(); diff --git a/engines/mads/game.h b/engines/mads/game.h index 9defb58b1a..b979160f3d 100644 --- a/engines/mads/game.h +++ b/engines/mads/game.h @@ -237,7 +237,7 @@ public: /** * Read in a savegame header */ - static bool readSavegameHeader(Common::InSaveFile *in, MADSSavegameHeader &header); + WARN_UNUSED_RESULT static bool readSavegameHeader(Common::InSaveFile *in, MADSSavegameHeader &header, bool skipThumbnail = true); /** * Creates a temporary thumbnail for use in saving games -- cgit v1.2.3