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.hpp"
23 #include "addon/md5.hpp"
24 #include "lisp/parser.hpp"
25 #include "util/reader.hpp"
26 #include "util/writer.hpp"
27 #include "util/log.hpp"
30 Addon::get_md5() const
34 if (stored_md5.empty())
36 log_warning << "Add-on not installed and no stored MD5 available" << std::endl;
40 else if (!calculated_md5.empty())
42 return calculated_md5;
44 else if (installed_physfs_filename.empty())
46 throw std::runtime_error("Tried to calculate MD5 of Add-on with unknown filename");
50 // TODO: this does not work as expected for some files -- IFileStream seems to not always behave like an ifstream.
51 //IFileStream ifs(installed_physfs_filename);
52 //std::string md5 = MD5(ifs).hex_digest();
56 file = PHYSFS_openRead(installed_physfs_filename.c_str());
57 unsigned char buffer[1024];
59 PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
61 md5.update(buffer, len);
65 calculated_md5 = md5.hex_digest();
66 log_debug << "MD5 of " << title << ": " << calculated_md5 << std::endl;
68 return calculated_md5;
73 Addon::parse(const Reader& lisp)
77 lisp.get("kind", kind);
78 lisp.get("title", title);
79 lisp.get("author", author);
80 lisp.get("license", license);
81 lisp.get("http-url", http_url);
82 lisp.get("file", suggested_filename);
83 lisp.get("md5", stored_md5);
85 catch(const std::exception& err)
87 std::stringstream msg;
88 msg << "Problem when parsing addoninfo: " << err.what();
89 throw std::runtime_error(msg.str());
94 Addon::parse(const std::string& fname)
99 const lisp::Lisp* root = parser.parse(fname);
100 const lisp::Lisp* addon = root->get_lisp("supertux-addoninfo");
101 if(!addon) throw std::runtime_error("file is not a supertux-addoninfo file.");
104 catch(const std::exception& err)
106 std::stringstream msg;
107 msg << "Problem when reading addoninfo '" << fname << "': " << err.what();
108 throw std::runtime_error(msg.str());
113 Addon::operator==(const Addon& addon2) const
115 std::string s1 = this->get_md5();
116 std::string s2 = addon2.get_md5();
118 if ((s1 != "") && (s2 != "")) return (s1 == s2);
120 if (this->title != addon2.title) return false;
121 if (this->author != addon2.author) return false;
122 if (this->kind != addon2.kind) return false;