1 // SuperTux - Add-on Manager
2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "addon/addon_manager.hpp"
30 # include <curl/curl.h>
31 # include <curl/easy.h>
34 #include "addon/addon.hpp"
35 #include "lisp/list_iterator.hpp"
36 #include "lisp/parser.hpp"
37 #include "util/reader.hpp"
38 #include "util/writer.hpp"
39 #include "util/log.hpp"
44 size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
46 std::string& s = *static_cast<std::string*>(string_ptr);
47 std::string buf(static_cast<char*>(ptr), size * nmemb);
49 log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
53 size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
55 PHYSFS_file* f = static_cast<PHYSFS_file*>(f_p);
56 PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
57 log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
58 return size * written;
65 AddonManager::get_instance()
67 static AddonManager instance;
71 AddonManager::AddonManager() :
73 ignored_addon_filenames()
76 curl_global_init(CURL_GLOBAL_ALL);
80 AddonManager::~AddonManager()
83 curl_global_cleanup();
86 for (std::vector<Addon*>::iterator i = addons.begin(); i != addons.end(); i++) delete *i;
90 AddonManager::get_addons()
93 for (std::vector<Addon>::iterator it = installed_addons.begin(); it != installed_addons.end(); ++it) {
95 if (addon.md5 == "") addon.md5 = calculate_md5(addon);
102 AddonManager::check_online()
105 char error_buffer[CURL_ERROR_SIZE+1];
107 const char* baseUrl = "http://addons.supertux.googlecode.com/git/index-0_3_5.nfo";
108 std::string addoninfos = "";
111 curl_handle = curl_easy_init();
112 curl_easy_setopt(curl_handle, CURLOPT_URL, baseUrl);
113 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
114 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_string_append);
115 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &addoninfos);
116 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
117 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
118 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
119 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
120 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
121 CURLcode result = curl_easy_perform(curl_handle);
122 curl_easy_cleanup(curl_handle);
124 if (result != CURLE_OK) {
125 std::string why = error_buffer[0] ? error_buffer : "unhandled error";
126 throw std::runtime_error("Downloading Add-on list failed: " + why);
131 std::stringstream addoninfos_stream(addoninfos);
132 const lisp::Lisp* root = parser.parse(addoninfos_stream, "supertux-addons");
134 const lisp::Lisp* addons_lisp = root->get_lisp("supertux-addons");
135 if(!addons_lisp) throw std::runtime_error("Downloaded file is not an Add-on list");
137 lisp::ListIterator iter(addons_lisp);
140 const std::string& token = iter.item();
141 if(token != "supertux-addoninfo")
143 log_warning << "Unknown token '" << token << "' in Add-on list" << std::endl;
146 std::unique_ptr<Addon> addon(new Addon());
147 addon->parse(*(iter.lisp()));
148 addon->installed = false;
149 addon->loaded = false;
151 // make sure the list of known Add-ons does not already contain this one
153 for (std::vector<Addon*>::const_iterator i = addons.begin(); i != addons.end(); i++) {
164 else if (addon->suggested_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos)
166 // make sure the Add-on's file name does not contain weird characters
167 log_warning << "Add-on \"" << addon->title << "\" contains unsafe file name. Skipping." << std::endl;
171 addons.push_back(addon.release());
174 } catch(std::exception& e) {
175 std::stringstream msg;
176 msg << "Problem when reading Add-on list: " << e.what();
177 throw std::runtime_error(msg.str());
184 AddonManager::install(Addon* addon)
188 if (addon->installed) throw std::runtime_error("Tried installing installed Add-on");
190 // make sure the Add-on's file name does not contain weird characters
191 if (addon->suggested_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
192 throw std::runtime_error("Add-on has unsafe file name (\""+addon->suggested_filename+"\")");
195 std::string fileName = addon->suggested_filename;
197 // make sure its file doesn't already exist
198 if (PHYSFS_exists(fileName.c_str())) {
199 fileName = addon->stored_md5 + "_" + addon->suggested_filename;
200 if (PHYSFS_exists(fileName.c_str())) {
201 throw std::runtime_error("Add-on of suggested filename already exists (\""+addon->suggested_filename+"\", \""+fileName+"\")");
205 char error_buffer[CURL_ERROR_SIZE+1];
207 char* url = (char*)malloc(addon->http_url.length() + 1);
208 strncpy(url, addon->http_url.c_str(), addon->http_url.length() + 1);
210 PHYSFS_file* f = PHYSFS_openWrite(fileName.c_str());
212 log_debug << "Downloading \"" << url << "\"" << std::endl;
215 curl_handle = curl_easy_init();
216 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
217 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
218 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_physfs_write);
219 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, f);
220 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
221 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
222 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
223 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
224 CURLcode result = curl_easy_perform(curl_handle);
225 curl_easy_cleanup(curl_handle);
231 if (result != CURLE_OK) {
232 PHYSFS_delete(fileName.c_str());
233 std::string why = error_buffer[0] ? error_buffer : "unhandled error";
234 throw std::runtime_error("Downloading Add-on failed: " + why);
237 addon->installed = true;
238 addon->installed_physfs_filename = fileName;
239 static const std::string writeDir = PHYSFS_getWriteDir();
240 static const std::string dirSep = PHYSFS_getDirSeparator();
241 addon->installed_absolute_filename = writeDir + dirSep + fileName;
242 addon->loaded = false;
244 if (addon->get_md5() != addon->stored_md5) {
245 addon->installed = false;
246 PHYSFS_delete(fileName.c_str());
247 std::string why = "MD5 checksums differ";
248 throw std::runtime_error("Downloading Add-on failed: " + why);
251 log_debug << "Finished downloading \"" << addon->installed_absolute_filename << "\". Enabling Add-on." << std::endl;
262 AddonManager::remove(Addon* addon)
264 if (!addon->installed) throw std::runtime_error("Tried removing non-installed Add-on");
268 // make sure the Add-on's file name does not contain weird characters
269 if (addon->installed_physfs_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
270 throw std::runtime_error("Add-on has unsafe file name (\""+addon->installed_physfs_filename+"\")");
275 log_debug << "deleting file \"" << addon->installed_absolute_filename << "\"" << std::endl;
276 PHYSFS_delete(addon->installed_absolute_filename.c_str());
277 addon->installed = false;
279 // FIXME: As we don't know anything more about it (e.g. where to get it), remove it from list of known Add-ons
283 AddonManager::disable(Addon* addon)
287 std::string fileName = addon->installed_physfs_filename;
288 if (std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName) == ignored_addon_filenames.end()) {
289 ignored_addon_filenames.push_back(fileName);
294 AddonManager::enable(Addon* addon)
298 std::string fileName = addon->installed_physfs_filename;
299 std::vector<std::string>::iterator i = std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName);
300 if (i != ignored_addon_filenames.end()) {
301 ignored_addon_filenames.erase(i);
306 AddonManager::unload(Addon* addon)
308 if (!addon->installed) throw std::runtime_error("Tried unloading non-installed Add-on");
309 if (!addon->loaded) return;
311 log_debug << "Removing archive \"" << addon->installed_absolute_filename << "\" from search path" << std::endl;
312 if (PHYSFS_removeFromSearchPath(addon->installed_absolute_filename.c_str()) == 0) {
313 log_warning << "Could not remove " << addon->installed_absolute_filename << " from search path. Ignoring." << std::endl;
317 addon->loaded = false;
321 AddonManager::load(Addon* addon)
323 if (!addon->installed) throw std::runtime_error("Tried loading non-installed Add-on");
324 if (addon->loaded) return;
326 log_debug << "Adding archive \"" << addon->installed_absolute_filename << "\" to search path" << std::endl;
327 if (PHYSFS_addToSearchPath(addon->installed_absolute_filename.c_str(), 0) == 0) {
328 log_warning << "Could not add " << addon->installed_absolute_filename << " to search path. Ignoring." << std::endl;
332 addon->loaded = true;
336 AddonManager::load_addons()
338 // unload all Addons and forget about them
339 for (std::vector<Addon*>::iterator i = addons.begin(); i != addons.end(); i++) {
340 if ((*i)->installed && (*i)->loaded) unload(*i);
345 // Search for archives and add them to the search path
346 char** rc = PHYSFS_enumerateFiles("/");
348 for(char** i = rc; *i != 0; ++i) {
350 // get filename of potential archive
351 std::string fileName = *i;
353 const std::string archiveDir = PHYSFS_getRealDir(fileName.c_str());
354 static const std::string dirSep = PHYSFS_getDirSeparator();
355 std::string fullFilename = archiveDir + dirSep + fileName;
358 // make sure it's in the writeDir
359 static const std::string writeDir = PHYSFS_getWriteDir();
360 if (fileName.compare(0, writeDir.length(), writeDir) != 0) continue;
363 // make sure it looks like an archive
364 static const std::string archiveExt = ".zip";
365 if (fullFilename.compare(fullFilename.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
367 // make sure it exists
369 if (stat(fullFilename.c_str(), &stats) != 0) continue;
371 // make sure it's an actual file
372 if (!S_ISREG(stats.st_mode)) continue;
374 log_debug << "Found archive \"" << fullFilename << "\"" << std::endl;
376 // add archive to search path
377 PHYSFS_addToSearchPath(fullFilename.c_str(), 0);
379 // Search for infoFiles
380 std::string infoFileName = "";
381 char** rc2 = PHYSFS_enumerateFiles("/");
382 for(char** j = rc2; *j != 0; ++j) {
384 // get filename of potential infoFile
385 std::string potentialInfoFileName = *j;
387 // make sure it looks like an infoFile
388 static const std::string infoExt = ".nfo";
389 if (potentialInfoFileName.length() <= infoExt.length())
392 if (potentialInfoFileName.compare(potentialInfoFileName.length()-infoExt.length(), infoExt.length(), infoExt) != 0)
395 // make sure it's in the current archive
396 std::string infoFileDir = PHYSFS_getRealDir(potentialInfoFileName.c_str());
397 if (infoFileDir != fullFilename) continue;
399 // found infoFileName
400 infoFileName = potentialInfoFileName;
403 PHYSFS_freeList(rc2);
405 // if we have an infoFile, it's an Addon
406 if (infoFileName != "") {
408 Addon* addon = new Addon();
409 addon->parse(infoFileName);
410 addon->installed = true;
411 addon->installed_physfs_filename = fileName;
412 addon->installed_absolute_filename = fullFilename;
413 addon->loaded = true;
414 addons.push_back(addon);
416 // check if the Addon is disabled
417 if (std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName) != ignored_addon_filenames.end()) {
421 } catch (const std::runtime_error& e) {
422 log_warning << "Could not load add-on info for " << fullFilename << ", loading as unmanaged:" << e.what() << std::endl;
432 AddonManager::read(const Reader& lisp)
434 lisp.get("disabled-addons", ignored_addon_filenames);
438 AddonManager::write(lisp::Writer& writer)
440 writer.write("disabled-addons", ignored_addon_filenames);