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.5 by root, Wed Feb 8 03:46:15 2006 UTC vs.
Revision 1.25 by root, Thu Jul 20 07:22:40 2006 UTC

1package cf; 1package cf;
2 2
3use Symbol; 3use Symbol;
4use List::Util; 4use List::Util;
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
5 12
6use strict; 13use strict;
7 14
8our %COMMAND; 15our %COMMAND;
9our @EVENT; 16our @EVENT;
21 print STDERR "cfperl: $msg"; 28 print STDERR "cfperl: $msg";
22 LOG llevError, "cfperl: $msg"; 29 LOG llevError, "cfperl: $msg";
23 }; 30 };
24} 31}
25 32
33my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply!
34
26# generate property mutators 35# generate property mutators
27sub prop_gen { 36sub prop_gen {
28 my ($prefix, $class) = @_; 37 my ($prefix, $class) = @_;
29 38
30 no strict 'refs'; 39 no strict 'refs';
40 $_[0]->get_property ($type, $idx) 49 $_[0]->get_property ($type, $idx)
41 }; 50 };
42 51
43 *{"$class\::set_$sub"} = sub { 52 *{"$class\::set_$sub"} = sub {
44 $_[0]->set_property ($type, $idx, $_[1]); 53 $_[0]->set_property ($type, $idx, $_[1]);
45 }; 54 } unless $ignore_set{$prop};
46 } 55 }
47} 56}
48 57
49# auto-generate most of the API 58# auto-generate most of the API
50 59
55prop_gen MAP_PROP => "cf::map"; 64prop_gen MAP_PROP => "cf::map";
56prop_gen ARCH_PROP => "cf::arch"; 65prop_gen ARCH_PROP => "cf::arch";
57 66
58# guessed hierarchies 67# guessed hierarchies
59 68
60@cf::object::player::ISA = 'cf::object'; 69@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
61@cf::object::map::ISA = 'cf::object'; 70@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
71
72# we bless all objects into derived classes to force a method lookup
73# within the Safe compartment.
74for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) {
75 no strict 'refs';
76 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
77}
78
79$Event::DIED = sub {
80 warn "error in event callback: @_";
81};
62 82
63my %ext_pkg; 83my %ext_pkg;
64my @exts; 84my @exts;
65my @hook; 85my @hook;
66my %command; 86my %command;
87my %extcmd;
67 88
68sub inject_event { 89sub inject_event {
69 my ($data) = @_; 90 my $extension = shift;
91 my $event_code = shift;
70 92
71 my $cb = $hook[$data->{event_code}]{$data->{extension}} 93 my $cb = $hook[$event_code]{$extension}
72 or return; 94 or return;
73 95
74 $cb->($data) 96 &$cb
75} 97}
76 98
77sub inject_global_event { 99sub inject_global_event {
78 my ($data) = @_; 100 my $event = shift;
79 101
80 my $cb = $hook[$data->{event_code}] 102 my $cb = $hook[$event]
81 or return; 103 or return;
82 104
83 $_->($data) for values %$cb; 105 List::Util::max map &$_, values %$cb
84
85 ()
86} 106}
87 107
88sub inject_command { 108sub inject_command {
89 my ($name, $obj, $params) = @_; 109 my ($name, $obj, $params) = @_;
90 110
97 117
98sub register_command { 118sub register_command {
99 my ($name, $time, $cb) = @_; 119 my ($name, $time, $cb) = @_;
100 120
101 my $caller = caller; 121 my $caller = caller;
102
103 warn "registering command '$name/$time' to '$caller'"; 122 #warn "registering command '$name/$time' to '$caller'";
104 123
105 push @{ $command{$name} }, [$time, $cb, $caller]; 124 push @{ $command{$name} }, [$time, $cb, $caller];
106 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 125 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
126}
127
128sub register_extcmd {
129 my ($name, $cb) = @_;
130
131 my $caller = caller;
132 #warn "registering extcmd '$name' to '$caller'";
133
134 $extcmd{$name} = [$cb, $caller];
135}
136
137sub register {
138 my ($base, $pkg) = @_;
139
140 for my $idx (0 .. $#EVENT) {
141 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
142 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
143 $hook[$idx]{$base} = $ref;
144 }
145 }
107} 146}
108 147
109sub load_extension { 148sub load_extension {
110 my ($path) = @_; 149 my ($path) = @_;
111 150
130 or die "$path: $@"; 169 or die "$path: $@";
131 170
132 push @exts, $pkg; 171 push @exts, $pkg;
133 $ext_pkg{$base} = $pkg; 172 $ext_pkg{$base} = $pkg;
134 173
135 no strict 'refs'; 174# no strict 'refs';
136
137# @{"$pkg\::ISA"} = cf::ext::; 175# @{"$pkg\::ISA"} = ext::;
138 176
139 for my $idx (0 .. $#EVENT) { 177 register $base, $pkg;
140 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
141 warn "registering $EVENT[$idx] hook\n";
142 $hook[$idx]{$base} = $ref;
143 }
144 }
145} 178}
146 179
147sub unload_extension { 180sub unload_extension {
148 my ($pkg) = @_; 181 my ($pkg) = @_;
149 182
150 warn "removing extension $pkg\n"; 183 warn "removing extension $pkg\n";
184
185 if (my $cb = $pkg->can ("on_unload")) {
186 $cb->($pkg);
187 }
151 188
152 # remove hooks 189 # remove hooks
153 for my $idx (0 .. $#EVENT) { 190 for my $idx (0 .. $#EVENT) {
154 delete $hook[$idx]{$pkg}; 191 delete $hook[$idx]{$pkg};
155 } 192 }
165 delete $command{$name}; 202 delete $command{$name};
166 delete $COMMAND{"$name\000"}; 203 delete $COMMAND{"$name\000"};
167 } 204 }
168 } 205 }
169 206
207 # remove extcmds
208 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
209 delete $extcmd{$name};
210 }
211
170 Symbol::delete_package $pkg; 212 Symbol::delete_package $pkg;
171} 213}
172 214
173sub load_extensions { 215sub load_extensions {
174 my $LIBDIR = maps_directory "perl"; 216 my $LIBDIR = maps_directory "perl";
188 if ($who->flag (FLAG_WIZ)) { 230 if ($who->flag (FLAG_WIZ)) {
189 $who->message ("reloading..."); 231 $who->message ("reloading...");
190 232
191 warn "reloading...\n"; 233 warn "reloading...\n";
192 eval { 234 eval {
235 $_->cancel for Event::all_watchers;
236
193 unload_extension $_ for @exts; 237 unload_extension $_ for @exts;
194 delete $INC{"cf.pm"}; 238 delete $INC{"cf.pm"};
195 239
196 # don't, removes xs symbols, too 240 # don't, removes xs symbols, too
197 #Symbol::delete_package $pkg; 241 #Symbol::delete_package __PACKAGE__;
198 242
199 require cf; 243 require cf;
200 }; 244 };
201 warn $@ if $@; 245 warn $@ if $@;
202 $who->message ($@) if $@; 246 $who->message ($@) if $@;
206 } else { 250 } else {
207 $who->message ("Intruder Alert!"); 251 $who->message ("Intruder Alert!");
208 } 252 }
209}; 253};
210 254
255#############################################################################
256# extcmd framework, basically convert ext <id> <pkg> arg1 args
257# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
258
259sub on_extcmd {
260 my ($pl, $buf) = @_;
261
262 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
263
264 $extcmd{$type}[0]->($pl, $buf)
265 if $extcmd{$type};
266}
267
268#############################################################################
269# load/save/clean perl data associated with a map
270
271*on_mapclean = sub {
272 my ($map) = @_;
273
274 my $path = $map->tmpname;
275 defined $path or return;
276
277 unlink "$path.cfperl";
278};
279
280*on_mapin =
281*on_mapload = sub {
282 my ($map) = @_;
283
284 my $path = $map->tmpname;
285 $path = $map->path unless defined $path;
286
287 open my $fh, "<:raw", "$path.cfperl"
288 or return; # no perl data
289
290 my $data = Storable::thaw do { local $/; <$fh> };
291
292 $data->{version} <= 1
293 or return; # too new
294
295 $map->_set_obs ($data->{obs});
296};
297
298*on_mapout = sub {
299 my ($map) = @_;
300
301 my $path = $map->tmpname;
302 $path = $map->path unless defined $path;
303
304 my $obs = $map->_get_obs;
305
306 if (defined $obs) {
307 open my $fh, ">:raw", "$path.cfperl"
308 or die "$path.cfperl: $!";
309
310 stat $path;
311
312 print $fh Storable::nfreeze {
313 size => (stat _)[7],
314 time => (stat _)[9],
315 version => 1,
316 obs => $obs,
317 };
318
319 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
320 } else {
321 unlink "$path.cfperl";
322 }
323};
324
325#############################################################################
326# load/save perl data associated with player->ob objects
327
328*on_player_load = sub {
329 my ($ob, $path) = @_;
330
331 if (open my $fh, "<:raw", "$path.cfperl") {
332
333 #d##TODO#remove
334
335 my $data = Storable::thaw do { local $/; <$fh> };
336
337 $data->{version} <= 1
338 or return; # too new
339
340 %$ob = %{$data->{ob}};
341 return;
342 }
343
344 for my $o ($ob, $ob->inv) {
345 if (my $value = $o->get_ob_key_value ("_perl_data")) {
346 $o->set_ob_key_value ("_perl_data");
347
348 %$o = %{ Storable::thaw pack "H*", $value };
349 }
350 }
351};
352
353*on_player_save = sub {
354 my ($ob, $path) = @_;
355
356 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
357 for grep %$_, $ob, $ob->inv;
358
359 unlink "$path.cfperl";#d##TODO#remove
360};
361
362#############################################################################
363# core extensions - in perl
364
365=item cf::player::exists $login
366
367Returns true when the given account exists.
368
369=cut
370
371sub cf::player::exists($) {
372 cf::player::find $_[0]
373 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
374}
375
376# rough implementation of a future "reply" method that works
377# with dialog boxes.
378sub cf::object::player::reply($$$;$) {
379 my ($self, $npc, $msg, $flags) = @_;
380
381 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
382
383 if ($self->{record_replies}) {
384 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
385 } else {
386 $msg = $npc->name . " says: $msg" if $npc;
387 $self->message ($msg, $flags);
388 }
389}
390
391#############################################################################
392# map scripting support
393
394our $safe = new Safe "ext";
395our $safe_hole = new Safe::Hole;
396
397$SIG{FPE} = 'IGNORE';
398
399$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
400
401# here we export the classes and methods available to script code
402
403for (
404 ["cf::object" => qw(contr)],
405 ["cf::object::player" => qw(player)],
406 ["cf::player" => qw(peaceful)],
407) {
408 no strict 'refs';
409 my ($pkg, @funs) = @$_;
410 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
411 for @funs;
412}
413
414sub safe_eval($;@) {
415 my ($code, %vars) = @_;
416
417 my $qcode = $code;
418 $qcode =~ s/"/‟/g; # not allowed in #line filenames
419 $qcode =~ s/\n/\\n/g;
420
421 local $_;
422 local @ext::cf::_safe_eval_args = values %vars;
423
424 $code =
425 "do {\n"
426 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
427 . "#line 0 \"{$qcode}\"\n"
428 . $code
429 . "\n}"
430 ;
431
432 sub_generation_inc;
433 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
434 sub_generation_inc;
435
436 wantarray ? @res : $res[0]
437}
438
439sub register_script_function {
440 my ($fun, $cb) = @_;
441
442 no strict 'refs';
443 *{"ext::$fun"} = $safe_hole->wrap ($cb);
444}
445
446#############################################################################
447# initialisation
448
449register "<global>", __PACKAGE__;
450
451unshift @INC, maps_directory "perl";
452
211load_extensions; 453load_extensions;
212 454
2131 4551
214 456

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines