The Battle for Wesnoth  1.15.13+dev
info.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 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 #pragma once
16 
17 #include "game_version.hpp"
18 
19 #include "addon/validation.hpp"
20 
21 #include <ctime>
22 #include <set>
23 #include <map>
24 
26 struct addon_info;
27 class config;
28 typedef std::map<std::string, addon_info> addons_list;
29 
31 {
33 
34  bool supported;
35  std::string title;
36  std::string description;
37 
39  : supported(true)
40  , title()
41  , description()
42  {}
43 
44  addon_info_translation(bool sup, std::string titl, std::string desc)
45  : supported(sup)
46  , title(titl)
47  , description(desc)
48  {
49  }
50 
51  explicit addon_info_translation(const config& cfg)
52  : supported(true)
53  , title()
54  , description()
55  {
56  this->read(cfg);
57  }
58 
60 
62 
63  void read(const config& cfg);
64 
65  void write(config& cfg) const;
66 
67  bool valid()
68  {
69  return !title.empty();
70  }
71 };
72 
73 struct addon_info
74 {
75  std::string id;
76  std::string title;
77  std::string description;
78 
79  std::string icon;
80 
82  std::set<version_info, std::greater<version_info>> versions;
83 
84  std::string author;
85 
86  int size;
87  int downloads;
88  int uploads;
89 
91 
92  std::vector<std::string> tags;
93  std::vector<std::string> locales;
94 
95  std::string core;
96 
97  std::vector<std::string> depends;
98  // std::vector<addon_dependency> conflicts, recommends, replaces;
99 
100  std::string feedback_url;
101 
102  std::time_t updated;
103  std::time_t created;
104 
105  // Flag to indicate whether this object was generaled from pbl info for an addon
106  // not previously published.
108 
109  std::map<std::string, addon_info_translation> info_translations;
110 
112  : id(), title(), description(), icon()
113  , current_version(), versions(), author(), size()
114  , downloads(), uploads(), type(), tags(), locales()
115  , core()
116  , depends()
117  , feedback_url()
118  , updated()
119  , created()
120  , local_only(false)
121  , info_translations()
122  {}
123 
124  explicit addon_info(const config& cfg)
125  : id(), title(), description(), icon()
126  , current_version(), versions(), author(), size()
127  , downloads(), uploads(), type(), tags(), locales()
128  , core()
129  , depends()
130  , feedback_url()
131  , updated()
132  , created()
133  , local_only(false)
134  , info_translations()
135  {
136  this->read(cfg);
137  }
138 
139  addon_info(const addon_info&) = default;
140 
141  addon_info& operator=(const addon_info& o) = default;
142 
143  void read(const config& cfg);
144 
145  void write(config& cfg) const;
146 
147  /**
148  * Write only minimal WML used for state tracking (_info.cfg) files.
149  *
150  * This currently only includes the add-on type, upload count,
151  * title, and version number.
152  *
153  * @param cfg Target WML config object.
154  */
155  void write_minimal(config& cfg) const;
156 
157  /**
158  * Get a title or automatic title for display.
159  *
160  * If the real @a title is empty, the returned value is the @a id with
161  * underscores replaced with blanks.
162  *
163  * @todo FIXME: Is it even possible for the add-ons server to provide untitled
164  * add-ons in its reply anymore? Titles seem to be required at upload time.
165  */
166  std::string display_title() const;
167 
168  addon_info_translation translated_info() const;
169 
170  std::string display_title_translated() const;
171 
172  std::string display_title_translated_or_original() const;
173 
174  std::string display_title_full() const;
175 
176  std::string description_translated() const;
177 
178  /** Get an icon path fixed for display (e.g. when TC is missing, or the image doesn't exist). */
179  std::string display_icon() const;
180 
181  /** Get an add-on type identifier for display in the user's language. */
182  std::string display_type() const;
183 
184  /**
185  * Resolve an add-on's dependency tree in a recursive fashion.
186  *
187  * The returned vector contains the list of resolved dependencies for this
188  * and any other add-ons upon which it depends.
189  *
190  * @param addons The add-ons list.
191  *
192  * @todo Tag resolved dependencies with information about where they come from,
193  * and implement more dependency tiers.
194  */
195  std::set<std::string> resolve_dependencies(const addons_list& addons) const;
196 };
197 
198 /**
199  * Parse the specified add-ons list WML into an actual addons_list object.
200  *
201  * @param cfg Add-ons list WML, currently a [campaigns] node from a server response.
202  * @param dest Target addons_list object. It will be cleared first.
203  */
204 void read_addons_list(const config& cfg, addons_list& dest);
205 
206 /**
207  * Get a human-readable representation of the specified byte count.
208  *
209  * The result includes the size unit, which is the largest byte multiply
210  * that makes sense. (e.g. 1 MiB for 1048576 bytes.)
211  */
212 std::string size_display_string(double size);
213 
214 /**
215  * Replaces underscores to dress up file or dirnames as add-on titles.
216  *
217  * @todo In the future we should store more local information about add-ons and use
218  * this only as a fallback; it could be desirable to fetch translated names as well
219  * somehow.
220  */
221 std::string make_addon_title(const std::string& id);
int size
Definition: info.hpp:86
ADDON_TYPE
Values used for add-on classification; UI-only at the moment, in the future it could be used for dire...
Definition: validation.hpp:97
Interfaces for manipulating version numbers of engine, add-ons, etc.
std::string size_display_string(double size)
Get a human-readable representation of the specified byte count.
Definition: info.cpp:310
std::string description
Definition: info.hpp:77
addon_info(const config &cfg)
Definition: info.hpp:124
std::vector< std::string > tags
Definition: info.hpp:92
std::string make_addon_title(const std::string &id)
Replaces underscores to dress up file or dirnames as add-on titles.
Definition: info.cpp:319
addon_info_translation & operator=(const addon_info_translation &o)=default
std::map< std::string, addon_info_translation > info_translations
Definition: info.hpp:109
std::string title
Definition: info.hpp:35
std::string feedback_url
Definition: info.hpp:100
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
static addon_info_translation invalid
Definition: info.hpp:32
addon_info_translation(const config &cfg)
Definition: info.hpp:51
std::string description
Definition: info.hpp:36
std::string id
Definition: info.hpp:75
addon_info()
Definition: info.hpp:111
ADDON_TYPE type
Definition: info.hpp:90
bool local_only
Definition: info.hpp:107
std::time_t created
Definition: info.hpp:103
std::vector< std::string > locales
Definition: info.hpp:93
std::string icon
Definition: info.hpp:79
int uploads
Definition: info.hpp:88
std::time_t updated
Definition: info.hpp:102
std::set< version_info, std::greater< version_info > > versions
Definition: info.hpp:82
Represents version numbers.
std::vector< std::string > depends
Definition: info.hpp:97
void read(const config &cfg)
Definition: info.cpp:60
std::string title
Definition: info.hpp:76
int downloads
Definition: info.hpp:87
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
version_info current_version
Definition: info.hpp:81
void write(config &cfg) const
Definition: info.cpp:67
std::map< std::string, addon_info > addons_list
Definition: info.hpp:27
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:293
std::string author
Definition: info.hpp:84
addon_info_translation(bool sup, std::string titl, std::string desc)
Definition: info.hpp:44
std::string core
Definition: info.hpp:95