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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines