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.16 by root, Mon Jun 12 13:25:27 2006 UTC vs.
Revision 1.33 by root, Mon Aug 21 01:51:23 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;
9
10use Time::HiRes;
11use Event;
12$Event::Eval = 1; # no idea why this is required, but it is
6 13
7use strict; 14use strict;
8 15
9our %COMMAND; 16our %COMMAND;
10our @EVENT; 17our @EVENT;
11our %PROP_TYPE; 18our %PROP_TYPE;
12our %PROP_IDX; 19our %PROP_IDX;
20our $LIBDIR = maps_directory "perl";
13 21
14BEGIN { 22BEGIN {
15 @EVENT = map lc, @EVENT; 23 @EVENT = map lc, @EVENT;
16 24
17 *CORE::GLOBAL::warn = sub { 25 *CORE::GLOBAL::warn = sub {
58prop_gen MAP_PROP => "cf::map"; 66prop_gen MAP_PROP => "cf::map";
59prop_gen ARCH_PROP => "cf::arch"; 67prop_gen ARCH_PROP => "cf::arch";
60 68
61# guessed hierarchies 69# guessed hierarchies
62 70
63@cf::object::player::ISA = 'cf::object'; 71@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
64@cf::object::map::ISA = 'cf::object'; 72@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
73
74# we bless all objects into derived classes to force a method lookup
75# within the Safe compartment.
76for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) {
77 no strict 'refs';
78 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
79}
80
81$Event::DIED = sub {
82 warn "error in event callback: @_";
83};
65 84
66my %ext_pkg; 85my %ext_pkg;
67my @exts; 86my @exts;
68my @hook; 87my @hook;
69my %command; 88my %command;
153 172
154 push @exts, $pkg; 173 push @exts, $pkg;
155 $ext_pkg{$base} = $pkg; 174 $ext_pkg{$base} = $pkg;
156 175
157# no strict 'refs'; 176# no strict 'refs';
158# @{"$pkg\::ISA"} = cf::ext::; 177# @{"$pkg\::ISA"} = ext::;
159 178
160 register $base, $pkg; 179 register $base, $pkg;
161} 180}
162 181
163sub unload_extension { 182sub unload_extension {
186 # remove extcmds 205 # remove extcmds
187 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 206 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
188 delete $extcmd{$name}; 207 delete $extcmd{$name};
189 } 208 }
190 209
210 if (my $cb = $pkg->can ("on_unload")) {
211 eval {
212 $cb->($pkg);
213 1
214 } or warn "$pkg unloaded, but with errors: $@";
215 }
216
191 Symbol::delete_package $pkg; 217 Symbol::delete_package $pkg;
192} 218}
193 219
194sub load_extensions { 220sub load_extensions {
195 my $LIBDIR = maps_directory "perl"; 221 my $LIBDIR = maps_directory "perl";
209 if ($who->flag (FLAG_WIZ)) { 235 if ($who->flag (FLAG_WIZ)) {
210 $who->message ("reloading..."); 236 $who->message ("reloading...");
211 237
212 warn "reloading...\n"; 238 warn "reloading...\n";
213 eval { 239 eval {
240 # 1. cancel all watchers
241 $_->cancel for Event::all_watchers;
242
243 # 2. unload all extensions
244 for (@exts) {
245 $who->message ("unloading <$_>");
214 unload_extension $_ for @exts; 246 unload_extension $_;
247 }
248
249 # 3. unload all modules loaded from $LIBDIR
250 while (my ($k, $v) = each %INC) {
251 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
252
253 $who->message ("removing <$k>");
254 delete $INC{$k};
255
256 $k =~ s/\.pm$//;
257 $k =~ s/\//::/g;
258
259 if (my $cb = $k->can ("unload_module")) {
260 $cb->();
261 }
262
263 Symbol::delete_package $k;
264 }
265
266 # 4. get rid of ext::, as good as possible
267 Symbol::delete_package "ext::$_"
268 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
269
270 # 5. remove register_script_function callbacks
271 # TODO
272
273 # 6. unload cf.pm "a bit"
215 delete $INC{"cf.pm"}; 274 delete $INC{"cf.pm"};
216 275
217 # don't, removes xs symbols, too 276 # don't, removes xs symbols, too
218 #Symbol::delete_package $pkg; 277 #Symbol::delete_package __PACKAGE__;
219 278
279 # 7. reload cf.pm
280 $who->message ("reloading cf.pm");
220 require cf; 281 require cf;
221 }; 282 };
222 warn $@ if $@; 283 warn $@ if $@;
223 $who->message ($@) if $@; 284 $who->message ($@) if $@;
224 warn "reloaded\n"; 285 warn "reloaded\n";
228 $who->message ("Intruder Alert!"); 289 $who->message ("Intruder Alert!");
229 } 290 }
230}; 291};
231 292
232############################################################################# 293#############################################################################
294# utility functions
295
296use JSON::Syck (); # TODO# replace by JSON::PC once working
297
298sub from_json($) {
299 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
300 JSON::Syck::Load $_[0]
301}
302
303sub to_json($) {
304 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
305 JSON::Syck::Dump $_[0]
306}
307
308#############################################################################
233# extcmd framework, basically convert ext <id> <pkg> arg1 args 309# extcmd framework, basically convert ext <msg>
234# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 310# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
235 311
236sub on_extcmd { 312sub on_extcmd {
237 my ($pl, $buf) = @_; 313 my ($pl, $buf) = @_;
238 314
239 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 315 my $msg = eval { from_json $buf };
240 316
241 $extcmd{$type}[0]->($pl, $buf) 317 if (ref $msg) {
242 if $extcmd{$type}; 318 if (my $cb = $extcmd{$msg->{msgtype}}) {
319 if (my %reply = $cb->[0]->($pl, $msg)) {
320 $pl->ext_reply ($msg->{msgid}, %reply);
321 }
322 }
323 } else {
324 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
325 }
326
327 1
243} 328}
244 329
245############################################################################# 330#############################################################################
246# load/save/clean perl data associated with a map 331# load/save/clean perl data associated with a map
247 332
300}; 385};
301 386
302############################################################################# 387#############################################################################
303# load/save perl data associated with player->ob objects 388# load/save perl data associated with player->ob objects
304 389
390sub all_objects(@) {
391 @_, map all_objects ($_->inv), @_
392}
393
305*on_player_load = sub { 394*on_player_load = sub {
306 my ($ob, $path) = @_; 395 my ($ob, $path) = @_;
307 396
308 if (open my $fh, "<:raw", "$path.cfperl") { 397 for my $o (all_objects $ob) {
309
310 #d##TODO#remove
311
312 my $data = Storable::thaw do { local $/; <$fh> };
313
314 $data->{version} <= 1
315 or return; # too new
316
317 %$ob = %{$data->{ob}};
318 return;
319 }
320
321 for my $o ($ob, $ob->inv) {
322 if (my $value = $o->get_ob_key_value ("_perl_data")) { 398 if (my $value = $o->get_ob_key_value ("_perl_data")) {
323 $o->set_ob_key_value ("_perl_data"); 399 $o->set_ob_key_value ("_perl_data");
324 400
325 %$o = %{ Storable::thaw pack "H*", $value }; 401 %$o = %{ Storable::thaw pack "H*", $value };
326 } 402 }
329 405
330*on_player_save = sub { 406*on_player_save = sub {
331 my ($ob, $path) = @_; 407 my ($ob, $path) = @_;
332 408
333 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 409 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
334 for grep %$_, $ob, $ob->inv; 410 for grep %$_, all_objects $ob;
335
336 unlink "$path.cfperl";#d##TODO#remove
337}; 411};
412
413#############################################################################
414# core extensions - in perl
415
416my $delta_timer = Event->timer (
417 parked => 1,
418 prio => Event::PRIO_HIGH,
419 cb => sub { Event::unloop (undef) },
420);
421
422sub sleep_delta($) {
423 $delta_timer->at (Event::time + $_[0]);
424 $delta_timer->start;
425 Event::loop;
426}
427
428=item cf::player::exists $login
429
430Returns true when the given account exists.
431
432=cut
433
434sub cf::player::exists($) {
435 cf::player::find $_[0]
436 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
437}
438
439=item $player->reply ($npc, $msg[, $flags])
440
441Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
442can be C<undef>. Does the right thing when the player is currently in a
443dialogue with the given NPC character.
444
445=cut
446
447# rough implementation of a future "reply" method that works
448# with dialog boxes.
449sub cf::object::player::reply($$$;$) {
450 my ($self, $npc, $msg, $flags) = @_;
451
452 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
453
454 if ($self->{record_replies}) {
455 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
456 } else {
457 $msg = $npc->name . " says: $msg" if $npc;
458 $self->message ($msg, $flags);
459 }
460}
461
462=item $player->ext_reply ($msgid, $msgtype, %msg)
463
464Sends an ext reply to the player.
465
466=cut
467
468sub cf::player::ext_reply($$$%) {
469 my ($self, $id, %msg) = @_;
470
471 $msg{msgid} = $id;
472
473 $self->send ("ext " . to_json \%msg);
474}
475
476#############################################################################
477# map scripting support
478
479our $safe = new Safe "ext";
480our $safe_hole = new Safe::Hole;
481
482$SIG{FPE} = 'IGNORE';
483
484$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
485
486# here we export the classes and methods available to script code
487
488for (
489 ["cf::object" => qw(contr pay_amount pay_player)],
490 ["cf::object::player" => qw(player)],
491 ["cf::player" => qw(peaceful)],
492) {
493 no strict 'refs';
494 my ($pkg, @funs) = @$_;
495 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
496 for @funs;
497}
498
499sub safe_eval($;@) {
500 my ($code, %vars) = @_;
501
502 my $qcode = $code;
503 $qcode =~ s/"/‟/g; # not allowed in #line filenames
504 $qcode =~ s/\n/\\n/g;
505
506 local $_;
507 local @ext::cf::_safe_eval_args = values %vars;
508
509 $code =
510 "do {\n"
511 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
512 . "#line 0 \"{$qcode}\"\n"
513 . $code
514 . "\n}"
515 ;
516
517 sub_generation_inc;
518 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
519 sub_generation_inc;
520
521 wantarray ? @res : $res[0]
522}
523
524sub register_script_function {
525 my ($fun, $cb) = @_;
526
527 no strict 'refs';
528 *{"ext::$fun"} = $safe_hole->wrap ($cb);
529}
530
531#############################################################################
532# initialisation
338 533
339register "<global>", __PACKAGE__; 534register "<global>", __PACKAGE__;
340 535
536unshift @INC, $LIBDIR;
537
341load_extensions; 538load_extensions;
342 539
3431 5401
344 541

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines