wesnoth/src/game_classification.cpp
Pentarctagon 971073055e
Refactor the preferences into a proper singleton. (#8930)
The current preferences handling is a mess:
* it's essentially a global config object that anything can modify in any way the caller wants, which is managed across multiple source files which have their own oddities and interdependencies.
* the general preferences has its own bit of SDL event handling and while I get the idea behind `events::sdl_handler` there's no reason to have SDL events handled in the preferences instead of just calling the relevant preferences setter for each event when it happens.
* the general preferences is where most of the preferences are handled and has its `base_manager` struct, which is part of the `manager` struct in the game preferences, which is then implicitly initialized as part of game_launcher's constructor.
* the editor preferences are the only preferences in a sub-namespace `preferences::editor` while all other preferences are just in the `preferences` namespace.
* the display, editor, and lobby preferences are all dependent on including the game preferences, the credentials are dependent on including the general preferences (but not the game preferences), the game preferences are dependent on including the general preferences, and the advanced preferences are entirely their own thing which is dependent on none of the other preference functionality and manages its own singleton.
* nothing checks whether the preferences file has actually been loaded before allowing values to be read from or written to the preferences config - if you attempt to get a value too early in wesnoth's initialization it will silently just give you whatever the default value for that preference happens to be.

With this there is instead a single access point (with exceptions handled via friend functions/classes), all predefined preferences are accessed via their own setter/getter, and all mainline preferences are defined in a single file (preference_list.hpp) so it's easily findable what preferences exist and where they're used. Having the list of all mainline preferences listed out also allows the lua preferences API to provide that full list rather than just the list of the preferences that have been set so far. Also it now checks for whether the location of the preferences file is known before attempting to load the preferences file and asserts if someone attempts to use the preferences too early.
2024-06-09 11:34:09 -05:00

167 lines
4.7 KiB
C++

/*
Copyright (C) 2003 - 2024
by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "game_classification.hpp"
#include "config.hpp"
#include "log.hpp"
#include "preferences/preferences.hpp"
#include "serialization/string_utils.hpp"
#include "game_version.hpp"
#include "game_config_manager.hpp"
#include <list>
static lg::log_domain log_engine("engine");
#define ERR_NG LOG_STREAM(err, log_engine)
#define WRN_NG LOG_STREAM(warn, log_engine)
#define LOG_NG LOG_STREAM(info, log_engine)
#define DBG_NG LOG_STREAM(debug, log_engine)
/** The default difficulty setting for campaigns. */
const std::string DEFAULT_DIFFICULTY("NORMAL");
game_classification::game_classification(const config& cfg)
: label(cfg["label"])
, version(cfg["version"])
, type(campaign_type::get_enum(cfg["campaign_type"].str()).value_or(campaign_type::type::scenario))
, campaign_define(cfg["campaign_define"])
, campaign_xtra_defines(utils::split(cfg["campaign_extra_defines"]))
, scenario_define(cfg["scenario_define"])
, era_define(cfg["era_define"])
, mod_defines(utils::split(cfg["mod_defines"]))
, active_mods(utils::split(cfg["active_mods"]))
, era_id(cfg["era_id"])
, campaign(cfg["campaign"])
, campaign_name(cfg["campaign_name"])
, abbrev(cfg["abbrev"])
, end_credits(cfg["end_credits"].to_bool(true))
, end_text(cfg["end_text"])
, end_text_duration(std::clamp<unsigned>(cfg["end_text_duration"].to_unsigned(0), 0, 5000))
, difficulty(cfg["difficulty"].empty() ? DEFAULT_DIFFICULTY : cfg["difficulty"].str())
, random_mode(cfg["random_mode"])
, oos_debug(cfg["oos_debug"].to_bool(false))
{
}
config game_classification::to_config() const
{
config cfg;
cfg["label"] = label;
cfg["version"] = game_config::wesnoth_version.str();
cfg["campaign_type"] = campaign_type::get_string(type);
cfg["campaign_define"] = campaign_define;
cfg["campaign_extra_defines"] = utils::join(campaign_xtra_defines);
cfg["scenario_define"] = scenario_define;
cfg["era_define"] = era_define;
cfg["mod_defines"] = utils::join(mod_defines);
cfg["active_mods"] = utils::join(active_mods);
cfg["era_id"] = era_id;
cfg["campaign"] = campaign;
cfg["campaign_name"] = campaign_name;
cfg["abbrev"] = abbrev;
cfg["end_credits"] = end_credits;
cfg["end_text"] = end_text;
cfg["end_text_duration"] = std::to_string(end_text_duration);
cfg["difficulty"] = difficulty;
cfg["random_mode"] = random_mode;
cfg["oos_debug"] = oos_debug;
cfg["core"] = prefs::get().core_id();
return cfg;
}
std::string game_classification::get_tagname() const
{
if(is_multiplayer()) {
return campaign.empty() ? campaign_type::multiplayer : campaign_type::scenario;
}
if(is_tutorial()) {
return campaign_type::scenario;
}
return campaign_type::get_string(type);
}
namespace
{
// helper objects for saved_game::expand_mp_events()
struct modevents_entry
{
modevents_entry(const std::string& _type, const std::string& _id)
: type(_type)
, id(_id)
{
}
std::string type;
std::string id;
};
}
std::set<std::string> game_classification::active_addons(const std::string& scenario_id) const
{
//FIXME: this doesn't include mods from the current scenario.
std::list<modevents_entry> mods;
std::set<std::string> loaded_resources;
std::set<std::string> res;
for(const auto& mod : active_mods) {
mods.emplace_back("modification", mod);
}
// We don't want the error message below if there is no era (= if this is a sp game).
if(!era_id.empty()) {
mods.emplace_back(get_tagname(), scenario_id);
}
if(!era_id.empty()) {
mods.emplace_back("era", era_id);
}
if(!campaign.empty()) {
mods.emplace_back("campaign", campaign);
}
while(!mods.empty()) {
const modevents_entry& current = mods.front();
if(current.type == "resource") {
if(!loaded_resources.insert(current.id).second) {
mods.pop_front();
continue;
}
}
if(auto cfg = game_config_manager::get()->game_config().find_child(current.type, "id", current.id)) {
if(!cfg["addon_id"].empty()) {
res.insert(cfg["addon_id"]);
}
for (const config& load_res : cfg->child_range("load_resource")) {
mods.emplace_back("resource", load_res["id"].str());
}
} else {
ERR_NG << "Unable to find config for content " << current.id << " of type " << current.type;
}
mods.pop_front( );
}
DBG_NG << "Active content for game set to:";
for(const std::string& mod : res) {
DBG_NG << mod;
}
return res;
}