2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 // 2014 Ingo Ruhnke <grumbel@gmail.com>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "addon/addon.hpp"
24 #include "lisp/parser.hpp"
25 #include "util/reader.hpp"
26 #include "util/writer.hpp"
27 #include "util/log.hpp"
31 static const char* s_allowed_characters = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
33 Addon::Type addon_type_from_string(const std::string& type)
39 else if (type == "worldmap")
41 return Addon::WORLDMAP;
43 else if (type == "levelset")
45 return Addon::LEVELSET;
49 throw std::runtime_error("not a valid Addon::Type: " + type);
55 std::unique_ptr<Addon>
56 Addon::parse(const Reader& lisp)
58 std::unique_ptr<Addon> addon(new Addon);
62 if (!lisp.get("id", addon->m_id))
64 throw std::runtime_error("(id ...) field missing from addon description");
67 if (addon->m_id.empty())
69 throw std::runtime_error("addon id is empty");
72 if (addon->m_id.find_first_not_of(s_allowed_characters) != std::string::npos)
74 throw std::runtime_error("addon id contains illegal characters: " + addon->m_id);
77 lisp.get("version", addon->m_version);
80 lisp.get("type", type);
81 addon->m_type = addon_type_from_string(type);
83 lisp.get("title", addon->m_title);
84 lisp.get("author", addon->m_author);
85 lisp.get("license", addon->m_license);
86 lisp.get("http-url", addon->m_http_url);
87 lisp.get("md5", addon->m_md5);
91 catch(const std::exception& err)
93 std::stringstream msg;
94 msg << "Problem when parsing addoninfo: " << err.what();
95 throw std::runtime_error(msg.str());
99 std::unique_ptr<Addon>
100 Addon::parse(const std::string& fname)
105 const lisp::Lisp* root = parser.parse(fname);
106 const lisp::Lisp* addon = root->get_lisp("supertux-addoninfo");
109 throw std::runtime_error("file is not a supertux-addoninfo file.");
113 return parse(*addon);
116 catch(const std::exception& err)
118 std::stringstream msg;
119 msg << "Problem when reading addoninfo '" << fname << "': " << err.what();
120 throw std::runtime_error(msg.str());
133 m_install_filename(),
138 Addon::get_filename() const
140 return get_id() + ".zip";
144 Addon::get_install_filename() const
146 return m_install_filename;
150 Addon::is_installed() const
152 return !m_install_filename.empty();
156 Addon::is_enabled() const
162 Addon::set_install_filename(const std::string& absolute_filename, const std::string& md5)
164 m_install_filename = absolute_filename;
169 Addon::set_enabled(bool v)