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 std::ostringstream out;
56 out << "PHYSFS_openRead() failed: " << PHYSFS_getLastError();
57 throw std::runtime_error(out.str());
63 PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
65 md5.update(buffer, len);
73 bool has_suffix(const std::string& str, const std::string& suffix)
75 if (str.length() >= suffix.length())
76 return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
83 AddonManager::AddonManager(const std::string& addon_directory,
84 std::vector<Config::Addon>& addon_config) :
86 m_addon_directory(addon_directory),
87 m_repository_url("http://addons.supertux.googlecode.com/git/index-0_4_0.nfo"),
88 m_addon_config(addon_config),
90 m_repository_addons(),
91 m_has_been_updated(false),
94 PHYSFS_mkdir(m_addon_directory.c_str());
96 add_installed_addons();
98 // FIXME: We should also restore the order here
99 for(auto& addon : m_addon_config)
105 enable_addon(addon.id);
107 catch(const std::exception& err)
109 log_warning << "failed to enable addon from config: " << err.what() << std::endl;
116 m_repository_addons = parse_addon_infos("/addons/repository.nfo");
118 catch(const std::exception& err)
120 log_warning << "parsing repository.nfo failed: " << err.what() << std::endl;
124 AddonManager::~AddonManager()
126 // sync enabled/disabled addons into the config for saving
127 m_addon_config.clear();
128 for(auto& addon : m_installed_addons)
130 m_addon_config.push_back({addon->get_id(), addon->is_enabled()});
135 AddonManager::get_repository_addon(const AddonId& id)
137 auto it = std::find_if(m_repository_addons.begin(), m_repository_addons.end(),
138 [&id](const std::unique_ptr<Addon>& addon)
140 return addon->get_id() == id;
143 if (it != m_repository_addons.end())
149 throw std::runtime_error("Couldn't find repository Addon with id: " + id);
154 AddonManager::get_installed_addon(const AddonId& id)
156 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
157 [&id](const std::unique_ptr<Addon>& addon)
159 return addon->get_id() == id;
162 if (it != m_installed_addons.end())
168 throw std::runtime_error("Couldn't find installed Addon with id: " + id);
173 AddonManager::get_repository_addons() const
175 std::vector<AddonId> results;
176 results.reserve(m_repository_addons.size());
177 std::transform(m_repository_addons.begin(), m_repository_addons.end(),
178 std::back_inserter(results),
179 [](const std::unique_ptr<Addon>& addon)
181 return addon->get_id();
188 AddonManager::get_installed_addons() const
190 std::vector<AddonId> results;
191 results.reserve(m_installed_addons.size());
192 std::transform(m_installed_addons.begin(), m_installed_addons.end(),
193 std::back_inserter(results),
194 [](const std::unique_ptr<Addon>& addon)
196 return addon->get_id();
202 AddonManager::has_online_support() const
208 AddonManager::has_been_updated() const
210 return m_has_been_updated;
214 AddonManager::request_check_online()
216 if (m_transfer_status)
218 throw std::runtime_error("only async request can be made to AddonManager at a time");
222 m_transfer_status = m_downloader.request_download(m_repository_url, "/addons/repository.nfo");
224 m_transfer_status->then(
227 m_transfer_status = {};
231 m_repository_addons = parse_addon_infos("/addons/repository.nfo");
232 m_has_been_updated = true;
236 return m_transfer_status;
241 AddonManager::check_online()
243 m_downloader.download(m_repository_url, "/addons/repository.nfo");
244 m_repository_addons = parse_addon_infos("/addons/repository.nfo");
245 m_has_been_updated = true;
249 AddonManager::request_install_addon(const AddonId& addon_id)
251 if (m_transfer_status)
253 throw std::runtime_error("only one addon install request allowed at a time");
257 { // remove addon if it already exists
258 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
259 [&addon_id](const std::unique_ptr<Addon>& addon)
261 return addon->get_id() == addon_id;
263 if (it != m_installed_addons.end())
265 log_debug << "reinstalling addon " << addon_id << std::endl;
266 if ((*it)->is_enabled())
268 disable_addon((*it)->get_id());
270 m_installed_addons.erase(it);
274 log_debug << "installing addon " << addon_id << std::endl;
278 Addon& addon = get_repository_addon(addon_id);
280 std::string install_filename = FileSystem::join(m_addon_directory, addon.get_filename());
282 m_transfer_status = m_downloader.request_download(addon.get_url(), install_filename);
284 m_transfer_status->then(
285 [this, install_filename, addon_id](bool success)
287 m_transfer_status = {};
291 // complete the addon install
292 Addon& repository_addon = get_repository_addon(addon_id);
294 MD5 md5 = md5_from_file(install_filename);
295 if (repository_addon.get_md5() != md5.hex_digest())
297 if (PHYSFS_delete(install_filename.c_str()) == 0)
299 log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl;
302 throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ");
306 const char* realdir = PHYSFS_getRealDir(install_filename.c_str());
309 throw std::runtime_error("PHYSFS_getRealDir failed: " + install_filename);
313 add_installed_archive(install_filename, md5.hex_digest());
319 return m_transfer_status;
324 AddonManager::install_addon(const AddonId& addon_id)
326 { // remove addon if it already exists
327 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
328 [&addon_id](const std::unique_ptr<Addon>& addon)
330 return addon->get_id() == addon_id;
332 if (it != m_installed_addons.end())
334 log_debug << "reinstalling addon " << addon_id << std::endl;
335 if ((*it)->is_enabled())
337 disable_addon((*it)->get_id());
339 m_installed_addons.erase(it);
343 log_debug << "installing addon " << addon_id << std::endl;
347 Addon& repository_addon = get_repository_addon(addon_id);
349 std::string install_filename = FileSystem::join(m_addon_directory, repository_addon.get_filename());
351 m_downloader.download(repository_addon.get_url(), install_filename);
353 MD5 md5 = md5_from_file(install_filename);
354 if (repository_addon.get_md5() != md5.hex_digest())
356 if (PHYSFS_delete(install_filename.c_str()) == 0)
358 log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl;
361 throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ");
365 const char* realdir = PHYSFS_getRealDir(install_filename.c_str());
368 throw std::runtime_error("PHYSFS_getRealDir failed: " + install_filename);
372 add_installed_archive(install_filename, md5.hex_digest());
378 AddonManager::uninstall_addon(const AddonId& addon_id)
380 log_debug << "uninstalling addon " << addon_id << std::endl;
381 Addon& addon = get_installed_addon(addon_id);
382 if (addon.is_enabled())
384 disable_addon(addon_id);
386 log_debug << "deleting file \"" << addon.get_install_filename() << "\"" << std::endl;
387 PHYSFS_delete(addon.get_install_filename().c_str());
388 m_installed_addons.erase(std::remove_if(m_installed_addons.begin(), m_installed_addons.end(),
389 [&addon](const std::unique_ptr<Addon>& rhs)
391 return addon.get_id() == rhs->get_id();
393 m_installed_addons.end());
397 AddonManager::enable_addon(const AddonId& addon_id)
399 log_debug << "enabling addon " << addon_id << std::endl;
400 Addon& addon = get_installed_addon(addon_id);
401 if (addon.is_enabled())
403 log_warning << "Tried enabling already enabled Add-on" << std::endl;
407 log_debug << "Adding archive \"" << addon.get_install_filename() << "\" to search path" << std::endl;
408 //int PHYSFS_mount(addon.installed_install_filename.c_str(), "addons/", 0)
409 if (PHYSFS_addToSearchPath(addon.get_install_filename().c_str(), 0) == 0)
411 log_warning << "Could not add " << addon.get_install_filename() << " to search path: "
412 << PHYSFS_getLastError() << std::endl;
416 addon.set_enabled(true);
422 AddonManager::disable_addon(const AddonId& addon_id)
424 log_debug << "disabling addon " << addon_id << std::endl;
425 Addon& addon = get_installed_addon(addon_id);
426 if (!addon.is_enabled())
428 log_warning << "Tried disabling already disabled Add-On" << std::endl;
432 log_debug << "Removing archive \"" << addon.get_install_filename() << "\" from search path" << std::endl;
433 if (PHYSFS_removeFromSearchPath(addon.get_install_filename().c_str()) == 0)
435 log_warning << "Could not remove " << addon.get_install_filename() << " from search path: "
436 << PHYSFS_getLastError() << std::endl;
440 addon.set_enabled(false);
445 std::vector<std::string>
446 AddonManager::scan_for_archives() const
448 std::vector<std::string> archives;
450 // Search for archives and add them to the search path
451 std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
452 rc(PHYSFS_enumerateFiles(m_addon_directory.c_str()),
454 for(char** i = rc.get(); *i != 0; ++i)
456 if (has_suffix(*i, ".zip"))
458 std::string archive = FileSystem::join(m_addon_directory, *i);
459 if (PHYSFS_exists(archive.c_str()))
461 archives.push_back(archive);
470 AddonManager::scan_for_info(const std::string& archive_os_path) const
472 std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
473 rc2(PHYSFS_enumerateFiles("/"),
475 for(char** j = rc2.get(); *j != 0; ++j)
477 if (has_suffix(*j, ".nfo"))
479 std::string nfo_filename = FileSystem::join("/", *j);
481 // make sure it's in the current archive_os_path
482 const char* realdir = PHYSFS_getRealDir(nfo_filename.c_str());
485 log_warning << "PHYSFS_getRealDir() failed for " << nfo_filename << ": " << PHYSFS_getLastError() << std::endl;
489 if (realdir == archive_os_path)
497 return std::string();
501 AddonManager::add_installed_archive(const std::string& archive, const std::string& md5)
503 const char* realdir = PHYSFS_getRealDir(archive.c_str());
506 log_warning << "PHYSFS_getRealDir() failed for " << archive << ": "
507 << PHYSFS_getLastError() << std::endl;
511 std::string os_path = FileSystem::join(realdir, archive);
513 PHYSFS_addToSearchPath(os_path.c_str(), 0);
515 std::string nfo_filename = scan_for_info(os_path);
517 if (nfo_filename.empty())
519 log_warning << "Couldn't find .nfo file for " << os_path << std::endl;
525 std::unique_ptr<Addon> addon = Addon::parse(nfo_filename);
526 addon->set_install_filename(os_path, md5);
527 m_installed_addons.push_back(std::move(addon));
529 catch (const std::runtime_error& e)
531 log_warning << "Could not load add-on info for " << archive << ": " << e.what() << std::endl;
535 PHYSFS_removeFromSearchPath(os_path.c_str());
540 AddonManager::add_installed_addons()
542 auto archives = scan_for_archives();
544 for(auto archive : archives)
546 MD5 md5 = md5_from_file(archive);
547 add_installed_archive(archive, md5.hex_digest());
551 AddonManager::AddonList
552 AddonManager::parse_addon_infos(const std::string& filename) const
559 const lisp::Lisp* root = parser.parse(filename);
560 const lisp::Lisp* addons_lisp = root->get_lisp("supertux-addons");
563 throw std::runtime_error("Downloaded file is not an Add-on list");
567 lisp::ListIterator iter(addons_lisp);
570 const std::string& token = iter.item();
571 if(token != "supertux-addoninfo")
573 log_warning << "Unknown token '" << token << "' in Add-on list" << std::endl;
577 std::unique_ptr<Addon> addon = Addon::parse(*iter.lisp());
578 m_addons.push_back(std::move(addon));
585 catch(const std::exception& e)
587 std::stringstream msg;
588 msg << "Problem when reading Add-on list: " << e.what();
589 throw std::runtime_error(msg.str());
596 AddonManager::update()
598 m_downloader.update();