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