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.39 by root, Fri Aug 25 13:24:50 2006 UTC vs.
Revision 1.52 by root, Mon Aug 28 16:52:51 2006 UTC

11use Event; 11use Event;
12$Event::Eval = 1; # no idea why this is required, but it is 12$Event::Eval = 1; # no idea why this is required, but it is
13 13
14use strict; 14use strict;
15 15
16_reload_1;
17
16our %COMMAND = (); 18our %COMMAND = ();
17our @EVENT; 19our @EVENT;
18our @PLUGIN_EVENT;
19our %PROP_TYPE; 20our %PROP_TYPE;
20our %PROP_IDX; 21our %PROP_IDX;
21our $LIBDIR = maps_directory "perl"; 22our $LIBDIR = maps_directory "perl";
22 23
23our $TICK = MAX_TIME * 1e-6; 24our $TICK = MAX_TIME * 1e-6;
24our $TICK_WATCHER; 25our $TICK_WATCHER;
25our $NEXT_TICK; 26our $NEXT_TICK;
26 27
27BEGIN { 28BEGIN {
28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
29
30 *CORE::GLOBAL::warn = sub { 29 *CORE::GLOBAL::warn = sub {
31 my $msg = join "", @_; 30 my $msg = join "", @_;
32 $msg .= "\n" 31 $msg .= "\n"
33 unless $msg =~ /\n$/; 32 unless $msg =~ /\n$/;
34 33
69prop_gen PLAYER_PROP => "cf::object::player"; 68prop_gen PLAYER_PROP => "cf::object::player";
70 69
71prop_gen MAP_PROP => "cf::map"; 70prop_gen MAP_PROP => "cf::map";
72prop_gen ARCH_PROP => "cf::arch"; 71prop_gen ARCH_PROP => "cf::arch";
73 72
74# guessed hierarchies
75
76@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 73@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
77@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
78 74
79# we bless all objects into derived classes to force a method lookup 75# we bless all objects into (empty) derived classes to force a method lookup
80# within the Safe compartment. 76# within the Safe compartment.
81for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { 77for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) {
82 no strict 'refs'; 78 no strict 'refs';
83 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 79 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
84} 80}
85 81
86$Event::DIED = sub { 82$Event::DIED = sub {
87 warn "error in event callback: @_"; 83 warn "error in event callback: @_";
88}; 84};
92my @hook; 88my @hook;
93my %command; 89my %command;
94my %extcmd; 90my %extcmd;
95 91
96############################################################################# 92#############################################################################
93# utility functions
94
95use JSON::Syck (); # TODO# replace by JSON::PC once working
96
97sub from_json($) {
98 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
99 JSON::Syck::Load $_[0]
100}
101
102sub to_json($) {
103 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
104 JSON::Syck::Dump $_[0]
105}
106
107#############################################################################
97# "new" plug-in system 108# "new" plug-in system
98 109
99=item cf::object::attach ... # NYI 110=item $object->attach ($attachment, key => $value...)
100 111
112Attach a pre-registered attachment to an object.
113
114=item $player->attach ($attachment, key => $value...)
115
116Attach a pre-registered attachment to a player.
117
118=item $map->attach ($attachment, key => $value...) # not yet persistent
119
120Attach a pre-registered attachment to a map.
121
101=item cf::attach_global ... # NYI 122=item cf::attach_global ...
102 123
103=item cf::attach_to_type ... # NYI 124Attach handlers for global events.
104 125
126This and all following C<attach_*>-functions expect any number of the
127following handler/hook descriptions:
128
129=over 4
130
131=item prio => $number
132
133Set the priority for all following handlers/hooks (unless overwritten
134by another C<prio> setting). Lower priority handlers get executed
135earlier. The default priority is C<0>, and many built-in handlers are
136registered at priority C<-1000>, so lower priorities should not be used
137unless you know what you are doing.
138
139=item on_I<event> => \&cb
140
141Call the given code reference whenever the named event happens (event is
142something like C<instantiate>, C<apply>, C<use_skill> and so on, and which
143handlers are recognised generally depends on the type of object these
144handlers attach to).
145
146See F<include/eventinc.h> for the full list of events supported, and their
147class.
148
149=item package => package::
150
151Look for sub functions of the name C<< on_I<event> >> in the given
152package and register them. Only handlers for eevents supported by the
153object/class are recognised.
154
155=back
156
157=item cf::attach_to_type $object_type, $subtype, ...
158
159Attach handlers for a specific object type (e.g. TRANSPORT) and
160subtype. If C<$subtype> is zero or undef, matches all objects of the given
161type.
162
105=item cf::attach_to_objects ... # NYI 163=item cf::attach_to_objects ...
106 164
165Attach handlers to all objects. Do not use this except for debugging or
166very rare events, as handlers are (obviously) called for I<all> objects in
167the game.
168
107=item cf::attach_to_players ... # NYI 169=item cf::attach_to_players ...
108 170
171Attach handlers to all players.
172
109=item cf::attach_to_maps ... # NYI 173=item cf::attach_to_maps ...
110 174
111 prio => $number, # higehr is earlier 175Attach handlers to all maps.
112 on_xxx => \&cb, 176
113 package => package::, 177=item cf:register_attachment $name, ...
178
179Register an attachment by name through which objects can refer to this
180attachment.
181
182=item cf:register_map_attachment $name, ...
183
184Register an attachment by name through which maps can refer to this
185attachment.
114 186
115=cut 187=cut
116 188
117our %CB_CLASS = (); # registry for class-based events 189# the following variables are defined in .xs and must not be re-created
118our @CB_GLOBAL = (); # registry for all global events 190our @CB_GLOBAL = (); # registry for all global events
191our @CB_OBJECT = (); # all objects (should not be used except in emergency)
192our @CB_PLAYER = ();
119our @CB_TYPE = (); # registry for type (cf-object class) based events 193our @CB_TYPE = (); # registry for type (cf-object class) based events
194our @CB_MAP = ();
195
196my %attachment;
120 197
121sub _attach_cb($\%$$$) { 198sub _attach_cb($\%$$$) {
122 my ($registry, $undo, $event, $prio, $cb) = @_; 199 my ($registry, $undo, $event, $prio, $cb) = @_;
123 200
124 use sort 'stable'; 201 use sort 'stable';
133} 210}
134 211
135# attach handles attaching event callbacks 212# attach handles attaching event callbacks
136# the only thing the caller has to do is pass the correct 213# the only thing the caller has to do is pass the correct
137# registry (== where the callback attaches to). 214# registry (== where the callback attaches to).
138sub _attach(\@$\@) { 215sub _attach(\@$@) {
139 my ($registry, $klass, $arg) = @_; 216 my ($registry, $klass, @arg) = @_;
140 217
141 my $prio = 0; 218 my $prio = 0;
142 219
143 my %undo = ( 220 my %undo = (
144 registry => $registry, 221 registry => $registry,
145 cb => [], 222 cb => [],
146 ); 223 );
147 224
148 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; 225 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
149 226
150 while (@$arg) { 227 while (@arg) {
151 my $type = shift @$arg; 228 my $type = shift @arg;
152 229
153 if ($type eq "prio") { 230 if ($type eq "prio") {
154 $prio = shift @$arg; 231 $prio = shift @arg;
155 232
156 } elsif ($type eq "package") { 233 } elsif ($type eq "package") {
157 my $pkg = shift @$arg; 234 my $pkg = shift @arg;
158 235
159 while (my ($name, $id) = each %cb_id) { 236 while (my ($name, $id) = each %cb_id) {
160 if (my $cb = $pkg->can ($name)) { 237 if (my $cb = $pkg->can ($name)) {
161 _attach_cb $registry, %undo, $id, $prio, $cb; 238 _attach_cb $registry, %undo, $id, $prio, $cb;
162 } 239 }
163 } 240 }
164 241
165 } elsif (exists $cb_id{$type}) { 242 } elsif (exists $cb_id{$type}) {
166 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; 243 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg;
167 244
168 } elsif (ref $type) { 245 } elsif (ref $type) {
169 warn "attaching objects not supported, ignoring.\n"; 246 warn "attaching objects not supported, ignoring.\n";
170 247
171 } else { 248 } else {
172 shift @$arg; 249 shift @arg;
173 warn "attach argument '$type' not supported, ignoring.\n"; 250 warn "attach argument '$type' not supported, ignoring.\n";
174 } 251 }
175 } 252 }
176 253
177 \%undo 254 \%undo
178} 255}
179 256
257sub _attach_attachment {
258 my ($obj, $name, %arg) = @_;
259
260 my $res;
261
262 if (my $attach = $attachment{$name}) {
263 my $registry = $obj->registry;
264
265 for (@$attach) {
266 my ($klass, @attach) = @$_;
267 $res = _attach @$registry, $klass, @attach;
268 }
269
270 $obj->{$name} = \%arg;
271 } else {
272 warn "object uses attachment '$name' that is not available, postponing.\n";
273 }
274
275 $obj->{_attachment}{$name} = undef;
276
277 $res->{attachment} = $name;
278 $res
279}
280
180sub cf::object::attach { 281sub cf::object::attach {
181 die; 282 my ($obj, $name, %arg) = @_;
283
284 _attach_attachment $obj, $name, %arg;
285}
286
287sub cf::player::attach {
288 my ($obj, $name, %arg) = @_;
289
290 _attach_attachment $obj, $name, %arg;
291}
292
293sub cf::map::attach {
294 my ($obj, $name, %arg) = @_;
295
296 _attach_attachment $obj, $name, %arg;
182} 297}
183 298
184sub attach_global { 299sub attach_global {
185 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 300 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
186} 301}
187 302
188sub attach_type { 303sub attach_to_type {
189 my $type = shift; 304 my $type = shift;
190 _attach @{$CB_TYPE[$type]}, KLASS_MAP, @_ 305 my $subtype = shift;
306
307 _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_
191} 308}
192 309
193sub attach_to_objects { 310sub attach_to_objects {
194 _attach @{$CB_CLASS{cf::object::wrap::}}, KLASS_OBJECT, @_ 311 _attach @CB_OBJECT, KLASS_OBJECT, @_
195} 312}
196 313
197sub attach_to_players { 314sub attach_to_players {
198 _attach @{$CB_CLASS{cf::player::wrap::}}, KLASS_PLAYER, @_ 315 _attach @CB_PLAYER, KLASS_PLAYER, @_
199} 316}
200 317
201sub attach_to_maps { 318sub attach_to_maps {
202 _attach @{$CB_CLASS{cf::map::wrap::}}, KLASS_MAP, @_ 319 _attach @CB_MAP, KLASS_MAP, @_
320}
321
322sub register_attachment {
323 my $name = shift;
324
325 $attachment{$name} = [[KLASS_OBJECT, @_]];
326}
327
328sub register_map_attachment {
329 my $name = shift;
330
331 $attachment{$name} = [[KLASS_MAP, @_]];
203} 332}
204 333
205our $override; 334our $override;
335our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
206 336
207sub override() { 337sub override {
208 $override = 1 338 $override = 1;
339 @invoke_results = ();
209} 340}
210 341
211sub invoke { 342sub do_invoke {
212 my $event = shift; 343 my $event = shift;
344 my $callbacks = shift;
213 345
214 my @cb; 346 @invoke_results = ();
215
216 if (my $ref = ref $_[0]) {
217 # 1. object-specific (NYI)
218 # 2. class-specific
219 push @cb, @{$CB_CLASS{$ref}[$event] || []};
220 }
221
222 # global
223 push @cb, @{$CB_GLOBAL[$event] || []};
224
225# warn "invoke id $EVENT[$event][0], args <@_> <=> @cb\n";#d#
226 347
227 local $override; 348 local $override;
228 349
229 for (@cb) { 350 for (@$callbacks) {
230 eval { &{$_->[1]} }; 351 eval { &{$_->[1]} };
231 352
232 if ($@) { 353 if ($@) {
233 warn "$@"; 354 warn "$@";
234 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; 355 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
240 361
241 0 362 0
242} 363}
243 364
244############################################################################# 365#############################################################################
366# object support
367
368sub instantiate {
369 my ($obj, $data) = @_;
370
371 $data = from_json $data;
372
373 for (@$data) {
374 my ($name, $args) = @$_;
375
376 $obj->attach ($name, %{$args || {} });
377 }
378}
379
380# basically do the same as instantiate, without calling instantiate
381sub reattach {
382 my ($obj) = @_;
383 my $registry = $obj->registry;
384
385 @$registry = ();
386
387 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
388
389 for my $name (keys %{ $obj->{_attachment} || {} }) {
390 if (my $attach = $attachment{$name}) {
391 for (@$attach) {
392 my ($klass, @attach) = @$_;
393 _attach @$registry, $klass, @attach;
394 }
395 } else {
396 warn "object uses attachment '$name' that is not available, postponing.\n";
397 }
398 }
399}
400
401sub object_freezer_save {
402 my ($filename, $objs) = @_;
403
404 if (@$objs) {
405 open my $fh, ">:raw", "$filename.pst~";
406 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
407 close $fh;
408 chmod SAVE_MODE, "$filename.pst~";
409 rename "$filename.pst~", "$filename.pst";
410 } else {
411 unlink "$filename.pst";
412 }
413
414 chmod SAVE_MODE, "$filename~";
415 rename "$filename~", $filename;
416}
417
418sub object_thawer_load {
419 my ($filename) = @_;
420
421 open my $fh, "<:raw:perlio", "$filename.pst"
422 or return;
423
424 eval { local $/; (Storable::thaw <$fh>)->{objs} }
425}
426
427attach_to_objects
428 prio => -1000000,
429 on_clone => sub {
430 my ($src, $dst) = @_;
431
432 @{$dst->registry} = @{$src->registry};
433
434 %$dst = %$src;
435
436 %{$dst->{_attachment}} = %{$src->{_attachment}}
437 if exists $src->{_attachment};
438 },
439;
440
441#############################################################################
245# old plug-in events 442# old plug-in events
246 443
247sub inject_event { 444sub inject_event {
248 my $extension = shift; 445 my $extension = shift;
249 my $event_code = shift; 446 my $event_code = shift;
293} 490}
294 491
295sub register { 492sub register {
296 my ($base, $pkg) = @_; 493 my ($base, $pkg) = @_;
297 494
298 for my $idx (0 .. $#PLUGIN_EVENT) { 495 #TODO
299 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
300 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
301 $hook[$idx]{$base} = $ref;
302 }
303 }
304} 496}
305 497
306sub load_extension { 498sub load_extension {
307 my ($path) = @_; 499 my ($path) = @_;
308 500
309 $path =~ /([^\/\\]+)\.ext$/ or die "$path"; 501 $path =~ /([^\/\\]+)\.ext$/ or die "$path";
310 my $base = $1; 502 my $base = $1;
311 my $pkg = $1; 503 my $pkg = $1;
312 $pkg =~ s/[^[:word:]]/_/g; 504 $pkg =~ s/[^[:word:]]/_/g;
313 $pkg = "cf::ext::$pkg"; 505 $pkg = "ext::$pkg";
314 506
315 warn "loading '$path' into '$pkg'\n"; 507 warn "loading '$path' into '$pkg'\n";
316 508
317 open my $fh, "<:utf8", $path 509 open my $fh, "<:utf8", $path
318 or die "$path: $!"; 510 or die "$path: $!";
339 my ($pkg) = @_; 531 my ($pkg) = @_;
340 532
341 warn "removing extension $pkg\n"; 533 warn "removing extension $pkg\n";
342 534
343 # remove hooks 535 # remove hooks
536 #TODO
344 for my $idx (0 .. $#PLUGIN_EVENT) { 537# for my $idx (0 .. $#PLUGIN_EVENT) {
345 delete $hook[$idx]{$pkg}; 538# delete $hook[$idx]{$pkg};
346 } 539# }
347 540
348 # remove commands 541 # remove commands
349 for my $name (keys %command) { 542 for my $name (keys %command) {
350 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 543 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
351 544
361 # remove extcmds 554 # remove extcmds
362 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 555 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
363 delete $extcmd{$name}; 556 delete $extcmd{$name};
364 } 557 }
365 558
366 if (my $cb = $pkg->can ("on_unload")) { 559 if (my $cb = $pkg->can ("unload")) {
367 eval { 560 eval {
368 $cb->($pkg); 561 $cb->($pkg);
369 1 562 1
370 } or warn "$pkg unloaded, but with errors: $@"; 563 } or warn "$pkg unloaded, but with errors: $@";
371 } 564 }
415 } 608 }
416 609
417 Symbol::delete_package $k; 610 Symbol::delete_package $k;
418 } 611 }
419 612
420 # 4. get rid of ext::, as good as possible 613 # 4. get rid of safe::, as good as possible
421 Symbol::delete_package "ext::$_" 614 Symbol::delete_package "safe::$_"
422 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); 615 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
423 616
424 # 5. remove register_script_function callbacks 617 # 5. remove register_script_function callbacks
425 # TODO 618 # TODO
426 619
427 # 6. unload cf.pm "a bit" 620 # 6. unload cf.pm "a bit"
428 delete $INC{"cf.pm"}; 621 delete $INC{"cf.pm"};
429 622
430 # don't, removes xs symbols, too 623 # don't, removes xs symbols, too,
624 # and global variables created in xs
431 #Symbol::delete_package __PACKAGE__; 625 #Symbol::delete_package __PACKAGE__;
432 626
433 # 7. reload cf.pm 627 # 7. reload cf.pm
434 $msg->("reloading cf.pm"); 628 $msg->("reloading cf.pm");
435 require cf; 629 require cf;
456 }; 650 };
457 } 651 }
458}; 652};
459 653
460############################################################################# 654#############################################################################
461# utility functions
462
463use JSON::Syck (); # TODO# replace by JSON::PC once working
464
465sub from_json($) {
466 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
467 JSON::Syck::Load $_[0]
468}
469
470sub to_json($) {
471 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
472 JSON::Syck::Dump $_[0]
473}
474
475#############################################################################
476# extcmd framework, basically convert ext <msg> 655# extcmd framework, basically convert ext <msg>
477# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 656# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
478 657
479sub on_extcmd { 658attach_to_players
659 on_extcmd => sub {
480 my ($pl, $buf) = @_; 660 my ($pl, $buf) = @_;
481 661
482 my $msg = eval { from_json $buf }; 662 my $msg = eval { from_json $buf };
483 663
484 if (ref $msg) { 664 if (ref $msg) {
485 if (my $cb = $extcmd{$msg->{msgtype}}) { 665 if (my $cb = $extcmd{$msg->{msgtype}}) {
486 if (my %reply = $cb->[0]->($pl, $msg)) { 666 if (my %reply = $cb->[0]->($pl, $msg)) {
487 $pl->ext_reply ($msg->{msgid}, %reply); 667 $pl->ext_reply ($msg->{msgid}, %reply);
668 }
488 } 669 }
670 } else {
671 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
489 } 672 }
490 } else { 673
491 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 674 cf::override;
492 } 675 },
493 676;
494 1
495}
496 677
497############################################################################# 678#############################################################################
498# load/save/clean perl data associated with a map 679# load/save/clean perl data associated with a map
499 680
500*cf::mapsupport::on_clean = sub { 681*cf::mapsupport::on_clean = sub {
501 my ($map) = @_; 682 my ($map) = @_;
502 683
503 my $path = $map->tmpname; 684 my $path = $map->tmpname;
504 defined $path or return; 685 defined $path or return;
505 686
506 unlink "$path.cfperl"; 687 unlink "$path.pst";
507}; 688};
508 689
690# old style persistent data, TODO: remove #d#
509*cf::mapsupport::on_swapin = 691*cf::mapsupport::on_swapin =
510*cf::mapsupport::on_load = sub { 692*cf::mapsupport::on_load = sub {
511 my ($map) = @_; 693 my ($map) = @_;
512 694
513 my $path = $map->tmpname; 695 my $path = $map->tmpname;
520 702
521 $data->{version} <= 1 703 $data->{version} <= 1
522 or return; # too new 704 or return; # too new
523 705
524 $map->_set_obs ($data->{obs}); 706 $map->_set_obs ($data->{obs});
525}; 707 $map->invoke (EVENT_MAP_UPGRADE);
526
527*cf::mapsupport::on_swapout = sub {
528 my ($map) = @_;
529
530 my $path = $map->tmpname;
531 $path = $map->path unless defined $path;
532
533 my $obs = $map->_get_obs;
534
535 if (defined $obs) {
536 open my $fh, ">:raw", "$path.cfperl"
537 or die "$path.cfperl: $!";
538
539 stat $path;
540
541 print $fh Storable::nfreeze {
542 size => (stat _)[7],
543 time => (stat _)[9],
544 version => 1,
545 obs => $obs,
546 };
547
548 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
549 } else {
550 unlink "$path.cfperl";
551 }
552}; 708};
553 709
554attach_to_maps prio => -10000, package => cf::mapsupport::; 710attach_to_maps prio => -10000, package => cf::mapsupport::;
555 711
556############################################################################# 712#############################################################################
569 $o->set_ob_key_value ("_perl_data"); 725 $o->set_ob_key_value ("_perl_data");
570 726
571 %$o = %{ Storable::thaw pack "H*", $value }; 727 %$o = %{ Storable::thaw pack "H*", $value };
572 } 728 }
573 } 729 }
574 },
575 on_save => sub {
576 my ($pl, $path) = @_;
577
578 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
579 for grep %$_, all_objects $pl->ob;
580 }, 730 },
581; 731;
582 732
583############################################################################# 733#############################################################################
584# core extensions - in perl 734# core extensions - in perl
632} 782}
633 783
634############################################################################# 784#############################################################################
635# map scripting support 785# map scripting support
636 786
637our $safe = new Safe "ext"; 787our $safe = new Safe "safe";
638our $safe_hole = new Safe::Hole; 788our $safe_hole = new Safe::Hole;
639 789
640$SIG{FPE} = 'IGNORE'; 790$SIG{FPE} = 'IGNORE';
641 791
642$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 792$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
648 ["cf::object::player" => qw(player)], 798 ["cf::object::player" => qw(player)],
649 ["cf::player" => qw(peaceful)], 799 ["cf::player" => qw(peaceful)],
650) { 800) {
651 no strict 'refs'; 801 no strict 'refs';
652 my ($pkg, @funs) = @$_; 802 my ($pkg, @funs) = @$_;
653 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 803 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
654 for @funs; 804 for @funs;
655} 805}
656 806
657sub safe_eval($;@) { 807sub safe_eval($;@) {
658 my ($code, %vars) = @_; 808 my ($code, %vars) = @_;
660 my $qcode = $code; 810 my $qcode = $code;
661 $qcode =~ s/"/‟/g; # not allowed in #line filenames 811 $qcode =~ s/"/‟/g; # not allowed in #line filenames
662 $qcode =~ s/\n/\\n/g; 812 $qcode =~ s/\n/\\n/g;
663 813
664 local $_; 814 local $_;
665 local @ext::cf::_safe_eval_args = values %vars; 815 local @safe::cf::_safe_eval_args = values %vars;
666 816
667 $code = 817 my $eval =
668 "do {\n" 818 "do {\n"
669 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 819 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
670 . "#line 0 \"{$qcode}\"\n" 820 . "#line 0 \"{$qcode}\"\n"
671 . $code 821 . $code
672 . "\n}" 822 . "\n}"
673 ; 823 ;
674 824
675 sub_generation_inc; 825 sub_generation_inc;
676 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); 826 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
677 sub_generation_inc; 827 sub_generation_inc;
828
829 if ($@) {
830 warn "$@";
831 warn "while executing safe code '$code'\n";
832 warn "with arguments " . (join " ", %vars) . "\n";
833 }
678 834
679 wantarray ? @res : $res[0] 835 wantarray ? @res : $res[0]
680} 836}
681 837
682sub register_script_function { 838sub register_script_function {
683 my ($fun, $cb) = @_; 839 my ($fun, $cb) = @_;
684 840
685 no strict 'refs'; 841 no strict 'refs';
686 *{"ext::$fun"} = $safe_hole->wrap ($cb); 842 *{"safe::$fun"} = $safe_hole->wrap ($cb);
687} 843}
688 844
689############################################################################# 845#############################################################################
690# the server's main() 846# the server's main()
691 847
717 $TICK_WATCHER->at ($NEXT_TICK); 873 $TICK_WATCHER->at ($NEXT_TICK);
718 $TICK_WATCHER->start; 874 $TICK_WATCHER->start;
719 }, 875 },
720); 876);
721 877
878_reload_2;
879
7221 8801
723 881

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines