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.31 by elmex, Mon Aug 14 04:22:04 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 cf::arch)) {
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
172 180
173sub unload_extension { 181sub unload_extension {
174 my ($pkg) = @_; 182 my ($pkg) = @_;
175 183
176 warn "removing extension $pkg\n"; 184 warn "removing extension $pkg\n";
177
178 if (my $cb = $pkg->can ("on_unload")) {
179 $cb->($pkg);
180 }
181 185
182 # remove hooks 186 # remove hooks
183 for my $idx (0 .. $#EVENT) { 187 for my $idx (0 .. $#EVENT) {
184 delete $hook[$idx]{$pkg}; 188 delete $hook[$idx]{$pkg};
185 } 189 }
200 # remove extcmds 204 # remove extcmds
201 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 205 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
202 delete $extcmd{$name}; 206 delete $extcmd{$name};
203 } 207 }
204 208
209 if (my $cb = $pkg->can ("on_unload")) {
210 eval {
211 $cb->($pkg);
212 1
213 } or warn "$pkg unloaded, but with errors: $@";
214 }
215
205 Symbol::delete_package $pkg; 216 Symbol::delete_package $pkg;
206} 217}
207 218
208sub load_extensions { 219sub load_extensions {
209 my $LIBDIR = maps_directory "perl"; 220 my $LIBDIR = maps_directory "perl";
223 if ($who->flag (FLAG_WIZ)) { 234 if ($who->flag (FLAG_WIZ)) {
224 $who->message ("reloading..."); 235 $who->message ("reloading...");
225 236
226 warn "reloading...\n"; 237 warn "reloading...\n";
227 eval { 238 eval {
239 # 1. cancel all watchers
228 $_->cancel for Event::all_watchers; 240 $_->cancel for Event::all_watchers;
229 241
242 # 2. unload all extensions
243 for (@exts) {
244 $who->message ("unloading <$_>");
230 unload_extension $_ for @exts; 245 unload_extension $_;
246 }
247
248 # 3. unload all modules loaded from $LIBDIR
249 while (my ($k, $v) = each %INC) {
250 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
251
252 $who->message ("removing <$k>");
253 delete $INC{$k};
254
255 $k =~ s/\.pm$//;
256 $k =~ s/\//::/g;
257
258 if (my $cb = $k->can ("unload_module")) {
259 $cb->();
260 }
261
262 Symbol::delete_package $k;
263 }
264
265 # 4. get rid of ext::, as good as possible
266 Symbol::delete_package "ext::$_"
267 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
268
269 # 5. remove register_script_function callbacks
270 # TODO
271
272 # 6. unload cf.pm "a bit"
231 delete $INC{"cf.pm"}; 273 delete $INC{"cf.pm"};
232 274
233 # don't, removes xs symbols, too 275 # don't, removes xs symbols, too
234 #Symbol::delete_package __PACKAGE__; 276 #Symbol::delete_package __PACKAGE__;
235 277
278 # 7. reload cf.pm
279 $who->message ("reloading cf.pm");
236 require cf; 280 require cf;
237 }; 281 };
238 warn $@ if $@; 282 warn $@ if $@;
239 $who->message ($@) if $@; 283 $who->message ($@) if $@;
240 warn "reloaded\n"; 284 warn "reloaded\n";
244 $who->message ("Intruder Alert!"); 288 $who->message ("Intruder Alert!");
245 } 289 }
246}; 290};
247 291
248############################################################################# 292#############################################################################
293# utility functions
294
295use JSON::Syck (); # TODO# replace by JSON::PC once working
296
297sub from_json($) {
298 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
299 JSON::Syck::Load $_[0]
300}
301
302sub to_json($) {
303 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
304 JSON::Syck::Dump $_[0]
305}
306
307#############################################################################
249# extcmd framework, basically convert ext <id> <pkg> arg1 args 308# extcmd framework, basically convert ext <msg>
250# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 309# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
251 310
252sub on_extcmd { 311sub on_extcmd {
253 my ($pl, $buf) = @_; 312 my ($pl, $buf) = @_;
254 313
255 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 314 my $msg = eval { from_json $buf };
256 315
257 $extcmd{$type}[0]->($pl, $buf) 316 if (ref $msg) {
258 if $extcmd{$type}; 317 if (my $cb = $extcmd{$msg->{msgtype}}) {
318 if (my %reply = $cb->[0]->($pl, $msg)) {
319 $pl->ext_reply ($msg->{msgid}, %reply);
320 }
321 }
322 } else {
323 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
324 }
325
326 1
259} 327}
260 328
261############################################################################# 329#############################################################################
262# load/save/clean perl data associated with a map 330# load/save/clean perl data associated with a map
263 331
319# load/save perl data associated with player->ob objects 387# load/save perl data associated with player->ob objects
320 388
321*on_player_load = sub { 389*on_player_load = sub {
322 my ($ob, $path) = @_; 390 my ($ob, $path) = @_;
323 391
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) { 392 for my $o ($ob, $ob->inv) {
338 if (my $value = $o->get_ob_key_value ("_perl_data")) { 393 if (my $value = $o->get_ob_key_value ("_perl_data")) {
339 $o->set_ob_key_value ("_perl_data"); 394 $o->set_ob_key_value ("_perl_data");
340 395
341 %$o = %{ Storable::thaw pack "H*", $value }; 396 %$o = %{ Storable::thaw pack "H*", $value };
346*on_player_save = sub { 401*on_player_save = sub {
347 my ($ob, $path) = @_; 402 my ($ob, $path) = @_;
348 403
349 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 404 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
350 for grep %$_, $ob, $ob->inv; 405 for grep %$_, $ob, $ob->inv;
351
352 unlink "$path.cfperl";#d##TODO#remove
353}; 406};
354 407
355############################################################################# 408#############################################################################
356# core extensions - in perl 409# core extensions - in perl
357 410
363 416
364sub cf::player::exists($) { 417sub cf::player::exists($) {
365 cf::player::find $_[0] 418 cf::player::find $_[0]
366 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 419 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
367} 420}
421
422=item $player->reply ($npc, $msg[, $flags])
423
424Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
425can be C<undef>. Does the right thing when the player is currently in a
426dialogue with the given NPC character.
427
428=cut
368 429
369# rough implementation of a future "reply" method that works 430# rough implementation of a future "reply" method that works
370# with dialog boxes. 431# with dialog boxes.
371sub cf::object::player::reply($$$;$) { 432sub cf::object::player::reply($$$;$) {
372 my ($self, $npc, $msg, $flags) = @_; 433 my ($self, $npc, $msg, $flags) = @_;
373 434
374 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; 435 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
375 436
437 if ($self->{record_replies}) {
438 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
439 } else {
376 $msg = $npc->name . " says: $msg" if $npc; 440 $msg = $npc->name . " says: $msg" if $npc;
377
378 $self->message ($msg, $flags); 441 $self->message ($msg, $flags);
442 }
443}
444
445=item $player->ext_reply ($msgid, $msgtype, %msg)
446
447Sends an ext reply to the player.
448
449=cut
450
451sub cf::player::ext_reply($$$%) {
452 my ($self, $id, %msg) = @_;
453
454 $msg{msgid} = $id;
455
456 $self->send ("ext " . to_json \%msg);
379} 457}
380 458
381############################################################################# 459#############################################################################
382# map scripting support 460# map scripting support
383 461
386 464
387$SIG{FPE} = 'IGNORE'; 465$SIG{FPE} = 'IGNORE';
388 466
389$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 467$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
390 468
391# here we would export the classes and methods available to script code 469# here we export the classes and methods available to script code
392#@ext::cf::object::player::ISA = @cf::object::player::ISA; 470
393#@ext::cf::object::map::ISA = @cf::object::map::ISA; 471for (
472 ["cf::object" => qw(contr pay_amount pay_player)],
473 ["cf::object::player" => qw(player)],
474 ["cf::player" => qw(peaceful)],
475) {
476 no strict 'refs';
477 my ($pkg, @funs) = @$_;
478 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
479 for @funs;
480}
394 481
395sub safe_eval($;@) { 482sub safe_eval($;@) {
396 my ($code, %vars) = @_; 483 my ($code, %vars) = @_;
397 484
398 my $qcode = $code; 485 my $qcode = $code;
400 $qcode =~ s/\n/\\n/g; 487 $qcode =~ s/\n/\\n/g;
401 488
402 local $_; 489 local $_;
403 local @ext::cf::_safe_eval_args = values %vars; 490 local @ext::cf::_safe_eval_args = values %vars;
404 491
405 $safe->reval ( 492 $code =
406 "do {\n" 493 "do {\n"
407 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 494 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
408 . "#line 0 \"{$qcode}\"\n" 495 . "#line 0 \"{$qcode}\"\n"
409 . $code 496 . $code
410 . "\n}" 497 . "\n}"
411 ) 498 ;
499
500 sub_generation_inc;
501 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
502 sub_generation_inc;
503
504 wantarray ? @res : $res[0]
412} 505}
413 506
414sub register_script_function { 507sub register_script_function {
415 my ($fun, $cb) = @_; 508 my ($fun, $cb) = @_;
416 509
421############################################################################# 514#############################################################################
422# initialisation 515# initialisation
423 516
424register "<global>", __PACKAGE__; 517register "<global>", __PACKAGE__;
425 518
426unshift @INC, maps_directory "perl"; 519unshift @INC, $LIBDIR;
427 520
428load_extensions; 521load_extensions;
429 522
4301 5231
431 524

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines