From 73cc973ad7000317a5f918a67a784ab3202e7580 Mon Sep 17 00:00:00 2001 From: aryanrawlani28 Date: Mon, 3 Aug 2020 04:16:09 +0530 Subject: [PATCH] BASE: ENGINES: Change saveload code to adapt to the new MEC class. - MEC: MetaEngineConnect. - How do games handle save/load if MetaEngine (detection) is seperate from MetaEngineConnect (engine factory)? - Most of the changes are quite similiar. ConfMan finds us the relevant MetaEngine, then simply use the new helpers from PluginMan. - The new helpers will help convert a relevant MetaEngine into the other format or vice versa. - Once the matching is complete, simply invoke functions by: plugin->get().engineMethod(); - Refer to previous commits to see the new class changes & notes. --- base/commandLine.cpp | 30 ++++++++++++++++++++++-------- engines/engine.cpp | 6 +++--- gui/launcher.cpp | 20 +++++++++++++------- gui/saveload-dialog.cpp | 18 +++++++++--------- gui/saveload-dialog.h | 22 +++++++++++----------- gui/saveload.cpp | 20 +++++++++++++++----- gui/saveload.h | 2 +- 7 files changed, 74 insertions(+), 44 deletions(-) diff --git a/base/commandLine.cpp b/base/commandLine.cpp index c52f280a317..d2b8ad3314a 100644 --- a/base/commandLine.cpp +++ b/base/commandLine.cpp @@ -867,15 +867,18 @@ static Common::Error listSaves(const Common::String &singleTarget) { // the specified game name, or alternatively whether there is a matching game id. Common::String currentTarget; QualifiedGameDescriptor game; - const Plugin *plugin = nullptr; + + const Plugin *metaEnginePlugin = nullptr; + const Plugin *enginePlugin = nullptr; + if (ConfMan.hasGameDomain(*i)) { // The name is a known target currentTarget = *i; EngineMan.upgradeTargetIfNecessary(*i); - game = EngineMan.findTarget(*i, &plugin); + game = EngineMan.findTarget(*i, &metaEnginePlugin); } else if (game = findGameMatchingName(*i), !game.gameId.empty()) { // The name is a known game id - plugin = EngineMan.findPlugin(game.engineId); + metaEnginePlugin = EngineMan.findPlugin(game.engineId); currentTarget = createTemporaryTarget(game.engineId, game.gameId); } else { return Common::Error(Common::kEnginePluginNotFound, Common::String::format("target '%s'", singleTarget.c_str())); @@ -884,19 +887,30 @@ static Common::Error listSaves(const Common::String &singleTarget) { // If we actually found a domain, we're going to change the domain ConfMan.setActiveDomain(currentTarget); - if (!plugin) { + if (!metaEnginePlugin) { // If the target was specified, treat this as an error, and otherwise skip it. if (!singleTarget.empty()) - return Common::Error(Common::kEnginePluginNotFound, + return Common::Error(Common::kMetaEnginePluginNotFound, Common::String::format("target '%s'", i->c_str())); - printf("Plugin could not be loaded for target '%s'\n", i->c_str()); + printf("MetaEnginePlugin could not be loaded for target '%s'\n", i->c_str()); continue; + } else { + enginePlugin = PluginMan.giveEngineFromMetaEngine(metaEnginePlugin); + + if (!enginePlugin) { + // If the target was specified, treat this as an error, and otherwise skip it. + if (!singleTarget.empty()) + return Common::Error(Common::kEnginePluginNotFound, + Common::String::format("target '%s'", i->c_str())); + printf("EnginePlugin could not be loaded for target '%s'\n", i->c_str()); + continue; + } } - const MetaEngine &metaEngine = plugin->get(); + const MetaEngineConnect &metaEngine = enginePlugin->get(); Common::String qualifiedGameId = buildQualifiedGameName(game.engineId, game.gameId); - if (!metaEngine.hasFeature(MetaEngine::kSupportsListSaves)) { + if (!metaEngine.hasFeature(MetaEngineConnect::kSupportsListSaves)) { // If the target was specified, treat this as an error, and otherwise skip it. if (!singleTarget.empty()) // TODO: Include more info about the target (desc, engine name, ...) ??? diff --git a/engines/engine.cpp b/engines/engine.cpp index fd5add4f9d3..dd14db771e7 100644 --- a/engines/engine.cpp +++ b/engines/engine.cpp @@ -531,7 +531,7 @@ void Engine::saveAutosaveIfEnabled() { if (saveFlag) { // First check for an existing savegame in the slot, and if present, if it's an autosave - SaveStateDescriptor desc = getMetaEngine().querySaveMetaInfos( + SaveStateDescriptor desc = getMetaEngineConnect().querySaveMetaInfos( _targetName.c_str(), getAutosaveSlot()); saveFlag = desc.getSaveSlot() == -1 || desc.isAutosave(); } @@ -731,7 +731,7 @@ Common::Error Engine::loadGameState(int slot) { Common::Error result = loadGameStream(saveFile); if (result.getCode() == Common::kNoError) { ExtendedSavegameHeader header; - if (MetaEngine::readSavegameHeader(saveFile, &header)) + if (MetaEngineConnect::readSavegameHeader(saveFile, &header)) setTotalPlayTime(header.playtime); } @@ -757,7 +757,7 @@ Common::Error Engine::saveGameState(int slot, const Common::String &desc, bool i Common::Error result = saveGameStream(saveFile, isAutosave); if (result.getCode() == Common::kNoError) { - MetaEngine::appendExtendedSave(saveFile, getTotalPlayTime() / 1000, desc, isAutosave); + MetaEngineConnect::appendExtendedSave(saveFile, getTotalPlayTime() / 1000, desc, isAutosave); saveFile->finalize(); } diff --git a/gui/launcher.cpp b/gui/launcher.cpp index 9135c058141..87f5866bb15 100644 --- a/gui/launcher.cpp +++ b/gui/launcher.cpp @@ -469,14 +469,20 @@ void LauncherDialog::loadGame(int item) { EngineMan.upgradeTargetIfNecessary(target); // Look for the plugin - const Plugin *plugin = nullptr; - EngineMan.findTarget(target, &plugin); + const Plugin *metaEnginePlugin = nullptr; + const Plugin *enginePlugin = nullptr; + EngineMan.findTarget(target, &metaEnginePlugin); - if (plugin) { - const MetaEngine &metaEngine = plugin->get(); - if (metaEngine.hasFeature(MetaEngine::kSupportsListSaves) && - metaEngine.hasFeature(MetaEngine::kSupportsLoadingDuringStartup)) { - int slot = _loadDialog->runModalWithPluginAndTarget(plugin, target); + // If we found a relevant plugin, find the matching engine plugin. + if (metaEnginePlugin) { + enginePlugin = PluginMan.giveEngineFromMetaEngine(metaEnginePlugin); + } + + if (enginePlugin) { + const MetaEngineConnect &metaEngineConnect = enginePlugin->get(); + if (metaEngineConnect.hasFeature(MetaEngineConnect::kSupportsListSaves) && + metaEngineConnect.hasFeature(MetaEngineConnect::kSupportsLoadingDuringStartup)) { + int slot = _loadDialog->runModalWithPluginAndTarget(enginePlugin, target); if (slot >= 0) { ConfMan.setActiveDomain(_domains[item]); ConfMan.setInt("save_slot", slot, Common::ConfigManager::kTransientDomain); diff --git a/gui/saveload-dialog.cpp b/gui/saveload-dialog.cpp index 54a8e2539bd..894529233a3 100644 --- a/gui/saveload-dialog.cpp +++ b/gui/saveload-dialog.cpp @@ -103,7 +103,7 @@ void SaveLoadCloudSyncProgressDialog::handleTickle() { #endif #ifndef DISABLE_SAVELOADCHOOSER_GRID -SaveLoadChooserType getRequestedSaveLoadDialog(const MetaEngine &metaEngine) { +SaveLoadChooserType getRequestedSaveLoadDialog(const MetaEngineConnect &metaEngine) { const Common::String &userConfig = ConfMan.get("gui_saveload_chooser", Common::ConfigManager::kApplicationDomain); // Check (and update if necessary) the theme config here. This catches @@ -114,8 +114,8 @@ SaveLoadChooserType getRequestedSaveLoadDialog(const MetaEngine &metaEngine) { g_gui.checkScreenChange(); if (g_gui.getWidth() >= 640 && g_gui.getHeight() >= 400 - && metaEngine.hasFeature(MetaEngine::kSavesSupportMetaInfo) - && metaEngine.hasFeature(MetaEngine::kSavesSupportThumbnail) + && metaEngine.hasFeature(MetaEngineConnect::kSavesSupportMetaInfo) + && metaEngine.hasFeature(MetaEngineConnect::kSavesSupportThumbnail) && userConfig.equalsIgnoreCase("grid")) { // In case we are 640x400 or higher, this dialog is not in save mode, // the user requested the grid dialog and the engines supports it we @@ -182,14 +182,14 @@ void SaveLoadChooserDialog::close() { Dialog::close(); } -int SaveLoadChooserDialog::run(const Common::String &target, const MetaEngine *metaEngine) { +int SaveLoadChooserDialog::run(const Common::String &target, const MetaEngineConnect *metaEngine) { _metaEngine = metaEngine; _target = target; - _delSupport = _metaEngine->hasFeature(MetaEngine::kSupportsDeleteSave); - _metaInfoSupport = _metaEngine->hasFeature(MetaEngine::kSavesSupportMetaInfo); - _thumbnailSupport = _metaInfoSupport && _metaEngine->hasFeature(MetaEngine::kSavesSupportThumbnail); - _saveDateSupport = _metaInfoSupport && _metaEngine->hasFeature(MetaEngine::kSavesSupportCreationDate); - _playTimeSupport = _metaInfoSupport && _metaEngine->hasFeature(MetaEngine::kSavesSupportPlayTime); + _delSupport = _metaEngine->hasFeature(MetaEngineConnect::kSupportsDeleteSave); + _metaInfoSupport = _metaEngine->hasFeature(MetaEngineConnect::kSavesSupportMetaInfo); + _thumbnailSupport = _metaInfoSupport && _metaEngine->hasFeature(MetaEngineConnect::kSavesSupportThumbnail); + _saveDateSupport = _metaInfoSupport && _metaEngine->hasFeature(MetaEngineConnect::kSavesSupportCreationDate); + _playTimeSupport = _metaInfoSupport && _metaEngine->hasFeature(MetaEngineConnect::kSavesSupportPlayTime); return runIntern(); } diff --git a/gui/saveload-dialog.h b/gui/saveload-dialog.h index aaea785b230..8ff5851ca74 100644 --- a/gui/saveload-dialog.h +++ b/gui/saveload-dialog.h @@ -65,7 +65,7 @@ enum SaveLoadChooserType { kSaveLoadDialogGrid = 1 }; -SaveLoadChooserType getRequestedSaveLoadDialog(const MetaEngine &metaEngine); +SaveLoadChooserType getRequestedSaveLoadDialog(const MetaEngineConnect &metaEngine); #endif // !DISABLE_SAVELOADCHOOSER_GRID class SaveLoadChooserDialog : protected Dialog { @@ -91,7 +91,7 @@ public: virtual SaveLoadChooserType getType() const = 0; #endif // !DISABLE_SAVELOADCHOOSER_GRID - int run(const Common::String &target, const MetaEngine *metaEngine); + int run(const Common::String &target, const MetaEngineConnect *metaEngine); virtual const Common::U32String &getResultString() const = 0; protected: @@ -110,16 +110,16 @@ protected: */ virtual void listSaves(); - const bool _saveMode; - const MetaEngine *_metaEngine; - bool _delSupport; - bool _metaInfoSupport; - bool _thumbnailSupport; - bool _saveDateSupport; - bool _playTimeSupport; - Common::String _target; + const bool _saveMode; + const MetaEngineConnect *_metaEngine; + bool _delSupport; + bool _metaInfoSupport; + bool _thumbnailSupport; + bool _saveDateSupport; + bool _playTimeSupport; + Common::String _target; bool _dialogWasShown; - SaveStateList _saveList; + SaveStateList _saveList; #ifndef DISABLE_SAVELOADCHOOSER_GRID ButtonWidget *_listButton; diff --git a/gui/saveload.cpp b/gui/saveload.cpp index f37d3ed57dc..80680bf9346 100644 --- a/gui/saveload.cpp +++ b/gui/saveload.cpp @@ -39,7 +39,7 @@ SaveLoadChooser::~SaveLoadChooser() { _impl = nullptr; } -void SaveLoadChooser::selectChooser(const MetaEngine &engine) { +void SaveLoadChooser::selectChooser(const MetaEngineConnect &engine) { #ifndef DISABLE_SAVELOADCHOOSER_GRID const SaveLoadChooserType requestedType = getRequestedSaveLoadDialog(engine); if (!_impl || _impl->getType() != requestedType) { @@ -77,14 +77,24 @@ Common::String SaveLoadChooser::createDefaultSaveDescription(const int slot) con int SaveLoadChooser::runModalWithCurrentTarget() { const Plugin *plugin = EngineMan.findPlugin(ConfMan.get("engineid")); + const Plugin *enginePlugin = nullptr; if (!plugin) { error("SaveLoadChooser::runModalWithCurrentTarget(): Cannot find plugin"); + } else { + enginePlugin = PluginMan.giveEngineFromMetaEngine(plugin); + + if (!enginePlugin) { + error("SaveLoadChooser::runModalWithCurrentTarget(): Couldn't match a Engine from the MetaEngine. \ + You will not be able to see savefiles until you have the necessary plugins."); + } } - return runModalWithPluginAndTarget(plugin, ConfMan.getActiveDomainName()); + return runModalWithPluginAndTarget(enginePlugin, ConfMan.getActiveDomainName()); } int SaveLoadChooser::runModalWithPluginAndTarget(const Plugin *plugin, const String &target) { - selectChooser(plugin->get()); + assert(plugin->getType() == PLUGIN_TYPE_ENGINE); + + selectChooser(plugin->get()); if (!_impl) return -1; @@ -99,10 +109,10 @@ int SaveLoadChooser::runModalWithPluginAndTarget(const Plugin *plugin, const Str int ret; do { - ret = _impl->run(target, &plugin->get()); + ret = _impl->run(target, &plugin->get()); #ifndef DISABLE_SAVELOADCHOOSER_GRID if (ret == kSwitchSaveLoadDialog) { - selectChooser(plugin->get()); + selectChooser(plugin->get()); } #endif // !DISABLE_SAVELOADCHOOSER_GRID } while (ret < -1); diff --git a/gui/saveload.h b/gui/saveload.h index 3a976863d53..5591c04dc43 100644 --- a/gui/saveload.h +++ b/gui/saveload.h @@ -40,7 +40,7 @@ protected: const U32String _buttonLabel; const bool _saveMode; - void selectChooser(const MetaEngine &engine); + void selectChooser(const MetaEngineConnect &engine); public: SaveLoadChooser(const U32String &title, const U32String &buttonLabel, bool saveMode); ~SaveLoadChooser();