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.36 by root, Thu Aug 24 14:04:29 2006 UTC vs.
Revision 1.43 by root, Fri Aug 25 17:11:53 2006 UTC

11use Event; 11use 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
14use strict; 14use strict;
15 15
16our %COMMAND; 16our %COMMAND = ();
17our @EVENT; 17our @EVENT;
18our @PLUGIN_EVENT;
18our %PROP_TYPE; 19our %PROP_TYPE;
19our %PROP_IDX; 20our %PROP_IDX;
20our $LIBDIR = maps_directory "perl"; 21our $LIBDIR = maps_directory "perl";
21 22
22our $TICK = MAX_TIME * 1e-6; 23our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER; 24our $TICK_WATCHER;
24our $NEXT_TICK; 25our $NEXT_TICK;
25 26
26BEGIN { 27BEGIN {
27 @EVENT = map lc, @EVENT; 28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
28 29
29 *CORE::GLOBAL::warn = sub { 30 *CORE::GLOBAL::warn = sub {
30 my $msg = join "", @_; 31 my $msg = join "", @_;
31 $msg .= "\n" 32 $msg .= "\n"
32 unless $msg =~ /\n$/; 33 unless $msg =~ /\n$/;
70prop_gen MAP_PROP => "cf::map"; 71prop_gen MAP_PROP => "cf::map";
71prop_gen ARCH_PROP => "cf::arch"; 72prop_gen ARCH_PROP => "cf::arch";
72 73
73# guessed hierarchies 74# guessed hierarchies
74 75
75@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 76@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
76@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; 77@safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
77 78
78# we bless all objects into derived classes to force a method lookup 79# we bless all objects into (empty) derived classes to force a method lookup
79# within the Safe compartment. 80# within the Safe compartment.
80for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { 81for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) {
81 no strict 'refs'; 82 no strict 'refs';
82 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 83 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
83} 84}
84 85
85$Event::DIED = sub { 86$Event::DIED = sub {
86 warn "error in event callback: @_"; 87 warn "error in event callback: @_";
87}; 88};
90my @exts; 91my @exts;
91my @hook; 92my @hook;
92my %command; 93my %command;
93my %extcmd; 94my %extcmd;
94 95
96#############################################################################
97# "new" plug-in system
98
99=item cf::object::attach ... # NYI
100
101=item cf::attach_global ...
102
103=item cf::attach_to_type ...
104
105=item cf::attach_to_objects ...
106
107=item cf::attach_to_players ...
108
109=item cf::attach_to_maps ...
110
111 prio => $number, # lower is earlier
112 on_xxx => \&cb,
113 package => package::,
114
115=cut
116
117# the following variables are defined in .xs and must not be re-created
118our @CB_GLOBAL = (); # registry for all global events
119our @CB_OBJECT = ();
120our @CB_PLAYER = ();
121our @CB_TYPE = (); # registry for type (cf-object class) based events
122our @CB_MAP = ();
123
124sub _attach_cb($\%$$$) {
125 my ($registry, $undo, $event, $prio, $cb) = @_;
126
127 use sort 'stable';
128
129 $cb = [$prio, $cb];
130
131 @{$registry->[$event]} = sort
132 { $a->[0] cmp $b->[0] }
133 @{$registry->[$event] || []}, $cb;
134
135 push @{$undo->{cb}}, [$event, $cb];
136}
137
138# attach handles attaching event callbacks
139# the only thing the caller has to do is pass the correct
140# registry (== where the callback attaches to).
141sub _attach(\@$\@) {
142 my ($registry, $klass, $arg) = @_;
143
144 my $prio = 0;
145
146 my %undo = (
147 registry => $registry,
148 cb => [],
149 );
150
151 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
152
153 while (@$arg) {
154 my $type = shift @$arg;
155
156 if ($type eq "prio") {
157 $prio = shift @$arg;
158
159 } elsif ($type eq "package") {
160 my $pkg = shift @$arg;
161
162 while (my ($name, $id) = each %cb_id) {
163 if (my $cb = $pkg->can ($name)) {
164 _attach_cb $registry, %undo, $id, $prio, $cb;
165 }
166 }
167
168 } elsif (exists $cb_id{$type}) {
169 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg;
170
171 } elsif (ref $type) {
172 warn "attaching objects not supported, ignoring.\n";
173
174 } else {
175 shift @$arg;
176 warn "attach argument '$type' not supported, ignoring.\n";
177 }
178 }
179
180 \%undo
181}
182
183sub cf::object::attach {
184 die;
185}
186
187sub attach_global {
188 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
189}
190
191sub attach_to_type {
192 my $type = shift;
193 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_
194}
195
196sub attach_to_objects {
197 _attach @CB_OBJECT, KLASS_OBJECT, @_
198}
199
200sub attach_to_players {
201 _attach @CB_PLAYER, KLASS_PLAYER, @_
202}
203
204sub attach_to_maps {
205 _attach @CB_MAP, KLASS_MAP, @_
206}
207
208our $override;
209
210sub override() {
211 $override = 1
212}
213
214sub invoke {
215 my $event = shift;
216 my $callbacks = shift;
217
218 local $override;
219
220 for (@$callbacks) {
221 eval { &{$_->[1]} };
222
223 if ($@) {
224 warn "$@";
225 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
226 override;
227 }
228
229 return 1 if $override;
230 }
231
232 0
233}
234
235#############################################################################
236# old plug-in events
237
95sub inject_event { 238sub inject_event {
96 my $extension = shift; 239 my $extension = shift;
97 my $event_code = shift; 240 my $event_code = shift;
98 241
99 my $cb = $hook[$event_code]{$extension} 242 my $cb = $hook[$event_code]{$extension}
141} 284}
142 285
143sub register { 286sub register {
144 my ($base, $pkg) = @_; 287 my ($base, $pkg) = @_;
145 288
146 for my $idx (0 .. $#EVENT) { 289 for my $idx (0 .. $#PLUGIN_EVENT) {
147 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 290 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
148 #warn "registering $EVENT[$idx] hook to '$pkg'\n"; 291 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
149 $hook[$idx]{$base} = $ref; 292 $hook[$idx]{$base} = $ref;
150 } 293 }
151 } 294 }
152} 295}
153 296
156 299
157 $path =~ /([^\/\\]+)\.ext$/ or die "$path"; 300 $path =~ /([^\/\\]+)\.ext$/ or die "$path";
158 my $base = $1; 301 my $base = $1;
159 my $pkg = $1; 302 my $pkg = $1;
160 $pkg =~ s/[^[:word:]]/_/g; 303 $pkg =~ s/[^[:word:]]/_/g;
161 $pkg = "cf::ext::$pkg"; 304 $pkg = "ext::$pkg";
162 305
163 warn "loading '$path' into '$pkg'\n"; 306 warn "loading '$path' into '$pkg'\n";
164 307
165 open my $fh, "<:utf8", $path 308 open my $fh, "<:utf8", $path
166 or die "$path: $!"; 309 or die "$path: $!";
187 my ($pkg) = @_; 330 my ($pkg) = @_;
188 331
189 warn "removing extension $pkg\n"; 332 warn "removing extension $pkg\n";
190 333
191 # remove hooks 334 # remove hooks
192 for my $idx (0 .. $#EVENT) { 335 for my $idx (0 .. $#PLUGIN_EVENT) {
193 delete $hook[$idx]{$pkg}; 336 delete $hook[$idx]{$pkg};
194 } 337 }
195 338
196 # remove commands 339 # remove commands
197 for my $name (keys %command) { 340 for my $name (keys %command) {
209 # remove extcmds 352 # remove extcmds
210 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 353 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
211 delete $extcmd{$name}; 354 delete $extcmd{$name};
212 } 355 }
213 356
214 if (my $cb = $pkg->can ("on_unload")) { 357 if (my $cb = $pkg->can ("unload")) {
215 eval { 358 eval {
216 $cb->($pkg); 359 $cb->($pkg);
217 1 360 1
218 } or warn "$pkg unloaded, but with errors: $@"; 361 } or warn "$pkg unloaded, but with errors: $@";
219 } 362 }
263 } 406 }
264 407
265 Symbol::delete_package $k; 408 Symbol::delete_package $k;
266 } 409 }
267 410
268 # 4. get rid of ext::, as good as possible 411 # 4. get rid of safe::, as good as possible
269 Symbol::delete_package "ext::$_" 412 Symbol::delete_package "safe::$_"
270 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); 413 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
271 414
272 # 5. remove register_script_function callbacks 415 # 5. remove register_script_function callbacks
273 # TODO 416 # TODO
274 417
275 # 6. unload cf.pm "a bit" 418 # 6. unload cf.pm "a bit"
276 delete $INC{"cf.pm"}; 419 delete $INC{"cf.pm"};
277 420
278 # don't, removes xs symbols, too 421 # don't, removes xs symbols, too,
422 # and global variables created in xs
279 #Symbol::delete_package __PACKAGE__; 423 #Symbol::delete_package __PACKAGE__;
280 424
281 # 7. reload cf.pm 425 # 7. reload cf.pm
282 $msg->("reloading cf.pm"); 426 $msg->("reloading cf.pm");
283 require cf; 427 require cf;
322 466
323############################################################################# 467#############################################################################
324# extcmd framework, basically convert ext <msg> 468# extcmd framework, basically convert ext <msg>
325# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 469# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
326 470
327sub on_extcmd { 471attach_global
472 on_extcmd => sub {
328 my ($pl, $buf) = @_; 473 my ($pl, $buf) = @_;
329 474
330 my $msg = eval { from_json $buf }; 475 my $msg = eval { from_json $buf };
331 476
332 if (ref $msg) { 477 if (ref $msg) {
333 if (my $cb = $extcmd{$msg->{msgtype}}) { 478 if (my $cb = $extcmd{$msg->{msgtype}}) {
334 if (my %reply = $cb->[0]->($pl, $msg)) { 479 if (my %reply = $cb->[0]->($pl, $msg)) {
335 $pl->ext_reply ($msg->{msgid}, %reply); 480 $pl->ext_reply ($msg->{msgid}, %reply);
481 }
336 } 482 }
483 } else {
484 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
337 } 485 }
338 } else { 486
339 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 487 cf::override;
340 } 488 },
341 489;
342 1
343}
344 490
345############################################################################# 491#############################################################################
346# load/save/clean perl data associated with a map 492# load/save/clean perl data associated with a map
347 493
348*on_mapclean = sub { 494*cf::mapsupport::on_clean = sub {
349 my ($map) = @_; 495 my ($map) = @_;
350 496
351 my $path = $map->tmpname; 497 my $path = $map->tmpname;
352 defined $path or return; 498 defined $path or return;
353 499
354 unlink "$path.cfperl"; 500 unlink "$path.cfperl";
355}; 501};
356 502
357*on_mapin = 503*cf::mapsupport::on_swapin =
358*on_mapload = sub { 504*cf::mapsupport::on_load = sub {
359 my ($map) = @_; 505 my ($map) = @_;
360 506
361 my $path = $map->tmpname; 507 my $path = $map->tmpname;
362 $path = $map->path unless defined $path; 508 $path = $map->path unless defined $path;
363 509
370 or return; # too new 516 or return; # too new
371 517
372 $map->_set_obs ($data->{obs}); 518 $map->_set_obs ($data->{obs});
373}; 519};
374 520
375*on_mapout = sub { 521*cf::mapsupport::on_swapout = sub {
376 my ($map) = @_; 522 my ($map) = @_;
377 523
378 my $path = $map->tmpname; 524 my $path = $map->tmpname;
379 $path = $map->path unless defined $path; 525 $path = $map->path unless defined $path;
380 526
397 } else { 543 } else {
398 unlink "$path.cfperl"; 544 unlink "$path.cfperl";
399 } 545 }
400}; 546};
401 547
548attach_to_maps prio => -10000, package => cf::mapsupport::;
549
402############################################################################# 550#############################################################################
403# load/save perl data associated with player->ob objects 551# load/save perl data associated with player->ob objects
404 552
405sub all_objects(@) { 553sub all_objects(@) {
406 @_, map all_objects ($_->inv), @_ 554 @_, map all_objects ($_->inv), @_
407} 555}
408 556
409*on_player_load = sub { 557attach_to_players
558 on_load => sub {
410 my ($ob, $path) = @_; 559 my ($pl, $path) = @_;
411 560
412 for my $o (all_objects $ob) { 561 for my $o (all_objects $pl->ob) {
413 if (my $value = $o->get_ob_key_value ("_perl_data")) { 562 if (my $value = $o->get_ob_key_value ("_perl_data")) {
414 $o->set_ob_key_value ("_perl_data"); 563 $o->set_ob_key_value ("_perl_data");
415 564
416 %$o = %{ Storable::thaw pack "H*", $value }; 565 %$o = %{ Storable::thaw pack "H*", $value };
566 }
417 } 567 }
418 } 568 },
419}; 569 on_save => sub {
420
421*on_player_save = sub {
422 my ($ob, $path) = @_; 570 my ($pl, $path) = @_;
423 571
424 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 572 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
425 for grep %$_, all_objects $ob; 573 for grep %$_, all_objects $pl->ob;
426}; 574 },
575;
427 576
428############################################################################# 577#############################################################################
429# core extensions - in perl 578# core extensions - in perl
430 579
431=item cf::player::exists $login 580=item cf::player::exists $login
477} 626}
478 627
479############################################################################# 628#############################################################################
480# map scripting support 629# map scripting support
481 630
482our $safe = new Safe "ext"; 631our $safe = new Safe "safe";
483our $safe_hole = new Safe::Hole; 632our $safe_hole = new Safe::Hole;
484 633
485$SIG{FPE} = 'IGNORE'; 634$SIG{FPE} = 'IGNORE';
486 635
487$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 636$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
493 ["cf::object::player" => qw(player)], 642 ["cf::object::player" => qw(player)],
494 ["cf::player" => qw(peaceful)], 643 ["cf::player" => qw(peaceful)],
495) { 644) {
496 no strict 'refs'; 645 no strict 'refs';
497 my ($pkg, @funs) = @$_; 646 my ($pkg, @funs) = @$_;
498 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 647 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
499 for @funs; 648 for @funs;
500} 649}
501 650
502sub safe_eval($;@) { 651sub safe_eval($;@) {
503 my ($code, %vars) = @_; 652 my ($code, %vars) = @_;
505 my $qcode = $code; 654 my $qcode = $code;
506 $qcode =~ s/"/‟/g; # not allowed in #line filenames 655 $qcode =~ s/"/‟/g; # not allowed in #line filenames
507 $qcode =~ s/\n/\\n/g; 656 $qcode =~ s/\n/\\n/g;
508 657
509 local $_; 658 local $_;
510 local @ext::cf::_safe_eval_args = values %vars; 659 local @safe::cf::_safe_eval_args = values %vars;
511 660
512 $code = 661 my $eval =
513 "do {\n" 662 "do {\n"
514 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 663 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
515 . "#line 0 \"{$qcode}\"\n" 664 . "#line 0 \"{$qcode}\"\n"
516 . $code 665 . $code
517 . "\n}" 666 . "\n}"
518 ; 667 ;
519 668
520 sub_generation_inc; 669 sub_generation_inc;
521 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); 670 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
522 sub_generation_inc; 671 sub_generation_inc;
672
673 if ($@) {
674 warn "$@";
675 warn "while executing safe code '$code'\n";
676 warn "with arguments " . (join " ", %vars) . "\n";
677 }
523 678
524 wantarray ? @res : $res[0] 679 wantarray ? @res : $res[0]
525} 680}
526 681
527sub register_script_function { 682sub register_script_function {
528 my ($fun, $cb) = @_; 683 my ($fun, $cb) = @_;
529 684
530 no strict 'refs'; 685 no strict 'refs';
531 *{"ext::$fun"} = $safe_hole->wrap ($cb); 686 *{"safe::$fun"} = $safe_hole->wrap ($cb);
532} 687}
533 688
534############################################################################# 689#############################################################################
535# the server's main() 690# the server's main()
536 691
537sub run { 692sub main {
538 Event::loop; 693 Event::loop;
539} 694}
540 695
541############################################################################# 696#############################################################################
542# initialisation 697# initialisation
554 cf::server_tick; # one server iteration 709 cf::server_tick; # one server iteration
555 710
556 my $NOW = Event::time; 711 my $NOW = Event::time;
557 $NEXT_TICK += $TICK; 712 $NEXT_TICK += $TICK;
558 713
559 # if we are delayed by > 0.25 second, skip ticks 714 # if we are delayed by four ticks, skip them all
560 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25; 715 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
561 716
562 $TICK_WATCHER->at ($NEXT_TICK); 717 $TICK_WATCHER->at ($NEXT_TICK);
563 $TICK_WATCHER->start; 718 $TICK_WATCHER->start;
564 }, 719 },
565); 720);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines