The Battle for Wesnoth  1.13.4+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 - 2016 by Boldizsár Lipka <[email protected]>
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 #ifndef MP_DEPCHECK_HPP_INCLUDED
16 #define MP_DEPCHECK_HPP_INCLUDED
17 
18 #include <string>
19 #include <vector>
20 #include "config.hpp"
21 #include "gettext.hpp"
22 #include "utils/make_enum.hpp"
23 
24 class CVideo;
25 
26 namespace ng
27 {
28 
29 namespace depcheck
30 {
31 
33 {
34  ERA,
37 };
38 
39 MAKE_ENUM(component_availabilty,
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 config& gamecfg, bool mp, CVideo& video);
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  * Returns the selected era
142  *
143  * @return the index of the era
144  */
145  int get_era_index() const;
146 
147  /**
148  * Returns the selected scenario
149  *
150  * @return the index of the scenario
151  */
152  int get_scenario_index() const;
153 
154  /**
155  * Adds a new element to the manager's database
156  *
157  * @param type the type of the element
158  * @param data a config object containing the dependency info for the
159  * element
160  * @param index where to insert the element
161  */
162  void insert_element(component_type type, const config& data, int index = 0);
163 
164 private:
165 
166  /** represents a component (era, modification or scenario)*/
167  struct elem {
168  elem(const std::string& _id, const std::string& _type)
169  : id(_id)
170  , type(_type)
171  {}
172 
173  std::string id;
175 
176  bool operator ==(const elem& e) const
177  { return id == e.id && type == e.type; }
178 
179  bool operator !=(const elem& e) const { return !(*this == e); }
180  };
181 
182  /** the screen to display dialogs on */
183  CVideo& video_;
184 
185  /** holds all required info about the components and their dependencies */
186  config depinfo_;
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 conflicts(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 requires(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
374 
375 #endif
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
Definition: video.hpp:58
bool operator!=(const config &a, const config &b)
Definition: config.hpp:79
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
Definitions for the interface to Wesnoth Markup Language (WML).
MAKE_ENUM(component_availabilty,(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:39
This module controls the multiplayer lobby.
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: image.cpp:1187
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
GLuint id
Definition: glew.h:1647
bool operator==(const config &a, const config &b)
Definition: config.cpp:1527
GLuint index
Definition: glew.h:1782
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
const std::vector< std::string > & modifications(bool mp)
#define e
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
const GLuint * ids
Definition: glew.h:1652
Defines the MAKE_ENUM macro.
GLsizei const GLcharARB ** string
Definition: glew.h:4503