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.47 by root, Sun Aug 27 13:02:04 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 {
105} 105}
106 106
107############################################################################# 107#############################################################################
108# "new" plug-in system 108# "new" plug-in system
109 109
110=item $object->attach ($attachment, ...) 110=item $object->attach ($attachment, key => $value...)
111 111
112Attach a pre-registered attachment to an object. 112Attach a pre-registered attachment to an object.
113 113
114=item $player->attach ($attachment, ...) 114=item $player->attach ($attachment, key => $value...)
115 115
116Attach a pre-registered attachment to a player. 116Attach a pre-registered attachment to a player.
117 117
118=item $map->attach ($attachment, ...) # not yet persistent 118=item $map->attach ($attachment, key => $value...) # not yet persistent
119 119
120Attach a pre-registered attachment to a map. 120Attach a pre-registered attachment to a map.
121 121
122=item cf::attach_global ... 122=item cf::attach_global ...
123 123
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
245 253
246 \%undo 254 \%undo
247} 255}
248 256
249sub _attach_attachment { 257sub _attach_attachment {
250 my ($obj, $name, @args) = @_; 258 my ($obj, $name, %arg) = @_;
251 259
252 my $res; 260 my $res;
253 261
254 if (my $attach = $attachment{$name}) { 262 if (my $attach = $attachment{$name}) {
255 my $registry = $obj->registry; 263 my $registry = $obj->registry;
257 for (@$attach) { 265 for (@$attach) {
258 my ($klass, @attach) = @$_; 266 my ($klass, @attach) = @$_;
259 $res = _attach @$registry, $klass, @attach; 267 $res = _attach @$registry, $klass, @attach;
260 } 268 }
261 269
262 if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { 270 $obj->{$name} = \%arg;
263 for (@$cb) {
264 eval { $_->[1]->($obj, @args); };
265 if ($@) {
266 warn "$@";
267 warn "... while processing '$name' instantiate with args <@args>.\n";
268 }
269 }
270 }
271 } else { 271 } else {
272 warn "object uses attachment '$name' that is not available, postponing.\n"; 272 warn "object uses attachment '$name' that is not available, postponing.\n";
273 } 273 }
274 274
275 push @{$obj->{_attachment}}, $name; 275 $obj->{_attachment}{$name} = undef;
276 276
277 $res->{attachment} = $name; 277 $res->{attachment} = $name;
278 $res 278 $res
279} 279}
280 280
281sub cf::object::attach { 281sub cf::object::attach {
282 my ($obj, $name, @args) = @_; 282 my ($obj, $name, %arg) = @_;
283 283
284 _attach_attachment $obj, $name, @args; 284 _attach_attachment $obj, $name, %arg;
285} 285}
286 286
287sub cf::player::attach { 287sub cf::player::attach {
288 my ($obj, $name, @args) = @_; 288 my ($obj, $name, %arg) = @_;
289 289
290 _attach_attachment KLASS_PLAYER, $obj, $name, @args; 290 _attach_attachment $obj, $name, %arg;
291} 291}
292 292
293sub cf::map::attach { 293sub cf::map::attach {
294 my ($obj, $name, @args) = @_; 294 my ($obj, $name, %arg) = @_;
295 295
296 _attach_attachment KLASS_MAP, $obj, $name, @args; 296 _attach_attachment $obj, $name, %arg;
297} 297}
298 298
299sub attach_global { 299sub attach_global {
300 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 300 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
301} 301}
321 321
322sub register_attachment { 322sub register_attachment {
323 my $name = shift; 323 my $name = shift;
324 324
325 $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, @_]];
326} 332}
327 333
328our $override; 334our $override;
329our @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?
330 336
364 370
365 $data = from_json $data; 371 $data = from_json $data;
366 372
367 for (@$data) { 373 for (@$data) {
368 my ($name, $args) = @$_; 374 my ($name, $args) = @$_;
369 attach $obj, $name, @{$args || [] }; 375
376 $obj->attach ($name, %{$args || {} });
370 } 377 }
371} 378}
372 379
373# basically do the same as instantiate, without calling instantiate 380# basically do the same as instantiate, without calling instantiate
374sub reattach { 381sub reattach {
375 my ($obj) = @_; 382 my ($obj) = @_;
376 my $registry = $obj->registry; 383 my $registry = $obj->registry;
377 384
378 @$registry = (); 385 @$registry = ();
379 386
387 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
388
380 for my $name (@{ $obj->{_attachment} }) { 389 for my $name (keys %{ $obj->{_attachment} || {} }) {
381 if (my $attach = $attachment{$name}) { 390 if (my $attach = $attachment{$name}) {
382 for (@$attach) { 391 for (@$attach) {
383 my ($klass, @attach) = @$_; 392 my ($klass, @attach) = @$_;
384 _attach @$registry, $klass, @attach; 393 _attach @$registry, $klass, @attach;
385 } 394 }
390} 399}
391 400
392sub object_freezer_save { 401sub object_freezer_save {
393 my ($filename, $objs) = @_; 402 my ($filename, $objs) = @_;
394 403
395 $filename .= ".pst";
396
397 if (@$objs) { 404 if (@$objs) {
398 open my $fh, ">:raw", "$filename~"; 405 open my $fh, ">:raw", "$filename.pst~";
399 chmod $fh, SAVE_MODE;
400 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; 406 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
401 close $fh; 407 close $fh;
408 chmod SAVE_MODE, "$filename.pst~";
402 rename "$filename~", $filename; 409 rename "$filename.pst~", "$filename.pst";
403 } else { 410 } else {
404 unlink $filename; 411 unlink "$filename.pst";
405 } 412 }
413
414 chmod SAVE_MODE, "$filename~";
415 rename "$filename~", $filename;
406} 416}
407 417
408sub object_thawer_load { 418sub object_thawer_load {
409 my ($filename) = @_; 419 my ($filename) = @_;
410 420
421 431
422 @{$dst->registry} = @{$src->registry}; 432 @{$dst->registry} = @{$src->registry};
423 433
424 %$dst = %$src; 434 %$dst = %$src;
425 435
426 $dst->{_attachment} = [@{ $src->{_attachment} }] 436 %{$dst->{_attachment}} = %{$src->{_attachment}}
427 if exists $src->{_attachment}; 437 if exists $src->{_attachment};
428 }, 438 },
429; 439;
430 440
431############################################################################# 441#############################################################################
672 my ($map) = @_; 682 my ($map) = @_;
673 683
674 my $path = $map->tmpname; 684 my $path = $map->tmpname;
675 defined $path or return; 685 defined $path or return;
676 686
677 unlink "$path.cfperl";
678 unlink "$path.pst"; 687 unlink "$path.pst";
679}; 688};
680 689
690# old style persistent data, TODO: remove #d#
681*cf::mapsupport::on_swapin = 691*cf::mapsupport::on_swapin =
682*cf::mapsupport::on_load = sub { 692*cf::mapsupport::on_load = sub {
683 my ($map) = @_; 693 my ($map) = @_;
684 694
685 my $path = $map->tmpname; 695 my $path = $map->tmpname;
692 702
693 $data->{version} <= 1 703 $data->{version} <= 1
694 or return; # too new 704 or return; # too new
695 705
696 $map->_set_obs ($data->{obs}); 706 $map->_set_obs ($data->{obs});
707 $map->invoke (EVENT_MAP_UPGRADE);
697}; 708};
698 709
699attach_to_maps prio => -10000, package => cf::mapsupport::; 710attach_to_maps prio => -10000, package => cf::mapsupport::;
700 711
701############################################################################# 712#############################################################################

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines