diff options
author | Filippos Karapetis | 2009-10-29 09:58:36 +0000 |
---|---|---|
committer | Filippos Karapetis | 2009-10-29 09:58:36 +0000 |
commit | e5e80dd6d692b0b352913198a1bb5aa91da4402e (patch) | |
tree | f098ba16694d49429fb13ecc42f62a8b2919e07a /engines/sci | |
parent | 25b3e687a7e4aa9fcd643aed24f9ac15cb88ec48 (diff) | |
download | scummvm-rg350-e5e80dd6d692b0b352913198a1bb5aa91da4402e.tar.gz scummvm-rg350-e5e80dd6d692b0b352913198a1bb5aa91da4402e.tar.bz2 scummvm-rg350-e5e80dd6d692b0b352913198a1bb5aa91da4402e.zip |
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
Diffstat (limited to 'engines/sci')
-rw-r--r-- | engines/sci/engine/kfile.cpp | 112 |
1 files changed, 53 insertions, 59 deletions
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<SavegameDesc> &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<SavegameDesc> 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<SavegameDesc> &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(); |