The Battle for Wesnoth  1.19.26+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 sound { class volume; }
70 namespace preferences
71 {
73 {
74 public:
76  {
77  }
78 
79  explicit acquaintance(const config& cfg)
80  {
81  nick_ = cfg["nick"].str();
82  status_ = cfg["status"].str();
83  notes_ = cfg["notes"].str();
84  }
85 
86  acquaintance(const std::string& nick, const std::string& status, const std::string& notes)
87  : nick_(nick)
88  , status_(status)
89  , notes_(notes)
90  {
91  }
92 
93  const std::string& get_nick() const { return nick_; }
94  const std::string& get_status() const { return status_; }
95  const std::string& get_notes() const { return notes_; }
96 
97  void save(config& item)
98  {
99  item["nick"] = nick_;
100  item["status"] = status_;
101  item["notes"] = notes_;
102  }
103 
104 private:
105  /** acquaintance's MP nick */
106  std::string nick_;
107 
108  /**status (e.g., "friend", "ignore") */
109  std::string status_;
110 
111  /** notes on the acquaintance */
112  std::string notes_;
113 };
114 
115 class secure_buffer : public std::vector<unsigned char>
116 {
117 public:
118  template<typename... T> secure_buffer(T&&... args)
119  : vector<unsigned char>(std::forward<T>(args)...)
120  {}
122  {
123  std::fill(begin(), end(), '\0');
124  }
125 };
126 
128 {
129  std::string username, server;
131  login_info(const std::string& username, const std::string& server, const secure_buffer& key)
133  {}
134  login_info(const std::string& username, const std::string& server)
136  {}
137  std::size_t size() const
138  {
139  return 3 + username.size() + server.size() + key.size();
140  }
141 };
142 
143 struct option
144 {
145  option(const config& pref)
146  : type()
147  , name(pref["name"].t_str())
148  , description(pref["description"].t_str())
149  , field(pref["field"].str())
150  , cfg(pref)
151  {
152  const std::string p_type = cfg["type"];
153 
154  if(p_type == "boolean") {
156  } else if(p_type == "int") {
158  } else if(p_type == "combo") {
160  } else if(p_type == "custom") {
162  } else {
163  throw std::invalid_argument("Unknown type '" + p_type + "' for advanced preference " + name);
164  }
165  }
166 
167  enum class avd_type { TOGGLE, SLIDER, COMBO, SPECIAL };
168 
169  /** The preference type. */
171 
172  /** Displayed name. */
174 
175  /** Displayed description. */
177 
178  /** The actual field saved in the prefs file and by which this preference may be accessed. */
179  std::string field;
180 
181  /** The full config, including type-specific options. */
183 };
184 };
185 
186 #define PREF_GETTER_SETTER(pref, type, def) \
187 void set_##pref(const type& value) { \
188  preferences_[prefs_list::pref] = value; \
189 } \
190 type pref() const { \
191  return preferences_[prefs_list::pref].to(def); \
192 }
193 
194 class prefs
195 {
197  friend int impl_preferences_set(lua_State* L);
198  friend int impl_preferences_get(lua_State* L);
199 
200 public:
201  static prefs& get()
202  {
203  static prefs prefs_manager;
204  return prefs_manager;
205  }
206 
207  ~prefs();
208 
209  void write_preferences();
210  void load_advanced_prefs(const game_config_view& gc);
211  void migrate_preferences(const std::string& prefs_dir);
212  void set_campaign_rng_mode_default_for_migration(); // TODO: remove after 1.20. (Not used after that. Needs no replacement.)
213  void reload_preferences();
214  std::set<std::string> all_attributes();
215 
216  // Proxies for preferences_dialog
217  void load_hotkeys();
218  void save_hotkeys();
219  void clear_hotkeys();
220 
221  // not actually preferences
222  bool show_fps();
223  void set_show_fps(bool value);
224 
225  bool message_private();
226  void set_message_private(bool value);
227 
228  static void disable_preferences_save() {
229  no_preferences_save = true;
230  }
231 
232  static bool preferences_save() {
233  return no_preferences_save;
234  }
235 
236  // getter-only
237  bool message_bell();
238  bool middle_click_scrolls();
239  /** Gets the threshold for when to scroll. This scrolling happens when the mouse is in the application and near the border. */
245  bool get_show_deprecation(bool def);
246  bool get_scroll_when_mouse_outside(bool def);
248  bool show_combat();
249  std::string get_chat_timestamp(const std::chrono::system_clock::time_point& t);
251  std::vector<std::string>* get_history(const std::string& id);
252  const std::vector<preferences::option>& get_advanced_preferences() { return advanced_prefs_; }
253 
254  // non-trivial getters+setters
255  point resolution();
256  void set_resolution(const point& res);
257 
258  int pixel_scale();
259  void set_pixel_scale(const int scale);
260 
261  bool turbo();
262  void set_turbo(bool ison);
263 
264  int font_scaling();
265  void set_font_scaling(int scale);
266  int font_scaled(int size);
267 
268  int keepalive_timeout();
269  void keepalive_timeout(int seconds);
270 
271  bool sound();
272  bool set_sound(bool ison);
273 
276 
278  void set_bell_volume(sound::volume vol);
279 
281  void set_ui_volume(sound::volume vol);
282 
283  bool music_on();
284  bool set_music(bool ison);
285 
288 
289  bool turn_bell();
290  bool set_turn_bell(bool ison);
291 
292  bool ui_sound_on();
293  bool set_ui_sound(bool ison);
294 
295  void add_alias(const std::string& alias, const std::string& command);
297 
298  std::string allied_color();
299  void set_allied_color(const std::string& color_id);
300 
301  std::string enemy_color();
302  void set_enemy_color(const std::string& color_id);
303 
304  std::string unmoved_color();
305  void set_unmoved_color(const std::string& color_id);
306 
307  std::string partial_color();
308  void set_partial_color(const std::string& color_id);
309 
310  std::string moved_color();
311  void set_moved_color(const std::string& color_id);
312 
313  std::string reach_map_color();
314  void set_reach_map_color(const std::string& color_id);
315 
316  std::string reach_map_enemy_color();
317  void set_reach_map_enemy_color(const std::string& color_id);
318 
320  void set_reach_map_border_opacity(const int new_opacity);
321 
323  void set_reach_map_tint_opacity(const int new_opacity);
324 
325  int scroll_speed();
326  void set_scroll_speed(const int scroll);
327 
328  void set_dir_bookmarks(const config& cfg);
330 
331  std::size_t editor_mru_limit();
332  std::vector<std::string> do_read_editor_mru();
333  void do_commit_editor_mru(const std::vector<std::string>& mru);
334  /** Retrieves the list of recently opened files. */
335  std::vector<std::string> recent_files();
336  /** Adds an entry to the recent files list. */
337  void add_recent_files_entry(const std::string& path);
338 
339  bool use_color_cursors();
340  void set_color_cursors(bool value);
341 
344 
346  bool show_theme_dialog();
347 
348  void set_theme(const std::string& theme);
349  std::string theme();
350 
351  void set_mp_server_program_name(const std::string&);
352  std::string get_mp_server_program_name();
353 
354  bool parse_should_show_lobby_join(const std::string& sender, const std::string& message);
357 
358  const std::map<std::string, preferences::acquaintance>& get_acquaintances();
359  const std::string get_ignored_delim();
360  std::map<std::string, std::string> get_acquaintances_nice(const std::string& filter);
361  std::pair<preferences::acquaintance*, bool> add_acquaintance(const std::string& nick, const std::string& mode, const std::string& notes);
362  void add_completed_campaign(const std::string&campaign_id, const std::string& difficulty_level);
363  bool remove_acquaintance(const std::string& nick);
364  bool is_friend(const std::string& nick);
365  bool is_ignored(const std::string& nick);
366  bool is_campaign_completed(const std::string& campaign_id);
367  bool is_campaign_completed(const std::string& campaign_id, const std::string& difficulty_level);
368 
369  void add_game_preset(config&& preset_data);
370  void remove_game_preset(int id);
373 
374  const std::vector<game_config::server_info>& builtin_servers_list();
375  std::vector<game_config::server_info> user_servers_list();
376  void set_user_servers_list(const std::vector<game_config::server_info>& value);
377 
378  std::string network_host();
379  void set_network_host(const std::string& host);
380 
381  std::string campaign_server();
382  void set_campaign_server(const std::string& host);
383 
384  const config& options();
385  void set_options(const config& values);
386 
387  std::chrono::seconds countdown_init_time();
388  void set_countdown_init_time(const std::chrono::seconds& value);
390 
391  std::chrono::seconds countdown_turn_bonus();
392  void set_countdown_turn_bonus(const std::chrono::seconds& value);
394 
395  std::chrono::seconds countdown_reservoir_time();
396  void set_countdown_reservoir_time(const std::chrono::seconds& value);
398 
399  std::chrono::seconds countdown_action_bonus();
400  void set_countdown_action_bonus(const std::chrono::seconds& value);
402 
403  std::chrono::minutes chat_message_aging();
404  void set_chat_message_aging(const std::chrono::minutes& value);
405 
406  int village_gold();
407  void set_village_gold(int value);
408 
409  int village_support();
410  void set_village_support(int value);
411 
412  int xp_modifier();
413  void set_xp_modifier(int value);
414 
415  bool remember_password();
416  void set_remember_password(bool remember);
417 
418  std::string login();
419  void set_login(const std::string& login);
420 
421  std::string password(const std::string& server, const std::string& login);
422  void set_password(const std::string& server, const std::string& login, const std::string& key);
423 
424  const std::vector<std::string>& modifications(bool mp = true);
425  void set_modifications(const std::vector<std::string>& value, bool mp = true);
426 
427  std::set<std::string>& encountered_units();
428  std::set<t_translation::terrain_code>& encountered_terrains();
429 
430  // Ask for end turn confirmation
431  bool yellow_confirm();
432  bool green_confirm();
433  bool confirm_no_moves();
434 
435  // Add all recruitable units as encountered so that information
436  // about them are displayed to the user in the help system.
437  void encounter_recruitable_units(const std::vector<team>& teams);
438 
439  // Add all units that exist at the start to the encountered units so
440  // that information about them are displayed to the user in the help
441  // system.
442  void encounter_start_units(const unit_map& units);
443 
444  // Add all units that are recallable as encountered units.
445  void encounter_recallable_units(const std::vector<team>& teams);
446 
447  // Add all terrains on the map as encountered terrains.
448  void encounter_map_terrain(const gamemap& map);
449 
450  // Mark the given terrain type as encountered.
451  void encounter_map_terrain(const terrain_type& terrain);
452 
453  // Calls all of the above functions on the current game board
454  void encounter_all_content(const game_board& gb);
455 
456  /**
457  * @param content_for The achievement group the achievement is part of.
458  * @param id The ID of the achievement within the achievement group.
459  * @return True if the achievement exists and is completed, false otherwise.
460  */
461  bool achievement(const std::string& content_for, const std::string& id);
462  /**
463  * Marks the specified achievement as completed.
464  *
465  * @param content_for The achievement group the achievement is part of.
466  * @param id The ID of the achievement within the achievement group.
467  */
468  void set_achievement(const std::string& content_for, const std::string& id);
469 
470  /**
471  * Increments the achievement's current progress by @a amount if it hasn't already been completed.
472  * If you only want to check the achievement's current progress, then omit the last three arguments.
473  * @a amount defaults to 0, which will result in the current progress value being returned without being changed (x + 0 == x).
474  *
475  * Note that this uses the same [in_progress] as is used for set_sub_achievement().
476  *
477  * @param content_for The id of the achievement group this achievement is in.
478  * @param id The id for the specific achievement in the achievement group.
479  * @param limit The maximum value that a specific call to this function can increase the achievement progress value.
480  * @param max_progress The value when the achievement is considered completed.
481  * @param amount The amount to progress the achievement.
482  * @return The achievement's current progress, or -1 if it has already been completed.
483  */
484  int progress_achievement(const std::string& content_for, const std::string& id, int limit = 999999, int max_progress = 999999, int amount = 0);
485 
486  /**
487  * @param content_for The achievement group the achievement is part of.
488  * @param id The ID of the achievement within the achievement group.
489  * @param sub_id The ID of the sub-achievement within the achievement.
490  * @return True if the sub-achievement exists and is completed, false otherwise.
491  */
492  bool sub_achievement(const std::string& content_for, const std::string& id, const std::string& sub_id);
493 
494  /**
495  * Marks the specified sub-achievement as completed.
496  *
497  * Note that this uses the same [in_progress] as is used for progress_achievement().
498  *
499  * @param content_for The achievement group the achievement is part of.
500  * @param id The ID of the achievement within the achievement group.
501  * @param sub_id The ID of the sub-achievement within the achievement.
502  */
503  void set_sub_achievement(const std::string& content_for, const std::string& id, const std::string& sub_id);
504 
505  PREF_GETTER_SETTER(addon_icons, bool, true)
513  PREF_GETTER_SETTER(core, std::string, std::string("default"))
514  PREF_GETTER_SETTER(scroll_to_action, bool, true)
515  PREF_GETTER_SETTER(auto_pixel_scale, bool, true)
516  PREF_GETTER_SETTER(maximized, bool, !fullscreen())
517  PREF_GETTER_SETTER(fullscreen, bool, true)
518  PREF_GETTER_SETTER(vsync, bool, true)
519  PREF_GETTER_SETTER(turbo_speed, double, 2.0)
520  PREF_GETTER_SETTER(idle_anim, bool, true)
521  PREF_GETTER_SETTER(idle_anim_rate, int, 0)
522  PREF_GETTER_SETTER(locale, std::string, std::string(""))
523  PREF_GETTER_SETTER(gui2_theme, std::string, std::string(""))
524  PREF_GETTER_SETTER(show_tips, bool, true)
525  PREF_GETTER_SETTER(stop_music_in_background, bool, false)
526  PREF_GETTER_SETTER(tile_size, unsigned, 0)
527  PREF_GETTER_SETTER(mouse_scrolling, bool, true)
528  PREF_GETTER_SETTER(refresh_rate, int, -1)
529  PREF_GETTER_SETTER(animate_map, bool, true)
530  PREF_GETTER_SETTER(animate_water, bool, true)
531  PREF_GETTER_SETTER(minimap_movement_coding, bool, true)
532  PREF_GETTER_SETTER(minimap_terrain_coding, bool, true)
533  PREF_GETTER_SETTER(minimap_draw_units, bool, true)
534  PREF_GETTER_SETTER(minimap_draw_villages, bool, true)
535  PREF_GETTER_SETTER(minimap_draw_terrain, bool, true)
536  PREF_GETTER_SETTER(show_side_colors, bool, true)
537  PREF_GETTER_SETTER(grid, bool, false)
538  PREF_GETTER_SETTER(disable_auto_moves, bool, false)
539  PREF_GETTER_SETTER(damage_prediction_allow_monte_carlo_simulation, bool, true)
540  PREF_GETTER_SETTER(campaign_rng_mode, std::string, std::string("biased"))
541  PREF_GETTER_SETTER(addon_manager_saved_order_name, std::string, std::string(""))
542  PREF_GETTER_SETTER(selected_achievement_group, std::string, std::string(""))
543  /** The most recently selected add-on id from the editor. May be an empty string. */
544  PREF_GETTER_SETTER(editor_chosen_addon, std::string, std::string(""))
545  PREF_GETTER_SETTER(_last_cache_cleaned_ver, std::string, std::string(""))
546  PREF_GETTER_SETTER(lobby_whisper_friends_only, bool, false)
547  PREF_GETTER_SETTER(fi_invert, bool, false)
548  PREF_GETTER_SETTER(fi_vacant_slots, bool, false)
549  PREF_GETTER_SETTER(fi_friends_in_game, bool, false)
550  PREF_GETTER_SETTER(fi_blocked_in_game, bool, false)
551  PREF_GETTER_SETTER(editor_auto_update_transitions, int, pref_constants::TRANSITION_UPDATE_PARTIAL)
552  PREF_GETTER_SETTER(editor_draw_terrain_codes, bool, false)
553  PREF_GETTER_SETTER(editor_draw_hex_coordinates, bool, false)
554  PREF_GETTER_SETTER(editor_draw_num_of_bitmaps, bool, false)
555  PREF_GETTER_SETTER(turn_dialog, bool, false)
556  PREF_GETTER_SETTER(enable_planning_mode_on_start, bool, false)
557  PREF_GETTER_SETTER(hide_whiteboard, bool, false)
558  PREF_GETTER_SETTER(allow_observers, bool, true)
559  PREF_GETTER_SETTER(shuffle_sides, bool, false)
560  PREF_GETTER_SETTER(random_faction_mode, std::string, std::string(""))
561  PREF_GETTER_SETTER(mp_use_map_settings, bool, true)
562  PREF_GETTER_SETTER(mp_server_warning_disabled, int, 0)
563  PREF_GETTER_SETTER(mp_random_start_time, bool, true)
564  PREF_GETTER_SETTER(mp_fog, bool, true)
565  PREF_GETTER_SETTER(mp_shroud, bool, true)
566  PREF_GETTER_SETTER(mp_turns, int, 0)
567  PREF_GETTER_SETTER(skip_mp_replay, bool, false)
568  PREF_GETTER_SETTER(blindfold_replay, bool, false)
569  PREF_GETTER_SETTER(mp_countdown, bool, false)
570  PREF_GETTER_SETTER(mp_era, std::string, std::string(""))
571  PREF_GETTER_SETTER(mp_level, std::string, std::string(""))
572  PREF_GETTER_SETTER(mp_level_type, int, 0)
573  PREF_GETTER_SETTER(mp_connect_type, int, 0)
574  PREF_GETTER_SETTER(skip_ai_moves, bool, false)
575  PREF_GETTER_SETTER(save_replays, bool, true)
576  PREF_GETTER_SETTER(delete_saves, bool, false)
577  PREF_GETTER_SETTER(ask_delete, bool, true)
578  PREF_GETTER_SETTER(ally_sighted_interrupts, bool, true)
579  PREF_GETTER_SETTER(auto_save_max, int, 10)
580  PREF_GETTER_SETTER(floating_labels, bool, true)
581  PREF_GETTER_SETTER(chat_timestamp, bool, false)
582  PREF_GETTER_SETTER(chat_lines, int, 6)
583  PREF_GETTER_SETTER(show_all_units_in_help, bool, false)
584  PREF_GETTER_SETTER(custom_command, std::string, std::string(""))
585  PREF_GETTER_SETTER(player_joins_sound, bool, true)
586  PREF_GETTER_SETTER(player_joins_lobby, bool, false)
587  PREF_GETTER_SETTER(player_joins_notif, bool, false)
588  PREF_GETTER_SETTER(player_leaves_sound, bool, true)
589  PREF_GETTER_SETTER(player_leaves_lobby, bool, false)
590  PREF_GETTER_SETTER(player_leaves_notif, bool, false)
591  PREF_GETTER_SETTER(private_message_sound, bool, true)
592  PREF_GETTER_SETTER(private_message_lobby, bool, desktop::notifications::available())
593  PREF_GETTER_SETTER(private_message_notif, bool, true)
594  PREF_GETTER_SETTER(friend_message_sound, bool, false)
595  PREF_GETTER_SETTER(friend_message_lobby, bool, false)
596  PREF_GETTER_SETTER(friend_message_notif, bool, false)
597  PREF_GETTER_SETTER(public_message_sound, bool, false)
598  PREF_GETTER_SETTER(public_message_lobby, bool, false)
599  PREF_GETTER_SETTER(public_message_notif, bool, false)
600  PREF_GETTER_SETTER(server_message_sound, bool, true)
601  PREF_GETTER_SETTER(server_message_lobby, bool, false)
602  PREF_GETTER_SETTER(server_message_notif, bool, true)
603  PREF_GETTER_SETTER(ready_for_start_sound, bool, true)
604  PREF_GETTER_SETTER(ready_for_start_lobby, bool, desktop::notifications::available())
605  PREF_GETTER_SETTER(ready_for_start_notif, bool, false)
606  PREF_GETTER_SETTER(game_has_begun_sound, bool, true)
607  PREF_GETTER_SETTER(game_has_begun_lobby, bool, desktop::notifications::available())
608  PREF_GETTER_SETTER(game_has_begun_notif, bool, false)
609  PREF_GETTER_SETTER(turn_changed_sound, bool, true)
610  PREF_GETTER_SETTER(turn_changed_lobby, bool, desktop::notifications::available())
611  PREF_GETTER_SETTER(turn_changed_notif, bool, false)
612  PREF_GETTER_SETTER(game_created_sound, bool, true)
613  PREF_GETTER_SETTER(game_created_lobby, bool, desktop::notifications::available())
614  PREF_GETTER_SETTER(game_created_notif, bool, true)
615  PREF_GETTER_SETTER(editor_help_text_shown, bool, true)
616  PREF_GETTER_SETTER(show_attack_miss_indicator, bool, false)
617  PREF_GETTER_SETTER(simd_enabled, bool, true)
618 #undef PREF_GETTER_SETTER
619  void clear_mp_alert_prefs();
620 
621 private:
622  prefs();
623  // don't move, assign, or copy a singleton
624  prefs(const prefs& p) = delete;
625  prefs& operator=(const prefs& p) = delete;
626  prefs(const prefs&& p) = delete;
627  prefs& operator=(const prefs&& p) = delete;
628 
629  inline static bool no_preferences_save = false;
630 
632  bool fps_;
633  std::map<std::string, std::set<std::string>> completed_campaigns_;
634  std::set<std::string> encountered_units_set_;
635  std::set<t_translation::terrain_code> encountered_terrains_set_;
636  std::map<std::string, std::vector<std::string>> history_map_;
637  std::map<std::string, preferences::acquaintance> acquaintances_;
640  std::vector<std::string> mp_modifications_;
642  std::vector<std::string> sp_modifications_;
645  std::vector<preferences::login_info> credentials_;
646  std::vector<preferences::option> advanced_prefs_;
647 
648  void load_preferences();
650  void load_credentials();
651  void save_credentials();
652  void clear_credentials();
653 
654  void set_child(const std::string& key, const config& val);
655  optional_const_config get_child(const std::string &key);
656  std::string get(const std::string& key, const std::string& def);
657  config::attribute_value get_as_attribute(const std::string& key);
658 
659  std::string get_system_username();
660  /**
661  * Encrypts the value of @a text using @a key and a hard coded IV using AES.
662  * Max size of @a text must not be larger than 1008 bytes.
663  *
664  * NOTE: This is not meant to provide strong protections against a determined attacker.
665  * This is meant to hide the passwords from malware scanning files for passwords, family/friends poking around, etc.
666  *
667  * @param text The original unencrypted data.
668  * @param key The value to use to encrypt the data. See build_key() for key generation.
669  * @return secure_buffer The encrypted data.
670  */
672  /**
673  * Same as aes_encrypt(), except of course it takes encrypted data as an argument and returns decrypted data.
674  */
676  /**
677  * 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.
678  * 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.
679  *
680  * @param server The server being logged into.
681  * @param login The username being used to login.
682  * @return secure_buffer The data to be used as the encryption key.
683  */
684  preferences::secure_buffer build_key(const std::string& server, const std::string& login);
687 
688  std::set<std::string> unknown_synced_attributes_;
689  std::set<std::string> unknown_unsynced_attributes_;
690  std::set<std::string> unknown_synced_children_;
691  std::set<std::string> unknown_unsynced_children_;
692 
693  // a bit verbose, but it being a compile time error if a preference hasn't been added is nice
694  static constexpr std::array synced_attributes_{
695  prefs_list::player_joins_sound,
696  prefs_list::player_joins_notif,
697  prefs_list::player_joins_lobby,
698  prefs_list::player_leaves_sound,
699  prefs_list::player_leaves_notif,
700  prefs_list::player_leaves_lobby,
701  prefs_list::private_message_sound,
702  prefs_list::private_message_notif,
703  prefs_list::private_message_lobby,
704  prefs_list::friend_message_sound,
705  prefs_list::friend_message_notif,
706  prefs_list::friend_message_lobby,
707  prefs_list::public_message_sound,
708  prefs_list::public_message_notif,
709  prefs_list::public_message_lobby,
710  prefs_list::server_message_sound,
711  prefs_list::server_message_notif,
712  prefs_list::server_message_lobby,
713  prefs_list::ready_for_start_sound,
714  prefs_list::ready_for_start_notif,
715  prefs_list::ready_for_start_lobby,
716  prefs_list::game_has_begun_sound,
717  prefs_list::game_has_begun_notif,
718  prefs_list::game_has_begun_lobby,
719  prefs_list::turn_changed_sound,
720  prefs_list::turn_changed_notif,
721  prefs_list::turn_changed_lobby,
722  prefs_list::game_created_sound,
723  prefs_list::game_created_notif,
724  prefs_list::game_created_lobby,
725  prefs_list::_last_cache_cleaned_ver,
726  prefs_list::addon_manager_saved_order_direction,
727  prefs_list::addon_manager_saved_order_name,
728  prefs_list::alias,
729  prefs_list::allow_observers,
731  prefs_list::ally_sighted_interrupts,
732  prefs_list::auto_save_max,
733  prefs_list::blindfold_replay,
734  prefs_list::campaign_rng_mode,
735  prefs_list::campaign_server,
736  prefs_list::chat_lines,
737  prefs_list::chat_timestamp,
738  prefs_list::confirm_end_turn,
739  prefs_list::custom_command,
740  prefs_list::delete_saves,
741  prefs_list::disable_auto_moves,
742  prefs_list::editor_auto_update_transitions,
743  prefs_list::editor_draw_hex_coordinates,
744  prefs_list::editor_draw_num_of_bitmaps,
745  prefs_list::editor_draw_terrain_codes,
746  prefs_list::enable_planning_mode_on_start,
747  prefs_list::encountered_terrain_list,
748  prefs_list::encountered_units,
750  prefs_list::fi_blocked_in_game,
751  prefs_list::fi_friends_in_game,
752  prefs_list::fi_invert,
753  prefs_list::fi_vacant_slots,
754  prefs_list::floating_labels,
755  prefs_list::grid,
756  prefs_list::hide_whiteboard,
757  prefs_list::host,
758  prefs_list::idle_anim,
759  prefs_list::idle_anim_rate,
761  prefs_list::lobby_whisper_friends_only,
762  prefs_list::locale,
763  prefs_list::login,
764  prefs_list::message_bell,
765  prefs_list::minimap_draw_terrain,
766  prefs_list::minimap_draw_units,
767  prefs_list::minimap_draw_villages,
768  prefs_list::minimap_movement_coding,
769  prefs_list::minimap_terrain_coding,
771  prefs_list::mp_countdown,
772  prefs_list::mp_countdown_action_bonus,
773  prefs_list::mp_countdown_init_time,
774  prefs_list::mp_countdown_reservoir_time,
775  prefs_list::mp_countdown_turn_bonus,
776  prefs_list::mp_fog,
777  prefs_list::mp_level_type,
778  prefs_list::mp_connect_type,
779  prefs_list::mp_random_start_time,
780  prefs_list::mp_server_warning_disabled,
781  prefs_list::mp_shroud,
782  prefs_list::mp_turns,
783  prefs_list::mp_use_map_settings,
784  prefs_list::mp_village_gold,
785  prefs_list::mp_village_support,
786  prefs_list::mp_xp_modifier,
787  prefs_list::music,
790  prefs_list::remember_password,
791  prefs_list::save_replays,
792  prefs_list::scroll,
793  prefs_list::scroll_threshold,
799  prefs_list::show_side_colors,
802  prefs_list::shuffle_sides,
803  prefs_list::skip_ai_moves,
804  prefs_list::skip_mp_replay,
805  prefs_list::sound,
806  prefs_list::stop_music_in_background,
807  prefs_list::turbo,
808  prefs_list::turbo_speed,
810  prefs_list::turn_dialog,
811  prefs_list::ui_sound,
812  prefs_list::unit_standing_animations,
814  prefs_list::ask_delete,
815  prefs_list::chat_message_aging,
816  prefs_list::color_cursors,
817  prefs_list::compress_saves,
818  prefs_list::confirm_load_save_from_different_version,
819  prefs_list::damage_prediction_allow_monte_carlo_simulation,
820  prefs_list::editor_max_recent_files,
821  prefs_list::keepalive_timeout,
822  prefs_list::lobby_auto_open_whisper_windows,
823  prefs_list::middle_click_scrolls,
824  prefs_list::mouse_scrolling,
825  prefs_list::scroll_to_action,
826  prefs_list::scroll_when_mouse_outside,
827  prefs_list::show_all_units_in_help,
828  prefs_list::show_combat,
829  prefs_list::show_deprecation,
830  prefs_list::show_attack_miss_indicator,
831  prefs_list::use_twelve_hour_clock_format,
832  prefs_list::mp_era,
833  prefs_list::mp_level,
834  prefs_list::mp_modifications,
835  prefs_list::selected_achievement_group,
836  prefs_list::sp_modifications,
837  prefs_list::animate_map,
838  prefs_list::animate_water,
839  prefs_list::addon_icons,
844  };
845  static constexpr std::array synced_children_{
846  prefs_list::acquaintance,
847  prefs_list::achievements,
848  prefs_list::completed_campaigns,
849  prefs_list::history,
850  prefs_list::options,
851  prefs_list::server,
852  prefs_list::game_preset,
853  };
854  static constexpr std::array unsynced_attributes_{
855  prefs_list::auto_pixel_scale,
856  prefs_list::core,
857  prefs_list::refresh_rate,
858  prefs_list::editor_chosen_addon,
859  prefs_list::gui2_theme,
860  prefs_list::show_tips,
861  prefs_list::mp_server_program_name,
862  prefs_list::pixel_scale,
863  prefs_list::theme,
865  prefs_list::vsync,
866  prefs_list::xresolution,
867  prefs_list::yresolution,
868  prefs_list::font_scale,
869  prefs_list::bell_volume,
870  prefs_list::music_volume,
871  prefs_list::simd_enabled,
872  prefs_list::sound_volume,
873  prefs_list::ui_volume,
874  prefs_list::fullscreen,
875  prefs_list::maximized,
876  };
877  static constexpr std::array unsynced_children_{
878  prefs_list::editor_recent_files,
879  prefs_list::dir_bookmarks,
880  };
881  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!");
882 };
883 
884 //
885 // might be overkill, but it does best limit the private functionality being exposed
886 //
887 
889 {
890  friend class preferences_dialog;
891 
892  static void set(const std::string& pref, bool value)
893  {
894  prefs::get().preferences_[pref] = value;
895  }
896  static void set(const std::string& pref, int value)
897  {
898  prefs::get().preferences_[pref] = value;
899  }
900  static void set(const std::string& pref, const std::string& value)
901  {
902  prefs::get().preferences_[pref] = value;
903  }
904 
905  static bool get(const std::string& pref, bool def)
906  {
907  return prefs::get().preferences_[pref].to_bool(def);
908  }
909  static int get(const std::string& pref, int def)
910  {
911  return prefs::get().preferences_[pref].to_int(def);
912  }
913  static std::string get(const std::string& pref, const std::string& def)
914  {
915  return prefs::get().get(pref, def);
916  }
917 };
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:97
acquaintance(const std::string &nick, const std::string &status, const std::string &notes)
Definition: preferences.hpp:86
std::string notes_
notes on the acquaintance
const std::string & get_status() const
Definition: preferences.hpp:94
const std::string & get_nick() const
Definition: preferences.hpp:93
std::string status_
status (e.g., "friend", "ignore")
const std::string & get_notes() const
Definition: preferences.hpp:95
acquaintance(const config &cfg)
Definition: preferences.hpp:79
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_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()
sound::volume music_volume()
prefs(const prefs &&p)=delete
void set_sound_volume(sound::volume vol)
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_
sound::volume sound_volume()
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_
sound::volume bell_volume()
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 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_
void set_reach_map_tint_opacity(const int new_opacity)
sound::volume ui_volume()
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 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)
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)
void set_music_volume(sound::volume vol)
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 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()
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_
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 &)
void set_bell_volume(sound::volume vol)
bool remember_password()
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()
bool use_twelve_hour_clock_format()
static bool no_preferences_save
int reach_map_border_opacity()
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()
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()
void set_ui_volume(sound::volume vol)
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
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:71
Audio output for sound and music.
Definition: preferences.hpp:69
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
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition: sound.cpp:138
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