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.23 by root, Wed Jul 19 22:19:19 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;
61# guessed hierarchies 67# guessed hierarchies
62 68
63@cf::object::player::ISA = 'cf::object'; 69@cf::object::player::ISA = 'cf::object';
64@cf::object::map::ISA = 'cf::object'; 70@cf::object::map::ISA = 'cf::object';
65 71
72$Event::DIED = sub {
73 warn "error in event callback: @_";
74};
75
66my %ext_pkg; 76my %ext_pkg;
67my @exts; 77my @exts;
68my @hook; 78my @hook;
69my %command; 79my %command;
80my %extcmd;
70 81
71sub inject_event { 82sub inject_event {
72 my ($data) = @_; 83 my $extension = shift;
84 my $event_code = shift;
73 85
74 my $cb = $hook[$data->{event_code}]{$data->{extension}} 86 my $cb = $hook[$event_code]{$extension}
75 or return; 87 or return;
76 88
77 $cb->($data) 89 &$cb
78} 90}
79 91
80sub inject_global_event { 92sub inject_global_event {
81 my ($data) = @_; 93 my $event = shift;
82 94
83 my $cb = $hook[$data->{event_code}] 95 my $cb = $hook[$event]
84 or return; 96 or return;
85 97
86 $_->($data) for values %$cb; 98 List::Util::max map &$_, values %$cb
87
88 ()
89} 99}
90 100
91sub inject_command { 101sub inject_command {
92 my ($name, $obj, $params) = @_; 102 my ($name, $obj, $params) = @_;
93 103
100 110
101sub register_command { 111sub register_command {
102 my ($name, $time, $cb) = @_; 112 my ($name, $time, $cb) = @_;
103 113
104 my $caller = caller; 114 my $caller = caller;
105
106 warn "registering command '$name/$time' to '$caller'"; 115 #warn "registering command '$name/$time' to '$caller'";
107 116
108 push @{ $command{$name} }, [$time, $cb, $caller]; 117 push @{ $command{$name} }, [$time, $cb, $caller];
109 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; 118 $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} };
110} 119}
111 120
121sub register_extcmd {
122 my ($name, $cb) = @_;
123
124 my $caller = caller;
125 #warn "registering extcmd '$name' to '$caller'";
126
127 $extcmd{$name} = [$cb, $caller];
128}
129
112sub register { 130sub register {
113 my ($base, $pkg) = @_; 131 my ($base, $pkg) = @_;
114 132
115 for my $idx (0 .. $#EVENT) { 133 for my $idx (0 .. $#EVENT) {
116 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 134 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
117 warn "registering $EVENT[$idx] hook to '$pkg'\n"; 135 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
118 $hook[$idx]{$base} = $ref; 136 $hook[$idx]{$base} = $ref;
119 } 137 }
120 } 138 }
121} 139}
122 140
145 163
146 push @exts, $pkg; 164 push @exts, $pkg;
147 $ext_pkg{$base} = $pkg; 165 $ext_pkg{$base} = $pkg;
148 166
149# no strict 'refs'; 167# no strict 'refs';
150# @{"$pkg\::ISA"} = cf::ext::; 168# @{"$pkg\::ISA"} = ext::;
151 169
152 register $base, $pkg; 170 register $base, $pkg;
153} 171}
154 172
155sub unload_extension { 173sub unload_extension {
156 my ($pkg) = @_; 174 my ($pkg) = @_;
157 175
158 warn "removing extension $pkg\n"; 176 warn "removing extension $pkg\n";
177
178 if (my $cb = $pkg->can ("on_unload")) {
179 $cb->($pkg);
180 }
159 181
160 # remove hooks 182 # remove hooks
161 for my $idx (0 .. $#EVENT) { 183 for my $idx (0 .. $#EVENT) {
162 delete $hook[$idx]{$pkg}; 184 delete $hook[$idx]{$pkg};
163 } 185 }
173 delete $command{$name}; 195 delete $command{$name};
174 delete $COMMAND{"$name\000"}; 196 delete $COMMAND{"$name\000"};
175 } 197 }
176 } 198 }
177 199
200 # remove extcmds
201 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
202 delete $extcmd{$name};
203 }
204
178 Symbol::delete_package $pkg; 205 Symbol::delete_package $pkg;
179} 206}
180 207
181sub load_extensions { 208sub load_extensions {
182 my $LIBDIR = maps_directory "perl"; 209 my $LIBDIR = maps_directory "perl";
196 if ($who->flag (FLAG_WIZ)) { 223 if ($who->flag (FLAG_WIZ)) {
197 $who->message ("reloading..."); 224 $who->message ("reloading...");
198 225
199 warn "reloading...\n"; 226 warn "reloading...\n";
200 eval { 227 eval {
228 $_->cancel for Event::all_watchers;
229
201 unload_extension $_ for @exts; 230 unload_extension $_ for @exts;
202 delete $INC{"cf.pm"}; 231 delete $INC{"cf.pm"};
203 232
204 # don't, removes xs symbols, too 233 # don't, removes xs symbols, too
205 #Symbol::delete_package $pkg; 234 #Symbol::delete_package __PACKAGE__;
206 235
207 require cf; 236 require cf;
208 }; 237 };
209 warn $@ if $@; 238 warn $@ if $@;
210 $who->message ($@) if $@; 239 $who->message ($@) if $@;
215 $who->message ("Intruder Alert!"); 244 $who->message ("Intruder Alert!");
216 } 245 }
217}; 246};
218 247
219############################################################################# 248#############################################################################
249# extcmd framework, basically convert ext <id> <pkg> arg1 args
250# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
251
252sub on_extcmd {
253 my ($pl, $buf) = @_;
254
255 my ($type) = $buf =~ s/^(\S+) // ? $1 : "";
256
257 $extcmd{$type}[0]->($pl, $buf)
258 if $extcmd{$type};
259}
260
261#############################################################################
220# load/save/clean perl data associated with a map 262# load/save/clean perl data associated with a map
221 263
222*on_mapclean = sub { 264*on_mapclean = sub {
223 my $map = shift->{map}; 265 my ($map) = @_;
224 266
225 my $path = $map->tmpname; 267 my $path = $map->tmpname;
226 defined $path or return; 268 defined $path or return;
227 269
228 unlink "$path.cfperl"; 270 unlink "$path.cfperl";
229}; 271};
230 272
231*on_mapin = 273*on_mapin =
232*on_mapload = sub { 274*on_mapload = sub {
233 my $map = shift->{map}; 275 my ($map) = @_;
234 276
235 my $path = $map->tmpname; 277 my $path = $map->tmpname;
236 $path = $map->path unless defined $path; 278 $path = $map->path unless defined $path;
237 279
238 open my $fh, "<:raw", "$path.cfperl" 280 open my $fh, "<:raw", "$path.cfperl"
245 287
246 $map->_set_obs ($data->{obs}); 288 $map->_set_obs ($data->{obs});
247}; 289};
248 290
249*on_mapout = sub { 291*on_mapout = sub {
250 my $map = shift->{map}; 292 my ($map) = @_;
251 293
252 my $path = $map->tmpname; 294 my $path = $map->tmpname;
253 $path = $map->path unless defined $path; 295 $path = $map->path unless defined $path;
254 296
255 my $obs = $map->_get_obs; 297 my $obs = $map->_get_obs;
275 317
276############################################################################# 318#############################################################################
277# load/save perl data associated with player->ob objects 319# load/save perl data associated with player->ob objects
278 320
279*on_player_load = sub { 321*on_player_load = sub {
280 my ($event) = @_; 322 my ($ob, $path) = @_;
281 my $path = $event->{message};
282 my $ob = $event->{who};
283 323
284 open my $fh, "<:raw", "$path.cfperl" 324 if (open my $fh, "<:raw", "$path.cfperl") {
285 or return; # no perl data
286 325
326 #d##TODO#remove
327
287 my $data = Storable::thaw do { local $/; <$fh> }; 328 my $data = Storable::thaw do { local $/; <$fh> };
288 329
289 $data->{version} <= 1 330 $data->{version} <= 1
290 or return; # too new 331 or return; # too new
291 332
292 %$ob = %{$data->{ob}}; 333 %$ob = %{$data->{ob}};
334 return;
335 }
336
337 for my $o ($ob, $ob->inv) {
338 if (my $value = $o->get_ob_key_value ("_perl_data")) {
339 $o->set_ob_key_value ("_perl_data");
340
341 %$o = %{ Storable::thaw pack "H*", $value };
342 }
343 }
293}; 344};
294 345
295*on_player_save = sub { 346*on_player_save = sub {
296 my ($event) = @_; 347 my ($ob, $path) = @_;
297 my $path = $event->{message};
298 my $ob = $event->{who};
299 348
300 if (keys %$ob) { 349 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
301 open my $fh, ">:raw", "$path.cfperl" 350 for grep %$_, $ob, $ob->inv;
302 or die "$path.cfperl: $!";
303 351
304 stat $path; 352 unlink "$path.cfperl";#d##TODO#remove
353};
305 354
306 print $fh Storable::nfreeze { 355#############################################################################
307 size => (stat _)[7], 356# core extensions - in perl
308 time => (stat _)[9],
309 version => 1,
310 ob => $ob,
311 };
312 357
313 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* 358=item cf::player::exists $login
314 } else { 359
315 unlink "$path.cfperl"; 360Returns true when the given account exists.
361
362=cut
363
364sub cf::player::exists($) {
365 cf::player::find $_[0]
366 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
367}
368
369# rough implementation of a future "reply" method that works
370# with dialog boxes.
371sub cf::object::player::reply($$$;$) {
372 my ($self, $npc, $msg, $flags) = @_;
373
374 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
375
376 $msg = $npc->name . " says: $msg" if $npc;
377
378 $self->message ($msg, $flags);
379}
380
381#############################################################################
382# map scripting support
383
384our $safe = new Safe "ext";
385our $safe_hole = new Safe::Hole;
386
387$SIG{FPE} = 'IGNORE';
388
389$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
390
391# here we would export the classes and methods available to script code
392#@ext::cf::object::player::ISA = @cf::object::player::ISA;
393#@ext::cf::object::map::ISA = @cf::object::map::ISA;
394
395sub safe_eval($;@) {
396 my ($code, %vars) = @_;
397
398 my $qcode = $code;
399 $qcode =~ s/"/‟/g; # not allowed in #line filenames
400 $qcode =~ s/\n/\\n/g;
401
402 local $_;
403 local @ext::cf::_safe_eval_args = values %vars;
404
405 $safe->reval (
406 "do {\n"
407 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
408 . "#line 0 \"{$qcode}\"\n"
409 . $code
410 . "\n}"
316 } 411 )
317}; 412}
413
414sub register_script_function {
415 my ($fun, $cb) = @_;
416
417 no strict 'refs';
418 *{"ext::$fun"} = $safe_hole->wrap ($cb);
419}
420
421#############################################################################
422# initialisation
318 423
319register "<global>", __PACKAGE__; 424register "<global>", __PACKAGE__;
320 425
426unshift @INC, maps_directory "perl";
427
321load_extensions; 428load_extensions;
322 429
3231 4301
324 431

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines