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.23 by root, Wed Jul 19 22:19:19 2006 UTC vs.
Revision 1.27 by root, Fri Jul 21 00:51:39 2006 UTC

14 14
15our %COMMAND; 15our %COMMAND;
16our @EVENT; 16our @EVENT;
17our %PROP_TYPE; 17our %PROP_TYPE;
18our %PROP_IDX; 18our %PROP_IDX;
19our $LIBDIR = maps_directory "perl";
19 20
20BEGIN { 21BEGIN {
21 @EVENT = map lc, @EVENT; 22 @EVENT = map lc, @EVENT;
22 23
23 *CORE::GLOBAL::warn = sub { 24 *CORE::GLOBAL::warn = sub {
64prop_gen MAP_PROP => "cf::map"; 65prop_gen MAP_PROP => "cf::map";
65prop_gen ARCH_PROP => "cf::arch"; 66prop_gen ARCH_PROP => "cf::arch";
66 67
67# guessed hierarchies 68# guessed hierarchies
68 69
69@cf::object::player::ISA = 'cf::object'; 70@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
70@cf::object::map::ISA = 'cf::object'; 71@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
72
73# we bless all objects into derived classes to force a method lookup
74# within the Safe compartment.
75for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) {
76 no strict 'refs';
77 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
78}
71 79
72$Event::DIED = sub { 80$Event::DIED = sub {
73 warn "error in event callback: @_"; 81 warn "error in event callback: @_";
74}; 82};
75 83
223 if ($who->flag (FLAG_WIZ)) { 231 if ($who->flag (FLAG_WIZ)) {
224 $who->message ("reloading..."); 232 $who->message ("reloading...");
225 233
226 warn "reloading...\n"; 234 warn "reloading...\n";
227 eval { 235 eval {
236 # 1. cancel all watchers
228 $_->cancel for Event::all_watchers; 237 $_->cancel for Event::all_watchers;
229 238
239 # 2. unload all extensions
240 for (@exts) {
241 $who->message ("unloading <$_>");
230 unload_extension $_ for @exts; 242 unload_extension $_;
243 }
244
245 # 3. unload all modules loaded from $LIBDIR
246 while (my ($k, $v) = each %INC) {
247 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
248
249 $who->message ("removing <$k>");
250 delete $INC{$k};
251
252 $k =~ s/\.pm$//;
253 $k =~ s/\//::/g;
254
255 if (my $cb = $k->can ("unload_module")) {
256 $cb->();
257 }
258
259 Symbol::delete_package $k;
260 }
261
262 # 4. get rid of ext::, as good as possible
263 Symbol::delete_package "ext::$_"
264 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
265
266 # 5. remove register_script_function callbacks
267 # TODO
268
269 # 6. unload cf.pm "a bit"
231 delete $INC{"cf.pm"}; 270 delete $INC{"cf.pm"};
232 271
233 # don't, removes xs symbols, too 272 # don't, removes xs symbols, too
234 #Symbol::delete_package __PACKAGE__; 273 #Symbol::delete_package __PACKAGE__;
235 274
275 # 7. reload cf.pm
276 $who->message ("reloading cf.pm");
236 require cf; 277 require cf;
237 }; 278 };
238 warn $@ if $@; 279 warn $@ if $@;
239 $who->message ($@) if $@; 280 $who->message ($@) if $@;
240 warn "reloaded\n"; 281 warn "reloaded\n";
319# load/save perl data associated with player->ob objects 360# load/save perl data associated with player->ob objects
320 361
321*on_player_load = sub { 362*on_player_load = sub {
322 my ($ob, $path) = @_; 363 my ($ob, $path) = @_;
323 364
324 if (open my $fh, "<:raw", "$path.cfperl") {
325
326 #d##TODO#remove
327
328 my $data = Storable::thaw do { local $/; <$fh> };
329
330 $data->{version} <= 1
331 or return; # too new
332
333 %$ob = %{$data->{ob}};
334 return;
335 }
336
337 for my $o ($ob, $ob->inv) { 365 for my $o ($ob, $ob->inv) {
338 if (my $value = $o->get_ob_key_value ("_perl_data")) { 366 if (my $value = $o->get_ob_key_value ("_perl_data")) {
339 $o->set_ob_key_value ("_perl_data"); 367 $o->set_ob_key_value ("_perl_data");
340 368
341 %$o = %{ Storable::thaw pack "H*", $value }; 369 %$o = %{ Storable::thaw pack "H*", $value };
346*on_player_save = sub { 374*on_player_save = sub {
347 my ($ob, $path) = @_; 375 my ($ob, $path) = @_;
348 376
349 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 377 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
350 for grep %$_, $ob, $ob->inv; 378 for grep %$_, $ob, $ob->inv;
351
352 unlink "$path.cfperl";#d##TODO#remove
353}; 379};
354 380
355############################################################################# 381#############################################################################
356# core extensions - in perl 382# core extensions - in perl
357 383
371sub cf::object::player::reply($$$;$) { 397sub cf::object::player::reply($$$;$) {
372 my ($self, $npc, $msg, $flags) = @_; 398 my ($self, $npc, $msg, $flags) = @_;
373 399
374 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; 400 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
375 401
402 if ($self->{record_replies}) {
403 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
404 } else {
376 $msg = $npc->name . " says: $msg" if $npc; 405 $msg = $npc->name . " says: $msg" if $npc;
377
378 $self->message ($msg, $flags); 406 $self->message ($msg, $flags);
407 }
379} 408}
380 409
381############################################################################# 410#############################################################################
382# map scripting support 411# map scripting support
383 412
386 415
387$SIG{FPE} = 'IGNORE'; 416$SIG{FPE} = 'IGNORE';
388 417
389$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 418$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
390 419
391# here we would export the classes and methods available to script code 420# here we export the classes and methods available to script code
392#@ext::cf::object::player::ISA = @cf::object::player::ISA; 421
393#@ext::cf::object::map::ISA = @cf::object::map::ISA; 422for (
423 ["cf::object" => qw(contr pay_amount pay_player)],
424 ["cf::object::player" => qw(player)],
425 ["cf::player" => qw(peaceful)],
426) {
427 no strict 'refs';
428 my ($pkg, @funs) = @$_;
429 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
430 for @funs;
431}
394 432
395sub safe_eval($;@) { 433sub safe_eval($;@) {
396 my ($code, %vars) = @_; 434 my ($code, %vars) = @_;
397 435
398 my $qcode = $code; 436 my $qcode = $code;
400 $qcode =~ s/\n/\\n/g; 438 $qcode =~ s/\n/\\n/g;
401 439
402 local $_; 440 local $_;
403 local @ext::cf::_safe_eval_args = values %vars; 441 local @ext::cf::_safe_eval_args = values %vars;
404 442
405 $safe->reval ( 443 $code =
406 "do {\n" 444 "do {\n"
407 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 445 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
408 . "#line 0 \"{$qcode}\"\n" 446 . "#line 0 \"{$qcode}\"\n"
409 . $code 447 . $code
410 . "\n}" 448 . "\n}"
411 ) 449 ;
450
451 sub_generation_inc;
452 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
453 sub_generation_inc;
454
455 wantarray ? @res : $res[0]
412} 456}
413 457
414sub register_script_function { 458sub register_script_function {
415 my ($fun, $cb) = @_; 459 my ($fun, $cb) = @_;
416 460
421############################################################################# 465#############################################################################
422# initialisation 466# initialisation
423 467
424register "<global>", __PACKAGE__; 468register "<global>", __PACKAGE__;
425 469
426unshift @INC, maps_directory "perl"; 470unshift @INC, $LIBDIR;
427 471
428load_extensions; 472load_extensions;
429 473
4301 4741
431 475

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines