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.37 by root, Thu Aug 24 14:05:22 2006 UTC vs.
Revision 1.44 by root, Sat Aug 26 08:44:06 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# object support
98
99sub reattach {
100 warn "reattach<@_>\n";
101}
102
103sub instantiate {
104 warn "instantiate<@_>\n";
105}
106
107sub clone {
108 warn "clone<@_>\n";
109}
110
111#############################################################################
112# "new" plug-in system
113
114=item cf::object::attach ... # NYI
115
116=item cf::attach_global ...
117
118=item cf::attach_to_type ...
119
120=item cf::attach_to_objects ...
121
122=item cf::attach_to_players ...
123
124=item cf::attach_to_maps ...
125
126 prio => $number, # lower is earlier
127 on_xxx => \&cb,
128 package => package::,
129
130=cut
131
132# the following variables are defined in .xs and must not be re-created
133our @CB_GLOBAL = (); # registry for all global events
134our @CB_OBJECT = ();
135our @CB_PLAYER = ();
136our @CB_TYPE = (); # registry for type (cf-object class) based events
137our @CB_MAP = ();
138
139sub _attach_cb($\%$$$) {
140 my ($registry, $undo, $event, $prio, $cb) = @_;
141
142 use sort 'stable';
143
144 $cb = [$prio, $cb];
145
146 @{$registry->[$event]} = sort
147 { $a->[0] cmp $b->[0] }
148 @{$registry->[$event] || []}, $cb;
149
150 push @{$undo->{cb}}, [$event, $cb];
151}
152
153# attach handles attaching event callbacks
154# the only thing the caller has to do is pass the correct
155# registry (== where the callback attaches to).
156sub _attach(\@$\@) {
157 my ($registry, $klass, $arg) = @_;
158
159 my $prio = 0;
160
161 my %undo = (
162 registry => $registry,
163 cb => [],
164 );
165
166 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
167
168 while (@$arg) {
169 my $type = shift @$arg;
170
171 if ($type eq "prio") {
172 $prio = shift @$arg;
173
174 } elsif ($type eq "package") {
175 my $pkg = shift @$arg;
176
177 while (my ($name, $id) = each %cb_id) {
178 if (my $cb = $pkg->can ($name)) {
179 _attach_cb $registry, %undo, $id, $prio, $cb;
180 }
181 }
182
183 } elsif (exists $cb_id{$type}) {
184 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg;
185
186 } elsif (ref $type) {
187 warn "attaching objects not supported, ignoring.\n";
188
189 } else {
190 shift @$arg;
191 warn "attach argument '$type' not supported, ignoring.\n";
192 }
193 }
194
195 \%undo
196}
197
198sub cf::object::attach {
199 die;
200}
201
202sub attach_global {
203 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
204}
205
206sub attach_to_type {
207 my $type = shift;
208 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_
209}
210
211sub attach_to_objects {
212 _attach @CB_OBJECT, KLASS_OBJECT, @_
213}
214
215sub attach_to_players {
216 _attach @CB_PLAYER, KLASS_PLAYER, @_
217}
218
219sub attach_to_maps {
220 _attach @CB_MAP, KLASS_MAP, @_
221}
222
223our $override;
224
225sub override() {
226 $override = 1
227}
228
229sub invoke {
230 my $event = shift;
231 my $callbacks = shift;
232
233 local $override;
234
235 for (@$callbacks) {
236 eval { &{$_->[1]} };
237
238 if ($@) {
239 warn "$@";
240 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
241 override;
242 }
243
244 return 1 if $override;
245 }
246
247 0
248}
249
250#############################################################################
251# old plug-in events
252
95sub inject_event { 253sub inject_event {
96 my $extension = shift; 254 my $extension = shift;
97 my $event_code = shift; 255 my $event_code = shift;
98 256
99 my $cb = $hook[$event_code]{$extension} 257 my $cb = $hook[$event_code]{$extension}
141} 299}
142 300
143sub register { 301sub register {
144 my ($base, $pkg) = @_; 302 my ($base, $pkg) = @_;
145 303
146 for my $idx (0 .. $#EVENT) { 304 for my $idx (0 .. $#PLUGIN_EVENT) {
147 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 305 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
148 #warn "registering $EVENT[$idx] hook to '$pkg'\n"; 306 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
149 $hook[$idx]{$base} = $ref; 307 $hook[$idx]{$base} = $ref;
150 } 308 }
151 } 309 }
152} 310}
153 311
156 314
157 $path =~ /([^\/\\]+)\.ext$/ or die "$path"; 315 $path =~ /([^\/\\]+)\.ext$/ or die "$path";
158 my $base = $1; 316 my $base = $1;
159 my $pkg = $1; 317 my $pkg = $1;
160 $pkg =~ s/[^[:word:]]/_/g; 318 $pkg =~ s/[^[:word:]]/_/g;
161 $pkg = "cf::ext::$pkg"; 319 $pkg = "ext::$pkg";
162 320
163 warn "loading '$path' into '$pkg'\n"; 321 warn "loading '$path' into '$pkg'\n";
164 322
165 open my $fh, "<:utf8", $path 323 open my $fh, "<:utf8", $path
166 or die "$path: $!"; 324 or die "$path: $!";
187 my ($pkg) = @_; 345 my ($pkg) = @_;
188 346
189 warn "removing extension $pkg\n"; 347 warn "removing extension $pkg\n";
190 348
191 # remove hooks 349 # remove hooks
192 for my $idx (0 .. $#EVENT) { 350 for my $idx (0 .. $#PLUGIN_EVENT) {
193 delete $hook[$idx]{$pkg}; 351 delete $hook[$idx]{$pkg};
194 } 352 }
195 353
196 # remove commands 354 # remove commands
197 for my $name (keys %command) { 355 for my $name (keys %command) {
209 # remove extcmds 367 # remove extcmds
210 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 368 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
211 delete $extcmd{$name}; 369 delete $extcmd{$name};
212 } 370 }
213 371
214 if (my $cb = $pkg->can ("on_unload")) { 372 if (my $cb = $pkg->can ("unload")) {
215 eval { 373 eval {
216 $cb->($pkg); 374 $cb->($pkg);
217 1 375 1
218 } or warn "$pkg unloaded, but with errors: $@"; 376 } or warn "$pkg unloaded, but with errors: $@";
219 } 377 }
263 } 421 }
264 422
265 Symbol::delete_package $k; 423 Symbol::delete_package $k;
266 } 424 }
267 425
268 # 4. get rid of ext::, as good as possible 426 # 4. get rid of safe::, as good as possible
269 Symbol::delete_package "ext::$_" 427 Symbol::delete_package "safe::$_"
270 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); 428 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
271 429
272 # 5. remove register_script_function callbacks 430 # 5. remove register_script_function callbacks
273 # TODO 431 # TODO
274 432
275 # 6. unload cf.pm "a bit" 433 # 6. unload cf.pm "a bit"
276 delete $INC{"cf.pm"}; 434 delete $INC{"cf.pm"};
277 435
278 # don't, removes xs symbols, too 436 # don't, removes xs symbols, too,
437 # and global variables created in xs
279 #Symbol::delete_package __PACKAGE__; 438 #Symbol::delete_package __PACKAGE__;
280 439
281 # 7. reload cf.pm 440 # 7. reload cf.pm
282 $msg->("reloading cf.pm"); 441 $msg->("reloading cf.pm");
283 require cf; 442 require cf;
322 481
323############################################################################# 482#############################################################################
324# extcmd framework, basically convert ext <msg> 483# extcmd framework, basically convert ext <msg>
325# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 484# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
326 485
327sub on_extcmd { 486attach_to_players
487 on_extcmd => sub {
328 my ($pl, $buf) = @_; 488 my ($pl, $buf) = @_;
329 489
330 my $msg = eval { from_json $buf }; 490 my $msg = eval { from_json $buf };
331 491
332 if (ref $msg) { 492 if (ref $msg) {
333 if (my $cb = $extcmd{$msg->{msgtype}}) { 493 if (my $cb = $extcmd{$msg->{msgtype}}) {
334 if (my %reply = $cb->[0]->($pl, $msg)) { 494 if (my %reply = $cb->[0]->($pl, $msg)) {
335 $pl->ext_reply ($msg->{msgid}, %reply); 495 $pl->ext_reply ($msg->{msgid}, %reply);
496 }
336 } 497 }
498 } else {
499 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
337 } 500 }
338 } else { 501
339 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 502 cf::override;
340 } 503 },
341 504;
342 1
343}
344 505
345############################################################################# 506#############################################################################
346# load/save/clean perl data associated with a map 507# load/save/clean perl data associated with a map
347 508
348*on_mapclean = sub { 509*cf::mapsupport::on_clean = sub {
349 my ($map) = @_; 510 my ($map) = @_;
350 511
351 my $path = $map->tmpname; 512 my $path = $map->tmpname;
352 defined $path or return; 513 defined $path or return;
353 514
354 unlink "$path.cfperl"; 515 unlink "$path.cfperl";
355}; 516};
356 517
357*on_mapin = 518*cf::mapsupport::on_swapin =
358*on_mapload = sub { 519*cf::mapsupport::on_load = sub {
359 my ($map) = @_; 520 my ($map) = @_;
360 521
361 my $path = $map->tmpname; 522 my $path = $map->tmpname;
362 $path = $map->path unless defined $path; 523 $path = $map->path unless defined $path;
363 524
370 or return; # too new 531 or return; # too new
371 532
372 $map->_set_obs ($data->{obs}); 533 $map->_set_obs ($data->{obs});
373}; 534};
374 535
375*on_mapout = sub { 536*cf::mapsupport::on_swapout = sub {
376 my ($map) = @_; 537 my ($map) = @_;
377 538
378 my $path = $map->tmpname; 539 my $path = $map->tmpname;
379 $path = $map->path unless defined $path; 540 $path = $map->path unless defined $path;
380 541
397 } else { 558 } else {
398 unlink "$path.cfperl"; 559 unlink "$path.cfperl";
399 } 560 }
400}; 561};
401 562
563attach_to_maps prio => -10000, package => cf::mapsupport::;
564
402############################################################################# 565#############################################################################
403# load/save perl data associated with player->ob objects 566# load/save perl data associated with player->ob objects
404 567
405sub all_objects(@) { 568sub all_objects(@) {
406 @_, map all_objects ($_->inv), @_ 569 @_, map all_objects ($_->inv), @_
407} 570}
408 571
409*on_player_load = sub { 572attach_to_players
573 on_load => sub {
410 my ($ob, $path) = @_; 574 my ($pl, $path) = @_;
411 575
412 for my $o (all_objects $ob) { 576 for my $o (all_objects $pl->ob) {
413 if (my $value = $o->get_ob_key_value ("_perl_data")) { 577 if (my $value = $o->get_ob_key_value ("_perl_data")) {
414 $o->set_ob_key_value ("_perl_data"); 578 $o->set_ob_key_value ("_perl_data");
415 579
416 %$o = %{ Storable::thaw pack "H*", $value }; 580 %$o = %{ Storable::thaw pack "H*", $value };
581 }
417 } 582 }
418 } 583 },
419}; 584 on_save => sub {
420
421*on_player_save = sub {
422 my ($ob, $path) = @_; 585 my ($pl, $path) = @_;
423 586
424 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 587 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
425 for grep %$_, all_objects $ob; 588 for grep %$_, all_objects $pl->ob;
426}; 589 },
590;
427 591
428############################################################################# 592#############################################################################
429# core extensions - in perl 593# core extensions - in perl
430 594
431=item cf::player::exists $login 595=item cf::player::exists $login
477} 641}
478 642
479############################################################################# 643#############################################################################
480# map scripting support 644# map scripting support
481 645
482our $safe = new Safe "ext"; 646our $safe = new Safe "safe";
483our $safe_hole = new Safe::Hole; 647our $safe_hole = new Safe::Hole;
484 648
485$SIG{FPE} = 'IGNORE'; 649$SIG{FPE} = 'IGNORE';
486 650
487$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 651$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
493 ["cf::object::player" => qw(player)], 657 ["cf::object::player" => qw(player)],
494 ["cf::player" => qw(peaceful)], 658 ["cf::player" => qw(peaceful)],
495) { 659) {
496 no strict 'refs'; 660 no strict 'refs';
497 my ($pkg, @funs) = @$_; 661 my ($pkg, @funs) = @$_;
498 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 662 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
499 for @funs; 663 for @funs;
500} 664}
501 665
502sub safe_eval($;@) { 666sub safe_eval($;@) {
503 my ($code, %vars) = @_; 667 my ($code, %vars) = @_;
505 my $qcode = $code; 669 my $qcode = $code;
506 $qcode =~ s/"/‟/g; # not allowed in #line filenames 670 $qcode =~ s/"/‟/g; # not allowed in #line filenames
507 $qcode =~ s/\n/\\n/g; 671 $qcode =~ s/\n/\\n/g;
508 672
509 local $_; 673 local $_;
510 local @ext::cf::_safe_eval_args = values %vars; 674 local @safe::cf::_safe_eval_args = values %vars;
511 675
512 $code = 676 my $eval =
513 "do {\n" 677 "do {\n"
514 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 678 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
515 . "#line 0 \"{$qcode}\"\n" 679 . "#line 0 \"{$qcode}\"\n"
516 . $code 680 . $code
517 . "\n}" 681 . "\n}"
518 ; 682 ;
519 683
520 sub_generation_inc; 684 sub_generation_inc;
521 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); 685 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
522 sub_generation_inc; 686 sub_generation_inc;
687
688 if ($@) {
689 warn "$@";
690 warn "while executing safe code '$code'\n";
691 warn "with arguments " . (join " ", %vars) . "\n";
692 }
523 693
524 wantarray ? @res : $res[0] 694 wantarray ? @res : $res[0]
525} 695}
526 696
527sub register_script_function { 697sub register_script_function {
528 my ($fun, $cb) = @_; 698 my ($fun, $cb) = @_;
529 699
530 no strict 'refs'; 700 no strict 'refs';
531 *{"ext::$fun"} = $safe_hole->wrap ($cb); 701 *{"safe::$fun"} = $safe_hole->wrap ($cb);
532} 702}
533 703
534############################################################################# 704#############################################################################
535# the server's main() 705# the server's main()
536 706
537sub run { 707sub main {
538 Event::loop; 708 Event::loop;
539} 709}
540 710
541############################################################################# 711#############################################################################
542# initialisation 712# initialisation

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines