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.15 by root, Sun Jun 11 15:02:16 2006 UTC vs.
Revision 1.26 by root, Thu Jul 20 22:03:36 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;
58prop_gen MAP_PROP => "cf::map"; 64prop_gen MAP_PROP => "cf::map";
59prop_gen ARCH_PROP => "cf::arch"; 65prop_gen ARCH_PROP => "cf::arch";
60 66
61# guessed hierarchies 67# guessed hierarchies
62 68
63@cf::object::player::ISA = 'cf::object'; 69@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
64@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};
65 82
66my %ext_pkg; 83my %ext_pkg;
67my @exts; 84my @exts;
68my @hook; 85my @hook;
69my %command; 86my %command;
100 117
101sub register_command { 118sub register_command {
102 my ($name, $time, $cb) = @_; 119 my ($name, $time, $cb) = @_;
103 120
104 my $caller = caller; 121 my $caller = caller;
105
106 warn "registering command '$name/$time' to '$caller'"; 122 #warn "registering command '$name/$time' to '$caller'";
107 123
108 push @{ $command{$name} }, [$time, $cb, $caller]; 124 push @{ $command{$name} }, [$time, $cb, $caller];
109 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 125 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
110} 126}
111 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
112sub register { 137sub register {
113 my ($base, $pkg) = @_; 138 my ($base, $pkg) = @_;
114 139
115 for my $idx (0 .. $#EVENT) { 140 for my $idx (0 .. $#EVENT) {
116 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 141 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
117 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 142 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
118 $hook[$idx]{$base} = $ref; 143 $hook[$idx]{$base} = $ref;
119 } 144 }
120 } 145 }
121} 146}
122 147
145 170
146 push @exts, $pkg; 171 push @exts, $pkg;
147 $ext_pkg{$base} = $pkg; 172 $ext_pkg{$base} = $pkg;
148 173
149# no strict 'refs'; 174# no strict 'refs';
150# @{"$pkg\::ISA"} = cf::ext::; 175# @{"$pkg\::ISA"} = ext::;
151 176
152 register $base, $pkg; 177 register $base, $pkg;
153} 178}
154 179
155sub unload_extension { 180sub unload_extension {
156 my ($pkg) = @_; 181 my ($pkg) = @_;
157 182
158 warn "removing extension $pkg\n"; 183 warn "removing extension $pkg\n";
184
185 if (my $cb = $pkg->can ("on_unload")) {
186 $cb->($pkg);
187 }
159 188
160 # remove hooks 189 # remove hooks
161 for my $idx (0 .. $#EVENT) { 190 for my $idx (0 .. $#EVENT) {
162 delete $hook[$idx]{$pkg}; 191 delete $hook[$idx]{$pkg};
163 } 192 }
174 delete $COMMAND{"$name\000"}; 203 delete $COMMAND{"$name\000"};
175 } 204 }
176 } 205 }
177 206
178 # remove extcmds 207 # remove extcmds
179 for my $name (keys %command) { 208 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
180 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
181
182 if (@cb) {
183 $command{$name} = \@cb;
184 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb;
185 } else {
186 delete $command{$name}; 209 delete $extcmd{$name};
187 delete $COMMAND{"$name\000"};
188 }
189 } 210 }
190
191 211
192 Symbol::delete_package $pkg; 212 Symbol::delete_package $pkg;
193} 213}
194 214
195sub load_extensions { 215sub load_extensions {
210 if ($who->flag (FLAG_WIZ)) { 230 if ($who->flag (FLAG_WIZ)) {
211 $who->message ("reloading..."); 231 $who->message ("reloading...");
212 232
213 warn "reloading...\n"; 233 warn "reloading...\n";
214 eval { 234 eval {
235 $_->cancel for Event::all_watchers;
236
215 unload_extension $_ for @exts; 237 unload_extension $_ for @exts;
216 delete $INC{"cf.pm"}; 238 delete $INC{"cf.pm"};
217 239
218 # don't, removes xs symbols, too 240 # don't, removes xs symbols, too
219 #Symbol::delete_package $pkg; 241 #Symbol::delete_package __PACKAGE__;
220 242
221 require cf; 243 require cf;
222 }; 244 };
223 warn $@ if $@; 245 warn $@ if $@;
224 $who->message ($@) if $@; 246 $who->message ($@) if $@;
235# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 257# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
236 258
237sub on_extcmd { 259sub on_extcmd {
238 my ($pl, $buf) = @_; 260 my ($pl, $buf) = @_;
239 261
240 my ($id, $pkg, $name, $data) = split / /, $buf, 4; 262 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
241 263
242 if (my $method = "cf::ext::$pkg"->can ("on_extcmd_$name")) { 264 $extcmd{$type}[0]->($pl, $buf)
243 $method->($pl, $id, $buf); 265 if $extcmd{$type};
244 }
245} 266}
246 267
247############################################################################# 268#############################################################################
248# load/save/clean perl data associated with a map 269# load/save/clean perl data associated with a map
249 270
305# load/save perl data associated with player->ob objects 326# load/save perl data associated with player->ob objects
306 327
307*on_player_load = sub { 328*on_player_load = sub {
308 my ($ob, $path) = @_; 329 my ($ob, $path) = @_;
309 330
310 if (open my $fh, "<:raw", "$path.cfperl") {
311
312 #d##TODO#remove
313
314 my $data = Storable::thaw do { local $/; <$fh> };
315
316 $data->{version} <= 1
317 or return; # too new
318
319 %$ob = %{$data->{ob}};
320 return;
321 }
322
323 for my $o ($ob, $ob->inv) { 331 for my $o ($ob, $ob->inv) {
324 if (my $value = $o->get_ob_key_value ("_perl_data")) { 332 if (my $value = $o->get_ob_key_value ("_perl_data")) {
325 $o->set_ob_key_value ("_perl_data"); 333 $o->set_ob_key_value ("_perl_data");
326 334
327 %$o = %{ Storable::thaw pack "H*", $value }; 335 %$o = %{ Storable::thaw pack "H*", $value };
332*on_player_save = sub { 340*on_player_save = sub {
333 my ($ob, $path) = @_; 341 my ($ob, $path) = @_;
334 342
335 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 343 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
336 for grep %$_, $ob, $ob->inv; 344 for grep %$_, $ob, $ob->inv;
337
338 unlink "$path.cfperl";#d##TODO#remove
339}; 345};
346
347#############################################################################
348# core extensions - in perl
349
350=item cf::player::exists $login
351
352Returns true when the given account exists.
353
354=cut
355
356sub cf::player::exists($) {
357 cf::player::find $_[0]
358 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
359}
360
361# rough implementation of a future "reply" method that works
362# with dialog boxes.
363sub cf::object::player::reply($$$;$) {
364 my ($self, $npc, $msg, $flags) = @_;
365
366 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
367
368 if ($self->{record_replies}) {
369 push @{ $self->{record_replies} }, [$npc, $msg, $flags];
370 } else {
371 $msg = $npc->name . " says: $msg" if $npc;
372 $self->message ($msg, $flags);
373 }
374}
375
376#############################################################################
377# map scripting support
378
379our $safe = new Safe "ext";
380our $safe_hole = new Safe::Hole;
381
382$SIG{FPE} = 'IGNORE';
383
384$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
385
386# here we export the classes and methods available to script code
387
388for (
389 ["cf::object" => qw(contr)],
390 ["cf::object::player" => qw(player)],
391 ["cf::player" => qw(peaceful)],
392) {
393 no strict 'refs';
394 my ($pkg, @funs) = @$_;
395 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
396 for @funs;
397}
398
399sub safe_eval($;@) {
400 my ($code, %vars) = @_;
401
402 my $qcode = $code;
403 $qcode =~ s/"/‟/g; # not allowed in #line filenames
404 $qcode =~ s/\n/\\n/g;
405
406 local $_;
407 local @ext::cf::_safe_eval_args = values %vars;
408
409 $code =
410 "do {\n"
411 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
412 . "#line 0 \"{$qcode}\"\n"
413 . $code
414 . "\n}"
415 ;
416
417 sub_generation_inc;
418 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code);
419 sub_generation_inc;
420
421 wantarray ? @res : $res[0]
422}
423
424sub register_script_function {
425 my ($fun, $cb) = @_;
426
427 no strict 'refs';
428 *{"ext::$fun"} = $safe_hole->wrap ($cb);
429}
430
431#############################################################################
432# initialisation
340 433
341register "<global>", __PACKAGE__; 434register "<global>", __PACKAGE__;
342 435
436unshift @INC, maps_directory "perl";
437
343load_extensions; 438load_extensions;
344 439
3451 4401
346 441

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines