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"
29 # include <curl/curl.h>
30 # include <curl/easy.h>
31 # include <curl/types.h>
34 #include "addon/addon.hpp"
35 #include "lisp/list_iterator.hpp"
36 #include "lisp/writer.hpp"
37 #include "lisp/parser.hpp"
38 #include "util/log.hpp"
43 size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
45 std::string& s = *static_cast<std::string*>(string_ptr);
46 std::string buf(static_cast<char*>(ptr), size * nmemb);
48 log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
52 size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
54 PHYSFS_file* f = static_cast<PHYSFS_file*>(f_p);
55 PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
56 log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
57 return size * written;
64 AddonManager::get_instance()
66 static AddonManager instance;
70 AddonManager::AddonManager() :
72 ignored_addon_filenames()
75 curl_global_init(CURL_GLOBAL_ALL);
79 AddonManager::~AddonManager()
82 curl_global_cleanup();
85 for (std::vector<Addon*>::iterator i = addons.begin(); i != addons.end(); i++) delete *i;
89 AddonManager::get_addons()
92 for (std::vector<Addon>::iterator it = installed_addons.begin(); it != installed_addons.end(); ++it) {
94 if (addon.md5 == "") addon.md5 = calculate_md5(addon);
101 AddonManager::check_online()
104 char error_buffer[CURL_ERROR_SIZE+1];
106 const char* baseUrl = "http://supertux.berlios.de/addons/index.nfo";
107 std::string addoninfos = "";
110 curl_handle = curl_easy_init();
111 curl_easy_setopt(curl_handle, CURLOPT_URL, baseUrl);
112 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
113 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_string_append);
114 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &addoninfos);
115 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
116 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
117 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
118 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
119 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
120 CURLcode result = curl_easy_perform(curl_handle);
121 curl_easy_cleanup(curl_handle);
123 if (result != CURLE_OK) {
124 std::string why = error_buffer[0] ? error_buffer : "unhandled error";
125 throw std::runtime_error("Downloading Add-on list failed: " + why);
130 std::stringstream addoninfos_stream(addoninfos);
131 const lisp::Lisp* root = parser.parse(addoninfos_stream, "supertux-addons");
133 const lisp::Lisp* addons_lisp = root->get_lisp("supertux-addons");
134 if(!addons_lisp) throw std::runtime_error("Downloaded file is not an Add-on list");
136 lisp::ListIterator iter(addons_lisp);
138 const std::string& token = iter.item();
139 if(token != "supertux-addoninfo") {
140 log_warning << "Unknown token '" << token << "' in Add-on list" << std::endl;
143 Addon* addon_ptr = new Addon();
144 Addon& addon = *addon_ptr;
145 addon.parse(*(iter.lisp()));
146 addon.installed = false;
147 addon.loaded = false;
149 // make sure the list of known Add-ons does not already contain this one
151 for (std::vector<Addon*>::const_iterator i = addons.begin(); i != addons.end(); i++) {
162 // make sure the Add-on's file name does not contain weird characters
163 if (addon.suggested_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
164 log_warning << "Add-on \"" << addon.title << "\" contains unsafe file name. Skipping." << std::endl;
169 addons.push_back(addon_ptr);
171 } catch(std::exception& e) {
172 std::stringstream msg;
173 msg << "Problem when reading Add-on list: " << e.what();
174 throw std::runtime_error(msg.str());
181 AddonManager::install(Addon* addon)
185 if (addon->installed) throw std::runtime_error("Tried installing installed Add-on");
187 // make sure the Add-on's file name does not contain weird characters
188 if (addon->suggested_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
189 throw std::runtime_error("Add-on has unsafe file name (\""+addon->suggested_filename+"\")");
192 std::string fileName = addon->suggested_filename;
194 // make sure its file doesn't already exist
195 if (PHYSFS_exists(fileName.c_str())) {
196 fileName = addon->stored_md5 + "_" + addon->suggested_filename;
197 if (PHYSFS_exists(fileName.c_str())) {
198 throw std::runtime_error("Add-on of suggested filename already exists (\""+addon->suggested_filename+"\", \""+fileName+"\")");
202 char error_buffer[CURL_ERROR_SIZE+1];
204 char* url = (char*)malloc(addon->http_url.length() + 1);
205 strncpy(url, addon->http_url.c_str(), addon->http_url.length() + 1);
207 PHYSFS_file* f = PHYSFS_openWrite(fileName.c_str());
209 log_debug << "Downloading \"" << url << "\"" << std::endl;
212 curl_handle = curl_easy_init();
213 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
214 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
215 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_physfs_write);
216 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, f);
217 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
218 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
219 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
220 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
221 CURLcode result = curl_easy_perform(curl_handle);
222 curl_easy_cleanup(curl_handle);
228 if (result != CURLE_OK) {
229 PHYSFS_delete(fileName.c_str());
230 std::string why = error_buffer[0] ? error_buffer : "unhandled error";
231 throw std::runtime_error("Downloading Add-on failed: " + why);
234 addon->installed = true;
235 addon->installed_physfs_filename = fileName;
236 static const std::string writeDir = PHYSFS_getWriteDir();
237 static const std::string dirSep = PHYSFS_getDirSeparator();
238 addon->installed_absolute_filename = writeDir + dirSep + fileName;
239 addon->loaded = false;
241 if (addon->get_md5() != addon->stored_md5) {
242 addon->installed = false;
243 PHYSFS_delete(fileName.c_str());
244 std::string why = "MD5 checksums differ";
245 throw std::runtime_error("Downloading Add-on failed: " + why);
248 log_debug << "Finished downloading \"" << addon->installed_absolute_filename << "\". Enabling Add-on." << std::endl;
259 AddonManager::remove(Addon* addon)
261 if (!addon->installed) throw std::runtime_error("Tried removing non-installed Add-on");
265 // make sure the Add-on's file name does not contain weird characters
266 if (addon->installed_physfs_filename.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
267 throw std::runtime_error("Add-on has unsafe file name (\""+addon->installed_physfs_filename+"\")");
272 log_debug << "deleting file \"" << addon->installed_absolute_filename << "\"" << std::endl;
273 PHYSFS_delete(addon->installed_absolute_filename.c_str());
274 addon->installed = false;
276 // FIXME: As we don't know anything more about it (e.g. where to get it), remove it from list of known Add-ons
280 AddonManager::disable(Addon* addon)
284 std::string fileName = addon->installed_physfs_filename;
285 if (std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName) == ignored_addon_filenames.end()) {
286 ignored_addon_filenames.push_back(fileName);
291 AddonManager::enable(Addon* addon)
295 std::string fileName = addon->installed_physfs_filename;
296 std::vector<std::string>::iterator i = std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName);
297 if (i != ignored_addon_filenames.end()) {
298 ignored_addon_filenames.erase(i);
303 AddonManager::unload(Addon* addon)
305 if (!addon->installed) throw std::runtime_error("Tried unloading non-installed Add-on");
306 if (!addon->loaded) return;
308 log_debug << "Removing archive \"" << addon->installed_absolute_filename << "\" from search path" << std::endl;
309 if (PHYSFS_removeFromSearchPath(addon->installed_absolute_filename.c_str()) == 0) {
310 log_warning << "Could not remove " << addon->installed_absolute_filename << " from search path. Ignoring." << std::endl;
314 addon->loaded = false;
318 AddonManager::load(Addon* addon)
320 if (!addon->installed) throw std::runtime_error("Tried loading non-installed Add-on");
321 if (addon->loaded) return;
323 log_debug << "Adding archive \"" << addon->installed_absolute_filename << "\" to search path" << std::endl;
324 if (PHYSFS_addToSearchPath(addon->installed_absolute_filename.c_str(), 0) == 0) {
325 log_warning << "Could not add " << addon->installed_absolute_filename << " to search path. Ignoring." << std::endl;
329 addon->loaded = true;
333 AddonManager::load_addons()
335 // unload all Addons and forget about them
336 for (std::vector<Addon*>::iterator i = addons.begin(); i != addons.end(); i++) {
337 if ((*i)->installed && (*i)->loaded) unload(*i);
342 // Search for archives and add them to the search path
343 char** rc = PHYSFS_enumerateFiles("/");
345 for(char** i = rc; *i != 0; ++i) {
347 // get filename of potential archive
348 std::string fileName = *i;
350 const std::string archiveDir = PHYSFS_getRealDir(fileName.c_str());
351 static const std::string dirSep = PHYSFS_getDirSeparator();
352 std::string fullFilename = archiveDir + dirSep + fileName;
355 // make sure it's in the writeDir
356 static const std::string writeDir = PHYSFS_getWriteDir();
357 if (fileName.compare(0, writeDir.length(), writeDir) != 0) continue;
360 // make sure it looks like an archive
361 static const std::string archiveExt = ".zip";
362 if (fullFilename.compare(fullFilename.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
364 // make sure it exists
366 if (stat(fullFilename.c_str(), &stats) != 0) continue;
368 // make sure it's an actual file
369 if (!S_ISREG(stats.st_mode)) continue;
371 log_debug << "Found archive \"" << fullFilename << "\"" << std::endl;
373 // add archive to search path
374 PHYSFS_addToSearchPath(fullFilename.c_str(), 0);
376 // Search for infoFiles
377 std::string infoFileName = "";
378 char** rc2 = PHYSFS_enumerateFiles("/");
379 for(char** i = rc2; *i != 0; ++i) {
381 // get filename of potential infoFile
382 std::string potentialInfoFileName = *i;
384 // make sure it looks like an infoFile
385 static const std::string infoExt = ".nfo";
386 if (potentialInfoFileName.length() <= infoExt.length())
389 if (potentialInfoFileName.compare(potentialInfoFileName.length()-infoExt.length(), infoExt.length(), infoExt) != 0)
392 // make sure it's in the current archive
393 std::string infoFileDir = PHYSFS_getRealDir(potentialInfoFileName.c_str());
394 if (infoFileDir != fullFilename) continue;
396 // found infoFileName
397 infoFileName = potentialInfoFileName;
400 PHYSFS_freeList(rc2);
402 // if we have an infoFile, it's an Addon
403 if (infoFileName != "") {
405 Addon* addon = new Addon();
406 addon->parse(infoFileName);
407 addon->installed = true;
408 addon->installed_physfs_filename = fileName;
409 addon->installed_absolute_filename = fullFilename;
410 addon->loaded = true;
411 addons.push_back(addon);
413 // check if the Addon is disabled
414 if (std::find(ignored_addon_filenames.begin(), ignored_addon_filenames.end(), fileName) != ignored_addon_filenames.end()) {
418 } catch (const std::runtime_error& e) {
419 log_warning << "Could not load add-on info for " << fullFilename << ", loading as unmanaged:" << e.what() << std::endl;
429 AddonManager::read(const Reader& lisp)
431 lisp.get("disabled-addons", ignored_addon_filenames);
435 AddonManager::write(lisp::Writer& writer)
437 writer.write("disabled-addons", ignored_addon_filenames);