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.51 by root, Mon Aug 28 14:05:24 2006 UTC vs.
Revision 1.57 by root, Wed Aug 30 11:21:24 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
116=item $object->detach ($attachment)
117
112Attach a pre-registered attachment to an object. 118Attach/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
116Attach a pre-registered attachment to a player. 124Attach/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
120Attach 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.
121 139
122=item cf::attach_global ... 140=item cf::attach_global ...
123 141
124Attach handlers for global events. 142Attach handlers for global events.
125 143
173=item cf::attach_to_maps ... 191=item cf::attach_to_maps ...
174 192
175Attach handlers to all maps. 193Attach handlers to all maps.
176 194
177=item cf:register_attachment $name, ... 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.
178 209
179=cut 210=cut
180 211
181# the following variables are defined in .xs and must not be re-created 212# the following variables are defined in .xs and must not be re-created
182our @CB_GLOBAL = (); # registry for all global events 213our @CB_GLOBAL = (); # registry for all global events
247} 278}
248 279
249sub _attach_attachment { 280sub _attach_attachment {
250 my ($obj, $name, %arg) = @_; 281 my ($obj, $name, %arg) = @_;
251 282
283 return if exists $obj->{_attachment}{$name};
284
252 my $res; 285 my $res;
253 286
254 if (my $attach = $attachment{$name}) { 287 if (my $attach = $attachment{$name}) {
255 my $registry = $obj->registry; 288 my $registry = $obj->registry;
256 289
268 301
269 $res->{attachment} = $name; 302 $res->{attachment} = $name;
270 $res 303 $res
271} 304}
272 305
273sub cf::object::attach { 306*cf::object::attach =
307*cf::player::attach =
308*cf::map::attach = sub {
274 my ($obj, $name, %arg) = @_; 309 my ($obj, $name, %arg) = @_;
275 310
276 _attach_attachment $obj, $name, %arg; 311 _attach_attachment $obj, $name, %arg;
277} 312};
278 313
314# all those should be optimised
315*cf::object::detach =
279sub cf::player::attach { 316*cf::player::detach =
317*cf::map::detach = sub {
280 my ($obj, $name, %arg) = @_; 318 my ($obj, $name) = @_;
281 319
282 _attach_attachment KLASS_PLAYER, $obj, $name, %arg; 320 delete $obj->{_attachment}{$name};
283} 321 reattach ($obj);
322};
284 323
285sub cf::map::attach { 324*cf::object::attached =
325*cf::player::attached =
326*cf::map::attached = sub {
286 my ($obj, $name, %arg) = @_; 327 my ($obj, $name) = @_;
287 328
288 _attach_attachment KLASS_MAP, $obj, $name, %arg; 329 exists $obj->{_attachment}{$name}
289} 330};
290 331
291sub attach_global { 332sub attach_global {
292 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 333 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
293} 334}
294 335
313 354
314sub register_attachment { 355sub register_attachment {
315 my $name = shift; 356 my $name = shift;
316 357
317 $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, @_]];
365}
366
367sub register_map_attachment {
368 my $name = shift;
369
370 $attachment{$name} = [[KLASS_MAP, @_]];
318} 371}
319 372
320our $override; 373our $override;
321our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? 374our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
322 375
345 return 1 if $override; 398 return 1 if $override;
346 } 399 }
347 400
348 0 401 0
349} 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;
350 443
351############################################################################# 444#############################################################################
352# object support 445# object support
353 446
354sub instantiate { 447sub instantiate {
389 482
390 if (@$objs) { 483 if (@$objs) {
391 open my $fh, ">:raw", "$filename.pst~"; 484 open my $fh, ">:raw", "$filename.pst~";
392 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; 485 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
393 close $fh; 486 close $fh;
487 unlink "$filename.cfperl";
394 chmod SAVE_MODE, "$filename.pst~"; 488 chmod SAVE_MODE, "$filename.pst~";
395 rename "$filename.pst~", "$filename.pst"; 489 rename "$filename.pst~", "$filename.pst";
396 } else { 490 } else {
397 unlink "$filename.pst"; 491 unlink "$filename.pst";
398 } 492 }
672 766
673 unlink "$path.pst"; 767 unlink "$path.pst";
674}; 768};
675 769
676# old style persistent data, TODO: remove #d# 770# old style persistent data, TODO: remove #d#
677*cf::mapsupport::on_swapin = 771*cf::mapsupport::on_swapin = sub {
678*cf::mapsupport::on_load = sub {
679 my ($map) = @_; 772 my ($map) = @_;
680 773
681 my $path = $map->tmpname; 774 my $path = $map->tmpname;
682 $path = $map->path unless defined $path; 775 $path = $map->path unless defined $path;
683 776

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines