The Battle for Wesnoth  1.19.25+dev
preferences.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2024 - 2025
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 "config.hpp"
19 #include "filesystem.hpp"
20 #include "game_config.hpp"
21 #include "gui/sort_order.hpp"
24 #include "team.hpp"
25 #include "terrain/translation.hpp"
26 #include "lua/wrapper_lua.h" // for lua_State arguments to friend functions
27 #include "log.hpp"
28 #include <set>
29 
30 struct point;
31 class unit_map;
32 class game_board;
33 class terrain_type;
34 
35 namespace pref_constants
36 {
37 const int min_window_width = 1280;
38 const int min_window_height = 720;
39 
40 const int def_window_width = 1920;
41 const int def_window_height = 1080;
42 
43 const int max_window_width = 1920;
44 const int max_window_height = 1080;
45 
46 const int min_font_scaling = 80;
47 const int max_font_scaling = 150;
48 
49 const int min_pixel_scale = 1;
50 const int max_pixel_scale = 4;
51 
52 const int TRANSITION_UPDATE_OFF = 0;
53 const int TRANSITION_UPDATE_ON = 1;
56 
57 const unsigned char CREDENTIAL_SEPARATOR = '\f';
58 const std::string EMPTY_LOGIN = "@@";
59 
60 const int INFINITE_AUTO_SAVES = 61;
61 
62 const std::string default_addons_server = "add-ons.wesnoth.org";
63 
65 
67 };
68 
69 namespace preferences
70 {
72 {
73 public:
75  {
76  }
77 
78  explicit acquaintance(const config& cfg)
79  {
80  nick_ = cfg["nick"].str();
81  status_ = cfg["status"].str();
82  notes_ = cfg["notes"].str();
83  }
84 
85  acquaintance(const std::string& nick, const std::string& status, const std::string& notes)
86  : nick_(nick)
87  , status_(status)
88  , notes_(notes)
89  {
90  }
91 
92  const std::string& get_nick() const { return nick_; }
93  const std::string& get_status() const { return status_; }
94  const std::string& get_notes() const { return notes_; }
95 
96  void save(config& item)
97  {
98  item["nick"] = nick_;
99  item["status"] = status_;
100  item["notes"] = notes_;
101  }
102 
103 private:
104  /** acquaintance's MP nick */
105  std::string nick_;
106 
107  /**status (e.g., "friend", "ignore") */
108  std::string status_;
109 
110  /** notes on the acquaintance */
111  std::string notes_;
112 };
113 
114 class secure_buffer : public std::vector<unsigned char>
115 {
116 public:
117  template<typename... T> secure_buffer(T&&... args)
118  : vector<unsigned char>(std::forward<T>(args)...)
119  {}
121  {
122  std::fill(begin(), end(), '\0');
123  }
124 };
125 
127 {
128  std::string username, server;
130  login_info(const std::string& username, const std::string& server, const secure_buffer& key)
132  {}
133  login_info(const std::string& username, const std::string& server)
135  {}
136  std::size_t size() const
137  {
138  return 3 + username.size() + server.size() + key.size();
139  }
140 };
141 
142 struct option
143 {
144  option(const config& pref)
145  : type()
146  , name(pref["name"].t_str())
147  , description(pref["description"].t_str())
148  , field(pref["field"].str())
149  , cfg(pref)
150  {
151  const std::string p_type = cfg["type"];
152 
153  if(p_type == "boolean") {
155  } else if(p_type == "int") {
157  } else if(p_type == "combo") {
159  } else if(p_type == "custom") {
161  } else {
162  throw std::invalid_argument("Unknown type '" + p_type + "' for advanced preference " + name);
163  }
164  }
165 
166  enum class avd_type { TOGGLE, SLIDER, COMBO, SPECIAL };
167 
168  /** The preference type. */
170 
171  /** Displayed name. */
173 
174  /** Displayed description. */
176 
177  /** The actual field saved in the prefs file and by which this preference may be accessed. */
178  std::string field;
179 
180  /** The full config, including type-specific options. */
182 };
183 };
184 
185 #define PREF_GETTER_SETTER(pref, type, def) \
186 void set_##pref(const type& value) { \
187  preferences_[prefs_list::pref] = value; \
188 } \
189 type pref() const { \
190  return preferences_[prefs_list::pref].to(def); \
191 }
192 
193 class prefs
194 {
196  friend int impl_preferences_set(lua_State* L);
197  friend int impl_preferences_get(lua_State* L);
198 
199 public:
200  static prefs& get()
201  {
202  static prefs prefs_manager;
203  return prefs_manager;
204  }
205 
206  ~prefs();
207 
208  void write_preferences();
209  void load_advanced_prefs(const game_config_view& gc);
210  void migrate_preferences(const std::string& prefs_dir);
211  void set_campaign_rng_mode_default_for_migration(); // TODO: remove after 1.20. (Not used after that. Needs no replacement.)
212  void reload_preferences();
213  std::set<std::string> all_attributes();
214 
215  // Proxies for preferences_dialog
216  void load_hotkeys();
217  void save_hotkeys();
218  void clear_hotkeys();
219 
220  // not actually preferences
221  bool show_fps();
222  void set_show_fps(bool value);
223 
224  bool message_private();
225  void set_message_private(bool value);
226 
227  static void disable_preferences_save() {
228  no_preferences_save = true;
229  }
230 
231  static bool preferences_save() {
232  return no_preferences_save;
233  }
234 
235  // getter-only
236  bool message_bell();
237  bool middle_click_scrolls();
238  /** Gets the threshold for when to scroll. This scrolling happens when the mouse is in the application and near the border. */
244  bool get_show_deprecation(bool def);
245  bool get_scroll_when_mouse_outside(bool def);
247  bool show_combat();
248  std::string get_chat_timestamp(const std::chrono::system_clock::time_point& t);
250  std::vector<std::string>* get_history(const std::string& id);
251  const std::vector<preferences::option>& get_advanced_preferences() { return advanced_prefs_; }
252 
253  // non-trivial getters+setters
254  point resolution();
255  void set_resolution(const point& res);
256 
257  int pixel_scale();
258  void set_pixel_scale(const int scale);
259 
260  bool turbo();
261  void set_turbo(bool ison);
262 
263  int font_scaling();
264  void set_font_scaling(int scale);
265  int font_scaled(int size);
266 
267  int keepalive_timeout();
268  void keepalive_timeout(int seconds);
269 
270  bool sound();
271  bool set_sound(bool ison);
272 
273  unsigned int sample_rate();
274  void save_sample_rate(const unsigned int rate);
275 
276  std::size_t sound_buffer_size();
277  void save_sound_buffer_size(const std::size_t size);
278 
279  int sound_volume();
280  void set_sound_volume(int vol);
281 
282  int bell_volume();
283  void set_bell_volume(int vol);
284 
285  int ui_volume();
286  void set_ui_volume(int vol);
287 
288  bool music_on();
289  bool set_music(bool ison);
290 
291  int music_volume();
292  void set_music_volume(int vol);
293 
294  bool turn_bell();
295  bool set_turn_bell(bool ison);
296 
297  bool ui_sound_on();
298  bool set_ui_sound(bool ison);
299 
300  void add_alias(const std::string& alias, const std::string& command);
302 
303  std::string allied_color();
304  void set_allied_color(const std::string& color_id);
305 
306  std::string enemy_color();
307  void set_enemy_color(const std::string& color_id);
308 
309  std::string unmoved_color();
310  void set_unmoved_color(const std::string& color_id);
311 
312  std::string partial_color();
313  void set_partial_color(const std::string& color_id);
314 
315  std::string moved_color();
316  void set_moved_color(const std::string& color_id);
317 
318  std::string reach_map_color();
319  void set_reach_map_color(const std::string& color_id);
320 
321  std::string reach_map_enemy_color();
322  void set_reach_map_enemy_color(const std::string& color_id);
323 
325  void set_reach_map_border_opacity(const int new_opacity);
326 
328  void set_reach_map_tint_opacity(const int new_opacity);
329 
330  int scroll_speed();
331  void set_scroll_speed(const int scroll);
332 
333  void set_dir_bookmarks(const config& cfg);
335 
336  std::size_t editor_mru_limit();
337  std::vector<std::string> do_read_editor_mru();
338  void do_commit_editor_mru(const std::vector<std::string>& mru);
339  /** Retrieves the list of recently opened files. */
340  std::vector<std::string> recent_files();
341  /** Adds an entry to the recent files list. */
342  void add_recent_files_entry(const std::string& path);
343 
344  bool use_color_cursors();
345  void set_color_cursors(bool value);
346 
348  void set_show_standing_animations(bool value);
349 
351  bool show_theme_dialog();
352 
353  void set_theme(const std::string& theme);
354  std::string theme();
355 
356  void set_mp_server_program_name(const std::string&);
357  std::string get_mp_server_program_name();
358 
359  bool parse_should_show_lobby_join(const std::string& sender, const std::string& message);
362 
363  const std::map<std::string, preferences::acquaintance>& get_acquaintances();
364  const std::string get_ignored_delim();
365  std::map<std::string, std::string> get_acquaintances_nice(const std::string& filter);
366  std::pair<preferences::acquaintance*, bool> add_acquaintance(const std::string& nick, const std::string& mode, const std::string& notes);
367  void add_completed_campaign(const std::string&campaign_id, const std::string& difficulty_level);
368  bool remove_acquaintance(const std::string& nick);
369  bool is_friend(const std::string& nick);
370  bool is_ignored(const std::string& nick);
371  bool is_campaign_completed(const std::string& campaign_id);
372  bool is_campaign_completed(const std::string& campaign_id, const std::string& difficulty_level);
373 
374  void add_game_preset(config&& preset_data);
375  void remove_game_preset(int id);
378 
379  const std::vector<game_config::server_info>& builtin_servers_list();
380  std::vector<game_config::server_info> user_servers_list();
381  void set_user_servers_list(const std::vector<game_config::server_info>& value);
382 
383  std::string network_host();
384  void set_network_host(const std::string& host);
385 
386  std::string campaign_server();
387  void set_campaign_server(const std::string& host);
388 
389  const config& options();
390  void set_options(const config& values);
391 
392  std::chrono::seconds countdown_init_time();
393  void set_countdown_init_time(const std::chrono::seconds& value);
395 
396  std::chrono::seconds countdown_turn_bonus();
397  void set_countdown_turn_bonus(const std::chrono::seconds& value);
399 
400  std::chrono::seconds countdown_reservoir_time();
401  void set_countdown_reservoir_time(const std::chrono::seconds& value);
403 
404  std::chrono::seconds countdown_action_bonus();
405  void set_countdown_action_bonus(const std::chrono::seconds& value);
407 
408  std::chrono::minutes chat_message_aging();
409  void set_chat_message_aging(const std::chrono::minutes& value);
410 
411  int village_gold();
412  void set_village_gold(int value);
413 
414  int village_support();
415  void set_village_support(int value);
416 
417  int xp_modifier();
418  void set_xp_modifier(int value);
419 
420  bool remember_password();
421  void set_remember_password(bool remember);
422 
423  std::string login();
424  void set_login(const std::string& login);
425 
426  std::string password(const std::string& server, const std::string& login);
427  void set_password(const std::string& server, const std::string& login, const std::string& key);
428 
429  const std::vector<std::string>& modifications(bool mp = true);
430  void set_modifications(const std::vector<std::string>& value, bool mp = true);
431 
432  std::set<std::string>& encountered_units();
433  std::set<t_translation::terrain_code>& encountered_terrains();
434 
435  // Ask for end turn confirmation
436  bool yellow_confirm();
437  bool green_confirm();
438  bool confirm_no_moves();
439 
440  // Add all recruitable units as encountered so that information
441  // about them are displayed to the user in the help system.
442  void encounter_recruitable_units(const std::vector<team>& teams);
443 
444  // Add all units that exist at the start to the encountered units so
445  // that information about them are displayed to the user in the help
446  // system.
447  void encounter_start_units(const unit_map& units);
448 
449  // Add all units that are recallable as encountered units.
450  void encounter_recallable_units(const std::vector<team>& teams);
451 
452  // Add all terrains on the map as encountered terrains.
453  void encounter_map_terrain(const gamemap& map);
454 
455  // Mark the given terrain type as encountered.
456  void encounter_map_terrain(const terrain_type& terrain);
457 
458  // Calls all of the above functions on the current game board
459  void encounter_all_content(const game_board& gb);
460 
461  /**
462  * @param content_for The achievement group the achievement is part of.
463  * @param id The ID of the achievement within the achievement group.
464  * @return True if the achievement exists and is completed, false otherwise.
465  */
466  bool achievement(const std::string& content_for, const std::string& id);
467  /**
468  * Marks the specified achievement as completed.
469  *
470  * @param content_for The achievement group the achievement is part of.
471  * @param id The ID of the achievement within the achievement group.
472  */
473  void set_achievement(const std::string& content_for, const std::string& id);
474 
475  /**
476  * Increments the achievement's current progress by @a amount if it hasn't already been completed.
477  * If you only want to check the achievement's current progress, then omit the last three arguments.
478  * @a amount defaults to 0, which will result in the current progress value being returned without being changed (x + 0 == x).
479  *
480  * Note that this uses the same [in_progress] as is used for set_sub_achievement().
481  *
482  * @param content_for The id of the achievement group this achievement is in.
483  * @param id The id for the specific achievement in the achievement group.
484  * @param limit The maximum value that a specific call to this function can increase the achievement progress value.
485  * @param max_progress The value when the achievement is considered completed.
486  * @param amount The amount to progress the achievement.
487  * @return The achievement's current progress, or -1 if it has already been completed.
488  */
489  int progress_achievement(const std::string& content_for, const std::string& id, int limit = 999999, int max_progress = 999999, int amount = 0);
490 
491  /**
492  * @param content_for The achievement group the achievement is part of.
493  * @param id The ID of the achievement within the achievement group.
494  * @param sub_id The ID of the sub-achievement within the achievement.
495  * @return True if the sub-achievement exists and is completed, false otherwise.
496  */
497  bool sub_achievement(const std::string& content_for, const std::string& id, const std::string& sub_id);
498 
499  /**
500  * Marks the specified sub-achievement as completed.
501  *
502  * Note that this uses the same [in_progress] as is used for progress_achievement().
503  *
504  * @param content_for The achievement group the achievement is part of.
505  * @param id The ID of the achievement within the achievement group.
506  * @param sub_id The ID of the sub-achievement within the achievement.
507  */
508  void set_sub_achievement(const std::string& content_for, const std::string& id, const std::string& sub_id);
509 
510  PREF_GETTER_SETTER(addon_icons, bool, true)
518  PREF_GETTER_SETTER(core, std::string, std::string("default"))
519  PREF_GETTER_SETTER(scroll_to_action, bool, true)
520  PREF_GETTER_SETTER(auto_pixel_scale, bool, true)
521  PREF_GETTER_SETTER(maximized, bool, !fullscreen())
522  PREF_GETTER_SETTER(fullscreen, bool, true)
523  PREF_GETTER_SETTER(vsync, bool, true)
524  PREF_GETTER_SETTER(turbo_speed, double, 2.0)
525  PREF_GETTER_SETTER(idle_anim, bool, true)
526  PREF_GETTER_SETTER(idle_anim_rate, int, 0)
527  PREF_GETTER_SETTER(locale, std::string, std::string(""))
528  PREF_GETTER_SETTER(gui2_theme, std::string, std::string(""))
529  PREF_GETTER_SETTER(show_tips, bool, true)
530  PREF_GETTER_SETTER(stop_music_in_background, bool, false)
531  PREF_GETTER_SETTER(tile_size, unsigned, 0)
532  PREF_GETTER_SETTER(mouse_scrolling, bool, true)
533  PREF_GETTER_SETTER(refresh_rate, int, -1)
534  PREF_GETTER_SETTER(animate_map, bool, true)
535  PREF_GETTER_SETTER(animate_water, bool, true)
536  PREF_GETTER_SETTER(minimap_movement_coding, bool, true)
537  PREF_GETTER_SETTER(minimap_terrain_coding, bool, true)
538  PREF_GETTER_SETTER(minimap_draw_units, bool, true)
539  PREF_GETTER_SETTER(minimap_draw_villages, bool, true)
540  PREF_GETTER_SETTER(minimap_draw_terrain, bool, true)
541  PREF_GETTER_SETTER(show_side_colors, bool, true)
542  PREF_GETTER_SETTER(grid, bool, false)
543  PREF_GETTER_SETTER(disable_auto_moves, bool, false)
544  PREF_GETTER_SETTER(damage_prediction_allow_monte_carlo_simulation, bool, true)
545  PREF_GETTER_SETTER(campaign_rng_mode, std::string, std::string("biased"))
546  PREF_GETTER_SETTER(addon_manager_saved_order_name, std::string, std::string(""))
547  PREF_GETTER_SETTER(selected_achievement_group, std::string, std::string(""))
548  /** The most recently selected add-on id from the editor. May be an empty string. */
549  PREF_GETTER_SETTER(editor_chosen_addon, std::string, std::string(""))
550  PREF_GETTER_SETTER(_last_cache_cleaned_ver, std::string, std::string(""))
551  PREF_GETTER_SETTER(lobby_whisper_friends_only, bool, false)
552  PREF_GETTER_SETTER(fi_invert, bool, false)
553  PREF_GETTER_SETTER(fi_vacant_slots, bool, false)
554  PREF_GETTER_SETTER(fi_friends_in_game, bool, false)
555  PREF_GETTER_SETTER(fi_blocked_in_game, bool, false)
556  PREF_GETTER_SETTER(editor_auto_update_transitions, int, pref_constants::TRANSITION_UPDATE_PARTIAL)
557  PREF_GETTER_SETTER(editor_draw_terrain_codes, bool, false)
558  PREF_GETTER_SETTER(editor_draw_hex_coordinates, bool, false)
559  PREF_GETTER_SETTER(editor_draw_num_of_bitmaps, bool, false)
560  PREF_GETTER_SETTER(turn_dialog, bool, false)
561  PREF_GETTER_SETTER(enable_planning_mode_on_start, bool, false)
562  PREF_GETTER_SETTER(hide_whiteboard, bool, false)
563  PREF_GETTER_SETTER(allow_observers, bool, true)
564  PREF_GETTER_SETTER(shuffle_sides, bool, false)
565  PREF_GETTER_SETTER(random_faction_mode, std::string, std::string(""))
566  PREF_GETTER_SETTER(mp_use_map_settings, bool, true)
567  PREF_GETTER_SETTER(mp_server_warning_disabled, int, 0)
568  PREF_GETTER_SETTER(mp_random_start_time, bool, true)
569  PREF_GETTER_SETTER(mp_fog, bool, true)
570  PREF_GETTER_SETTER(mp_shroud, bool, true)
571  PREF_GETTER_SETTER(mp_turns, int, 0)
572  PREF_GETTER_SETTER(skip_mp_replay, bool, false)
573  PREF_GETTER_SETTER(blindfold_replay, bool, false)
574  PREF_GETTER_SETTER(mp_countdown, bool, false)
575  PREF_GETTER_SETTER(mp_era, std::string, std::string(""))
576  PREF_GETTER_SETTER(mp_level, std::string, std::string(""))
577  PREF_GETTER_SETTER(mp_level_type, int, 0)
578  PREF_GETTER_SETTER(mp_connect_type, int, 0)
579  PREF_GETTER_SETTER(skip_ai_moves, bool, false)
580  PREF_GETTER_SETTER(save_replays, bool, true)
581  PREF_GETTER_SETTER(delete_saves, bool, false)
582  PREF_GETTER_SETTER(ask_delete, bool, true)
583  PREF_GETTER_SETTER(ally_sighted_interrupts, bool, true)
584  PREF_GETTER_SETTER(auto_save_max, int, 10)
585  PREF_GETTER_SETTER(floating_labels, bool, true)
586  PREF_GETTER_SETTER(chat_timestamp, bool, false)
587  PREF_GETTER_SETTER(chat_lines, int, 6)
588  PREF_GETTER_SETTER(show_all_units_in_help, bool, false)
589  PREF_GETTER_SETTER(custom_command, std::string, std::string(""))
590  PREF_GETTER_SETTER(player_joins_sound, bool, true)
591  PREF_GETTER_SETTER(player_joins_lobby, bool, false)
592  PREF_GETTER_SETTER(player_joins_notif, bool, false)
593  PREF_GETTER_SETTER(player_leaves_sound, bool, true)
594  PREF_GETTER_SETTER(player_leaves_lobby, bool, false)
595  PREF_GETTER_SETTER(player_leaves_notif, bool, false)
596  PREF_GETTER_SETTER(private_message_sound, bool, true)
597  PREF_GETTER_SETTER(private_message_lobby, bool, desktop::notifications::available())
598  PREF_GETTER_SETTER(private_message_notif, bool, true)
599  PREF_GETTER_SETTER(friend_message_sound, bool, false)
600  PREF_GETTER_SETTER(friend_message_lobby, bool, false)
601  PREF_GETTER_SETTER(friend_message_notif, bool, false)
602  PREF_GETTER_SETTER(public_message_sound, bool, false)
603  PREF_GETTER_SETTER(public_message_lobby, bool, false)
604  PREF_GETTER_SETTER(public_message_notif, bool, false)
605  PREF_GETTER_SETTER(server_message_sound, bool, true)
606  PREF_GETTER_SETTER(server_message_lobby, bool, false)
607  PREF_GETTER_SETTER(server_message_notif, bool, true)
608  PREF_GETTER_SETTER(ready_for_start_sound, bool, true)
609  PREF_GETTER_SETTER(ready_for_start_lobby, bool, desktop::notifications::available())
610  PREF_GETTER_SETTER(ready_for_start_notif, bool, false)
611  PREF_GETTER_SETTER(game_has_begun_sound, bool, true)
612  PREF_GETTER_SETTER(game_has_begun_lobby, bool, desktop::notifications::available())
613  PREF_GETTER_SETTER(game_has_begun_notif, bool, false)
614  PREF_GETTER_SETTER(turn_changed_sound, bool, true)
615  PREF_GETTER_SETTER(turn_changed_lobby, bool, desktop::notifications::available())
616  PREF_GETTER_SETTER(turn_changed_notif, bool, false)
617  PREF_GETTER_SETTER(game_created_sound, bool, true)
618  PREF_GETTER_SETTER(game_created_lobby, bool, desktop::notifications::available())
619  PREF_GETTER_SETTER(game_created_notif, bool, true)
620  PREF_GETTER_SETTER(editor_help_text_shown, bool, true)
621  PREF_GETTER_SETTER(show_attack_miss_indicator, bool, false)
622  PREF_GETTER_SETTER(simd_enabled, bool, true)
623 #undef PREF_GETTER_SETTER
624  void clear_mp_alert_prefs();
625 
626 private:
627  prefs();
628  // don't move, assign, or copy a singleton
629  prefs(const prefs& p) = delete;
630  prefs& operator=(const prefs& p) = delete;
631  prefs(const prefs&& p) = delete;
632  prefs& operator=(const prefs&& p) = delete;
633 
634  inline static bool no_preferences_save = false;
635 
637  bool fps_;
638  std::map<std::string, std::set<std::string>> completed_campaigns_;
639  std::set<std::string> encountered_units_set_;
640  std::set<t_translation::terrain_code> encountered_terrains_set_;
641  std::map<std::string, std::vector<std::string>> history_map_;
642  std::map<std::string, preferences::acquaintance> acquaintances_;
645  std::vector<std::string> mp_modifications_;
647  std::vector<std::string> sp_modifications_;
650  std::vector<preferences::login_info> credentials_;
651  std::vector<preferences::option> advanced_prefs_;
652 
653  void load_preferences();
655  void load_credentials();
656  void save_credentials();
657  void clear_credentials();
658 
659  void set_child(const std::string& key, const config& val);
660  optional_const_config get_child(const std::string &key);
661  std::string get(const std::string& key, const std::string& def);
662  config::attribute_value get_as_attribute(const std::string& key);
663 
664  std::string get_system_username();
665  /**
666  * Encrypts the value of @a text using @a key and a hard coded IV using AES.
667  * Max size of @a text must not be larger than 1008 bytes.
668  *
669  * NOTE: This is not meant to provide strong protections against a determined attacker.
670  * This is meant to hide the passwords from malware scanning files for passwords, family/friends poking around, etc.
671  *
672  * @param text The original unencrypted data.
673  * @param key The value to use to encrypt the data. See build_key() for key generation.
674  * @return secure_buffer The encrypted data.
675  */
677  /**
678  * Same as aes_encrypt(), except of course it takes encrypted data as an argument and returns decrypted data.
679  */
681  /**
682  * Fills a secure_buffer with 32 bytes of deterministically generated bytes, then overwrites it with the system login name, server login name, and server name.
683  * If this is more than 32 bytes, then it's truncated. If it's less than 32 bytes, then the pre-generated bytes are used to pad it.
684  *
685  * @param server The server being logged into.
686  * @param login The username being used to login.
687  * @return secure_buffer The data to be used as the encryption key.
688  */
689  preferences::secure_buffer build_key(const std::string& server, const std::string& login);
692 
693  std::set<std::string> unknown_synced_attributes_;
694  std::set<std::string> unknown_unsynced_attributes_;
695  std::set<std::string> unknown_synced_children_;
696  std::set<std::string> unknown_unsynced_children_;
697 
698  // a bit verbose, but it being a compile time error if a preference hasn't been added is nice
699  static constexpr std::array synced_attributes_{
700  prefs_list::player_joins_sound,
701  prefs_list::player_joins_notif,
702  prefs_list::player_joins_lobby,
703  prefs_list::player_leaves_sound,
704  prefs_list::player_leaves_notif,
705  prefs_list::player_leaves_lobby,
706  prefs_list::private_message_sound,
707  prefs_list::private_message_notif,
708  prefs_list::private_message_lobby,
709  prefs_list::friend_message_sound,
710  prefs_list::friend_message_notif,
711  prefs_list::friend_message_lobby,
712  prefs_list::public_message_sound,
713  prefs_list::public_message_notif,
714  prefs_list::public_message_lobby,
715  prefs_list::server_message_sound,
716  prefs_list::server_message_notif,
717  prefs_list::server_message_lobby,
718  prefs_list::ready_for_start_sound,
719  prefs_list::ready_for_start_notif,
720  prefs_list::ready_for_start_lobby,
721  prefs_list::game_has_begun_sound,
722  prefs_list::game_has_begun_notif,
723  prefs_list::game_has_begun_lobby,
724  prefs_list::turn_changed_sound,
725  prefs_list::turn_changed_notif,
726  prefs_list::turn_changed_lobby,
727  prefs_list::game_created_sound,
728  prefs_list::game_created_notif,
729  prefs_list::game_created_lobby,
730  prefs_list::_last_cache_cleaned_ver,
731  prefs_list::addon_manager_saved_order_direction,
732  prefs_list::addon_manager_saved_order_name,
733  prefs_list::alias,
734  prefs_list::allow_observers,
736  prefs_list::ally_sighted_interrupts,
737  prefs_list::auto_save_max,
738  prefs_list::blindfold_replay,
739  prefs_list::campaign_rng_mode,
740  prefs_list::campaign_server,
741  prefs_list::chat_lines,
742  prefs_list::chat_timestamp,
743  prefs_list::confirm_end_turn,
744  prefs_list::custom_command,
745  prefs_list::delete_saves,
746  prefs_list::disable_auto_moves,
747  prefs_list::editor_auto_update_transitions,
748  prefs_list::editor_draw_hex_coordinates,
749  prefs_list::editor_draw_num_of_bitmaps,
750  prefs_list::editor_draw_terrain_codes,
751  prefs_list::enable_planning_mode_on_start,
752  prefs_list::encountered_terrain_list,
753  prefs_list::encountered_units,
755  prefs_list::fi_blocked_in_game,
756  prefs_list::fi_friends_in_game,
757  prefs_list::fi_invert,
758  prefs_list::fi_vacant_slots,
759  prefs_list::floating_labels,
760  prefs_list::grid,
761  prefs_list::hide_whiteboard,
762  prefs_list::host,
763  prefs_list::idle_anim,
764  prefs_list::idle_anim_rate,
766  prefs_list::lobby_whisper_friends_only,
767  prefs_list::locale,
768  prefs_list::login,
769  prefs_list::message_bell,
770  prefs_list::minimap_draw_terrain,
771  prefs_list::minimap_draw_units,
772  prefs_list::minimap_draw_villages,
773  prefs_list::minimap_movement_coding,
774  prefs_list::minimap_terrain_coding,
776  prefs_list::mp_countdown,
777  prefs_list::mp_countdown_action_bonus,
778  prefs_list::mp_countdown_init_time,
779  prefs_list::mp_countdown_reservoir_time,
780  prefs_list::mp_countdown_turn_bonus,
781  prefs_list::mp_fog,
782  prefs_list::mp_level_type,
783  prefs_list::mp_connect_type,
784  prefs_list::mp_random_start_time,
785  prefs_list::mp_server_warning_disabled,
786  prefs_list::mp_shroud,
787  prefs_list::mp_turns,
788  prefs_list::mp_use_map_settings,
789  prefs_list::mp_village_gold,
790  prefs_list::mp_village_support,
791  prefs_list::mp_xp_modifier,
792  prefs_list::music,
795  prefs_list::remember_password,
796  prefs_list::save_replays,
797  prefs_list::scroll,
798  prefs_list::scroll_threshold,
804  prefs_list::show_side_colors,
807  prefs_list::shuffle_sides,
808  prefs_list::skip_ai_moves,
809  prefs_list::skip_mp_replay,
811  prefs_list::sample_rate,
812  prefs_list::stop_music_in_background,
813  prefs_list::turbo,
814  prefs_list::turbo_speed,
816  prefs_list::turn_dialog,
817  prefs_list::ui_sound,
818  prefs_list::unit_standing_animations,
820  prefs_list::ask_delete,
821  prefs_list::chat_message_aging,
822  prefs_list::color_cursors,
823  prefs_list::compress_saves,
824  prefs_list::confirm_load_save_from_different_version,
825  prefs_list::damage_prediction_allow_monte_carlo_simulation,
826  prefs_list::editor_max_recent_files,
827  prefs_list::keepalive_timeout,
828  prefs_list::lobby_auto_open_whisper_windows,
829  prefs_list::middle_click_scrolls,
830  prefs_list::mouse_scrolling,
831  prefs_list::scroll_to_action,
832  prefs_list::scroll_when_mouse_outside,
833  prefs_list::show_all_units_in_help,
834  prefs_list::show_combat,
835  prefs_list::show_deprecation,
836  prefs_list::show_attack_miss_indicator,
837  prefs_list::use_twelve_hour_clock_format,
838  prefs_list::mp_era,
839  prefs_list::mp_level,
840  prefs_list::mp_modifications,
841  prefs_list::selected_achievement_group,
842  prefs_list::sp_modifications,
843  prefs_list::animate_map,
844  prefs_list::animate_water,
845  prefs_list::addon_icons,
850  };
851  static constexpr std::array synced_children_{
852  prefs_list::acquaintance,
853  prefs_list::achievements,
854  prefs_list::completed_campaigns,
855  prefs_list::history,
856  prefs_list::options,
857  prefs_list::server,
858  prefs_list::game_preset,
859  };
860  static constexpr std::array unsynced_attributes_{
861  prefs_list::auto_pixel_scale,
862  prefs_list::core,
863  prefs_list::refresh_rate,
864  prefs_list::editor_chosen_addon,
865  prefs_list::gui2_theme,
866  prefs_list::show_tips,
867  prefs_list::mp_server_program_name,
868  prefs_list::pixel_scale,
869  prefs_list::sound_buffer_size,
870  prefs_list::theme,
872  prefs_list::vsync,
873  prefs_list::xresolution,
874  prefs_list::yresolution,
875  prefs_list::font_scale,
878  prefs_list::simd_enabled,
881  prefs_list::fullscreen,
882  prefs_list::maximized,
883  };
884  static constexpr std::array unsynced_children_{
885  prefs_list::editor_recent_files,
886  prefs_list::dir_bookmarks,
887  };
888  static_assert(synced_attributes_.size() + synced_children_.size() + unsynced_attributes_.size() + unsynced_children_.size() == prefs_list::values.size(), "attribute missing from lists of synced or unsynced preferences!");
889 };
890 
891 //
892 // might be overkill, but it does best limit the private functionality being exposed
893 //
894 
896 {
897  friend class preferences_dialog;
898 
899  static void set(const std::string& pref, bool value)
900  {
901  prefs::get().preferences_[pref] = value;
902  }
903  static void set(const std::string& pref, int value)
904  {
905  prefs::get().preferences_[pref] = value;
906  }
907  static void set(const std::string& pref, const std::string& value)
908  {
909  prefs::get().preferences_[pref] = value;
910  }
911 
912  static bool get(const std::string& pref, bool def)
913  {
914  return prefs::get().preferences_[pref].to_bool(def);
915  }
916  static int get(const std::string& pref, int def)
917  {
918  return prefs::get().preferences_[pref].to_int(def);
919  }
920  static std::string get(const std::string& pref, const std::string& def)
921  {
922  return prefs::get().get(pref, def);
923  }
924 };
double t
Definition: astarsearch.cpp:63
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
boost::iterator_range< child_iterator > child_itors
Definition: config.hpp:280
void clear()
Definition: config.cpp:802
Game board class.
Definition: game_board.hpp:47
A class grating read only view to a vector of config objects, viewed as one config with all children ...
Encapsulates the map of the game.
Definition: map.hpp:176
std::string nick_
acquaintance's MP nick
void save(config &item)
Definition: preferences.hpp:96
acquaintance(const std::string &nick, const std::string &status, const std::string &notes)
Definition: preferences.hpp:85
std::string notes_
notes on the acquaintance
const std::string & get_status() const
Definition: preferences.hpp:93
const std::string & get_nick() const
Definition: preferences.hpp:92
std::string status_
status (e.g., "friend", "ignore")
const std::string & get_notes() const
Definition: preferences.hpp:94
acquaintance(const config &cfg)
Definition: preferences.hpp:78
void set_lobby_joins(pref_constants::lobby_joins show)
std::string enemy_color()
config::attribute_value get_as_attribute(const std::string &key)
void set_reach_map_enemy_color(const std::string &color_id)
void set_network_host(const std::string &host)
std::set< t_translation::terrain_code > & encountered_terrains()
const config & options()
bool set_music(bool ison)
void set_sound_volume(int vol)
void set_remember_password(bool remember)
const std::map< std::string, preferences::acquaintance > & get_acquaintances()
optional_const_config get_alias()
bool show_theme_dialog()
void set_turbo(bool ison)
std::string get_system_username()
prefs(const prefs &&p)=delete
bool mp_modifications_initialized_
std::map< std::string, std::vector< std::string > > history_map_
void save_hotkeys()
int village_support()
void set_addon_manager_saved_order_direction(sort_order::type value)
std::chrono::seconds countdown_turn_bonus()
bool middle_click_scrolls()
bool turn_bell()
void set_countdown_reservoir_time(const std::chrono::seconds &value)
void remove_game_preset(int id)
std::set< std::string > & encountered_units()
void add_alias(const std::string &alias, const std::string &command)
void write_preferences()
void clear_preferences()
std::vector< std::string > sp_modifications_
void clear_mp_alert_prefs()
The most recently selected add-on id from the editor.
void set_village_gold(int value)
bool confirm_load_save_from_different_version()
std::string network_host()
void clear_countdown_init_time()
std::set< std::string > unknown_unsynced_attributes_
bool set_ui_sound(bool ison)
void set_reach_map_border_opacity(const int new_opacity)
std::chrono::minutes chat_message_aging()
void set_login(const std::string &login)
static prefs & get()
const std::string get_ignored_delim()
std::map< std::string, preferences::acquaintance > acquaintances_
friend int impl_preferences_set(lua_State *L)
The __newindex metamethod.
static constexpr std::array unsynced_children_
bool fps_
config::child_itors get_game_presets()
int scroll_speed()
bool message_private()
void encounter_recallable_units(const std::vector< team > &teams)
std::map< std::string, std::set< std::string > > completed_campaigns_
bool show_combat()
std::string theme()
optional_const_config dir_bookmarks()
void set_theme(const std::string &theme)
void set_xp_modifier(int value)
void set_user_servers_list(const std::vector< game_config::server_info > &value)
preferences::secure_buffer build_key(const std::string &server, const std::string &login)
Fills a secure_buffer with 32 bytes of deterministically generated bytes, then overwrites it with the...
bool sound()
bool is_campaign_completed(const std::string &campaign_id)
void set_allied_color(const std::string &color_id)
bool get_scroll_when_mouse_outside(bool def)
void show_wesnothd_server_search()
bool green_confirm()
int bell_volume()
int mouse_scroll_threshold()
Gets the threshold for when to scroll.
std::string get_chat_timestamp(const std::chrono::system_clock::time_point &t)
bool is_ignored(const std::string &nick)
std::vector< std::string > mp_modifications_
bool achievement(const std::string &content_for, const std::string &id)
std::set< std::string > unknown_synced_attributes_
void set_enemy_color(const std::string &color_id)
void save_credentials()
std::chrono::seconds countdown_init_time()
void set_password(const std::string &server, const std::string &login, const std::string &key)
bool sub_achievement(const std::string &content_for, const std::string &id, const std::string &sub_id)
void clear_credentials()
std::vector< preferences::login_info > credentials_
std::vector< std::string > do_read_editor_mru()
void encounter_recruitable_units(const std::vector< team > &teams)
bool parse_should_show_lobby_join(const std::string &sender, const std::string &message)
static bool preferences_save()
int progress_achievement(const std::string &content_for, const std::string &id, int limit=999999, int max_progress=999999, int amount=0)
Increments the achievement's current progress by amount if it hasn't already been completed.
sort_order::type addon_manager_saved_order_direction()
bool set_turn_bell(bool ison)
static constexpr std::array synced_attributes_
unsigned int sample_rate()
void set_reach_map_tint_opacity(const int new_opacity)
void set_countdown_turn_bonus(const std::chrono::seconds &value)
static constexpr std::array synced_children_
void set_options(const config &values)
int reach_map_tint_opacity()
void set_message_private(bool value)
std::vector< game_config::server_info > user_servers_list()
optional_const_config get_child(const std::string &key)
void load_credentials()
std::string unmoved_color()
void set_music_volume(int vol)
void load_advanced_prefs(const game_config_view &gc)
std::string allied_color()
void set_show_standing_animations(bool value)
void set_show_fps(bool value)
void set_pixel_scale(const int scale)
std::size_t editor_mru_limit()
void set_color_cursors(bool value)
std::vector< preferences::option > advanced_prefs_
void encounter_start_units(const unit_map &units)
pref_constants::lobby_joins get_lobby_joins()
bool show_fps()
void load_hotkeys()
void set_chat_message_aging(const std::chrono::minutes &value)
optional_const_config get_game_preset(int id)
void encounter_map_terrain(const gamemap &map)
void set_campaign_rng_mode_default_for_migration()
int font_scaling()
void set_village_support(int value)
preferences::secure_buffer escape(const preferences::secure_buffer &text)
prefs & operator=(const prefs &&p)=delete
std::chrono::seconds countdown_action_bonus()
std::string moved_color()
bool confirm_no_moves()
void set_font_scaling(int scale)
std::size_t sound_buffer_size()
bool use_color_cursors()
bool yellow_confirm()
config option_values_
void migrate_preferences(const std::string &prefs_dir)
bool message_bell()
void set_dir_bookmarks(const config &cfg)
bool message_private_on_
std::set< t_translation::terrain_code > encountered_terrains_set_
friend int impl_preferences_get(lua_State *L)
The __index metamethod.
bool set_sound(bool ison)
bool is_friend(const std::string &nick)
void set_achievement(const std::string &content_for, const std::string &id)
Marks the specified achievement as completed.
preferences::secure_buffer aes_decrypt(const preferences::secure_buffer &text, const preferences::secure_buffer &key)
Same as aes_encrypt(), except of course it takes encrypted data as an argument and returns decrypted ...
int village_gold()
config preferences_
void clear_countdown_turn_bonus()
std::string login()
bool music_on()
int font_scaled(int size)
void set_scroll_speed(const int scroll)
std::pair< preferences::acquaintance *, bool > add_acquaintance(const std::string &nick, const std::string &mode, const std::string &notes)
prefs(const prefs &p)=delete
bool ui_sound_on()
bool options_initialized_
int music_volume()
int pixel_scale()
std::string partial_color()
preferences::secure_buffer unescape(const preferences::secure_buffer &text)
void set_resolution(const point &res)
static void disable_preferences_save()
void set_campaign_server(const std::string &host)
void set_unmoved_color(const std::string &color_id)
bool remove_acquaintance(const std::string &nick)
void clear_countdown_reservoir_time()
void set_ui_volume(int vol)
bool turbo()
const std::vector< std::string > & modifications(bool mp=true)
void do_commit_editor_mru(const std::vector< std::string > &mru)
std::chrono::seconds countdown_reservoir_time()
std::string reach_map_color()
compression::format save_compression_format()
std::vector< std::string > recent_files()
Retrieves the list of recently opened files.
std::set< std::string > encountered_units_set_
void set_bell_volume(int vol)
std::string get_mp_server_program_name()
std::string reach_map_enemy_color()
prefs & operator=(const prefs &p)=delete
void add_game_preset(config &&preset_data)
void set_partial_color(const std::string &color_id)
void set_child(const std::string &key, const config &val)
static constexpr std::array unsynced_attributes_
std::set< std::string > all_attributes()
const std::vector< preferences::option > & get_advanced_preferences()
int keepalive_timeout()
void set_mp_server_program_name(const std::string &)
bool remember_password()
int ui_volume()
void add_recent_files_entry(const std::string &path)
Adds an entry to the recent files list.
void set_moved_color(const std::string &color_id)
point resolution()
void save_sample_rate(const unsigned int rate)
bool use_twelve_hour_clock_format()
static bool no_preferences_save
int reach_map_border_opacity()
int sound_volume()
void set_modifications(const std::vector< std::string > &value, bool mp=true)
void set_countdown_init_time(const std::chrono::seconds &value)
void set_sub_achievement(const std::string &content_for, const std::string &id, const std::string &sub_id)
Marks the specified sub-achievement as completed.
std::set< std::string > unknown_unsynced_children_
const std::vector< game_config::server_info > & builtin_servers_list()
void set_reach_map_color(const std::string &color_id)
void reload_preferences()
std::string campaign_server()
std::set< std::string > unknown_synced_children_
void add_completed_campaign(const std::string &campaign_id, const std::string &difficulty_level)
void clear_hotkeys()
void save_sound_buffer_size(const std::size_t size)
int xp_modifier()
void encounter_all_content(const game_board &gb)
std::string password(const std::string &server, const std::string &login)
bool sp_modifications_initialized_
std::vector< std::string > * get_history(const std::string &id)
Returns a pointer to the history vector associated with given id making a new one if it doesn't exist...
preferences::secure_buffer aes_encrypt(const preferences::secure_buffer &text, const preferences::secure_buffer &key)
Encrypts the value of text using key and a hard coded IV using AES.
void set_countdown_action_bonus(const std::chrono::seconds &value)
bool auto_open_whisper_windows()
void load_preferences()
std::map< std::string, std::string > get_acquaintances_nice(const std::string &filter)
bool get_show_deprecation(bool def)
bool show_standing_animations()
void clear_countdown_action_bonus()
Definition: theme.hpp:43
Container associating units to locations.
Definition: map.hpp:98
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
Declarations for File-IO.
Standard logging facilities (interface).
void fill(const ::rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
Definition: draw.cpp:62
std::string partial_orb_color
std::string reach_map_enemy_color
std::string moved_orb_color
std::string unmoved_orb_color
std::string ally_orb_color
std::string enemy_orb_color
std::string reach_map_color
std::string turn_bell
Game configuration data as global variables.
Definition: build_info.cpp:68
std::string path
Definition: filesystem.cpp:106
bool show_status_on_ally_orb
bool show_moved_orb
int reach_map_border_opacity
unsigned int tile_size
Definition: game_config.cpp:55
bool show_ally_orb
bool show_disengaged_orb
bool show_partial_orb
bool show_enemy_orb
bool show_unmoved_orb
int reach_map_tint_opacity
static int bell_volume()
static int music_volume()
static bool sound()
static int ui_volume()
static int sound_volume()
Main entry points of multiplayer mode.
Definition: lobby_data.cpp:49
const int min_window_height
Definition: preferences.hpp:38
const int TRANSITION_UPDATE_ON
Definition: preferences.hpp:53
const int max_pixel_scale
Definition: preferences.hpp:50
const std::string EMPTY_LOGIN
Definition: preferences.hpp:58
const int min_pixel_scale
Definition: preferences.hpp:49
const std::string default_addons_server
Definition: preferences.hpp:62
const int def_window_width
Definition: preferences.hpp:40
const int min_font_scaling
Definition: preferences.hpp:46
const int TRANSITION_UPDATE_COUNT
Definition: preferences.hpp:55
const int min_window_width
Definition: preferences.hpp:37
const int TRANSITION_UPDATE_OFF
Definition: preferences.hpp:52
const int max_font_scaling
Definition: preferences.hpp:47
const int max_window_height
Definition: preferences.hpp:44
const int max_window_width
Definition: preferences.hpp:43
const int def_window_height
Definition: preferences.hpp:41
const int INFINITE_AUTO_SAVES
Definition: preferences.hpp:60
const unsigned char CREDENTIAL_SEPARATOR
Definition: preferences.hpp:57
const int TRANSITION_UPDATE_PARTIAL
Definition: preferences.hpp:54
Modify, read and display user preferences.
Definition: preferences.hpp:70
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:81
constexpr auto values
Definition: ranges.hpp:46
constexpr auto filter
Definition: ranges.hpp:42
bool show(std::string title, std::string message)
Displays a tray notification.
void scale(size_t factor, const uint32_t *src, uint32_t *trg, int srcWidth, int srcHeight, ColorFormat colFmt, const ScalerCfg &cfg=ScalerCfg(), int yFirst=0, int yLast=std::numeric_limits< int >::max())
Definition: xbrz.cpp:1175
#define PREF_GETTER_SETTER(pref, type, def)
string_enums::enum_base< random_faction_mode_defines > random_faction_mode
Holds a 2D point.
Definition: point.hpp:25
login_info(const std::string &username, const std::string &server, const secure_buffer &key)
std::size_t size() const
login_info(const std::string &username, const std::string &server)
t_string description
Displayed description.
avd_type type
The preference type.
option(const config &pref)
t_string name
Displayed name.
std::string field
The actual field saved in the prefs file and by which this preference may be accessed.
config cfg
The full config, including type-specific options.
static void set(const std::string &pref, int value)
friend class preferences_dialog
static void set(const std::string &pref, const std::string &value)
static int get(const std::string &pref, int def)
static std::string get(const std::string &pref, const std::string &def)
static bool get(const std::string &pref, bool def)
static void set(const std::string &pref, bool value)
The base template for associating string values with enum values.
Definition: enum_base.hpp:33
mock_party p