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