28 #define DBG_NG LOG_STREAM(debug, log_engine)
29 #define LOG_NG LOG_STREAM(info, log_engine)
30 #define WRN_NG LOG_STREAM(warn, log_engine)
31 #define ERR_NG LOG_STREAM(err, log_engine)
34 #define LOG_EH LOG_STREAM(info, log_event_handler)
35 #define DBG_EH LOG_STREAM(debug, log_event_handler)
40 class event_handler_list_lock
43 event_handler_list_lock()
48 ~event_handler_list_lock()
55 return num_locks_ == 0u;
58 static unsigned int num_locks_;
61 unsigned int event_handler_list_lock::num_locks_ = 0u;
72 !handler[
"first_time_only"].to_bool(
true),
73 handler[
"priority"].to_double(0.),
76 if(new_handler.valid()) {
77 new_handler->read_filters(handler);
82 if(attr ==
"id" || attr ==
"name" || attr ==
"first_time_only" || attr ==
"priority" || attr.compare(0, 6,
"filter") == 0) {
88 if(key.compare(0, 6,
"filter") != 0) {
92 new_handler->set_arguments(args);
93 new_handler->register_wml_event(lk);
94 DBG_EH <<
"Registered WML event "
95 << (new_handler->names_raw().empty() ?
"" :
"'" + new_handler->names_raw() +
"'")
96 << (new_handler->id().empty() ?
"" :
"{id=" + new_handler->id() +
"}")
97 << (new_handler->repeatable() ?
" (repeating" :
" (first time only")
98 <<
"; priority " + std::to_string(new_handler->priority())
99 << (is_menu_item ?
"; menu item)" :
")")
100 <<
" with the following actions:\n"
103 LOG_EH <<
"Content of failed event:\n" << handler.
debug();
109 return event_handlers_->add_event_handler(name,
id, repeat, priority, is_menu_item);
140 for(
const std::string&
id :
utils::split(scenario_cfg[
"unit_wml_ids"])) {
164 for(
const config& new_ev : cfgs) {
165 if(
type.empty() && new_ev[
"id"].empty()) {
166 WRN_NG <<
"attempt to add an [event] with empty id= from [unit], ignoring ";
177 if(!eh || eh->is_menu_item()) {
192 ERR_NG <<
"found disabled event with no event running, seems like event cleanup has failed";
198 eh->write_config(event_cfg, include_nonserializable);
199 if(!event_cfg.
empty()) {
200 cfg.
add_child(
"event", std::move(event_cfg));
217 event_handler_list_lock lock;
219 for (
unsigned i = 0;
i < active_handlers.size(); ++
i) {
223 handler = active_handlers.at(
i);
225 catch (
const std::out_of_range&) {
230 if (!handler || handler->disabled()) {
235 for(
const std::string& name : handler->names(gd)) {
236 if(standardized_event_id == name) {
237 func(*
this, handler);
245 if(event_handler_list_lock::none()) {
253 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
auto all_children_view() 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, const 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, double priority=0., 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)
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
Define the game's event mechanism.