Skip to content

Commit

Permalink
API: Added HOOK_SAVEGAME_CONVERT for use by savegame converter plugins
Browse files Browse the repository at this point in the history
The convertSavegame() utility attempts to perform this conversion,
synchronously, via the Doomsday plugin system.

Each plugin will be tried in turn and if the conversion succeeds then
given SavedSession is updated accordingly.
  • Loading branch information
danij-deng committed Mar 14, 2014
1 parent 5e247d5 commit 2e8cf9a
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
7 changes: 7 additions & 0 deletions doomsday/api/api_plugin.h
Expand Up @@ -55,6 +55,7 @@ enum {
HOOK_FINALE_SCRIPT_TICKER = 8, ///< Called each time a script 'thinks'.
HOOK_FINALE_EVAL_IF = 9, ///< Called to evaluate an IF conditional statement.
HOOK_VIEWPORT_RESHAPE = 10, ///< Called when viewport dimensions change.
HOOK_SAVEGAME_CONVERT = 11, ///< Called when a legacy savegame needs converting.
NUM_HOOK_TYPES
};

Expand All @@ -80,6 +81,12 @@ typedef struct {
RectRaw oldGeometry; // Previous.
} ddhook_viewport_reshape_t;

/// Parameters for HOOK_SAVEGAME_CONVERT
typedef struct {
Str inputFilePath;
Str outputFilePath;
} ddhook_savegame_convert_t;

DENG_API_TYPEDEF(Plug) // v1
{
de_api_t api;
Expand Down
2 changes: 2 additions & 0 deletions doomsday/client/client.pro
Expand Up @@ -347,6 +347,7 @@ DENG_HEADERS += \
include/resource/pcx.h \
include/resource/rawtexture.h \
include/resource/resourcesystem.h \
include/resource/savegameconverter.h \
include/resource/sprite.h \
include/resource/texture.h \
include/resource/texturemanifest.h \
Expand Down Expand Up @@ -676,6 +677,7 @@ SOURCES += \
src/resource/patchname.cpp \
src/resource/pcx.cpp \
src/resource/resourcesystem.cpp \
src/resource/savegameconverter.cpp \
src/resource/sprite.cpp \
src/resource/texture.cpp \
src/resource/texturemanifest.cpp \
Expand Down
39 changes: 39 additions & 0 deletions doomsday/client/include/resource/savegameconverter.h
@@ -0,0 +1,39 @@
/** @file savegameconverter.h Utility for converting legacy savegames.
*
* @authors Copyright © 2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef DENG_RESOURCE_SAVEGAMECONVERTER_H
#define DENG_RESOURCE_SAVEGAMECONVERTER_H

#include <de/game/SavedSession>
#include <de/Path>

namespace de {

/**
* Utility for converting legacy savegame file formats (via plugins).
*
* @param inputFilePath Path to the savegame file to be converted.
* @param session SavedSession to update if conversion is successful.
*
* @ingroup resource
*/
bool convertSavegame(de::Path inputFilePath, de::game::SavedSession &session);

}

#endif // DENG_RESOURCE_SAVEGAMECONVERTER_H
71 changes: 71 additions & 0 deletions doomsday/client/src/resource/savegameconverter.cpp
@@ -0,0 +1,71 @@
/** @file savegameconverter.cpp Utility for converting legacy savegames.
*
* @authors Copyright © 2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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, see:
* http://www.gnu.org/licenses</small>
*/

#include "de_base.h"
#include "resource/savegameconverter.h"

#include "dd_main.h"
#include <de/game/SavedSessionRepository>
#include <de/Log>
#include <de/NativePath>

namespace de {

static void tryConversion(Path const &inputFilePath, Path const &outputFilePath)
{
LOG_DEBUG("Attempting \"%s\"...") << NativePath(inputFilePath).pretty();

ddhook_savegame_convert_t parm;
Str_Set(Str_InitStd(&parm.inputFilePath), NativePath(inputFilePath).expand().asText().toUtf8().constData());
Str_Set(Str_InitStd(&parm.outputFilePath), NativePath(outputFilePath).expand().asText().toUtf8().constData());

// Try to converter the savegame via each plugin in turn.
dd_bool success = DD_CallHooks(HOOK_MAP_CONVERT, 0, &parm);

Str_Free(&parm.inputFilePath);
Str_Free(&parm.inputFilePath);

if(!success)
{
/// @throw Error Seemingly no converter was able to fulfill our request.
throw Error("SavegameConverter", "Savegame file format was not recognized");
}
}

bool convertSavegame(Path inputFilePath, game::SavedSession &session)
{
DENG2_ASSERT(!inputFilePath.isEmpty());

LOG_AS("SavegameConverter");
try
{
tryConversion(inputFilePath, session.repository().folder().path() / session.fileName());
// Successful, update the relevant feed and saved session.
session.repository().folder().populate(Folder::PopulateOnlyThisFolder);
session.updateFromFile();
return true;
}
catch(Error const &er)
{
LOG_RES_WARNING("Failed conversion of \"%s\":\n")
<< NativePath(inputFilePath).pretty() << er.asText();
}
return false;
}

} // namespace de
9 changes: 8 additions & 1 deletion doomsday/libdeng2/include/de/game/savedsession.h
Expand Up @@ -123,7 +123,14 @@ class DENG2_PUBLIC SavedSession
String description() const;

/**
* Returns the saved session repository which owns the saved session (if any).
* Determines whether a repository is configured for the saved session.
*/
bool hasRepository() const;

/**
* Returns the saved session repository which owns the saved session.
*
* @see hasRepository()
*/
SavedSessionRepository &repository() const;

Expand Down
5 changes: 5 additions & 0 deletions doomsday/libdeng2/src/game/savedsession.cpp
Expand Up @@ -268,6 +268,11 @@ SavedSession &SavedSession::operator = (SavedSession const &other)
return *this;
}

bool SavedSession::hasRepository() const
{
return d->repo != 0;
}

SavedSessionRepository &SavedSession::repository() const
{
if(d->repo)
Expand Down

0 comments on commit 2e8cf9a

Please sign in to comment.