3 // SuperTux - Add-on Manager
4 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "addon_manager.hpp"
32 #include <curl/curl.h>
33 #include <curl/types.h>
34 #include <curl/easy.h>
40 size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
42 std::string& s = *static_cast<std::string*>(string_ptr);
43 std::string buf(static_cast<char*>(ptr), size * nmemb);
45 log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
49 size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
51 PHYSFS_file* f = static_cast<PHYSFS_file*>(f_p);
52 PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
53 log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
54 return size * written;
61 AddonManager::get_instance()
63 static AddonManager instance;
67 AddonManager::AddonManager()
70 curl_global_init(CURL_GLOBAL_ALL);
74 AddonManager::~AddonManager()
77 curl_global_cleanup();
82 AddonManager::get_addons() const
84 std::vector<Addon> addons;
86 // first step: search for installed addons
88 // iterate over complete search path (i.e. directories and archives)
89 char **i = PHYSFS_getSearchPath();
90 if (!i) throw std::runtime_error("Could not query physfs search path");
91 for (; *i != NULL; i++) {
93 // get filename of potential archive
94 std::string fileName = *i;
96 // make sure it's in the writeDir
97 static const std::string writeDir = PHYSFS_getWriteDir();
98 if (fileName.compare(0, writeDir.length(), writeDir) != 0) continue;
100 // make sure it looks like an archive
101 static const std::string archiveExt = ".zip";
102 if (fileName.compare(fileName.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
104 // make sure it exists
106 if (stat(fileName.c_str(), &stats) != 0) continue;
108 // make sure it's an actual file
109 if (!S_ISREG(stats.st_mode)) continue;
111 // extract nice title
112 static const char* dirSep = PHYSFS_getDirSeparator();
113 std::string::size_type n = fileName.rfind(dirSep) + 1;
114 if (n == std::string::npos) n = 0;
115 std::string title = fileName.substr(n, fileName.length() - n - archiveExt.length());
119 addon.fname = fileName;
120 addon.isInstalled = true;
122 addons.push_back(addon);
126 // second step: search for available addons
128 // FIXME: This URL is just for testing!
129 const char* baseUrl = "http://www.deltadevelopment.de/users/christoph/supertux/addons/";
130 std::string html = "";
133 curl_handle = curl_easy_init();
134 curl_easy_setopt(curl_handle, CURLOPT_URL, baseUrl);
135 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
136 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_curl_string_append);
137 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &html);
138 curl_easy_perform(curl_handle);
139 curl_easy_cleanup(curl_handle);
141 //std::string html = "Blubb<a href=\"http://www.deltadevelopment.de/users/christoph/supertux/addons/coconut_fortress.zip\">Coconut Fortress</a>\nFoobar<a href=\"http://www.deltadevelopment.de/users/christoph/supertux/addons/in_the_spring.zip\">Another</a>Baz";
142 static const std::string startToken = "href=\"";
143 static const std::string endToken = "\"";
145 // extract urls: for each startToken found...
146 std::string::size_type n = 0;
147 while ((n = html.find(startToken)) != std::string::npos) {
149 // strip everything up to and including token
150 html.erase(0, n + startToken.length());
153 std::string::size_type n2 = html.find(endToken);
154 if (n2 == std::string::npos) break;
156 // extract url: it's the string inbetween
157 std::string url = html.substr(0, n2);
159 // strip everything up to and including endToken
160 html.erase(0, n2 + endToken.length());
163 url = std::string(baseUrl) + url;
165 // make sure url looks like it points to an archive
166 static const std::string archiveExt = ".zip";
167 if (url.compare(url.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
169 // extract nice title
170 std::string::size_type n = url.rfind('/') + 1;
171 if (n == std::string::npos) n = 0;
172 std::string title = url.substr(n, url.length() - n - archiveExt.length());
174 // construct file name
175 std::string fname = url.substr(n);
177 // make sure it does not contain weird characters
178 if (fname.find_first_not_of("match.quiz-proxy_gwenblvdjfks0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) continue;
184 addon.isInstalled = false;
186 addons.push_back(addon);
195 AddonManager::install(const Addon&
204 char* url = (char*)malloc(addon.url.length() + 1);
205 strncpy(url, addon.url.c_str(), addon.url.length() + 1);
207 PHYSFS_file* f = PHYSFS_openWrite(addon.fname.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_perform(curl_handle);
218 curl_easy_cleanup(curl_handle);
224 static const std::string writeDir = PHYSFS_getWriteDir();
225 static const std::string dirSep = PHYSFS_getDirSeparator();
226 std::string fullFilename = writeDir + dirSep + addon.fname;
227 log_debug << "Finished downloading \"" << fullFilename << "\"" << std::endl;
228 PHYSFS_addToSearchPath(fullFilename.c_str(), 1);
234 AddonManager::remove(const Addon& addon)
236 PHYSFS_removeFromSearchPath(addon.fname.c_str());
237 ::remove(addon.fname.c_str());