7e7e98ee0acb7f1d20602d8ede8e35016501a1b3
[supertux.git] / src / addon / addon.cpp
1 //  $Id$
2 //
3 //  SuperTux - Add-on
4 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
5 //
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.
10 //
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.
15 //
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
19 //  02111-1307, USA.
20 //
21
22 #include <string>
23 #include <sstream>
24 #include <stdexcept>
25 #include <physfs.h>
26 #include "lisp/lisp.hpp"
27 #include "lisp/writer.hpp"
28 #include "lisp/parser.hpp"
29 #include "addon/addon.hpp"
30 #include "log.hpp"
31 #include "addon/md5.hpp"
32
33 std::string
34 Addon::get_md5() const
35 {
36   if (!installed) {
37     if (stored_md5 == "") log_warning << "Add-on not installed and no stored MD5 available" << std::endl;
38     return stored_md5;
39   }
40
41   if (calculated_md5 != "") return calculated_md5;
42
43   if (installed_physfs_filename == "") throw std::runtime_error("Tried to calculate MD5 of Add-on with unknown filename");
44
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();
48
49   MD5 md5;
50   PHYSFS_file* file;
51   file = PHYSFS_openRead(installed_physfs_filename.c_str());
52   unsigned char buffer[1024];
53   while (true) {
54     PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
55     if (len <= 0) break;
56     md5.update(buffer, len);
57   }
58   PHYSFS_close(file);
59
60   calculated_md5 = md5.hex_digest();
61   log_debug << "MD5 of " << title << ": " << calculated_md5 << std::endl;
62
63   return calculated_md5;
64 }
65
66 void
67 Addon::parse(const lisp::Lisp& lisp)
68 {
69   try {
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());
81   }
82 }
83
84 void
85 Addon::parse(std::string fname)
86 {
87   try {
88     lisp::Parser parser;
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.");
92     parse(*addon);
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());
97   }
98 }
99
100 void
101 Addon::write(lisp::Writer& writer) const
102 {
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");
112 }
113
114 void 
115 Addon::write(std::string fname) const
116 {
117   lisp::Writer writer(fname);
118   write(writer);
119 }
120
121 bool 
122 Addon::operator==(Addon addon2) const
123 {
124   std::string s1 = this->get_md5();
125   std::string s2 = addon2.get_md5();
126
127   if ((s1 != "") && (s2 != "")) return (s1 == s2);
128
129   if (this->title != addon2.title) return false;
130   if (this->author != addon2.author) return false;
131   if (this->kind != addon2.kind) return false;
132   return true; 
133 }
134