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.49 by root, Sun Aug 27 17:59:26 2006 UTC vs.
Revision 1.52 by root, Mon Aug 28 16:52:51 2006 UTC

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.
77for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { 77for 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 {
173=item cf::attach_to_maps ... 173=item cf::attach_to_maps ...
174 174
175Attach handlers to all maps. 175Attach handlers to all maps.
176 176
177=item cf:register_attachment $name, ... 177=item cf:register_attachment $name, ...
178
179Register an attachment by name through which objects can refer to this
180attachment.
181
182=item cf:register_map_attachment $name, ...
183
184Register an attachment by name through which maps can refer to this
185attachment.
178 186
179=cut 187=cut
180 188
181# the following variables are defined in .xs and must not be re-created 189# the following variables are defined in .xs and must not be re-created
182our @CB_GLOBAL = (); # registry for all global events 190our @CB_GLOBAL = (); # registry for all global events
262 $obj->{$name} = \%arg; 270 $obj->{$name} = \%arg;
263 } else { 271 } else {
264 warn "object uses attachment '$name' that is not available, postponing.\n"; 272 warn "object uses attachment '$name' that is not available, postponing.\n";
265 } 273 }
266 274
267 push @{$obj->{_attachment}}, $name; 275 $obj->{_attachment}{$name} = undef;
268 276
269 $res->{attachment} = $name; 277 $res->{attachment} = $name;
270 $res 278 $res
271} 279}
272 280
277} 285}
278 286
279sub cf::player::attach { 287sub cf::player::attach {
280 my ($obj, $name, %arg) = @_; 288 my ($obj, $name, %arg) = @_;
281 289
282 _attach_attachment KLASS_PLAYER, $obj, $name, %arg; 290 _attach_attachment $obj, $name, %arg;
283} 291}
284 292
285sub cf::map::attach { 293sub cf::map::attach {
286 my ($obj, $name, %arg) = @_; 294 my ($obj, $name, %arg) = @_;
287 295
288 _attach_attachment KLASS_MAP, $obj, $name, %arg; 296 _attach_attachment $obj, $name, %arg;
289} 297}
290 298
291sub attach_global { 299sub attach_global {
292 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 300 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
293} 301}
313 321
314sub register_attachment { 322sub register_attachment {
315 my $name = shift; 323 my $name = shift;
316 324
317 $attachment{$name} = [[KLASS_OBJECT, @_]]; 325 $attachment{$name} = [[KLASS_OBJECT, @_]];
326}
327
328sub register_map_attachment {
329 my $name = shift;
330
331 $attachment{$name} = [[KLASS_MAP, @_]];
318} 332}
319 333
320our $override; 334our $override;
321our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? 335our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
322 336
368 my ($obj) = @_; 382 my ($obj) = @_;
369 my $registry = $obj->registry; 383 my $registry = $obj->registry;
370 384
371 @$registry = (); 385 @$registry = ();
372 386
373 delete $obj->{_attachment} unless @{ $obj->{_attachment} || [] }; 387 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
374 388
375 for my $name (@{ $obj->{_attachment} || [] }) { 389 for my $name (keys %{ $obj->{_attachment} || {} }) {
376 if (my $attach = $attachment{$name}) { 390 if (my $attach = $attachment{$name}) {
377 for (@$attach) { 391 for (@$attach) {
378 my ($klass, @attach) = @$_; 392 my ($klass, @attach) = @$_;
379 _attach @$registry, $klass, @attach; 393 _attach @$registry, $klass, @attach;
380 } 394 }
385} 399}
386 400
387sub object_freezer_save { 401sub object_freezer_save {
388 my ($filename, $objs) = @_; 402 my ($filename, $objs) = @_;
389 403
390 $filename .= ".pst";
391
392 if (@$objs) { 404 if (@$objs) {
393 open my $fh, ">:raw", "$filename~"; 405 open my $fh, ">:raw", "$filename.pst~";
394 chmod $fh, SAVE_MODE;
395 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; 406 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
396 close $fh; 407 close $fh;
408 chmod SAVE_MODE, "$filename.pst~";
397 rename "$filename~", $filename; 409 rename "$filename.pst~", "$filename.pst";
398 } else { 410 } else {
399 unlink $filename; 411 unlink "$filename.pst";
400 } 412 }
413
414 chmod SAVE_MODE, "$filename~";
415 rename "$filename~", $filename;
401} 416}
402 417
403sub object_thawer_load { 418sub object_thawer_load {
404 my ($filename) = @_; 419 my ($filename) = @_;
405 420
416 431
417 @{$dst->registry} = @{$src->registry}; 432 @{$dst->registry} = @{$src->registry};
418 433
419 %$dst = %$src; 434 %$dst = %$src;
420 435
421 $dst->{_attachment} = [@{ $src->{_attachment} }] 436 %{$dst->{_attachment}} = %{$src->{_attachment}}
422 if exists $src->{_attachment}; 437 if exists $src->{_attachment};
423 }, 438 },
424; 439;
425 440
426############################################################################# 441#############################################################################
667 my ($map) = @_; 682 my ($map) = @_;
668 683
669 my $path = $map->tmpname; 684 my $path = $map->tmpname;
670 defined $path or return; 685 defined $path or return;
671 686
672 unlink "$path.cfperl";
673 unlink "$path.pst"; 687 unlink "$path.pst";
674}; 688};
675 689
690# old style persistent data, TODO: remove #d#
676*cf::mapsupport::on_swapin = 691*cf::mapsupport::on_swapin =
677*cf::mapsupport::on_load = sub { 692*cf::mapsupport::on_load = sub {
678 my ($map) = @_; 693 my ($map) = @_;
679 694
680 my $path = $map->tmpname; 695 my $path = $map->tmpname;
687 702
688 $data->{version} <= 1 703 $data->{version} <= 1
689 or return; # too new 704 or return; # too new
690 705
691 $map->_set_obs ($data->{obs}); 706 $map->_set_obs ($data->{obs});
707 $map->invoke (EVENT_MAP_UPGRADE);
692}; 708};
693 709
694attach_to_maps prio => -10000, package => cf::mapsupport::; 710attach_to_maps prio => -10000, package => cf::mapsupport::;
695 711
696############################################################################# 712#############################################################################

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines