diff --git a/backends/dlc/scummvmcloud.cpp b/backends/dlc/scummvmcloud.cpp index a74760cb2232..f46f4dad73da 100644 --- a/backends/dlc/scummvmcloud.cpp +++ b/backends/dlc/scummvmcloud.cpp @@ -19,6 +19,15 @@ * */ +#if defined(POSIX) +#define FORBIDDEN_SYMBOL_EXCEPTION_unlink + +#include +#endif + +#include "common/archive.h" +#include "common/compression/unzip.h" +#include "common/file.h" #include "backends/dlc/scummvmcloud.h" #include "backends/dlc/dlcmanager.h" #include "common/config-manager.h" @@ -71,6 +80,12 @@ void ScummVMCloud::downloadFileCallback(Networking::DataResponse r) { Networking::SessionFileResponse *response = static_cast(r.value); if (response->eos) { warning("downloaded"); + Common::Path relativeFilePath = Common::Path(DLCMan._queuedDownloadTasks.front()->id); + // extract the downloaded zip + extractZip(relativeFilePath); + // remove cache (the downloaded .zip) + removeCacheFile(relativeFilePath); + // handle next download DLCMan._queuedDownloadTasks.front()->state = DLCDesc::kDownloaded; DLCMan._queuedDownloadTasks.pop(); DLCMan.processDownloadQueue(); @@ -95,5 +110,28 @@ void ScummVMCloud::startDownloadAsync(const Common::String &id, const Common::St rq->start(); } +void ScummVMCloud::extractZip(Common::Path file) { + Common::Archive *dataArchive = nullptr; + Common::Path dlcPath = Common::Path(ConfMan.get("iconspath")); + Common::FSNode *fs = new Common::FSNode(dlcPath.join(file)); + if (fs->exists()) { + dataArchive = Common::makeZipArchive(*fs); + // dataArchive is nullptr if zip file is incomplete + if (dataArchive != nullptr) { + dataArchive->dumpArchive(dlcPath.toString()); + } + } + delete fs; + delete dataArchive; +} + +void ScummVMCloud::removeCacheFile(Common::Path file) { + Common::Path dlcPath = Common::Path(ConfMan.get("iconspath")); + Common::Path fileToDelete = dlcPath.join(file); + #if defined(POSIX) + unlink(fileToDelete.toString().c_str()); + #endif +} + } // End of namespace ScummVMCloud } // End of namespace DLC diff --git a/backends/dlc/scummvmcloud.h b/backends/dlc/scummvmcloud.h index 7cf6b33b42d2..d248671d6ede 100644 --- a/backends/dlc/scummvmcloud.h +++ b/backends/dlc/scummvmcloud.h @@ -56,6 +56,10 @@ Networking::Session session; virtual void startDownloadAsync(const Common::String &id, const Common::String &url) override; + virtual void removeCacheFile(Common::Path file) override; + + void extractZip(Common::Path file); + // callback functions void jsonCallbackGetAllDLCs(Networking::JsonResponse response); diff --git a/backends/dlc/store.h b/backends/dlc/store.h index def0b03ae7a4..d0d35ae3b0d0 100644 --- a/backends/dlc/store.h +++ b/backends/dlc/store.h @@ -25,6 +25,7 @@ #include "common/str.h" #include "common/array.h" #include "common/queue.h" +#include "common/path.h" #include "backends/dlc/dlcdesc.h" namespace DLC { @@ -51,6 +52,7 @@ class Store { virtual void startDownloadAsync(const Common::String &id, const Common::String &url) = 0; + virtual void removeCacheFile(Common::Path file) = 0; }; } // End of namespace DLC