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),
96 PHYSFS_mkdir(m_addon_directory.c_str());
98 add_installed_addons();
100 // FIXME: We should also restore the order here
101 for(auto& addon : m_addon_config)
107 enable_addon(addon.id);
109 catch(const std::exception& err)
111 log_warning << "failed to enable addon from config: " << err.what() << std::endl;
117 AddonManager::~AddonManager()
119 // sync enabled/disabled addons into the config for saving
120 m_addon_config.clear();
121 for(auto& addon : m_installed_addons)
123 m_addon_config.push_back({addon->get_id(), addon->is_enabled()});
128 AddonManager::get_repository_addon(const AddonId& id)
130 auto it = std::find_if(m_repository_addons.begin(), m_repository_addons.end(),
131 [&id](const std::unique_ptr<Addon>& addon)
133 return addon->get_id() == id;
136 if (it != m_repository_addons.end())
142 throw std::runtime_error("Couldn't find repository Addon with id: " + id);
147 AddonManager::get_installed_addon(const AddonId& id)
149 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
150 [&id](const std::unique_ptr<Addon>& addon)
152 return addon->get_id() == id;
155 if (it != m_installed_addons.end())
161 throw std::runtime_error("Couldn't find installed Addon with id: " + id);
166 AddonManager::get_repository_addons() const
168 std::vector<AddonId> results;
169 results.reserve(m_repository_addons.size());
170 std::transform(m_repository_addons.begin(), m_repository_addons.end(),
171 std::back_inserter(results),
172 [](const std::unique_ptr<Addon>& addon)
174 return addon->get_id();
181 AddonManager::get_installed_addons() const
183 std::vector<AddonId> results;
184 results.reserve(m_installed_addons.size());
185 std::transform(m_installed_addons.begin(), m_installed_addons.end(),
186 std::back_inserter(results),
187 [](const std::unique_ptr<Addon>& addon)
189 return addon->get_id();
195 AddonManager::has_online_support() const
201 AddonManager::has_been_updated() const
203 return m_has_been_updated;
207 AddonManager::check_online()
209 std::string addoninfos = m_downloader.download(m_repository_url);
210 m_repository_addons = parse_addon_infos(addoninfos);
211 m_has_been_updated = true;
214 AddonManager::InstallStatusPtr
215 AddonManager::request_install_addon(const AddonId& addon_id)
217 if (m_install_status)
219 throw std::runtime_error("only one addon install request allowed at a time");
223 { // remove addon if it already exists
224 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
225 [&addon_id](const std::unique_ptr<Addon>& addon)
227 return addon->get_id() == addon_id;
229 if (it != m_installed_addons.end())
231 log_debug << "reinstalling addon " << addon_id << std::endl;
232 if ((*it)->is_enabled())
234 disable_addon((*it)->get_id());
236 m_installed_addons.erase(it);
240 log_debug << "installing addon " << addon_id << std::endl;
245 Addon& repository_addon = get_repository_addon(addon_id);
247 m_install_request = std::make_shared<InstallRequest>();
248 m_install_request->install_filename = FileSystem::join(m_addon_directory, repository_addon.get_filename());
249 m_install_request->addon_id = addon_id;
251 m_transfer_status = m_downloader.request_download(repository_addon.get_url(),
252 m_install_request->install_filename);
255 m_transfer_status->then(
258 // complete the addon install
259 Addon& repository_addon = get_repository_addon(m_install_request->addon_id);
261 MD5 md5 = md5_from_file(m_install_request->install_filename);
262 if (repository_addon.get_md5() != md5.hex_digest())
264 if (PHYSFS_delete(m_install_request->install_filename.c_str()) == 0)
266 log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl;
269 throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ");
273 const char* realdir = PHYSFS_getRealDir(m_install_request->install_filename.c_str());
276 throw std::runtime_error("PHYSFS_getRealDir failed: " + m_install_request->install_filename);
280 add_installed_archive(m_install_request->install_filename, md5.hex_digest());
284 // signal that the request is done and cleanup
285 if (m_install_status->callback)
287 m_install_status->callback();
290 m_install_request = {};
291 m_install_status = {};
292 m_transfer_status = {};
295 m_install_status = std::make_shared<InstallStatus>();
297 return m_install_status;
302 AddonManager::install_addon(const AddonId& addon_id)
304 { // remove addon if it already exists
305 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
306 [&addon_id](const std::unique_ptr<Addon>& addon)
308 return addon->get_id() == addon_id;
310 if (it != m_installed_addons.end())
312 log_debug << "reinstalling addon " << addon_id << std::endl;
313 if ((*it)->is_enabled())
315 disable_addon((*it)->get_id());
317 m_installed_addons.erase(it);
321 log_debug << "installing addon " << addon_id << std::endl;
325 Addon& repository_addon = get_repository_addon(addon_id);
327 std::string install_filename = FileSystem::join(m_addon_directory, repository_addon.get_filename());
329 m_downloader.download(repository_addon.get_url(), install_filename);
331 MD5 md5 = md5_from_file(install_filename);
332 if (repository_addon.get_md5() != md5.hex_digest())
334 if (PHYSFS_delete(install_filename.c_str()) == 0)
336 log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl;
339 throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ");
343 const char* realdir = PHYSFS_getRealDir(install_filename.c_str());
346 throw std::runtime_error("PHYSFS_getRealDir failed: " + install_filename);
350 add_installed_archive(install_filename, md5.hex_digest());
356 AddonManager::uninstall_addon(const AddonId& addon_id)
358 log_debug << "uninstalling addon " << addon_id << std::endl;
359 Addon& addon = get_installed_addon(addon_id);
360 if (addon.is_enabled())
362 disable_addon(addon_id);
364 log_debug << "deleting file \"" << addon.get_install_filename() << "\"" << std::endl;
365 PHYSFS_delete(addon.get_install_filename().c_str());
366 m_installed_addons.erase(std::remove_if(m_installed_addons.begin(), m_installed_addons.end(),
367 [&addon](const std::unique_ptr<Addon>& rhs)
369 return addon.get_id() == rhs->get_id();
371 m_installed_addons.end());
375 AddonManager::enable_addon(const AddonId& addon_id)
377 log_debug << "enabling addon " << addon_id << std::endl;
378 Addon& addon = get_installed_addon(addon_id);
379 if (addon.is_enabled())
381 log_warning << "Tried enabling already enabled Add-on" << std::endl;
385 log_debug << "Adding archive \"" << addon.get_install_filename() << "\" to search path" << std::endl;
386 //int PHYSFS_mount(addon.installed_install_filename.c_str(), "addons/", 0)
387 if (PHYSFS_addToSearchPath(addon.get_install_filename().c_str(), 0) == 0)
389 log_warning << "Could not add " << addon.get_install_filename() << " to search path: "
390 << PHYSFS_getLastError() << std::endl;
394 addon.set_enabled(true);
400 AddonManager::disable_addon(const AddonId& addon_id)
402 log_debug << "disabling addon " << addon_id << std::endl;
403 Addon& addon = get_installed_addon(addon_id);
404 if (!addon.is_enabled())
406 log_warning << "Tried disabling already disabled Add-On" << std::endl;
410 log_debug << "Removing archive \"" << addon.get_install_filename() << "\" from search path" << std::endl;
411 if (PHYSFS_removeFromSearchPath(addon.get_install_filename().c_str()) == 0)
413 log_warning << "Could not remove " << addon.get_install_filename() << " from search path: "
414 << PHYSFS_getLastError() << std::endl;
418 addon.set_enabled(false);
423 std::vector<std::string>
424 AddonManager::scan_for_archives() const
426 std::vector<std::string> archives;
428 // Search for archives and add them to the search path
429 std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
430 rc(PHYSFS_enumerateFiles(m_addon_directory.c_str()),
432 for(char** i = rc.get(); *i != 0; ++i)
434 if (has_suffix(*i, ".zip"))
436 std::string archive = FileSystem::join(m_addon_directory, *i);
437 if (PHYSFS_exists(archive.c_str()))
439 archives.push_back(archive);
448 AddonManager::scan_for_info(const std::string& archive_os_path) const
450 std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
451 rc2(PHYSFS_enumerateFiles("/"),
453 for(char** j = rc2.get(); *j != 0; ++j)
455 if (has_suffix(*j, ".nfo"))
457 std::string nfo_filename = FileSystem::join("/", *j);
459 // make sure it's in the current archive_os_path
460 const char* realdir = PHYSFS_getRealDir(nfo_filename.c_str());
463 log_warning << "PHYSFS_getRealDir() failed for " << nfo_filename << ": " << PHYSFS_getLastError() << std::endl;
467 if (realdir == archive_os_path)
475 return std::string();
479 AddonManager::add_installed_archive(const std::string& archive, const std::string& md5)
481 const char* realdir = PHYSFS_getRealDir(archive.c_str());
484 log_warning << "PHYSFS_getRealDir() failed for " << archive << ": "
485 << PHYSFS_getLastError() << std::endl;
489 std::string os_path = FileSystem::join(realdir, archive);
491 PHYSFS_addToSearchPath(os_path.c_str(), 0);
493 std::string nfo_filename = scan_for_info(os_path);
495 if (nfo_filename.empty())
497 log_warning << "Couldn't find .nfo file for " << os_path << std::endl;
503 std::unique_ptr<Addon> addon = Addon::parse(nfo_filename);
504 addon->set_install_filename(os_path, md5);
505 m_installed_addons.push_back(std::move(addon));
507 catch (const std::runtime_error& e)
509 log_warning << "Could not load add-on info for " << archive << ": " << e.what() << std::endl;
513 PHYSFS_removeFromSearchPath(os_path.c_str());
518 AddonManager::add_installed_addons()
520 auto archives = scan_for_archives();
522 for(auto archive : archives)
524 MD5 md5 = md5_from_file(archive);
525 add_installed_archive(archive, md5.hex_digest());
529 AddonManager::AddonList
530 AddonManager::parse_addon_infos(const std::string& addoninfos) const
537 std::stringstream addoninfos_stream(addoninfos);
538 const lisp::Lisp* root = parser.parse(addoninfos_stream, "supertux-addons");
539 const lisp::Lisp* addons_lisp = root->get_lisp("supertux-addons");
542 throw std::runtime_error("Downloaded file is not an Add-on list");
546 lisp::ListIterator iter(addons_lisp);
549 const std::string& token = iter.item();
550 if(token != "supertux-addoninfo")
552 log_warning << "Unknown token '" << token << "' in Add-on list" << std::endl;
556 std::unique_ptr<Addon> addon = Addon::parse(*iter.lisp());
557 m_addons.push_back(std::move(addon));
564 catch(const std::exception& e)
566 std::stringstream msg;
567 msg << "Problem when reading Add-on list: " << e.what();
568 throw std::runtime_error(msg.str());
575 AddonManager::update()
577 m_downloader.update();
579 if (m_install_status)
581 m_install_status->now = m_transfer_status->dlnow;
582 m_install_status->total = m_transfer_status->dltotal;
587 AddonManager::abort_install()
589 log_info << "addon install aborted" << std::endl;
591 m_downloader.abort(m_transfer_status->id);
593 m_install_request = {};
594 m_install_status = {};
595 m_transfer_status = {};