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.53 by root, Tue Aug 29 13:11:58 2006 UTC vs.
Revision 1.59 by root, Wed Aug 30 16:30:37 2006 UTC

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
112=item $object->detach ($attachment) 116=item $object->detach ($attachment)
113 117
114Attach/detach a pre-registered attachment to an object. 118Attach/detach a pre-registered attachment to an object.
122=item $map->attach ($attachment, key => $value...) 126=item $map->attach ($attachment, key => $value...)
123 127
124=item $map->detach ($attachment) 128=item $map->detach ($attachment)
125 129
126Attach/detach a pre-registered attachment to a map. 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.
127 139
128=item cf::attach_global ... 140=item cf::attach_global ...
129 141
130Attach handlers for global events. 142Attach handlers for global events.
131 143
181Attach handlers to all maps. 193Attach handlers to all maps.
182 194
183=item cf:register_attachment $name, ... 195=item cf:register_attachment $name, ...
184 196
185Register an attachment by name through which objects can refer to this 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
186attachment. 203attachment.
187 204
188=item cf:register_map_attachment $name, ... 205=item cf:register_map_attachment $name, ...
189 206
190Register an attachment by name through which maps can refer to this 207Register an attachment by name through which maps can refer to this
261} 278}
262 279
263sub _attach_attachment { 280sub _attach_attachment {
264 my ($obj, $name, %arg) = @_; 281 my ($obj, $name, %arg) = @_;
265 282
283 return if exists $obj->{_attachment}{$name};
284
266 my $res; 285 my $res;
267 286
268 if (my $attach = $attachment{$name}) { 287 if (my $attach = $attachment{$name}) {
269 my $registry = $obj->registry; 288 my $registry = $obj->registry;
270 289
282 301
283 $res->{attachment} = $name; 302 $res->{attachment} = $name;
284 $res 303 $res
285} 304}
286 305
287sub cf::object::attach { 306*cf::object::attach =
307*cf::player::attach =
308*cf::map::attach = sub {
288 my ($obj, $name, %arg) = @_; 309 my ($obj, $name, %arg) = @_;
289 310
290 _attach_attachment $obj, $name, %arg; 311 _attach_attachment $obj, $name, %arg;
291} 312};
292 313
293sub cf::player::attach { 314# all those should be optimised
294 my ($obj, $name, %arg) = @_;
295
296 _attach_attachment $obj, $name, %arg;
297}
298
299sub cf::map::attach {
300 my ($obj, $name, %arg) = @_;
301
302 _attach_attachment $obj, $name, %arg;
303}
304
305sub cf::object::detach { 315*cf::object::detach =
316*cf::player::detach =
317*cf::map::detach = sub {
306 my ($obj, $name) = @_; 318 my ($obj, $name) = @_;
307 die;#d# 319
308} 320 delete $obj->{_attachment}{$name};
309sub cf::player::detach { 321 reattach ($obj);
322};
323
324*cf::object::attached =
325*cf::player::attached =
326*cf::map::attached = sub {
310 my ($obj, $name) = @_; 327 my ($obj, $name) = @_;
311 die;#d# 328
312} 329 exists $obj->{_attachment}{$name}
313sub cf::map::detach { 330};
314 my ($obj, $name) = @_;
315 die;#d#
316}
317 331
318sub attach_global { 332sub attach_global {
319 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 333 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
320} 334}
321 335
340 354
341sub register_attachment { 355sub register_attachment {
342 my $name = shift; 356 my $name = shift;
343 357
344 $attachment{$name} = [[KLASS_OBJECT, @_]]; 358 $attachment{$name} = [[KLASS_OBJECT, @_]];
359}
360
361sub register_player_attachment {
362 my $name = shift;
363
364 $attachment{$name} = [[KLASS_PLAYER, @_]];
345} 365}
346 366
347sub register_map_attachment { 367sub register_map_attachment {
348 my $name = shift; 368 my $name = shift;
349 369
378 return 1 if $override; 398 return 1 if $override;
379 } 399 }
380 400
381 0 401 0
382} 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;
383 443
384############################################################################# 444#############################################################################
385# object support 445# object support
386 446
387sub instantiate { 447sub instantiate {
416 } 476 }
417 } 477 }
418} 478}
419 479
420sub object_freezer_save { 480sub object_freezer_save {
421 my ($filename, $objs) = @_; 481 my ($filename, $rdata, $objs) = @_;
422 482
423 if (@$objs) {
424 open my $fh, ">:raw", "$filename.pst~"; 483 if (open my $fh, ">:raw", "$filename~") {
425 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; 484 chmod SAVE_MODE, $fh;
485 syswrite $fh, $$rdata;
426 close $fh; 486 close $fh;
427 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;
428 rename "$filename.pst~", "$filename.pst"; 492 rename "$filename.pst~", "$filename.pst";
493 } else {
494 unlink "$filename.pst";
495 }
496
497 rename "$filename~", $filename;
429 } else { 498 } else {
430 unlink "$filename.pst"; 499 warn "FATAL: $filename~: $!\n";
431 } 500 }
432
433 chmod SAVE_MODE, "$filename~";
434 rename "$filename~", $filename;
435} 501}
436 502
437sub object_thawer_load { 503sub object_thawer_load {
438 my ($filename) = @_; 504 my ($filename) = @_;
439 505
704 defined $path or return; 770 defined $path or return;
705 771
706 unlink "$path.pst"; 772 unlink "$path.pst";
707}; 773};
708 774
709# old style persistent data, TODO: remove #d#
710*cf::mapsupport::on_swapin =
711*cf::mapsupport::on_load = sub {
712 my ($map) = @_;
713
714 my $path = $map->tmpname;
715 $path = $map->path unless defined $path;
716
717 open my $fh, "<:raw", "$path.cfperl"
718 or return; # no perl data
719
720 my $data = Storable::thaw do { local $/; <$fh> };
721
722 $data->{version} <= 1
723 or return; # too new
724
725 $map->_set_obs ($data->{obs});
726 $map->invoke (EVENT_MAP_UPGRADE);
727};
728
729attach_to_maps prio => -10000, package => cf::mapsupport::; 775attach_to_maps prio => -10000, package => cf::mapsupport::;
730 776
731############################################################################# 777#############################################################################
732# load/save perl data associated with player->ob objects 778# load/save perl data associated with player->ob objects
733 779

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines