The Battle for Wesnoth  1.19.10+dev
depcheck.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2025
3  by Boldizsár Lipka <lipkab@zoho.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 <string>
19 #include <vector>
20 #include "config.hpp"
21 #include "gettext.hpp"
22 
23 class game_config_view;
24 
25 namespace ng
26 {
27 
28 namespace depcheck
29 {
30 
32 {
33  ERA,
36 };
37 
38 /**
39  * Note to all triers:
40  * It's not guaranteed that the specified component will be selected
41  * (if the user denies to perform dependency resolution, all changes
42  * will be reverted). Consequently, it's essential to check the
43  * selected values after calling any trier.
44  *
45  * Note to ctor & insert_element:
46  * Please note that the ctor collects data for scenario elements from
47  * "multiplayer" nodes, while insert_element from "scenario" nodes.
48  */
49 class manager
50 {
51 public:
52  manager(const game_config_view& gamecfg, bool mp);
53 
54  /**
55  * Tries to set the selected era
56  *
57  * @param id the id of the era
58  * @param force whether to skip dependency check
59  */
60  void try_era(const std::string& id, bool force = false);
61 
62  /**
63  * Tries to set the selected scenario
64  *
65  * @param id the id of the scenario
66  * @param force whether to skip dependency check
67  */
68  void try_scenario(const std::string& id, bool force = false);
69 
70  /**
71  * Tries to set the enabled modifications
72  *
73  * @param ids the ids of the modifications
74  * @param force whether to skip dependency check
75  */
76  void try_modifications(const std::vector<std::string>& ids,
77  bool force = false );
78 
79  /**
80  * Tries to enable/disable a specific modification
81  *
82  * @param id the id of the modification
83  * @param activate activate or deactivate
84  * @param force whether to skip dependency check
85  */
86  void try_modification_by_id(const std::string& id, bool activate, bool force = false);
87 
88  /**
89  * Tries to set the selected era
90  *
91  * @param index the index of the era
92  * @param force whether to skip dependency check
93  */
94  void try_era_by_index(int index, bool force = false);
95 
96  /**
97  * Tries to set the selected scenario
98  *
99  * @param index the index of the scenario
100  * @param force whether to skip dependency check
101  */
102  void try_scenario_by_index(int index, bool force = false);
103 
104  /**
105  * Returns the selected era
106  *
107  * @return the id of the era
108  */
109  const std::string& get_era() const { return era_; }
110 
111  /**
112  * Returns the selected scenario
113  *
114  * @return the id of the scenario
115  */
116  const std::string& get_scenario() const { return scenario_; }
117 
118  /**
119  * Returns the enabled modifications
120  *
121  * @return the ids of the modifications
122  */
123  const std::vector<std::string>& get_modifications() const { return mods_; }
124 
125  /**
126  * Tells whether a certain mod is activated.
127  *
128  * @param index the index of the mod
129  *
130  * @return true if activated, false is not
131  */
132  bool is_modification_active(int index) const;
133 
134  /**
135  * Tells whether a certain mod is activated.
136  *
137  * @param id the string id of the mod
138  *
139  * @return true if activated, false is not
140  */
141  bool is_modification_active(const std::string& id) const;
142 
143  /**
144  * Returns the selected era
145  *
146  * @return the index of the era
147  */
148  int get_era_index() const;
149  int get_era_index(const std::string& id) const;
150 
151  /**
152  * Returns the selected scenario
153  *
154  * @return the index of the scenario
155  */
156  int get_scenario_index() const;
157 
158  /**
159  * Adds a new element to the manager's database
160  *
161  * @param type the type of the element
162  * @param data a config object containing the dependency info for the
163  * element
164  * @param index where to insert the element
165  */
166  void insert_element(component_type type, const config& data, int index = 0);
167 
168 private:
169 
170  /** represents a component (era, modification or scenario)*/
171  struct elem {
172  elem(const std::string& _id, const std::string& _type)
173  : id(_id)
174  , type(_type)
175  {}
176 
177  std::string id;
178  std::string type;
179 
180  bool operator ==(const elem& e) const
181  { return id == e.id && type == e.type; }
182 
183  bool operator !=(const elem& e) const { return !(*this == e); }
184  };
185 
186  /** holds all required info about the components and their dependencies */
188 
189  /** the id of the currently selected era */
190  std::string era_;
191 
192  /** the id of the currently selected scenario */
193  std::string scenario_;
194 
195  /** the ids of the currently selected modifications */
196  std::vector<std::string> mods_;
197 
198  /** used by save_state() and revert() to backup/restore era_ */
199  std::string prev_era_;
200 
201  /** used by save_state() and revert() to backup/restore scenario_ */
202  std::string prev_scenario_;
203 
204  /** used by save_state() and revert() to backup/restore mods_ */
205  std::vector<std::string> prev_mods_;
206 
207  /** saves the current values of era_, scenarios_ and mods_ */
208  void save_state();
209 
210  /** restores the lastly saved values of era_, scenarios_ and mods_ */
211  void revert();
212 
213  /**
214  * Attempts to change the selected scenario.
215  *
216  * @param id the scenario's id
217  * @return true if the selection was changed; false if not
218  */
219  bool change_scenario(const std::string& id);
220 
221  /**
222  * Attempts to change the selected era.
223  *
224  * @param id the era's id
225  * @return true if the selection was changed; false if not
226  */
227  bool change_era(const std::string& id);
228 
229  /**
230  * Attempts to change the selected modifications.
231  *
232  * @param modifications the list of the modifications' ids
233  * @return true if the selection was changed; false if not
234  */
235  bool change_modifications(const std::vector<std::string>& modifications);
236 
237  /**
238  * Decides if two components are conflicting or not
239  *
240  * @param elem1 the first component
241  * @param elem2 the second component
242  * @param directonly whether the function should ignore any possible
243  * conflicts between the components' dependencies.
244  *
245  * @return true if e1 and e2 conflict, false if not
246  */
247  bool does_conflict(const elem& elem1, const elem& elem2, bool directonly=false) const;
248 
249  /**
250  * Decides whether e1 requires e2
251  *
252  * @param elem1 a component; by definition, passing a modification here
253  * makes no sense
254  * @param elem2 another component; by definition, passing anything else
255  * than a modification here makes no sense
256  *
257  * @return true if e2 is required by e1, false if not
258  */
259  bool does_require(const elem& elem1, const elem& elem2) const;
260 
261  /**
262  * Get the list of modifications required by a certain component
263  *
264  * @param e the component
265  *
266  * @return the list of the modifications' ids
267  */
268  std::vector<std::string> get_required(const elem& e) const;
269 
270  /**
271  * Get the list of modifications which are required by a certain
272  * component, but aren't currently enabled
273  *
274  * @param e the component
275  *
276  * @return the list of the modifications' ids
277  */
278  std::vector<std::string> get_required_not_enabled(const elem& e) const;
279 
280  /**
281  * Get the list of modifications which are conflicting a certain
282  * component and are currently enabled
283  *
284  * @param e the component
285  *
286  * @return the list of the modifications' ids
287  */
288  std::vector<std::string> get_conflicting_enabled(const elem& e) const;
289 
290  /**
291  * Get the list of modifications which are required by a certain
292  * component, but currently unavailable on the computer
293  *
294  * @param e the component
295  *
296  * @return the list of the modifications' ids
297  */
298  std::vector<std::string> get_required_not_installed(const elem& e) const;
299 
300  /**
301  * Display a dialog requesting confirmation for enabling some
302  * modifications
303  *
304  * @param mods the list of modifications to be enabled
305  * @param requester the add-on's name which requests the action to be done
306  *
307  * @return true, if the user accepted the change, false if not
308  */
309  bool enable_mods_dialog(const std::vector<std::string>& mods,
310  const std::string& requester = _("A component"));
311 
312  /**
313  * Display a dialog requesting confirmation for disabling some
314  * modifications
315  *
316  * @param mods the list of modifications to be disabled
317  * @param requester the add-on's name which requests the action to be done
318  *
319  * @return true, if the user accepted the change, false if not
320  */
321  bool disable_mods_dialog(const std::vector<std::string>& mods,
322  const std::string& requester = _("A component"));
323 
324  /**
325  * Display a dialog requesting the user to select a new era
326  *
327  * @param eras the possible options (ids)
328  *
329  * @return the selected era's id or empty string if the user
330  * refused to select any
331  */
332  std::string change_era_dialog(const std::vector<std::string>& eras);
333 
334  /**
335  * Display a dialog requesting the user to select a new scenario
336  *
337  * @param scenarios the possible options (ids)
338  *
339  * @return the selected scenario's id or empty string if the user
340  * refused to select any
341  */
342  std::string change_scenario_dialog
343  (const std::vector<std::string>& scenarios);
344 
345  /**
346  * Shows an error message
347  *
348  * @param msg the message to be displayed
349  */
350  void failure_dialog(const std::string& msg);
351 
352  /**
353  * Decides whether a certain component is installed or not
354  *
355  * @param e the component
356  *
357  * @return true if the component exists false if not
358  */
359  bool exists(const elem& e) const;
360 
361  /**
362  * Look up the name of a given component.
363  *
364  * @param e the component
365  *
366  * @return the name of the component
367  */
368  std::string find_name_for(const elem& e) const;
369 
370 };
371 
372 } //namespace depcheck
373 
374 } //namespace ng
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
A class grating read only view to a vector of config objects, viewed as one config with all children ...
Note to all triers: It's not guaranteed that the specified component will be selected (if the user de...
Definition: depcheck.hpp:50
bool change_scenario(const std::string &id)
Attempts to change the selected scenario.
Definition: depcheck.cpp:526
void try_era_by_index(int index, bool force=false)
Tries to set the selected era.
Definition: depcheck.cpp:388
bool change_modifications(const std::vector< std::string > &modifications)
Attempts to change the selected modifications.
Definition: depcheck.cpp:663
void revert()
restores the lastly saved values of era_, scenarios_ and mods_
Definition: depcheck.cpp:137
void try_modifications(const std::vector< std::string > &ids, bool force=false)
Tries to set the enabled modifications.
Definition: depcheck.cpp:359
bool disable_mods_dialog(const std::vector< std::string > &mods, const std::string &requester=_("A component"))
Display a dialog requesting confirmation for disabling some modifications.
Definition: depcheck.cpp:462
bool exists(const elem &e) const
Decides whether a certain component is installed or not.
Definition: depcheck.cpp:145
void insert_element(component_type type, const config &data, int index=0)
Adds a new element to the manager's database.
Definition: depcheck.cpp:508
std::string change_era_dialog(const std::vector< std::string > &eras)
Display a dialog requesting the user to select a new era.
Definition: depcheck.cpp:472
void try_era(const std::string &id, bool force=false)
Tries to set the selected era.
Definition: depcheck.cpp:337
bool enable_mods_dialog(const std::vector< std::string > &mods, const std::string &requester=_("A component"))
Display a dialog requesting confirmation for enabling some modifications.
Definition: depcheck.cpp:452
bool does_conflict(const elem &elem1, const elem &elem2, bool directonly=false) const
Decides if two components are conflicting or not.
Definition: depcheck.cpp:221
config depinfo_
holds all required info about the components and their dependencies
Definition: depcheck.hpp:187
std::vector< std::string > get_required(const elem &e) const
Get the list of modifications required by a certain component.
Definition: depcheck.cpp:177
std::string prev_era_
used by save_state() and revert() to backup/restore era_
Definition: depcheck.hpp:199
std::vector< std::string > get_conflicting_enabled(const elem &e) const
Get the list of modifications which are conflicting a certain component and are currently enabled.
Definition: depcheck.cpp:208
const std::string & get_era() const
Returns the selected era.
Definition: depcheck.hpp:109
std::string find_name_for(const elem &e) const
Look up the name of a given component.
Definition: depcheck.cpp:156
const std::string & get_scenario() const
Returns the selected scenario.
Definition: depcheck.hpp:116
bool does_require(const elem &elem1, const elem &elem2) const
Decides whether e1 requires e2.
Definition: depcheck.cpp:320
std::vector< std::string > mods_
the ids of the currently selected modifications
Definition: depcheck.hpp:196
int get_era_index() const
Returns the selected era.
Definition: depcheck.cpp:398
std::string scenario_
the id of the currently selected scenario
Definition: depcheck.hpp:193
std::vector< std::string > get_required_not_installed(const elem &e) const
Get the list of modifications which are required by a certain component, but currently unavailable on...
Definition: depcheck.cpp:162
std::string change_scenario_dialog(const std::vector< std::string > &scenarios)
Display a dialog requesting the user to select a new scenario.
Definition: depcheck.cpp:488
bool is_modification_active(int index) const
Tells whether a certain mod is activated.
Definition: depcheck.cpp:441
std::string era_
the id of the currently selected era
Definition: depcheck.hpp:190
void try_scenario(const std::string &id, bool force=false)
Tries to set the selected scenario.
Definition: depcheck.cpp:348
const std::vector< std::string > & get_modifications() const
Returns the enabled modifications.
Definition: depcheck.hpp:123
void try_scenario_by_index(int index, bool force=false)
Tries to set the selected scenario.
Definition: depcheck.cpp:393
manager(const game_config_view &gamecfg, bool mp)
Definition: depcheck.cpp:61
void failure_dialog(const std::string &msg)
Shows an error message.
Definition: depcheck.cpp:503
bool change_era(const std::string &id)
Attempts to change the selected era.
Definition: depcheck.cpp:595
void save_state()
saves the current values of era_, scenarios_ and mods_
Definition: depcheck.cpp:129
std::string prev_scenario_
used by save_state() and revert() to backup/restore scenario_
Definition: depcheck.hpp:202
int get_scenario_index() const
Returns the selected scenario.
Definition: depcheck.cpp:426
std::vector< std::string > get_required_not_enabled(const elem &e) const
Get the list of modifications which are required by a certain component, but aren't currently enabled...
Definition: depcheck.cpp:194
void try_modification_by_id(const std::string &id, bool activate, bool force=false)
Tries to enable/disable a specific modification.
Definition: depcheck.cpp:370
std::vector< std::string > prev_mods_
used by save_state() and revert() to backup/restore mods_
Definition: depcheck.hpp:205
Definitions for the interface to Wesnoth Markup Language (WML).
static std::string _(const char *str)
Definition: gettext.hpp:97
Main entry points of multiplayer mode.
Definition: lobby_data.cpp:49
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
std::string_view data
Definition: picture.cpp:178
represents a component (era, modification or scenario)
Definition: depcheck.hpp:171
elem(const std::string &_id, const std::string &_type)
Definition: depcheck.hpp:172
bool operator!=(const elem &e) const
Definition: depcheck.hpp:183
bool operator==(const elem &e) const
Definition: depcheck.hpp:180
#define e