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
26 #include "lisp/lisp.hpp"
27 #include "lisp/writer.hpp"
28 #include "lisp/parser.hpp"
29 #include "addon/addon.hpp"
31 #include "addon/md5.hpp"
34 Addon::get_md5() const
37 if (stored_md5 == "") log_warning << "Add-on not installed and no stored MD5 available" << std::endl;
41 if (calculated_md5 != "") return calculated_md5;
43 if (installed_physfs_filename == "") throw std::runtime_error("Tried to calculate MD5 of Add-on with unknown filename");
45 // TODO: this does not work as expected for some files -- IFileStream seems to not always behave like an ifstream.
46 //IFileStream ifs(installed_physfs_filename);
47 //std::string md5 = MD5(ifs).hex_digest();
51 file = PHYSFS_openRead(installed_physfs_filename.c_str());
52 unsigned char buffer[1024];
54 PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
56 md5.update(buffer, len);
60 calculated_md5 = md5.hex_digest();
61 log_debug << "MD5 of " << title << ": " << calculated_md5 << std::endl;
63 return calculated_md5;
67 Addon::parse(const lisp::Lisp& lisp)
70 lisp.get("kind", kind);
71 lisp.get("title", title);
72 lisp.get("author", author);
73 lisp.get("license", license);
74 lisp.get("http-url", http_url);
75 lisp.get("file", suggested_filename);
76 lisp.get("md5", stored_md5);
77 } catch(std::exception& e) {
78 std::stringstream msg;
79 msg << "Problem when parsing addoninfo: " << e.what();
80 throw std::runtime_error(msg.str());
85 Addon::parse(std::string fname)
89 const lisp::Lisp* root = parser.parse(fname);
90 const lisp::Lisp* addon = root->get_lisp("supertux-addoninfo");
91 if(!addon) throw std::runtime_error("file is not a supertux-addoninfo file.");
93 } catch(std::exception& e) {
94 std::stringstream msg;
95 msg << "Problem when reading addoninfo '" << fname << "': " << e.what();
96 throw std::runtime_error(msg.str());
101 Addon::write(lisp::Writer& writer) const
103 writer.start_list("supertux-addoninfo");
104 if (kind != "") writer.write("kind", kind);
105 if (title != "") writer.write("title", title);
106 if (author != "") writer.write("author", author);
107 if (license != "") writer.write("license", license);
108 if (http_url != "") writer.write("http-url", http_url);
109 if (suggested_filename != "") writer.write("file", suggested_filename);
110 if (stored_md5 != "") writer.write("md5", stored_md5);
111 writer.end_list("supertux-addoninfo");
115 Addon::write(std::string fname) const
117 lisp::Writer writer(fname);
122 Addon::operator==(Addon addon2) const
124 std::string s1 = this->get_md5();
125 std::string s2 = addon2.get_md5();
127 if ((s1 != "") && (s2 != "")) return (s1 == s2);
129 if (this->title != addon2.title) return false;
130 if (this->author != addon2.author) return false;
131 if (this->kind != addon2.kind) return false;