… | |
… | |
105 | } |
105 | } |
106 | |
106 | |
107 | ############################################################################# |
107 | ############################################################################# |
108 | # "new" plug-in system |
108 | # "new" plug-in system |
109 | |
109 | |
|
|
110 | =head3 EVENTS AND OBJECT ATTACHMENTS |
|
|
111 | |
|
|
112 | =over 4 |
|
|
113 | |
110 | =item $object->attach ($attachment, key => $value...) |
114 | =item $object->attach ($attachment, key => $value...) |
111 | |
115 | |
|
|
116 | =item $object->detach ($attachment) |
|
|
117 | |
112 | Attach a pre-registered attachment to an object. |
118 | Attach/detach a pre-registered attachment to an object. |
113 | |
119 | |
114 | =item $player->attach ($attachment, key => $value...) |
120 | =item $player->attach ($attachment, key => $value...) |
115 | |
121 | |
|
|
122 | =item $player->detach ($attachment) |
|
|
123 | |
116 | Attach a pre-registered attachment to a player. |
124 | Attach/detach a pre-registered attachment to a player. |
117 | |
125 | |
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
126 | =item $map->attach ($attachment, key => $value...) |
119 | |
127 | |
|
|
128 | =item $map->detach ($attachment) |
|
|
129 | |
120 | Attach a pre-registered attachment to a map. |
130 | Attach/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 | |
|
|
138 | Checks wether the named attachment is currently attached to the object. |
121 | |
139 | |
122 | =item cf::attach_global ... |
140 | =item cf::attach_global ... |
123 | |
141 | |
124 | Attach handlers for global events. |
142 | Attach handlers for global events. |
125 | |
143 | |
… | |
… | |
175 | Attach handlers to all maps. |
193 | Attach handlers to all maps. |
176 | |
194 | |
177 | =item cf:register_attachment $name, ... |
195 | =item cf:register_attachment $name, ... |
178 | |
196 | |
179 | Register an attachment by name through which objects can refer to this |
197 | Register an attachment by name through which objects can refer to this |
|
|
198 | attachment. |
|
|
199 | |
|
|
200 | =item cf:register_player_attachment $name, ... |
|
|
201 | |
|
|
202 | Register an attachment by name through which players can refer to this |
180 | attachment. |
203 | attachment. |
181 | |
204 | |
182 | =item cf:register_map_attachment $name, ... |
205 | =item cf:register_map_attachment $name, ... |
183 | |
206 | |
184 | Register an attachment by name through which maps can refer to this |
207 | Register an attachment by name through which maps can refer to this |
… | |
… | |
255 | } |
278 | } |
256 | |
279 | |
257 | sub _attach_attachment { |
280 | sub _attach_attachment { |
258 | my ($obj, $name, %arg) = @_; |
281 | my ($obj, $name, %arg) = @_; |
259 | |
282 | |
|
|
283 | return if exists $obj->{_attachment}{$name}; |
|
|
284 | |
260 | my $res; |
285 | my $res; |
261 | |
286 | |
262 | if (my $attach = $attachment{$name}) { |
287 | if (my $attach = $attachment{$name}) { |
263 | my $registry = $obj->registry; |
288 | my $registry = $obj->registry; |
264 | |
289 | |
… | |
… | |
276 | |
301 | |
277 | $res->{attachment} = $name; |
302 | $res->{attachment} = $name; |
278 | $res |
303 | $res |
279 | } |
304 | } |
280 | |
305 | |
281 | sub cf::object::attach { |
306 | *cf::object::attach = |
|
|
307 | *cf::player::attach = |
|
|
308 | *cf::map::attach = sub { |
282 | my ($obj, $name, %arg) = @_; |
309 | my ($obj, $name, %arg) = @_; |
283 | |
310 | |
284 | _attach_attachment $obj, $name, %arg; |
311 | _attach_attachment $obj, $name, %arg; |
285 | } |
312 | }; |
286 | |
313 | |
|
|
314 | # all those should be optimised |
|
|
315 | *cf::object::detach = |
287 | sub cf::player::attach { |
316 | *cf::player::detach = |
|
|
317 | *cf::map::detach = sub { |
288 | my ($obj, $name, %arg) = @_; |
318 | my ($obj, $name) = @_; |
289 | |
319 | |
290 | _attach_attachment $obj, $name, %arg; |
320 | delete $obj->{_attachment}{$name}; |
291 | } |
321 | reattach ($obj); |
|
|
322 | }; |
292 | |
323 | |
293 | sub cf::map::attach { |
324 | *cf::object::attached = |
|
|
325 | *cf::player::attached = |
|
|
326 | *cf::map::attached = sub { |
294 | my ($obj, $name, %arg) = @_; |
327 | my ($obj, $name) = @_; |
295 | |
328 | |
296 | _attach_attachment $obj, $name, %arg; |
329 | exists $obj->{_attachment}{$name} |
297 | } |
330 | }; |
298 | |
331 | |
299 | sub attach_global { |
332 | sub attach_global { |
300 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
333 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
301 | } |
334 | } |
302 | |
335 | |
… | |
… | |
321 | |
354 | |
322 | sub register_attachment { |
355 | sub register_attachment { |
323 | my $name = shift; |
356 | my $name = shift; |
324 | |
357 | |
325 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
358 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
359 | } |
|
|
360 | |
|
|
361 | sub register_player_attachment { |
|
|
362 | my $name = shift; |
|
|
363 | |
|
|
364 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
326 | } |
365 | } |
327 | |
366 | |
328 | sub register_map_attachment { |
367 | sub register_map_attachment { |
329 | my $name = shift; |
368 | my $name = shift; |
330 | |
369 | |
… | |
… | |
359 | return 1 if $override; |
398 | return 1 if $override; |
360 | } |
399 | } |
361 | |
400 | |
362 | 0 |
401 | 0 |
363 | } |
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 | |
|
|
412 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
413 | |
|
|
414 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
415 | removed in future versions), and there is no public API to access override |
|
|
416 | results (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 | |
|
|
430 | Just because you have a perl object does not mean that the corresponding |
|
|
431 | C-level object still exists. If you try to access an object that has no |
|
|
432 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
433 | can be used to test for existence of the C object part without causing an |
|
|
434 | exception. |
|
|
435 | |
|
|
436 | =back |
|
|
437 | |
|
|
438 | =cut |
|
|
439 | |
|
|
440 | *cf::object::valid = |
|
|
441 | *cf::player::valid = |
|
|
442 | *cf::map::valid = \&cf::_valid; |
364 | |
443 | |
365 | ############################################################################# |
444 | ############################################################################# |
366 | # object support |
445 | # object support |
367 | |
446 | |
368 | sub instantiate { |
447 | sub instantiate { |
… | |
… | |
397 | } |
476 | } |
398 | } |
477 | } |
399 | } |
478 | } |
400 | |
479 | |
401 | sub object_freezer_save { |
480 | sub object_freezer_save { |
402 | my ($filename, $objs) = @_; |
481 | my ($filename, $rdata, $objs) = @_; |
403 | |
482 | |
404 | if (@$objs) { |
|
|
405 | open my $fh, ">:raw", "$filename.pst~"; |
483 | if (open my $fh, ">:raw", "$filename~") { |
406 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
484 | chmod SAVE_MODE, $fh; |
|
|
485 | syswrite $fh, $$rdata; |
407 | close $fh; |
486 | close $fh; |
408 | chmod SAVE_MODE, "$filename.pst~"; |
487 | |
|
|
488 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
489 | chmod SAVE_MODE, $fh; |
|
|
490 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
491 | close $fh; |
409 | rename "$filename.pst~", "$filename.pst"; |
492 | rename "$filename.pst~", "$filename.pst"; |
|
|
493 | } else { |
|
|
494 | unlink "$filename.pst"; |
|
|
495 | } |
|
|
496 | |
|
|
497 | rename "$filename~", $filename; |
410 | } else { |
498 | } else { |
411 | unlink "$filename.pst"; |
499 | warn "FATAL: $filename~: $!\n"; |
412 | } |
500 | } |
413 | |
|
|
414 | chmod SAVE_MODE, "$filename~"; |
|
|
415 | rename "$filename~", $filename; |
|
|
416 | } |
501 | } |
417 | |
502 | |
418 | sub object_thawer_load { |
503 | sub object_thawer_load { |
419 | my ($filename) = @_; |
504 | my ($filename) = @_; |
420 | |
505 | |
… | |
… | |
685 | defined $path or return; |
770 | defined $path or return; |
686 | |
771 | |
687 | unlink "$path.pst"; |
772 | unlink "$path.pst"; |
688 | }; |
773 | }; |
689 | |
774 | |
690 | # old style persistent data, TODO: remove #d# |
|
|
691 | *cf::mapsupport::on_swapin = |
|
|
692 | *cf::mapsupport::on_load = sub { |
|
|
693 | my ($map) = @_; |
|
|
694 | |
|
|
695 | my $path = $map->tmpname; |
|
|
696 | $path = $map->path unless defined $path; |
|
|
697 | |
|
|
698 | open my $fh, "<:raw", "$path.cfperl" |
|
|
699 | or return; # no perl data |
|
|
700 | |
|
|
701 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
702 | |
|
|
703 | $data->{version} <= 1 |
|
|
704 | or return; # too new |
|
|
705 | |
|
|
706 | $map->_set_obs ($data->{obs}); |
|
|
707 | $map->invoke (EVENT_MAP_UPGRADE); |
|
|
708 | }; |
|
|
709 | |
|
|
710 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
775 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
711 | |
776 | |
712 | ############################################################################# |
777 | ############################################################################# |
713 | # load/save perl data associated with player->ob objects |
778 | # load/save perl data associated with player->ob objects |
714 | |
779 | |