Skip to content

Commit

Permalink
initialize wml menu item handlers when they are set, not in ctor
Browse files Browse the repository at this point in the history
this commit causes wml menu items to have their events registered
not during construction but only when "init_handler" is called.
it refactors the wmi_container object to do this when it
constructs new items via the [set_menu_item] path.

this is necessary because the items might be constructed before
the event queue even exists, and we want them to be able to
persist and be reactivated when a new campaign scenario starts.

more testing is necessary to determine if all code paths, including
carryover and reloading, are still working after this commit, and
the earlier commit 6fc1ac1

See also discussion of bug #23115.
  • Loading branch information
cbeck88 committed Jan 5, 2015
1 parent 18b5059 commit eaa078a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/game_events/manager.cpp
Expand Up @@ -245,4 +245,9 @@ game_events::t_pump & manager::pump()
return *pump_;
}

const boost::shared_ptr<manager * const> & manager::get_shared()
{
return me_;
}

} //end namespace game_events
2 changes: 2 additions & 0 deletions src/game_events/manager.hpp
Expand Up @@ -136,6 +136,8 @@ namespace game_events {
const std::string& type = std::string());
void write_events(config& cfg);

const boost::shared_ptr<manager * const> & get_shared();

game_events::t_pump & pump();
};
}
Expand Down
16 changes: 12 additions & 4 deletions src/game_events/menu_item.cpp
Expand Up @@ -244,6 +244,7 @@ void wml_menu_item::init_handler(const boost::shared_ptr<manager * const> & man)
if ( !command_.empty() ) {
assert(man);
my_manager_ = man;
LOG_NG << "Setting command for " << event_name_ << " to:\n" << command_;
(**man).add_event_handler(command_, true);
}

Expand All @@ -253,6 +254,11 @@ void wml_menu_item::init_handler(const boost::shared_ptr<manager * const> & man)
}
}

void wml_menu_item::init_handler(manager & man) const
{
init_handler(man.get_shared());
}

/**
* Writes *this to the provided config.
* This is the reverse of the constructor from a config and corresponds to
Expand Down Expand Up @@ -355,7 +361,7 @@ void wml_menu_item::update(const vconfig & vcfg)
*/
void wml_menu_item::update_command(const config & new_command)
{
if (boost::shared_ptr<manager * const> man = my_manager_.lock()) {
/* if (boost::shared_ptr<manager * const> man = my_manager_.lock()) {
// If there is an old command, remove it from the event handlers.
if ( !command_.empty() ) {
manager::iteration iter(event_name_, **man);
Expand All @@ -367,7 +373,7 @@ void wml_menu_item::update_command(const config & new_command)
++iter;
}
}

*/
// Update our stored command.
if ( new_command.empty() )
command_.clear();
Expand All @@ -382,13 +388,15 @@ void wml_menu_item::update_command(const config & new_command)
command_["name"] = event_name_;
command_["first_time_only"] = false;

/*
// Register the event.
LOG_NG << "Setting command for " << event_name_ << " to:\n" << command_;
(**man).add_event_handler(command_, true);
}
} else {
*/ }
/* } else {
ERR_NG << "Tried to set a command for a menu item, but the manager could not be found\n";
}
*/
}

} // end namespace game_events
Expand Down
1 change: 1 addition & 0 deletions src/game_events/menu_item.hpp
Expand Up @@ -62,6 +62,7 @@ class wml_menu_item
void finish_handler() const;
/// Initializes the implicit event handler for an inlined [command].
void init_handler(const boost::shared_ptr<manager * const> &) const;
void init_handler(manager &) const;
/// The text to put in a menu for this item.
/// This will be either translated text or a hotkey identifier.
std::string menu_text() const
Expand Down
15 changes: 9 additions & 6 deletions src/game_events/wmi_container.cpp
Expand Up @@ -155,9 +155,7 @@ void wmi_container::init_handlers(const boost::shared_ptr<manager * const> & man
}

// Diagnostic:
if ( wmi_count > 0 ) {
LOG_NG << wmi_count << " WML menu items found, loaded." << std::endl;
}
LOG_NG << wmi_count << " WML menu items found, loaded." << std::endl;
}

void wmi_container::to_config(config& cfg) const
Expand All @@ -171,19 +169,24 @@ void wmi_container::to_config(config& cfg) const
/**
* Updates or creates (as appropriate) the menu item with the given @a id.
*/
void wmi_container::set_item(const std::string& id, const vconfig& menu_item)
void wmi_container::set_item(const std::string& id, const vconfig& menu_item, manager * man)
{
// Try to insert a dummy value. This combines looking for an existing
// entry with insertion.
map_t::iterator add_it = wml_menu_items_.insert(map_t::value_type(id, item_ptr())).first;

if ( add_it->second )
if ( add_it->second ) {
// Create a new menu item based on the old. This leaves the old item
// alone in case someone else is holding on to (and processing) it.
add_it->second.reset(new wml_menu_item(id, menu_item, *add_it->second));
else
} else {
// This is a new menu item.
add_it->second.reset(new wml_menu_item(id, menu_item));
}

if (man) {
add_it->second->init_handler(*man);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/game_events/wmi_container.hpp
Expand Up @@ -88,7 +88,7 @@ class wmi_container{
void init_handlers(const boost::shared_ptr<manager * const> &) const;
void to_config(config& cfg) const;
/// Updates or creates (as appropriate) the menu item with the given @a id.
void set_item(const std::string& id, const vconfig& menu_item);
void set_item(const std::string& id, const vconfig& menu_item, manager *);
/// Sets the current menu items to the "menu_item" children of @a cfg.
void set_menu_items(const config& cfg);

Expand Down
3 changes: 2 additions & 1 deletion src/scripting/game_lua_kernel.cpp
Expand Up @@ -757,7 +757,8 @@ int game_lua_kernel::intf_set_variable(lua_State *L)

int game_lua_kernel::intf_set_menu_item(lua_State *L)
{
gamedata().get_wml_menu_items().set_item(luaL_checkstring(L, 1), luaW_checkvconfig(L,2));
gamedata().get_wml_menu_items().set_item(luaL_checkstring(L, 1), luaW_checkvconfig(L,2), game_state_.events_manager_.get());

return 0;
}

Expand Down

0 comments on commit eaa078a

Please sign in to comment.