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_3_5.nfo"),
79 m_repository_url("http://localhost:8000/index-0_4_0.nfo"),
80 m_addon_config(addon_config),
82 m_repository_addons(),
83 m_has_been_updated(false)
85 PHYSFS_mkdir(m_addon_directory.c_str());
87 add_installed_addons();
89 // FIXME: We should also restore the order here
90 for(auto& addon : m_addon_config)
94 enable_addon(addon.id);
99 AddonManager::~AddonManager()
101 // sync enabled/disabled addons into the config for saving
102 m_addon_config.clear();
103 for(auto& addon : m_installed_addons)
105 m_addon_config.push_back({addon->get_id(), addon->is_enabled()});
110 AddonManager::get_repository_addon(const AddonId& id)
112 auto it = std::find_if(m_repository_addons.begin(), m_repository_addons.end(),
113 [&id](const std::unique_ptr<Addon>& addon)
115 return addon->get_id() == id;
118 if (it != m_repository_addons.end())
124 throw std::runtime_error("Couldn't find repository Addon with id: " + id);
129 AddonManager::get_installed_addon(const AddonId& id)
131 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
132 [&id](const std::unique_ptr<Addon>& addon)
134 return addon->get_id() == id;
137 if (it != m_installed_addons.end())
143 throw std::runtime_error("Couldn't find installed Addon with id: " + id);
148 AddonManager::get_repository_addons() const
150 std::vector<AddonId> results;
151 results.reserve(m_repository_addons.size());
152 std::transform(m_repository_addons.begin(), m_repository_addons.end(),
153 std::back_inserter(results),
154 [](const std::unique_ptr<Addon>& addon)
156 return addon->get_id();
163 AddonManager::get_installed_addons() const
165 std::vector<AddonId> results;
166 results.reserve(m_installed_addons.size());
167 std::transform(m_installed_addons.begin(), m_installed_addons.end(),
168 std::back_inserter(results),
169 [](const std::unique_ptr<Addon>& addon)
171 return addon->get_id();
177 AddonManager::has_online_support() const
183 AddonManager::has_been_updated() const
185 return m_has_been_updated;
189 AddonManager::check_online()
191 std::string addoninfos = m_downloader.download(m_repository_url);
192 m_repository_addons = parse_addon_infos(addoninfos);
193 m_has_been_updated = true;
197 AddonManager::install_addon(const AddonId& addon_id)
199 { // remove addon if it already exists
200 auto it = std::find_if(m_installed_addons.begin(), m_installed_addons.end(),
201 [&addon_id](const std::unique_ptr<Addon>& addon)
203 return addon->get_id() == addon_id;
205 if (it != m_installed_addons.end())
207 log_debug << "reinstalling addon " << addon_id << std::endl;
208 if ((*it)->is_enabled())
210 disable_addon((*it)->get_id());
212 m_installed_addons.erase(it);
216 log_debug << "installing addon " << addon_id << std::endl;
220 Addon& repository_addon = get_repository_addon(addon_id);
222 std::string install_filename = FileSystem::join(m_addon_directory, repository_addon.get_filename());
224 m_downloader.download(repository_addon.get_http_url(), install_filename);
226 MD5 md5 = md5_from_file(install_filename);
227 if (repository_addon.get_md5() != md5.hex_digest())
229 if (PHYSFS_delete(install_filename.c_str()) == 0)
231 log_warning << "PHYSFS_delete failed: " << PHYSFS_getLastError() << std::endl;
234 throw std::runtime_error("Downloading Add-on failed: MD5 checksums differ");
238 const char* realdir = PHYSFS_getRealDir(install_filename.c_str());
241 throw std::runtime_error("PHYSFS_getRealDir failed: " + install_filename);
245 add_installed_archive(install_filename, md5.hex_digest());
251 AddonManager::uninstall_addon(const AddonId& addon_id)
253 log_debug << "uninstalling addon " << addon_id << std::endl;
254 Addon& addon = get_installed_addon(addon_id);
255 if (addon.is_enabled())
257 disable_addon(addon_id);
259 log_debug << "deleting file \"" << addon.get_install_filename() << "\"" << std::endl;
260 PHYSFS_delete(addon.get_install_filename().c_str());
261 m_installed_addons.erase(std::remove_if(m_installed_addons.begin(), m_installed_addons.end(),
262 [&addon](const std::unique_ptr<Addon>& rhs)
264 return addon.get_id() == rhs->get_id();
266 m_installed_addons.end());
270 AddonManager::enable_addon(const AddonId& addon_id)
272 log_debug << "enabling addon " << addon_id << std::endl;
273 Addon& addon = get_installed_addon(addon_id);
274 if (addon.is_enabled())
276 log_warning << "Tried enabling already enabled Add-on" << std::endl;
280 log_debug << "Adding archive \"" << addon.get_install_filename() << "\" to search path" << std::endl;
281 //int PHYSFS_mount(addon.installed_install_filename.c_str(), "addons/", 0)
282 if (PHYSFS_addToSearchPath(addon.get_install_filename().c_str(), 0) == 0)
284 log_warning << "Could not add " << addon.get_install_filename() << " to search path: "
285 << PHYSFS_getLastError() << std::endl;
289 addon.set_enabled(true);
295 AddonManager::disable_addon(const AddonId& addon_id)
297 log_debug << "disabling addon " << addon_id << std::endl;
298 Addon& addon = get_installed_addon(addon_id);
299 if (!addon.is_enabled())
301 log_warning << "Tried disabling already disabled Add-On" << std::endl;
305 log_debug << "Removing archive \"" << addon.get_install_filename() << "\" from search path" << std::endl;
306 if (PHYSFS_removeFromSearchPath(addon.get_install_filename().c_str()) == 0)
308 log_warning << "Could not remove " << addon.get_install_filename() << " from search path: "
309 << PHYSFS_getLastError() << std::endl;
313 addon.set_enabled(false);
318 std::vector<std::string>
319 AddonManager::scan_for_archives() const
321 std::vector<std::string> archives;
323 // Search for archives and add them to the search path
324 std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
325 rc(PHYSFS_enumerateFiles(m_addon_directory.c_str()),
327 for(char** i = rc.get(); *i != 0; ++i)
329 if (has_suffix(*i, ".zip"))
331 std::string archive = FileSystem::join(m_addon_directory, *i);
332 if (PHYSFS_exists(archive.c_str()))
334 archives.push_back(archive);
343 AddonManager::scan_for_info(const std::string& archive_os_path) const
345 std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
346 rc2(PHYSFS_enumerateFiles("/"),
348 for(char** j = rc2.get(); *j != 0; ++j)
350 if (has_suffix(*j, ".nfo"))
352 std::string nfo_filename = FileSystem::join("/", *j);
354 // make sure it's in the current archive_os_path
355 const char* realdir = PHYSFS_getRealDir(nfo_filename.c_str());
358 log_warning << "PHYSFS_getRealDir() failed for " << nfo_filename << ": " << PHYSFS_getLastError() << std::endl;
362 if (realdir == archive_os_path)
370 return std::string();
374 AddonManager::add_installed_archive(const std::string& archive, const std::string& md5)
376 const char* realdir = PHYSFS_getRealDir(archive.c_str());
379 log_warning << "PHYSFS_getRealDir() failed for " << archive << ": "
380 << PHYSFS_getLastError() << std::endl;
384 std::string os_path = FileSystem::join(realdir, archive);
386 PHYSFS_addToSearchPath(os_path.c_str(), 0);
388 std::string nfo_filename = scan_for_info(os_path);
390 if (nfo_filename.empty())
392 log_warning << "Couldn't find .nfo file for " << os_path << std::endl;
398 std::unique_ptr<Addon> addon = Addon::parse(nfo_filename);
399 addon->set_install_filename(os_path, md5);
400 m_installed_addons.push_back(std::move(addon));
402 catch (const std::runtime_error& e)
404 log_warning << "Could not load add-on info for " << archive << ": " << e.what() << std::endl;
408 PHYSFS_removeFromSearchPath(os_path.c_str());
413 AddonManager::add_installed_addons()
415 auto archives = scan_for_archives();
417 for(auto archive : archives)
419 MD5 md5 = md5_from_file(archive);
420 add_installed_archive(archive, md5.hex_digest());
424 AddonManager::AddonList
425 AddonManager::parse_addon_infos(const std::string& addoninfos) const
432 std::stringstream addoninfos_stream(addoninfos);
433 const lisp::Lisp* root = parser.parse(addoninfos_stream, "supertux-addons");
434 const lisp::Lisp* addons_lisp = root->get_lisp("supertux-addons");
437 throw std::runtime_error("Downloaded file is not an Add-on list");
441 lisp::ListIterator iter(addons_lisp);
444 const std::string& token = iter.item();
445 if(token != "supertux-addoninfo")
447 log_warning << "Unknown token '" << token << "' in Add-on list" << std::endl;
451 std::unique_ptr<Addon> addon = Addon::parse(*iter.lisp());
452 m_addons.push_back(std::move(addon));
459 catch(const std::exception& e)
461 std::stringstream msg;
462 msg << "Problem when reading Add-on list: " << e.what();
463 throw std::runtime_error(msg.str());