30 #define DBG_NG LOG_STREAM(debug, log_engine)
31 #define LOG_NG LOG_STREAM(info, log_engine)
32 #define WRN_NG LOG_STREAM(warn, log_engine)
35 #define LOG_EH LOG_STREAM(info, log_event_handler)
36 #define DBG_EH LOG_STREAM(debug, log_event_handler)
41 class event_handler_list_lock
44 event_handler_list_lock()
49 ~event_handler_list_lock()
56 return num_locks_ == 0u;
59 static unsigned int num_locks_;
62 unsigned int event_handler_list_lock::num_locks_ = 0u;
70 auto new_handler =
event_handlers_->add_event_handler(handler[
"name"], handler[
"id"], !handler[
"first_time_only"].to_bool(
true), is_menu_item);
71 if(new_handler.valid()) {
72 new_handler->read_filters(handler);
77 if(attr ==
"id" || attr ==
"name" || attr ==
"first_time_only" || attr.compare(0, 6,
"filter") == 0) {
83 if(child.key.compare(0, 6,
"filter") != 0) {
87 new_handler->set_arguments(args);
88 new_handler->register_wml_event(lk);
89 DBG_EH <<
"Registered WML event "
90 << (new_handler->names_raw().empty() ?
"" :
"'" + new_handler->names_raw() +
"'")
91 << (new_handler->id().empty() ?
"" :
"{id=" + new_handler->id() +
"}")
92 << (new_handler->repeatable() ?
" (repeating" :
" (first time only")
93 << (is_menu_item ?
"; menu item)" :
")")
94 <<
" with the following actions:\n"
97 LOG_EH <<
"Content of failed event:\n" << handler.
debug();
103 return event_handlers_->add_event_handler(name,
id, repeat, is_menu_item);
134 for(
const std::string&
id :
utils::split(scenario_cfg[
"unit_wml_ids"])) {
158 for(
const config& new_ev : cfgs) {
159 if(
type.empty() && new_ev[
"id"].empty()) {
160 WRN_NG <<
"attempt to add an [event] with empty id=, ignoring ";
171 if(!eh || eh->is_menu_item()) {
186 assert(!eh->disabled());
190 eh->write_config(event_cfg, include_nonserializable);
191 if(!event_cfg.
empty()) {
192 cfg.
add_child(
"event", std::move(event_cfg));
208 const unsigned saved_end = active_handlers.size();
213 event_handler_list_lock lock;
215 for (
unsigned i = 0;
i < saved_end; ++
i) {
219 handler = active_handlers.at(
i);
221 catch (
const std::out_of_range&) {
226 if (!handler || handler->disabled()) {
231 for(
const std::string& name : handler->names(gd)) {
232 if(standardized_event_id == name) {
233 func(*
this, handler);
241 if(event_handler_list_lock::none()) {
249 return !event_handler_list_lock::none();
A config object defines a single node in a WML file, with access to child nodes.
const_attr_itors attribute_range() const
const_all_children_itors all_children_range() const
In-order iteration over all children.
child_itors child_range(config_key_type key)
std::string debug() const
boost::iterator_range< const_child_iterator > const_child_itors
config & add_child(config_key_type key)
static std::string standardize_name(const std::string &name)
Utility to standardize the event names used in by_name_.
std::function< void(game_events::manager &, handler_ptr &)> event_func_t
void execute_on_events(const std::string &event_id, event_func_t func)
const std::unique_ptr< game_events::wml_event_pump > pump_
game_events::wml_event_pump & pump()
void write_events(config &cfg, bool include_nonserializable=false) const
void remove_event_handler(const std::string &id)
Removes an event handler.
void read_scenario(const config &scenario_cfg, game_lua_kernel &lk)
void add_event_handler_from_wml(const config &handler, game_lua_kernel &lk, bool is_menu_item=false)
Create an event handler from an [event] tag.
void add_events(const config::const_child_itors &cfgs, game_lua_kernel &lk, const std::string &type=std::string())
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
bool is_event_running() const
const std::unique_ptr< event_handlers > event_handlers_
game_events::wmi_manager wml_menu_items_
pending_event_handler add_event_handler_from_lua(const std::string &name, const std::string &id, bool repeat=false, bool is_menu_item=false)
Create an empty event handler.
std::set< std::string > unit_wml_ids_
Represents a handler that is about to be added to the events manager but is still waiting for some da...
void init_handlers(game_lua_kernel &lk) const
Initializes the implicit event handlers for inlined [command]s.
void to_config(config &cfg) const
void set_menu_items(const config &cfg)
Sets the current menu items to the "menu_item" children of cfg.
static lg::log_domain log_engine("engine")
static lg::log_domain log_event_handler("event_handler")
Define the handlers for the game's events mechanism.
Standard logging facilities (interface).
std::shared_ptr< event_handler > handler_ptr
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
Define the game's event mechanism.