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;
64 AddonManager::AddonManager() :
66 ignored_addon_filenames()
69 curl_global_init(CURL_GLOBAL_ALL);
73 AddonManager::~AddonManager()
76 curl_global_cleanup();
79 for (std::vector<Addon*>::iterator i = addons.begin(); i != addons.end(); i++) delete *i;
83 AddonManager::get_addons()
86 for (std::vector<Addon>::iterator it = installed_addons.begin(); it != installed_addons.end(); ++it) {
88 if (addon.md5 == "") addon.md5 = calculate_md5(addon);
95 AddonManager::check_online()
98 char error_buffer[CURL_ERROR_SIZE+1];
100 const char* baseUrl = "http://addons.supertux.googlecode.com/git/index-0_3_5.nfo";
101 std::string addoninfos = "";
104 curl_handle = curl_easy_init();
105 curl_easy_setopt(curl_handle, CURLOPT_URL, baseUrl);
106 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
107 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_string_append);
108 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &addoninfos);
109 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
110 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
111 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
112 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
113 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
114 CURLcode result = curl_easy_perform(curl_handle);
115 curl_easy_cleanup(curl_handle);
117 if (result != CURLE_OK) {
118 std::string why = error_buffer[0] ? error_buffer : "unhandled error";
119 throw std::runtime_error("Downloading Add-on list failed: " + why);
124 std::stringstream addoninfos_stream(addoninfos);
125 const lisp::Lisp* root = parser.parse(addoninfos_stream, "supertux-addons");
127 const lisp::Lisp* addons_lisp = root->get_lisp("supertux-addons");
128 if(!addons_lisp) throw std::runtime_error("Downloaded file is not an Add-on list");
130 lisp::ListIterator iter(addons_lisp);
133 const std::string& token = iter.item();
134 if(token != "supertux-addoninfo")
136 log_warning << "Unknown token '" << token << "' in Add-on list" << std::endl;
139 std::unique_ptr<Addon> addon(new Addon());
140 addon->parse(*(iter.lisp()));
141 addon->installed = false;
142 addon->loaded = false;
144 // make sure the list of known Add-ons does not already contain this one
146 for (std::vector<Addon*>::const_iterator i = addons.begin(); i != addons.end(); i++) {
157 else if (addon->suggested_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos)
159 // make sure the Add-on's file name does not contain weird characters
160 log_warning << "Add-on \"" << addon->title << "\" contains unsafe file name. Skipping." << std::endl;
164 addons.push_back(addon.release());
167 } catch(std::exception& e) {
168 std::stringstream msg;
169 msg << "Problem when reading Add-on list: " << e.what();
170 throw std::runtime_error(msg.str());
177 AddonManager::install(Addon* addon)
181 if (addon->installed) throw std::runtime_error("Tried installing installed Add-on");
183 // make sure the Add-on's file name does not contain weird characters
184 if (addon->suggested_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
185 throw std::runtime_error("Add-on has unsafe file name (\""+addon->suggested_filename+"\")");
188 std::string fileName = addon->suggested_filename;
190 // make sure its file doesn't already exist
191 if (PHYSFS_exists(fileName.c_str())) {
192 fileName = addon->stored_md5 + "_" + addon->suggested_filename;
193 if (PHYSFS_exists(fileName.c_str())) {
194 throw std::runtime_error("Add-on of suggested filename already exists (\""+addon->suggested_filename+"\", \""+fileName+"\")");
198 char error_buffer[CURL_ERROR_SIZE+1];
200 char* url = (char*)malloc(addon->http_url.length() + 1);
201 strncpy(url, addon->http_url.c_str(), addon->http_url.length() + 1);
203 PHYSFS_file* f = PHYSFS_openWrite(fileName.c_str());
205 log_debug << "Downloading \"" << url << "\"" << std::endl;
208 curl_handle = curl_easy_init();
209 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
210 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
211 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_physfs_write);
212 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, f);
213 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
214 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
215 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
216 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
217 CURLcode result = curl_easy_perform(curl_handle);
218 curl_easy_cleanup(curl_handle);
224 if (result != CURLE_OK) {
225 PHYSFS_delete(fileName.c_str());
226 std::string why = error_buffer[0] ? error_buffer : "unhandled error";
227 throw std::runtime_error("Downloading Add-on failed: " + why);
230 addon->installed = true;
231 addon->installed_physfs_filename = fileName;
232 static const std::string writeDir = PHYSFS_getWriteDir();
233 static const std::string dirSep = PHYSFS_getDirSeparator();
234 addon->installed_absolute_filename = writeDir + dirSep + fileName;
235 addon->loaded = false;
237 if (addon->get_md5() != addon->stored_md5) {
238 addon->installed = false;
239 PHYSFS_delete(fileName.c_str());
240 std::string why = "MD5 checksums differ";
241 throw std::runtime_error("Downloading Add-on failed: " + why);
244 log_debug << "Finished downloading \"" << addon->installed_absolute_filename << "\". Enabling Add-on." << std::endl;
255 AddonManager::remove(Addon* addon)
257 if (!addon->installed) throw std::runtime_error("Tried removing non-installed Add-on");
261 // make sure the Add-on's file name does not contain weird characters
262 if (addon->installed_physfs_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
263 throw std::runtime_error("Add-on has unsafe file name (\""+addon->installed_physfs_filename+"\")");
268 log_debug << "deleting file \"" << addon->installed_absolute_filename << "\"" << std::endl;
269 PHYSFS_delete(addon->installed_absolute_filename.c_str());
270 addon->installed = false;
272 // FIXME: As we don't know anything more about it (e.g. where to get it), remove it from list of known Add-ons
276 AddonManager::disable(Addon* addon)
280 std::string fileName = addon->installed_physfs_filename;
281 if (std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName) == ignored_addon_filenames.end()) {
282 ignored_addon_filenames.push_back(fileName);
287 AddonManager::enable(Addon* addon)
291 std::string fileName = addon->installed_physfs_filename;
292 std::vector<std::string>::iterator i = std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName);
293 if (i != ignored_addon_filenames.end()) {
294 ignored_addon_filenames.erase(i);
299 AddonManager::unload(Addon* addon)
301 if (!addon->installed) throw std::runtime_error("Tried unloading non-installed Add-on");
302 if (!addon->loaded) return;
304 log_debug << "Removing archive \"" << addon->installed_absolute_filename << "\" from search path" << std::endl;
305 if (PHYSFS_removeFromSearchPath(addon->installed_absolute_filename.c_str()) == 0) {
306 log_warning << "Could not remove " << addon->installed_absolute_filename << " from search path. Ignoring." << std::endl;
310 addon->loaded = false;
314 AddonManager::load(Addon* addon)
316 if (!addon->installed) throw std::runtime_error("Tried loading non-installed Add-on");
317 if (addon->loaded) return;
319 log_debug << "Adding archive \"" << addon->installed_absolute_filename << "\" to search path" << std::endl;
320 if (PHYSFS_addToSearchPath(addon->installed_absolute_filename.c_str(), 0) == 0) {
321 log_warning << "Could not add " << addon->installed_absolute_filename << " to search path. Ignoring." << std::endl;
325 addon->loaded = true;
329 AddonManager::load_addons()
331 // unload all Addons and forget about them
332 for (std::vector<Addon*>::iterator i = addons.begin(); i != addons.end(); i++) {
333 if ((*i)->installed && (*i)->loaded) unload(*i);
338 // Search for archives and add them to the search path
339 char** rc = PHYSFS_enumerateFiles("/");
341 for(char** i = rc; *i != 0; ++i) {
343 // get filename of potential archive
344 std::string fileName = *i;
346 const std::string archiveDir = PHYSFS_getRealDir(fileName.c_str());
347 static const std::string dirSep = PHYSFS_getDirSeparator();
348 std::string fullFilename = archiveDir + dirSep + fileName;
351 // make sure it's in the writeDir
352 static const std::string writeDir = PHYSFS_getWriteDir();
353 if (fileName.compare(0, writeDir.length(), writeDir) != 0) continue;
356 // make sure it looks like an archive
357 static const std::string archiveExt = ".zip";
358 if (fullFilename.compare(fullFilename.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
360 // make sure it exists
362 if (stat(fullFilename.c_str(), &stats) != 0) continue;
364 // make sure it's an actual file
365 if (!S_ISREG(stats.st_mode)) continue;
367 log_debug << "Found archive \"" << fullFilename << "\"" << std::endl;
369 // add archive to search path
370 PHYSFS_addToSearchPath(fullFilename.c_str(), 0);
372 // Search for infoFiles
373 std::string infoFileName = "";
374 char** rc2 = PHYSFS_enumerateFiles("/");
375 for(char** j = rc2; *j != 0; ++j) {
377 // get filename of potential infoFile
378 std::string potentialInfoFileName = *j;
380 // make sure it looks like an infoFile
381 static const std::string infoExt = ".nfo";
382 if (potentialInfoFileName.length() <= infoExt.length())
385 if (potentialInfoFileName.compare(potentialInfoFileName.length()-infoExt.length(), infoExt.length(), infoExt) != 0)
388 // make sure it's in the current archive
389 std::string infoFileDir = PHYSFS_getRealDir(potentialInfoFileName.c_str());
390 if (infoFileDir != fullFilename) continue;
392 // found infoFileName
393 infoFileName = potentialInfoFileName;
396 PHYSFS_freeList(rc2);
398 // if we have an infoFile, it's an Addon
399 if (infoFileName != "") {
401 Addon* addon = new Addon();
402 addon->parse(infoFileName);
403 addon->installed = true;
404 addon->installed_physfs_filename = fileName;
405 addon->installed_absolute_filename = fullFilename;
406 addon->loaded = true;
407 addons.push_back(addon);
409 // check if the Addon is disabled
410 if (std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName) != ignored_addon_filenames.end()) {
414 } catch (const std::runtime_error& e) {
415 log_warning << "Could not load add-on info for " << fullFilename << ", loading as unmanaged:" << e.what() << std::endl;
425 AddonManager::read(const Reader& lisp)
427 lisp.get("disabled-addons", ignored_addon_filenames);
431 AddonManager::write(lisp::Writer& writer)
433 writer.write("disabled-addons", ignored_addon_filenames);