/
addon.cpp
173 lines (148 loc) · 3.63 KB
/
addon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// SuperTux - Add-on
// Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "addon/addon.hpp"
#include <physfs.h>
#include <stdexcept>
#include <sstream>
#include "lisp/parser.hpp"
#include "util/reader.hpp"
#include "util/writer.hpp"
#include "util/log.hpp"
namespace {
static const char* s_allowed_characters = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Addon::Type addon_type_from_string(const std::string& type)
{
if (type == "world")
{
return Addon::WORLD;
}
else if (type == "worldmap")
{
return Addon::WORLDMAP;
}
else if (type == "levelset")
{
return Addon::LEVELSET;
}
else
{
throw std::runtime_error("not a valid Addon::Type: " + type);
}
}
} // namespace
std::unique_ptr<Addon>
Addon::parse(const Reader& lisp)
{
std::unique_ptr<Addon> addon(new Addon);
try
{
if (!lisp.get("id", addon->m_id))
{
throw std::runtime_error("(id ...) field missing from addon description");
}
if (addon->m_id.empty())
{
throw std::runtime_error("addon id is empty");
}
if (addon->m_id.find_first_not_of(s_allowed_characters) != std::string::npos)
{
throw std::runtime_error("addon id contains illegal characters: " + addon->m_id);
}
lisp.get("version", addon->m_version);
std::string type;
lisp.get("type", type);
addon->m_type = addon_type_from_string(type);
lisp.get("title", addon->m_title);
lisp.get("author", addon->m_author);
lisp.get("license", addon->m_license);
lisp.get("http-url", addon->m_http_url);
lisp.get("md5", addon->m_md5);
return addon;
}
catch(const std::exception& err)
{
std::stringstream msg;
msg << "Problem when parsing addoninfo: " << err.what();
throw std::runtime_error(msg.str());
}
}
std::unique_ptr<Addon>
Addon::parse(const std::string& fname)
{
try
{
lisp::Parser parser;
const lisp::Lisp* root = parser.parse(fname);
const lisp::Lisp* addon = root->get_lisp("supertux-addoninfo");
if(!addon)
{
throw std::runtime_error("file is not a supertux-addoninfo file.");
}
else
{
return parse(*addon);
}
}
catch(const std::exception& err)
{
std::stringstream msg;
msg << "Problem when reading addoninfo '" << fname << "': " << err.what();
throw std::runtime_error(msg.str());
}
}
Addon::Addon() :
m_id(),
m_version(0),
m_type(),
m_title(),
m_author(),
m_license(),
m_http_url(),
m_md5(),
m_install_filename(),
m_enabled(false)
{}
std::string
Addon::get_filename() const
{
return get_id() + ".zip";
}
std::string
Addon::get_install_filename() const
{
return m_install_filename;
}
bool
Addon::is_installed() const
{
return !m_install_filename.empty();
}
bool
Addon::is_enabled() const
{
return m_enabled;
}
void
Addon::set_install_filename(const std::string& absolute_filename)
{
m_install_filename = absolute_filename;
}
void
Addon::set_enabled(bool v)
{
m_enabled = v;
}
/* EOF */