-
-
Notifications
You must be signed in to change notification settings - Fork 994
/
handlers.cpp
569 lines (479 loc) · 15.7 KB
/
handlers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* The structure that tracks WML event handlers.
* (Typically, handlers are defined by [event] tags.)
*/
#include "../global.hpp"
#include "handlers.hpp"
#include "menu_item.hpp"
#include "pump.hpp"
#include "../formula_string_utils.hpp"
#include "../game_data.hpp"
#include "../hotkey/hotkey_command.hpp"
#include "../log.hpp"
#include "../reports.hpp"
#include "../resources.hpp"
#include "../scripting/game_lua_kernel.hpp"
#include "../serialization/string_utils.hpp"
#include "../soundsource.hpp"
#include "../util.hpp"
#include <boost/foreach.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
static lg::log_domain log_engine("engine");
#define DBG_NG LOG_STREAM(debug, log_engine)
#define LOG_NG LOG_STREAM(info, log_engine)
#define WRN_NG LOG_STREAM(warn, log_engine)
static lg::log_domain log_event_handler("event_handler");
#define DBG_EH LOG_STREAM(debug, log_event_handler)
// This file is in the game_events namespace.
namespace game_events {
namespace { // Types
class t_event_handlers {
typedef boost::unordered_map<std::string, handler_list> map_t;
typedef boost::unordered_map<std::string, boost::weak_ptr<event_handler> > id_map_t;
public:
typedef handler_vec::iterator iterator;
typedef handler_vec::const_iterator const_iterator;
private:
handler_vec active_; /// Active event handlers. Will not have elements removed unless the t_event_handlers is clear()ed.
map_t by_name_; /// Active event handlers with fixed event names, organized by event name.
handler_list dynamic_; /// Active event handlers with variables in their event names.
id_map_t id_map_; /// Allows quick locating of handlers by id.
void log_handlers();
/// Utility to standardize the event names used in by_name_.
static std::string standardize_name(const std::string & name);
public:
typedef handler_vec::size_type size_type;
t_event_handlers()
: active_()
, by_name_()
, dynamic_()
, id_map_()
{}
/// Read-only access to the handlers with varying event names.
const handler_list & get() const { return dynamic_; }
/// Read-only access to the handlers with fixed event names, by event name.
const handler_list & get(const std::string & name) const;
/// Adds an event handler.
void add_event_handler(const config & cfg, bool is_menu_item=false);
/// Removes an event handler, identified by its ID.
void remove_event_handler(std::string const & id);
void clear();
iterator begin() { return active_.begin(); }
const_iterator begin() const { return active_.begin(); }
iterator end() { return active_.end(); }
const_iterator end() const { return active_.end(); }
/// The number of active event handlers.
size_type size() const { return active_.size(); }
/// Access to active event handlers by index.
handler_ptr & operator[](size_type index) { return active_[index]; }
};//t_event_handlers
void t_event_handlers::log_handlers()
{
if(lg::debug.dont_log("event_handler")) return;
std::stringstream ss;
BOOST_FOREACH( const handler_ptr & h, active_ ) {
if ( !h )
continue;
const config& cfg = h->get_config();
ss << "name=" << cfg["name"] << ", with id=" << cfg["id"] << "; ";
}
DBG_EH << "active handlers are now " << ss.str() << "\n";
}
/**
* Utility to standardize the event names used in by_name_.
* This means stripping leading and trailing spaces, and converting internal
* spaces to underscores.
*/
std::string t_event_handlers::standardize_name(const std::string & name)
{
std::string retval;
size_t name_index = 0;
size_t name_size = name.size();
// Trim trailing spaces off the name.
while ( name_size > 0 && name[name_size-1] == ' ' )
--name_size ;
// Trim leading spaces off the name.
while ( name_index < name_size && name[name_index] == ' ' )
++name_index;
// Copy the rest, converting any remaining spaces to underscores.
retval.reserve(name_size - name_index);
while ( name_index < name_size ) {
char c = name[name_index++];
retval.push_back(c == ' ' ? '_' : c);
}
return retval;
}
/**
* Read-only access to the handlers with fixed event names, by event name.
*/
const handler_list & t_event_handlers::get(const std::string & name) const
{
// Empty list for the "not found" case.
static const handler_list empty_list;
// Look for the name in the name map.
map_t::const_iterator find_it = by_name_.find(standardize_name(name));
return find_it == by_name_.end() ? empty_list : find_it->second;
}
/**
* Adds an event handler.
* An event with a nonempty ID will not be added if an event with that
* ID already exists.
*/
void t_event_handlers::add_event_handler(const config & cfg, bool is_menu_item)
{
const std::string name = cfg["name"];
std::string id = cfg["id"];
if(!id.empty()) {
// Ignore this handler if there is already one with this ID.
id_map_t::iterator find_it = id_map_.find(id);
if ( find_it != id_map_.end() && !find_it->second.expired() ) {
DBG_EH << "ignoring event handler for name='" << name
<< "' with id '" << id << "'\n";
return;
}
}
// Create a new handler.
DBG_EH << "inserting event handler for name=" << name <<
" with id=" << id << "\n";
handler_ptr new_handler(new event_handler(cfg, is_menu_item, active_.size()));
active_.push_back(new_handler);
// File by name.
if ( utils::might_contain_variables(name) )
dynamic_.push_back(new_handler);
else {
std::vector<std::string> name_list = utils::split(name);
BOOST_FOREACH( const std::string & single_name, name_list )
by_name_[standardize_name(single_name)].push_back(new_handler);
}
// File by ID.
if ( !id.empty() )
id_map_[id] = new_handler;
log_handlers();
}
/**
* Removes an event handler, identified by its ID.
* Events with empty IDs cannot be removed.
*/
void t_event_handlers::remove_event_handler(std::string const & id)
{
if ( id.empty() )
return;
DBG_EH << "removing event handler with id " << id << "\n";
// Find the existing handler with this ID.
id_map_t::iterator find_it = id_map_.find(id);
if ( find_it != id_map_.end() ) {
handler_ptr handler = find_it->second.lock();
// Remove handler.
if ( handler )
handler->disable();
id_map_.erase(find_it); // Do this even if the lock failed.
// The index by name will self-adjust later. No need to adjust it now.
}
log_handlers();
}
void t_event_handlers::clear()
{
active_.clear();
by_name_.clear();
dynamic_.clear();
id_map_.clear();
}
}// end anonymous namespace (types)
namespace { // Variables
t_event_handlers event_handlers;
/** Map of the default action handlers known of the engine. */
std::set<std::string> unit_wml_ids;
std::set<std::string> used_items;
}// end anonymous namespace (variables)
/** Create an event handler. */
void add_event_handler(const config & handler, bool is_menu_item)
{
event_handlers.add_event_handler(handler, is_menu_item);
}
/**
* Checks if an item has been used.
* (An empty id will never be considered used.)
*/
bool item_used(const std::string & id)
{
return !id.empty() && used_items.count(id) > 0;
}
/** Records if an item has been used. */
void item_used(const std::string & id, bool used)
{
// Empty IDs are not tracked.
if ( id.empty() )
return;
if ( used )
used_items.insert(id);
else
used_items.erase(id);
}
/** Removes an event handler. */
void remove_event_handler(const std::string & id)
{
event_handlers.remove_event_handler(id);
}
/* ** manager ** */
manager::manager(const config& cfg)
{
BOOST_FOREACH(const config &ev, cfg.child_range("event")) {
add_event_handler(ev);
}
BOOST_FOREACH(const std::string &id, utils::split(cfg["unit_wml_ids"])) {
unit_wml_ids.insert(id);
}
// Guard against a memory leak (now) / memory corruption (when this is deleted).
// This is why creating multiple manager objects is prohibited.
assert(resources::lua_kernel != NULL);
wml_action::map::const_iterator action_end = wml_action::end();
wml_action::map::const_iterator action_cur = wml_action::begin();
for ( ; action_cur != action_end; ++action_cur ) {
resources::lua_kernel->set_wml_action(action_cur->first, action_cur->second);
}
const std::string used = cfg["used_items"];
if(!used.empty()) {
const std::vector<std::string>& v = utils::split(used);
for(std::vector<std::string>::const_iterator i = v.begin(); i != v.end(); ++i) {
item_used(*i, true);
}
}
// Create the event handlers for menu items.
resources::gamedata->get_wml_menu_items().init_handlers();
}
manager::~manager() {
clear_events();
event_handlers.clear();
hotkey::delete_all_wml_hotkeys();
unit_wml_ids.clear();
used_items.clear();
}
/* ** manager::iteration ** */
/**
* Event-specific constructor.
* This iteration will go through all event handlers matching the given name
* (including those defined via menu items).
* An empty @a event_name will automatically match nothing.
*/
manager::iteration::iteration(const std::string & event_name) :
main_list_(event_handlers.get(event_name)),
var_list_(event_handlers.get()),
event_name_(event_name),
end_(event_handlers.size()),
current_is_known_(false),
main_is_current_(false),
main_it_(main_list_.begin()),
var_it_(event_name.empty() ? var_list_.end() : var_list_.begin())
{
}
/**
* Increment
* Incrementing guarantees that the next dereference will differ from the
* previous derference (unless the iteration is exhausted). However, multiple
* increments between dereferences are allowed to have the same effect as a
* single increment.
*/
manager::iteration & manager::iteration::operator++()
{
if ( !current_is_known_ )
// Either *this has never been dereferenced, or we already incremented
// since the last dereference. We are allowed to ignore this increment.
return *this;
// Guarantee a different element next dereference.
if ( main_is_current_ )
++main_it_;
else
++var_it_; // (We'll check for a name match when we dereference.)
// We no longer know which list is current.
current_is_known_ = false;
// Done.
return *this;
}
/**
* Dereference
* Will return a null pointer when the end of the iteration is reached.
*/
handler_ptr manager::iteration::operator*()
{
// Get the candidate for the current element from the main list.
handler_ptr main_ptr = *main_it_;
handler_vec::size_type main_index = ptr_index(main_ptr);
// Get the candidate for the current element from the var list.
handler_ptr var_ptr = *var_it_;
// (Loop while var_ptr would be chosen over main_ptr, but the name does not match.)
while ( var_ptr && var_ptr->index() < main_index &&
!var_ptr->matches_name(event_name_) )
var_ptr = *++var_it_;
handler_vec::size_type var_index = ptr_index(var_ptr);
// Which list? (Index ties go to the main list.)
current_is_known_ = main_index < end_ || var_index < end_;
main_is_current_ = main_index <= var_index;
if ( !current_is_known_ )
return handler_ptr(); // End of list; return a null pointer.
else
return main_is_current_ ? main_ptr : var_ptr;
}
/* ** handler_list::iterator ** */
/**
* Dereference.
* If the current element has become invalid, we will increment first.
*/
handler_ptr handler_list::iterator::operator*()
{
// Check for an available handler.
while ( iter_.derefable() ) {
// Handler still accessible?
if ( handler_ptr lock = iter_->lock() )
return lock;
else
// Remove the now-defunct entry.
iter_ = list_t::erase(iter_);
}
// End of the list.
return handler_ptr();
}
/* ** event_handler ** */
event_handler::event_handler(const config &cfg, bool imi, handler_vec::size_type index) :
first_time_only_(cfg["first_time_only"].to_bool(true)),
is_menu_item_(imi), index_(index), cfg_(cfg)
{}
/**
* Disables *this, removing it from the game.
* (Technically, the handler is only removed once no one is hanging on to a
* handler_ptr to *this. So be careful how long they persist.)
*
* WARNING: *this may be destroyed at the end of this call, unless
* the caller maintains a handler_ptr to this.
*/
void event_handler::disable()
{
// Handlers must have an index after they're created.
assert ( index_ < event_handlers.size() );
// Disable this handler.
event_handlers[index_].reset();
}
/**
* Handles the queued event, according to our WML instructions.
* WARNING: *this may be destroyed at the end of this call, unless
* the caller maintains a handler_ptr to this.
*
* @param[in] event_info Information about the event that needs handling.
* @param[in,out] handler_p The caller's smart pointer to *this. It may be
* reset() during processing.
*/
void event_handler::handle_event(const queued_event& event_info, handler_ptr& handler_p)
{
// We will need our config after possibly self-destructing. Make a copy now.
vconfig vcfg(cfg_, true);
if (is_menu_item_) {
DBG_NG << cfg_["name"] << " will now invoke the following command(s):\n" << cfg_;
}
if (first_time_only_)
{
// Disable this handler.
disable();
// Also remove our caller's hold on us.
handler_p.reset();
}
// *WARNING*: At this point, dereferencing this could be a memory violation!
handle_event_commands(event_info, vcfg);
}
bool event_handler::matches_name(const std::string &name) const
{
const std::string my_names =
utils::interpolate_variables_into_string(cfg_["name"], *(resources::gamedata));
std::string::const_iterator itor,
it_begin = my_names.begin(),
it_end = my_names.end(),
match_it = name.begin(),
match_begin = name.begin(),
match_end = name.end();
int skip_count = 0;
for(itor = it_begin; itor != it_end; ++itor) {
bool do_eat = false,
do_skip = false;
switch(*itor) {
case ',':
if(itor - it_begin - skip_count == match_it - match_begin && match_it == match_end) {
return true;
}
it_begin = itor + 1;
match_it = match_begin;
skip_count = 0;
continue;
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
do_skip = (match_it == match_begin || match_it == match_end);
break;
case ' ':
do_skip = (match_it == match_begin || match_it == match_end);
// fall through to case '_'
case '_':
do_eat = (match_it != match_end && (*match_it == ' ' || *match_it == '_'));
break;
default:
do_eat = (match_it != match_end && *match_it == *itor);
break;
}
if(do_eat) {
++match_it;
} else if(do_skip) {
++skip_count;
} else {
itor = std::find(itor, it_end, ',');
if(itor == it_end) {
return false;
}
it_begin = itor + 1;
match_it = match_begin;
skip_count = 0;
}
}
if(itor - it_begin - skip_count == match_it - match_begin && match_it == match_end) {
return true;
}
return false;
}
void add_events(const config::const_child_itors &cfgs, const std::string& type)
{
if(!type.empty()) {
if(std::find(unit_wml_ids.begin(),unit_wml_ids.end(),type) != unit_wml_ids.end()) return;
unit_wml_ids.insert(type);
}
BOOST_FOREACH(const config &new_ev, cfgs) {
if(type.empty() && new_ev["id"].empty())
{
WRN_NG << "attempt to add an [event] with empty id=, ignoring " << std::endl;
continue;
}
add_event_handler(new_ev);
}
}
void write_events(config& cfg)
{
BOOST_FOREACH(const handler_ptr &eh, event_handlers) {
if ( !eh || eh->is_menu_item() ) {
continue;
}
cfg.add_child("event", eh->get_config());
}
cfg["used_items"] = utils::join(used_items);
cfg["unit_wml_ids"] = utils::join(unit_wml_ids);
}
} // end namespace game_events