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.36 by root, Thu Aug 24 14:04:29 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";
208 1 231 1
209 } or warn "$ext not loaded: $@"; 232 } or warn "$ext not loaded: $@";
210 } 233 }
211} 234}
212 235
236sub _perl_reload(&) {
237 my ($msg) = @_;
238
239 $msg->("reloading...");
240
241 eval {
242 # 1. cancel all watchers
243 $_->cancel for Event::all_watchers;
244
245 # 2. unload all extensions
246 for (@exts) {
247 $msg->("unloading <$_>");
248 unload_extension $_;
249 }
250
251 # 3. unload all modules loaded from $LIBDIR
252 while (my ($k, $v) = each %INC) {
253 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
254
255 $msg->("removing <$k>");
256 delete $INC{$k};
257
258 $k =~ s/\.pm$//;
259 $k =~ s/\//::/g;
260
261 if (my $cb = $k->can ("unload_module")) {
262 $cb->();
263 }
264
265 Symbol::delete_package $k;
266 }
267
268 # 4. get rid of ext::, as good as possible
269 Symbol::delete_package "ext::$_"
270 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
271
272 # 5. remove register_script_function callbacks
273 # TODO
274
275 # 6. unload cf.pm "a bit"
276 delete $INC{"cf.pm"};
277
278 # don't, removes xs symbols, too
279 #Symbol::delete_package __PACKAGE__;
280
281 # 7. reload cf.pm
282 $msg->("reloading cf.pm");
283 require cf;
284 };
285 $msg->($@) if $@;
286
287 $msg->("reloaded");
288};
289
290sub perl_reload() {
291 _perl_reload {
292 warn $_[0];
293 print "$_[0]\n";
294 };
295}
296
213register_command "perl-reload", 0, sub { 297register_command "perl-reload", 0, sub {
214 my ($who, $arg) = @_; 298 my ($who, $arg) = @_;
215 299
216 if ($who->flag (FLAG_WIZ)) { 300 if ($who->flag (FLAG_WIZ)) {
217 $who->message ("reloading..."); 301 _perl_reload {
218 302 warn $_[0];
219 warn "reloading...\n"; 303 $who->message ($_[0]);
220 eval {
221 $_->cancel for Event::all_watchers;
222
223 unload_extension $_ for @exts;
224 delete $INC{"cf.pm"};
225
226 # don't, removes xs symbols, too
227 #Symbol::delete_package $pkg;
228
229 require cf;
230 }; 304 };
231 warn $@ if $@;
232 $who->message ($@) if $@;
233 warn "reloaded\n";
234
235 $who->message ("reloaded");
236 } else {
237 $who->message ("Intruder Alert!");
238 } 305 }
239}; 306};
240 307
241############################################################################# 308#############################################################################
309# utility functions
310
311use JSON::Syck (); # TODO# replace by JSON::PC once working
312
313sub from_json($) {
314 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
315 JSON::Syck::Load $_[0]
316}
317
318sub to_json($) {
319 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
320 JSON::Syck::Dump $_[0]
321}
322
323#############################################################################
242# extcmd framework, basically convert ext <id> <pkg> arg1 args 324# extcmd framework, basically convert ext <msg>
243# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 325# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
244 326
245sub on_extcmd { 327sub on_extcmd {
246 my ($pl, $buf) = @_; 328 my ($pl, $buf) = @_;
247 329
248 my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; 330 my $msg = eval { from_json $buf };
249 331
250 $extcmd{$type}[0]->($pl, $buf) 332 if (ref $msg) {
251 if $extcmd{$type}; 333 if (my $cb = $extcmd{$msg->{msgtype}}) {
334 if (my %reply = $cb->[0]->($pl, $msg)) {
335 $pl->ext_reply ($msg->{msgid}, %reply);
336 }
337 }
338 } else {
339 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
340 }
341
342 1
252} 343}
253 344
254############################################################################# 345#############################################################################
255# load/save/clean perl data associated with a map 346# load/save/clean perl data associated with a map
256 347
309}; 400};
310 401
311############################################################################# 402#############################################################################
312# load/save perl data associated with player->ob objects 403# load/save perl data associated with player->ob objects
313 404
405sub all_objects(@) {
406 @_, map all_objects ($_->inv), @_
407}
408
314*on_player_load = sub { 409*on_player_load = sub {
315 my ($ob, $path) = @_; 410 my ($ob, $path) = @_;
316 411
317 if (open my $fh, "<:raw", "$path.cfperl") { 412 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")) { 413 if (my $value = $o->get_ob_key_value ("_perl_data")) {
332 $o->set_ob_key_value ("_perl_data"); 414 $o->set_ob_key_value ("_perl_data");
333 415
334 %$o = %{ Storable::thaw pack "H*", $value }; 416 %$o = %{ Storable::thaw pack "H*", $value };
335 } 417 }
338 420
339*on_player_save = sub { 421*on_player_save = sub {
340 my ($ob, $path) = @_; 422 my ($ob, $path) = @_;
341 423
342 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 424 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
343 for grep %$_, $ob, $ob->inv; 425 for grep %$_, all_objects $ob;
344
345 unlink "$path.cfperl";#d##TODO#remove
346}; 426};
427
428#############################################################################
429# core extensions - in perl
430
431=item cf::player::exists $login
432
433Returns true when the given account exists.
434
435=cut
436
437sub cf::player::exists($) {
438 cf::player::find $_[0]
439 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
440}
441
442=item $player->reply ($npc, $msg[, $flags])
443
444Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
445can be C<undef>. Does the right thing when the player is currently in a
446dialogue with the given NPC character.
447
448=cut
449
450# rough implementation of a future "reply" method that works
451# with dialog boxes.
452sub cf::object::player::reply($$$;$) {
453 my ($self, $npc, $msg, $flags) = @_;
454
455 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
456
457 if ($self->{record_replies}) {
458 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
459 } else {
460 $msg = $npc->name . " says: $msg" if $npc;
461 $self->message ($msg, $flags);
462 }
463}
464
465=item $player->ext_reply ($msgid, $msgtype, %msg)
466
467Sends an ext reply to the player.
468
469=cut
470
471sub cf::player::ext_reply($$$%) {
472 my ($self, $id, %msg) = @_;
473
474 $msg{msgid} = $id;
475
476 $self->send ("ext " . to_json \%msg);
477}
478
479#############################################################################
480# map scripting support
481
482our $safe = new Safe "ext";
483our $safe_hole = new Safe::Hole;
484
485$SIG{FPE} = 'IGNORE';
486
487$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
488
489# here we export the classes and methods available to script code
490
491for (
492 ["cf::object" => qw(contr pay_amount pay_player)],
493 ["cf::object::player" => qw(player)],
494 ["cf::player" => qw(peaceful)],
495) {
496 no strict 'refs';
497 my ($pkg, @funs) = @$_;
498 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
499 for @funs;
500}
501
502sub safe_eval($;@) {
503 my ($code, %vars) = @_;
504
505 my $qcode = $code;
506 $qcode =~ s/"/‟/g; # not allowed in #line filenames
507 $qcode =~ s/\n/\\n/g;
508
509 local $_;
510 local @ext::cf::_safe_eval_args = values %vars;
511
512 $code =
513 "do {\n"
514 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
515 . "#line 0 \"{$qcode}\"\n"
516 . $code
517 . "\n}"
518 ;
519
520 sub_generation_inc;
521 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
522 sub_generation_inc;
523
524 wantarray ? @res : $res[0]
525}
526
527sub register_script_function {
528 my ($fun, $cb) = @_;
529
530 no strict 'refs';
531 *{"ext::$fun"} = $safe_hole->wrap ($cb);
532}
533
534#############################################################################
535# the server's main()
536
537sub run {
538 Event::loop;
539}
540
541#############################################################################
542# initialisation
347 543
348register "<global>", __PACKAGE__; 544register "<global>", __PACKAGE__;
349 545
350unshift @INC, maps_directory "perl"; 546unshift @INC, $LIBDIR;
351 547
352load_extensions; 548load_extensions;
353 549
550$TICK_WATCHER = Event->timer (
551 prio => 1,
552 at => $NEXT_TICK || 1,
553 cb => sub {
554 cf::server_tick; # one server iteration
555
556 my $NOW = Event::time;
557 $NEXT_TICK += $TICK;
558
559 # if we are delayed by > 0.25 second, skip ticks
560 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25;
561
562 $TICK_WATCHER->at ($NEXT_TICK);
563 $TICK_WATCHER->start;
564 },
565);
566
3541 5671
355 568

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines