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.36 by root, Thu Aug 24 14:04:29 2006 UTC vs.
Revision 1.60 by root, Thu Aug 31 06:23:19 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 %PROP_TYPE; 20our %PROP_TYPE;
19our %PROP_IDX; 21our %PROP_IDX;
20our $LIBDIR = maps_directory "perl"; 22our $LIBDIR = maps_directory "perl";
21 23
22our $TICK = MAX_TIME * 1e-6; 24our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER; 25our $TICK_WATCHER;
24our $NEXT_TICK; 26our $NEXT_TICK;
25 27
26BEGIN { 28BEGIN {
27 @EVENT = map lc, @EVENT;
28
29 *CORE::GLOBAL::warn = sub { 29 *CORE::GLOBAL::warn = sub {
30 my $msg = join "", @_; 30 my $msg = join "", @_;
31 $msg .= "\n" 31 $msg .= "\n"
32 unless $msg =~ /\n$/; 32 unless $msg =~ /\n$/;
33 33
68prop_gen PLAYER_PROP => "cf::object::player"; 68prop_gen PLAYER_PROP => "cf::object::player";
69 69
70prop_gen MAP_PROP => "cf::map"; 70prop_gen MAP_PROP => "cf::map";
71prop_gen ARCH_PROP => "cf::arch"; 71prop_gen ARCH_PROP => "cf::arch";
72 72
73# guessed hierarchies
74
75@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 73@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
76@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
77 74
78# 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
79# within the Safe compartment. 76# within the Safe compartment.
80for 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)) {
81 no strict 'refs'; 78 no strict 'refs';
82 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 79 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
83} 80}
84 81
85$Event::DIED = sub { 82$Event::DIED = sub {
86 warn "error in event callback: @_"; 83 warn "error in event callback: @_";
87}; 84};
90my @exts; 87my @exts;
91my @hook; 88my @hook;
92my %command; 89my %command;
93my %extcmd; 90my %extcmd;
94 91
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#############################################################################
108# "new" plug-in system
109
110=head3 EVENTS AND OBJECT ATTACHMENTS
111
112=over 4
113
114=item $object->attach ($attachment, key => $value...)
115
116=item $object->detach ($attachment)
117
118Attach/detach a pre-registered attachment to an object.
119
120=item $player->attach ($attachment, key => $value...)
121
122=item $player->detach ($attachment)
123
124Attach/detach a pre-registered attachment to a player.
125
126=item $map->attach ($attachment, key => $value...)
127
128=item $map->detach ($attachment)
129
130Attach/detach a pre-registered attachment to a map.
131
132=item $bool = $object->attached ($name)
133
134=item $bool = $player->attached ($name)
135
136=item $bool = $map->attached ($name)
137
138Checks wether the named attachment is currently attached to the object.
139
140=item cf::attach_global ...
141
142Attach handlers for global events.
143
144This and all following C<attach_*>-functions expect any number of the
145following handler/hook descriptions:
146
147=over 4
148
149=item prio => $number
150
151Set the priority for all following handlers/hooks (unless overwritten
152by another C<prio> setting). Lower priority handlers get executed
153earlier. The default priority is C<0>, and many built-in handlers are
154registered at priority C<-1000>, so lower priorities should not be used
155unless you know what you are doing.
156
157=item on_I<event> => \&cb
158
159Call the given code reference whenever the named event happens (event is
160something like C<instantiate>, C<apply>, C<use_skill> and so on, and which
161handlers are recognised generally depends on the type of object these
162handlers attach to).
163
164See F<include/eventinc.h> for the full list of events supported, and their
165class.
166
167=item package => package::
168
169Look for sub functions of the name C<< on_I<event> >> in the given
170package and register them. Only handlers for eevents supported by the
171object/class are recognised.
172
173=back
174
175=item cf::attach_to_type $object_type, $subtype, ...
176
177Attach handlers for a specific object type (e.g. TRANSPORT) and
178subtype. If C<$subtype> is zero or undef, matches all objects of the given
179type.
180
181=item cf::attach_to_objects ...
182
183Attach handlers to all objects. Do not use this except for debugging or
184very rare events, as handlers are (obviously) called for I<all> objects in
185the game.
186
187=item cf::attach_to_players ...
188
189Attach handlers to all players.
190
191=item cf::attach_to_maps ...
192
193Attach handlers to all maps.
194
195=item cf:register_attachment $name, ...
196
197Register an attachment by name through which objects can refer to this
198attachment.
199
200=item cf:register_player_attachment $name, ...
201
202Register an attachment by name through which players can refer to this
203attachment.
204
205=item cf:register_map_attachment $name, ...
206
207Register an attachment by name through which maps can refer to this
208attachment.
209
210=cut
211
212# the following variables are defined in .xs and must not be re-created
213our @CB_GLOBAL = (); # registry for all global events
214our @CB_OBJECT = (); # all objects (should not be used except in emergency)
215our @CB_PLAYER = ();
216our @CB_TYPE = (); # registry for type (cf-object class) based events
217our @CB_MAP = ();
218
219my %attachment;
220
221sub _attach_cb($\%$$$) {
222 my ($registry, $undo, $event, $prio, $cb) = @_;
223
224 use sort 'stable';
225
226 $cb = [$prio, $cb];
227
228 @{$registry->[$event]} = sort
229 { $a->[0] cmp $b->[0] }
230 @{$registry->[$event] || []}, $cb;
231
232 push @{$undo->{cb}}, [$event, $cb];
233}
234
235# attach handles attaching event callbacks
236# the only thing the caller has to do is pass the correct
237# registry (== where the callback attaches to).
238sub _attach(\@$@) {
239 my ($registry, $klass, @arg) = @_;
240
241 my $prio = 0;
242
243 my %undo = (
244 registry => $registry,
245 cb => [],
246 );
247
248 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
249
250 while (@arg) {
251 my $type = shift @arg;
252
253 if ($type eq "prio") {
254 $prio = shift @arg;
255
256 } elsif ($type eq "package") {
257 my $pkg = shift @arg;
258
259 while (my ($name, $id) = each %cb_id) {
260 if (my $cb = $pkg->can ($name)) {
261 _attach_cb $registry, %undo, $id, $prio, $cb;
262 }
263 }
264
265 } elsif (exists $cb_id{$type}) {
266 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg;
267
268 } elsif (ref $type) {
269 warn "attaching objects not supported, ignoring.\n";
270
271 } else {
272 shift @arg;
273 warn "attach argument '$type' not supported, ignoring.\n";
274 }
275 }
276
277 \%undo
278}
279
280sub _attach_attachment {
281 my ($obj, $name, %arg) = @_;
282
283 return if exists $obj->{_attachment}{$name};
284
285 my $res;
286
287 if (my $attach = $attachment{$name}) {
288 my $registry = $obj->registry;
289
290 for (@$attach) {
291 my ($klass, @attach) = @$_;
292 $res = _attach @$registry, $klass, @attach;
293 }
294
295 $obj->{$name} = \%arg;
296 } else {
297 warn "object uses attachment '$name' that is not available, postponing.\n";
298 }
299
300 $obj->{_attachment}{$name} = undef;
301
302 $res->{attachment} = $name;
303 $res
304}
305
306*cf::object::attach =
307*cf::player::attach =
308*cf::map::attach = sub {
309 my ($obj, $name, %arg) = @_;
310
311 _attach_attachment $obj, $name, %arg;
312};
313
314# all those should be optimised
315*cf::object::detach =
316*cf::player::detach =
317*cf::map::detach = sub {
318 my ($obj, $name) = @_;
319
320 delete $obj->{_attachment}{$name};
321 reattach ($obj);
322};
323
324*cf::object::attached =
325*cf::player::attached =
326*cf::map::attached = sub {
327 my ($obj, $name) = @_;
328
329 exists $obj->{_attachment}{$name}
330};
331
332sub attach_global {
333 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
334}
335
336sub attach_to_type {
337 my $type = shift;
338 my $subtype = shift;
339
340 _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_
341}
342
343sub attach_to_objects {
344 _attach @CB_OBJECT, KLASS_OBJECT, @_
345}
346
347sub attach_to_players {
348 _attach @CB_PLAYER, KLASS_PLAYER, @_
349}
350
351sub attach_to_maps {
352 _attach @CB_MAP, KLASS_MAP, @_
353}
354
355sub register_attachment {
356 my $name = shift;
357
358 $attachment{$name} = [[KLASS_OBJECT, @_]];
359}
360
361sub register_player_attachment {
362 my $name = shift;
363
364 $attachment{$name} = [[KLASS_PLAYER, @_]];
365}
366
367sub register_map_attachment {
368 my $name = shift;
369
370 $attachment{$name} = [[KLASS_MAP, @_]];
371}
372
373our $override;
374our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
375
376sub override {
377 $override = 1;
378 @invoke_results = ();
379}
380
381sub do_invoke {
382 my $event = shift;
383 my $callbacks = shift;
384
385 @invoke_results = ();
386
387 local $override;
388
389 for (@$callbacks) {
390 eval { &{$_->[1]} };
391
392 if ($@) {
393 warn "$@";
394 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
395 override;
396 }
397
398 return 1 if $override;
399 }
400
401 0
402}
403
404=item $bool = cf::invoke EVENT_GLOBAL_XXX, ...
405
406=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
407
408=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
409
410=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
411
412Generate a global/object/player/map-specific event with the given arguments.
413
414This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be
415removed in future versions), and there is no public API to access override
416results (if you must, access C<@cf::invoke_results> directly).
417
418=back
419
420=head2 methods valid for all pointers
421
422=over 4
423
424=item $object->valid
425
426=item $player->valid
427
428=item $map->valid
429
430Just because you have a perl object does not mean that the corresponding
431C-level object still exists. If you try to access an object that has no
432valid C counterpart anymore you get an exception at runtime. This method
433can be used to test for existence of the C object part without causing an
434exception.
435
436=back
437
438=cut
439
440*cf::object::valid =
441*cf::player::valid =
442*cf::map::valid = \&cf::_valid;
443
444#############################################################################
445# object support
446
447sub instantiate {
448 my ($obj, $data) = @_;
449
450 $data = from_json $data;
451
452 for (@$data) {
453 my ($name, $args) = @$_;
454
455 $obj->attach ($name, %{$args || {} });
456 }
457}
458
459# basically do the same as instantiate, without calling instantiate
460sub reattach {
461 my ($obj) = @_;
462 my $registry = $obj->registry;
463
464 @$registry = ();
465
466 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
467
468 for my $name (keys %{ $obj->{_attachment} || {} }) {
469 if (my $attach = $attachment{$name}) {
470 for (@$attach) {
471 my ($klass, @attach) = @$_;
472 _attach @$registry, $klass, @attach;
473 }
474 } else {
475 warn "object uses attachment '$name' that is not available, postponing.\n";
476 }
477 }
478}
479
480sub object_freezer_save {
481 my ($filename, $rdata, $objs) = @_;
482
483 if (length $$rdata) {
484 warn sprintf "saving %s (%d,%d)\n",
485 $filename, length $$rdata, scalar @$objs;
486
487 if (open my $fh, ">:raw", "$filename~") {
488 chmod SAVE_MODE, $fh;
489 syswrite $fh, $$rdata;
490 close $fh;
491
492 if (@$objs && open my $fh, ">:raw", "$filename.pst~") {
493 chmod SAVE_MODE, $fh;
494 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
495 close $fh;
496 rename "$filename.pst~", "$filename.pst";
497 } else {
498 unlink "$filename.pst";
499 }
500
501 rename "$filename~", $filename;
502 } else {
503 warn "FATAL: $filename~: $!\n";
504 }
505 } else {
506 unlink $filename;
507 unlink "$filename.pst";
508 }
509}
510
511sub object_thawer_load {
512 my ($filename) = @_;
513
514 open my $fh, "<:raw:perlio", "$filename.pst"
515 or return;
516
517 eval { local $/; (Storable::thaw <$fh>)->{objs} }
518}
519
520attach_to_objects
521 prio => -1000000,
522 on_clone => sub {
523 my ($src, $dst) = @_;
524
525 @{$dst->registry} = @{$src->registry};
526
527 %$dst = %$src;
528
529 %{$dst->{_attachment}} = %{$src->{_attachment}}
530 if exists $src->{_attachment};
531 },
532;
533
534#############################################################################
535# old plug-in events
536
95sub inject_event { 537sub inject_event {
96 my $extension = shift; 538 my $extension = shift;
97 my $event_code = shift; 539 my $event_code = shift;
98 540
99 my $cb = $hook[$event_code]{$extension} 541 my $cb = $hook[$event_code]{$extension}
141} 583}
142 584
143sub register { 585sub register {
144 my ($base, $pkg) = @_; 586 my ($base, $pkg) = @_;
145 587
146 for my $idx (0 .. $#EVENT) { 588 #TODO
147 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) {
148 #warn "registering $EVENT[$idx] hook to '$pkg'\n";
149 $hook[$idx]{$base} = $ref;
150 }
151 }
152} 589}
153 590
154sub load_extension { 591sub load_extension {
155 my ($path) = @_; 592 my ($path) = @_;
156 593
157 $path =~ /([^\/\\]+)\.ext$/ or die "$path"; 594 $path =~ /([^\/\\]+)\.ext$/ or die "$path";
158 my $base = $1; 595 my $base = $1;
159 my $pkg = $1; 596 my $pkg = $1;
160 $pkg =~ s/[^[:word:]]/_/g; 597 $pkg =~ s/[^[:word:]]/_/g;
161 $pkg = "cf::ext::$pkg"; 598 $pkg = "ext::$pkg";
162 599
163 warn "loading '$path' into '$pkg'\n"; 600 warn "loading '$path' into '$pkg'\n";
164 601
165 open my $fh, "<:utf8", $path 602 open my $fh, "<:utf8", $path
166 or die "$path: $!"; 603 or die "$path: $!";
187 my ($pkg) = @_; 624 my ($pkg) = @_;
188 625
189 warn "removing extension $pkg\n"; 626 warn "removing extension $pkg\n";
190 627
191 # remove hooks 628 # remove hooks
629 #TODO
192 for my $idx (0 .. $#EVENT) { 630# for my $idx (0 .. $#PLUGIN_EVENT) {
193 delete $hook[$idx]{$pkg}; 631# delete $hook[$idx]{$pkg};
194 } 632# }
195 633
196 # remove commands 634 # remove commands
197 for my $name (keys %command) { 635 for my $name (keys %command) {
198 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 636 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
199 637
209 # remove extcmds 647 # remove extcmds
210 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 648 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
211 delete $extcmd{$name}; 649 delete $extcmd{$name};
212 } 650 }
213 651
214 if (my $cb = $pkg->can ("on_unload")) { 652 if (my $cb = $pkg->can ("unload")) {
215 eval { 653 eval {
216 $cb->($pkg); 654 $cb->($pkg);
217 1 655 1
218 } or warn "$pkg unloaded, but with errors: $@"; 656 } or warn "$pkg unloaded, but with errors: $@";
219 } 657 }
263 } 701 }
264 702
265 Symbol::delete_package $k; 703 Symbol::delete_package $k;
266 } 704 }
267 705
268 # 4. get rid of ext::, as good as possible 706 # 4. get rid of safe::, as good as possible
269 Symbol::delete_package "ext::$_" 707 Symbol::delete_package "safe::$_"
270 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); 708 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
271 709
272 # 5. remove register_script_function callbacks 710 # 5. remove register_script_function callbacks
273 # TODO 711 # TODO
274 712
275 # 6. unload cf.pm "a bit" 713 # 6. unload cf.pm "a bit"
276 delete $INC{"cf.pm"}; 714 delete $INC{"cf.pm"};
277 715
278 # don't, removes xs symbols, too 716 # don't, removes xs symbols, too,
717 # and global variables created in xs
279 #Symbol::delete_package __PACKAGE__; 718 #Symbol::delete_package __PACKAGE__;
280 719
281 # 7. reload cf.pm 720 # 7. reload cf.pm
282 $msg->("reloading cf.pm"); 721 $msg->("reloading cf.pm");
283 require cf; 722 require cf;
304 }; 743 };
305 } 744 }
306}; 745};
307 746
308############################################################################# 747#############################################################################
309# utility functions
310
311use JSON::Syck (); # TODO# replace by JSON::PC once working
312
313sub from_json($) {
314 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
315 JSON::Syck::Load $_[0]
316}
317
318sub to_json($) {
319 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
320 JSON::Syck::Dump $_[0]
321}
322
323#############################################################################
324# extcmd framework, basically convert ext <msg> 748# extcmd framework, basically convert ext <msg>
325# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 749# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
326 750
327sub on_extcmd { 751attach_to_players
752 on_extcmd => sub {
328 my ($pl, $buf) = @_; 753 my ($pl, $buf) = @_;
329 754
330 my $msg = eval { from_json $buf }; 755 my $msg = eval { from_json $buf };
331 756
332 if (ref $msg) { 757 if (ref $msg) {
333 if (my $cb = $extcmd{$msg->{msgtype}}) { 758 if (my $cb = $extcmd{$msg->{msgtype}}) {
334 if (my %reply = $cb->[0]->($pl, $msg)) { 759 if (my %reply = $cb->[0]->($pl, $msg)) {
335 $pl->ext_reply ($msg->{msgid}, %reply); 760 $pl->ext_reply ($msg->{msgid}, %reply);
761 }
762 }
763 } else {
764 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
765 }
766
767 cf::override;
768 },
769;
770
771#############################################################################
772# load/save/clean perl data associated with a map
773
774*cf::mapsupport::on_clean = sub {
775 my ($map) = @_;
776
777 my $path = $map->tmpname;
778 defined $path or return;
779
780 unlink "$path.pst";
781};
782
783attach_to_maps prio => -10000, package => cf::mapsupport::;
784
785#############################################################################
786# load/save perl data associated with player->ob objects
787
788sub all_objects(@) {
789 @_, map all_objects ($_->inv), @_
790}
791
792# TODO: compatibility cruft, remove when no longer needed
793attach_to_players
794 on_load => sub {
795 my ($pl, $path) = @_;
796
797 for my $o (all_objects $pl->ob) {
798 if (my $value = $o->get_ob_key_value ("_perl_data")) {
799 $o->set_ob_key_value ("_perl_data");
800
801 %$o = %{ Storable::thaw pack "H*", $value };
336 } 802 }
337 } 803 }
338 } else {
339 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
340 } 804 },
341 805;
342 1
343}
344
345#############################################################################
346# load/save/clean perl data associated with a map
347
348*on_mapclean = sub {
349 my ($map) = @_;
350
351 my $path = $map->tmpname;
352 defined $path or return;
353
354 unlink "$path.cfperl";
355};
356
357*on_mapin =
358*on_mapload = sub {
359 my ($map) = @_;
360
361 my $path = $map->tmpname;
362 $path = $map->path unless defined $path;
363
364 open my $fh, "<:raw", "$path.cfperl"
365 or return; # no perl data
366
367 my $data = Storable::thaw do { local $/; <$fh> };
368
369 $data->{version} <= 1
370 or return; # too new
371
372 $map->_set_obs ($data->{obs});
373};
374
375*on_mapout = sub {
376 my ($map) = @_;
377
378 my $path = $map->tmpname;
379 $path = $map->path unless defined $path;
380
381 my $obs = $map->_get_obs;
382
383 if (defined $obs) {
384 open my $fh, ">:raw", "$path.cfperl"
385 or die "$path.cfperl: $!";
386
387 stat $path;
388
389 print $fh Storable::nfreeze {
390 size => (stat _)[7],
391 time => (stat _)[9],
392 version => 1,
393 obs => $obs,
394 };
395
396 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
397 } else {
398 unlink "$path.cfperl";
399 }
400};
401
402#############################################################################
403# load/save perl data associated with player->ob objects
404
405sub all_objects(@) {
406 @_, map all_objects ($_->inv), @_
407}
408
409*on_player_load = sub {
410 my ($ob, $path) = @_;
411
412 for my $o (all_objects $ob) {
413 if (my $value = $o->get_ob_key_value ("_perl_data")) {
414 $o->set_ob_key_value ("_perl_data");
415
416 %$o = %{ Storable::thaw pack "H*", $value };
417 }
418 }
419};
420
421*on_player_save = sub {
422 my ($ob, $path) = @_;
423
424 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
425 for grep %$_, all_objects $ob;
426};
427 806
428############################################################################# 807#############################################################################
429# core extensions - in perl 808# core extensions - in perl
430 809
431=item cf::player::exists $login 810=item cf::player::exists $login
477} 856}
478 857
479############################################################################# 858#############################################################################
480# map scripting support 859# map scripting support
481 860
482our $safe = new Safe "ext"; 861our $safe = new Safe "safe";
483our $safe_hole = new Safe::Hole; 862our $safe_hole = new Safe::Hole;
484 863
485$SIG{FPE} = 'IGNORE'; 864$SIG{FPE} = 'IGNORE';
486 865
487$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 866$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
493 ["cf::object::player" => qw(player)], 872 ["cf::object::player" => qw(player)],
494 ["cf::player" => qw(peaceful)], 873 ["cf::player" => qw(peaceful)],
495) { 874) {
496 no strict 'refs'; 875 no strict 'refs';
497 my ($pkg, @funs) = @$_; 876 my ($pkg, @funs) = @$_;
498 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 877 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
499 for @funs; 878 for @funs;
500} 879}
501 880
502sub safe_eval($;@) { 881sub safe_eval($;@) {
503 my ($code, %vars) = @_; 882 my ($code, %vars) = @_;
505 my $qcode = $code; 884 my $qcode = $code;
506 $qcode =~ s/"/‟/g; # not allowed in #line filenames 885 $qcode =~ s/"/‟/g; # not allowed in #line filenames
507 $qcode =~ s/\n/\\n/g; 886 $qcode =~ s/\n/\\n/g;
508 887
509 local $_; 888 local $_;
510 local @ext::cf::_safe_eval_args = values %vars; 889 local @safe::cf::_safe_eval_args = values %vars;
511 890
512 $code = 891 my $eval =
513 "do {\n" 892 "do {\n"
514 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 893 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
515 . "#line 0 \"{$qcode}\"\n" 894 . "#line 0 \"{$qcode}\"\n"
516 . $code 895 . $code
517 . "\n}" 896 . "\n}"
518 ; 897 ;
519 898
520 sub_generation_inc; 899 sub_generation_inc;
521 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); 900 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
522 sub_generation_inc; 901 sub_generation_inc;
902
903 if ($@) {
904 warn "$@";
905 warn "while executing safe code '$code'\n";
906 warn "with arguments " . (join " ", %vars) . "\n";
907 }
523 908
524 wantarray ? @res : $res[0] 909 wantarray ? @res : $res[0]
525} 910}
526 911
527sub register_script_function { 912sub register_script_function {
528 my ($fun, $cb) = @_; 913 my ($fun, $cb) = @_;
529 914
530 no strict 'refs'; 915 no strict 'refs';
531 *{"ext::$fun"} = $safe_hole->wrap ($cb); 916 *{"safe::$fun"} = $safe_hole->wrap ($cb);
532} 917}
533 918
534############################################################################# 919#############################################################################
535# the server's main() 920# the server's main()
536 921
537sub run { 922sub main {
538 Event::loop; 923 Event::loop;
539} 924}
540 925
541############################################################################# 926#############################################################################
542# initialisation 927# initialisation
554 cf::server_tick; # one server iteration 939 cf::server_tick; # one server iteration
555 940
556 my $NOW = Event::time; 941 my $NOW = Event::time;
557 $NEXT_TICK += $TICK; 942 $NEXT_TICK += $TICK;
558 943
559 # if we are delayed by > 0.25 second, skip ticks 944 # if we are delayed by four ticks, skip them all
560 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25; 945 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
561 946
562 $TICK_WATCHER->at ($NEXT_TICK); 947 $TICK_WATCHER->at ($NEXT_TICK);
563 $TICK_WATCHER->start; 948 $TICK_WATCHER->start;
564 }, 949 },
565); 950);
566 951
952_reload_2;
953
5671 9541
568 955

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines