wesnoth/src/achievements.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

164 lines
5.3 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 "achievements.hpp"
#include "filesystem.hpp"
#include "log.hpp"
#include "preferences/preferences.hpp"
#include "serialization/parser.hpp"
#include "serialization/preprocessor.hpp"
static lg::log_domain log_config("config");
#define ERR_CONFIG LOG_STREAM(err, log_config)
sub_achievement::sub_achievement(const config& cfg, bool achieved)
: id_(cfg["id"].str())
, description_(cfg["description"].t_str())
, icon_(cfg["icon"].str()+"~GS()")
, icon_completed_(cfg["icon"].str())
, achieved_(achieved)
{}
achievement::achievement(const config& cfg, const std::string& content_for, bool achieved, int progress)
: id_(cfg["id"].str())
, name_(cfg["name"].t_str())
, name_completed_(cfg["name_completed"].t_str())
, description_(cfg["description"].t_str())
, description_completed_(cfg["description_completed"].t_str())
, icon_(cfg["icon"].str()+"~GS()")
, icon_completed_(cfg["icon_completed"].str())
, hidden_(cfg["hidden"].to_bool())
, achieved_(achieved)
, max_progress_(cfg["max_progress"].to_int(0))
, current_progress_(progress)
, sound_path_(cfg["sound"].str())
, sub_achievements_()
{
if(name_completed_.empty()) {
name_completed_ = name_;
}
if(description_completed_.empty()) {
description_completed_ = description_;
}
if(icon_completed_.empty()) {
// avoid the ~GS() appended to icon_
icon_completed_ = cfg["icon"].str();
}
for(const config& sub_ach : cfg.child_range("sub_achievement"))
{
std::string sub_id = sub_ach["id"].str();
if(sub_id.empty()) {
ERR_CONFIG << "Achievement " << id_ << " has a sub-achievement missing the id attribute:\n" << sub_ach.debug();
} else {
sub_achievements_.emplace_back(sub_ach, achieved_ || prefs::get().sub_achievement(content_for, id_, sub_id));
max_progress_++;
}
}
}
achievement_group::achievement_group(const config& cfg)
: display_name_(cfg["display_name"].t_str())
, content_for_(cfg["content_for"].str())
, achievements_()
{
for(const config& ach : cfg.child_range("achievement")) {
std::string id = ach["id"].str();
if(id.empty()) {
ERR_CONFIG << content_for_ + " achievement missing id attribute:\n" << ach.debug();
} else if(id.find(',') != std::string::npos) {
ERR_CONFIG << content_for_ + " achievement id " << id << " contains a comma, skipping.";
continue;
} else {
achievements_.emplace_back(ach, content_for_, prefs::get().achievement(content_for_, id), prefs::get().progress_achievement(content_for_, id));
}
}
}
achievements::achievements()
: achievement_list_()
{
reload();
}
/**
* Reads the mainline achievements.cfg and then all the achievements of each installed add-on.
*
* This is intentionally handled separately from other WML loading so that:
* a) All achievements and their status are able to be displayed on the main menu right after Wesnoth starts and regardless of which add-ons are active.
* b) Add-ons can add additional achievements to other content, whether UMC or mainline. For example, a modification that adds more achievements for mainline campaigns.
*
* NOTE: These are *not* in any way related to Steam achievements!
*/
void achievements::reload()
{
achievement_list_.clear();
// mainline
try {
config cfg = read_achievements_file(game_config::path + "/data/achievements.cfg");
process_achievements_file(cfg, "Mainline");
} catch(const game::error& e) {
ERR_CONFIG << "Error processing mainline achievements, ignoring: " << e.what();
}
// add-ons
std::vector<std::string> dirs;
filesystem::get_files_in_dir(filesystem::get_addons_dir(), nullptr, &dirs);
for(const std::string& dir : dirs) {
try {
config cfg = read_achievements_file(filesystem::get_addons_dir() + "/" + dir + "/achievements.cfg");
process_achievements_file(cfg, dir);
} catch(const game::error& e) {
ERR_CONFIG << "Error processing add-on " << dir << " achievements, ignoring: " << e.what();
}
}
}
/**
* Reads an achievements.cfg file into a config.
*
* @param path The path to the achievements.cfg file.
* @return The config containing all the achievements.
*/
config achievements::read_achievements_file(const std::string& path)
{
config cfg;
if(filesystem::file_exists(path)) {
filesystem::scoped_istream stream = preprocess_file(path);
read(cfg, *stream);
}
return cfg;
}
/**
* Processes a config object to add new achievements to @a achievement_list_.
*
* @param cfg The config containing additional achievements.
* @param content_source The source of the additional achievements - either mainline or an add-on.
*/
void achievements::process_achievements_file(const config& cfg, const std::string& content_source)
{
for(const config& achgrp : cfg.child_range("achievement_group")) {
if(achgrp["content_for"].str().empty()) {
ERR_CONFIG << content_source + " achievement_group missing content_for attribute:\n" << achgrp.debug();
continue;
}
achievement_list_.emplace_back(achgrp);
}
}