Skip to content

Commit

Permalink
less config reloads
Browse files Browse the repository at this point in the history
This removes a config reload when opening multiplayer or singleplayer
campaign selection screen after playign a campaign.

To do this we must make sure the default era for sp and for mp have the
same id becasue otherwise we might get an id not found error.
  • Loading branch information
gfgtdf committed Jun 18, 2015
1 parent 024c564 commit c2e1d71
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
3 changes: 2 additions & 1 deletion data/_main.cfg
Expand Up @@ -32,7 +32,8 @@
#endif

#else
{era_blank.cfg}
# using different default eras in sp and mp forcesus to a config reload which we dont want.
{multiplayer/eras.cfg}
#endif

{campaigns/}
Expand Down
47 changes: 42 additions & 5 deletions src/game_config_manager.cpp
Expand Up @@ -101,7 +101,18 @@ bool game_config_manager::init_game_config(FORCE_RELOAD_CONFIG force_reload)

return true;
}

/// returns true if every define in special is also defined in general
bool map_includes(const preproc_map& general, const preproc_map& special)
{
BOOST_FOREACH(const preproc_map::value_type& pair, special)
{
preproc_map::const_iterator it = general.find(pair.first);
if (it == general.end() || it->second != pair.second) {
return false;
}
}
return true;
}
void game_config_manager::load_game_config(FORCE_RELOAD_CONFIG force_reload,
game_classification const* classification)
{
Expand All @@ -112,10 +123,13 @@ void game_config_manager::load_game_config(FORCE_RELOAD_CONFIG force_reload,
game_config::debug || game_config::mp_debug);

// 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;
if(!game_config_.empty()) {
if((force_reload == NO_FORCE_RELOAD) && old_defines_map_ == cache_.get_preproc_map()) {
return;
}
if((force_reload == NO_INCLUDE_RELOAD) && map_includes(old_defines_map_, cache_.get_preproc_map())) {
return;
}
}

loadscreen::global_loadscreen_manager loadscreen_manager(disp_.video());
Expand Down Expand Up @@ -540,4 +554,27 @@ void game_config_manager::load_game_config_for_game(
throw;
}
}
void game_config_manager::load_game_config_for_create(bool is_mp)
{
game_config::scoped_preproc_define multiplayer("MULTIPLAYER", is_mp);

typedef boost::shared_ptr<game_config::scoped_preproc_define> define;
try{
load_game_config(NO_INCLUDE_RELOAD);
}
catch(game::error&) {
cache_.clear_defines();

std::deque<define> previous_defines;
BOOST_FOREACH(const preproc_map::value_type& preproc, old_defines_map_) {
define new_define
(new game_config::scoped_preproc_define(preproc.first));
previous_defines.push_back(new_define);
}

load_game_config(NO_FORCE_RELOAD);

throw;
}
}

12 changes: 10 additions & 2 deletions src/game_config_manager.hpp
Expand Up @@ -29,8 +29,15 @@ class game_config_manager
game_config_manager(const commandline_options& cmdline_opts,
game_display& disp, const bool jump_to_editor);
~game_config_manager();

enum FORCE_RELOAD_CONFIG { FORCE_RELOAD, NO_FORCE_RELOAD };
enum FORCE_RELOAD_CONFIG
{
/// Always reload config
FORCE_RELOAD,
/// Dont reload if the previous defines equal the new defines
NO_FORCE_RELOAD,
/// Dont reload if the previous defines include the new defines
NO_INCLUDE_RELOAD,
};

const config& game_config() const { return game_config_; }
const preproc_map& old_defines_map() const { return old_defines_map_; }
Expand All @@ -41,6 +48,7 @@ class game_config_manager

void load_game_config_for_editor();
void load_game_config_for_game(const game_classification& classification);
void load_game_config_for_create(bool is_mp);

static game_config_manager * get();

Expand Down
11 changes: 4 additions & 7 deletions src/game_initialization/create_engine.cpp
Expand Up @@ -420,13 +420,7 @@ create_engine::create_engine(game_display& disp, saved_game& state) :
state_.classification().campaign_type = type;
state_.mp_settings().show_configure = configure;
state_.mp_settings().show_connect = connect;

if (!(type == game_classification::CAMPAIGN_TYPE::SCENARIO &&
game_config_manager::get()->old_defines_map().count("TITLE_SCREEN") != 0))
{
game_config_manager::get()->
load_game_config_for_game(state_.classification());
}
game_config_manager::get()->load_game_config_for_create(type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER);

//TODO the editor dir is already configurable, is the preferences value
filesystem::get_files_in_dir(filesystem::get_user_data_dir() + "/editor/maps", &user_map_names_,
Expand Down Expand Up @@ -1077,6 +1071,9 @@ void create_engine::init_all_levels()
if (!data["allow_new_game"].to_bool(true))
continue;

if (!data["campaign_id"].empty())
continue;

if (data.has_attribute("map_generation") || data.has_attribute("scenario_generation")) {
random_map_ptr new_random_map(new random_map(data));
random_maps_.push_back(new_random_map);
Expand Down
6 changes: 3 additions & 3 deletions src/game_launcher.cpp
Expand Up @@ -942,9 +942,9 @@ bool game_launcher::play_multiplayer()


}

game_config_manager::get()->
load_game_config_for_game(state_.classification());
//create_engine already calls game_config_manager::get()->load_config
//game_config_manager::get()->
// load_game_config_for_game(state_.classification());

events::discard_input(); // prevent the "keylogger" effect
cursor::set(cursor::NORMAL);
Expand Down

0 comments on commit c2e1d71

Please sign in to comment.