The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
depcheck.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2017 by Boldizs√°r Lipka <lipkab@zoho.com>
3  Part of the Battle for Wesnoth Project http://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 CVideo;
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 config& gamecfg, bool mp, CVideo& video);
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;
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  /** the screen to display dialogs on */
191  CVideo& video_;
192 
193  /** holds all required info about the components and their dependencies */
194  config depinfo_;
195 
196  /** the id of the currently selected era */
197  std::string era_;
198 
199  /** the id of the currently selected scenario */
200  std::string scenario_;
201 
202  /** the ids of the currently selected modifications */
203  std::vector<std::string> mods_;
204 
205  /** used by save_state() and revert() to backup/restore era_ */
206  std::string prev_era_;
207 
208  /** used by save_state() and revert() to backup/restore scenario_ */
209  std::string prev_scenario_;
210 
211  /** used by save_state() and revert() to backup/restore mods_ */
212  std::vector<std::string> prev_mods_;
213 
214  /** saves the current values of era_, scenarios_ and mods_ */
215  void save_state();
216 
217  /** restores the lastly saved values of era_, scenarios_ and mods_ */
218  void revert();
219 
220  /**
221  * Attempts to change the selected scenario.
222  *
223  * @param id the scenario's id
224  * @return true if the selection was changed; false if not
225  */
226  bool change_scenario(const std::string& id);
227 
228  /**
229  * Attempts to change the selected era.
230  *
231  * @param id the era's id
232  * @return true if the selection was changed; false if not
233  */
234  bool change_era(const std::string& id);
235 
236  /**
237  * Attempts to change the selected modifications.
238  *
239  * @param modifications the list of the modifications' ids
240  * @return true if the selection was changed; false if not
241  */
242  bool change_modifications(const std::vector<std::string>& modifications);
243 
244  /**
245  * Decides if two components are conflicting or not
246  *
247  * @param elem1 the first component
248  * @param elem2 the second component
249  * @param directonly whether the function should ignore any possible
250  * conflicts between the components' dependencies.
251  *
252  * @return true if e1 and e2 conflict, false if not
253  */
254  bool conflicts(const elem& elem1, const elem& elem2, bool directonly=false) const;
255 
256  /**
257  * Decides whether e1 requires e2
258  *
259  * @param elem1 a component; by definition, passing a modification here
260  * makes no sense
261  * @param elem2 another component; by definition, passing anything else
262  * than a modification here makes no sense
263  *
264  * @return true if e2 is required by e1, false if not
265  */
266  bool requires(const elem& elem1, const elem& elem2) const;
267 
268  /**
269  * Get the list of modifications required by a certain component
270  *
271  * @param e the component
272  *
273  * @return the list of the modifications' ids
274  */
275  std::vector<std::string> get_required(const elem& e) const;
276 
277  /**
278  * Get the list of modifications which are required by a certain
279  * component, but aren't currently enabled
280  *
281  * @param e the component
282  *
283  * @return the list of the modifications' ids
284  */
285  std::vector<std::string> get_required_not_enabled(const elem& e) const;
286 
287  /**
288  * Get the list of modifications which are conflicting a certain
289  * component and are currently enabled
290  *
291  * @param e the component
292  *
293  * @return the list of the modifications' ids
294  */
295  std::vector<std::string> get_conflicting_enabled(const elem& e) const;
296 
297  /**
298  * Get the list of modifications which are required by a certain
299  * component, but currently unavailable on the computer
300  *
301  * @param e the component
302  *
303  * @return the list of the modifications' ids
304  */
305  std::vector<std::string> get_required_not_installed(const elem& e) const;
306 
307  /**
308  * Display a dialog requesting confirmation for enabling some
309  * modifications
310  *
311  * @param mods the list of modifications to be enabled
312  * @param requester the add-on's name which requests the action to be done
313  *
314  * @return true, if the user accepted the change, false if not
315  */
316  bool enable_mods_dialog(const std::vector<std::string>& mods,
317  const std::string& requester = _("A component"));
318 
319  /**
320  * Display a dialog requesting confirmation for disabling some
321  * modifications
322  *
323  * @param mods the list of modifications to be disabled
324  * @param requester the add-on's name which requests the action to be done
325  *
326  * @return true, if the user accepted the change, false if not
327  */
328  bool disable_mods_dialog(const std::vector<std::string>& mods,
329  const std::string& requester = _("A component"));
330 
331  /**
332  * Display a dialog requesting the user to select a new era
333  *
334  * @param eras the possible options (ids)
335  *
336  * @return the selected era's id or empty string if the user
337  * refused to select any
338  */
339  std::string change_era_dialog(const std::vector<std::string>& eras);
340 
341  /**
342  * Display a dialog requesting the user to select a new scenario
343  *
344  * @param scenarios the possible options (ids)
345  *
346  * @return the selected scenario's id or empty string if the user
347  * refused to select any
348  */
349  std::string change_scenario_dialog
350  (const std::vector<std::string>& scenarios);
351 
352  /**
353  * Shows an error message
354  *
355  * @param msg the message to be displayed
356  */
357  void failure_dialog(const std::string& msg);
358 
359  /**
360  * Decides whether a certain component is installed or not
361  *
362  * @param e the component
363  *
364  * @return true if the component exists false if not
365  */
366  bool exists(const elem& e) const;
367 
368  /**
369  * Look up the name of a given component.
370  *
371  * @param e the component
372  *
373  * @return the name of the component
374  */
375  std::string find_name_for(const elem& e) const;
376 
377 };
378 
379 } //namespace depcheck
380 
381 } //namespace ng
std::vector< char_t > string
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
Definition: video.hpp:29
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
bool operator!=(const config &a, const config &b)
Definition: config.hpp:89
MAKE_ENUM(component_availability,(SP,"sp")(MP,"mp")(HYBRID,"hybrid")) class manager
Note to all triers: It's not guaranteed that the specified component will be selected (if the user de...
Definition: depcheck.hpp:38
Definitions for the interface to Wesnoth Markup Language (WML).
Pubic entry points for the MP workflow.
Definition: lobby_data.cpp:48
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: image.cpp:1135
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
bool operator==(const config &a, const config &b)
Definition: config.cpp:1245
const std::vector< std::string > & modifications(bool mp)
Definition: game.cpp:730
#define e
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
Defines the MAKE_ENUM macro.