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.22 by root, Wed Jul 19 08:50:42 2006 UTC vs.
Revision 1.30 by elmex, Sat Aug 12 11:51:37 2006 UTC

1package cf; 1package cf;
2 2
3use Symbol; 3use Symbol;
4use List::Util; 4use List::Util;
5use Storable; 5use Storable;
6use Opcode;
7use Safe;
8use Safe::Hole;
6 9
7use Event; 10use Event;
8$Event::Eval = 1; # no idea why this is required, but it is 11$Event::Eval = 1; # no idea why this is required, but it is
9 12
10use strict; 13use strict;
11 14
12our %COMMAND; 15our %COMMAND;
13our @EVENT; 16our @EVENT;
14our %PROP_TYPE; 17our %PROP_TYPE;
15our %PROP_IDX; 18our %PROP_IDX;
19our $LIBDIR = maps_directory "perl";
16 20
17BEGIN { 21BEGIN {
18 @EVENT = map lc, @EVENT; 22 @EVENT = map lc, @EVENT;
19 23
20 *CORE::GLOBAL::warn = sub { 24 *CORE::GLOBAL::warn = sub {
61prop_gen MAP_PROP => "cf::map"; 65prop_gen MAP_PROP => "cf::map";
62prop_gen ARCH_PROP => "cf::arch"; 66prop_gen ARCH_PROP => "cf::arch";
63 67
64# guessed hierarchies 68# guessed hierarchies
65 69
66@cf::object::player::ISA = 'cf::object'; 70@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
67@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 cf::arch)) {
76 no strict 'refs';
77 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
78}
68 79
69$Event::DIED = sub { 80$Event::DIED = sub {
70 warn "error in event callback: @_"; 81 warn "error in event callback: @_";
71}; 82};
72 83
160 171
161 push @exts, $pkg; 172 push @exts, $pkg;
162 $ext_pkg{$base} = $pkg; 173 $ext_pkg{$base} = $pkg;
163 174
164# no strict 'refs'; 175# no strict 'refs';
165# @{"$pkg\::ISA"} = cf::ext::; 176# @{"$pkg\::ISA"} = ext::;
166 177
167 register $base, $pkg; 178 register $base, $pkg;
168} 179}
169 180
170sub unload_extension { 181sub unload_extension {
220 if ($who->flag (FLAG_WIZ)) { 231 if ($who->flag (FLAG_WIZ)) {
221 $who->message ("reloading..."); 232 $who->message ("reloading...");
222 233
223 warn "reloading...\n"; 234 warn "reloading...\n";
224 eval { 235 eval {
236 # 1. cancel all watchers
225 $_->cancel for Event::all_watchers; 237 $_->cancel for Event::all_watchers;
226 238
239 # 2. unload all extensions
240 for (@exts) {
241 $who->message ("unloading <$_>");
227 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"
228 delete $INC{"cf.pm"}; 270 delete $INC{"cf.pm"};
229 271
230 # don't, removes xs symbols, too 272 # don't, removes xs symbols, too
231 #Symbol::delete_package __PACKAGE__; 273 #Symbol::delete_package __PACKAGE__;
232 274
275 # 7. reload cf.pm
276 $who->message ("reloading cf.pm");
233 require cf; 277 require cf;
234 }; 278 };
235 warn $@ if $@; 279 warn $@ if $@;
236 $who->message ($@) if $@; 280 $who->message ($@) if $@;
237 warn "reloaded\n"; 281 warn "reloaded\n";
241 $who->message ("Intruder Alert!"); 285 $who->message ("Intruder Alert!");
242 } 286 }
243}; 287};
244 288
245############################################################################# 289#############################################################################
290# utility functions
291
292use JSON::Syck (); # TODO# replace by JSON::PC once working
293
294sub from_json($) {
295 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
296 JSON::Syck::Load $_[0]
297}
298
299sub to_json($) {
300 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
301 JSON::Syck::Dump $_[0]
302}
303
304#############################################################################
246# extcmd framework, basically convert ext <id> <pkg> arg1 args 305# extcmd framework, basically convert ext <msg>
247# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 306# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
248 307
249sub on_extcmd { 308sub on_extcmd {
250 my ($pl, $buf) = @_; 309 my ($pl, $buf) = @_;
251 310
252 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 311 my $msg = eval { from_json $buf };
253 312
254 $extcmd{$type}[0]->($pl, $buf) 313 if (ref $msg) {
255 if $extcmd{$type}; 314 if (my $cb = $extcmd{$msg->{msgtype}}) {
315 if (my %reply = $cb->[0]->($pl, $msg)) {
316 $pl->ext_reply ($msg->{msgid}, %reply);
317 }
318 }
319 } else {
320 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
321 }
322
323 1
256} 324}
257 325
258############################################################################# 326#############################################################################
259# load/save/clean perl data associated with a map 327# load/save/clean perl data associated with a map
260 328
316# load/save perl data associated with player->ob objects 384# load/save perl data associated with player->ob objects
317 385
318*on_player_load = sub { 386*on_player_load = sub {
319 my ($ob, $path) = @_; 387 my ($ob, $path) = @_;
320 388
321 if (open my $fh, "<:raw", "$path.cfperl") {
322
323 #d##TODO#remove
324
325 my $data = Storable::thaw do { local $/; <$fh> };
326
327 $data->{version} <= 1
328 or return; # too new
329
330 %$ob = %{$data->{ob}};
331 return;
332 }
333
334 for my $o ($ob, $ob->inv) { 389 for my $o ($ob, $ob->inv) {
335 if (my $value = $o->get_ob_key_value ("_perl_data")) { 390 if (my $value = $o->get_ob_key_value ("_perl_data")) {
336 $o->set_ob_key_value ("_perl_data"); 391 $o->set_ob_key_value ("_perl_data");
337 392
338 %$o = %{ Storable::thaw pack "H*", $value }; 393 %$o = %{ Storable::thaw pack "H*", $value };
343*on_player_save = sub { 398*on_player_save = sub {
344 my ($ob, $path) = @_; 399 my ($ob, $path) = @_;
345 400
346 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 401 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
347 for grep %$_, $ob, $ob->inv; 402 for grep %$_, $ob, $ob->inv;
348
349 unlink "$path.cfperl";#d##TODO#remove
350}; 403};
351 404
352############################################################################# 405#############################################################################
353# core extensions - in perl 406# core extensions - in perl
407
408=item cf::player::exists $login
409
410Returns true when the given account exists.
411
412=cut
413
414sub cf::player::exists($) {
415 cf::player::find $_[0]
416 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
417}
418
419=item $player->reply ($npc, $msg[, $flags])
420
421Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
422can be C<undef>. Does the right thing when the player is currently in a
423dialogue with the given NPC character.
424
425=cut
354 426
355# rough implementation of a future "reply" method that works 427# rough implementation of a future "reply" method that works
356# with dialog boxes. 428# with dialog boxes.
357sub cf::object::player::reply($$;$) { 429sub cf::object::player::reply($$$;$) {
358 my ($self, $msg, $flags) = @_; 430 my ($self, $npc, $msg, $flags) = @_;
359 431
360 $flags = cf::NDI_WHITE unless @_ >= 3; 432 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
361 433
434 if ($self->{record_replies}) {
435 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
436 } else {
437 $msg = $npc->name . " says: $msg" if $npc;
362 $self->message ($msg, $flags); 438 $self->message ($msg, $flags);
439 }
440}
441
442=item $player->ext_reply ($msgid, $msgtype, %msg)
443
444Sends an ext reply to the player.
445
446=cut
447
448sub cf::player::ext_reply($$$%) {
449 my ($self, $id, %msg) = @_;
450
451 $msg{msgid} = $id;
452
453 $self->send ("ext " . to_json \%msg);
454}
455
456#############################################################################
457# map scripting support
458
459our $safe = new Safe "ext";
460our $safe_hole = new Safe::Hole;
461
462$SIG{FPE} = 'IGNORE';
463
464$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
465
466# here we export the classes and methods available to script code
467
468for (
469 ["cf::object" => qw(contr pay_amount pay_player)],
470 ["cf::object::player" => qw(player)],
471 ["cf::player" => qw(peaceful)],
472) {
473 no strict 'refs';
474 my ($pkg, @funs) = @$_;
475 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
476 for @funs;
477}
478
479sub safe_eval($;@) {
480 my ($code, %vars) = @_;
481
482 my $qcode = $code;
483 $qcode =~ s/"/‟/g; # not allowed in #line filenames
484 $qcode =~ s/\n/\\n/g;
485
486 local $_;
487 local @ext::cf::_safe_eval_args = values %vars;
488
489 $code =
490 "do {\n"
491 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
492 . "#line 0 \"{$qcode}\"\n"
493 . $code
494 . "\n}"
495 ;
496
497 sub_generation_inc;
498 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
499 sub_generation_inc;
500
501 wantarray ? @res : $res[0]
502}
503
504sub register_script_function {
505 my ($fun, $cb) = @_;
506
507 no strict 'refs';
508 *{"ext::$fun"} = $safe_hole->wrap ($cb);
363} 509}
364 510
365############################################################################# 511#############################################################################
366# initialisation 512# initialisation
367 513
368register "<global>", __PACKAGE__; 514register "<global>", __PACKAGE__;
369 515
370unshift @INC, maps_directory "perl"; 516unshift @INC, $LIBDIR;
371 517
372load_extensions; 518load_extensions;
373 519
3741 5201
375 521

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines