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.32 by root, Tue Aug 15 18:07:25 2006 UTC

5use Storable; 5use Storable;
6use Opcode; 6use Opcode;
7use Safe; 7use Safe;
8use Safe::Hole; 8use Safe::Hole;
9 9
10use Time::HiRes;
10use Event; 11use Event;
11$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
12 13
13use strict; 14use strict;
14 15
15our %COMMAND; 16our %COMMAND;
16our @EVENT; 17our @EVENT;
17our %PROP_TYPE; 18our %PROP_TYPE;
18our %PROP_IDX; 19our %PROP_IDX;
20our $LIBDIR = maps_directory "perl";
19 21
20BEGIN { 22BEGIN {
21 @EVENT = map lc, @EVENT; 23 @EVENT = map lc, @EVENT;
22 24
23 *CORE::GLOBAL::warn = sub { 25 *CORE::GLOBAL::warn = sub {
64prop_gen MAP_PROP => "cf::map"; 66prop_gen MAP_PROP => "cf::map";
65prop_gen ARCH_PROP => "cf::arch"; 67prop_gen ARCH_PROP => "cf::arch";
66 68
67# guessed hierarchies 69# guessed hierarchies
68 70
69@cf::object::player::ISA = 'cf::object'; 71@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
70@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}
71 80
72$Event::DIED = sub { 81$Event::DIED = sub {
73 warn "error in event callback: @_"; 82 warn "error in event callback: @_";
74}; 83};
75 84
172 181
173sub unload_extension { 182sub unload_extension {
174 my ($pkg) = @_; 183 my ($pkg) = @_;
175 184
176 warn "removing extension $pkg\n"; 185 warn "removing extension $pkg\n";
177
178 if (my $cb = $pkg->can ("on_unload")) {
179 $cb->($pkg);
180 }
181 186
182 # remove hooks 187 # remove hooks
183 for my $idx (0 .. $#EVENT) { 188 for my $idx (0 .. $#EVENT) {
184 delete $hook[$idx]{$pkg}; 189 delete $hook[$idx]{$pkg};
185 } 190 }
200 # remove extcmds 205 # remove extcmds
201 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 206 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
202 delete $extcmd{$name}; 207 delete $extcmd{$name};
203 } 208 }
204 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
205 Symbol::delete_package $pkg; 217 Symbol::delete_package $pkg;
206} 218}
207 219
208sub load_extensions { 220sub load_extensions {
209 my $LIBDIR = maps_directory "perl"; 221 my $LIBDIR = maps_directory "perl";
223 if ($who->flag (FLAG_WIZ)) { 235 if ($who->flag (FLAG_WIZ)) {
224 $who->message ("reloading..."); 236 $who->message ("reloading...");
225 237
226 warn "reloading...\n"; 238 warn "reloading...\n";
227 eval { 239 eval {
240 # 1. cancel all watchers
228 $_->cancel for Event::all_watchers; 241 $_->cancel for Event::all_watchers;
229 242
243 # 2. unload all extensions
244 for (@exts) {
245 $who->message ("unloading <$_>");
230 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"
231 delete $INC{"cf.pm"}; 274 delete $INC{"cf.pm"};
232 275
233 # don't, removes xs symbols, too 276 # don't, removes xs symbols, too
234 #Symbol::delete_package __PACKAGE__; 277 #Symbol::delete_package __PACKAGE__;
235 278
279 # 7. reload cf.pm
280 $who->message ("reloading cf.pm");
236 require cf; 281 require cf;
237 }; 282 };
238 warn $@ if $@; 283 warn $@ if $@;
239 $who->message ($@) if $@; 284 $who->message ($@) if $@;
240 warn "reloaded\n"; 285 warn "reloaded\n";
244 $who->message ("Intruder Alert!"); 289 $who->message ("Intruder Alert!");
245 } 290 }
246}; 291};
247 292
248############################################################################# 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#############################################################################
249# extcmd framework, basically convert ext <id> <pkg> arg1 args 309# extcmd framework, basically convert ext <msg>
250# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 310# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
251 311
252sub on_extcmd { 312sub on_extcmd {
253 my ($pl, $buf) = @_; 313 my ($pl, $buf) = @_;
254 314
255 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 315 my $msg = eval { from_json $buf };
256 316
257 $extcmd{$type}[0]->($pl, $buf) 317 if (ref $msg) {
258 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
259} 328}
260 329
261############################################################################# 330#############################################################################
262# load/save/clean perl data associated with a map 331# load/save/clean perl data associated with a map
263 332
319# load/save perl data associated with player->ob objects 388# load/save perl data associated with player->ob objects
320 389
321*on_player_load = sub { 390*on_player_load = sub {
322 my ($ob, $path) = @_; 391 my ($ob, $path) = @_;
323 392
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) { 393 for my $o ($ob, $ob->inv) {
338 if (my $value = $o->get_ob_key_value ("_perl_data")) { 394 if (my $value = $o->get_ob_key_value ("_perl_data")) {
339 $o->set_ob_key_value ("_perl_data"); 395 $o->set_ob_key_value ("_perl_data");
340 396
341 %$o = %{ Storable::thaw pack "H*", $value }; 397 %$o = %{ Storable::thaw pack "H*", $value };
346*on_player_save = sub { 402*on_player_save = sub {
347 my ($ob, $path) = @_; 403 my ($ob, $path) = @_;
348 404
349 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 405 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
350 for grep %$_, $ob, $ob->inv; 406 for grep %$_, $ob, $ob->inv;
351
352 unlink "$path.cfperl";#d##TODO#remove
353}; 407};
354 408
355############################################################################# 409#############################################################################
356# core extensions - in perl 410# core extensions - in perl
411
412my $delta_timer = Event->timer (
413 parked => 1,
414 prio => Event::PRIO_HIGH,
415 cb => sub { Event::unloop (undef) },
416);
417
418sub sleep_delta($) {
419 $delta_timer->at (Event::time + $_[0]);
420 $delta_timer->start;
421 Event::loop;
422}
357 423
358=item cf::player::exists $login 424=item cf::player::exists $login
359 425
360Returns true when the given account exists. 426Returns true when the given account exists.
361 427
363 429
364sub cf::player::exists($) { 430sub cf::player::exists($) {
365 cf::player::find $_[0] 431 cf::player::find $_[0]
366 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 432 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
367} 433}
434
435=item $player->reply ($npc, $msg[, $flags])
436
437Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
438can be C<undef>. Does the right thing when the player is currently in a
439dialogue with the given NPC character.
440
441=cut
368 442
369# rough implementation of a future "reply" method that works 443# rough implementation of a future "reply" method that works
370# with dialog boxes. 444# with dialog boxes.
371sub cf::object::player::reply($$$;$) { 445sub cf::object::player::reply($$$;$) {
372 my ($self, $npc, $msg, $flags) = @_; 446 my ($self, $npc, $msg, $flags) = @_;
373 447
374 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; 448 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
375 449
450 if ($self->{record_replies}) {
451 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
452 } else {
376 $msg = $npc->name . " says: $msg" if $npc; 453 $msg = $npc->name . " says: $msg" if $npc;
377
378 $self->message ($msg, $flags); 454 $self->message ($msg, $flags);
455 }
456}
457
458=item $player->ext_reply ($msgid, $msgtype, %msg)
459
460Sends an ext reply to the player.
461
462=cut
463
464sub cf::player::ext_reply($$$%) {
465 my ($self, $id, %msg) = @_;
466
467 $msg{msgid} = $id;
468
469 $self->send ("ext " . to_json \%msg);
379} 470}
380 471
381############################################################################# 472#############################################################################
382# map scripting support 473# map scripting support
383 474
386 477
387$SIG{FPE} = 'IGNORE'; 478$SIG{FPE} = 'IGNORE';
388 479
389$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 480$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
390 481
391# here we would export the classes and methods available to script code 482# here we export the classes and methods available to script code
392#@ext::cf::object::player::ISA = @cf::object::player::ISA; 483
393#@ext::cf::object::map::ISA = @cf::object::map::ISA; 484for (
485 ["cf::object" => qw(contr pay_amount pay_player)],
486 ["cf::object::player" => qw(player)],
487 ["cf::player" => qw(peaceful)],
488) {
489 no strict 'refs';
490 my ($pkg, @funs) = @$_;
491 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
492 for @funs;
493}
394 494
395sub safe_eval($;@) { 495sub safe_eval($;@) {
396 my ($code, %vars) = @_; 496 my ($code, %vars) = @_;
397 497
398 my $qcode = $code; 498 my $qcode = $code;
400 $qcode =~ s/\n/\\n/g; 500 $qcode =~ s/\n/\\n/g;
401 501
402 local $_; 502 local $_;
403 local @ext::cf::_safe_eval_args = values %vars; 503 local @ext::cf::_safe_eval_args = values %vars;
404 504
405 $safe->reval ( 505 $code =
406 "do {\n" 506 "do {\n"
407 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 507 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
408 . "#line 0 \"{$qcode}\"\n" 508 . "#line 0 \"{$qcode}\"\n"
409 . $code 509 . $code
410 . "\n}" 510 . "\n}"
411 ) 511 ;
512
513 sub_generation_inc;
514 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
515 sub_generation_inc;
516
517 wantarray ? @res : $res[0]
412} 518}
413 519
414sub register_script_function { 520sub register_script_function {
415 my ($fun, $cb) = @_; 521 my ($fun, $cb) = @_;
416 522
421############################################################################# 527#############################################################################
422# initialisation 528# initialisation
423 529
424register "<global>", __PACKAGE__; 530register "<global>", __PACKAGE__;
425 531
426unshift @INC, maps_directory "perl"; 532unshift @INC, $LIBDIR;
427 533
428load_extensions; 534load_extensions;
429 535
4301 5361
431 537

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines