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.35 by root, Thu Aug 24 13:35:48 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";
21
22our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER;
24our $NEXT_TICK;
19 25
20BEGIN { 26BEGIN {
21 @EVENT = map lc, @EVENT; 27 @EVENT = map lc, @EVENT;
22 28
23 *CORE::GLOBAL::warn = sub { 29 *CORE::GLOBAL::warn = sub {
64prop_gen MAP_PROP => "cf::map"; 70prop_gen MAP_PROP => "cf::map";
65prop_gen ARCH_PROP => "cf::arch"; 71prop_gen ARCH_PROP => "cf::arch";
66 72
67# guessed hierarchies 73# guessed hierarchies
68 74
69@cf::object::player::ISA = 'cf::object'; 75@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
70@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}
71 84
72$Event::DIED = sub { 85$Event::DIED = sub {
73 warn "error in event callback: @_"; 86 warn "error in event callback: @_";
74}; 87};
75 88
172 185
173sub unload_extension { 186sub unload_extension {
174 my ($pkg) = @_; 187 my ($pkg) = @_;
175 188
176 warn "removing extension $pkg\n"; 189 warn "removing extension $pkg\n";
177
178 if (my $cb = $pkg->can ("on_unload")) {
179 $cb->($pkg);
180 }
181 190
182 # remove hooks 191 # remove hooks
183 for my $idx (0 .. $#EVENT) { 192 for my $idx (0 .. $#EVENT) {
184 delete $hook[$idx]{$pkg}; 193 delete $hook[$idx]{$pkg};
185 } 194 }
200 # remove extcmds 209 # remove extcmds
201 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 210 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
202 delete $extcmd{$name}; 211 delete $extcmd{$name};
203 } 212 }
204 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
205 Symbol::delete_package $pkg; 221 Symbol::delete_package $pkg;
206} 222}
207 223
208sub load_extensions { 224sub load_extensions {
209 my $LIBDIR = maps_directory "perl"; 225 my $LIBDIR = maps_directory "perl";
223 if ($who->flag (FLAG_WIZ)) { 239 if ($who->flag (FLAG_WIZ)) {
224 $who->message ("reloading..."); 240 $who->message ("reloading...");
225 241
226 warn "reloading...\n"; 242 warn "reloading...\n";
227 eval { 243 eval {
244 # 1. cancel all watchers
228 $_->cancel for Event::all_watchers; 245 $_->cancel for Event::all_watchers;
229 246
247 # 2. unload all extensions
248 for (@exts) {
249 $who->message ("unloading <$_>");
230 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"
231 delete $INC{"cf.pm"}; 278 delete $INC{"cf.pm"};
232 279
233 # don't, removes xs symbols, too 280 # don't, removes xs symbols, too
234 #Symbol::delete_package __PACKAGE__; 281 #Symbol::delete_package __PACKAGE__;
235 282
283 # 7. reload cf.pm
284 $who->message ("reloading cf.pm");
236 require cf; 285 require cf;
237 }; 286 };
238 warn $@ if $@; 287 warn $@ if $@;
239 $who->message ($@) if $@; 288 $who->message ($@) if $@;
240 warn "reloaded\n"; 289 warn "reloaded\n";
244 $who->message ("Intruder Alert!"); 293 $who->message ("Intruder Alert!");
245 } 294 }
246}; 295};
247 296
248############################################################################# 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#############################################################################
249# extcmd framework, basically convert ext <id> <pkg> arg1 args 313# extcmd framework, basically convert ext <msg>
250# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 314# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
251 315
252sub on_extcmd { 316sub on_extcmd {
253 my ($pl, $buf) = @_; 317 my ($pl, $buf) = @_;
254 318
255 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 319 my $msg = eval { from_json $buf };
256 320
257 $extcmd{$type}[0]->($pl, $buf) 321 if (ref $msg) {
258 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
259} 332}
260 333
261############################################################################# 334#############################################################################
262# load/save/clean perl data associated with a map 335# load/save/clean perl data associated with a map
263 336
316}; 389};
317 390
318############################################################################# 391#############################################################################
319# load/save perl data associated with player->ob objects 392# load/save perl data associated with player->ob objects
320 393
394sub all_objects(@) {
395 @_, map all_objects ($_->inv), @_
396}
397
321*on_player_load = sub { 398*on_player_load = sub {
322 my ($ob, $path) = @_; 399 my ($ob, $path) = @_;
323 400
324 if (open my $fh, "<:raw", "$path.cfperl") { 401 for my $o (all_objects $ob) {
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) {
338 if (my $value = $o->get_ob_key_value ("_perl_data")) { 402 if (my $value = $o->get_ob_key_value ("_perl_data")) {
339 $o->set_ob_key_value ("_perl_data"); 403 $o->set_ob_key_value ("_perl_data");
340 404
341 %$o = %{ Storable::thaw pack "H*", $value }; 405 %$o = %{ Storable::thaw pack "H*", $value };
342 } 406 }
345 409
346*on_player_save = sub { 410*on_player_save = sub {
347 my ($ob, $path) = @_; 411 my ($ob, $path) = @_;
348 412
349 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 413 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
350 for grep %$_, $ob, $ob->inv; 414 for grep %$_, all_objects $ob;
351
352 unlink "$path.cfperl";#d##TODO#remove
353}; 415};
354 416
355############################################################################# 417#############################################################################
356# core extensions - in perl 418# core extensions - in perl
357 419
363 425
364sub cf::player::exists($) { 426sub cf::player::exists($) {
365 cf::player::find $_[0] 427 cf::player::find $_[0]
366 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 428 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
367} 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
368 438
369# rough implementation of a future "reply" method that works 439# rough implementation of a future "reply" method that works
370# with dialog boxes. 440# with dialog boxes.
371sub cf::object::player::reply($$$;$) { 441sub cf::object::player::reply($$$;$) {
372 my ($self, $npc, $msg, $flags) = @_; 442 my ($self, $npc, $msg, $flags) = @_;
373 443
374 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; 444 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
375 445
446 if ($self->{record_replies}) {
447 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
448 } else {
376 $msg = $npc->name . " says: $msg" if $npc; 449 $msg = $npc->name . " says: $msg" if $npc;
377
378 $self->message ($msg, $flags); 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);
379} 466}
380 467
381############################################################################# 468#############################################################################
382# map scripting support 469# map scripting support
383 470
386 473
387$SIG{FPE} = 'IGNORE'; 474$SIG{FPE} = 'IGNORE';
388 475
389$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 476$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
390 477
391# here we would export the classes and methods available to script code 478# here we export the classes and methods available to script code
392#@ext::cf::object::player::ISA = @cf::object::player::ISA; 479
393#@ext::cf::object::map::ISA = @cf::object::map::ISA; 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}
394 490
395sub safe_eval($;@) { 491sub safe_eval($;@) {
396 my ($code, %vars) = @_; 492 my ($code, %vars) = @_;
397 493
398 my $qcode = $code; 494 my $qcode = $code;
400 $qcode =~ s/\n/\\n/g; 496 $qcode =~ s/\n/\\n/g;
401 497
402 local $_; 498 local $_;
403 local @ext::cf::_safe_eval_args = values %vars; 499 local @ext::cf::_safe_eval_args = values %vars;
404 500
405 $safe->reval ( 501 $code =
406 "do {\n" 502 "do {\n"
407 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 503 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
408 . "#line 0 \"{$qcode}\"\n" 504 . "#line 0 \"{$qcode}\"\n"
409 . $code 505 . $code
410 . "\n}" 506 . "\n}"
411 ) 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]
412} 514}
413 515
414sub register_script_function { 516sub register_script_function {
415 my ($fun, $cb) = @_; 517 my ($fun, $cb) = @_;
416 518
417 no strict 'refs'; 519 no strict 'refs';
418 *{"ext::$fun"} = $safe_hole->wrap ($cb); 520 *{"ext::$fun"} = $safe_hole->wrap ($cb);
419} 521}
420 522
421############################################################################# 523#############################################################################
524# the server's main()
525
526sub run {
527 Event::loop;
528}
529
530#############################################################################
422# initialisation 531# initialisation
423 532
424register "<global>", __PACKAGE__; 533register "<global>", __PACKAGE__;
425 534
426unshift @INC, maps_directory "perl"; 535unshift @INC, $LIBDIR;
427 536
428load_extensions; 537load_extensions;
429 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
4301 5561
431 557

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines