The Battle for Wesnoth  1.19.5+dev
info.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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 <chrono>
23 #include <set>
24 #include <map>
25 
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::chrono::system_clock::time_point updated;
103  std::chrono::system_clock::time_point 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)
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)
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  std::string display_title() const;
164 
166 
167  std::string display_title_translated() const;
168 
169  std::string display_title_translated_or_original() const;
170 
171  std::string display_title_full() const;
172 
173  std::string description_translated() const;
174 
175  /** Get an icon path fixed for display (e.g. when TC is missing, or the image doesn't exist). */
176  std::string display_icon() const;
177 
178  /** Get an add-on type identifier for display in the user's language. */
179  std::string display_type() const;
180 
181  /**
182  * Resolve an add-on's dependency tree in a recursive fashion.
183  *
184  * The returned vector contains the list of resolved dependencies for this
185  * and any other add-ons upon which it depends.
186  *
187  * @param addons The add-ons list.
188  *
189  * @todo Tag resolved dependencies with information about where they come from,
190  * and implement more dependency tiers.
191  */
192  std::set<std::string> resolve_dependencies(const addons_list& addons) const;
193 };
194 
195 /**
196  * Parse the specified add-ons list WML into an actual addons_list object.
197  *
198  * @param cfg Add-ons list WML, currently a [campaigns] node from a server response.
199  * @param dest Target addons_list object. It will be cleared first.
200  */
201 void read_addons_list(const config& cfg, addons_list& dest);
202 
203 /**
204  * Get a human-readable representation of the specified byte count.
205  *
206  * The result includes the size unit, which is the largest byte multiply
207  * that makes sense. (e.g. 1 MiB for 1048576 bytes.)
208  */
209 std::string size_display_string(double size);
210 
211 /**
212  * Replaces underscores to dress up file or dirnames as add-on titles.
213  *
214  * @todo In the future we should store more local information about add-ons and use
215  * this only as a fallback; it could be desirable to fetch translated names as well
216  * somehow.
217  */
218 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:172
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: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:294
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
addon_info_translation(bool sup, std::string titl, std::string desc)
Definition: info.hpp:44
std::string title
Definition: info.hpp:35
std::string description
Definition: info.hpp:36
addon_info_translation(const addon_info_translation &)=default
static addon_info_translation invalid
Definition: info.hpp:32
void read(const config &cfg)
Definition: info.cpp:61
addon_info_translation(const config &cfg)
Definition: info.hpp:51
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:92
void read(const config &cfg)
Definition: info.cpp:75
addon_info(const addon_info &)=default
version_info current_version
Definition: info.hpp:81
int uploads
Definition: info.hpp:88
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:109
addon_info_translation translated_info() const
Definition: info.cpp:166
int downloads
Definition: info.hpp:87
std::string icon
Definition: info.hpp:79
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:111
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
std::chrono::system_clock::time_point updated
Definition: info.hpp:102
bool local_only
Definition: info.hpp:107
std::string display_title() const
Get a title or automatic title for display.
Definition: info.cpp:155
std::string title
Definition: info.hpp:76
std::string display_title_translated_or_original() const
Definition: info.cpp:207
std::chrono::system_clock::time_point created
Definition: info.hpp:103
std::vector< std::string > depends
Definition: info.hpp:97
std::string feedback_url
Definition: info.hpp:100
std::string author
Definition: info.hpp:84
std::string core
Definition: info.hpp:95
std::string description
Definition: info.hpp:77
addon_info(const config &cfg)
Definition: info.hpp:124
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:82
int size
Definition: info.hpp:86
std::vector< std::string > locales
Definition: info.hpp:93
std::string id
Definition: info.hpp:75
ADDON_TYPE type
Definition: info.hpp:90
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