The Battle for Wesnoth  1.13.11+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 - 2018 by Iris 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  this->local_only = cfg["local_only"].to_bool();
88 }
89 
90 void addon_info::write(config& cfg) const
91 {
92  cfg["id"] = this->id;
93  cfg["title"] = this->title;
94  cfg["description"] = this->description;
95  cfg["icon"] = this->icon;
96  cfg["version"] = this->version.str();
97  cfg["author"] = this->author;
98  cfg["size"] = this->size;
99  cfg["downloads"] = this->downloads;
100  cfg["uploads"] = this->uploads;
101  cfg["type"] = get_addon_type_string(this->type);
102 
103  for (const std::string& locale_id : this->locales) {
104  cfg.add_child("translation")["language"] = locale_id;
105  }
106 
107  cfg["core"] = this->core;
108  cfg["dependencies"] = utils::join(this->depends);
109  cfg["feedback_url"] = this->feedback_url;
110 
111  cfg["timestamp"] = this->updated;
112  cfg["original_timestamp"] = this->created;
113 }
114 
116 {
117  cfg["version"] = this->version.str();
118  cfg["uploads"] = this->uploads;
119  cfg["type"] = get_addon_type_string(this->type);
120  cfg["title"] = this->title;
121  cfg["dependencies"] = utils::join(this->depends);
122  cfg["core"] = this->core;
123 }
124 
126 {
127  if(this->title.empty()) {
128  return font::escape_text(make_addon_title(this->id));
129  } else {
130  return font::escape_text(this->title);
131  }
132 }
133 
135 {
136  std::string ret = icon;
137 
138  if(ret.empty()) {
139  ERR_AC << "add-on '" << id << "' doesn't have an icon path set" << std::endl;
140  } else if(!image::exists(ret)) {
141  ERR_AC << "add-on '" << id << "' has an icon which cannot be found: '" << ret << "'" << std::endl;
142  } else if(ret.find("units/") != std::string::npos && ret.find_first_of('~') == std::string::npos) {
143  // HACK: prevent magenta icons, because they look awful
144  LOG_AC << "add-on '" << id << "' uses a unit baseframe as icon without TC/RC specifications\n";
145  ret += "~RC(magenta>red)";
146  }
147 
148  return ret;
149 }
150 
152 {
153  switch (type) {
154  case ADDON_SP_CAMPAIGN:
155  return _("addon_type^Campaign");
156  case ADDON_SP_SCENARIO:
157  return _("addon_type^Scenario");
159  return _("addon_type^SP/MP Campaign");
160  case ADDON_MP_ERA:
161  return _("addon_type^MP era");
162  case ADDON_MP_FACTION:
163  return _("addon_type^MP faction");
164  case ADDON_MP_MAPS:
165  return _("addon_type^MP map-pack");
166  case ADDON_MP_SCENARIO:
167  return _("addon_type^MP scenario");
168  case ADDON_MP_CAMPAIGN:
169  return _("addon_type^MP campaign");
170  case ADDON_MOD:
171  return _("addon_type^Modification");
172  case ADDON_CORE:
173  return _("addon_type^Core");
174  case ADDON_MEDIA:
175  return _("addon_type^Resources");
176  case ADDON_OTHER:
177  return _("addon_type^Other");
178  default:
179  return _("addon_type^(unknown)");
180  }
181 }
182 
183 std::set<std::string> addon_info::resolve_dependencies(const addons_list& addons) const
184 {
185  std::set<std::string> deps;
186  resolve_deps_recursive(addons, this->id, deps);
187 
188  if(deps.find(this->id) != deps.end()) {
189  LOG_AC << this->id << " depends upon itself; breaking circular dependency\n";
190  deps.erase(this->id);
191  }
192 
193  return deps;
194 }
195 
196 void read_addons_list(const config& cfg, addons_list& dest)
197 {
198  dest.clear();
199 
200  unsigned order = 0;
201 
202  /** @todo FIXME: get rid of this legacy "campaign"/"campaigns" silliness
203  */
204  const config::const_child_itors &addon_cfgs = cfg.child_range("campaign");
205  for(const config& addon_cfg : addon_cfgs) {
206  const std::string& id = addon_cfg["name"].str();
207  if(dest.find(id) != dest.end()) {
208  ERR_AC << "add-ons list has multiple entries for '" << id << "', not good; ignoring them" << std::endl;
209  continue;
210  }
211  dest[id].read(addon_cfg);
212  dest[id].order = order++;
213  }
214 }
215 
217 {
218  if(size > 0.0) {
219  return utils::si_string(size, true, _("unit_byte^B"));
220  } else {
221  return "";
222  }
223 }
224 
226 {
227  std::string ret(id);
228  std::replace(ret.begin(), ret.end(), '_', ' ');
229  return ret;
230 }
int size
Definition: info.hpp:41
void read_addons_list(const config &cfg, addons_list &dest)
Definition: info.cpp:196
Modification of the game.
Definition: validation.hpp:52
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:362
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:115
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: image.cpp:1129
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:125
std::string id
Definition: info.hpp:31
ADDON_TYPE type
Definition: info.hpp:45
ADDON_TYPE get_addon_type(const std::string &str)
Definition: validation.cpp:235
std::string str() const
Serializes the version number into string form.
Definition: version.cpp:93
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:239
std::string display_icon() const
Get an icon path fixed for display (e.g.
Definition: info.cpp:134
void read(const config &cfg)
Definition: info.cpp:61
bool local_only
Definition: info.hpp:66
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:151
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:216
Multiplayer plain (no WML) map pack.
Definition: validation.hpp:48
config & add_child(config_key_type key)
Definition: config.cpp:475
std::string make_addon_title(const std::string &id)
Replaces underscores to dress up file or dirnames as add-on titles.
Definition: info.cpp:225
void write(config &cfg) const
Definition: info.cpp:90
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:251
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:183
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