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.53 by root, Tue Aug 29 13:11:58 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, key => $value...)
111
110=item $object->attach ($attachment, ...) 112=item $object->detach ($attachment)
111 113
112Attach a pre-registered attachment to an object. 114Attach/detach a pre-registered attachment to an object.
113 115
116=item $player->attach ($attachment, key => $value...)
117
114=item $player->attach ($attachment, ...) 118=item $player->detach ($attachment)
115 119
116Attach a pre-registered attachment to a player. 120Attach/detach a pre-registered attachment to a player.
117 121
118=item $map->attach ($attachment, ...) # not yet persistent 122=item $map->attach ($attachment, key => $value...)
119 123
124=item $map->detach ($attachment)
125
120Attach a pre-registered attachment to a map. 126Attach/detach a pre-registered attachment to a map.
121 127
122=item cf::attach_global ... 128=item cf::attach_global ...
123 129
124Attach handlers for global events. 130Attach handlers for global events.
125 131
173=item cf::attach_to_maps ... 179=item cf::attach_to_maps ...
174 180
175Attach handlers to all maps. 181Attach handlers to all maps.
176 182
177=item cf:register_attachment $name, ... 183=item cf:register_attachment $name, ...
184
185Register an attachment by name through which objects can refer to this
186attachment.
187
188=item cf:register_map_attachment $name, ...
189
190Register an attachment by name through which maps can refer to this
191attachment.
178 192
179=cut 193=cut
180 194
181# the following variables are defined in .xs and must not be re-created 195# the following variables are defined in .xs and must not be re-created
182our @CB_GLOBAL = (); # registry for all global events 196our @CB_GLOBAL = (); # registry for all global events
245 259
246 \%undo 260 \%undo
247} 261}
248 262
249sub _attach_attachment { 263sub _attach_attachment {
250 my ($obj, $name, @args) = @_; 264 my ($obj, $name, %arg) = @_;
251 265
252 my $res; 266 my $res;
253 267
254 if (my $attach = $attachment{$name}) { 268 if (my $attach = $attachment{$name}) {
255 my $registry = $obj->registry; 269 my $registry = $obj->registry;
257 for (@$attach) { 271 for (@$attach) {
258 my ($klass, @attach) = @$_; 272 my ($klass, @attach) = @$_;
259 $res = _attach @$registry, $klass, @attach; 273 $res = _attach @$registry, $klass, @attach;
260 } 274 }
261 275
262 if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) { 276 $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 { 277 } else {
272 warn "object uses attachment '$name' that is not available, postponing.\n"; 278 warn "object uses attachment '$name' that is not available, postponing.\n";
273 } 279 }
274 280
275 push @{$obj->{_attachment}}, $name; 281 $obj->{_attachment}{$name} = undef;
276 282
277 $res->{attachment} = $name; 283 $res->{attachment} = $name;
278 $res 284 $res
279} 285}
280 286
281sub cf::object::attach { 287sub cf::object::attach {
282 my ($obj, $name, @args) = @_; 288 my ($obj, $name, %arg) = @_;
283 289
284 _attach_attachment $obj, $name, @args; 290 _attach_attachment $obj, $name, %arg;
285} 291}
286 292
287sub cf::player::attach { 293sub cf::player::attach {
288 my ($obj, $name, @args) = @_; 294 my ($obj, $name, %arg) = @_;
289 295
290 _attach_attachment KLASS_PLAYER, $obj, $name, @args; 296 _attach_attachment $obj, $name, %arg;
291} 297}
292 298
293sub cf::map::attach { 299sub cf::map::attach {
294 my ($obj, $name, @args) = @_; 300 my ($obj, $name, %arg) = @_;
295 301
296 _attach_attachment KLASS_MAP, $obj, $name, @args; 302 _attach_attachment $obj, $name, %arg;
303}
304
305sub cf::object::detach {
306 my ($obj, $name) = @_;
307 die;#d#
308}
309sub cf::player::detach {
310 my ($obj, $name) = @_;
311 die;#d#
312}
313sub cf::map::detach {
314 my ($obj, $name) = @_;
315 die;#d#
297} 316}
298 317
299sub attach_global { 318sub attach_global {
300 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 319 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
301} 320}
321 340
322sub register_attachment { 341sub register_attachment {
323 my $name = shift; 342 my $name = shift;
324 343
325 $attachment{$name} = [[KLASS_OBJECT, @_]]; 344 $attachment{$name} = [[KLASS_OBJECT, @_]];
345}
346
347sub register_map_attachment {
348 my $name = shift;
349
350 $attachment{$name} = [[KLASS_MAP, @_]];
326} 351}
327 352
328our $override; 353our $override;
329our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? 354our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
330 355
364 389
365 $data = from_json $data; 390 $data = from_json $data;
366 391
367 for (@$data) { 392 for (@$data) {
368 my ($name, $args) = @$_; 393 my ($name, $args) = @$_;
369 attach $obj, $name, @{$args || [] }; 394
395 $obj->attach ($name, %{$args || {} });
370 } 396 }
371} 397}
372 398
373# basically do the same as instantiate, without calling instantiate 399# basically do the same as instantiate, without calling instantiate
374sub reattach { 400sub reattach {
375 my ($obj) = @_; 401 my ($obj) = @_;
376 my $registry = $obj->registry; 402 my $registry = $obj->registry;
377 403
378 @$registry = (); 404 @$registry = ();
379 405
406 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
407
380 for my $name (@{ $obj->{_attachment} }) { 408 for my $name (keys %{ $obj->{_attachment} || {} }) {
381 if (my $attach = $attachment{$name}) { 409 if (my $attach = $attachment{$name}) {
382 for (@$attach) { 410 for (@$attach) {
383 my ($klass, @attach) = @$_; 411 my ($klass, @attach) = @$_;
384 _attach @$registry, $klass, @attach; 412 _attach @$registry, $klass, @attach;
385 } 413 }
390} 418}
391 419
392sub object_freezer_save { 420sub object_freezer_save {
393 my ($filename, $objs) = @_; 421 my ($filename, $objs) = @_;
394 422
395 $filename .= ".pst";
396
397 if (@$objs) { 423 if (@$objs) {
398 open my $fh, ">:raw", "$filename~"; 424 open my $fh, ">:raw", "$filename.pst~";
399 chmod $fh, SAVE_MODE;
400 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; 425 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
401 close $fh; 426 close $fh;
427 chmod SAVE_MODE, "$filename.pst~";
402 rename "$filename~", $filename; 428 rename "$filename.pst~", "$filename.pst";
403 } else { 429 } else {
404 unlink $filename; 430 unlink "$filename.pst";
405 } 431 }
432
433 chmod SAVE_MODE, "$filename~";
434 rename "$filename~", $filename;
406} 435}
407 436
408sub object_thawer_load { 437sub object_thawer_load {
409 my ($filename) = @_; 438 my ($filename) = @_;
410 439
421 450
422 @{$dst->registry} = @{$src->registry}; 451 @{$dst->registry} = @{$src->registry};
423 452
424 %$dst = %$src; 453 %$dst = %$src;
425 454
426 $dst->{_attachment} = [@{ $src->{_attachment} }] 455 %{$dst->{_attachment}} = %{$src->{_attachment}}
427 if exists $src->{_attachment}; 456 if exists $src->{_attachment};
428 }, 457 },
429; 458;
430 459
431############################################################################# 460#############################################################################
672 my ($map) = @_; 701 my ($map) = @_;
673 702
674 my $path = $map->tmpname; 703 my $path = $map->tmpname;
675 defined $path or return; 704 defined $path or return;
676 705
677 unlink "$path.cfperl";
678 unlink "$path.pst"; 706 unlink "$path.pst";
679}; 707};
680 708
709# old style persistent data, TODO: remove #d#
681*cf::mapsupport::on_swapin = 710*cf::mapsupport::on_swapin =
682*cf::mapsupport::on_load = sub { 711*cf::mapsupport::on_load = sub {
683 my ($map) = @_; 712 my ($map) = @_;
684 713
685 my $path = $map->tmpname; 714 my $path = $map->tmpname;
692 721
693 $data->{version} <= 1 722 $data->{version} <= 1
694 or return; # too new 723 or return; # too new
695 724
696 $map->_set_obs ($data->{obs}); 725 $map->_set_obs ($data->{obs});
726 $map->invoke (EVENT_MAP_UPGRADE);
697}; 727};
698 728
699attach_to_maps prio => -10000, package => cf::mapsupport::; 729attach_to_maps prio => -10000, package => cf::mapsupport::;
700 730
701############################################################################# 731#############################################################################

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines