scummvm/engines/sword1/metaengine.cpp
2021-04-15 21:20:36 +02:00

198 lines
5.9 KiB
C++

/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "sword1/sword1.h"
#include "sword1/control.h"
#include "common/savefile.h"
#include "common/system.h"
#include "graphics/thumbnail.h"
#include "graphics/surface.h"
#include "engines/metaengine.h"
class SwordMetaEngine : public MetaEngine {
public:
const char *getName() const override {
return "sword1";
}
bool hasFeature(MetaEngineFeature f) const override;
SaveStateList listSaves(const char *target) const override;
int getMaximumSaveSlot() const override;
void removeSaveState(const char *target, int slot) const override;
SaveStateDescriptor querySaveMetaInfos(const char *target, int slot) const override;
Common::Error createInstance(OSystem *syst, Engine **engine) const override;
};
bool SwordMetaEngine::hasFeature(MetaEngineFeature f) const {
return
(f == kSupportsListSaves) ||
(f == kSupportsLoadingDuringStartup) ||
(f == kSupportsDeleteSave) ||
(f == kSavesSupportMetaInfo) ||
(f == kSavesSupportThumbnail) ||
(f == kSavesSupportCreationDate) ||
(f == kSavesSupportPlayTime);
}
bool Sword1::SwordEngine::hasFeature(EngineFeature f) const {
return
(f == kSupportsReturnToLauncher) ||
(f == kSupportsSavingDuringRuntime) ||
(f == kSupportsLoadingDuringRuntime);
}
Common::Error SwordMetaEngine::createInstance(OSystem *syst, Engine **engine) const {
assert(engine);
*engine = new Sword1::SwordEngine(syst);
return Common::kNoError;
}
SaveStateList SwordMetaEngine::listSaves(const char *target) const {
Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
SaveStateList saveList;
char saveName[40];
Common::StringArray filenames = saveFileMan->listSavefiles("sword1.###");
int slotNum = 0;
for (Common::StringArray::const_iterator file = filenames.begin(); file != filenames.end(); ++file) {
// Obtain the last 3 digits of the filename, since they correspond to the save slot
slotNum = atoi(file->c_str() + file->size() - 3);
if (slotNum >= 0 && slotNum <= 999) {
Common::InSaveFile *in = saveFileMan->openForLoading(*file);
if (in) {
in->readUint32LE(); // header
in->read(saveName, 40);
saveList.push_back(SaveStateDescriptor(slotNum, saveName));
delete in;
}
}
}
// Sort saves based on slot number.
Common::sort(saveList.begin(), saveList.end(), SaveStateDescriptorSlotComparator());
return saveList;
}
int SwordMetaEngine::getMaximumSaveSlot() const { return 999; }
void SwordMetaEngine::removeSaveState(const char *target, int slot) const {
g_system->getSavefileManager()->removeSavefile(Common::String::format("sword1.%03d", slot));
}
SaveStateDescriptor SwordMetaEngine::querySaveMetaInfos(const char *target, int slot) const {
Common::String fileName = Common::String::format("sword1.%03d", slot);
char name[40];
uint32 playTime = 0;
byte versionSave;
Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(fileName);
if (in) {
in->skip(4); // header
in->read(name, sizeof(name));
in->read(&versionSave, 1); // version
SaveStateDescriptor desc(slot, name);
if (versionSave < 2) // These older version of the savegames used a flag to signal presence of thumbnail
in->skip(1);
if (Graphics::checkThumbnailHeader(*in)) {
Graphics::Surface *thumbnail;
if (!Graphics::loadThumbnail(*in, thumbnail)) {
delete in;
return SaveStateDescriptor();
}
desc.setThumbnail(thumbnail);
}
uint32 saveDate = in->readUint32BE();
uint16 saveTime = in->readUint16BE();
if (versionSave > 1) // Previous versions did not have playtime data
playTime = in->readUint32BE();
int day = (saveDate >> 24) & 0xFF;
int month = (saveDate >> 16) & 0xFF;
int year = saveDate & 0xFFFF;
desc.setSaveDate(year, month, day);
int hour = (saveTime >> 8) & 0xFF;
int minutes = saveTime & 0xFF;
desc.setSaveTime(hour, minutes);
if (versionSave > 1) {
desc.setPlayTime(playTime * 1000);
} else { //We have no playtime data
desc.setPlayTime(0);
}
delete in;
return desc;
}
return SaveStateDescriptor();
}
#if PLUGIN_ENABLED_DYNAMIC(SWORD1)
REGISTER_PLUGIN_DYNAMIC(SWORD1, PLUGIN_TYPE_ENGINE, SwordMetaEngine);
#else
REGISTER_PLUGIN_STATIC(SWORD1, PLUGIN_TYPE_ENGINE, SwordMetaEngine);
#endif
namespace Sword1 {
Common::Error SwordEngine::loadGameState(int slot) {
_systemVars.forceRestart = false;
_systemVars.controlPanelMode = CP_NORMAL;
_control->restoreGameFromFile(slot);
reinitialize();
_control->doRestore();
reinitRes();
return Common::kNoError; // TODO: return success/failure
}
bool SwordEngine::canLoadGameStateCurrently() {
return (mouseIsActive() && !_control->isPanelShown()); // Disable GMM loading when game panel is shown
}
Common::Error SwordEngine::saveGameState(int slot, const Common::String &desc, bool isAutosave) {
_control->setSaveDescription(slot, desc.c_str());
_control->saveGameToFile(slot);
return Common::kNoError; // TODO: return success/failure
}
bool SwordEngine::canSaveGameStateCurrently() {
return (mouseIsActive() && !_control->isPanelShown());
}
} // End of namespace Sword1