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.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; 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 {
58prop_gen MAP_PROP => "cf::map"; 65prop_gen MAP_PROP => "cf::map";
59prop_gen ARCH_PROP => "cf::arch"; 66prop_gen ARCH_PROP => "cf::arch";
60 67
61# guessed hierarchies 68# guessed hierarchies
62 69
63@cf::object::player::ISA = 'cf::object'; 70@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
64@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)) {
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};
65 83
66my %ext_pkg; 84my %ext_pkg;
67my @exts; 85my @exts;
68my @hook; 86my @hook;
69my %command; 87my %command;
88my %extcmd;
70 89
71sub inject_event { 90sub inject_event {
72 my ($data) = @_; 91 my $extension = shift;
92 my $event_code = shift;
73 93
74 my $cb = $hook[$data->{event_code}]{$data->{extension}} 94 my $cb = $hook[$event_code]{$extension}
75 or return; 95 or return;
76 96
77 $cb->($data) 97 &$cb
78} 98}
79 99
80sub inject_global_event { 100sub inject_global_event {
81 my ($data) = @_; 101 my $event = shift;
82 102
83 my $cb = $hook[$data->{event_code}] 103 my $cb = $hook[$event]
84 or return; 104 or return;
85 105
86 $_->($data) for values %$cb; 106 List::Util::max map &$_, values %$cb
87
88 ()
89} 107}
90 108
91sub inject_command { 109sub inject_command {
92 my ($name, $obj, $params) = @_; 110 my ($name, $obj, $params) = @_;
93 111
100 118
101sub register_command { 119sub register_command {
102 my ($name, $time, $cb) = @_; 120 my ($name, $time, $cb) = @_;
103 121
104 my $caller = caller; 122 my $caller = caller;
105
106 warn "registering command '$name/$time' to '$caller'"; 123 #warn "registering command '$name/$time' to '$caller'";
107 124
108 push @{ $command{$name} }, [$time, $cb, $caller]; 125 push @{ $command{$name} }, [$time, $cb, $caller];
109 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 126 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
110} 127}
111 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
112sub register { 138sub register {
113 my ($base, $pkg) = @_; 139 my ($base, $pkg) = @_;
114 140
115 for my $idx (0 .. $#EVENT) { 141 for my $idx (0 .. $#EVENT) {
116 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 142 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
117 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 143 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
118 $hook[$idx]{$base} = $ref; 144 $hook[$idx]{$base} = $ref;
119 } 145 }
120 } 146 }
121} 147}
122 148
145 171
146 push @exts, $pkg; 172 push @exts, $pkg;
147 $ext_pkg{$base} = $pkg; 173 $ext_pkg{$base} = $pkg;
148 174
149# no strict 'refs'; 175# no strict 'refs';
150# @{"$pkg\::ISA"} = cf::ext::; 176# @{"$pkg\::ISA"} = ext::;
151 177
152 register $base, $pkg; 178 register $base, $pkg;
153} 179}
154 180
155sub unload_extension { 181sub unload_extension {
156 my ($pkg) = @_; 182 my ($pkg) = @_;
157 183
158 warn "removing extension $pkg\n"; 184 warn "removing extension $pkg\n";
185
186 if (my $cb = $pkg->can ("on_unload")) {
187 $cb->($pkg);
188 }
159 189
160 # remove hooks 190 # remove hooks
161 for my $idx (0 .. $#EVENT) { 191 for my $idx (0 .. $#EVENT) {
162 delete $hook[$idx]{$pkg}; 192 delete $hook[$idx]{$pkg};
163 } 193 }
173 delete $command{$name}; 203 delete $command{$name};
174 delete $COMMAND{"$name\000"}; 204 delete $COMMAND{"$name\000"};
175 } 205 }
176 } 206 }
177 207
208 # remove extcmds
209 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
210 delete $extcmd{$name};
211 }
212
178 Symbol::delete_package $pkg; 213 Symbol::delete_package $pkg;
179} 214}
180 215
181sub load_extensions { 216sub load_extensions {
182 my $LIBDIR = maps_directory "perl"; 217 my $LIBDIR = maps_directory "perl";
196 if ($who->flag (FLAG_WIZ)) { 231 if ($who->flag (FLAG_WIZ)) {
197 $who->message ("reloading..."); 232 $who->message ("reloading...");
198 233
199 warn "reloading...\n"; 234 warn "reloading...\n";
200 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 <$_>");
201 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"
202 delete $INC{"cf.pm"}; 270 delete $INC{"cf.pm"};
203 271
204 # don't, removes xs symbols, too 272 # don't, removes xs symbols, too
205 #Symbol::delete_package $pkg; 273 #Symbol::delete_package __PACKAGE__;
206 274
275 # 7. reload cf.pm
276 $who->message ("reloading cf.pm");
207 require cf; 277 require cf;
208 }; 278 };
209 warn $@ if $@; 279 warn $@ if $@;
210 $who->message ($@) if $@; 280 $who->message ($@) if $@;
211 warn "reloaded\n"; 281 warn "reloaded\n";
215 $who->message ("Intruder Alert!"); 285 $who->message ("Intruder Alert!");
216 } 286 }
217}; 287};
218 288
219############################################################################# 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#############################################################################
220# load/save/clean perl data associated with a map 303# load/save/clean perl data associated with a map
221 304
222*on_mapclean = sub { 305*on_mapclean = sub {
223 my $map = shift->{map}; 306 my ($map) = @_;
224 307
225 my $path = $map->tmpname; 308 my $path = $map->tmpname;
226 defined $path or return; 309 defined $path or return;
227 310
228 unlink "$path.cfperl"; 311 unlink "$path.cfperl";
229}; 312};
230 313
231*on_mapin = 314*on_mapin =
232*on_mapload = sub { 315*on_mapload = sub {
233 my $map = shift->{map}; 316 my ($map) = @_;
234 317
235 my $path = $map->tmpname; 318 my $path = $map->tmpname;
236 $path = $map->path unless defined $path; 319 $path = $map->path unless defined $path;
237 320
238 open my $fh, "<:raw", "$path.cfperl" 321 open my $fh, "<:raw", "$path.cfperl"
245 328
246 $map->_set_obs ($data->{obs}); 329 $map->_set_obs ($data->{obs});
247}; 330};
248 331
249*on_mapout = sub { 332*on_mapout = sub {
250 my $map = shift->{map}; 333 my ($map) = @_;
251 334
252 my $path = $map->tmpname; 335 my $path = $map->tmpname;
253 $path = $map->path unless defined $path; 336 $path = $map->path unless defined $path;
254 337
255 my $obs = $map->_get_obs; 338 my $obs = $map->_get_obs;
275 358
276############################################################################# 359#############################################################################
277# load/save perl data associated with player->ob objects 360# load/save perl data associated with player->ob objects
278 361
279*on_player_load = sub { 362*on_player_load = sub {
280 my ($event) = @_; 363 my ($ob, $path) = @_;
281 my $path = $event->{message};
282 my $ob = $event->{who};
283 364
284 open my $fh, "<:raw", "$path.cfperl" 365 for my $o ($ob, $ob->inv) {
285 or return; # no perl data 366 if (my $value = $o->get_ob_key_value ("_perl_data")) {
367 $o->set_ob_key_value ("_perl_data");
286 368
287 my $data = Storable::thaw do { local $/; <$fh> }; 369 %$o = %{ Storable::thaw pack "H*", $value };
288 370 }
289 $data->{version} <= 1 371 }
290 or return; # too new
291
292 %$ob = %{$data->{ob}};
293}; 372};
294 373
295*on_player_save = sub { 374*on_player_save = sub {
296 my ($event) = @_; 375 my ($ob, $path) = @_;
297 my $path = $event->{message};
298 my $ob = $event->{who};
299 376
300 if (keys %$ob) { 377 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
301 open my $fh, ">:raw", "$path.cfperl" 378 for grep %$_, $ob, $ob->inv;
302 or die "$path.cfperl: $!"; 379};
303 380
304 stat $path; 381#############################################################################
382# core extensions - in perl
305 383
306 print $fh Storable::nfreeze { 384=item cf::player::exists $login
307 size => (stat _)[7],
308 time => (stat _)[9],
309 version => 1,
310 ob => $ob,
311 };
312 385
313 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* 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];
314 } else { 404 } else {
315 unlink "$path.cfperl"; 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}"
316 } 449 ;
317}; 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
318 467
319register "<global>", __PACKAGE__; 468register "<global>", __PACKAGE__;
320 469
470unshift @INC, $LIBDIR;
471
321load_extensions; 472load_extensions;
322 473
3231 4741
324 475

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines