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"
30 #include "addon/addon_manager.hpp"
32 #include "addon/md5.hpp"
35 Addon::get_md5() const
38 if (stored_md5 == "") log_warning << "Add-on not installed and no stored MD5 available" << std::endl;
42 if (calculated_md5 != "") return calculated_md5;
44 if (installed_physfs_filename == "") throw std::runtime_error("Tried to calculate MD5 of Add-on with unknown filename");
46 // TODO: this does not work as expected for some files -- IFileStream seems to not always behave like an ifstream.
47 //IFileStream ifs(installed_physfs_filename);
48 //std::string md5 = MD5(ifs).hex_digest();
52 file = PHYSFS_openRead(installed_physfs_filename.c_str());
53 unsigned char buffer[1024];
55 PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
57 md5.update(buffer, len);
61 calculated_md5 = md5.hex_digest();
62 log_debug << "MD5 of " << title << ": " << calculated_md5 << std::endl;
64 return calculated_md5;
68 Addon::parse(const lisp::Lisp& lisp)
71 lisp.get("kind", kind);
72 lisp.get("title", title);
73 lisp.get("author", author);
74 lisp.get("license", license);
75 lisp.get("http-url", http_url);
76 lisp.get("file", suggested_filename);
77 lisp.get("md5", stored_md5);
78 } catch(std::exception& e) {
79 std::stringstream msg;
80 msg << "Problem when parsing addoninfo: " << e.what();
81 throw std::runtime_error(msg.str());
86 Addon::parse(std::string fname)
90 const lisp::Lisp* root = parser.parse(fname);
91 const lisp::Lisp* addon = root->get_lisp("supertux-addoninfo");
92 if(!addon) throw std::runtime_error("file is not a supertux-addoninfo file.");
94 } catch(std::exception& e) {
95 std::stringstream msg;
96 msg << "Problem when reading addoninfo '" << fname << "': " << e.what();
97 throw std::runtime_error(msg.str());
102 Addon::write(lisp::Writer& writer) const
104 writer.start_list("supertux-addoninfo");
105 if (kind != "") writer.write_string("kind", kind);
106 if (title != "") writer.write_string("title", title);
107 if (author != "") writer.write_string("author", author);
108 if (license != "") writer.write_string("license", license);
109 if (http_url != "") writer.write_string("http-url", http_url);
110 if (suggested_filename != "") writer.write_string("file", suggested_filename);
111 if (stored_md5 != "") writer.write_string("md5", stored_md5);
112 writer.end_list("supertux-addoninfo");
116 Addon::write(std::string fname) const
118 lisp::Writer writer(fname);
123 Addon::operator==(Addon addon2) const
125 std::string s1 = this->get_md5();
126 std::string s2 = addon2.get_md5();
128 if ((s1 != "") && (s2 != "")) return (s1 == s2);
130 if (this->title != addon2.title) return false;
131 if (this->author != addon2.author) return false;
132 if (this->kind != addon2.kind) return false;