-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
info.cpp
316 lines (265 loc) · 8.18 KB
/
info.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright (C) 2012 - 2018 by Iris Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
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 2 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.
See the COPYING file for more details.
*/
#include "addon/info.hpp"
#include "addon/manager.hpp"
#include "config.hpp"
#include "font/pango/escape.hpp"
#include "gettext.hpp"
#include "picture.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include <boost/locale.hpp>
static lg::log_domain log_addons_client("addons-client");
#define ERR_AC LOG_STREAM(err , log_addons_client)
#define LOG_AC LOG_STREAM(info, log_addons_client)
namespace {
void resolve_deps_recursive(const addons_list& addons, const std::string& base_id, std::set<std::string>& dest)
{
addons_list::const_iterator it = addons.find(base_id);
if(it == addons.end()) {
LOG_AC << "resolve_deps_recursive(): " << base_id << " not in add-ons list\n";
return;
}
const std::vector<std::string>& base_deps = it->second.depends;
if(base_deps.empty()) {
return;
}
for(const std::string& dep : base_deps) {
if(base_id == dep) {
LOG_AC << dep << " depends upon itself; breaking circular dependency\n";
continue;
} else if(dest.find(dep) != dest.end()) {
LOG_AC << dep << " already in dependency tree; breaking circular dependency\n";
continue;
}
dest.insert(dep);
resolve_deps_recursive(addons, dep, dest);
}
}
}
void addon_info_translation::read(const config& cfg)
{
supported = cfg["supported"].to_bool(true);
title = cfg["title"].str();
description = cfg["description"].str();
}
void addon_info_translation::write(config& cfg) const
{
cfg["supported"] = supported;
cfg["title"] = title;
cfg["description"] = description;
}
void addon_info::read(const config& cfg)
{
id = cfg["name"].str();
title = cfg["title"].str();
description = cfg["description"].str();
icon = cfg["icon"].str();
version = cfg["version"].str();
author = cfg["author"].str();
size = cfg["size"];
downloads = cfg["downloads"];
uploads = cfg["uploads"];
type = get_addon_type(cfg["type"].str());
const config::const_child_itors& locales_as_configs = cfg.child_range("translation");
for(const config& locale : locales_as_configs) {
if(locale["supported"].to_bool(true))
locales.emplace_back(locale["language"].str());
info_translations.emplace(locale["language"].str(), addon_info_translation(locale));
}
core = cfg["core"].str();
depends = utils::split(cfg["dependencies"].str());
tags = utils::split(cfg["tags"].str());
feedback_url = cfg["feedback_url"].str();
updated = cfg["timestamp"].to_time_t();
created = cfg["original_timestamp"].to_time_t();
local_only = cfg["local_only"].to_bool();
}
void addon_info::write(config& cfg) const
{
cfg["id"] = id;
cfg["title"] = title;
cfg["description"] = description;
cfg["icon"] = icon;
cfg["version"] = version.str();
cfg["author"] = author;
cfg["size"] = size;
cfg["downloads"] = downloads;
cfg["uploads"] = uploads;
cfg["type"] = get_addon_type_string(type);
for(const auto& element : info_translations) {
config& locale = cfg.add_child("translation");
locale["language"] = element.first;
element.second.write(locale);
}
cfg["core"] = core;
cfg["dependencies"] = utils::join(depends);
cfg["tags"] = utils::join(tags);
cfg["feedback_url"] = feedback_url;
cfg["timestamp"] = updated;
cfg["original_timestamp"] = created;
}
void addon_info::write_minimal(config& cfg) const
{
cfg["version"] = version.str();
cfg["uploads"] = uploads;
cfg["type"] = get_addon_type_string(type);
cfg["title"] = title;
cfg["dependencies"] = utils::join(depends);
cfg["core"] = core;
}
std::string addon_info::display_title() const
{
if(title.empty()) {
return font::escape_text(make_addon_title(id));
} else {
return font::escape_text(title);
}
}
addon_info_translation addon_info_translation::invalid = {false, "", ""};
addon_info_translation addon_info::translated_info() const
{
const boost::locale::info& locale_info = translation::get_effective_locale_info();
std::string lang_name_short = locale_info.language();
std::string lang_name_long = lang_name_short;
if(!locale_info.country().empty()) {
lang_name_long += '_';
lang_name_long += locale_info.country();
}
if(!locale_info.variant().empty()) {
lang_name_long += '@';
lang_name_long += locale_info.variant();
lang_name_short += '@';
lang_name_short += locale_info.variant();
}
auto info = info_translations.find(lang_name_long);
if(info != info_translations.end()) {
return info->second;
}
info = info_translations.find(lang_name_short);
if(info != info_translations.end()) {
return info->second;
}
return addon_info_translation::invalid;
}
std::string addon_info::display_title_translated() const
{
addon_info_translation info = translated_info();
if(info.valid()) {
return info.title;
}
return "";
}
std::string addon_info::display_title_translated_or_original() const
{
std::string title = display_title_translated();
return title.empty() ? display_title() : title;
}
std::string addon_info::description_translated() const
{
addon_info_translation info = translated_info();
if(info.valid() && !info.description.empty()) {
return info.description;
}
return description;
}
std::string addon_info::display_title_full() const
{
std::string local_title = display_title_translated();
if(local_title.empty())
return display_title();
return local_title + " (" + display_title() + ")";
}
std::string addon_info::display_icon() const
{
std::string ret = icon;
if(ret.empty()) {
ERR_AC << "add-on '" << id << "' doesn't have an icon path set" << std::endl;
} else if(!image::exists(ret)) {
ERR_AC << "add-on '" << id << "' has an icon which cannot be found: '" << ret << "'" << std::endl;
} else if(ret.find("units/") != std::string::npos && ret.find_first_of('~') == std::string::npos) {
// HACK: prevent magenta icons, because they look awful
LOG_AC << "add-on '" << id << "' uses a unit baseframe as icon without TC/RC specifications\n";
ret += "~RC(magenta>red)";
}
return ret;
}
std::string addon_info::display_type() const
{
switch (type) {
case ADDON_SP_CAMPAIGN:
return _("addon_type^Campaign");
case ADDON_SP_SCENARIO:
return _("addon_type^Scenario");
case ADDON_SP_MP_CAMPAIGN:
return _("addon_type^SP/MP Campaign");
case ADDON_MP_ERA:
return _("addon_type^MP era");
case ADDON_MP_FACTION:
return _("addon_type^MP faction");
case ADDON_MP_MAPS:
return _("addon_type^MP map-pack");
case ADDON_MP_SCENARIO:
return _("addon_type^MP scenario");
case ADDON_MP_CAMPAIGN:
return _("addon_type^MP campaign");
case ADDON_MOD:
return _("addon_type^Modification");
case ADDON_CORE:
return _("addon_type^Core");
case ADDON_MEDIA:
return _("addon_type^Resources");
case ADDON_OTHER:
return _("addon_type^Other");
default:
return _("addon_type^(unknown)");
}
}
std::set<std::string> addon_info::resolve_dependencies(const addons_list& addons) const
{
std::set<std::string> deps;
resolve_deps_recursive(addons, id, deps);
if(deps.find(id) != deps.end()) {
LOG_AC << id << " depends upon itself; breaking circular dependency\n";
deps.erase(id);
}
return deps;
}
void read_addons_list(const config& cfg, addons_list& dest)
{
dest.clear();
/** @todo FIXME: get rid of this legacy "campaign"/"campaigns" silliness
*/
const config::const_child_itors &addon_cfgs = cfg.child_range("campaign");
for(const config& addon_cfg : addon_cfgs) {
const std::string& id = addon_cfg["name"].str();
if(dest.find(id) != dest.end()) {
ERR_AC << "add-ons list has multiple entries for '" << id << "', not good; ignoring them" << std::endl;
continue;
}
dest[id].read(addon_cfg);
}
}
std::string size_display_string(double size)
{
if(size > 0.0) {
return utils::si_string(size, true, _("unit_byte^B"));
} else {
return "";
}
}
std::string make_addon_title(const std::string& id)
{
std::string ret(id);
std::replace(ret.begin(), ret.end(), '_', ' ');
return ret;
}