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