… | |
… | |
72 | |
72 | |
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
74 | |
74 | |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
76 | # within the Safe compartment. |
76 | # within the Safe compartment. |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
78 | no strict 'refs'; |
78 | no strict 'refs'; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
80 | } |
80 | } |
81 | |
81 | |
82 | $Event::DIED = sub { |
82 | $Event::DIED = sub { |
… | |
… | |
105 | } |
105 | } |
106 | |
106 | |
107 | ############################################################################# |
107 | ############################################################################# |
108 | # "new" plug-in system |
108 | # "new" plug-in system |
109 | |
109 | |
110 | =item $object->attach ($attachment, ...) |
110 | =item $object->attach ($attachment, key => $value...) |
111 | |
111 | |
112 | Attach a pre-registered attachment to an object. |
112 | Attach a pre-registered attachment to an object. |
113 | |
113 | |
114 | =item $player->attach ($attachment, ...) |
114 | =item $player->attach ($attachment, key => $value...) |
115 | |
115 | |
116 | Attach a pre-registered attachment to a player. |
116 | Attach a pre-registered attachment to a player. |
117 | |
117 | |
118 | =item $map->attach ($attachment, ...) # not yet persistent |
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
119 | |
119 | |
120 | Attach a pre-registered attachment to a map. |
120 | Attach a pre-registered attachment to a map. |
121 | |
121 | |
122 | =item cf::attach_global ... |
122 | =item cf::attach_global ... |
123 | |
123 | |
… | |
… | |
245 | |
245 | |
246 | \%undo |
246 | \%undo |
247 | } |
247 | } |
248 | |
248 | |
249 | sub _attach_attachment { |
249 | sub _attach_attachment { |
250 | my ($obj, $name, @args) = @_; |
250 | my ($obj, $name, %arg) = @_; |
251 | |
251 | |
252 | my $res; |
252 | my $res; |
253 | |
253 | |
254 | if (my $attach = $attachment{$name}) { |
254 | if (my $attach = $attachment{$name}) { |
255 | my $registry = $obj->registry; |
255 | my $registry = $obj->registry; |
… | |
… | |
257 | for (@$attach) { |
257 | for (@$attach) { |
258 | my ($klass, @attach) = @$_; |
258 | my ($klass, @attach) = @$_; |
259 | $res = _attach @$registry, $klass, @attach; |
259 | $res = _attach @$registry, $klass, @attach; |
260 | } |
260 | } |
261 | |
261 | |
262 | if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { |
262 | $obj->{$name} = \%arg; |
263 | for (@$cb) { |
|
|
264 | eval { $_->[1]->($obj, @args); }; |
|
|
265 | if ($@) { |
|
|
266 | warn "$@"; |
|
|
267 | warn "... while processing '$name' instantiate with args <@args>.\n"; |
|
|
268 | } |
|
|
269 | } |
|
|
270 | } |
|
|
271 | } else { |
263 | } else { |
272 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
264 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
273 | } |
265 | } |
274 | |
266 | |
275 | push @{$obj->{_attachment}}, $name; |
267 | $obj->{_attachment}{$name} = undef; |
276 | |
268 | |
277 | $res->{attachment} = $name; |
269 | $res->{attachment} = $name; |
278 | $res |
270 | $res |
279 | } |
271 | } |
280 | |
272 | |
281 | sub cf::object::attach { |
273 | sub cf::object::attach { |
282 | my ($obj, $name, @args) = @_; |
274 | my ($obj, $name, %arg) = @_; |
283 | |
275 | |
284 | _attach_attachment $obj, $name, @args; |
276 | _attach_attachment $obj, $name, %arg; |
285 | } |
277 | } |
286 | |
278 | |
287 | sub cf::player::attach { |
279 | sub cf::player::attach { |
288 | my ($obj, $name, @args) = @_; |
280 | my ($obj, $name, %arg) = @_; |
289 | |
281 | |
290 | _attach_attachment KLASS_PLAYER, $obj, $name, @args; |
282 | _attach_attachment KLASS_PLAYER, $obj, $name, %arg; |
291 | } |
283 | } |
292 | |
284 | |
293 | sub cf::map::attach { |
285 | sub cf::map::attach { |
294 | my ($obj, $name, @args) = @_; |
286 | my ($obj, $name, %arg) = @_; |
295 | |
287 | |
296 | _attach_attachment KLASS_MAP, $obj, $name, @args; |
288 | _attach_attachment KLASS_MAP, $obj, $name, %arg; |
297 | } |
289 | } |
298 | |
290 | |
299 | sub attach_global { |
291 | sub attach_global { |
300 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
292 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
301 | } |
293 | } |
… | |
… | |
364 | |
356 | |
365 | $data = from_json $data; |
357 | $data = from_json $data; |
366 | |
358 | |
367 | for (@$data) { |
359 | for (@$data) { |
368 | my ($name, $args) = @$_; |
360 | my ($name, $args) = @$_; |
369 | attach $obj, $name, @{$args || [] }; |
361 | |
|
|
362 | $obj->attach ($name, %{$args || {} }); |
370 | } |
363 | } |
371 | } |
364 | } |
372 | |
365 | |
373 | # basically do the same as instantiate, without calling instantiate |
366 | # basically do the same as instantiate, without calling instantiate |
374 | sub reattach { |
367 | sub reattach { |
375 | my ($obj) = @_; |
368 | my ($obj) = @_; |
376 | my $registry = $obj->registry; |
369 | my $registry = $obj->registry; |
377 | |
370 | |
378 | @$registry = (); |
371 | @$registry = (); |
379 | |
372 | |
|
|
373 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
|
|
374 | |
380 | for my $name (@{ $obj->{_attachment} }) { |
375 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
381 | if (my $attach = $attachment{$name}) { |
376 | if (my $attach = $attachment{$name}) { |
382 | for (@$attach) { |
377 | for (@$attach) { |
383 | my ($klass, @attach) = @$_; |
378 | my ($klass, @attach) = @$_; |
384 | _attach @$registry, $klass, @attach; |
379 | _attach @$registry, $klass, @attach; |
385 | } |
380 | } |
… | |
… | |
390 | } |
385 | } |
391 | |
386 | |
392 | sub object_freezer_save { |
387 | sub object_freezer_save { |
393 | my ($filename, $objs) = @_; |
388 | my ($filename, $objs) = @_; |
394 | |
389 | |
395 | $filename .= ".pst"; |
|
|
396 | |
|
|
397 | if (@$objs) { |
390 | if (@$objs) { |
398 | open my $fh, ">:raw", "$filename~"; |
391 | open my $fh, ">:raw", "$filename.pst~"; |
399 | chmod $fh, SAVE_MODE; |
|
|
400 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
392 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
401 | close $fh; |
393 | close $fh; |
|
|
394 | chmod SAVE_MODE, "$filename.pst~"; |
402 | rename "$filename~", $filename; |
395 | rename "$filename.pst~", "$filename.pst"; |
403 | } else { |
396 | } else { |
404 | unlink $filename; |
397 | unlink "$filename.pst"; |
405 | } |
398 | } |
|
|
399 | |
|
|
400 | chmod SAVE_MODE, "$filename~"; |
|
|
401 | rename "$filename~", $filename; |
406 | } |
402 | } |
407 | |
403 | |
408 | sub object_thawer_load { |
404 | sub object_thawer_load { |
409 | my ($filename) = @_; |
405 | my ($filename) = @_; |
410 | |
406 | |
… | |
… | |
421 | |
417 | |
422 | @{$dst->registry} = @{$src->registry}; |
418 | @{$dst->registry} = @{$src->registry}; |
423 | |
419 | |
424 | %$dst = %$src; |
420 | %$dst = %$src; |
425 | |
421 | |
426 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
422 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
427 | if exists $src->{_attachment}; |
423 | if exists $src->{_attachment}; |
428 | }, |
424 | }, |
429 | ; |
425 | ; |
430 | |
426 | |
431 | ############################################################################# |
427 | ############################################################################# |
… | |
… | |
672 | my ($map) = @_; |
668 | my ($map) = @_; |
673 | |
669 | |
674 | my $path = $map->tmpname; |
670 | my $path = $map->tmpname; |
675 | defined $path or return; |
671 | defined $path or return; |
676 | |
672 | |
677 | unlink "$path.cfperl"; |
|
|
678 | unlink "$path.pst"; |
673 | unlink "$path.pst"; |
679 | }; |
674 | }; |
680 | |
675 | |
|
|
676 | # old style persistent data, TODO: remove #d# |
681 | *cf::mapsupport::on_swapin = |
677 | *cf::mapsupport::on_swapin = |
682 | *cf::mapsupport::on_load = sub { |
678 | *cf::mapsupport::on_load = sub { |
683 | my ($map) = @_; |
679 | my ($map) = @_; |
684 | |
680 | |
685 | my $path = $map->tmpname; |
681 | my $path = $map->tmpname; |
… | |
… | |
692 | |
688 | |
693 | $data->{version} <= 1 |
689 | $data->{version} <= 1 |
694 | or return; # too new |
690 | or return; # too new |
695 | |
691 | |
696 | $map->_set_obs ($data->{obs}); |
692 | $map->_set_obs ($data->{obs}); |
|
|
693 | $map->invoke (EVENT_MAP_UPGRADE); |
697 | }; |
694 | }; |
698 | |
695 | |
699 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
696 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
700 | |
697 | |
701 | ############################################################################# |
698 | ############################################################################# |