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