The Battle for Wesnoth  1.19.0-dev
manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "game_events/manager.hpp"
17 
18 #include "game_events/handlers.hpp"
20 #include "game_events/pump.hpp"
21 
22 #include "game_data.hpp"
23 #include "log.hpp"
24 #include "resources.hpp"
26 
27 static lg::log_domain log_engine("engine");
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 
32 static lg::log_domain log_event_handler("event_handler");
33 #define LOG_EH LOG_STREAM(info, log_event_handler)
34 #define DBG_EH LOG_STREAM(debug, log_event_handler)
35 
36 namespace
37 {
38  // Event handlers can't be destroyed as long as at least one of these locks exist.
39  class event_handler_list_lock
40  {
41  public:
42  event_handler_list_lock()
43  {
44  ++num_locks_;
45  }
46 
47  ~event_handler_list_lock()
48  {
49  --num_locks_;
50  }
51 
52  static bool none()
53  {
54  return num_locks_ == 0u;
55  }
56  private:
57  static unsigned int num_locks_;
58  };
59 
60  unsigned int event_handler_list_lock::num_locks_ = 0u;
61 }
62 
63 namespace game_events
64 {
65 /** Create an event handler. */
66 void manager::add_event_handler_from_wml(const config& handler, game_lua_kernel& lk, bool is_menu_item)
67 {
68  auto new_handler = event_handlers_->add_event_handler(
69  handler["name"],
70  handler["id"],
71  !handler["first_time_only"].to_bool(true),
72  handler["priority"].to_double(0.),
73  is_menu_item
74  );
75  if(new_handler.valid()) {
76  new_handler->read_filters(handler);
77 
78  // Strip out anything that's used by the event system itself.
79  config args;
80  for(const auto& [attr, val] : handler.attribute_range()) {
81  if(attr == "id" || attr == "name" || attr == "first_time_only" || attr == "priority" || attr.compare(0, 6, "filter") == 0) {
82  continue;
83  }
84  args[attr] = val;
85  }
86  for(auto child : handler.all_children_range()) {
87  if(child.key.compare(0, 6, "filter") != 0) {
88  args.add_child(child.key, child.cfg);
89  }
90  }
91  new_handler->set_arguments(args);
92  new_handler->register_wml_event(lk);
93  DBG_EH << "Registered WML event "
94  << (new_handler->names_raw().empty() ? "" : "'" + new_handler->names_raw() + "'")
95  << (new_handler->id().empty() ? "" : "{id=" + new_handler->id() + "}")
96  << (new_handler->repeatable() ? " (repeating" : " (first time only")
97  << "; priority " + std::to_string(new_handler->priority())
98  << (is_menu_item ? "; menu item)" : ")")
99  << " with the following actions:\n"
100  << args.debug();
101  } else {
102  LOG_EH << "Content of failed event:\n" << handler.debug();
103  }
104 }
105 
106 pending_event_handler manager::add_event_handler_from_lua(const std::string& name, const std::string& id, bool repeat, double priority, bool is_menu_item)
107 {
108  return event_handlers_->add_event_handler(name, id, repeat, priority, is_menu_item);
109 }
110 
111 /** Removes an event handler. */
112 void manager::remove_event_handler(const std::string& id)
113 {
114  event_handlers_->remove_event_handler(id);
115 }
116 
117 /** Gets an event handler by id */
118 const handler_ptr manager::get_event_handler_by_id(const std::string& id)
119 {
120  return event_handlers_->get_event_handler_by_id(id);
121 }
122 
123 /* ** manager ** */
124 
126  : event_handlers_(new event_handlers())
127  , unit_wml_ids_()
128  , pump_(new game_events::wml_event_pump(*this))
129  , wml_menu_items_()
130 {
131 }
132 
133 void manager::read_scenario(const config& scenario_cfg, game_lua_kernel& lk)
134 {
135  for(const config& ev : scenario_cfg.child_range("event")) {
137  }
138 
139  for(const std::string& id : utils::split(scenario_cfg["unit_wml_ids"])) {
140  unit_wml_ids_.insert(id);
141  }
142 
143  wml_menu_items_.set_menu_items(scenario_cfg);
144 
145  // Create the event handlers for menu items.
147 }
148 
150 {
151 }
152 
153 void manager::add_events(const config::const_child_itors& cfgs, game_lua_kernel& lk, const std::string& type)
154 {
155  if(!type.empty()) {
156  if(std::find(unit_wml_ids_.begin(), unit_wml_ids_.end(), type) != unit_wml_ids_.end()) {
157  return;
158  }
159 
160  unit_wml_ids_.insert(type);
161  }
162 
163  for(const config& new_ev : cfgs) {
164  if(type.empty() && new_ev["id"].empty()) {
165  WRN_NG << "attempt to add an [event] with empty id= from [unit], ignoring ";
166  continue;
167  }
168 
169  add_event_handler_from_wml(new_ev, lk);
170  }
171 }
172 
173 void manager::write_events(config& cfg, bool include_nonserializable) const
174 {
175  for(const handler_ptr& eh : event_handlers_->get_active()) {
176  if(!eh || eh->is_menu_item()) {
177  continue;
178  }
179 
180  // Silently skip disabled events if this function is invoked mid-event, such as via
181  // [inspect] (the inspector writes the events to a local config) or if an out-of-sync
182  // error occurs in MP. If the event in question is first-time-only, it will already
183  // have been flagged as disabled by this point (such events are disabled before their
184  // actions are run). If a disabled event is encountered outside an event context,
185  // however, assert. That means something went wrong with event list cleanup.
186  // Also silently skip them when including nonserializable events, which can happen
187  // if viewing the inspector with :inspect after removing an event from the Lua console.
188  if(eh->disabled() && (is_event_running() || include_nonserializable)) {
189  continue;
190  } else {
191  assert(!eh->disabled());
192  }
193 
194  config event_cfg;
195  eh->write_config(event_cfg, include_nonserializable);
196  if(!event_cfg.empty()) {
197  cfg.add_child("event", std::move(event_cfg));
198  }
199  }
200 
201  cfg["unit_wml_ids"] = utils::join(unit_wml_ids_);
203 }
204 
205 void manager::execute_on_events(const std::string& event_id, manager::event_func_t func)
206 {
207  const std::string standardized_event_id = event_handlers::standardize_name(event_id);
208  const game_data* gd = resources::gamedata;
209  // Copy the list so that new events added during processing are not executed.
210  auto active_handlers = event_handlers_->get_active();
211 
212  {
213  // Ensure that event handlers won't be cleaned up while we're iterating them.
214  event_handler_list_lock lock;
215 
216  for (unsigned i = 0; i < active_handlers.size(); ++i) {
217  handler_ptr handler = nullptr;
218 
219  try {
220  handler = active_handlers.at(i);
221  }
222  catch (const std::out_of_range&) {
223  continue;
224  }
225 
226  // Shouldn't happen, but we're just being safe.
227  if (!handler || handler->disabled()) {
228  continue;
229  }
230 
231  // Could be more than one.
232  for(const std::string& name : handler->names(gd)) {
233  if(standardized_event_id == name) {
234  func(*this, handler);
235  break;
236  }
237  }
238  }
239  }
240 
241  // Clean up expired ptrs. This saves us effort later since it ensures every ptr is valid.
242  if(event_handler_list_lock::none()) {
243  event_handlers_->clean_up_expired_handlers(standardized_event_id);
244  }
245 }
246 
248 {
249  // If there is an event handler list lock, an event is being processed.
250  return !event_handler_list_lock::none();
251 }
252 
254 {
255  return *pump_;
256 }
257 
258 } // end namespace game_events
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
const_attr_itors attribute_range() const
Definition: config.cpp:763
const_all_children_itors all_children_range() const
In-order iteration over all children.
Definition: config.cpp:887
child_itors child_range(config_key_type key)
Definition: config.cpp:273
std::string debug() const
Definition: config.cpp:1244
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:283
bool empty() const
Definition: config.cpp:852
config & add_child(config_key_type key)
Definition: config.cpp:441
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
Definition: manager.hpp:79
void execute_on_events(const std::string &event_id, event_func_t func)
Definition: manager.cpp:205
const std::unique_ptr< game_events::wml_event_pump > pump_
Definition: manager.hpp:50
game_events::wml_event_pump & pump()
Definition: manager.cpp:253
void write_events(config &cfg, bool include_nonserializable=false) const
Definition: manager.cpp:173
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition: manager.cpp:112
void read_scenario(const config &scenario_cfg, game_lua_kernel &lk)
Definition: manager.cpp:133
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.
Definition: manager.cpp:66
void add_events(const config::const_child_itors &cfgs, game_lua_kernel &lk, const std::string &type=std::string())
Definition: manager.cpp:153
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
Definition: manager.cpp:118
bool is_event_running() const
Definition: manager.cpp:247
const std::unique_ptr< event_handlers > event_handlers_
Definition: manager.hpp:47
game_events::wmi_manager wml_menu_items_
Definition: manager.hpp:51
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.
Definition: manager.cpp:106
std::set< std::string > unit_wml_ids_
Definition: manager.hpp:48
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.
std::size_t i
Definition: function.cpp:968
#define WRN_NG
Definition: manager.cpp:30
static lg::log_domain log_engine("engine")
#define LOG_EH
Definition: manager.cpp:33
static lg::log_domain log_event_handler("event_handler")
#define DBG_EH
Definition: manager.cpp:34
Define the handlers for the game's events mechanism.
Standard logging facilities (interface).
Domain specific events.
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:25
game_data * gamedata
Definition: resources.cpp:22
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.