1 // SuperTux - Add-on Manager
2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 // 2014 Ingo Ruhnke <grumbel@gmail.com>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "addon/addon_manager.hpp"
32 #include "addon/addon.hpp"
33 #include "addon/md5.hpp"
34 #include "lisp/list_iterator.hpp"
35 #include "lisp/parser.hpp"
36 #include "util/file_system.hpp"
37 #include "util/log.hpp"
38 #include "util/reader.hpp"
39 #include "util/writer.hpp"
43 MD5 md5_from_file(const std::string& filename)
45 // TODO: this does not work as expected for some files -- IFileStream seems to not always behave like an ifstream.
46 //IFileStream ifs(installed_physfs_filename);
47 //std::string md5 = MD5(ifs).hex_digest();
51 unsigned char buffer[1024];
52 PHYSFS_file* file = PHYSFS_openRead(filename.c_str());
55 PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
57 md5.update(buffer, len);
64 bool has_suffix(const std::string& str, const std::string& suffix)
66 if (str.length() >= suffix.length())
67 return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
74 AddonManager::AddonManager(const std::string& addon_directory,
75 std::vector<Config::Addon>& addon_config) :
77 m_addon_directory(addon_directory),
78 m_repository_url("http://addons.supertux.googlecode.com/git/index-0_4_0.nfo"),
79 m_addon_config(addon_config),
81 m_repository_addons(),
82 m_has_been_updated(false),
87 PHYSFS_mkdir(m_addon_directory.c_str());
89 add_installed_addons();
91 // FIXME: We should also restore the order here
92 for(auto& addon : m_addon_config)
98 enable_addon(addon.id);
100 catch(const std::exception& err)
102 log_warning << "failed to enable addon from config: " << err.what() << std::endl;
108 AddonManager::~AddonManager()
110 // sync enabled/disabled addons into the config for saving
111 m_addon_config.clear();
112 for(auto& addon : m_installed_addons)
114 m_addon_config.push_back({addon->get_id(), addon->is_enabled()});
119 AddonManager::get_repository_addon(const AddonId& id)
121 auto it = std::find_if(m_repository_addons.begin(), m_repository_addons.end(),
122 [&id](const std::unique_ptr<Addon>& addon)
124 return addon->get_id() == id;
127 if (it != m_repository_addons.end())
133 throw std::runtime_error("Couldn't find repository Addon with id: " + id);
138 AddonManager::get_installed_addon(const AddonId& id)
140 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
141 [&id](const std::unique_ptr<Addon>& addon)
143 return addon->get_id() == id;
146 if (it != m_installed_addons.end())
152 throw std::runtime_error("Couldn't find installed Addon with id: " + id);
157 AddonManager::get_repository_addons() const
159 std::vector<AddonId> results;
160 results.reserve(m_repository_addons.size());
161 std::transform(m_repository_addons.begin(), m_repository_addons.end(),
162 std::back_inserter(results),
163 [](const std::unique_ptr<Addon>& addon)
165 return addon->get_id();
172 AddonManager::get_installed_addons() const
174 std::vector<AddonId> results;
175 results.reserve(m_installed_addons.size());
176 std::transform(m_installed_addons.begin(), m_installed_addons.end(),
177 std::back_inserter(results),
178 [](const std::unique_ptr<Addon>& addon)
180 return addon->get_id();
186 AddonManager::has_online_support() const
192 AddonManager::has_been_updated() const
194 return m_has_been_updated;
198 AddonManager::check_online()
200 std::string addoninfos = m_downloader.download(m_repository_url);
201 m_repository_addons = parse_addon_infos(addoninfos);
202 m_has_been_updated = true;
205 AddonManager::InstallStatusPtr
206 AddonManager::request_install_addon(const AddonId& addon_id)
208 if (m_install_status)
210 throw std::runtime_error("only one addon install request allowed at a time");
214 { // remove addon if it already exists
215 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
216 [&addon_id](const std::unique_ptr<Addon>& addon)
218 return addon->get_id() == addon_id;
220 if (it != m_installed_addons.end())
222 log_debug << "reinstalling addon " << addon_id << std::endl;
223 if ((*it)->is_enabled())
225 disable_addon((*it)->get_id());
227 m_installed_addons.erase(it);
231 log_debug << "installing addon " << addon_id << std::endl;
235 Addon& repository_addon = get_repository_addon(addon_id);
237 m_install_request = std::make_shared<InstallRequest>();
238 m_install_request->install_filename = FileSystem::join(m_addon_directory, repository_addon.get_filename());
239 m_install_request->addon_id = addon_id;
241 m_transfer_status = m_downloader.request_download(repository_addon.get_url(),
242 m_install_request->install_filename);
244 m_install_status = std::make_shared<InstallStatus>();
246 return m_install_status;
251 AddonManager::install_addon(const AddonId& addon_id)
253 { // remove addon if it already exists
254 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
255 [&addon_id](const std::unique_ptr<Addon>& addon)
257 return addon->get_id() == addon_id;
259 if (it != m_installed_addons.end())
261 log_debug << "reinstalling addon " << addon_id << std::endl;
262 if ((*it)->is_enabled())
264 disable_addon((*it)->get_id());
266 m_installed_addons.erase(it);
270 log_debug << "installing addon " << addon_id << std::endl;
274 Addon& repository_addon = get_repository_addon(addon_id);
276 std::string install_filename = FileSystem::join(m_addon_directory, repository_addon.get_filename());
278 m_downloader.download(repository_addon.get_url(), install_filename);
280 MD5 md5 = md5_from_file(install_filename);
281 if (repository_addon.get_md5() != md5.hex_digest())
283 if (PHYSFS_delete(install_filename.c_str()) == 0)
285 log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl;
288 throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ");
292 const char* realdir = PHYSFS_getRealDir(install_filename.c_str());
295 throw std::runtime_error("PHYSFS_getRealDir failed: " + install_filename);
299 add_installed_archive(install_filename, md5.hex_digest());
305 AddonManager::uninstall_addon(const AddonId& addon_id)
307 log_debug << "uninstalling addon " << addon_id << std::endl;
308 Addon& addon = get_installed_addon(addon_id);
309 if (addon.is_enabled())
311 disable_addon(addon_id);
313 log_debug << "deleting file \"" << addon.get_install_filename() << "\"" << std::endl;
314 PHYSFS_delete(addon.get_install_filename().c_str());
315 m_installed_addons.erase(std::remove_if(m_installed_addons.begin(), m_installed_addons.end(),
316 [&addon](const std::unique_ptr<Addon>& rhs)
318 return addon.get_id() == rhs->get_id();
320 m_installed_addons.end());
324 AddonManager::enable_addon(const AddonId& addon_id)
326 log_debug << "enabling addon " << addon_id << std::endl;
327 Addon& addon = get_installed_addon(addon_id);
328 if (addon.is_enabled())
330 log_warning << "Tried enabling already enabled Add-on" << std::endl;
334 log_debug << "Adding archive \"" << addon.get_install_filename() << "\" to search path" << std::endl;
335 //int PHYSFS_mount(addon.installed_install_filename.c_str(), "addons/", 0)
336 if (PHYSFS_addToSearchPath(addon.get_install_filename().c_str(), 0) == 0)
338 log_warning << "Could not add " << addon.get_install_filename() << " to search path: "
339 << PHYSFS_getLastError() << std::endl;
343 addon.set_enabled(true);
349 AddonManager::disable_addon(const AddonId& addon_id)
351 log_debug << "disabling addon " << addon_id << std::endl;
352 Addon& addon = get_installed_addon(addon_id);
353 if (!addon.is_enabled())
355 log_warning << "Tried disabling already disabled Add-On" << std::endl;
359 log_debug << "Removing archive \"" << addon.get_install_filename() << "\" from search path" << std::endl;
360 if (PHYSFS_removeFromSearchPath(addon.get_install_filename().c_str()) == 0)
362 log_warning << "Could not remove " << addon.get_install_filename() << " from search path: "
363 << PHYSFS_getLastError() << std::endl;
367 addon.set_enabled(false);
372 std::vector<std::string>
373 AddonManager::scan_for_archives() const
375 std::vector<std::string> archives;
377 // Search for archives and add them to the search path
378 std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
379 rc(PHYSFS_enumerateFiles(m_addon_directory.c_str()),
381 for(char** i = rc.get(); *i != 0; ++i)
383 if (has_suffix(*i, ".zip"))
385 std::string archive = FileSystem::join(m_addon_directory, *i);
386 if (PHYSFS_exists(archive.c_str()))
388 archives.push_back(archive);
397 AddonManager::scan_for_info(const std::string& archive_os_path) const
399 std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
400 rc2(PHYSFS_enumerateFiles("/"),
402 for(char** j = rc2.get(); *j != 0; ++j)
404 if (has_suffix(*j, ".nfo"))
406 std::string nfo_filename = FileSystem::join("/", *j);
408 // make sure it's in the current archive_os_path
409 const char* realdir = PHYSFS_getRealDir(nfo_filename.c_str());
412 log_warning << "PHYSFS_getRealDir() failed for " << nfo_filename << ": " << PHYSFS_getLastError() << std::endl;
416 if (realdir == archive_os_path)
424 return std::string();
428 AddonManager::add_installed_archive(const std::string& archive, const std::string& md5)
430 const char* realdir = PHYSFS_getRealDir(archive.c_str());
433 log_warning << "PHYSFS_getRealDir() failed for " << archive << ": "
434 << PHYSFS_getLastError() << std::endl;
438 std::string os_path = FileSystem::join(realdir, archive);
440 PHYSFS_addToSearchPath(os_path.c_str(), 0);
442 std::string nfo_filename = scan_for_info(os_path);
444 if (nfo_filename.empty())
446 log_warning << "Couldn't find .nfo file for " << os_path << std::endl;
452 std::unique_ptr<Addon> addon = Addon::parse(nfo_filename);
453 addon->set_install_filename(os_path, md5);
454 m_installed_addons.push_back(std::move(addon));
456 catch (const std::runtime_error& e)
458 log_warning << "Could not load add-on info for " << archive << ": " << e.what() << std::endl;
462 PHYSFS_removeFromSearchPath(os_path.c_str());
467 AddonManager::add_installed_addons()
469 auto archives = scan_for_archives();
471 for(auto archive : archives)
473 MD5 md5 = md5_from_file(archive);
474 add_installed_archive(archive, md5.hex_digest());
478 AddonManager::AddonList
479 AddonManager::parse_addon_infos(const std::string& addoninfos) const
486 std::stringstream addoninfos_stream(addoninfos);
487 const lisp::Lisp* root = parser.parse(addoninfos_stream, "supertux-addons");
488 const lisp::Lisp* addons_lisp = root->get_lisp("supertux-addons");
491 throw std::runtime_error("Downloaded file is not an Add-on list");
495 lisp::ListIterator iter(addons_lisp);
498 const std::string& token = iter.item();
499 if(token != "supertux-addoninfo")
501 log_warning << "Unknown token '" << token << "' in Add-on list" << std::endl;
505 std::unique_ptr<Addon> addon = Addon::parse(*iter.lisp());
506 m_addons.push_back(std::move(addon));
513 catch(const std::exception& e)
515 std::stringstream msg;
516 msg << "Problem when reading Add-on list: " << e.what();
517 throw std::runtime_error(msg.str());
524 AddonManager::update()
526 m_downloader.update();
528 if (m_install_status)
530 m_install_status->now = m_transfer_status->dlnow;
531 m_install_status->total = m_transfer_status->dltotal;
533 if (m_transfer_status->status != TransferStatus::RUNNING)
535 if (m_transfer_status->status != TransferStatus::COMPLETED)
537 log_warning << "Some error" << std::endl;
541 Addon& repository_addon = get_repository_addon(m_install_request->addon_id);
543 MD5 md5 = md5_from_file(m_install_request->install_filename);
544 if (repository_addon.get_md5() != md5.hex_digest())
546 if (PHYSFS_delete(m_install_request->install_filename.c_str()) == 0)
548 log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl;
551 throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ");
555 const char* realdir = PHYSFS_getRealDir(m_install_request->install_filename.c_str());
558 throw std::runtime_error("PHYSFS_getRealDir failed: " + m_install_request->install_filename);
562 add_installed_archive(m_install_request->install_filename, md5.hex_digest());
567 m_install_request = {};
568 m_install_status = {};
569 m_transfer_status = {};