The Battle for Wesnoth  1.19.0+dev
advanced.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 - 2024
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 #include "preferences/advanced.hpp"
16 
17 #include "game_config_view.hpp"
18 #include "game_version.hpp"
19 #include "gettext.hpp"
20 #include "log.hpp"
21 
22 #include <cassert>
23 
24 static lg::log_domain advanced_preferences("advanced_preferences");
25 #define ERR_ADV LOG_STREAM(err, advanced_preferences)
26 
27 namespace preferences
28 {
29 static std::unique_ptr<preferences::advanced_manager> singleton = nullptr;
30 
32  : prefs()
33 {
34  for(const config& pref : gc.child_range("advanced_preference")) {
35  try {
36  prefs.emplace_back(pref);
37  } catch(const std::invalid_argument& e) {
38  ERR_ADV << e.what();
39  continue;
40  }
41  }
42 
43  // show_deprecation has a different default on the dev branch
44  if(game_config::wesnoth_version.is_dev_version()) {
45  for(option& op : prefs) {
46  if(op.field == "show_deprecation") {
47  op.cfg["default"] = true;
48  }
49  }
50  }
51 
52  std::sort(prefs.begin(), prefs.end(),
53  [](const auto& lhs, const auto& rhs) { return translation::icompare(lhs.name, rhs.name) < 0; });
54 }
55 
57 {
58 }
59 
61  : type()
62  , name(pref["name"].t_str())
63  , description(pref["description"].t_str())
64  , field(pref["field"].str())
65  , cfg(pref)
66 {
67  const std::string p_type = cfg["type"];
68 
69  if(p_type == "boolean") {
71  } else if(p_type == "int") {
73  } else if(p_type == "combo") {
75  } else if(p_type == "custom") {
77  } else {
78  throw std::invalid_argument("Unknown type '" + p_type + "' for advanced preference " + name);
79  }
80 }
81 
83 {
84  singleton = std::make_unique<advanced_manager>(gc);
85 }
86 
88 {
89  assert(singleton && "Advanced preference manager singleton is null!");
90  return singleton->get_preferences();
91 }
92 
93 } // namespace preferences
#define ERR_ADV
Definition: advanced.cpp:25
static lg::log_domain advanced_preferences("advanced_preferences")
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A class grating read only view to a vector of config objects, viewed as one config with all children ...
config_array_view child_range(config_key_type key) const
advanced_manager(const game_config_view &gc)
Definition: advanced.cpp:31
Interfaces for manipulating version numbers of engine, add-ons, etc.
Standard logging facilities (interface).
const version_info wesnoth_version(VERSION)
Modify, read and display user preferences.
std::vector< advanced_manager::option > advanced_pref_list
Definition: advanced.hpp:68
static std::unique_ptr< preferences::advanced_manager > singleton
Definition: advanced.cpp:29
void init_advanced_manager(const game_config_view &gc)
Initializes the manager singleton.
Definition: advanced.cpp:82
const advanced_pref_list & get_advanced_preferences()
Gets a list of the available advanced preferences.
Definition: advanced.cpp:87
config cfg
The full config, including type-specific options.
Definition: advanced.hpp:53
avd_type type
The preference type.
Definition: advanced.hpp:41
t_string name
Displayed name.
Definition: advanced.hpp:44
#define e