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.19 by root, Sun Jul 16 17:15:39 2006 UTC vs.
Revision 1.24 by root, Wed Jul 19 22:51:40 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;
160 163
161 push @exts, $pkg; 164 push @exts, $pkg;
162 $ext_pkg{$base} = $pkg; 165 $ext_pkg{$base} = $pkg;
163 166
164# no strict 'refs'; 167# no strict 'refs';
165# @{"$pkg\::ISA"} = cf::ext::; 168# @{"$pkg\::ISA"} = ext::;
166 169
167 register $base, $pkg; 170 register $base, $pkg;
168} 171}
169 172
170sub unload_extension { 173sub unload_extension {
171 my ($pkg) = @_; 174 my ($pkg) = @_;
172 175
173 warn "removing extension $pkg\n"; 176 warn "removing extension $pkg\n";
177
178 if (my $cb = $pkg->can ("on_unload")) {
179 $cb->($pkg);
180 }
174 181
175 # remove hooks 182 # remove hooks
176 for my $idx (0 .. $#EVENT) { 183 for my $idx (0 .. $#EVENT) {
177 delete $hook[$idx]{$pkg}; 184 delete $hook[$idx]{$pkg};
178 } 185 }
216 if ($who->flag (FLAG_WIZ)) { 223 if ($who->flag (FLAG_WIZ)) {
217 $who->message ("reloading..."); 224 $who->message ("reloading...");
218 225
219 warn "reloading...\n"; 226 warn "reloading...\n";
220 eval { 227 eval {
228 $_->cancel for Event::all_watchers;
229
221 unload_extension $_ for @exts; 230 unload_extension $_ for @exts;
222 delete $INC{"cf.pm"}; 231 delete $INC{"cf.pm"};
223 232
224 # don't, removes xs symbols, too 233 # don't, removes xs symbols, too
225 #Symbol::delete_package $pkg; 234 #Symbol::delete_package __PACKAGE__;
226 235
227 require cf; 236 require cf;
228 }; 237 };
229 warn $@ if $@; 238 warn $@ if $@;
230 $who->message ($@) if $@; 239 $who->message ($@) if $@;
341 for grep %$_, $ob, $ob->inv; 350 for grep %$_, $ob, $ob->inv;
342 351
343 unlink "$path.cfperl";#d##TODO#remove 352 unlink "$path.cfperl";#d##TODO#remove
344}; 353};
345 354
355#############################################################################
356# core extensions - in perl
357
358=item cf::player::exists $login
359
360Returns true when the given account exists.
361
362=cut
363
364sub cf::player::exists($) {
365 cf::player::find $_[0]
366 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
367}
368
369# rough implementation of a future "reply" method that works
370# with dialog boxes.
371sub cf::object::player::reply($$$;$) {
372 my ($self, $npc, $msg, $flags) = @_;
373
374 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
375
376 if ($self->{record_replies}) {
377 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
378 } else {
379 $msg = $npc->name . " says: $msg" if $npc;
380 $self->message ($msg, $flags);
381 }
382}
383
384#############################################################################
385# map scripting support
386
387our $safe = new Safe "ext";
388our $safe_hole = new Safe::Hole;
389
390$SIG{FPE} = 'IGNORE';
391
392$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
393
394# here we would export the classes and methods available to script code
395#@ext::cf::object::player::ISA = @cf::object::player::ISA;
396#@ext::cf::object::map::ISA = @cf::object::map::ISA;
397
398sub safe_eval($;@) {
399 my ($code, %vars) = @_;
400
401 my $qcode = $code;
402 $qcode =~ s/"/‟/g; # not allowed in #line filenames
403 $qcode =~ s/\n/\\n/g;
404
405 local $_;
406 local @ext::cf::_safe_eval_args = values %vars;
407
408 $safe->reval (
409 "do {\n"
410 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
411 . "#line 0 \"{$qcode}\"\n"
412 . $code
413 . "\n}"
414 )
415}
416
417sub register_script_function {
418 my ($fun, $cb) = @_;
419
420 no strict 'refs';
421 *{"ext::$fun"} = $safe_hole->wrap ($cb);
422}
423
424#############################################################################
425# initialisation
426
346register "<global>", __PACKAGE__; 427register "<global>", __PACKAGE__;
347 428
348unshift @INC, maps_directory "perl"; 429unshift @INC, maps_directory "perl";
349 430
350load_extensions; 431load_extensions;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines