From: Ingo Ruhnke Date: Tue, 26 Aug 2014 07:06:41 +0000 (+0200) Subject: Added then-callback to AddonManager and use the then-callback of Downloader X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=317e547a8d73e7f0ae6fbd6522381cc97e141a2e;p=supertux.git Added then-callback to AddonManager and use the then-callback of Downloader --- diff --git a/src/addon/addon_manager.cpp b/src/addon/addon_manager.cpp index 62e8a7c44..c20764f36 100644 --- a/src/addon/addon_manager.cpp +++ b/src/addon/addon_manager.cpp @@ -50,15 +50,24 @@ MD5 md5_from_file(const std::string& filename) unsigned char buffer[1024]; PHYSFS_file* file = PHYSFS_openRead(filename.c_str()); - while (true) + if (!file) { - PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer)); - if (len <= 0) break; - md5.update(buffer, len); + std::ostringstream out; + out << "PHYSFS_openRead() failed: " << PHYSFS_getLastError(); + throw std::runtime_error(out.str()); } - PHYSFS_close(file); + else + { + while (true) + { + PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer)); + if (len <= 0) break; + md5.update(buffer, len); + } + PHYSFS_close(file); - return md5; + return md5; + } } bool has_suffix(const std::string& str, const std::string& suffix) @@ -232,14 +241,56 @@ AddonManager::request_install_addon(const AddonId& addon_id) } } - Addon& repository_addon = get_repository_addon(addon_id); + { + Addon& repository_addon = get_repository_addon(addon_id); + + m_install_request = std::make_shared(); + m_install_request->install_filename = FileSystem::join(m_addon_directory, repository_addon.get_filename()); + m_install_request->addon_id = addon_id; + + m_transfer_status = m_downloader.request_download(repository_addon.get_url(), + m_install_request->install_filename); + } + + m_transfer_status->then( + [this] + { + // complete the addon install + Addon& repository_addon = get_repository_addon(m_install_request->addon_id); + + MD5 md5 = md5_from_file(m_install_request->install_filename); + if (repository_addon.get_md5() != md5.hex_digest()) + { + if (PHYSFS_delete(m_install_request->install_filename.c_str()) == 0) + { + log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl; + } + + throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ"); + } + else + { + const char* realdir = PHYSFS_getRealDir(m_install_request->install_filename.c_str()); + if (!realdir) + { + throw std::runtime_error("PHYSFS_getRealDir failed: " + m_install_request->install_filename); + } + else + { + add_installed_archive(m_install_request->install_filename, md5.hex_digest()); + } + } - m_install_request = std::make_shared(); - m_install_request->install_filename = FileSystem::join(m_addon_directory, repository_addon.get_filename()); - m_install_request->addon_id = addon_id; + // signal that the request is done and cleanup + if (m_install_status->callback) + { + m_install_status->callback(); + } - m_transfer_status = m_downloader.request_download(repository_addon.get_url(), - m_install_request->install_filename); + m_install_request = {}; + m_install_status = {}; + m_transfer_status = {}; + }); m_install_status = std::make_shared(); @@ -529,45 +580,6 @@ AddonManager::update() { m_install_status->now = m_transfer_status->dlnow; m_install_status->total = m_transfer_status->dltotal; - - if (m_transfer_status->status != TransferStatus::RUNNING) - { - if (m_transfer_status->status != TransferStatus::COMPLETED) - { - log_warning << "Some error" << std::endl; - } - else - { - Addon& repository_addon = get_repository_addon(m_install_request->addon_id); - - MD5 md5 = md5_from_file(m_install_request->install_filename); - if (repository_addon.get_md5() != md5.hex_digest()) - { - if (PHYSFS_delete(m_install_request->install_filename.c_str()) == 0) - { - log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl; - } - - throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ"); - } - else - { - const char* realdir = PHYSFS_getRealDir(m_install_request->install_filename.c_str()); - if (!realdir) - { - throw std::runtime_error("PHYSFS_getRealDir failed: " + m_install_request->install_filename); - } - else - { - add_installed_archive(m_install_request->install_filename, md5.hex_digest()); - } - } - } - - m_install_request = {}; - m_install_status = {}; - m_transfer_status = {}; - } } } diff --git a/src/addon/addon_manager.hpp b/src/addon/addon_manager.hpp index a58f38cee..cd23e619e 100644 --- a/src/addon/addon_manager.hpp +++ b/src/addon/addon_manager.hpp @@ -18,6 +18,7 @@ #ifndef HEADER_SUPERTUX_ADDON_ADDON_MANAGER_HPP #define HEADER_SUPERTUX_ADDON_ADDON_MANAGER_HPP +#include #include #include #include @@ -42,12 +43,19 @@ public: InstallStatus() : now(0), total(0), - done(false) + done(false), + callback() {} int now; int total; bool done; + std::function callback; + + void then(const std::function& callback_) + { + callback = callback_; + } }; struct InstallRequest