more svn:eol-style = native and header fixes
[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 "addon/addon_manager.hpp"
31 #include "log.hpp"
32 #include "addon/md5.hpp"
33
34 std::string
35 Addon::get_md5() const
36 {
37   if (!installed) {
38     if (stored_md5 == "") log_warning << "Add-on not installed and no stored MD5 available" << std::endl;
39     return stored_md5;
40   }
41
42   if (calculated_md5 != "") return calculated_md5;
43
44   if (installed_physfs_filename == "") throw std::runtime_error("Tried to calculate MD5 of Add-on with unknown filename");
45
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();
49
50   MD5 md5;
51   PHYSFS_file* file;
52   file = PHYSFS_openRead(installed_physfs_filename.c_str());
53   unsigned char buffer[1024];
54   while (true) {
55     PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
56     if (len <= 0) break;
57     md5.update(buffer, len);
58   }
59   PHYSFS_close(file);
60
61   calculated_md5 = md5.hex_digest();
62   log_debug << "MD5 of " << title << ": " << calculated_md5 << std::endl;
63
64   return calculated_md5;
65 }
66
67 void
68 Addon::parse(const lisp::Lisp& lisp)
69 {
70   try {
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());
82   }
83 }
84
85 void
86 Addon::parse(std::string fname)
87 {
88   try {
89     lisp::Parser parser;
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.");
93     parse(*addon);
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());
98   }
99 }
100
101 void
102 Addon::write(lisp::Writer& writer) const
103 {
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");
113 }
114
115 void 
116 Addon::write(std::string fname) const
117 {
118   lisp::Writer writer(fname);
119   write(writer);
120 }
121
122 bool 
123 Addon::operator==(Addon addon2) const
124 {
125   std::string s1 = this->get_md5();
126   std::string s2 = addon2.get_md5();
127
128   if ((s1 != "") && (s2 != "")) return (s1 == s2);
129
130   if (this->title != addon2.title) return false;
131   if (this->author != addon2.author) return false;
132   if (this->kind != addon2.kind) return false;
133   return true; 
134 }
135