Skip to content

Commit

Permalink
Savegame Converter: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Apr 9, 2014
1 parent 7e863a4 commit 0799603
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions doomsday/plugins/savegameconverter/src/savegameconverter.cpp
Expand Up @@ -17,86 +17,88 @@
* 02110-1301 USA</small>
*/

#include <QCoreApplication>
#include <de/App>
#include <de/CommandLine>
#include <de/DirectoryFeed>
#include <de/Error>
#include <de/Log>
#include <de/NativeFile>
#include <de/NativePath>
#include <de/String>

#include "savegameconverter.h"

using namespace de;

static NativePath findSavegameTool()
{
#ifdef MACOSX
return App::executablePath().fileNamePath() / "../Resources/savegametool";
/// @todo fixme: Need to try alternate locations?
#elif WIN32
return App::executablePath().fileNamePath() / "savegametool.exe";
#else // UNIX
return App::executablePath().fileNamePath() / "savegametool";
/// @todo fixme: Need to try alternate locations?
#endif

}

int SavegameConvertHook(int /*hook_type*/, int /*parm*/, void *data)
{
DENG2_ASSERT(data != 0);
ddhook_savegame_convert_t const &parm = *static_cast<ddhook_savegame_convert_t *>(data);

LOG_AS("SavegameConverter");

CommandLine cmd;

#ifdef MACOSX
// First locate the savegametool executable.
NativePath bin = NativePath(qApp->applicationDirPath()) / "../Resources/savegametool";
/// @todo fixme: Need to try alternate locations?
#elif WIN32
NativePath bin = NativePath(qApp->applicationDirPath()) / "savegametool.exe";
#else // UNIX
NativePath bin = NativePath(qApp->applicationDirPath()) / "savegametool";
/// @todo fixme: Need to try alternate locations?
#endif
NativePath bin = findSavegameTool();
if(!bin.exists())
{
LOG_RES_ERROR("Failed to locate Savegame Tool");
return false;
}
cmd.append(bin);
CommandLine cmd;
cmd << bin;

cmd.append("-idkey");
cmd.append(Str_Text(&parm.fallbackGameId));
// Specify the fallback game identity key for ambiguous format resolution.
cmd << "-idkey" << Str_Text(&parm.fallbackGameId);

// We can only convert native files and output to native folders using Savegame Tool.
Path const outputPath(Str_Text(&parm.outputPath));
Path const sourcePath(Str_Text(&parm.sourcePath));
try
{
cmd.append("-output");
cmd.append(DENG2_APP->rootFolder().locate<Folder>(Str_Text(&parm.outputPath))
.feeds().front()->as<DirectoryFeed>().nativePath().expand());
// Redirect output to the folder specified.
cmd << "-output" << App::rootFolder().locate<Folder>(outputPath)
.feeds().front()->as<DirectoryFeed>().nativePath().expand();

NativeFile &file = DENG2_APP->rootFolder().locate<NativeFile>(Str_Text(&parm.sourcePath));
cmd.append(file.nativePath());
// Add the path of the savegame to be converted.
cmd << App::rootFolder().locate<NativeFile>(sourcePath).nativePath();

LOG_RES_NOTE("Starting conversion of \"%s\" using Savegame Tool")
<< Path(Str_Text(&parm.sourcePath));
LOG_RES_NOTE("Starting conversion of \"%s\" using Savegame Tool") << sourcePath;
cmd.executeAndWait();

return true;
}
catch(Error const &er)
{
LOG_RES_NOTE("Failed conversion of \"%s\":\n")
<< Path(Str_Text(&parm.sourcePath)) << er.asText();
LOG_RES_NOTE("Failed conversion of \"%s\":\n") << sourcePath << er.asText();
}

return false;
}

/**
* This function is called automatically when the plugin is loaded.
* We let the engine know what we'd like to do.
* This function is called automatically when the plugin is loaded. We let the engine know
* what we'd like to do.
*/
void DP_Initialize()
{
Plug_AddHook(HOOK_SAVEGAME_CONVERT, SavegameConvertHook);
}

/**
* Declares the type of the plugin so the engine knows how to treat it. Called
* automatically when the plugin is loaded.
* Declares the type of the plugin so the engine knows how to treat it. Called automatically
* when the plugin is loaded.
*/
extern "C" char const *deng_LibraryType()
{
Expand Down

0 comments on commit 0799603

Please sign in to comment.