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.9 by root, Fri Mar 17 03:22:00 2006 UTC vs.
Revision 1.32 by root, Tue Aug 15 18:07:25 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;
9
10use Time::HiRes;
11use Event;
12$Event::Eval = 1; # no idea why this is required, but it is
6 13
7use strict; 14use strict;
8 15
9our %COMMAND; 16our %COMMAND;
10our @EVENT; 17our @EVENT;
11our %PROP_TYPE; 18our %PROP_TYPE;
12our %PROP_IDX; 19our %PROP_IDX;
20our $LIBDIR = maps_directory "perl";
13 21
14BEGIN { 22BEGIN {
15 @EVENT = map lc, @EVENT; 23 @EVENT = map lc, @EVENT;
16 24
17 *CORE::GLOBAL::warn = sub { 25 *CORE::GLOBAL::warn = sub {
58prop_gen MAP_PROP => "cf::map"; 66prop_gen MAP_PROP => "cf::map";
59prop_gen ARCH_PROP => "cf::arch"; 67prop_gen ARCH_PROP => "cf::arch";
60 68
61# guessed hierarchies 69# guessed hierarchies
62 70
63@cf::object::player::ISA = 'cf::object'; 71@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
64@cf::object::map::ISA = 'cf::object'; 72@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
73
74# we bless all objects into derived classes to force a method lookup
75# within the Safe compartment.
76for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) {
77 no strict 'refs';
78 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
79}
80
81$Event::DIED = sub {
82 warn "error in event callback: @_";
83};
65 84
66my %ext_pkg; 85my %ext_pkg;
67my @exts; 86my @exts;
68my @hook; 87my @hook;
69my %command; 88my %command;
89my %extcmd;
70 90
71sub inject_event { 91sub inject_event {
72 my ($data) = @_; 92 my $extension = shift;
93 my $event_code = shift;
73 94
74 my $cb = $hook[$data->{event_code}]{$data->{extension}} 95 my $cb = $hook[$event_code]{$extension}
75 or return; 96 or return;
76 97
77 $cb->($data) 98 &$cb
78} 99}
79 100
80sub inject_global_event { 101sub inject_global_event {
81 my ($data) = @_; 102 my $event = shift;
82 103
83 my $cb = $hook[$data->{event_code}] 104 my $cb = $hook[$event]
84 or return; 105 or return;
85 106
86 $_->($data) for values %$cb; 107 List::Util::max map &$_, values %$cb
87
88 ()
89} 108}
90 109
91sub inject_command { 110sub inject_command {
92 my ($name, $obj, $params) = @_; 111 my ($name, $obj, $params) = @_;
93 112
100 119
101sub register_command { 120sub register_command {
102 my ($name, $time, $cb) = @_; 121 my ($name, $time, $cb) = @_;
103 122
104 my $caller = caller; 123 my $caller = caller;
105
106 warn "registering command '$name/$time' to '$caller'"; 124 #warn "registering command '$name/$time' to '$caller'";
107 125
108 push @{ $command{$name} }, [$time, $cb, $caller]; 126 push @{ $command{$name} }, [$time, $cb, $caller];
109 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 127 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
110} 128}
111 129
130sub register_extcmd {
131 my ($name, $cb) = @_;
132
133 my $caller = caller;
134 #warn "registering extcmd '$name' to '$caller'";
135
136 $extcmd{$name} = [$cb, $caller];
137}
138
112sub register { 139sub register {
113 my ($base, $pkg) = @_; 140 my ($base, $pkg) = @_;
114 141
115 for my $idx (0 .. $#EVENT) { 142 for my $idx (0 .. $#EVENT) {
116 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 143 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
117 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 144 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
118 $hook[$idx]{$base} = $ref; 145 $hook[$idx]{$base} = $ref;
119 } 146 }
120 } 147 }
121} 148}
122 149
145 172
146 push @exts, $pkg; 173 push @exts, $pkg;
147 $ext_pkg{$base} = $pkg; 174 $ext_pkg{$base} = $pkg;
148 175
149# no strict 'refs'; 176# no strict 'refs';
150# @{"$pkg\::ISA"} = cf::ext::; 177# @{"$pkg\::ISA"} = ext::;
151 178
152 register $base, $pkg; 179 register $base, $pkg;
153} 180}
154 181
155sub unload_extension { 182sub unload_extension {
173 delete $command{$name}; 200 delete $command{$name};
174 delete $COMMAND{"$name\000"}; 201 delete $COMMAND{"$name\000"};
175 } 202 }
176 } 203 }
177 204
205 # remove extcmds
206 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
207 delete $extcmd{$name};
208 }
209
210 if (my $cb = $pkg->can ("on_unload")) {
211 eval {
212 $cb->($pkg);
213 1
214 } or warn "$pkg unloaded, but with errors: $@";
215 }
216
178 Symbol::delete_package $pkg; 217 Symbol::delete_package $pkg;
179} 218}
180 219
181sub load_extensions { 220sub load_extensions {
182 my $LIBDIR = maps_directory "perl"; 221 my $LIBDIR = maps_directory "perl";
196 if ($who->flag (FLAG_WIZ)) { 235 if ($who->flag (FLAG_WIZ)) {
197 $who->message ("reloading..."); 236 $who->message ("reloading...");
198 237
199 warn "reloading...\n"; 238 warn "reloading...\n";
200 eval { 239 eval {
240 # 1. cancel all watchers
241 $_->cancel for Event::all_watchers;
242
243 # 2. unload all extensions
244 for (@exts) {
245 $who->message ("unloading <$_>");
201 unload_extension $_ for @exts; 246 unload_extension $_;
247 }
248
249 # 3. unload all modules loaded from $LIBDIR
250 while (my ($k, $v) = each %INC) {
251 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
252
253 $who->message ("removing <$k>");
254 delete $INC{$k};
255
256 $k =~ s/\.pm$//;
257 $k =~ s/\//::/g;
258
259 if (my $cb = $k->can ("unload_module")) {
260 $cb->();
261 }
262
263 Symbol::delete_package $k;
264 }
265
266 # 4. get rid of ext::, as good as possible
267 Symbol::delete_package "ext::$_"
268 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
269
270 # 5. remove register_script_function callbacks
271 # TODO
272
273 # 6. unload cf.pm "a bit"
202 delete $INC{"cf.pm"}; 274 delete $INC{"cf.pm"};
203 275
204 # don't, removes xs symbols, too 276 # don't, removes xs symbols, too
205 #Symbol::delete_package $pkg; 277 #Symbol::delete_package __PACKAGE__;
206 278
279 # 7. reload cf.pm
280 $who->message ("reloading cf.pm");
207 require cf; 281 require cf;
208 }; 282 };
209 warn $@ if $@; 283 warn $@ if $@;
210 $who->message ($@) if $@; 284 $who->message ($@) if $@;
211 warn "reloaded\n"; 285 warn "reloaded\n";
215 $who->message ("Intruder Alert!"); 289 $who->message ("Intruder Alert!");
216 } 290 }
217}; 291};
218 292
219############################################################################# 293#############################################################################
294# utility functions
295
296use JSON::Syck (); # TODO# replace by JSON::PC once working
297
298sub from_json($) {
299 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
300 JSON::Syck::Load $_[0]
301}
302
303sub to_json($) {
304 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
305 JSON::Syck::Dump $_[0]
306}
307
308#############################################################################
309# extcmd framework, basically convert ext <msg>
310# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
311
312sub on_extcmd {
313 my ($pl, $buf) = @_;
314
315 my $msg = eval { from_json $buf };
316
317 if (ref $msg) {
318 if (my $cb = $extcmd{$msg->{msgtype}}) {
319 if (my %reply = $cb->[0]->($pl, $msg)) {
320 $pl->ext_reply ($msg->{msgid}, %reply);
321 }
322 }
323 } else {
324 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
325 }
326
327 1
328}
329
330#############################################################################
220# load/save/clean perl data associated with a map 331# load/save/clean perl data associated with a map
221 332
222*on_mapclean = sub { 333*on_mapclean = sub {
223 my $map = shift->{map}; 334 my ($map) = @_;
224 335
225 my $path = $map->tmpname; 336 my $path = $map->tmpname;
226 defined $path or return; 337 defined $path or return;
227 338
228 unlink "$path.cfperl"; 339 unlink "$path.cfperl";
229}; 340};
230 341
231*on_mapin = 342*on_mapin =
232*on_mapload = sub { 343*on_mapload = sub {
233 my $map = shift->{map}; 344 my ($map) = @_;
234 345
235 my $path = $map->tmpname; 346 my $path = $map->tmpname;
236 $path = $map->path unless defined $path; 347 $path = $map->path unless defined $path;
237 348
238 open my $fh, "<:raw", "$path.cfperl" 349 open my $fh, "<:raw", "$path.cfperl"
245 356
246 $map->_set_obs ($data->{obs}); 357 $map->_set_obs ($data->{obs});
247}; 358};
248 359
249*on_mapout = sub { 360*on_mapout = sub {
250 my $map = shift->{map}; 361 my ($map) = @_;
251 362
252 my $path = $map->tmpname; 363 my $path = $map->tmpname;
253 $path = $map->path unless defined $path; 364 $path = $map->path unless defined $path;
254 365
255 my $obs = $map->_get_obs; 366 my $obs = $map->_get_obs;
275 386
276############################################################################# 387#############################################################################
277# load/save perl data associated with player->ob objects 388# load/save perl data associated with player->ob objects
278 389
279*on_player_load = sub { 390*on_player_load = sub {
280 my ($event) = @_; 391 my ($ob, $path) = @_;
281 my $path = $event->{message};
282 my $ob = $event->{who};
283 392
284 open my $fh, "<:raw", "$path.cfperl" 393 for my $o ($ob, $ob->inv) {
285 or return; # no perl data 394 if (my $value = $o->get_ob_key_value ("_perl_data")) {
395 $o->set_ob_key_value ("_perl_data");
286 396
287 my $data = Storable::thaw do { local $/; <$fh> }; 397 %$o = %{ Storable::thaw pack "H*", $value };
288 398 }
289 $data->{version} <= 1 399 }
290 or return; # too new
291
292 %$ob = %{$data->{ob}};
293}; 400};
294 401
295*on_player_save = sub { 402*on_player_save = sub {
296 my ($event) = @_; 403 my ($ob, $path) = @_;
297 my $path = $event->{message};
298 my $ob = $event->{who};
299 404
300 if (keys %$ob) { 405 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
301 open my $fh, ">:raw", "$path.cfperl" 406 for grep %$_, $ob, $ob->inv;
302 or die "$path.cfperl: $!"; 407};
303 408
304 stat $path; 409#############################################################################
410# core extensions - in perl
305 411
306 print $fh Storable::nfreeze { 412my $delta_timer = Event->timer (
307 size => (stat _)[7], 413 parked => 1,
308 time => (stat _)[9], 414 prio => Event::PRIO_HIGH,
309 version => 1, 415 cb => sub { Event::unloop (undef) },
310 ob => $ob, 416);
311 };
312 417
313 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* 418sub sleep_delta($) {
419 $delta_timer->at (Event::time + $_[0]);
420 $delta_timer->start;
421 Event::loop;
422}
423
424=item cf::player::exists $login
425
426Returns true when the given account exists.
427
428=cut
429
430sub cf::player::exists($) {
431 cf::player::find $_[0]
432 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
433}
434
435=item $player->reply ($npc, $msg[, $flags])
436
437Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
438can be C<undef>. Does the right thing when the player is currently in a
439dialogue with the given NPC character.
440
441=cut
442
443# rough implementation of a future "reply" method that works
444# with dialog boxes.
445sub cf::object::player::reply($$$;$) {
446 my ($self, $npc, $msg, $flags) = @_;
447
448 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
449
450 if ($self->{record_replies}) {
451 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
314 } else { 452 } else {
315 unlink "$path.cfperl"; 453 $msg = $npc->name . " says: $msg" if $npc;
454 $self->message ($msg, $flags);
455 }
456}
457
458=item $player->ext_reply ($msgid, $msgtype, %msg)
459
460Sends an ext reply to the player.
461
462=cut
463
464sub cf::player::ext_reply($$$%) {
465 my ($self, $id, %msg) = @_;
466
467 $msg{msgid} = $id;
468
469 $self->send ("ext " . to_json \%msg);
470}
471
472#############################################################################
473# map scripting support
474
475our $safe = new Safe "ext";
476our $safe_hole = new Safe::Hole;
477
478$SIG{FPE} = 'IGNORE';
479
480$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
481
482# here we export the classes and methods available to script code
483
484for (
485 ["cf::object" => qw(contr pay_amount pay_player)],
486 ["cf::object::player" => qw(player)],
487 ["cf::player" => qw(peaceful)],
488) {
489 no strict 'refs';
490 my ($pkg, @funs) = @$_;
491 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
492 for @funs;
493}
494
495sub safe_eval($;@) {
496 my ($code, %vars) = @_;
497
498 my $qcode = $code;
499 $qcode =~ s/"/‟/g; # not allowed in #line filenames
500 $qcode =~ s/\n/\\n/g;
501
502 local $_;
503 local @ext::cf::_safe_eval_args = values %vars;
504
505 $code =
506 "do {\n"
507 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
508 . "#line 0 \"{$qcode}\"\n"
509 . $code
510 . "\n}"
316 } 511 ;
317}; 512
513 sub_generation_inc;
514 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
515 sub_generation_inc;
516
517 wantarray ? @res : $res[0]
518}
519
520sub register_script_function {
521 my ($fun, $cb) = @_;
522
523 no strict 'refs';
524 *{"ext::$fun"} = $safe_hole->wrap ($cb);
525}
526
527#############################################################################
528# initialisation
318 529
319register "<global>", __PACKAGE__; 530register "<global>", __PACKAGE__;
320 531
532unshift @INC, $LIBDIR;
533
321load_extensions; 534load_extensions;
322 535
3231 5361
324 537

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines