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.48 by root, Sun Aug 27 15:24:22 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
356 370
357 $data = from_json $data; 371 $data = from_json $data;
358 372
359 for (@$data) { 373 for (@$data) {
360 my ($name, $args) = @$_; 374 my ($name, $args) = @$_;
375
361 attach $obj, $name, %{$args || {} }; 376 $obj->attach ($name, %{$args || {} });
362 } 377 }
363} 378}
364 379
365# basically do the same as instantiate, without calling instantiate 380# basically do the same as instantiate, without calling instantiate
366sub reattach { 381sub reattach {
367 my ($obj) = @_; 382 my ($obj) = @_;
368 my $registry = $obj->registry; 383 my $registry = $obj->registry;
369 384
370 @$registry = (); 385 @$registry = ();
371 386
387 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
388
372 for my $name (@{ $obj->{_attachment} }) { 389 for my $name (keys %{ $obj->{_attachment} || {} }) {
373 if (my $attach = $attachment{$name}) { 390 if (my $attach = $attachment{$name}) {
374 for (@$attach) { 391 for (@$attach) {
375 my ($klass, @attach) = @$_; 392 my ($klass, @attach) = @$_;
376 _attach @$registry, $klass, @attach; 393 _attach @$registry, $klass, @attach;
377 } 394 }
382} 399}
383 400
384sub object_freezer_save { 401sub object_freezer_save {
385 my ($filename, $objs) = @_; 402 my ($filename, $objs) = @_;
386 403
387 $filename .= ".pst";
388
389 if (@$objs) { 404 if (@$objs) {
390 open my $fh, ">:raw", "$filename~"; 405 open my $fh, ">:raw", "$filename.pst~";
391 chmod $fh, SAVE_MODE;
392 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; 406 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
393 close $fh; 407 close $fh;
408 chmod SAVE_MODE, "$filename.pst~";
394 rename "$filename~", $filename; 409 rename "$filename.pst~", "$filename.pst";
395 } else { 410 } else {
396 unlink $filename; 411 unlink "$filename.pst";
397 } 412 }
413
414 chmod SAVE_MODE, "$filename~";
415 rename "$filename~", $filename;
398} 416}
399 417
400sub object_thawer_load { 418sub object_thawer_load {
401 my ($filename) = @_; 419 my ($filename) = @_;
402 420
413 431
414 @{$dst->registry} = @{$src->registry}; 432 @{$dst->registry} = @{$src->registry};
415 433
416 %$dst = %$src; 434 %$dst = %$src;
417 435
418 $dst->{_attachment} = [@{ $src->{_attachment} }] 436 %{$dst->{_attachment}} = %{$src->{_attachment}}
419 if exists $src->{_attachment}; 437 if exists $src->{_attachment};
420 }, 438 },
421; 439;
422 440
423############################################################################# 441#############################################################################
664 my ($map) = @_; 682 my ($map) = @_;
665 683
666 my $path = $map->tmpname; 684 my $path = $map->tmpname;
667 defined $path or return; 685 defined $path or return;
668 686
669 unlink "$path.cfperl";
670 unlink "$path.pst"; 687 unlink "$path.pst";
671}; 688};
672 689
690# old style persistent data, TODO: remove #d#
673*cf::mapsupport::on_swapin = 691*cf::mapsupport::on_swapin =
674*cf::mapsupport::on_load = sub { 692*cf::mapsupport::on_load = sub {
675 my ($map) = @_; 693 my ($map) = @_;
676 694
677 my $path = $map->tmpname; 695 my $path = $map->tmpname;
684 702
685 $data->{version} <= 1 703 $data->{version} <= 1
686 or return; # too new 704 or return; # too new
687 705
688 $map->_set_obs ($data->{obs}); 706 $map->_set_obs ($data->{obs});
707 $map->invoke (EVENT_MAP_UPGRADE);
689}; 708};
690 709
691attach_to_maps prio => -10000, package => cf::mapsupport::; 710attach_to_maps prio => -10000, package => cf::mapsupport::;
692 711
693############################################################################# 712#############################################################################

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines