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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines