… | |
… | |
11 | use Event; |
11 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
13 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
16 | our %COMMAND; |
16 | our %COMMAND = (); |
17 | our @EVENT; |
17 | our @EVENT; |
18 | our @PLUGIN_EVENT; |
18 | our @PLUGIN_EVENT; |
19 | our %PROP_TYPE; |
19 | our %PROP_TYPE; |
20 | our %PROP_IDX; |
20 | our %PROP_IDX; |
21 | our $LIBDIR = maps_directory "perl"; |
21 | our $LIBDIR = maps_directory "perl"; |
… | |
… | |
90 | my %ext_pkg; |
90 | my %ext_pkg; |
91 | my @exts; |
91 | my @exts; |
92 | my @hook; |
92 | my @hook; |
93 | my %command; |
93 | my %command; |
94 | my %extcmd; |
94 | my %extcmd; |
|
|
95 | |
|
|
96 | ############################################################################# |
|
|
97 | # "new" plug-in system |
|
|
98 | |
|
|
99 | =item cf::object::attach ... # NYI |
|
|
100 | |
|
|
101 | =item cf::attach_global ... # NYI |
|
|
102 | |
|
|
103 | =item cf::attach_to_type ... # NYI |
|
|
104 | |
|
|
105 | =item cf::attach_to_objects ... # NYI |
|
|
106 | |
|
|
107 | =item cf::attach_to_players ... # NYI |
|
|
108 | |
|
|
109 | =item cf::attach_to_maps ... # NYI |
|
|
110 | |
|
|
111 | prio => $number, # higehr is earlier |
|
|
112 | on_xxx => \&cb, |
|
|
113 | package => package::, |
|
|
114 | |
|
|
115 | =cut |
|
|
116 | |
|
|
117 | our %CB_CLASS = (); # registry for class-based events |
|
|
118 | our @CB_GLOBAL = (); # registry for all global events |
|
|
119 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
120 | |
|
|
121 | sub _attach_cb($\%$$$) { |
|
|
122 | my ($registry, $undo, $event, $prio, $cb) = @_; |
|
|
123 | |
|
|
124 | use sort 'stable'; |
|
|
125 | |
|
|
126 | $cb = [$prio, $cb]; |
|
|
127 | |
|
|
128 | @{$registry->[$event]} = sort |
|
|
129 | { $a->[0] cmp $b->[0] } |
|
|
130 | @{$registry->[$event] || []}, $cb; |
|
|
131 | |
|
|
132 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
133 | } |
|
|
134 | |
|
|
135 | # attach handles attaching event callbacks |
|
|
136 | # the only thing the caller has to do is pass the correct |
|
|
137 | # registry (== where the callback attaches to). |
|
|
138 | sub _attach(\@$\@) { |
|
|
139 | my ($registry, $klass, $arg) = @_; |
|
|
140 | |
|
|
141 | my $prio = 0; |
|
|
142 | |
|
|
143 | my %undo = ( |
|
|
144 | registry => $registry, |
|
|
145 | cb => [], |
|
|
146 | ); |
|
|
147 | |
|
|
148 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
149 | |
|
|
150 | while (@$arg) { |
|
|
151 | my $type = shift @$arg; |
|
|
152 | |
|
|
153 | if ($type eq "prio") { |
|
|
154 | $prio = shift @$arg; |
|
|
155 | |
|
|
156 | } elsif ($type eq "package") { |
|
|
157 | my $pkg = shift @$arg; |
|
|
158 | |
|
|
159 | while (my ($name, $id) = each %cb_id) { |
|
|
160 | if (my $cb = $pkg->can ($name)) { |
|
|
161 | _attach_cb $registry, %undo, $id, $prio, $cb; |
|
|
162 | } |
|
|
163 | } |
|
|
164 | |
|
|
165 | } elsif (exists $cb_id{$type}) { |
|
|
166 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; |
|
|
167 | |
|
|
168 | } elsif (ref $type) { |
|
|
169 | warn "attaching objects not supported, ignoring.\n"; |
|
|
170 | |
|
|
171 | } else { |
|
|
172 | shift @$arg; |
|
|
173 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
174 | } |
|
|
175 | } |
|
|
176 | |
|
|
177 | \%undo |
|
|
178 | } |
|
|
179 | |
|
|
180 | sub cf::object::attach { |
|
|
181 | die; |
|
|
182 | } |
|
|
183 | |
|
|
184 | sub attach_global { |
|
|
185 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
186 | } |
|
|
187 | |
|
|
188 | sub attach_type { |
|
|
189 | my $type = shift; |
|
|
190 | _attach @{$CB_TYPE[$type]}, KLASS_MAP, @_ |
|
|
191 | } |
|
|
192 | |
|
|
193 | sub attach_to_objects { |
|
|
194 | _attach @{$CB_CLASS{cf::object::wrap::}}, KLASS_OBJECT, @_ |
|
|
195 | } |
|
|
196 | |
|
|
197 | sub attach_to_players { |
|
|
198 | _attach @{$CB_CLASS{cf::player::wrap::}}, KLASS_PLAYER, @_ |
|
|
199 | } |
|
|
200 | |
|
|
201 | sub attach_to_maps { |
|
|
202 | _attach @{$CB_CLASS{cf::map::wrap::}}, KLASS_MAP, @_ |
|
|
203 | } |
|
|
204 | |
|
|
205 | our $override; |
|
|
206 | |
|
|
207 | sub override() { |
|
|
208 | $override = 1 |
|
|
209 | } |
|
|
210 | |
|
|
211 | sub invoke { |
|
|
212 | my $event = shift; |
|
|
213 | |
|
|
214 | my @cb; |
|
|
215 | |
|
|
216 | if (my $ref = ref $_[0]) { |
|
|
217 | # 1. object-specific (NYI) |
|
|
218 | # 2. class-specific |
|
|
219 | push @cb, @{$CB_CLASS{$ref}[$event] || []}; |
|
|
220 | } |
|
|
221 | |
|
|
222 | # global |
|
|
223 | push @cb, @{$CB_GLOBAL[$event] || []}; |
|
|
224 | |
|
|
225 | # warn "invoke id $EVENT[$event][0], args <@_> <=> @cb\n";#d# |
|
|
226 | |
|
|
227 | local $override; |
|
|
228 | |
|
|
229 | for (@cb) { |
|
|
230 | eval { &{$_->[1]} }; |
|
|
231 | |
|
|
232 | if ($@) { |
|
|
233 | warn "$@"; |
|
|
234 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
|
|
235 | override; |
|
|
236 | } |
|
|
237 | |
|
|
238 | return 1 if $override; |
|
|
239 | } |
|
|
240 | |
|
|
241 | 0 |
|
|
242 | } |
|
|
243 | |
|
|
244 | ############################################################################# |
|
|
245 | # old plug-in events |
95 | |
246 | |
96 | sub inject_event { |
247 | sub inject_event { |
97 | my $extension = shift; |
248 | my $extension = shift; |
98 | my $event_code = shift; |
249 | my $event_code = shift; |
99 | |
250 | |
… | |
… | |
344 | } |
495 | } |
345 | |
496 | |
346 | ############################################################################# |
497 | ############################################################################# |
347 | # load/save/clean perl data associated with a map |
498 | # load/save/clean perl data associated with a map |
348 | |
499 | |
349 | *on_mapclean = sub { |
500 | *cf::mapsupport::on_clean = sub { |
350 | my ($map) = @_; |
501 | my ($map) = @_; |
351 | |
502 | |
352 | my $path = $map->tmpname; |
503 | my $path = $map->tmpname; |
353 | defined $path or return; |
504 | defined $path or return; |
354 | |
505 | |
355 | unlink "$path.cfperl"; |
506 | unlink "$path.cfperl"; |
356 | }; |
507 | }; |
357 | |
508 | |
358 | *on_mapin = |
509 | *cf::mapsupport::on_swapin = |
359 | *on_mapload = sub { |
510 | *cf::mapsupport::on_load = sub { |
360 | my ($map) = @_; |
511 | my ($map) = @_; |
361 | |
512 | |
362 | my $path = $map->tmpname; |
513 | my $path = $map->tmpname; |
363 | $path = $map->path unless defined $path; |
514 | $path = $map->path unless defined $path; |
364 | |
515 | |
… | |
… | |
371 | or return; # too new |
522 | or return; # too new |
372 | |
523 | |
373 | $map->_set_obs ($data->{obs}); |
524 | $map->_set_obs ($data->{obs}); |
374 | }; |
525 | }; |
375 | |
526 | |
376 | *on_mapout = sub { |
527 | *cf::mapsupport::on_swapout = sub { |
377 | my ($map) = @_; |
528 | my ($map) = @_; |
378 | |
529 | |
379 | my $path = $map->tmpname; |
530 | my $path = $map->tmpname; |
380 | $path = $map->path unless defined $path; |
531 | $path = $map->path unless defined $path; |
381 | |
532 | |
… | |
… | |
398 | } else { |
549 | } else { |
399 | unlink "$path.cfperl"; |
550 | unlink "$path.cfperl"; |
400 | } |
551 | } |
401 | }; |
552 | }; |
402 | |
553 | |
|
|
554 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
|
|
555 | |
403 | ############################################################################# |
556 | ############################################################################# |
404 | # load/save perl data associated with player->ob objects |
557 | # load/save perl data associated with player->ob objects |
405 | |
558 | |
406 | sub all_objects(@) { |
559 | sub all_objects(@) { |
407 | @_, map all_objects ($_->inv), @_ |
560 | @_, map all_objects ($_->inv), @_ |
408 | } |
561 | } |
409 | |
562 | |
410 | *on_player_load = sub { |
563 | attach_to_players |
|
|
564 | on_load => sub { |
411 | my ($ob, $path) = @_; |
565 | my ($pl, $path) = @_; |
412 | |
566 | |
413 | for my $o (all_objects $ob) { |
567 | for my $o (all_objects $pl->ob) { |
414 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
568 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
415 | $o->set_ob_key_value ("_perl_data"); |
569 | $o->set_ob_key_value ("_perl_data"); |
416 | |
570 | |
417 | %$o = %{ Storable::thaw pack "H*", $value }; |
571 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
572 | } |
418 | } |
573 | } |
419 | } |
574 | }, |
420 | }; |
575 | on_save => sub { |
421 | |
|
|
422 | *on_player_save = sub { |
|
|
423 | my ($ob, $path) = @_; |
576 | my ($pl, $path) = @_; |
424 | |
577 | |
425 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
578 | $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) |
426 | for grep %$_, all_objects $ob; |
579 | for grep %$_, all_objects $pl->ob; |
427 | }; |
580 | }, |
|
|
581 | ; |
428 | |
582 | |
429 | ############################################################################# |
583 | ############################################################################# |
430 | # core extensions - in perl |
584 | # core extensions - in perl |
431 | |
585 | |
432 | =item cf::player::exists $login |
586 | =item cf::player::exists $login |
… | |
… | |
533 | } |
687 | } |
534 | |
688 | |
535 | ############################################################################# |
689 | ############################################################################# |
536 | # the server's main() |
690 | # the server's main() |
537 | |
691 | |
538 | sub run { |
692 | sub main { |
539 | Event::loop; |
693 | Event::loop; |
540 | } |
694 | } |
541 | |
695 | |
542 | ############################################################################# |
696 | ############################################################################# |
543 | # initialisation |
697 | # initialisation |