The Battle for Wesnoth  1.19.7+dev
depcheck.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2024
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 
150  /**
151  * Returns the selected scenario
152  *
153  * @return the index of the scenario
154  */
155  int get_scenario_index() const;
156 
157  /**
158  * Adds a new element to the manager's database
159  *
160  * @param type the type of the element
161  * @param data a config object containing the dependency info for the
162  * element
163  * @param index where to insert the element
164  */
165  void insert_element(component_type type, const config& data, int index = 0);
166 
167 private:
168 
169  /** represents a component (era, modification or scenario)*/
170  struct elem {
171  elem(const std::string& _id, const std::string& _type)
172  : id(_id)
173  , type(_type)
174  {}
175 
176  std::string id;
177  std::string type;
178 
179  bool operator ==(const elem& e) const
180  { return id == e.id && type == e.type; }
181 
182  bool operator !=(const elem& e) const { return !(*this == e); }
183  };
184 
185  /** holds all required info about the components and their dependencies */
187 
188  /** the id of the currently selected era */
189  std::string era_;
190 
191  /** the id of the currently selected scenario */
192  std::string scenario_;
193 
194  /** the ids of the currently selected modifications */
195  std::vector<std::string> mods_;
196 
197  /** used by save_state() and revert() to backup/restore era_ */
198  std::string prev_era_;
199 
200  /** used by save_state() and revert() to backup/restore scenario_ */
201  std::string prev_scenario_;
202 
203  /** used by save_state() and revert() to backup/restore mods_ */
204  std::vector<std::string> prev_mods_;
205 
206  /** saves the current values of era_, scenarios_ and mods_ */
207  void save_state();
208 
209  /** restores the lastly saved values of era_, scenarios_ and mods_ */
210  void revert();
211 
212  /**
213  * Attempts to change the selected scenario.
214  *
215  * @param id the scenario's id
216  * @return true if the selection was changed; false if not
217  */
218  bool change_scenario(const std::string& id);
219 
220  /**
221  * Attempts to change the selected era.
222  *
223  * @param id the era's id
224  * @return true if the selection was changed; false if not
225  */
226  bool change_era(const std::string& id);
227 
228  /**
229  * Attempts to change the selected modifications.
230  *
231  * @param modifications the list of the modifications' ids
232  * @return true if the selection was changed; false if not
233  */
234  bool change_modifications(const std::vector<std::string>& modifications);
235 
236  /**
237  * Decides if two components are conflicting or not
238  *
239  * @param elem1 the first component
240  * @param elem2 the second component
241  * @param directonly whether the function should ignore any possible
242  * conflicts between the components' dependencies.
243  *
244  * @return true if e1 and e2 conflict, false if not
245  */
246  bool does_conflict(const elem& elem1, const elem& elem2, bool directonly=false) const;
247 
248  /**
249  * Decides whether e1 requires e2
250  *
251  * @param elem1 a component; by definition, passing a modification here
252  * makes no sense
253  * @param elem2 another component; by definition, passing anything else
254  * than a modification here makes no sense
255  *
256  * @return true if e2 is required by e1, false if not
257  */
258  bool does_require(const elem& elem1, const elem& elem2) const;
259 
260  /**
261  * Get the list of modifications required by a certain component
262  *
263  * @param e the component
264  *
265  * @return the list of the modifications' ids
266  */
267  std::vector<std::string> get_required(const elem& e) const;
268 
269  /**
270  * Get the list of modifications which are required by a certain
271  * component, but aren't currently enabled
272  *
273  * @param e the component
274  *
275  * @return the list of the modifications' ids
276  */
277  std::vector<std::string> get_required_not_enabled(const elem& e) const;
278 
279  /**
280  * Get the list of modifications which are conflicting a certain
281  * component and are currently enabled
282  *
283  * @param e the component
284  *
285  * @return the list of the modifications' ids
286  */
287  std::vector<std::string> get_conflicting_enabled(const elem& e) const;
288 
289  /**
290  * Get the list of modifications which are required by a certain
291  * component, but currently unavailable on the computer
292  *
293  * @param e the component
294  *
295  * @return the list of the modifications' ids
296  */
297  std::vector<std::string> get_required_not_installed(const elem& e) const;
298 
299  /**
300  * Display a dialog requesting confirmation for enabling some
301  * modifications
302  *
303  * @param mods the list of modifications to be enabled
304  * @param requester the add-on's name which requests the action to be done
305  *
306  * @return true, if the user accepted the change, false if not
307  */
308  bool enable_mods_dialog(const std::vector<std::string>& mods,
309  const std::string& requester = _("A component"));
310 
311  /**
312  * Display a dialog requesting confirmation for disabling some
313  * modifications
314  *
315  * @param mods the list of modifications to be disabled
316  * @param requester the add-on's name which requests the action to be done
317  *
318  * @return true, if the user accepted the change, false if not
319  */
320  bool disable_mods_dialog(const std::vector<std::string>& mods,
321  const std::string& requester = _("A component"));
322 
323  /**
324  * Display a dialog requesting the user to select a new era
325  *
326  * @param eras the possible options (ids)
327  *
328  * @return the selected era's id or empty string if the user
329  * refused to select any
330  */
331  std::string change_era_dialog(const std::vector<std::string>& eras);
332 
333  /**
334  * Display a dialog requesting the user to select a new scenario
335  *
336  * @param scenarios the possible options (ids)
337  *
338  * @return the selected scenario's id or empty string if the user
339  * refused to select any
340  */
341  std::string change_scenario_dialog
342  (const std::vector<std::string>& scenarios);
343 
344  /**
345  * Shows an error message
346  *
347  * @param msg the message to be displayed
348  */
349  void failure_dialog(const std::string& msg);
350 
351  /**
352  * Decides whether a certain component is installed or not
353  *
354  * @param e the component
355  *
356  * @return true if the component exists false if not
357  */
358  bool exists(const elem& e) const;
359 
360  /**
361  * Look up the name of a given component.
362  *
363  * @param e the component
364  *
365  * @return the name of the component
366  */
367  std::string find_name_for(const elem& e) const;
368 
369 };
370 
371 } //namespace depcheck
372 
373 } //namespace ng
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
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:512
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:649
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:448
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:494
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:458
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:438
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:186
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:198
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:195
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:192
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:474
bool is_modification_active(int index) const
Tells whether a certain mod is activated.
Definition: depcheck.cpp:427
std::string era_
the id of the currently selected era
Definition: depcheck.hpp:189
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:489
bool change_era(const std::string &id)
Attempts to change the selected era.
Definition: depcheck.cpp:581
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:201
int get_scenario_index() const
Returns the selected scenario.
Definition: depcheck.cpp:412
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:204
Definitions for the interface to Wesnoth Markup Language (WML).
static std::string _(const char *str)
Definition: gettext.hpp:93
Main entry points of multiplayer mode.
Definition: lobby_data.cpp:50
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:170
elem(const std::string &_id, const std::string &_type)
Definition: depcheck.hpp:171
bool operator!=(const elem &e) const
Definition: depcheck.hpp:182
bool operator==(const elem &e) const
Definition: depcheck.hpp:179
#define e