The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
info.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2017 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project http://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 "game_config.hpp"
21 #include "gettext.hpp"
22 #include "image.hpp"
23 #include "log.hpp"
25 
26 static lg::log_domain log_addons_client("addons-client");
27 #define ERR_AC LOG_STREAM(err , log_addons_client)
28 #define LOG_AC LOG_STREAM(info, log_addons_client)
29 
30 namespace {
31  void resolve_deps_recursive(const addons_list& addons, const std::string& base_id, std::set<std::string>& dest)
32  {
33  addons_list::const_iterator it = addons.find(base_id);
34  if(it == addons.end()) {
35  LOG_AC << "resolve_deps_recursive(): " << base_id << " not in add-ons list\n";
36  return;
37  }
38 
39  const std::vector<std::string>& base_deps = it->second.depends;
40 
41  if(base_deps.empty()) {
42  return;
43  }
44 
45  for(const std::string& dep : base_deps) {
46  if(base_id == dep) {
47  LOG_AC << dep << " depends upon itself; breaking circular dependency\n";
48  continue;
49  } else if(dest.find(dep) != dest.end()) {
50  LOG_AC << dep << " already in dependency tree; breaking circular dependency\n";
51  continue;
52  }
53 
54  dest.insert(dep);
55 
56  resolve_deps_recursive(addons, dep, dest);
57  }
58  }
59 }
60 
61 void addon_info::read(const config& cfg)
62 {
63  this->id = cfg["name"].str();
64  this->title = cfg["title"].str();
65  this->description = cfg["description"].str();
66  this->icon = cfg["icon"].str();
67  this->version = cfg["version"].str();
68  this->author = cfg["author"].str();
69  this->size = cfg["size"];
70  this->downloads = cfg["downloads"];
71  this->uploads = cfg["uploads"];
72  this->type = get_addon_type(cfg["type"].str());
73 
74  const config::const_child_itors& locales_as_configs = cfg.child_range("translation");
75 
76  for(const config& locale : locales_as_configs) {
77  this->locales.push_back(locale["language"].str());
78  }
79 
80  this->core = cfg["core"].str();
81  this->depends = utils::split(cfg["dependencies"].str());
82  this->feedback_url = cfg["feedback_url"].str();
83 
84  this->updated = cfg["timestamp"].to_time_t();
85  this->created = cfg["original_timestamp"].to_time_t();
86 }
87 
88 void addon_info::write(config& cfg) const
89 {
90  cfg["id"] = this->id;
91  cfg["title"] = this->title;
92  cfg["description"] = this->description;
93  cfg["icon"] = this->icon;
94  cfg["version"] = this->version.str();
95  cfg["author"] = this->author;
96  cfg["size"] = this->size;
97  cfg["downloads"] = this->downloads;
98  cfg["uploads"] = this->uploads;
99  cfg["type"] = get_addon_type_string(this->type);
100 
101  for (const std::string& locale_id : this->locales) {
102  cfg.add_child("translation")["language"] = locale_id;
103  }
104 
105  cfg["core"] = this->core;
106  cfg["dependencies"] = utils::join(this->depends);
107  cfg["feedback_url"] = this->feedback_url;
108 
109  cfg["timestamp"] = this->updated;
110  cfg["original_timestamp"] = this->created;
111 }
112 
114 {
115  cfg["version"] = this->version.str();
116  cfg["uploads"] = this->uploads;
117  cfg["type"] = get_addon_type_string(this->type);
118  cfg["title"] = this->title;
119  cfg["dependencies"] = utils::join(this->depends);
120  cfg["core"] = this->core;
121 }
122 
124 {
125  if(this->title.empty()) {
126  return font::escape_text(make_addon_title(this->id));
127  } else {
128  return font::escape_text(this->title);
129  }
130 }
131 
133 {
134  std::string ret = icon;
135 
136  if(ret.empty()) {
137  ERR_AC << "add-on '" << id << "' doesn't have an icon path set" << std::endl;
138  } else if(!image::exists(ret)) {
139  ERR_AC << "add-on '" << id << "' has an icon which cannot be found: '" << ret << "'" << std::endl;
140  } else if(ret.find("units/") != std::string::npos && ret.find_first_of('~') == std::string::npos) {
141  // HACK: prevent magenta icons, because they look awful
142  LOG_AC << "add-on '" << id << "' uses a unit baseframe as icon without TC/RC specifications\n";
143  ret += "~RC(magenta>red)";
144  }
145 
146  return ret;
147 }
148 
150 {
151  switch (type) {
152  case ADDON_SP_CAMPAIGN:
153  return _("addon_type^Campaign");
154  case ADDON_SP_SCENARIO:
155  return _("addon_type^Scenario");
157  return _("addon_type^SP/MP Campaign");
158  case ADDON_MP_ERA:
159  return _("addon_type^MP era");
160  case ADDON_MP_FACTION:
161  return _("addon_type^MP faction");
162  case ADDON_MP_MAPS:
163  return _("addon_type^MP map-pack");
164  case ADDON_MP_SCENARIO:
165  return _("addon_type^MP scenario");
166  case ADDON_MP_CAMPAIGN:
167  return _("addon_type^MP campaign");
168  case ADDON_MP_MOD:
169  return _("addon_type^MP modification");
170  case ADDON_CORE:
171  return _("addon_type^Core");
172  case ADDON_MEDIA:
173  return _("addon_type^Resources");
174  case ADDON_OTHER:
175  return _("addon_type^Other");
176  default:
177  return _("addon_type^(unknown)");
178  }
179 }
180 
181 std::set<std::string> addon_info::resolve_dependencies(const addons_list& addons) const
182 {
183  std::set<std::string> deps;
184  resolve_deps_recursive(addons, this->id, deps);
185 
186  if(deps.find(this->id) != deps.end()) {
187  LOG_AC << this->id << " depends upon itself; breaking circular dependency\n";
188  deps.erase(this->id);
189  }
190 
191  return deps;
192 }
193 
194 void read_addons_list(const config& cfg, addons_list& dest)
195 {
196  dest.clear();
197 
198  unsigned order = 0;
199 
200  /** @todo FIXME: get rid of this legacy "campaign"/"campaigns" silliness
201  */
202  const config::const_child_itors &addon_cfgs = cfg.child_range("campaign");
203  for(const config& addon_cfg : addon_cfgs) {
204  const std::string& id = addon_cfg["name"].str();
205  if(dest.find(id) != dest.end()) {
206  ERR_AC << "add-ons list has multiple entries for '" << id << "', not good; ignoring them" << std::endl;
207  continue;
208  }
209  dest[id].read(addon_cfg);
210  dest[id].order = order++;
211  }
212 }
213 
215 {
216  if(size > 0.0) {
217  return utils::si_string(size, true, _("unit_byte^B"));
218  } else {
219  return "";
220  }
221 }
222 
224 {
225  std::string ret(id);
226  std::replace(ret.begin(), ret.end(), '_', ' ');
227  return ret;
228 }
int size
Definition: info.hpp:41
void read_addons_list(const config &cfg, addons_list &dest)
Definition: info.cpp:194
Single-player scenario.
Definition: validation.hpp:44
std::vector< char_t > string
an add-on that fits in no other category
Definition: validation.hpp:55
Total Conversion Core.
Definition: validation.hpp:42
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:27
static lg::log_domain log_addons_client("addons-client")
child_itors child_range(config_key_type key)
Definition: config.cpp:295
std::string description
Definition: info.hpp:33
Multiplayer faction.
Definition: validation.hpp:50
std::string feedback_url
Definition: info.hpp:54
Definitions for the interface to Wesnoth Markup Language (WML).
time_t created
Definition: info.hpp:57
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
void write_minimal(config &cfg) const
Write only minimal WML used for state tracking (_info.cfg) files.
Definition: info.cpp:113
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: image.cpp:1135
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
#define LOG_AC
Definition: info.cpp:28
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
std::string display_title() const
Get a title or automatic title for display.
Definition: info.cpp:123
std::string id
Definition: info.hpp:31
ADDON_TYPE type
Definition: info.hpp:45
Modification of the game for MP.
Definition: validation.hpp:52
ADDON_TYPE get_addon_type(const std::string &str)
Definition: validation.cpp:211
std::string str() const
Serializes the version number into string form.
Definition: version.cpp:92
Miscellaneous content/media (unit packs, terrain packs, music packs, etc.).
Definition: validation.hpp:54
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:236
std::string display_icon() const
Get an icon path fixed for display (e.g.
Definition: info.cpp:132
void read(const config &cfg)
Definition: info.cpp:61
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
std::vector< std::string > locales
Definition: info.hpp:47
Multiplayer scenario.
Definition: validation.hpp:47
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:30
Multiplayer era.
Definition: validation.hpp:49
std::string icon
Definition: info.hpp:35
std::string display_type() const
Get an add-on type identifier for display in the user's language.
Definition: info.cpp:149
int uploads
Definition: info.hpp:43
std::string size_display_string(double size)
Get a human-readable representation of the specified byte count.
Definition: info.cpp:214
Multiplayer plain (no WML) map pack.
Definition: validation.hpp:48
config & add_child(config_key_type key)
Definition: config.cpp:408
std::string make_addon_title(const std::string &id)
Replaces underscores to dress up file or dirnames as add-on titles.
Definition: info.cpp:223
void write(config &cfg) const
Definition: info.cpp:88
std::vector< std::string > depends
Definition: info.hpp:51
Hybrid campaign.
Definition: validation.hpp:45
std::string title
Definition: info.hpp:32
Standard logging facilities (interface).
version_info version
Definition: info.hpp:37
std::string get_addon_type_string(ADDON_TYPE type)
Definition: validation.cpp:227
int downloads
Definition: info.hpp:42
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
std::map< std::string, addon_info > addons_list
Definition: info.hpp:26
Single-player campaign.
Definition: validation.hpp:43
std::set< std::string > resolve_dependencies(const addons_list &addons) const
Resolve an add-on's dependency tree in a recursive fashion.
Definition: info.cpp:181
Multiplayer campaign.
Definition: validation.hpp:46
std::string author
Definition: info.hpp:39
time_t updated
Definition: info.hpp:56
std::string core
Definition: info.hpp:49