2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
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.
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.
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/>.
17 #include "addon/addon.hpp"
23 #include "lisp/parser.hpp"
24 #include "util/reader.hpp"
25 #include "util/writer.hpp"
26 #include "util/log.hpp"
30 static const char* s_allowed_characters = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
32 Addon::Type addon_type_from_string(const std::string& type)
38 else if (type == "worldmap")
40 return Addon::WORLDMAP;
42 else if (type == "levelset")
44 return Addon::LEVELSET;
48 throw std::runtime_error("not a valid Addon::Type: " + type);
54 std::unique_ptr<Addon>
55 Addon::parse(const Reader& lisp)
57 std::unique_ptr<Addon> addon(new Addon);
61 if (!lisp.get("id", addon->m_id))
63 throw std::runtime_error("(id ...) field missing from addon description");
66 if (addon->m_id.empty())
68 throw std::runtime_error("addon id is empty");
71 if (addon->m_id.find_first_not_of(s_allowed_characters) != std::string::npos)
73 throw std::runtime_error("addon id contains illegal characters: " + addon->m_id);
76 lisp.get("version", addon->m_version);
79 lisp.get("type", type);
80 addon->m_type = addon_type_from_string(type);
82 lisp.get("title", addon->m_title);
83 lisp.get("author", addon->m_author);
84 lisp.get("license", addon->m_license);
85 lisp.get("http-url", addon->m_http_url);
86 lisp.get("md5", addon->m_md5);
90 catch(const std::exception& err)
92 std::stringstream msg;
93 msg << "Problem when parsing addoninfo: " << err.what();
94 throw std::runtime_error(msg.str());
98 std::unique_ptr<Addon>
99 Addon::parse(const std::string& fname)
104 const lisp::Lisp* root = parser.parse(fname);
105 const lisp::Lisp* addon = root->get_lisp("supertux-addoninfo");
108 throw std::runtime_error("file is not a supertux-addoninfo file.");
112 return parse(*addon);
115 catch(const std::exception& err)
117 std::stringstream msg;
118 msg << "Problem when reading addoninfo '" << fname << "': " << err.what();
119 throw std::runtime_error(msg.str());
132 m_install_filename(),
137 Addon::get_filename() const
139 return get_id() + ".zip";
143 Addon::get_install_filename() const
145 return m_install_filename;
149 Addon::is_installed() const
151 return !m_install_filename.empty();
155 Addon::is_enabled() const
161 Addon::set_install_filename(const std::string& absolute_filename)
163 m_install_filename = absolute_filename;
167 Addon::set_enabled(bool v)