First round of cleanup up the AddonManager a bit
[supertux.git] / src / addon / addon.cpp
1 //  SuperTux - Add-on
2 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "addon/addon.hpp"
18
19 #include <physfs.h>
20 #include <stdexcept>
21 #include <sstream>
22
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"
28
29 std::string
30 Addon::get_md5() const
31 {
32   if (!installed)
33   {
34     if (stored_md5.empty())
35     {
36       log_warning << "Add-on not installed and no stored MD5 available" << std::endl;
37     }
38     return stored_md5;
39   }
40   else if (!calculated_md5.empty())
41   {
42     return calculated_md5;
43   }
44   else if (installed_physfs_filename.empty())
45   {
46     throw std::runtime_error("Tried to calculate MD5 of Add-on with unknown filename");
47   }
48   else
49   {
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();
53
54     MD5 md5;
55     PHYSFS_file* file;
56     file = PHYSFS_openRead(installed_physfs_filename.c_str());
57     unsigned char buffer[1024];
58     while (true) {
59       PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
60       if (len <= 0) break;
61       md5.update(buffer, len);
62     }
63     PHYSFS_close(file);
64
65     calculated_md5 = md5.hex_digest();
66     log_debug << "MD5 of " << title << ": " << calculated_md5 << std::endl;
67
68     return calculated_md5;
69   }
70 }
71
72 void
73 Addon::parse(const Reader& lisp)
74 {
75   try
76   {
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);
84   }
85   catch(const std::exception& err)
86   {
87     std::stringstream msg;
88     msg << "Problem when parsing addoninfo: " << err.what();
89     throw std::runtime_error(msg.str());
90   }
91 }
92
93 void
94 Addon::parse(const std::string& fname)
95 {
96   try
97   {
98     lisp::Parser parser;
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.");
102     parse(*addon);
103   }
104   catch(const std::exception& err)
105   {
106     std::stringstream msg;
107     msg << "Problem when reading addoninfo '" << fname << "': " << err.what();
108     throw std::runtime_error(msg.str());
109   }
110 }
111
112 bool
113 Addon::operator==(const Addon& addon2) const
114 {
115   std::string s1 = this->get_md5();
116   std::string s2 = addon2.get_md5();
117
118   if ((s1 != "") && (s2 != "")) return (s1 == s2);
119
120   if (this->title != addon2.title) return false;
121   if (this->author != addon2.author) return false;
122   if (this->kind != addon2.kind) return false;
123   return true;
124 }
125
126 /* EOF */