ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.83 by root, Mon Dec 11 01:30:41 2006 UTC vs.
Revision 1.93 by root, Thu Dec 21 22:41:35 2006 UTC

16# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? 16# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode?
17$YAML::Syck::ImplicitUnicode = 1; 17$YAML::Syck::ImplicitUnicode = 1;
18 18
19use strict; 19use strict;
20 20
21sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
22
23our %COMMAND = ();
24our %COMMAND_TIME = ();
25our %EXTCMD = ();
26
21_init_vars; 27_init_vars;
22 28
23our %COMMAND = ();
24our @EVENT; 29our @EVENT;
25our $LIBDIR = maps_directory "perl"; 30our $LIBDIR = datadir . "/ext";
26 31
27our $TICK = MAX_TIME * 1e-6; 32our $TICK = MAX_TIME * 1e-6;
28our $TICK_WATCHER; 33our $TICK_WATCHER;
29our $NEXT_TICK; 34our $NEXT_TICK;
30 35
31our %CFG; 36our %CFG;
32 37
33our $uptime;#d# 38our $UPTIME; $UPTIME ||= time;
34our $UPTIME;
35$UPTIME ||= $uptime;#d#
36$UPTIME ||= time;
37 39
38############################################################################# 40#############################################################################
39 41
40=head2 GLOBAL VARIABLES 42=head2 GLOBAL VARIABLES
41 43
72 print STDERR "cfperl: $msg"; 74 print STDERR "cfperl: $msg";
73 LOG llevError, "cfperl: $msg"; 75 LOG llevError, "cfperl: $msg";
74 }; 76 };
75} 77}
76 78
79@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
80@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable';
81@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable';
82@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable';
83@safe::cf::map::ISA = @cf::map::ISA = 'cf::attachable';
77@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 84@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
78 85
79# we bless all objects into (empty) derived classes to force a method lookup 86# we bless all objects into (empty) derived classes to force a method lookup
80# within the Safe compartment. 87# within the Safe compartment.
81for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { 88for my $pkg (qw(
89 cf::global
90 cf::object cf::object::player
91 cf::client cf::player
92 cf::arch cf::living
93 cf::map cf::party cf::region
94)) {
82 no strict 'refs'; 95 no strict 'refs';
83 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 96 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
84} 97}
85 98
86$Event::DIED = sub { 99$Event::DIED = sub {
88}; 101};
89 102
90my %ext_pkg; 103my %ext_pkg;
91my @exts; 104my @exts;
92my @hook; 105my @hook;
93my %command;
94my %extcmd;
95 106
96=head2 UTILITY FUNCTIONS 107=head2 UTILITY FUNCTIONS
97 108
98=over 4 109=over 4
99 110
127 138
128=cut 139=cut
129 140
130############################################################################# 141#############################################################################
131 142
132=head2 EVENTS AND OBJECT ATTACHMENTS 143=head2 ATTACHABLE OBJECTS
144
145You can define and attach attachments to each "attachable" object in
146crossfire+ (objects, players, clients, maps and the special "global"
147class). In the following description, CLASS can be any of C<global>,
148C<object> C<player>, C<client> or C<map>.
133 149
134=over 4 150=over 4
135 151
136=item $object->attach ($attachment, key => $value...) 152=item cf::CLASS::attachment $name, ...
137 153
138=item $object->detach ($attachment) 154Register an attachment by name through which attachable objects can refer
155to this attachment.
139 156
140Attach/detach a pre-registered attachment to an object.
141
142=item $player->attach ($attachment, key => $value...)
143
144=item $player->detach ($attachment)
145
146Attach/detach a pre-registered attachment to a player.
147
148=item $map->attach ($attachment, key => $value...)
149
150=item $map->detach ($attachment)
151
152Attach/detach a pre-registered attachment to a map.
153
154=item $bool = $object->attached ($name)
155
156=item $bool = $player->attached ($name)
157
158=item $bool = $map->attached ($name) 157=item $bool = $attachable->attached ($name)
159 158
160Checks wether the named attachment is currently attached to the object. 159Checks wether the named attachment is currently attached to the object.
161 160
162=item cf::attach_global ... 161=item $attachable->attach ($attachment, key => $value...)
163 162
164Attach handlers for global events. 163=item $attachable->detach ($attachment)
165 164
166This and all following C<attach_*>-functions expect any number of the 165Attach/detach a pre-registered attachment either to a specific object
167following handler/hook descriptions: 166(C<$attachable>) or all objects of the given class (if C<$attachable> is a
167class in a static method call).
168
169You can attach to global events by using the C<cf::global> class.
170
171These method calls expect any number of the following handler/hook
172descriptions:
168 173
169=over 4 174=over 4
170 175
171=item prio => $number 176=item prio => $number
172 177
174by another C<prio> setting). Lower priority handlers get executed 179by another C<prio> setting). Lower priority handlers get executed
175earlier. The default priority is C<0>, and many built-in handlers are 180earlier. The default priority is C<0>, and many built-in handlers are
176registered at priority C<-1000>, so lower priorities should not be used 181registered at priority C<-1000>, so lower priorities should not be used
177unless you know what you are doing. 182unless you know what you are doing.
178 183
184=item type => $type
185
186(Only for C<< cf::object->attach >> calls), limits the attachment to the
187given type of objects only (the additional parameter C<subtype> can be
188used to further limit to the given subtype).
189
179=item on_I<event> => \&cb 190=item on_I<event> => \&cb
180 191
181Call the given code reference whenever the named event happens (event is 192Call the given code reference whenever the named event happens (event is
182something like C<instantiate>, C<apply>, C<use_skill> and so on, and which 193something like C<instantiate>, C<apply>, C<use_skill> and so on, and which
183handlers are recognised generally depends on the type of object these 194handlers are recognised generally depends on the type of object these
192package and register them. Only handlers for eevents supported by the 203package and register them. Only handlers for eevents supported by the
193object/class are recognised. 204object/class are recognised.
194 205
195=back 206=back
196 207
197=item cf::attach_to_type $object_type, $subtype, ...
198
199Attach handlers for a specific object type (e.g. TRANSPORT) and
200subtype. If C<$subtype> is zero or undef, matches all objects of the given
201type.
202
203=item cf::attach_to_objects ...
204
205Attach handlers to all objects. Do not use this except for debugging or
206very rare events, as handlers are (obviously) called for I<all> objects in
207the game.
208
209=item cf::attach_to_players ...
210
211Attach handlers to all players.
212
213=item cf::attach_to_maps ...
214
215Attach handlers to all maps.
216
217=item cf:register_attachment $name, ...
218
219Register an attachment by name through which objects can refer to this
220attachment.
221
222=item cf:register_player_attachment $name, ...
223
224Register an attachment by name through which players can refer to this
225attachment.
226
227=item cf:register_map_attachment $name, ...
228
229Register an attachment by name through which maps can refer to this
230attachment.
231
232=cut 208=cut
233 209
234# the following variables are defined in .xs and must not be re-created 210# the following variables are defined in .xs and must not be re-created
235our @CB_GLOBAL = (); # registry for all global events 211our @CB_GLOBAL = (); # registry for all global events
236our @CB_OBJECT = (); # all objects (should not be used except in emergency) 212our @CB_OBJECT = (); # all objects (should not be used except in emergency)
237our @CB_PLAYER = (); 213our @CB_PLAYER = ();
214our @CB_CLIENT = ();
238our @CB_TYPE = (); # registry for type (cf-object class) based events 215our @CB_TYPE = (); # registry for type (cf-object class) based events
239our @CB_MAP = (); 216our @CB_MAP = ();
240 217
241my %attachment; 218my %attachment;
242 219
243sub _attach_cb($\%$$$) { 220sub _attach_cb($$$$) {
244 my ($registry, $undo, $event, $prio, $cb) = @_; 221 my ($registry, $event, $prio, $cb) = @_;
245 222
246 use sort 'stable'; 223 use sort 'stable';
247 224
248 $cb = [$prio, $cb]; 225 $cb = [$prio, $cb];
249 226
250 @{$registry->[$event]} = sort 227 @{$registry->[$event]} = sort
251 { $a->[0] cmp $b->[0] } 228 { $a->[0] cmp $b->[0] }
252 @{$registry->[$event] || []}, $cb; 229 @{$registry->[$event] || []}, $cb;
253
254 push @{$undo->{cb}}, [$event, $cb];
255} 230}
256 231
257# attach handles attaching event callbacks 232# attach handles attaching event callbacks
258# the only thing the caller has to do is pass the correct 233# the only thing the caller has to do is pass the correct
259# registry (== where the callback attaches to). 234# registry (== where the callback attaches to).
260sub _attach(\@$@) { 235sub _attach {
261 my ($registry, $klass, @arg) = @_; 236 my ($registry, $klass, @arg) = @_;
262 237
238 my $object_type;
263 my $prio = 0; 239 my $prio = 0;
264
265 my %undo = (
266 registry => $registry,
267 cb => [],
268 );
269
270 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; 240 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
271 241
272 while (@arg) { 242 while (@arg) {
273 my $type = shift @arg; 243 my $type = shift @arg;
274 244
275 if ($type eq "prio") { 245 if ($type eq "prio") {
276 $prio = shift @arg; 246 $prio = shift @arg;
277 247
248 } elsif ($type eq "type") {
249 $object_type = shift @arg;
250 $registry = $CB_TYPE[$object_type] ||= [];
251
252 } elsif ($type eq "subtype") {
253 defined $object_type or Carp::croak "subtype specified without type";
254 my $object_subtype = shift @arg;
255 $registry = $CB_TYPE[$object_type + $object_subtype * NUM_SUBTYPES] ||= [];
256
278 } elsif ($type eq "package") { 257 } elsif ($type eq "package") {
279 my $pkg = shift @arg; 258 my $pkg = shift @arg;
280 259
281 while (my ($name, $id) = each %cb_id) { 260 while (my ($name, $id) = each %cb_id) {
282 if (my $cb = $pkg->can ($name)) { 261 if (my $cb = $pkg->can ($name)) {
283 _attach_cb $registry, %undo, $id, $prio, $cb; 262 _attach_cb $registry, $id, $prio, $cb;
284 } 263 }
285 } 264 }
286 265
287 } elsif (exists $cb_id{$type}) { 266 } elsif (exists $cb_id{$type}) {
288 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; 267 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg;
289 268
290 } elsif (ref $type) { 269 } elsif (ref $type) {
291 warn "attaching objects not supported, ignoring.\n"; 270 warn "attaching objects not supported, ignoring.\n";
292 271
293 } else { 272 } else {
294 shift @arg; 273 shift @arg;
295 warn "attach argument '$type' not supported, ignoring.\n"; 274 warn "attach argument '$type' not supported, ignoring.\n";
296 } 275 }
297 } 276 }
298
299 \%undo
300} 277}
301 278
302sub _attach_attachment { 279sub _object_attach {
303 my ($obj, $name, %arg) = @_; 280 my ($obj, $name, %arg) = @_;
304 281
305 return if exists $obj->{_attachment}{$name}; 282 return if exists $obj->{_attachment}{$name};
306
307 my $res;
308 283
309 if (my $attach = $attachment{$name}) { 284 if (my $attach = $attachment{$name}) {
310 my $registry = $obj->registry; 285 my $registry = $obj->registry;
311 286
312 for (@$attach) { 287 for (@$attach) {
313 my ($klass, @attach) = @$_; 288 my ($klass, @attach) = @$_;
314 $res = _attach @$registry, $klass, @attach; 289 _attach $registry, $klass, @attach;
315 } 290 }
316 291
317 $obj->{$name} = \%arg; 292 $obj->{$name} = \%arg;
318 } else { 293 } else {
319 warn "object uses attachment '$name' that is not available, postponing.\n"; 294 warn "object uses attachment '$name' that is not available, postponing.\n";
320 } 295 }
321 296
322 $obj->{_attachment}{$name} = undef; 297 $obj->{_attachment}{$name} = undef;
323
324 $res->{attachment} = $name;
325 $res
326} 298}
327 299
328*cf::object::attach = 300sub cf::attachable::attach {
329*cf::player::attach = 301 if (ref $_[0]) {
330*cf::map::attach = sub { 302 _object_attach @_;
331 my ($obj, $name, %arg) = @_; 303 } else {
332 304 _attach shift->_attach_registry, @_;
333 _attach_attachment $obj, $name, %arg; 305 }
334}; 306};
335 307
336# all those should be optimised 308# all those should be optimised
337*cf::object::detach = 309sub cf::attachable::detach {
338*cf::player::detach =
339*cf::map::detach = sub {
340 my ($obj, $name) = @_; 310 my ($obj, $name) = @_;
341 311
312 if (ref $obj) {
342 delete $obj->{_attachment}{$name}; 313 delete $obj->{_attachment}{$name};
343 reattach ($obj); 314 reattach ($obj);
315 } else {
316 Carp::croak "cannot, currently, detach class attachments";
317 }
344}; 318};
345 319
346*cf::object::attached = 320sub cf::attachable::attached {
347*cf::player::attached =
348*cf::map::attached = sub {
349 my ($obj, $name) = @_; 321 my ($obj, $name) = @_;
350 322
351 exists $obj->{_attachment}{$name} 323 exists $obj->{_attachment}{$name}
352};
353
354sub attach_global {
355 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
356} 324}
357 325
358sub attach_to_type { 326for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) {
359 my $type = shift; 327 eval "#line " . __LINE__ . " 'cf.pm'
360 my $subtype = shift; 328 sub cf::\L$klass\E::_attach_registry {
329 (\\\@CB_$klass, KLASS_$klass)
330 }
361 331
362 _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ 332 sub cf::\L$klass\E::attachment {
363}
364
365sub attach_to_objects {
366 _attach @CB_OBJECT, KLASS_OBJECT, @_
367}
368
369sub attach_to_players {
370 _attach @CB_PLAYER, KLASS_PLAYER, @_
371}
372
373sub attach_to_maps {
374 _attach @CB_MAP, KLASS_MAP, @_
375}
376
377sub register_attachment {
378 my $name = shift; 333 my \$name = shift;
379 334
380 $attachment{$name} = [[KLASS_OBJECT, @_]];
381}
382
383sub register_player_attachment {
384 my $name = shift;
385
386 $attachment{$name} = [[KLASS_PLAYER, @_]];
387}
388
389sub register_map_attachment {
390 my $name = shift;
391
392 $attachment{$name} = [[KLASS_MAP, @_]]; 335 \$attachment{\$name} = [[KLASS_$klass, \@_]];
336 }
337 ";
338 die if $@;
393} 339}
394 340
395our $override; 341our $override;
396our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? 342our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
397 343
427 373
428=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) 374=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
429 375
430=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) 376=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
431 377
378=item $bool = $client->invoke (EVENT_CLIENT_XXX, ...)
379
432=item $bool = $map->invoke (EVENT_MAP_XXX, ...) 380=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
433 381
434Generate a global/object/player/map-specific event with the given arguments. 382Generate a global/object/player/map-specific event with the given arguments.
435 383
436This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be 384This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be
441 389
442=cut 390=cut
443 391
444############################################################################# 392#############################################################################
445 393
446=head2 METHODS VALID FOR ALL CORE OBJECTS 394=head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS
395
396Attachable objects includes objects, players, clients and maps.
447 397
448=over 4 398=over 4
449 399
450=item $object->valid, $player->valid, $map->valid 400=item $object->valid
451 401
452Just because you have a perl object does not mean that the corresponding 402Just because you have a perl object does not mean that the corresponding
453C-level object still exists. If you try to access an object that has no 403C-level object still exists. If you try to access an object that has no
454valid C counterpart anymore you get an exception at runtime. This method 404valid C counterpart anymore you get an exception at runtime. This method
455can be used to test for existence of the C object part without causing an 405can be used to test for existence of the C object part without causing an
457 407
458=back 408=back
459 409
460=cut 410=cut
461 411
462*cf::object::valid =
463*cf::player::valid =
464*cf::map::valid = \&cf::_valid;
465
466############################################################################# 412#############################################################################
467# object support 413# object support
468 414
469sub instantiate { 415sub instantiate {
470 my ($obj, $data) = @_; 416 my ($obj, $data) = @_;
489 435
490 for my $name (keys %{ $obj->{_attachment} || {} }) { 436 for my $name (keys %{ $obj->{_attachment} || {} }) {
491 if (my $attach = $attachment{$name}) { 437 if (my $attach = $attachment{$name}) {
492 for (@$attach) { 438 for (@$attach) {
493 my ($klass, @attach) = @$_; 439 my ($klass, @attach) = @$_;
494 _attach @$registry, $klass, @attach; 440 _attach $registry, $klass, @attach;
495 } 441 }
496 } else { 442 } else {
497 warn "object uses attachment '$name' that is not available, postponing.\n"; 443 warn "object uses attachment '$name' that is not available, postponing.\n";
498 } 444 }
499 } 445 }
555 } 501 }
556 502
557 () 503 ()
558} 504}
559 505
560attach_to_objects 506cf::object->attach (
561 prio => -1000000, 507 prio => -1000000,
562 on_clone => sub { 508 on_clone => sub {
563 my ($src, $dst) = @_; 509 my ($src, $dst) = @_;
564 510
565 @{$dst->registry} = @{$src->registry}; 511 @{$dst->registry} = @{$src->registry};
567 %$dst = %$src; 513 %$dst = %$src;
568 514
569 %{$dst->{_attachment}} = %{$src->{_attachment}} 515 %{$dst->{_attachment}} = %{$src->{_attachment}}
570 if exists $src->{_attachment}; 516 if exists $src->{_attachment};
571 }, 517 },
572; 518);
573 519
574############################################################################# 520#############################################################################
575# old plug-in events 521# command handling &c
576 522
577sub inject_event { 523=item cf::register_command $name => \&callback($ob,$args);
578 my $extension = shift;
579 my $event_code = shift;
580 524
581 my $cb = $hook[$event_code]{$extension} 525Register a callback for execution when the client sends the user command
582 or return; 526$name.
583 527
584 &$cb 528=cut
585}
586
587sub inject_global_event {
588 my $event = shift;
589
590 my $cb = $hook[$event]
591 or return;
592
593 List::Util::max map &$_, values %$cb
594}
595
596sub inject_command {
597 my ($name, $obj, $params) = @_;
598
599 for my $cmd (@{ $command{$name} }) {
600 $cmd->[1]->($obj, $params);
601 }
602
603 -1
604}
605 529
606sub register_command { 530sub register_command {
607 my ($name, $time, $cb) = @_; 531 my ($name, $cb) = @_;
608 532
609 my $caller = caller; 533 my $caller = caller;
610 #warn "registering command '$name/$time' to '$caller'"; 534 #warn "registering command '$name/$time' to '$caller'";
611 535
612 push @{ $command{$name} }, [$time, $cb, $caller]; 536 push @{ $COMMAND{$name} }, [$caller, $cb];
613 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
614} 537}
538
539=item cf::register_extcmd $name => \&callback($pl,$packet);
540
541Register a callbackf ro execution when the client sends an extcmd packet.
542
543If the callback returns something, it is sent back as if reply was being
544called.
545
546=cut
615 547
616sub register_extcmd { 548sub register_extcmd {
617 my ($name, $cb) = @_; 549 my ($name, $cb) = @_;
618 550
619 my $caller = caller; 551 my $caller = caller;
620 #warn "registering extcmd '$name' to '$caller'"; 552 #warn "registering extcmd '$name' to '$caller'";
621 553
622 $extcmd{$name} = [$cb, $caller]; 554 $EXTCMD{$name} = [$cb, $caller];
623} 555}
556
557cf::player->attach (
558 on_command => sub {
559 my ($pl, $name, $params) = @_;
560
561 my $cb = $COMMAND{$name}
562 or return;
563
564 for my $cmd (@$cb) {
565 $cmd->[1]->($pl->ob, $params);
566 }
567
568 cf::override;
569 },
570 on_extcmd => sub {
571 my ($pl, $buf) = @_;
572
573 my $msg = eval { from_json $buf };
574
575 if (ref $msg) {
576 if (my $cb = $EXTCMD{$msg->{msgtype}}) {
577 if (my %reply = $cb->[0]->($pl, $msg)) {
578 $pl->ext_reply ($msg->{msgid}, %reply);
579 }
580 }
581 } else {
582 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
583 }
584
585 cf::override;
586 },
587);
624 588
625sub register { 589sub register {
626 my ($base, $pkg) = @_; 590 my ($base, $pkg) = @_;
627 591
628 #TODO 592 #TODO
671# for my $idx (0 .. $#PLUGIN_EVENT) { 635# for my $idx (0 .. $#PLUGIN_EVENT) {
672# delete $hook[$idx]{$pkg}; 636# delete $hook[$idx]{$pkg};
673# } 637# }
674 638
675 # remove commands 639 # remove commands
676 for my $name (keys %command) { 640 for my $name (keys %COMMAND) {
677 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 641 my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} };
678 642
679 if (@cb) { 643 if (@cb) {
680 $command{$name} = \@cb; 644 $COMMAND{$name} = \@cb;
681 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
682 } else { 645 } else {
683 delete $command{$name};
684 delete $COMMAND{"$name\000"}; 646 delete $COMMAND{$name};
685 } 647 }
686 } 648 }
687 649
688 # remove extcmds 650 # remove extcmds
689 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 651 for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) {
690 delete $extcmd{$name}; 652 delete $EXTCMD{$name};
691 } 653 }
692 654
693 if (my $cb = $pkg->can ("unload")) { 655 if (my $cb = $pkg->can ("unload")) {
694 eval { 656 eval {
695 $cb->($pkg); 657 $cb->($pkg);
699 661
700 Symbol::delete_package $pkg; 662 Symbol::delete_package $pkg;
701} 663}
702 664
703sub load_extensions { 665sub load_extensions {
704 my $LIBDIR = maps_directory "perl";
705
706 for my $ext (<$LIBDIR/*.ext>) { 666 for my $ext (<$LIBDIR/*.ext>) {
707 next unless -r $ext; 667 next unless -r $ext;
708 eval { 668 eval {
709 load_extension $ext; 669 load_extension $ext;
710 1 670 1
711 } or warn "$ext not loaded: $@"; 671 } or warn "$ext not loaded: $@";
712 } 672 }
713} 673}
714 674
715############################################################################# 675#############################################################################
716# extcmd framework, basically convert ext <msg>
717# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
718
719attach_to_players
720 on_extcmd => sub {
721 my ($pl, $buf) = @_;
722
723 my $msg = eval { from_json $buf };
724
725 if (ref $msg) {
726 if (my $cb = $extcmd{$msg->{msgtype}}) {
727 if (my %reply = $cb->[0]->($pl, $msg)) {
728 $pl->ext_reply ($msg->{msgid}, %reply);
729 }
730 }
731 } else {
732 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
733 }
734
735 cf::override;
736 },
737;
738
739#############################################################################
740# load/save/clean perl data associated with a map 676# load/save/clean perl data associated with a map
741 677
742*cf::mapsupport::on_clean = sub { 678*cf::mapsupport::on_clean = sub {
743 my ($map) = @_; 679 my ($map) = @_;
744 680
746 defined $path or return; 682 defined $path or return;
747 683
748 unlink "$path.pst"; 684 unlink "$path.pst";
749}; 685};
750 686
751attach_to_maps prio => -10000, package => cf::mapsupport::; 687cf::map->attach (prio => -10000, package => cf::mapsupport::);
752 688
753############################################################################# 689#############################################################################
754# load/save perl data associated with player->ob objects 690# load/save perl data associated with player->ob objects
755 691
756sub all_objects(@) { 692sub all_objects(@) {
757 @_, map all_objects ($_->inv), @_ 693 @_, map all_objects ($_->inv), @_
758} 694}
759 695
760# TODO: compatibility cruft, remove when no longer needed 696# TODO: compatibility cruft, remove when no longer needed
761attach_to_players 697cf::player->attach (
762 on_load => sub { 698 on_load => sub {
763 my ($pl, $path) = @_; 699 my ($pl, $path) = @_;
764 700
765 for my $o (all_objects $pl->ob) { 701 for my $o (all_objects $pl->ob) {
766 if (my $value = $o->get_ob_key_value ("_perl_data")) { 702 if (my $value = $o->get_ob_key_value ("_perl_data")) {
768 704
769 %$o = %{ Storable::thaw pack "H*", $value }; 705 %$o = %{ Storable::thaw pack "H*", $value };
770 } 706 }
771 } 707 }
772 }, 708 },
773; 709);
774 710
775############################################################################# 711#############################################################################
776 712
777=head2 CORE EXTENSIONS 713=head2 CORE EXTENSIONS
778 714
869 805
870=pod 806=pod
871 807
872The following fucntions and emthods are available within a safe environment: 808The following fucntions and emthods are available within a safe environment:
873 809
874 cf::object contr pay_amount pay_player 810 cf::object contr pay_amount pay_player map
875 cf::object::player player 811 cf::object::player player
876 cf::player peaceful 812 cf::player peaceful
813 cf::map trigger
877 814
878=cut 815=cut
879 816
880for ( 817for (
881 ["cf::object" => qw(contr pay_amount pay_player)], 818 ["cf::object" => qw(contr pay_amount pay_player map)],
882 ["cf::object::player" => qw(player)], 819 ["cf::object::player" => qw(player)],
883 ["cf::player" => qw(peaceful)], 820 ["cf::player" => qw(peaceful)],
821 ["cf::map" => qw(trigger)],
884) { 822) {
885 no strict 'refs'; 823 no strict 'refs';
886 my ($pkg, @funs) = @$_; 824 my ($pkg, @funs) = @$_;
887 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 825 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
888 for @funs; 826 for @funs;
1028 sub db_sync() { 966 sub db_sync() {
1029 db_save if $dirty; 967 db_save if $dirty;
1030 undef $dirty; 968 undef $dirty;
1031 } 969 }
1032 970
1033 my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { 971 my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub {
1034 db_sync; 972 db_sync;
1035 }); 973 });
1036 974
1037 sub db_dirty() { 975 sub db_dirty() {
1038 $dirty = 1; 976 $dirty = 1;
1052 $DB->{$_[0]} = $_[1]; 990 $DB->{$_[0]} = $_[1];
1053 } 991 }
1054 db_dirty; 992 db_dirty;
1055 } 993 }
1056 994
1057 attach_global 995 cf::global->attach (
1058 prio => 10000, 996 prio => 10000,
1059 on_cleanup => sub { 997 on_cleanup => sub {
1060 db_sync; 998 db_sync;
1061 }, 999 },
1062 ; 1000 );
1063} 1001}
1064 1002
1065############################################################################# 1003#############################################################################
1066# the server's main() 1004# the server's main()
1067 1005
1088 1026
1089 $msg->("reloading..."); 1027 $msg->("reloading...");
1090 1028
1091 eval { 1029 eval {
1092 # cancel all watchers 1030 # cancel all watchers
1093 $_->cancel for Event::all_watchers; 1031 for (Event::all_watchers) {
1032 $_->cancel if $_->data & WF_AUTOCANCEL;
1033 }
1094 1034
1095 # unload all extensions 1035 # unload all extensions
1096 for (@exts) { 1036 for (@exts) {
1097 $msg->("unloading <$_>"); 1037 $msg->("unloading <$_>");
1098 unload_extension $_; 1038 unload_extension $_;
1158 warn $_[0]; 1098 warn $_[0];
1159 print "$_[0]\n"; 1099 print "$_[0]\n";
1160 }; 1100 };
1161} 1101}
1162 1102
1103register "<global>", __PACKAGE__;
1104
1163register_command "perl-reload", 0, sub { 1105register_command "perl-reload" => sub {
1164 my ($who, $arg) = @_; 1106 my ($who, $arg) = @_;
1165 1107
1166 if ($who->flag (FLAG_WIZ)) { 1108 if ($who->flag (FLAG_WIZ)) {
1167 _perl_reload { 1109 _perl_reload {
1168 warn $_[0]; 1110 warn $_[0];
1169 $who->message ($_[0]); 1111 $who->message ($_[0]);
1170 }; 1112 };
1171 } 1113 }
1172}; 1114};
1173 1115
1174register "<global>", __PACKAGE__;
1175
1176unshift @INC, $LIBDIR; 1116unshift @INC, $LIBDIR;
1177 1117
1178$TICK_WATCHER = Event->timer ( 1118$TICK_WATCHER = Event->timer (
1179 prio => 1, 1119 prio => 0,
1180 async => 1,
1181 at => $NEXT_TICK || 1, 1120 at => $NEXT_TICK || 1,
1121 data => WF_AUTOCANCEL,
1182 cb => sub { 1122 cb => sub {
1183 cf::server_tick; # one server iteration 1123 cf::server_tick; # one server iteration
1184 1124
1185 my $NOW = Event::time; 1125 my $NOW = Event::time;
1186 $NEXT_TICK += $TICK; 1126 $NEXT_TICK += $TICK;
1196IO::AIO::max_poll_time $TICK * 0.2; 1136IO::AIO::max_poll_time $TICK * 0.2;
1197 1137
1198Event->io (fd => IO::AIO::poll_fileno, 1138Event->io (fd => IO::AIO::poll_fileno,
1199 poll => 'r', 1139 poll => 'r',
1200 prio => 5, 1140 prio => 5,
1141 data => WF_AUTOCANCEL,
1201 cb => \&IO::AIO::poll_cb); 1142 cb => \&IO::AIO::poll_cb);
1202 1143
12031 11441
1204 1145

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines