… | |
… | |
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 { |
… | |
… | |
107 | ############################################################################# |
107 | ############################################################################# |
108 | # "new" plug-in system |
108 | # "new" plug-in system |
109 | |
109 | |
110 | =item $object->attach ($attachment, key => $value...) |
110 | =item $object->attach ($attachment, key => $value...) |
111 | |
111 | |
|
|
112 | =item $object->detach ($attachment) |
|
|
113 | |
112 | Attach a pre-registered attachment to an object. |
114 | Attach/detach a pre-registered attachment to an object. |
113 | |
115 | |
114 | =item $player->attach ($attachment, key => $value...) |
116 | =item $player->attach ($attachment, key => $value...) |
115 | |
117 | |
|
|
118 | =item $player->detach ($attachment) |
|
|
119 | |
116 | Attach a pre-registered attachment to a player. |
120 | Attach/detach a pre-registered attachment to a player. |
117 | |
121 | |
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
122 | =item $map->attach ($attachment, key => $value...) |
119 | |
123 | |
|
|
124 | =item $map->detach ($attachment) |
|
|
125 | |
120 | Attach a pre-registered attachment to a map. |
126 | Attach/detach a pre-registered attachment to a map. |
121 | |
127 | |
122 | =item cf::attach_global ... |
128 | =item cf::attach_global ... |
123 | |
129 | |
124 | Attach handlers for global events. |
130 | Attach handlers for global events. |
125 | |
131 | |
… | |
… | |
173 | =item cf::attach_to_maps ... |
179 | =item cf::attach_to_maps ... |
174 | |
180 | |
175 | Attach handlers to all maps. |
181 | Attach handlers to all maps. |
176 | |
182 | |
177 | =item cf:register_attachment $name, ... |
183 | =item cf:register_attachment $name, ... |
|
|
184 | |
|
|
185 | Register an attachment by name through which objects can refer to this |
|
|
186 | attachment. |
|
|
187 | |
|
|
188 | =item cf:register_map_attachment $name, ... |
|
|
189 | |
|
|
190 | Register an attachment by name through which maps can refer to this |
|
|
191 | attachment. |
178 | |
192 | |
179 | =cut |
193 | =cut |
180 | |
194 | |
181 | # the following variables are defined in .xs and must not be re-created |
195 | # the following variables are defined in .xs and must not be re-created |
182 | our @CB_GLOBAL = (); # registry for all global events |
196 | our @CB_GLOBAL = (); # registry for all global events |
… | |
… | |
262 | $obj->{$name} = \%arg; |
276 | $obj->{$name} = \%arg; |
263 | } else { |
277 | } else { |
264 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
278 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
265 | } |
279 | } |
266 | |
280 | |
267 | push @{$obj->{_attachment}}, $name; |
281 | $obj->{_attachment}{$name} = undef; |
268 | |
282 | |
269 | $res->{attachment} = $name; |
283 | $res->{attachment} = $name; |
270 | $res |
284 | $res |
271 | } |
285 | } |
272 | |
286 | |
273 | sub cf::object::attach { |
287 | *cf::object::attach = |
|
|
288 | *cf::player::attach = |
|
|
289 | *cf::map::attach = sub { |
274 | my ($obj, $name, %arg) = @_; |
290 | my ($obj, $name, %arg) = @_; |
275 | |
291 | |
276 | _attach_attachment $obj, $name, %arg; |
292 | _attach_attachment $obj, $name, %arg; |
277 | } |
293 | } |
278 | |
294 | |
|
|
295 | # all those should be optimised |
|
|
296 | *cf::object::detach = |
279 | sub cf::player::attach { |
297 | *cf::player::detach = |
|
|
298 | *cf::map::detach = sub { |
280 | my ($obj, $name, %arg) = @_; |
299 | my ($obj, $name) = @_; |
281 | |
300 | |
282 | _attach_attachment KLASS_PLAYER, $obj, $name, %arg; |
301 | delete $obj->{_attachment}{$name}; |
283 | } |
302 | reattach $obj; |
284 | |
303 | }; |
285 | sub cf::map::attach { |
|
|
286 | my ($obj, $name, %arg) = @_; |
|
|
287 | |
|
|
288 | _attach_attachment KLASS_MAP, $obj, $name, %arg; |
|
|
289 | } |
|
|
290 | |
304 | |
291 | sub attach_global { |
305 | sub attach_global { |
292 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
306 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
293 | } |
307 | } |
294 | |
308 | |
… | |
… | |
313 | |
327 | |
314 | sub register_attachment { |
328 | sub register_attachment { |
315 | my $name = shift; |
329 | my $name = shift; |
316 | |
330 | |
317 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
331 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
332 | } |
|
|
333 | |
|
|
334 | sub register_map_attachment { |
|
|
335 | my $name = shift; |
|
|
336 | |
|
|
337 | $attachment{$name} = [[KLASS_MAP, @_]]; |
318 | } |
338 | } |
319 | |
339 | |
320 | our $override; |
340 | our $override; |
321 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
341 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
322 | |
342 | |
… | |
… | |
368 | my ($obj) = @_; |
388 | my ($obj) = @_; |
369 | my $registry = $obj->registry; |
389 | my $registry = $obj->registry; |
370 | |
390 | |
371 | @$registry = (); |
391 | @$registry = (); |
372 | |
392 | |
373 | delete $obj->{_attachment} unless @{ $obj->{_attachment} || [] }; |
393 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
374 | |
394 | |
375 | for my $name (@{ $obj->{_attachment} || [] }) { |
395 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
376 | if (my $attach = $attachment{$name}) { |
396 | if (my $attach = $attachment{$name}) { |
377 | for (@$attach) { |
397 | for (@$attach) { |
378 | my ($klass, @attach) = @$_; |
398 | my ($klass, @attach) = @$_; |
379 | _attach @$registry, $klass, @attach; |
399 | _attach @$registry, $klass, @attach; |
380 | } |
400 | } |
… | |
… | |
385 | } |
405 | } |
386 | |
406 | |
387 | sub object_freezer_save { |
407 | sub object_freezer_save { |
388 | my ($filename, $objs) = @_; |
408 | my ($filename, $objs) = @_; |
389 | |
409 | |
390 | $filename .= ".pst"; |
|
|
391 | |
|
|
392 | if (@$objs) { |
410 | if (@$objs) { |
393 | open my $fh, ">:raw", "$filename~"; |
411 | open my $fh, ">:raw", "$filename.pst~"; |
394 | chmod $fh, SAVE_MODE; |
|
|
395 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
412 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
396 | close $fh; |
413 | close $fh; |
|
|
414 | chmod SAVE_MODE, "$filename.pst~"; |
397 | rename "$filename~", $filename; |
415 | rename "$filename.pst~", "$filename.pst"; |
398 | } else { |
416 | } else { |
399 | unlink $filename; |
417 | unlink "$filename.pst"; |
400 | } |
418 | } |
|
|
419 | |
|
|
420 | chmod SAVE_MODE, "$filename~"; |
|
|
421 | rename "$filename~", $filename; |
401 | } |
422 | } |
402 | |
423 | |
403 | sub object_thawer_load { |
424 | sub object_thawer_load { |
404 | my ($filename) = @_; |
425 | my ($filename) = @_; |
405 | |
426 | |
… | |
… | |
416 | |
437 | |
417 | @{$dst->registry} = @{$src->registry}; |
438 | @{$dst->registry} = @{$src->registry}; |
418 | |
439 | |
419 | %$dst = %$src; |
440 | %$dst = %$src; |
420 | |
441 | |
421 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
442 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
422 | if exists $src->{_attachment}; |
443 | if exists $src->{_attachment}; |
423 | }, |
444 | }, |
424 | ; |
445 | ; |
425 | |
446 | |
426 | ############################################################################# |
447 | ############################################################################# |
… | |
… | |
667 | my ($map) = @_; |
688 | my ($map) = @_; |
668 | |
689 | |
669 | my $path = $map->tmpname; |
690 | my $path = $map->tmpname; |
670 | defined $path or return; |
691 | defined $path or return; |
671 | |
692 | |
672 | unlink "$path.cfperl"; |
|
|
673 | unlink "$path.pst"; |
693 | unlink "$path.pst"; |
674 | }; |
694 | }; |
675 | |
695 | |
|
|
696 | # old style persistent data, TODO: remove #d# |
676 | *cf::mapsupport::on_swapin = |
697 | *cf::mapsupport::on_swapin = |
677 | *cf::mapsupport::on_load = sub { |
698 | *cf::mapsupport::on_load = sub { |
678 | my ($map) = @_; |
699 | my ($map) = @_; |
679 | |
700 | |
680 | my $path = $map->tmpname; |
701 | my $path = $map->tmpname; |
… | |
… | |
687 | |
708 | |
688 | $data->{version} <= 1 |
709 | $data->{version} <= 1 |
689 | or return; # too new |
710 | or return; # too new |
690 | |
711 | |
691 | $map->_set_obs ($data->{obs}); |
712 | $map->_set_obs ($data->{obs}); |
|
|
713 | $map->invoke (EVENT_MAP_UPGRADE); |
692 | }; |
714 | }; |
693 | |
715 | |
694 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
716 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
695 | |
717 | |
696 | ############################################################################# |
718 | ############################################################################# |