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.54 by root, Tue Aug 29 14:49:28 2006 UTC vs.
Revision 1.62 by root, Fri Sep 8 16:51:44 2006 UTC

15 15
16_reload_1; 16_reload_1;
17 17
18our %COMMAND = (); 18our %COMMAND = ();
19our @EVENT; 19our @EVENT;
20our %PROP_TYPE;
21our %PROP_IDX;
22our $LIBDIR = maps_directory "perl"; 20our $LIBDIR = maps_directory "perl";
23 21
24our $TICK = MAX_TIME * 1e-6; 22our $TICK = MAX_TIME * 1e-6;
25our $TICK_WATCHER; 23our $TICK_WATCHER;
26our $NEXT_TICK; 24our $NEXT_TICK;
34 print STDERR "cfperl: $msg"; 32 print STDERR "cfperl: $msg";
35 LOG llevError, "cfperl: $msg"; 33 LOG llevError, "cfperl: $msg";
36 }; 34 };
37} 35}
38 36
39my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply!
40
41# generate property mutators
42sub prop_gen {
43 my ($prefix, $class) = @_;
44
45 no strict 'refs';
46
47 for my $prop (keys %PROP_TYPE) {
48 $prop =~ /^\Q$prefix\E_(.*$)/ or next;
49 my $sub = lc $1;
50
51 my $type = $PROP_TYPE{$prop};
52 my $idx = $PROP_IDX {$prop};
53
54 *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub {
55 $_[0]->get_property ($type, $idx)
56 };
57
58 *{"$class\::set_$sub"} = sub {
59 $_[0]->set_property ($type, $idx, $_[1]);
60 } unless $ignore_set{$prop};
61 }
62}
63
64# auto-generate most of the API
65
66prop_gen OBJECT_PROP => "cf::object";
67# CFAPI_OBJECT_ANIMATION?
68prop_gen PLAYER_PROP => "cf::object::player";
69
70prop_gen MAP_PROP => "cf::map";
71prop_gen ARCH_PROP => "cf::arch";
72
73@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 37@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
74 38
75# we bless all objects into (empty) derived classes to force a method lookup 39# we bless all objects into (empty) derived classes to force a method lookup
76# within the Safe compartment. 40# within the Safe compartment.
77for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { 41for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) {
105} 69}
106 70
107############################################################################# 71#############################################################################
108# "new" plug-in system 72# "new" plug-in system
109 73
74=head3 EVENTS AND OBJECT ATTACHMENTS
75
76=over 4
77
110=item $object->attach ($attachment, key => $value...) 78=item $object->attach ($attachment, key => $value...)
111 79
112=item $object->detach ($attachment) 80=item $object->detach ($attachment)
113 81
114Attach/detach a pre-registered attachment to an object. 82Attach/detach a pre-registered attachment to an object.
122=item $map->attach ($attachment, key => $value...) 90=item $map->attach ($attachment, key => $value...)
123 91
124=item $map->detach ($attachment) 92=item $map->detach ($attachment)
125 93
126Attach/detach a pre-registered attachment to a map. 94Attach/detach a pre-registered attachment to a map.
95
96=item $bool = $object->attached ($name)
97
98=item $bool = $player->attached ($name)
99
100=item $bool = $map->attached ($name)
101
102Checks wether the named attachment is currently attached to the object.
127 103
128=item cf::attach_global ... 104=item cf::attach_global ...
129 105
130Attach handlers for global events. 106Attach handlers for global events.
131 107
181Attach handlers to all maps. 157Attach handlers to all maps.
182 158
183=item cf:register_attachment $name, ... 159=item cf:register_attachment $name, ...
184 160
185Register an attachment by name through which objects can refer to this 161Register an attachment by name through which objects can refer to this
162attachment.
163
164=item cf:register_player_attachment $name, ...
165
166Register an attachment by name through which players can refer to this
186attachment. 167attachment.
187 168
188=item cf:register_map_attachment $name, ... 169=item cf:register_map_attachment $name, ...
189 170
190Register an attachment by name through which maps can refer to this 171Register an attachment by name through which maps can refer to this
261} 242}
262 243
263sub _attach_attachment { 244sub _attach_attachment {
264 my ($obj, $name, %arg) = @_; 245 my ($obj, $name, %arg) = @_;
265 246
247 return if exists $obj->{_attachment}{$name};
248
266 my $res; 249 my $res;
267 250
268 if (my $attach = $attachment{$name}) { 251 if (my $attach = $attachment{$name}) {
269 my $registry = $obj->registry; 252 my $registry = $obj->registry;
270 253
288*cf::player::attach = 271*cf::player::attach =
289*cf::map::attach = sub { 272*cf::map::attach = sub {
290 my ($obj, $name, %arg) = @_; 273 my ($obj, $name, %arg) = @_;
291 274
292 _attach_attachment $obj, $name, %arg; 275 _attach_attachment $obj, $name, %arg;
293} 276};
294 277
295# all those should be optimised 278# all those should be optimised
296*cf::object::detach = 279*cf::object::detach =
297*cf::player::detach = 280*cf::player::detach =
298*cf::map::detach = sub { 281*cf::map::detach = sub {
299 my ($obj, $name) = @_; 282 my ($obj, $name) = @_;
300 283
301 delete $obj->{_attachment}{$name}; 284 delete $obj->{_attachment}{$name};
302 reattach $obj; 285 reattach ($obj);
286};
287
288*cf::object::attached =
289*cf::player::attached =
290*cf::map::attached = sub {
291 my ($obj, $name) = @_;
292
293 exists $obj->{_attachment}{$name}
303}; 294};
304 295
305sub attach_global { 296sub attach_global {
306 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 297 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
307} 298}
327 318
328sub register_attachment { 319sub register_attachment {
329 my $name = shift; 320 my $name = shift;
330 321
331 $attachment{$name} = [[KLASS_OBJECT, @_]]; 322 $attachment{$name} = [[KLASS_OBJECT, @_]];
323}
324
325sub register_player_attachment {
326 my $name = shift;
327
328 $attachment{$name} = [[KLASS_PLAYER, @_]];
332} 329}
333 330
334sub register_map_attachment { 331sub register_map_attachment {
335 my $name = shift; 332 my $name = shift;
336 333
365 return 1 if $override; 362 return 1 if $override;
366 } 363 }
367 364
368 0 365 0
369} 366}
367
368=item $bool = cf::invoke EVENT_GLOBAL_XXX, ...
369
370=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
371
372=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
373
374=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
375
376Generate a global/object/player/map-specific event with the given arguments.
377
378This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be
379removed in future versions), and there is no public API to access override
380results (if you must, access C<@cf::invoke_results> directly).
381
382=back
383
384=head2 methods valid for all pointers
385
386=over 4
387
388=item $object->valid
389
390=item $player->valid
391
392=item $map->valid
393
394Just because you have a perl object does not mean that the corresponding
395C-level object still exists. If you try to access an object that has no
396valid C counterpart anymore you get an exception at runtime. This method
397can be used to test for existence of the C object part without causing an
398exception.
399
400=back
401
402=cut
403
404*cf::object::valid =
405*cf::player::valid =
406*cf::map::valid = \&cf::_valid;
370 407
371############################################################################# 408#############################################################################
372# object support 409# object support
373 410
374sub instantiate { 411sub instantiate {
403 } 440 }
404 } 441 }
405} 442}
406 443
407sub object_freezer_save { 444sub object_freezer_save {
408 my ($filename, $objs) = @_; 445 my ($filename, $rdata, $objs) = @_;
409 446
410 if (@$objs) { 447 if (length $$rdata) {
448 warn sprintf "saving %s (%d,%d)\n",
449 $filename, length $$rdata, scalar @$objs;
450
411 open my $fh, ">:raw", "$filename.pst~"; 451 if (open my $fh, ">:raw", "$filename~") {
452 chmod SAVE_MODE, $fh;
453 syswrite $fh, $$rdata;
454 close $fh;
455
456 if (@$objs && open my $fh, ">:raw", "$filename.pst~") {
457 chmod SAVE_MODE, $fh;
412 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; 458 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
413 close $fh; 459 close $fh;
414 chmod SAVE_MODE, "$filename.pst~";
415 rename "$filename.pst~", "$filename.pst"; 460 rename "$filename.pst~", "$filename.pst";
461 } else {
462 unlink "$filename.pst";
463 }
464
465 rename "$filename~", $filename;
466 } else {
467 warn "FATAL: $filename~: $!\n";
468 }
416 } else { 469 } else {
470 unlink $filename;
417 unlink "$filename.pst"; 471 unlink "$filename.pst";
418 } 472 }
419
420 chmod SAVE_MODE, "$filename~";
421 rename "$filename~", $filename;
422} 473}
423 474
424sub object_thawer_load { 475sub object_thawer_load {
425 my ($filename) = @_; 476 my ($filename) = @_;
426 477
478 local $/;
479
480 my $av;
481
482 #TODO: use sysread etc.
483 if (open my $data, "<:raw:perlio", $filename) {
484 $data = <$data>;
427 open my $fh, "<:raw:perlio", "$filename.pst" 485 if (open my $pst, "<:raw:perlio", "$filename.pst") {
428 or return; 486 $av = eval { (Storable::thaw <$pst>)->{objs} };
487 }
488 return ($data, $av);
489 }
429 490
430 eval { local $/; (Storable::thaw <$fh>)->{objs} } 491 ()
431} 492}
432 493
433attach_to_objects 494attach_to_objects
434 prio => -1000000, 495 prio => -1000000,
435 on_clone => sub { 496 on_clone => sub {
631 #Symbol::delete_package __PACKAGE__; 692 #Symbol::delete_package __PACKAGE__;
632 693
633 # 7. reload cf.pm 694 # 7. reload cf.pm
634 $msg->("reloading cf.pm"); 695 $msg->("reloading cf.pm");
635 require cf; 696 require cf;
697
698 $msg->("load extensions");
699 cf::load_extensions;
636 }; 700 };
637 $msg->($@) if $@; 701 $msg->($@) if $@;
638 702
639 $msg->("reloaded"); 703 $msg->("reloaded");
640}; 704};
691 defined $path or return; 755 defined $path or return;
692 756
693 unlink "$path.pst"; 757 unlink "$path.pst";
694}; 758};
695 759
696# old style persistent data, TODO: remove #d#
697*cf::mapsupport::on_swapin =
698*cf::mapsupport::on_load = sub {
699 my ($map) = @_;
700
701 my $path = $map->tmpname;
702 $path = $map->path unless defined $path;
703
704 open my $fh, "<:raw", "$path.cfperl"
705 or return; # no perl data
706
707 my $data = Storable::thaw do { local $/; <$fh> };
708
709 $data->{version} <= 1
710 or return; # too new
711
712 $map->_set_obs ($data->{obs});
713 $map->invoke (EVENT_MAP_UPGRADE);
714};
715
716attach_to_maps prio => -10000, package => cf::mapsupport::; 760attach_to_maps prio => -10000, package => cf::mapsupport::;
717 761
718############################################################################# 762#############################################################################
719# load/save perl data associated with player->ob objects 763# load/save perl data associated with player->ob objects
720 764
721sub all_objects(@) { 765sub all_objects(@) {
722 @_, map all_objects ($_->inv), @_ 766 @_, map all_objects ($_->inv), @_
723} 767}
724 768
769# TODO: compatibility cruft, remove when no longer needed
725attach_to_players 770attach_to_players
726 on_load => sub { 771 on_load => sub {
727 my ($pl, $path) = @_; 772 my ($pl, $path) = @_;
728 773
729 for my $o (all_objects $pl->ob) { 774 for my $o (all_objects $pl->ob) {
850 895
851############################################################################# 896#############################################################################
852# the server's main() 897# the server's main()
853 898
854sub main { 899sub main {
900 load_extensions;
855 Event::loop; 901 Event::loop;
856} 902}
857 903
858############################################################################# 904#############################################################################
859# initialisation 905# initialisation
860 906
861register "<global>", __PACKAGE__; 907register "<global>", __PACKAGE__;
862 908
863unshift @INC, $LIBDIR; 909unshift @INC, $LIBDIR;
864
865load_extensions;
866 910
867$TICK_WATCHER = Event->timer ( 911$TICK_WATCHER = Event->timer (
868 prio => 1, 912 prio => 1,
869 at => $NEXT_TICK || 1, 913 at => $NEXT_TICK || 1,
870 cb => sub { 914 cb => sub {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines