diff options
author | Gregory Montoir | 2008-12-07 14:24:46 +0000 |
---|---|---|
committer | Gregory Montoir | 2008-12-07 14:24:46 +0000 |
commit | 3de7a31dc18547167364967f1a33e3b03f555e58 (patch) | |
tree | 06e9581a13aa925760c58891634e526bae99e716 /engines/touche | |
parent | e63c0ba1e2295f4848ea02753e920613c3b49d38 (diff) | |
download | scummvm-rg350-3de7a31dc18547167364967f1a33e3b03f555e58.tar.gz scummvm-rg350-3de7a31dc18547167364967f1a33e3b03f555e58.tar.bz2 scummvm-rg350-3de7a31dc18547167364967f1a33e3b03f555e58.zip |
reduced saveload code dup
svn-id: r35276
Diffstat (limited to 'engines/touche')
-rw-r--r-- | engines/touche/detection.cpp | 72 | ||||
-rw-r--r-- | engines/touche/menu.cpp | 11 | ||||
-rw-r--r-- | engines/touche/saveload.cpp | 52 | ||||
-rw-r--r-- | engines/touche/touche.h | 19 |
4 files changed, 66 insertions, 88 deletions
diff --git a/engines/touche/detection.cpp b/engines/touche/detection.cpp index 4e81eb9c1e..039df82515 100644 --- a/engines/touche/detection.cpp +++ b/engines/touche/detection.cpp @@ -159,73 +159,47 @@ bool Touche::ToucheEngine::hasFeature(EngineFeature f) const { } bool ToucheMetaEngine::createInstance(OSystem *syst, Engine **engine, const Common::ADGameDescription *desc) const { - const Common::ADGameDescription *gd = desc; - if (gd) { - *engine = new Touche::ToucheEngine(syst, gd->language); + if (desc) { + *engine = new Touche::ToucheEngine(syst, desc->language); } - return gd != 0; + return desc != 0; } SaveStateList ToucheMetaEngine::listSaves(const char *target) const { - Common::SaveFileManager *saveFileMan = g_system->getSavefileManager(); - Common::StringList filenames; - char saveDesc[Touche::kGameStateDescriptionLen]; - Common::String pattern = target; - pattern += ".?"; - - filenames = saveFileMan->listSavefiles(pattern.c_str()); - sort(filenames.begin(), filenames.end()); // Sort (hopefully ensuring we are sorted numerically..) - + Common::String pattern = Touche::generateGameStateFileName(target, 0, true); + Common::StringList filenames = g_system->getSavefileManager()->listSavefiles(pattern.c_str()); + bool slotsTable[Touche::kMaxSaveStates]; + memset(slotsTable, 0, sizeof(slotsTable)); SaveStateList saveList; for (Common::StringList::const_iterator file = filenames.begin(); file != filenames.end(); ++file) { - // Obtain the last digit of the filename, since they correspond to the save slot - int slotNum = atoi(file->c_str() + file->size() - 1); - - if (slotNum >= 0 && slotNum <= 9) { - Common::InSaveFile *in = saveFileMan->openForLoading(file->c_str()); - if (in) { - in->readUint16LE(); - in->readUint16LE(); - in->read(saveDesc, Touche::kGameStateDescriptionLen); - saveList.push_back(SaveStateDescriptor(slotNum, saveDesc)); - delete in; - } + int slot = Touche::getGameStateFileSlot(file->c_str()); + if (slot >= 0 && slot < Touche::kMaxSaveStates) { + slotsTable[slot] = true; } } - - pattern += "?"; - - filenames = saveFileMan->listSavefiles(pattern.c_str()); - sort(filenames.begin(), filenames.end()); // Sort (hopefully ensuring we are sorted numerically..) - - for (Common::StringList::const_iterator file = filenames.begin(); file != filenames.end(); ++file) { - // Obtain the last 2 digits of the filename, since they correspond to the save slot - int slotNum = atoi(file->c_str() + file->size() - 2); - - if (slotNum >= 10 && slotNum <= 99) { - Common::InSaveFile *in = saveFileMan->openForLoading(file->c_str()); + for (int slot = 0; slot < Touche::kMaxSaveStates; ++slot) { + if (slotsTable[slot]) { + Common::String file = Touche::generateGameStateFileName(target, slot); + Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(file.c_str()); if (in) { - in->readUint16LE(); - in->readUint16LE(); - in->read(saveDesc, Touche::kGameStateDescriptionLen); - saveList.push_back(SaveStateDescriptor(slotNum, saveDesc)); + char description[64] = { 0 }; + Touche::readGameStateDescription(in, description, sizeof(description)); + if (description[0]) { + saveList.push_back(SaveStateDescriptor(slot, description)); + } delete in; } } } - return saveList; } -int ToucheMetaEngine::getMaximumSaveSlot() const { return 99; } +int ToucheMetaEngine::getMaximumSaveSlot() const { + return Touche::kMaxSaveStates - 1; +} void ToucheMetaEngine::removeSaveState(const char *target, int slot) const { - char extension[5]; - snprintf(extension, sizeof(extension), ".%d", slot); - - Common::String filename = target; - filename += extension; - + Common::String filename = Touche::generateGameStateFileName(target, slot); g_system->getSavefileManager()->removeSavefile(filename.c_str()); } diff --git a/engines/touche/menu.cpp b/engines/touche/menu.cpp index fbc73d027e..4511c3043e 100644 --- a/engines/touche/menu.cpp +++ b/engines/touche/menu.cpp @@ -375,13 +375,16 @@ void ToucheEngine::handleOptions(int forceDisplay) { for (int i = 0; i < kMaxSaveStates; ++i) { menuData.saveLoadDescriptionsTable[i][0] = 0; } - char gameStateFileName[16]; - generateGameStateFileName(999, gameStateFileName, 15, true); - Common::StringList filenames = _saveFileMan->listSavefiles(gameStateFileName); + Common::String gameStateFileName = generateGameStateFileName(_targetName.c_str(), 0, true); + Common::StringList filenames = _saveFileMan->listSavefiles(gameStateFileName.c_str()); for (Common::StringList::const_iterator it = filenames.begin(); it != filenames.end(); ++it) { int i = getGameStateFileSlot(it->c_str()); if (i >= 0 && i < kMaxSaveStates) { - readGameStateDescription(i, menuData.saveLoadDescriptionsTable[i], 32); + Common::InSaveFile *f = _saveFileMan->openForLoading(it->c_str()); + if (f) { + readGameStateDescription(f, menuData.saveLoadDescriptionsTable[i], 32); + delete f; + } } } } diff --git a/engines/touche/saveload.cpp b/engines/touche/saveload.cpp index 5d63d45bb5..3bbb1253f3 100644 --- a/engines/touche/saveload.cpp +++ b/engines/touche/saveload.cpp @@ -31,6 +31,11 @@ namespace Touche { +enum { + kGameStateDescriptionLen = 32, + kCurrentGameStateVersion = 6 +}; + static void saveOrLoad(Common::WriteStream &stream, uint16 &i) { stream.writeUint16LE(i); } @@ -318,9 +323,8 @@ void ToucheEngine::loadGameStateData(Common::ReadStream *stream) { Common::Error ToucheEngine::saveGameState(int num, const char *description) { bool saveOk = false; - char gameStateFileName[64]; - generateGameStateFileName(num, gameStateFileName, 63); - Common::OutSaveFile *f = _saveFileMan->openForSaving(gameStateFileName); + Common::String gameStateFileName = generateGameStateFileName(_targetName.c_str(), num); + Common::OutSaveFile *f = _saveFileMan->openForSaving(gameStateFileName.c_str()); if (f) { f->writeUint16LE(kCurrentGameStateVersion); f->writeUint16LE(0); @@ -333,7 +337,7 @@ Common::Error ToucheEngine::saveGameState(int num, const char *description) { if (!f->ioFailed()) { saveOk = true; } else { - warning("Can't write file '%s'", gameStateFileName); + warning("Can't write file '%s'", gameStateFileName.c_str()); } delete f; } @@ -342,9 +346,8 @@ Common::Error ToucheEngine::saveGameState(int num, const char *description) { Common::Error ToucheEngine::loadGameState(int num) { bool loadOk = false; - char gameStateFileName[64]; - generateGameStateFileName(num, gameStateFileName, 63); - Common::InSaveFile *f = _saveFileMan->openForLoading(gameStateFileName); + Common::String gameStateFileName = generateGameStateFileName(_targetName.c_str(), num); + Common::InSaveFile *f = _saveFileMan->openForLoading(gameStateFileName.c_str()); if (f) { uint16 version = f->readUint16LE(); if (version < kCurrentGameStateVersion) { @@ -355,7 +358,7 @@ Common::Error ToucheEngine::loadGameState(int num) { if (!f->ioFailed()) { loadOk = true; } else { - warning("Can't read file '%s'", gameStateFileName); + warning("Can't read file '%s'", gameStateFileName.c_str()); } } delete f; @@ -363,31 +366,30 @@ Common::Error ToucheEngine::loadGameState(int num) { return loadOk ? Common::kNoError : Common::kUnknownError; } -void ToucheEngine::readGameStateDescription(int num, char *description, int len) { - char gameStateFileName[64]; - generateGameStateFileName(num, gameStateFileName, 63); - Common::InSaveFile *f = _saveFileMan->openForLoading(gameStateFileName); - if (f) { - uint16 version = f->readUint16LE(); - if (version >= kCurrentGameStateVersion) { - f->readUint16LE(); - f->read(description, MIN<int>(len, kGameStateDescriptionLen)); - description[len] = 0; - } - delete f; +void readGameStateDescription(Common::ReadStream *f, char *description, int len) { + uint16 version = f->readUint16LE(); + if (version >= kCurrentGameStateVersion) { + f->readUint16LE(); + f->read(description, MIN<int>(len, kGameStateDescriptionLen)); + description[len] = 0; + } else { + description[0] = 0; } } -void ToucheEngine::generateGameStateFileName(int num, char *dst, int len, bool prefixOnly) const { +Common::String generateGameStateFileName(const char *target, int slot, bool prefixOnly) { + Common::String name(target); if (prefixOnly) { - snprintf(dst, len, "%s.*", _targetName.c_str()); + name += ".*"; } else { - snprintf(dst, len, "%s.%d", _targetName.c_str(), num); + char slotStr[16]; + snprintf(slotStr, sizeof(slotStr), ".%d", slot); + name += slotStr; } - dst[len] = 0; + return name; } -int ToucheEngine::getGameStateFileSlot(const char *filename) const { +int getGameStateFileSlot(const char *filename) { int i = -1; const char *slot = strrchr(filename, '.'); if (slot) { diff --git a/engines/touche/touche.h b/engines/touche/touche.h index 475104f35a..3882699a52 100644 --- a/engines/touche/touche.h +++ b/engines/touche/touche.h @@ -328,9 +328,7 @@ enum { kCursorHeight = 42, kTextHeight = 16, kMaxProgramDataSize = 61440, - kMaxSaveStates = 100, - kGameStateDescriptionLen = 32, // Need these two values defined here - kCurrentGameStateVersion = 6 // for --list-saves support + kMaxSaveStates = 100 }; enum StringType { @@ -338,6 +336,10 @@ enum StringType { kStringTypeConversation }; +void readGameStateDescription(Common::ReadStream *f, char *description, int len); +Common::String generateGameStateFileName(const char *target, int slot, bool prefixOnly = false); +int getGameStateFileSlot(const char *filename); + class MidiPlayer; class ToucheEngine: public Engine { @@ -497,13 +499,10 @@ protected: void saveGameStateData(Common::WriteStream *stream); void loadGameStateData(Common::ReadStream *stream); - Common::Error saveGameState(int num, const char *description); - Common::Error loadGameState(int num); - bool canLoadGameStateCurrently(); - bool canSaveGameStateCurrently(); - void readGameStateDescription(int num, char *description, int len); - void generateGameStateFileName(int num, char *dst, int len, bool prefixOnly = false) const; - int getGameStateFileSlot(const char *filename) const; + virtual Common::Error saveGameState(int num, const char *description); + virtual Common::Error loadGameState(int num); + virtual bool canLoadGameStateCurrently(); + virtual bool canSaveGameStateCurrently(); void setupOpcodes(); void op_nop(); |