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.57 by root, Wed Aug 30 11:21:24 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=head3 EVENTS AND OBJECT ATTACHMENTS
100 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
101=item cf::attach_global ... # NYI 140=item cf::attach_global ...
102 141
103=item cf::attach_to_type ... # NYI 142Attach handlers for global events.
104 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
105=item cf::attach_to_objects ... # NYI 181=item cf::attach_to_objects ...
106 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
107=item cf::attach_to_players ... # NYI 187=item cf::attach_to_players ...
108 188
189Attach handlers to all players.
190
109=item cf::attach_to_maps ... # NYI 191=item cf::attach_to_maps ...
110 192
111 prio => $number, # higehr is earlier 193Attach handlers to all maps.
112 on_xxx => \&cb, 194
113 package => package::, 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.
114 209
115=cut 210=cut
116 211
117our %CB_CLASS = (); # registry for class-based events 212# the following variables are defined in .xs and must not be re-created
118our @CB_GLOBAL = (); # registry for all global events 213our @CB_GLOBAL = (); # registry for all global events
214our @CB_OBJECT = (); # all objects (should not be used except in emergency)
215our @CB_PLAYER = ();
119our @CB_TYPE = (); # registry for type (cf-object class) based events 216our @CB_TYPE = (); # registry for type (cf-object class) based events
217our @CB_MAP = ();
218
219my %attachment;
120 220
121sub _attach_cb($\%$$$) { 221sub _attach_cb($\%$$$) {
122 my ($registry, $undo, $event, $prio, $cb) = @_; 222 my ($registry, $undo, $event, $prio, $cb) = @_;
123 223
124 use sort 'stable'; 224 use sort 'stable';
133} 233}
134 234
135# attach handles attaching event callbacks 235# attach handles attaching event callbacks
136# the only thing the caller has to do is pass the correct 236# the only thing the caller has to do is pass the correct
137# registry (== where the callback attaches to). 237# registry (== where the callback attaches to).
138sub _attach(\@$\@) { 238sub _attach(\@$@) {
139 my ($registry, $klass, $arg) = @_; 239 my ($registry, $klass, @arg) = @_;
140 240
141 my $prio = 0; 241 my $prio = 0;
142 242
143 my %undo = ( 243 my %undo = (
144 registry => $registry, 244 registry => $registry,
145 cb => [], 245 cb => [],
146 ); 246 );
147 247
148 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; 248 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
149 249
150 while (@$arg) { 250 while (@arg) {
151 my $type = shift @$arg; 251 my $type = shift @arg;
152 252
153 if ($type eq "prio") { 253 if ($type eq "prio") {
154 $prio = shift @$arg; 254 $prio = shift @arg;
155 255
156 } elsif ($type eq "package") { 256 } elsif ($type eq "package") {
157 my $pkg = shift @$arg; 257 my $pkg = shift @arg;
158 258
159 while (my ($name, $id) = each %cb_id) { 259 while (my ($name, $id) = each %cb_id) {
160 if (my $cb = $pkg->can ($name)) { 260 if (my $cb = $pkg->can ($name)) {
161 _attach_cb $registry, %undo, $id, $prio, $cb; 261 _attach_cb $registry, %undo, $id, $prio, $cb;
162 } 262 }
163 } 263 }
164 264
165 } elsif (exists $cb_id{$type}) { 265 } elsif (exists $cb_id{$type}) {
166 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; 266 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg;
167 267
168 } elsif (ref $type) { 268 } elsif (ref $type) {
169 warn "attaching objects not supported, ignoring.\n"; 269 warn "attaching objects not supported, ignoring.\n";
170 270
171 } else { 271 } else {
172 shift @$arg; 272 shift @arg;
173 warn "attach argument '$type' not supported, ignoring.\n"; 273 warn "attach argument '$type' not supported, ignoring.\n";
174 } 274 }
175 } 275 }
176 276
177 \%undo 277 \%undo
178} 278}
179 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
180sub cf::object::attach { 306*cf::object::attach =
181 die; 307*cf::player::attach =
182} 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};
183 331
184sub attach_global { 332sub attach_global {
185 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 333 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
186} 334}
187 335
188sub attach_type { 336sub attach_to_type {
189 my $type = shift; 337 my $type = shift;
190 _attach @{$CB_TYPE[$type]}, KLASS_MAP, @_ 338 my $subtype = shift;
339
340 _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_
191} 341}
192 342
193sub attach_to_objects { 343sub attach_to_objects {
194 _attach @{$CB_CLASS{cf::object::wrap::}}, KLASS_OBJECT, @_ 344 _attach @CB_OBJECT, KLASS_OBJECT, @_
195} 345}
196 346
197sub attach_to_players { 347sub attach_to_players {
198 _attach @{$CB_CLASS{cf::player::wrap::}}, KLASS_PLAYER, @_ 348 _attach @CB_PLAYER, KLASS_PLAYER, @_
199} 349}
200 350
201sub attach_to_maps { 351sub attach_to_maps {
202 _attach @{$CB_CLASS{cf::map::wrap::}}, KLASS_MAP, @_ 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, @_]];
203} 371}
204 372
205our $override; 373our $override;
374our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
206 375
207sub override() { 376sub override {
208 $override = 1 377 $override = 1;
378 @invoke_results = ();
209} 379}
210 380
211sub invoke { 381sub do_invoke {
212 my $event = shift; 382 my $event = shift;
383 my $callbacks = shift;
213 384
214 my @cb; 385 @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 386
227 local $override; 387 local $override;
228 388
229 for (@cb) { 389 for (@$callbacks) {
230 eval { &{$_->[1]} }; 390 eval { &{$_->[1]} };
231 391
232 if ($@) { 392 if ($@) {
233 warn "$@"; 393 warn "$@";
234 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; 394 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
239 } 399 }
240 400
241 0 401 0
242} 402}
243 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, $objs) = @_;
482
483 if (@$objs) {
484 open my $fh, ">:raw", "$filename.pst~";
485 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
486 close $fh;
487 unlink "$filename.cfperl";
488 chmod SAVE_MODE, "$filename.pst~";
489 rename "$filename.pst~", "$filename.pst";
490 } else {
491 unlink "$filename.pst";
492 }
493
494 chmod SAVE_MODE, "$filename~";
495 rename "$filename~", $filename;
496}
497
498sub object_thawer_load {
499 my ($filename) = @_;
500
501 open my $fh, "<:raw:perlio", "$filename.pst"
502 or return;
503
504 eval { local $/; (Storable::thaw <$fh>)->{objs} }
505}
506
507attach_to_objects
508 prio => -1000000,
509 on_clone => sub {
510 my ($src, $dst) = @_;
511
512 @{$dst->registry} = @{$src->registry};
513
514 %$dst = %$src;
515
516 %{$dst->{_attachment}} = %{$src->{_attachment}}
517 if exists $src->{_attachment};
518 },
519;
520
244############################################################################# 521#############################################################################
245# old plug-in events 522# old plug-in events
246 523
247sub inject_event { 524sub inject_event {
248 my $extension = shift; 525 my $extension = shift;
293} 570}
294 571
295sub register { 572sub register {
296 my ($base, $pkg) = @_; 573 my ($base, $pkg) = @_;
297 574
298 for my $idx (0 .. $#PLUGIN_EVENT) { 575 #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} 576}
305 577
306sub load_extension { 578sub load_extension {
307 my ($path) = @_; 579 my ($path) = @_;
308 580
309 $path =~ /([^\/\\]+)\.ext$/ or die "$path"; 581 $path =~ /([^\/\\]+)\.ext$/ or die "$path";
310 my $base = $1; 582 my $base = $1;
311 my $pkg = $1; 583 my $pkg = $1;
312 $pkg =~ s/[^[:word:]]/_/g; 584 $pkg =~ s/[^[:word:]]/_/g;
313 $pkg = "cf::ext::$pkg"; 585 $pkg = "ext::$pkg";
314 586
315 warn "loading '$path' into '$pkg'\n"; 587 warn "loading '$path' into '$pkg'\n";
316 588
317 open my $fh, "<:utf8", $path 589 open my $fh, "<:utf8", $path
318 or die "$path: $!"; 590 or die "$path: $!";
339 my ($pkg) = @_; 611 my ($pkg) = @_;
340 612
341 warn "removing extension $pkg\n"; 613 warn "removing extension $pkg\n";
342 614
343 # remove hooks 615 # remove hooks
616 #TODO
344 for my $idx (0 .. $#PLUGIN_EVENT) { 617# for my $idx (0 .. $#PLUGIN_EVENT) {
345 delete $hook[$idx]{$pkg}; 618# delete $hook[$idx]{$pkg};
346 } 619# }
347 620
348 # remove commands 621 # remove commands
349 for my $name (keys %command) { 622 for my $name (keys %command) {
350 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 623 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
351 624
361 # remove extcmds 634 # remove extcmds
362 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 635 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
363 delete $extcmd{$name}; 636 delete $extcmd{$name};
364 } 637 }
365 638
366 if (my $cb = $pkg->can ("on_unload")) { 639 if (my $cb = $pkg->can ("unload")) {
367 eval { 640 eval {
368 $cb->($pkg); 641 $cb->($pkg);
369 1 642 1
370 } or warn "$pkg unloaded, but with errors: $@"; 643 } or warn "$pkg unloaded, but with errors: $@";
371 } 644 }
415 } 688 }
416 689
417 Symbol::delete_package $k; 690 Symbol::delete_package $k;
418 } 691 }
419 692
420 # 4. get rid of ext::, as good as possible 693 # 4. get rid of safe::, as good as possible
421 Symbol::delete_package "ext::$_" 694 Symbol::delete_package "safe::$_"
422 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); 695 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
423 696
424 # 5. remove register_script_function callbacks 697 # 5. remove register_script_function callbacks
425 # TODO 698 # TODO
426 699
427 # 6. unload cf.pm "a bit" 700 # 6. unload cf.pm "a bit"
428 delete $INC{"cf.pm"}; 701 delete $INC{"cf.pm"};
429 702
430 # don't, removes xs symbols, too 703 # don't, removes xs symbols, too,
704 # and global variables created in xs
431 #Symbol::delete_package __PACKAGE__; 705 #Symbol::delete_package __PACKAGE__;
432 706
433 # 7. reload cf.pm 707 # 7. reload cf.pm
434 $msg->("reloading cf.pm"); 708 $msg->("reloading cf.pm");
435 require cf; 709 require cf;
456 }; 730 };
457 } 731 }
458}; 732};
459 733
460############################################################################# 734#############################################################################
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> 735# extcmd framework, basically convert ext <msg>
477# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 736# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
478 737
479sub on_extcmd { 738attach_to_players
739 on_extcmd => sub {
480 my ($pl, $buf) = @_; 740 my ($pl, $buf) = @_;
481 741
482 my $msg = eval { from_json $buf }; 742 my $msg = eval { from_json $buf };
483 743
484 if (ref $msg) { 744 if (ref $msg) {
485 if (my $cb = $extcmd{$msg->{msgtype}}) { 745 if (my $cb = $extcmd{$msg->{msgtype}}) {
486 if (my %reply = $cb->[0]->($pl, $msg)) { 746 if (my %reply = $cb->[0]->($pl, $msg)) {
487 $pl->ext_reply ($msg->{msgid}, %reply); 747 $pl->ext_reply ($msg->{msgid}, %reply);
748 }
488 } 749 }
750 } else {
751 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
489 } 752 }
490 } else { 753
491 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 754 cf::override;
492 } 755 },
493 756;
494 1
495}
496 757
497############################################################################# 758#############################################################################
498# load/save/clean perl data associated with a map 759# load/save/clean perl data associated with a map
499 760
500*cf::mapsupport::on_clean = sub { 761*cf::mapsupport::on_clean = sub {
501 my ($map) = @_; 762 my ($map) = @_;
502 763
503 my $path = $map->tmpname; 764 my $path = $map->tmpname;
504 defined $path or return; 765 defined $path or return;
505 766
506 unlink "$path.cfperl"; 767 unlink "$path.pst";
507}; 768};
508 769
770# old style persistent data, TODO: remove #d#
509*cf::mapsupport::on_swapin = 771*cf::mapsupport::on_swapin = sub {
510*cf::mapsupport::on_load = sub {
511 my ($map) = @_; 772 my ($map) = @_;
512 773
513 my $path = $map->tmpname; 774 my $path = $map->tmpname;
514 $path = $map->path unless defined $path; 775 $path = $map->path unless defined $path;
515 776
520 781
521 $data->{version} <= 1 782 $data->{version} <= 1
522 or return; # too new 783 or return; # too new
523 784
524 $map->_set_obs ($data->{obs}); 785 $map->_set_obs ($data->{obs});
525}; 786 $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}; 787};
553 788
554attach_to_maps prio => -10000, package => cf::mapsupport::; 789attach_to_maps prio => -10000, package => cf::mapsupport::;
555 790
556############################################################################# 791#############################################################################
569 $o->set_ob_key_value ("_perl_data"); 804 $o->set_ob_key_value ("_perl_data");
570 805
571 %$o = %{ Storable::thaw pack "H*", $value }; 806 %$o = %{ Storable::thaw pack "H*", $value };
572 } 807 }
573 } 808 }
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 }, 809 },
581; 810;
582 811
583############################################################################# 812#############################################################################
584# core extensions - in perl 813# core extensions - in perl
632} 861}
633 862
634############################################################################# 863#############################################################################
635# map scripting support 864# map scripting support
636 865
637our $safe = new Safe "ext"; 866our $safe = new Safe "safe";
638our $safe_hole = new Safe::Hole; 867our $safe_hole = new Safe::Hole;
639 868
640$SIG{FPE} = 'IGNORE'; 869$SIG{FPE} = 'IGNORE';
641 870
642$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 871$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
648 ["cf::object::player" => qw(player)], 877 ["cf::object::player" => qw(player)],
649 ["cf::player" => qw(peaceful)], 878 ["cf::player" => qw(peaceful)],
650) { 879) {
651 no strict 'refs'; 880 no strict 'refs';
652 my ($pkg, @funs) = @$_; 881 my ($pkg, @funs) = @$_;
653 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 882 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
654 for @funs; 883 for @funs;
655} 884}
656 885
657sub safe_eval($;@) { 886sub safe_eval($;@) {
658 my ($code, %vars) = @_; 887 my ($code, %vars) = @_;
660 my $qcode = $code; 889 my $qcode = $code;
661 $qcode =~ s/"/‟/g; # not allowed in #line filenames 890 $qcode =~ s/"/‟/g; # not allowed in #line filenames
662 $qcode =~ s/\n/\\n/g; 891 $qcode =~ s/\n/\\n/g;
663 892
664 local $_; 893 local $_;
665 local @ext::cf::_safe_eval_args = values %vars; 894 local @safe::cf::_safe_eval_args = values %vars;
666 895
667 $code = 896 my $eval =
668 "do {\n" 897 "do {\n"
669 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 898 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
670 . "#line 0 \"{$qcode}\"\n" 899 . "#line 0 \"{$qcode}\"\n"
671 . $code 900 . $code
672 . "\n}" 901 . "\n}"
673 ; 902 ;
674 903
675 sub_generation_inc; 904 sub_generation_inc;
676 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); 905 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
677 sub_generation_inc; 906 sub_generation_inc;
907
908 if ($@) {
909 warn "$@";
910 warn "while executing safe code '$code'\n";
911 warn "with arguments " . (join " ", %vars) . "\n";
912 }
678 913
679 wantarray ? @res : $res[0] 914 wantarray ? @res : $res[0]
680} 915}
681 916
682sub register_script_function { 917sub register_script_function {
683 my ($fun, $cb) = @_; 918 my ($fun, $cb) = @_;
684 919
685 no strict 'refs'; 920 no strict 'refs';
686 *{"ext::$fun"} = $safe_hole->wrap ($cb); 921 *{"safe::$fun"} = $safe_hole->wrap ($cb);
687} 922}
688 923
689############################################################################# 924#############################################################################
690# the server's main() 925# the server's main()
691 926
717 $TICK_WATCHER->at ($NEXT_TICK); 952 $TICK_WATCHER->at ($NEXT_TICK);
718 $TICK_WATCHER->start; 953 $TICK_WATCHER->start;
719 }, 954 },
720); 955);
721 956
957_reload_2;
958
7221 9591
723 960

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines