Clean-up and polish of game_config_manager game_controller class.

Add extra braces.
Substitute for loop with BOOST_FOREACH.
Update CMakeLists.txt and VC9/wesnoth.vcproj.
Split some long lines.
Change pass by value to pass by reference.
Fix formatting of some comments.
Introduced enums for load_game_cfg().
This commit is contained in:
Andrius Silinskas 2013-06-07 11:10:46 +01:00
parent 2863818293
commit 377e9adc8e
6 changed files with 152 additions and 85 deletions

View file

@ -17774,6 +17774,14 @@
RelativePath="..\..\src\game_config.hpp"
>
</File>
<File
RelativePath="..\..\src\game_config_manager.cpp"
>
</File>
<File
RelativePath="..\..\src\game_config_manager.hpp"
>
</File>
<File
RelativePath="..\..\src\game_controller.cpp"
>

View file

@ -684,6 +684,7 @@ set(wesnoth-main_SRC
formula_function.cpp
formula_string_utils.cpp
formula_tokenizer.cpp
game_config_manager.cpp
game_controller.cpp
game_controller_abstract.cpp
game_controller_new.cpp

View file

@ -27,12 +27,16 @@
#include "resources.hpp"
#include "scripting/lua.hpp"
#include <boost/foreach.hpp>
static lg::log_domain log_config("config");
#define ERR_CONFIG LOG_STREAM(err, log_config)
#define WRN_CONFIG LOG_STREAM(warn, log_config)
#define LOG_CONFIG LOG_STREAM(info, log_config)
game_config_manager::game_config_manager(const commandline_options& cmdline_opts, game_display& display) :
game_config_manager::game_config_manager(
const commandline_options& cmdline_opts,
game_display& display) :
cmdline_opts_(cmdline_opts),
disp_(display),
game_config_(),
@ -54,12 +58,12 @@ game_config_manager::~game_config_manager()
resources::config_manager = NULL;
}
void game_config_manager::add_define(const std::string name, const bool add)
void game_config_manager::add_define(const std::string& name, const bool add)
{
defines_.push_back(std::make_pair(name, add));
}
void game_config_manager::add_cache_define(const std::string name)
void game_config_manager::add_cache_define(const std::string& name)
{
cache_.add_define(name);
}
@ -69,22 +73,28 @@ void game_config_manager::clear_cache_defines()
cache_.clear_defines();
}
bool game_config_manager::init_config(const bool force, const bool jump_to_editor)
bool game_config_manager::init_config(FORCE_RELOAD_CONFIG force_reload,
const bool jump_to_editor)
{
load_game_cfg(true, true, force);
load_game_cfg(SET_PATHS, CLEAR_CACHE, force_reload);
// make sure that multiplayer mode is set if command line parameter is selected
if (cmdline_opts_.multiplayer)
// Make sure that multiplayer mode is set
// if command line parameter is selected.
if (cmdline_opts_.multiplayer) {
cache_.add_define("MULTIPLAYER");
}
if (cmdline_opts_.test)
if (cmdline_opts_.test) {
cache_.add_define("TEST");
}
if (jump_to_editor)
if (jump_to_editor) {
cache_.add_define("EDITOR");
}
if (!cmdline_opts_.multiplayer && !cmdline_opts_.test && !jump_to_editor)
if (!cmdline_opts_.multiplayer && !cmdline_opts_.test && !jump_to_editor) {
cache_.add_define("TITLE_SCREEN");
}
game_config::load_config(game_config_.child("game_config"));
@ -100,82 +110,93 @@ bool game_config_manager::init_config(const bool force, const bool jump_to_edito
return true;
}
void game_config_manager::load_game_cfg(const bool set_bin_paths, const bool clear_cache_defines, const bool force)
void game_config_manager::load_game_cfg(
SET_BINARY_PATHS set_paths,
CLEAR_CACHE_DEFINES clear_cache,
FORCE_RELOAD_CONFIG force_reload)
{
if (clear_cache_defines)
if (clear_cache == CLEAR_CACHE)
cache_.clear_defines();
// scoped defines
typedef boost::shared_ptr<game_config::scoped_preproc_define> define_ptr;
std::deque<define_ptr> defines;
for(std::vector<std::pair<std::string, bool> >::iterator it = defines_.begin(); it != defines_.end(); ++it) {
define_ptr newdefine(new game_config::scoped_preproc_define((*it).first, (*it).second));
typedef std::pair<std::string, bool> def_pair;
BOOST_FOREACH(const def_pair& define, defines_) {
define_ptr newdefine
(new game_config::scoped_preproc_define(define.first,
define.second));
defines.push_back(newdefine);
}
defines_.clear();
// make sure that 'debug mode' symbol is set if command line parameter is selected
// also if we're in multiplayer and actual debug mode is disabled
// Make sure that 'debug mode' symbol is set
// if command line parameter is selected
// also if we're in multiplayer and actual debug mode is disabled.
if (game_config::debug || game_config::mp_debug) {
cache_.add_define("DEBUG_MODE");
}
if (!game_config_.empty() && !force && old_defines_map_ == cache_.get_preproc_map())
return; // game_config already holds requested config in memory
if (!game_config_.empty() &&
(force_reload == NO_FORCE_RELOAD)
&& old_defines_map_ == cache_.get_preproc_map())
return; // Game_config already holds requested config in memory.
old_defines_map_ = cache_.get_preproc_map();
loadscreen::global_loadscreen_manager loadscreen_manager(disp_.video());
cursor::setter cur(cursor::WAIT);
// The loadscreen will erase the titlescreen
// NOTE: even without loadscreen, needed after MP lobby
// The loadscreen will erase the titlescreen.
// NOTE: even without loadscreen, needed after MP lobby.
try {
/**
* Read all game configs
* First we should load data/
* Then handle terrains so that they are last loaded from data/
* 2nd everything in userdata
**/
// Read all game configs.
// First we should load data/,
// then handle terrains so that they are last loaded from data/.
// 2nd everything in userdata.
loadscreen::start_stage("verify cache");
data_tree_checksum();
loadscreen::start_stage("create cache");
// start transaction so macros are shared
// Start transaction so macros are shared.
game_config::config_cache_transaction main_transaction;
cache_.get_config(game_config::path +"/data", game_config_);
main_transaction.lock();
/* Put the gfx rules aside so that we can prepend the add-on
rules to them. */
// Put the gfx rules aside so that we can prepend the add-on
// rules to them.
config core_terrain_rules;
core_terrain_rules.splice_children(game_config_, "terrain_graphics");
// load usermade add-ons
// Load usermade add-ons.
const std::string user_campaign_dir = get_addon_campaigns_dir();
std::vector< std::string > error_addons;
// Scan addon directories
// Scan addon directories.
std::vector<std::string> user_dirs;
// Scan for standalone files
// Scan for standalone files.
std::vector<std::string> user_files;
// The addons that we'll actually load
// The addons that we'll actually load.
std::vector<std::string> addons_to_load;
get_files_in_dir(user_campaign_dir,&user_files,&user_dirs,ENTIRE_FILE_PATH);
get_files_in_dir(user_campaign_dir,&user_files,&user_dirs,
ENTIRE_FILE_PATH);
std::stringstream user_error_log;
// Append the $user_campaign_dir/*.cfg files to addons_to_load.
for(std::vector<std::string>::const_iterator uc = user_files.begin(); uc != user_files.end(); ++uc) {
const std::string file = *uc;
BOOST_FOREACH(const std::string& uc, user_files) {
const std::string file = uc;
const int size_minus_extension = file.size() - 4;
if(file.substr(size_minus_extension, file.size()) == ".cfg") {
bool ok = true;
// Allowing it if the dir doesn't exist, for the single-file add-on.
// Allowing it if the dir doesn't exist,
// for the single-file add-on.
if(file_exists(file.substr(0, size_minus_extension))) {
// Unfortunately, we create the dir plus _info.cfg ourselves on download
// Unfortunately, we create the dir plus
// _info.cfg ourselves on download.
std::vector<std::string> dirs, files;
get_files_in_dir(file.substr(0, size_minus_extension), &files, &dirs);
get_files_in_dir(file.substr(0, size_minus_extension),
&files, &dirs);
if(dirs.size() > 0)
ok = false;
if(files.size() > 1)
@ -185,7 +206,8 @@ void game_config_manager::load_game_cfg(const bool set_bin_paths, const bool cle
}
if(!ok) {
const int userdata_loc = file.find("data/add-ons") + 5;
ERR_CONFIG << "error reading usermade add-on '" << file << "'\n";
ERR_CONFIG << "error reading usermade add-on '"
<< file << "'\n";
error_addons.push_back(file);
user_error_log << "The format '~" << file.substr(userdata_loc) << "' is only for single-file add-ons, use '~" << file.substr(userdata_loc, size_minus_extension - userdata_loc) << "/_main.cfg' instead.\n";
} else
@ -194,31 +216,31 @@ void game_config_manager::load_game_cfg(const bool set_bin_paths, const bool cle
}
// Append the $user_campaign_dir/*/_main.cfg files to addons_to_load.
for(std::vector<std::string>::const_iterator uc = user_dirs.begin(); uc != user_dirs.end(); ++uc){
const std::string main_cfg = *uc + "/_main.cfg";
BOOST_FOREACH(const std::string& uc, user_dirs) {
const std::string main_cfg = uc + "/_main.cfg";
if (file_exists(main_cfg))
addons_to_load.push_back(main_cfg);
}
// Load the addons
for(std::vector<std::string>::const_iterator uc = addons_to_load.begin(); uc != addons_to_load.end(); ++uc) {
const std::string toplevel = *uc;
// Load the addons.
BOOST_FOREACH(const std::string& uc, addons_to_load) {
const std::string toplevel = uc;
try {
config umc_cfg;
cache_.get_config(toplevel, umc_cfg);
game_config_.append(umc_cfg);
} catch(config::error& err) {
ERR_CONFIG << "error reading usermade add-on '" << *uc << "'\n";
error_addons.push_back(*uc);
ERR_CONFIG << "error reading usermade add-on '" << uc << "'\n";
error_addons.push_back(uc);
user_error_log << err.message << "\n";
} catch(preproc_config::error& err) {
ERR_CONFIG << "error reading usermade add-on '" << *uc << "'\n";
error_addons.push_back(*uc);
ERR_CONFIG << "error reading usermade add-on '" << uc << "'\n";
error_addons.push_back(uc);
user_error_log << err.message << "\n";
} catch(io_exception&) {
ERR_CONFIG << "error reading usermade add-on '" << *uc << "'\n";
error_addons.push_back(*uc);
ERR_CONFIG << "error reading usermade add-on '" << uc << "'\n";
error_addons.push_back(uc);
}
}
if(error_addons.empty() == false) {
@ -226,8 +248,8 @@ void game_config_manager::load_game_cfg(const bool set_bin_paths, const bool cle
msg << _n("The following add-on had errors and could not be loaded:",
"The following add-ons had errors and could not be loaded:",
error_addons.size());
for(std::vector<std::string>::const_iterator i = error_addons.begin(); i != error_addons.end(); ++i) {
msg << "\n" << *i;
BOOST_FOREACH(const std::string& error_addon, error_addons) {
msg << "\n" << error_addon;
}
msg << '\n' << _("ERROR DETAILS:") << '\n' << user_error_log.str();
@ -253,7 +275,6 @@ void game_config_manager::load_game_cfg(const bool set_bin_paths, const bool cle
game_config::add_color_info(colorsys_info);
//set_unit_data();
loadscreen::start_stage("load unit types");
if (config &units = game_config_.child("units")) {
unit_types.set_config(units);
@ -272,19 +293,18 @@ void game_config_manager::load_game_cfg(const bool set_bin_paths, const bool cle
throw;
}
// set binary paths
if (set_bin_paths)
if (set_paths == SET_PATHS)
paths_manager_.set_paths(game_config());
}
void game_config_manager::reload_changed_game_config(const bool jump_to_editor)
{
// rebuild addon version info cache
// Rebuild addon version info cache.
refresh_addon_version_info_cache();
//force a reload of configuration information
// Force a reload of configuration information.
cache_.recheck_filetree_checksum();
old_defines_map_.clear();
clear_binary_paths_cache();
init_config(true, jump_to_editor);
init_config(FORCE_RELOAD, jump_to_editor);
}

View file

@ -23,24 +23,32 @@ class config;
class game_config_manager
{
public:
game_config_manager(const commandline_options& cmdline_opts, game_display& disp);
game_config_manager(const commandline_options& cmdline_opts,
game_display& disp);
~game_config_manager();
enum SET_BINARY_PATHS { SET_PATHS, NO_SET_PATHS };
enum CLEAR_CACHE_DEFINES { CLEAR_CACHE, NO_CLEAR_CACHE };
enum FORCE_RELOAD_CONFIG { FORCE_RELOAD, NO_FORCE_RELOAD };
const config& game_config() const { return game_config_; }
void add_define(const std::string name, const bool add = true);
void add_cache_define(const std::string name);
void add_define(const std::string& name, const bool add = true);
void add_cache_define(const std::string& name);
void clear_cache_defines();
bool init_config(const bool force = false, const bool jump_to_editor = false);
void load_game_cfg(const bool set_bin_paths, const bool clear_cache_defines, const bool force = false);
bool init_config(FORCE_RELOAD_CONFIG force_reload,
const bool jump_to_editor = false);
void load_game_cfg(SET_BINARY_PATHS set_paths,
CLEAR_CACHE_DEFINES clear_cache,
FORCE_RELOAD_CONFIG force_reload);
void reload_changed_game_config(const bool jump_to_editor = false);
private:
game_config_manager(const game_config_manager&);
void operator=(const game_config_manager&);
const commandline_options& cmdline_opts_;
game_display& disp_;

View file

@ -274,9 +274,11 @@ game_controller::game_controller(const commandline_options& cmdline_opts, const
}
}
bool game_controller::init_config(const bool force)
bool game_controller::init_config(
game_config_manager::FORCE_RELOAD_CONFIG force_reload)
{
return resources::config_manager->init_config(force, jump_to_editor_);
return resources::config_manager->
init_config(force_reload, jump_to_editor_);
}
bool game_controller::play_test()
@ -296,7 +298,10 @@ bool game_controller::play_test()
state_.classification().campaign_define = "TEST";
resources::config_manager->add_cache_define("TEST");
resources::config_manager->load_game_cfg(true, false);
resources::config_manager->load_game_cfg(
game_config_manager::SET_PATHS,
game_config_manager::NO_CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
try {
play_game(disp(),state_,game_config());
@ -314,7 +319,10 @@ bool game_controller::play_screenshot_mode()
}
resources::config_manager->add_define("EDITOR");
resources::config_manager->load_game_cfg(false, true);
resources::config_manager->load_game_cfg(
game_config_manager::NO_SET_PATHS,
game_config_manager::CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
const binary_paths_manager bin_paths_manager(game_config());
::init_textdomains(game_config());
@ -338,15 +346,21 @@ bool game_controller::load_game()
resources::config_manager->add_define(state_.classification().campaign_define, !state_.classification().campaign_define.empty());
resources::config_manager->add_define("MULTIPLAYER", state_.classification().campaign_define.empty() && (state_.classification().campaign_type == "multiplayer"));
for(std::vector<std::string>::const_iterator i = state_.classification().campaign_xtra_defines.begin();
i != state_.classification().campaign_xtra_defines.end(); ++i) {
resources::config_manager->add_define(*i);
BOOST_FOREACH(const std::string& xtra_def,
state_.classification().campaign_xtra_defines) {
resources::config_manager->add_define(xtra_def);
}
try {
resources::config_manager->load_game_cfg(true, true);
resources::config_manager->load_game_cfg(
game_config_manager::SET_PATHS,
game_config_manager::CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
} catch(config::error&) {
resources::config_manager->load_game_cfg(false, true);
resources::config_manager->load_game_cfg(
game_config_manager::NO_SET_PATHS,
game_config_manager::CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
return false;
}
@ -726,7 +740,10 @@ bool game_controller::play_multiplayer()
/* do */ {
resources::config_manager->add_define(state_.classification().campaign_define);
resources::config_manager->load_game_cfg(true, true);
resources::config_manager->load_game_cfg(
game_config_manager::SET_PATHS,
game_config_manager::CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
events::discard_input(); // prevent the "keylogger" effect
cursor::set(cursor::NORMAL);
clear_binary_paths_cache();
@ -799,7 +816,10 @@ bool game_controller::play_multiplayer_commandline()
state_.classification().campaign_define = "MULTIPLAYER";
resources::config_manager->add_define(state_.classification().campaign_define);
resources::config_manager->load_game_cfg(true, true);
resources::config_manager->load_game_cfg(
game_config_manager::SET_PATHS,
game_config_manager::CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
events::discard_input(); // prevent the "keylogger" effect
cursor::set(cursor::NORMAL);
clear_binary_paths_cache();
@ -842,14 +862,20 @@ void game_controller::launch_game(RELOAD_GAME_DATA reload)
if(reload == RELOAD_DATA) {
resources::config_manager->add_define(state_.classification().campaign_define, state_.classification().campaign_define.empty() == false);
for(std::vector<std::string>::const_iterator i = state_.classification().campaign_xtra_defines.begin();
i != state_.classification().campaign_xtra_defines.end(); ++i) {
resources::config_manager->add_define(*i);
BOOST_FOREACH(const std::string& xtra_def,
state_.classification().campaign_xtra_defines) {
resources::config_manager->add_define(xtra_def);
}
try {
resources::config_manager->load_game_cfg(false, false);
resources::config_manager->load_game_cfg(
game_config_manager::NO_SET_PATHS,
game_config_manager::NO_CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
} catch(config::error&) {
resources::config_manager->load_game_cfg(false, true);
resources::config_manager->load_game_cfg(
game_config_manager::NO_SET_PATHS,
game_config_manager::CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
return;
}
}
@ -895,7 +921,10 @@ editor::EXIT_STATUS game_controller::start_editor(const std::string& filename)
{
while(true){
resources::config_manager->add_define("EDITOR");
resources::config_manager->load_game_cfg(false, true);
resources::config_manager->load_game_cfg(
game_config_manager::NO_SET_PATHS,
game_config_manager::CLEAR_CACHE,
game_config_manager::NO_FORCE_RELOAD);
const binary_paths_manager bin_paths_manager(game_config());
::init_textdomains(game_config());

View file

@ -47,7 +47,8 @@ public:
game_controller(const commandline_options& cmdline_opts, const char* appname);
~game_controller();
bool init_config() { return init_config(false); }
bool init_config() {
return init_config(game_config_manager::NO_FORCE_RELOAD); }
bool play_test();
bool play_screenshot_mode();
@ -82,7 +83,7 @@ private:
game_controller(const game_controller&);
void operator=(const game_controller&);
bool init_config(const bool force);
bool init_config(game_config_manager::FORCE_RELOAD_CONFIG force_reload);
void mark_completed_campaigns(std::vector<config>& campaigns);