The Battle for Wesnoth  1.15.7+dev
info.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2018 by Iris Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
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 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "addon/info.hpp"
16 
17 #include "addon/manager.hpp"
18 #include "config.hpp"
19 #include "font/pango/escape.hpp"
20 #include "gettext.hpp"
21 #include "picture.hpp"
22 #include "log.hpp"
24 
25 #include <boost/locale.hpp>
26 
27 static lg::log_domain log_addons_client("addons-client");
28 #define ERR_AC LOG_STREAM(err , log_addons_client)
29 #define LOG_AC LOG_STREAM(info, log_addons_client)
30 
31 namespace {
32  void resolve_deps_recursive(const addons_list& addons, const std::string& base_id, std::set<std::string>& dest)
33  {
34  addons_list::const_iterator it = addons.find(base_id);
35  if(it == addons.end()) {
36  LOG_AC << "resolve_deps_recursive(): " << base_id << " not in add-ons list\n";
37  return;
38  }
39 
40  const std::vector<std::string>& base_deps = it->second.depends;
41 
42  if(base_deps.empty()) {
43  return;
44  }
45 
46  for(const std::string& dep : base_deps) {
47  if(base_id == dep) {
48  LOG_AC << dep << " depends upon itself; breaking circular dependency\n";
49  continue;
50  } else if(dest.find(dep) != dest.end()) {
51  LOG_AC << dep << " already in dependency tree; breaking circular dependency\n";
52  continue;
53  }
54 
55  dest.insert(dep);
56 
57  resolve_deps_recursive(addons, dep, dest);
58  }
59  }
60 }
61 
63 {
64  supported = cfg["supported"].to_bool(true);
65  title = cfg["title"].str();
66  description = cfg["description"].str();
67 }
68 
70 {
71  cfg["supported"] = supported;
72  cfg["title"] = title;
73  cfg["description"] = description;
74 }
75 
76 void addon_info::read(const config& cfg)
77 {
78  id = cfg["name"].str();
79  title = cfg["title"].str();
80  description = cfg["description"].str();
81  icon = cfg["icon"].str();
82  current_version = cfg["version"].str();
83  versions.emplace(cfg["version"].str());
84  author = cfg["author"].str();
85  size = cfg["size"];
86  downloads = cfg["downloads"];
87  uploads = cfg["uploads"];
88  type = get_addon_type(cfg["type"].str());
89 
90  for(const config& version : cfg.child_range("version")) {
91  versions.emplace(version["version"].str());
92  }
93 
94  const config::const_child_itors& locales_as_configs = cfg.child_range("translation");
95 
96  for(const config& locale : locales_as_configs) {
97  if(locale["supported"].to_bool(true))
98  locales.emplace_back(locale["language"].str());
99  info_translations.emplace(locale["language"].str(), addon_info_translation(locale));
100  }
101 
102  core = cfg["core"].str();
103  depends = utils::split(cfg["dependencies"].str());
104  tags = utils::split(cfg["tags"].str());
105  feedback_url = cfg["feedback_url"].str();
106 
107  updated = cfg["timestamp"].to_time_t();
108  created = cfg["original_timestamp"].to_time_t();
109 
110  local_only = cfg["local_only"].to_bool();
111 }
112 
113 void addon_info::write(config& cfg) const
114 {
115  cfg["id"] = id;
116  cfg["title"] = title;
117  cfg["description"] = description;
118  cfg["icon"] = icon;
119  cfg["version"] = current_version.str();
120  cfg["author"] = author;
121  cfg["size"] = size;
122  cfg["downloads"] = downloads;
123  cfg["uploads"] = uploads;
124  cfg["type"] = get_addon_type_string(type);
125 
126  for(const version_info& version : versions) {
127  config& version_cfg = cfg.add_child("version");
128  version_cfg["version"] = version.str();
129  }
130 
131  for(const auto& element : info_translations) {
132  config& locale = cfg.add_child("translation");
133  locale["language"] = element.first;
134  element.second.write(locale);
135  }
136 
137  cfg["core"] = core;
138  cfg["dependencies"] = utils::join(depends);
139  cfg["tags"] = utils::join(tags);
140  cfg["feedback_url"] = feedback_url;
141 
142  cfg["timestamp"] = updated;
143  cfg["original_timestamp"] = created;
144 }
145 
147 {
148  cfg["version"] = current_version.str();
149  cfg["uploads"] = uploads;
150  cfg["type"] = get_addon_type_string(type);
151  cfg["title"] = title;
152  cfg["dependencies"] = utils::join(depends);
153  cfg["core"] = core;
154 }
155 
157 {
158  if(title.empty()) {
160  } else {
161  return font::escape_text(title);
162  }
163 }
164 
166 
168 {
170 
171  std::string lang_name_short = locale_info.language();
172  std::string lang_name_long = lang_name_short;
173  if(!locale_info.country().empty()) {
174  lang_name_long += '_';
175  lang_name_long += locale_info.country();
176  }
177  if(!locale_info.variant().empty()) {
178  lang_name_long += '@';
179  lang_name_long += locale_info.variant();
180  lang_name_short += '@';
181  lang_name_short += locale_info.variant();
182  }
183 
184  auto info = info_translations.find(lang_name_long);
185  if(info != info_translations.end()) {
186  return info->second;
187  }
188 
189  info = info_translations.find(lang_name_short);
190  if(info != info_translations.end()) {
191  return info->second;
192  }
193 
195 }
196 
198 {
199  addon_info_translation info = translated_info();
200 
201  if(info.valid()) {
202  return info.title;
203  }
204 
205  return "";
206 }
207 
209 {
210  std::string title = display_title_translated();
211  return title.empty() ? display_title() : title;
212 }
213 
215 {
216  addon_info_translation info = translated_info();
217 
218  if(info.valid() && !info.description.empty()) {
219  return info.description;
220  }
221 
222  return description;
223 }
224 
226 {
227  std::string local_title = display_title_translated();
228  if(local_title.empty())
229  return display_title();
230  return local_title + " (" + display_title() + ")";
231 }
232 
234 {
235  std::string ret = icon;
236 
237  if(ret.empty()) {
238  ERR_AC << "add-on '" << id << "' doesn't have an icon path set" << std::endl;
239  } else if(!image::exists(ret)) {
240  ERR_AC << "add-on '" << id << "' has an icon which cannot be found: '" << ret << "'" << std::endl;
241  } else if(ret.find("units/") != std::string::npos && ret.find_first_of('~') == std::string::npos) {
242  // HACK: prevent magenta icons, because they look awful
243  LOG_AC << "add-on '" << id << "' uses a unit baseframe as icon without TC/RC specifications\n";
244  ret += "~RC(magenta>red)";
245  }
246 
247  return ret;
248 }
249 
251 {
252  switch (type) {
253  case ADDON_SP_CAMPAIGN:
254  return _("addon_type^Campaign");
255  case ADDON_SP_SCENARIO:
256  return _("addon_type^Scenario");
258  return _("addon_type^SP/MP Campaign");
259  case ADDON_MP_ERA:
260  return _("addon_type^MP era");
261  case ADDON_MP_FACTION:
262  return _("addon_type^MP faction");
263  case ADDON_MP_MAPS:
264  return _("addon_type^MP map-pack");
265  case ADDON_MP_SCENARIO:
266  return _("addon_type^MP scenario");
267  case ADDON_MP_CAMPAIGN:
268  return _("addon_type^MP campaign");
269  case ADDON_MOD:
270  return _("addon_type^Modification");
271  case ADDON_CORE:
272  return _("addon_type^Core");
273  case ADDON_MEDIA:
274  return _("addon_type^Resources");
275  case ADDON_OTHER:
276  return _("addon_type^Other");
277  default:
278  return _("addon_type^(unknown)");
279  }
280 }
281 
282 std::set<std::string> addon_info::resolve_dependencies(const addons_list& addons) const
283 {
284  std::set<std::string> deps;
285  resolve_deps_recursive(addons, id, deps);
286 
287  if(deps.find(id) != deps.end()) {
288  LOG_AC << id << " depends upon itself; breaking circular dependency\n";
289  deps.erase(id);
290  }
291 
292  return deps;
293 }
294 
295 void read_addons_list(const config& cfg, addons_list& dest)
296 {
297  dest.clear();
298 
299  /** @todo FIXME: get rid of this legacy "campaign"/"campaigns" silliness
300  */
301  const config::const_child_itors &addon_cfgs = cfg.child_range("campaign");
302  for(const config& addon_cfg : addon_cfgs) {
303  const std::string& id = addon_cfg["name"].str();
304  if(dest.find(id) != dest.end()) {
305  ERR_AC << "add-ons list has multiple entries for '" << id << "', not good; ignoring them" << std::endl;
306  continue;
307  }
308  dest[id].read(addon_cfg);
309  }
310 }
311 
313 {
314  if(size > 0.0) {
315  return utils::si_string(size, true, _("unit_byte^B"));
316  } else {
317  return "";
318  }
319 }
320 
322 {
323  std::string ret(id);
324  std::replace(ret.begin(), ret.end(), '_', ' ');
325  return ret;
326 }
void read_addons_list(const config &cfg, addons_list &dest)
Parse the specified add-ons list WML into an actual addons_list object.
Definition: info.cpp:295
Modification of the game.
Definition: validation.hpp:106
Single-player scenario.
Definition: validation.hpp:98
void write(config &cfg) const
Definition: info.cpp:113
an add-on that fits in no other category
Definition: validation.hpp:109
std::string display_title_translated() const
Definition: info.cpp:197
Total Conversion Core.
Definition: validation.hpp:96
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
#define ERR_AC
Definition: info.cpp:28
std::string display_title_translated_or_original() const
Definition: info.cpp:208
std::string display_title() const
Get a title or automatic title for display.
Definition: info.cpp:156
logger & info()
Definition: log.cpp:91
static lg::log_domain log_addons_client("addons-client")
child_itors child_range(config_key_type key)
Definition: config.cpp:362
Multiplayer faction.
Definition: validation.hpp:104
std::string str
Definition: statement.cpp:110
std::string title
Definition: info.hpp:35
Definitions for the interface to Wesnoth Markup Language (WML).
void write_minimal(config &cfg) const
Write only minimal WML used for state tracking (_info.cfg) files.
Definition: info.cpp:146
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: picture.cpp:1028
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:100
#define LOG_AC
Definition: info.cpp:29
static addon_info_translation invalid
Definition: info.hpp:32
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
std::string display_icon() const
Get an icon path fixed for display (e.g.
Definition: info.cpp:233
std::string description
Definition: info.hpp:36
std::string display_type() const
Get an add-on type identifier for display in the user&#39;s language.
Definition: info.cpp:250
ADDON_TYPE get_addon_type(const std::string &str)
Definition: validation.cpp:240
Miscellaneous content/media (unit packs, terrain packs, music packs, etc.).
Definition: validation.hpp:108
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:213
void read(const config &cfg)
Definition: info.cpp:76
Multiplayer scenario.
Definition: validation.hpp:101
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:32
Multiplayer era.
Definition: validation.hpp:103
u64 size
Definition: statement.cpp:80
std::string size_display_string(double size)
Get a human-readable representation of the specified byte count.
Definition: info.cpp:312
std::string display_title_full() const
Definition: info.cpp:225
Multiplayer plain (no WML) map pack.
Definition: validation.hpp:102
Represents version numbers.
config & add_child(config_key_type key)
Definition: config.cpp:476
std::string make_addon_title(const std::string &id)
Replaces underscores to dress up file or dirnames as add-on titles.
Definition: info.cpp:321
Hybrid campaign.
Definition: validation.hpp:99
void read(const config &cfg)
Definition: info.cpp:62
std::vector< std::string > split(const config_attribute_value &val)
std::set< std::string > resolve_dependencies(const addons_list &addons) const
Resolve an add-on&#39;s dependency tree in a recursive fashion.
Definition: info.cpp:282
addon_info_translation translated_info() const
Definition: info.cpp:167
Standard logging facilities (interface).
std::string get_addon_type_string(ADDON_TYPE type)
Definition: validation.cpp:256
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
void write(config &cfg) const
Definition: info.cpp:69
std::map< std::string, addon_info > addons_list
Definition: info.hpp:27
Single-player campaign.
Definition: validation.hpp:97
Multiplayer campaign.
Definition: validation.hpp:100
std::string description_translated() const
Definition: info.cpp:214
const boost::locale::info & get_effective_locale_info()
A facet that holds general information about the effective locale.
Definition: gettext.cpp:530