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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines