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.44 by root, Sat Aug 26 08:44:06 2006 UTC vs.
Revision 1.55 by root, Tue Aug 29 17:32:19 2006 UTC

11use Event; 11use Event;
12$Event::Eval = 1; # no idea why this is required, but it is 12$Event::Eval = 1; # no idea why this is required, but it is
13 13
14use strict; 14use strict;
15 15
16_reload_1;
17
16our %COMMAND = (); 18our %COMMAND = ();
17our @EVENT; 19our @EVENT;
18our @PLUGIN_EVENT;
19our %PROP_TYPE; 20our %PROP_TYPE;
20our %PROP_IDX; 21our %PROP_IDX;
21our $LIBDIR = maps_directory "perl"; 22our $LIBDIR = maps_directory "perl";
22 23
23our $TICK = MAX_TIME * 1e-6; 24our $TICK = MAX_TIME * 1e-6;
24our $TICK_WATCHER; 25our $TICK_WATCHER;
25our $NEXT_TICK; 26our $NEXT_TICK;
26 27
27BEGIN { 28BEGIN {
28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
29
30 *CORE::GLOBAL::warn = sub { 29 *CORE::GLOBAL::warn = sub {
31 my $msg = join "", @_; 30 my $msg = join "", @_;
32 $msg .= "\n" 31 $msg .= "\n"
33 unless $msg =~ /\n$/; 32 unless $msg =~ /\n$/;
34 33
69prop_gen PLAYER_PROP => "cf::object::player"; 68prop_gen PLAYER_PROP => "cf::object::player";
70 69
71prop_gen MAP_PROP => "cf::map"; 70prop_gen MAP_PROP => "cf::map";
72prop_gen ARCH_PROP => "cf::arch"; 71prop_gen ARCH_PROP => "cf::arch";
73 72
74# guessed hierarchies
75
76@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 73@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
77@safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
78 74
79# 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
80# within the Safe compartment. 76# within the Safe compartment.
81for my $pkg (qw(cf::object cf::object::map 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)) {
82 no strict 'refs'; 78 no strict 'refs';
83 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 79 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
84} 80}
85 81
86$Event::DIED = sub { 82$Event::DIED = sub {
92my @hook; 88my @hook;
93my %command; 89my %command;
94my %extcmd; 90my %extcmd;
95 91
96############################################################################# 92#############################################################################
97# object support 93# utility functions
98 94
99sub reattach { 95use JSON::Syck (); # TODO# replace by JSON::PC once working
100 warn "reattach<@_>\n";
101}
102 96
103sub instantiate { 97sub from_json($) {
104 warn "instantiate<@_>\n"; 98 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
99 JSON::Syck::Load $_[0]
105} 100}
106 101
107sub clone { 102sub to_json($) {
108 warn "clone<@_>\n"; 103 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
104 JSON::Syck::Dump $_[0]
109} 105}
110 106
111############################################################################# 107#############################################################################
112# "new" plug-in system 108# "new" plug-in system
113 109
114=item cf::object::attach ... # NYI 110=head3 EVENTS AND OBJECT ATTACHMENTS
111
112=over 4
113
114=item $object->attach ($attachment, key => $value...)
115
116=item $object->detach ($attachment)
117
118Attach/detach a pre-registered attachment to an object.
119
120=item $player->attach ($attachment, key => $value...)
121
122=item $player->detach ($attachment)
123
124Attach/detach a pre-registered attachment to a player.
125
126=item $map->attach ($attachment, key => $value...)
127
128=item $map->detach ($attachment)
129
130Attach/detach a pre-registered attachment to a map.
131
132=item $bool = $object->attached ($name)
133
134=item $bool = $player->attached ($name)
135
136=item $bool = $map->attached ($name)
137
138Checks wether the named attachment is currently attached to the object.
115 139
116=item cf::attach_global ... 140=item cf::attach_global ...
117 141
118=item cf::attach_to_type ... 142Attach handlers for global events.
143
144This and all following C<attach_*>-functions expect any number of the
145following handler/hook descriptions:
146
147=over 4
148
149=item prio => $number
150
151Set the priority for all following handlers/hooks (unless overwritten
152by another C<prio> setting). Lower priority handlers get executed
153earlier. The default priority is C<0>, and many built-in handlers are
154registered at priority C<-1000>, so lower priorities should not be used
155unless you know what you are doing.
156
157=item on_I<event> => \&cb
158
159Call the given code reference whenever the named event happens (event is
160something like C<instantiate>, C<apply>, C<use_skill> and so on, and which
161handlers are recognised generally depends on the type of object these
162handlers attach to).
163
164See F<include/eventinc.h> for the full list of events supported, and their
165class.
166
167=item package => package::
168
169Look for sub functions of the name C<< on_I<event> >> in the given
170package and register them. Only handlers for eevents supported by the
171object/class are recognised.
172
173=back
174
175=item cf::attach_to_type $object_type, $subtype, ...
176
177Attach handlers for a specific object type (e.g. TRANSPORT) and
178subtype. If C<$subtype> is zero or undef, matches all objects of the given
179type.
119 180
120=item cf::attach_to_objects ... 181=item cf::attach_to_objects ...
121 182
183Attach handlers to all objects. Do not use this except for debugging or
184very rare events, as handlers are (obviously) called for I<all> objects in
185the game.
186
122=item cf::attach_to_players ... 187=item cf::attach_to_players ...
123 188
189Attach handlers to all players.
190
124=item cf::attach_to_maps ... 191=item cf::attach_to_maps ...
125 192
126 prio => $number, # lower is earlier 193Attach handlers to all maps.
127 on_xxx => \&cb, 194
128 package => package::, 195=item cf:register_attachment $name, ...
196
197Register an attachment by name through which objects can refer to this
198attachment.
199
200=item cf:register_player_attachment $name, ...
201
202Register an attachment by name through which players can refer to this
203attachment.
204
205=item cf:register_map_attachment $name, ...
206
207Register an attachment by name through which maps can refer to this
208attachment.
129 209
130=cut 210=cut
131 211
132# the following variables are defined in .xs and must not be re-created 212# the following variables are defined in .xs and must not be re-created
133our @CB_GLOBAL = (); # registry for all global events 213our @CB_GLOBAL = (); # registry for all global events
134our @CB_OBJECT = (); 214our @CB_OBJECT = (); # all objects (should not be used except in emergency)
135our @CB_PLAYER = (); 215our @CB_PLAYER = ();
136our @CB_TYPE = (); # registry for type (cf-object class) based events 216our @CB_TYPE = (); # registry for type (cf-object class) based events
137our @CB_MAP = (); 217our @CB_MAP = ();
138 218
219my %attachment;
220
139sub _attach_cb($\%$$$) { 221sub _attach_cb($\%$$$) {
140 my ($registry, $undo, $event, $prio, $cb) = @_; 222 my ($registry, $undo, $event, $prio, $cb) = @_;
141 223
142 use sort 'stable'; 224 use sort 'stable';
143 225
151} 233}
152 234
153# attach handles attaching event callbacks 235# attach handles attaching event callbacks
154# the only thing the caller has to do is pass the correct 236# the only thing the caller has to do is pass the correct
155# registry (== where the callback attaches to). 237# registry (== where the callback attaches to).
156sub _attach(\@$\@) { 238sub _attach(\@$@) {
157 my ($registry, $klass, $arg) = @_; 239 my ($registry, $klass, @arg) = @_;
158 240
159 my $prio = 0; 241 my $prio = 0;
160 242
161 my %undo = ( 243 my %undo = (
162 registry => $registry, 244 registry => $registry,
163 cb => [], 245 cb => [],
164 ); 246 );
165 247
166 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; 248 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
167 249
168 while (@$arg) { 250 while (@arg) {
169 my $type = shift @$arg; 251 my $type = shift @arg;
170 252
171 if ($type eq "prio") { 253 if ($type eq "prio") {
172 $prio = shift @$arg; 254 $prio = shift @arg;
173 255
174 } elsif ($type eq "package") { 256 } elsif ($type eq "package") {
175 my $pkg = shift @$arg; 257 my $pkg = shift @arg;
176 258
177 while (my ($name, $id) = each %cb_id) { 259 while (my ($name, $id) = each %cb_id) {
178 if (my $cb = $pkg->can ($name)) { 260 if (my $cb = $pkg->can ($name)) {
179 _attach_cb $registry, %undo, $id, $prio, $cb; 261 _attach_cb $registry, %undo, $id, $prio, $cb;
180 } 262 }
181 } 263 }
182 264
183 } elsif (exists $cb_id{$type}) { 265 } elsif (exists $cb_id{$type}) {
184 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; 266 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg;
185 267
186 } elsif (ref $type) { 268 } elsif (ref $type) {
187 warn "attaching objects not supported, ignoring.\n"; 269 warn "attaching objects not supported, ignoring.\n";
188 270
189 } else { 271 } else {
190 shift @$arg; 272 shift @arg;
191 warn "attach argument '$type' not supported, ignoring.\n"; 273 warn "attach argument '$type' not supported, ignoring.\n";
192 } 274 }
193 } 275 }
194 276
195 \%undo 277 \%undo
196} 278}
197 279
280sub _attach_attachment {
281 my ($obj, $name, %arg) = @_;
282
283 return if exists $obj->{_attachment}{$name};
284
285 my $res;
286
287 if (my $attach = $attachment{$name}) {
288 my $registry = $obj->registry;
289
290 for (@$attach) {
291 my ($klass, @attach) = @$_;
292 $res = _attach @$registry, $klass, @attach;
293 }
294
295 $obj->{$name} = \%arg;
296 } else {
297 warn "object uses attachment '$name' that is not available, postponing.\n";
298 }
299
300 $obj->{_attachment}{$name} = undef;
301
302 $res->{attachment} = $name;
303 $res
304}
305
198sub cf::object::attach { 306*cf::object::attach =
199 die; 307*cf::player::attach =
200} 308*cf::map::attach = sub {
309 my ($obj, $name, %arg) = @_;
310
311 _attach_attachment $obj, $name, %arg;
312};
313
314# all those should be optimised
315*cf::object::detach =
316*cf::player::detach =
317*cf::map::detach = sub {
318 my ($obj, $name) = @_;
319
320 delete $obj->{_attachment}{$name};
321 reattach ($obj);
322};
323
324*cf::object::attached =
325*cf::player::attached =
326*cf::map::attached = sub {
327 my ($obj, $name) = @_;
328
329 exists $obj->{_attachment}{$name}
330};
201 331
202sub attach_global { 332sub attach_global {
203 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 333 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
204} 334}
205 335
206sub attach_to_type { 336sub attach_to_type {
207 my $type = shift; 337 my $type = shift;
338 my $subtype = shift;
339
208 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ 340 _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_
209} 341}
210 342
211sub attach_to_objects { 343sub attach_to_objects {
212 _attach @CB_OBJECT, KLASS_OBJECT, @_ 344 _attach @CB_OBJECT, KLASS_OBJECT, @_
213} 345}
218 350
219sub attach_to_maps { 351sub attach_to_maps {
220 _attach @CB_MAP, KLASS_MAP, @_ 352 _attach @CB_MAP, KLASS_MAP, @_
221} 353}
222 354
355sub register_attachment {
356 my $name = shift;
357
358 $attachment{$name} = [[KLASS_OBJECT, @_]];
359}
360
361sub register_player_attachment {
362 my $name = shift;
363
364 $attachment{$name} = [[KLASS_PLAYER, @_]];
365}
366
367sub register_map_attachment {
368 my $name = shift;
369
370 $attachment{$name} = [[KLASS_MAP, @_]];
371}
372
223our $override; 373our $override;
374our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
224 375
225sub override() { 376sub override {
226 $override = 1 377 $override = 1;
378 @invoke_results = ();
227} 379}
228 380
229sub invoke { 381sub do_invoke {
230 my $event = shift; 382 my $event = shift;
231 my $callbacks = shift; 383 my $callbacks = shift;
384
385 @invoke_results = ();
232 386
233 local $override; 387 local $override;
234 388
235 for (@$callbacks) { 389 for (@$callbacks) {
236 eval { &{$_->[1]} }; 390 eval { &{$_->[1]} };
245 } 399 }
246 400
247 0 401 0
248} 402}
249 403
404=item $bool = cf::invoke EVENT_GLOBAL_XXX, ...
405
406=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
407
408=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
409
410=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
411
412Generate a global/object/player/map-specific event with the given arguments.
413
414This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be
415removed in future versions), and there is no public API to access override
416results (if you must, access C<@cf::invoke_results> directly).
417
418=back
419
420=head2 methods valid for all pointers
421
422=over 4
423
424=item $object->valid
425
426=item $player->valid
427
428=item $map->valid
429
430Just because you have a perl object does not mean that the corresponding
431C-level object still exists. If you try to access an object that has no
432valid C counterpart anymore you get an exception at runtime. This method
433can be used to test for existence of the C object part without causing an
434exception.
435
436=back
437
438=cut
439
440*cf::object::valid =
441*cf::player::valid =
442*cf::map::valid = \&cf::_valid;
443
444#############################################################################
445# object support
446
447sub instantiate {
448 my ($obj, $data) = @_;
449
450 $data = from_json $data;
451
452 for (@$data) {
453 my ($name, $args) = @$_;
454
455 $obj->attach ($name, %{$args || {} });
456 }
457}
458
459# basically do the same as instantiate, without calling instantiate
460sub reattach {
461 my ($obj) = @_;
462 my $registry = $obj->registry;
463
464 @$registry = ();
465
466 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
467
468 for my $name (keys %{ $obj->{_attachment} || {} }) {
469 if (my $attach = $attachment{$name}) {
470 for (@$attach) {
471 my ($klass, @attach) = @$_;
472 _attach @$registry, $klass, @attach;
473 }
474 } else {
475 warn "object uses attachment '$name' that is not available, postponing.\n";
476 }
477 }
478}
479
480sub object_freezer_save {
481 my ($filename, $objs) = @_;
482
483 if (@$objs) {
484 open my $fh, ">:raw", "$filename.pst~";
485 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
486 close $fh;
487 chmod SAVE_MODE, "$filename.pst~";
488 rename "$filename.pst~", "$filename.pst";
489 } else {
490 unlink "$filename.pst";
491 }
492
493 chmod SAVE_MODE, "$filename~";
494 rename "$filename~", $filename;
495}
496
497sub object_thawer_load {
498 my ($filename) = @_;
499
500 open my $fh, "<:raw:perlio", "$filename.pst"
501 or return;
502
503 eval { local $/; (Storable::thaw <$fh>)->{objs} }
504}
505
506attach_to_objects
507 prio => -1000000,
508 on_clone => sub {
509 my ($src, $dst) = @_;
510
511 @{$dst->registry} = @{$src->registry};
512
513 %$dst = %$src;
514
515 %{$dst->{_attachment}} = %{$src->{_attachment}}
516 if exists $src->{_attachment};
517 },
518;
519
250############################################################################# 520#############################################################################
251# old plug-in events 521# old plug-in events
252 522
253sub inject_event { 523sub inject_event {
254 my $extension = shift; 524 my $extension = shift;
299} 569}
300 570
301sub register { 571sub register {
302 my ($base, $pkg) = @_; 572 my ($base, $pkg) = @_;
303 573
304 for my $idx (0 .. $#PLUGIN_EVENT) { 574 #TODO
305 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
306 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
307 $hook[$idx]{$base} = $ref;
308 }
309 }
310} 575}
311 576
312sub load_extension { 577sub load_extension {
313 my ($path) = @_; 578 my ($path) = @_;
314 579
345 my ($pkg) = @_; 610 my ($pkg) = @_;
346 611
347 warn "removing extension $pkg\n"; 612 warn "removing extension $pkg\n";
348 613
349 # remove hooks 614 # remove hooks
615 #TODO
350 for my $idx (0 .. $#PLUGIN_EVENT) { 616# for my $idx (0 .. $#PLUGIN_EVENT) {
351 delete $hook[$idx]{$pkg}; 617# delete $hook[$idx]{$pkg};
352 } 618# }
353 619
354 # remove commands 620 # remove commands
355 for my $name (keys %command) { 621 for my $name (keys %command) {
356 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 622 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
357 623
423 Symbol::delete_package $k; 689 Symbol::delete_package $k;
424 } 690 }
425 691
426 # 4. get rid of safe::, as good as possible 692 # 4. get rid of safe::, as good as possible
427 Symbol::delete_package "safe::$_" 693 Symbol::delete_package "safe::$_"
428 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); 694 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
429 695
430 # 5. remove register_script_function callbacks 696 # 5. remove register_script_function callbacks
431 # TODO 697 # TODO
432 698
433 # 6. unload cf.pm "a bit" 699 # 6. unload cf.pm "a bit"
461 warn $_[0]; 727 warn $_[0];
462 $who->message ($_[0]); 728 $who->message ($_[0]);
463 }; 729 };
464 } 730 }
465}; 731};
466
467#############################################################################
468# utility functions
469
470use JSON::Syck (); # TODO# replace by JSON::PC once working
471
472sub from_json($) {
473 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
474 JSON::Syck::Load $_[0]
475}
476
477sub to_json($) {
478 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
479 JSON::Syck::Dump $_[0]
480}
481 732
482############################################################################# 733#############################################################################
483# extcmd framework, basically convert ext <msg> 734# extcmd framework, basically convert ext <msg>
484# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 735# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
485 736
510 my ($map) = @_; 761 my ($map) = @_;
511 762
512 my $path = $map->tmpname; 763 my $path = $map->tmpname;
513 defined $path or return; 764 defined $path or return;
514 765
515 unlink "$path.cfperl"; 766 unlink "$path.pst";
516}; 767};
517 768
769# old style persistent data, TODO: remove #d#
518*cf::mapsupport::on_swapin = 770*cf::mapsupport::on_swapin =
519*cf::mapsupport::on_load = sub { 771*cf::mapsupport::on_load = sub {
520 my ($map) = @_; 772 my ($map) = @_;
521 773
522 my $path = $map->tmpname; 774 my $path = $map->tmpname;
529 781
530 $data->{version} <= 1 782 $data->{version} <= 1
531 or return; # too new 783 or return; # too new
532 784
533 $map->_set_obs ($data->{obs}); 785 $map->_set_obs ($data->{obs});
534}; 786 $map->invoke (EVENT_MAP_UPGRADE);
535
536*cf::mapsupport::on_swapout = sub {
537 my ($map) = @_;
538
539 my $path = $map->tmpname;
540 $path = $map->path unless defined $path;
541
542 my $obs = $map->_get_obs;
543
544 if (defined $obs) {
545 open my $fh, ">:raw", "$path.cfperl"
546 or die "$path.cfperl: $!";
547
548 stat $path;
549
550 print $fh Storable::nfreeze {
551 size => (stat _)[7],
552 time => (stat _)[9],
553 version => 1,
554 obs => $obs,
555 };
556
557 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
558 } else {
559 unlink "$path.cfperl";
560 }
561}; 787};
562 788
563attach_to_maps prio => -10000, package => cf::mapsupport::; 789attach_to_maps prio => -10000, package => cf::mapsupport::;
564 790
565############################################################################# 791#############################################################################
578 $o->set_ob_key_value ("_perl_data"); 804 $o->set_ob_key_value ("_perl_data");
579 805
580 %$o = %{ Storable::thaw pack "H*", $value }; 806 %$o = %{ Storable::thaw pack "H*", $value };
581 } 807 }
582 } 808 }
583 },
584 on_save => sub {
585 my ($pl, $path) = @_;
586
587 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
588 for grep %$_, all_objects $pl->ob;
589 }, 809 },
590; 810;
591 811
592############################################################################# 812#############################################################################
593# core extensions - in perl 813# core extensions - in perl
732 $TICK_WATCHER->at ($NEXT_TICK); 952 $TICK_WATCHER->at ($NEXT_TICK);
733 $TICK_WATCHER->start; 953 $TICK_WATCHER->start;
734 }, 954 },
735); 955);
736 956
957_reload_2;
958
7371 9591
738 960

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines