From e5e80dd6d692b0b352913198a1bb5aa91da4402e Mon Sep 17 00:00:00 2001 From: Filippos Karapetis Date: Thu, 29 Oct 2009 09:58:36 +0000 Subject: Fixed the deletion of save games - the wrong save game would be deleted with the previous code, as we sort the list by date svn-id: r45499 --- engines/sci/engine/kfile.cpp | 112 ++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 59 deletions(-) (limited to 'engines/sci') diff --git a/engines/sci/engine/kfile.cpp b/engines/sci/engine/kfile.cpp index ad20ce5c67..7010258172 100644 --- a/engines/sci/engine/kfile.cpp +++ b/engines/sci/engine/kfile.cpp @@ -39,7 +39,11 @@ enum { MAX_SAVEGAME_NR = 20 /**< Maximum number of savegames */ }; - +struct SavegameDesc { + int id; + int date; + int time; +}; /* * Note on how file I/O is implemented: In ScummVM, one can not create/write @@ -325,6 +329,47 @@ static void fseek_wrapper(EngineState *s, int handle, int offset, int whence) { s->r_acc = make_reg(0, f->_in->seek(offset, whence)); } +static int _savegame_index_struct_compare(const void *a, const void *b) { + const SavegameDesc *A = (const SavegameDesc *)a; + const SavegameDesc *B = (const SavegameDesc *)b; + + if (B->date != A->date) + return B->date - A->date; + return B->time - A->time; +} + +void listSavegames(Common::Array &saves) { + Common::SaveFileManager *saveFileMan = g_engine->getSaveFileManager(); + + // Load all saves + Common::StringList saveNames = saveFileMan->listSavefiles(((SciEngine *)g_engine)->getSavegamePattern()); + + for (Common::StringList::const_iterator iter = saveNames.begin(); iter != saveNames.end(); ++iter) { + Common::String filename = *iter; + Common::SeekableReadStream *in; + if ((in = saveFileMan->openForLoading(filename))) { + SavegameMetadata meta; + if (!get_savegame_metadata(in, &meta)) { + // invalid + delete in; + continue; + } + delete in; + + SavegameDesc desc; + desc.id = strtol(filename.end() - 3, NULL, 10); + desc.date = meta.savegame_date; + desc.time = meta.savegame_time; + debug(3, "Savegame in file %s ok, id %d", filename.c_str(), desc.id); + + saves.push_back(desc); + } + } + + // Sort the list by creation date of the saves + qsort(saves.begin(), saves.size(), sizeof(SavegameDesc), _savegame_index_struct_compare); +} + reg_t kFGets(EngineState *s, int argc, reg_t *argv) { int maxsize = argv[1].toUint16(); char *buf = new char[maxsize]; @@ -350,15 +395,6 @@ reg_t kGetCWD(EngineState *s, int argc, reg_t *argv) { return argv[0]; } -void delete_savegame(EngineState *s, int savedir_nr) { - Common::String filename = ((Sci::SciEngine*)g_engine)->getSavegameName(savedir_nr); - - //printf("Deleting savegame '%s'\n", filename.c_str()); - - Common::SaveFileManager *saveFileMan = g_engine->getSaveFileManager(); - saveFileMan->removeSavefile(filename); -} - enum { K_DEVICE_INFO_GET_DEVICE = 0, K_DEVICE_INFO_GET_CURRENT_DEVICE = 1, @@ -414,7 +450,13 @@ reg_t kDeviceInfo(EngineState *s, int argc, reg_t *argv) { int savegame_id = argv[3].toUint16(); s->_segMan->strcpy(argv[1], "__throwaway"); debug(3, "K_DEVICE_INFO_GET_SAVEFILE_NAME(%s,%d) -> %s", game_prefix.c_str(), savegame_id, "__throwaway"); - delete_savegame(s, savegame_id); + Common::Array saves; + listSavegames(saves); + int savedir_nr = saves[savegame_id].id; + Common::String filename = ((Sci::SciEngine*)g_engine)->getSavegameName(savedir_nr); + //printf("Deleting savegame '%s'\n", filename.c_str()); + Common::SaveFileManager *saveFileMan = g_engine->getSaveFileManager(); + saveFileMan->removeSavefile(filename); } break; @@ -447,54 +489,6 @@ reg_t kCheckFreeSpace(EngineState *s, int argc, reg_t *argv) { return make_reg(0, 1); } - -struct SavegameDesc { - int id; - int date; - int time; -}; - -static int _savegame_index_struct_compare(const void *a, const void *b) { - const SavegameDesc *A = (const SavegameDesc *)a; - const SavegameDesc *B = (const SavegameDesc *)b; - - if (B->date != A->date) - return B->date - A->date; - return B->time - A->time; -} - -void listSavegames(Common::Array &saves) { - Common::SaveFileManager *saveFileMan = g_engine->getSaveFileManager(); - - // Load all saves - Common::StringList saveNames = saveFileMan->listSavefiles(((SciEngine *)g_engine)->getSavegamePattern()); - - for (Common::StringList::const_iterator iter = saveNames.begin(); iter != saveNames.end(); ++iter) { - Common::String filename = *iter; - Common::SeekableReadStream *in; - if ((in = saveFileMan->openForLoading(filename))) { - SavegameMetadata meta; - if (!get_savegame_metadata(in, &meta)) { - // invalid - delete in; - continue; - } - delete in; - - SavegameDesc desc; - desc.id = strtol(filename.end() - 3, NULL, 10); - desc.date = meta.savegame_date; - desc.time = meta.savegame_time; - debug(3, "Savegame in file %s ok, id %d", filename.c_str(), desc.id); - - saves.push_back(desc); - } - } - - // Sort the list by creation date of the saves - qsort(saves.begin(), saves.size(), sizeof(SavegameDesc), _savegame_index_struct_compare); -} - reg_t kCheckSaveGame(EngineState *s, int argc, reg_t *argv) { Common::String game_id = s->_segMan->getString(argv[0]); int savedir_nr = argv[1].toUint16(); -- cgit v1.2.3