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.46 by root, Sun Aug 27 16:15:12 2006 UTC

13 13
14use strict; 14use strict;
15 15
16our %COMMAND = (); 16our %COMMAND = ();
17our @EVENT; 17our @EVENT;
18our @PLUGIN_EVENT;
19our %PROP_TYPE; 18our %PROP_TYPE;
20our %PROP_IDX; 19our %PROP_IDX;
21our $LIBDIR = maps_directory "perl"; 20our $LIBDIR = maps_directory "perl";
22 21
23our $TICK = MAX_TIME * 1e-6; 22our $TICK = MAX_TIME * 1e-6;
24our $TICK_WATCHER; 23our $TICK_WATCHER;
25our $NEXT_TICK; 24our $NEXT_TICK;
26 25
27BEGIN { 26BEGIN {
28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
29
30 *CORE::GLOBAL::warn = sub { 27 *CORE::GLOBAL::warn = sub {
31 my $msg = join "", @_; 28 my $msg = join "", @_;
32 $msg .= "\n" 29 $msg .= "\n"
33 unless $msg =~ /\n$/; 30 unless $msg =~ /\n$/;
34 31
69prop_gen PLAYER_PROP => "cf::object::player"; 66prop_gen PLAYER_PROP => "cf::object::player";
70 67
71prop_gen MAP_PROP => "cf::map"; 68prop_gen MAP_PROP => "cf::map";
72prop_gen ARCH_PROP => "cf::arch"; 69prop_gen ARCH_PROP => "cf::arch";
73 70
74# guessed hierarchies
75
76@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 71@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
77@safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
78 72
79# we bless all objects into (empty) derived classes to force a method lookup 73# we bless all objects into (empty) derived classes to force a method lookup
80# within the Safe compartment. 74# 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)) { 75for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) {
82 no strict 'refs'; 76 no strict 'refs';
83 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 77 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
84} 78}
85 79
86$Event::DIED = sub { 80$Event::DIED = sub {
92my @hook; 86my @hook;
93my %command; 87my %command;
94my %extcmd; 88my %extcmd;
95 89
96############################################################################# 90#############################################################################
97# object support 91# utility functions
98 92
99sub reattach { 93use JSON::Syck (); # TODO# replace by JSON::PC once working
100 warn "reattach<@_>\n";
101}
102 94
103sub instantiate { 95sub from_json($) {
104 warn "instantiate<@_>\n"; 96 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
97 JSON::Syck::Load $_[0]
105} 98}
106 99
107sub clone { 100sub to_json($) {
108 warn "clone<@_>\n"; 101 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
102 JSON::Syck::Dump $_[0]
109} 103}
110 104
111############################################################################# 105#############################################################################
112# "new" plug-in system 106# "new" plug-in system
113 107
114=item cf::object::attach ... # NYI 108=item $object->attach ($attachment, ...)
109
110Attach a pre-registered attachment to an object.
111
112=item $player->attach ($attachment, ...)
113
114Attach a pre-registered attachment to a player.
115
116=item $map->attach ($attachment, ...) # not yet persistent
117
118Attach a pre-registered attachment to a map.
115 119
116=item cf::attach_global ... 120=item cf::attach_global ...
117 121
122Attach handlers for global events.
123
124This and all following C<attach_*>-functions expect any number of the
125following handler/hook descriptions:
126
127=over 4
128
129=item prio => $number
130
131Set the priority for all following handlers/hooks (unless overwritten
132by another C<prio> setting). Lower priority handlers get executed
133earlier. The default priority is C<0>, and many built-in handlers are
134registered at priority C<-1000>, so lower priorities should not be used
135unless you know what you are doing.
136
137=item on_I<event> => \&cb
138
139Call the given code reference whenever the named event happens (event is
140something like C<instantiate>, C<apply>, C<use_skill> and so on, and which
141handlers are recognised generally depends on the type of object these
142handlers attach to).
143
144See F<include/eventinc.h> for the full list of events supported, and their
145class.
146
147=item package => package::
148
149Look for sub functions of the name C<< on_I<event> >> in the given
150package and register them. Only handlers for eevents supported by the
151object/class are recognised.
152
153=back
154
118=item cf::attach_to_type ... 155=item cf::attach_to_type $object_type, ...
156
157Attach handlers for a specific object type (e.g. TRANSPORT).
119 158
120=item cf::attach_to_objects ... 159=item cf::attach_to_objects ...
121 160
161Attach handlers to all objects. Do not use this except for debugging or
162very rare events, as handlers are (obviously) called for I<all> objects in
163the game.
164
122=item cf::attach_to_players ... 165=item cf::attach_to_players ...
123 166
167Attach handlers to all players.
168
124=item cf::attach_to_maps ... 169=item cf::attach_to_maps ...
125 170
126 prio => $number, # lower is earlier 171Attach handlers to all maps.
127 on_xxx => \&cb, 172
128 package => package::, 173=item cf:register_attachment $name, ...
129 174
130=cut 175=cut
131 176
132# the following variables are defined in .xs and must not be re-created 177# the following variables are defined in .xs and must not be re-created
133our @CB_GLOBAL = (); # registry for all global events 178our @CB_GLOBAL = (); # registry for all global events
134our @CB_OBJECT = (); 179our @CB_OBJECT = (); # all objects (should not be used except in emergency)
135our @CB_PLAYER = (); 180our @CB_PLAYER = ();
136our @CB_TYPE = (); # registry for type (cf-object class) based events 181our @CB_TYPE = (); # registry for type (cf-object class) based events
137our @CB_MAP = (); 182our @CB_MAP = ();
138 183
184my %attachment;
185
139sub _attach_cb($\%$$$) { 186sub _attach_cb($\%$$$) {
140 my ($registry, $undo, $event, $prio, $cb) = @_; 187 my ($registry, $undo, $event, $prio, $cb) = @_;
141 188
142 use sort 'stable'; 189 use sort 'stable';
143 190
151} 198}
152 199
153# attach handles attaching event callbacks 200# attach handles attaching event callbacks
154# the only thing the caller has to do is pass the correct 201# the only thing the caller has to do is pass the correct
155# registry (== where the callback attaches to). 202# registry (== where the callback attaches to).
156sub _attach(\@$\@) { 203sub _attach(\@$@) {
157 my ($registry, $klass, $arg) = @_; 204 my ($registry, $klass, @arg) = @_;
158 205
159 my $prio = 0; 206 my $prio = 0;
160 207
161 my %undo = ( 208 my %undo = (
162 registry => $registry, 209 registry => $registry,
163 cb => [], 210 cb => [],
164 ); 211 );
165 212
166 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; 213 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
167 214
168 while (@$arg) { 215 while (@arg) {
169 my $type = shift @$arg; 216 my $type = shift @arg;
170 217
171 if ($type eq "prio") { 218 if ($type eq "prio") {
172 $prio = shift @$arg; 219 $prio = shift @arg;
173 220
174 } elsif ($type eq "package") { 221 } elsif ($type eq "package") {
175 my $pkg = shift @$arg; 222 my $pkg = shift @arg;
176 223
177 while (my ($name, $id) = each %cb_id) { 224 while (my ($name, $id) = each %cb_id) {
178 if (my $cb = $pkg->can ($name)) { 225 if (my $cb = $pkg->can ($name)) {
179 _attach_cb $registry, %undo, $id, $prio, $cb; 226 _attach_cb $registry, %undo, $id, $prio, $cb;
180 } 227 }
181 } 228 }
182 229
183 } elsif (exists $cb_id{$type}) { 230 } elsif (exists $cb_id{$type}) {
184 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; 231 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg;
185 232
186 } elsif (ref $type) { 233 } elsif (ref $type) {
187 warn "attaching objects not supported, ignoring.\n"; 234 warn "attaching objects not supported, ignoring.\n";
188 235
189 } else { 236 } else {
190 shift @$arg; 237 shift @arg;
191 warn "attach argument '$type' not supported, ignoring.\n"; 238 warn "attach argument '$type' not supported, ignoring.\n";
192 } 239 }
193 } 240 }
194 241
195 \%undo 242 \%undo
196} 243}
197 244
245sub _attach_attachment {
246 my ($klass, $obj, $name, @args) = q_;
247
248 my $res;
249
250 if (my $attach = $attachment{$name}) {
251 my $registry = $obj->registry;
252
253 $res = _attach @$registry, $klass, @$attach;
254
255 if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) {
256 for (@$cb) {
257 eval { $_->[1]->($obj, @args); };
258 if ($@) {
259 warn "$@";
260 warn "... while processing '$name' instantiate with args <@args>.\n";
261 }
262 }
263 }
264 } else {
265 warn "object uses attachment '$name' that is not available, postponing.\n";
266 }
267
268 push @{$obj->{_attachment}}, $name;
269
270 $res->{attachment} = $name;
271 $res
272}
273
198sub cf::object::attach { 274sub cf::object::attach {
199 die; 275 my ($obj, $name, @args) = @_;
276
277 _attach_attachment KLASS_OBJECT, $obj, $name, @args;
278}
279
280sub cf::player::attach {
281 my ($obj, $name, @args) = @_;
282
283 _attach_attachment KLASS_PLAYER, $obj, $name, @args;
284}
285
286sub cf::map::attach {
287 my ($obj, $name, @args) = @_;
288
289 _attach_attachment KLASS_MAP, $obj, $name, @args;
200} 290}
201 291
202sub attach_global { 292sub attach_global {
203 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 293 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
204} 294}
205 295
206sub attach_to_type { 296sub attach_to_type {
207 my $type = shift; 297 my $type = shift;
298
208 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ 299 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_
209} 300}
210 301
211sub attach_to_objects { 302sub attach_to_objects {
212 _attach @CB_OBJECT, KLASS_OBJECT, @_ 303 _attach @CB_OBJECT, KLASS_OBJECT, @_
218 309
219sub attach_to_maps { 310sub attach_to_maps {
220 _attach @CB_MAP, KLASS_MAP, @_ 311 _attach @CB_MAP, KLASS_MAP, @_
221} 312}
222 313
314sub register_attachment {
315 my $name = shift;
316
317 $attachment{$name} = [@_];
318}
319
223our $override; 320our $override;
321our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
224 322
225sub override() { 323sub override {
226 $override = 1 324 $override = 1;
325 @invoke_results = ();
227} 326}
228 327
229sub invoke { 328sub do_invoke {
230 my $event = shift; 329 my $event = shift;
231 my $callbacks = shift; 330 my $callbacks = shift;
331
332 @invoke_results = ();
232 333
233 local $override; 334 local $override;
234 335
235 for (@$callbacks) { 336 for (@$callbacks) {
236 eval { &{$_->[1]} }; 337 eval { &{$_->[1]} };
246 347
247 0 348 0
248} 349}
249 350
250############################################################################# 351#############################################################################
352# object support
353
354sub instantiate {
355 my ($obj, $data) = @_;
356
357 $data = from_json $data;
358
359 for (@$data) {
360 my ($name, $args) = @$_;
361 attach $obj, $name, @{$args || [] };
362 }
363}
364
365# basically do the same as instantiate, without calling instantiate
366sub reattach {
367 warn "reattach<@_>\n";#d#
368 my ($obj) = @_;
369 my $registry = $obj->registry;
370
371 for my $name (@{ $obj->{_attachment} }) {
372 if (my $attach = $attachment{$name}) {
373 _attach @$registry, KLASS_OBJECT, @$attach;
374 } else {
375 warn "object uses attachment '$name' that is not available, postponing.\n";
376 }
377 }
378
379 warn "reattach<@_, $_>\n";
380}
381
382sub object_freezer_save {
383 my ($filename, $objs) = @_;
384 warn "freeze $filename\n";#d#
385 use Data::Dumper; print Dumper $objs;
386
387 $filename .= ".pst";
388
389 if (@$objs) {
390 open my $fh, ">:raw", "$filename~";
391 chmod $fh, SAVE_MODE;
392 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
393 close $fh;
394 rename "$filename~", $filename;
395 } else {
396 unlink $filename;
397 }
398}
399
400sub object_thawer_load {
401 my ($filename) = @_;
402
403 warn "thaw $filename\n";#d#
404
405 open my $fh, "<:raw:perlio", "$filename.pst"
406 or return;
407
408 eval { local $/; (Storable::thaw <$fh>)->{objs} }
409}
410
411attach_to_objects
412 prio => -1000000,
413 on_clone => sub {
414 my ($src, $dst) = @_;
415
416 @{$dst->registry} = @{$src->registry};
417 warn "registry clone ", join ":", @{$src->registry};#d#
418
419 %$dst = %$src;
420
421 $dst->{_attachment} = [@{ $src->{_attachment} }]
422 if exists $src->{_attachment};
423
424 warn "clone<@_>\n";#d#
425 },
426;
427
428#############################################################################
251# old plug-in events 429# old plug-in events
252 430
253sub inject_event { 431sub inject_event {
254 my $extension = shift; 432 my $extension = shift;
255 my $event_code = shift; 433 my $event_code = shift;
299} 477}
300 478
301sub register { 479sub register {
302 my ($base, $pkg) = @_; 480 my ($base, $pkg) = @_;
303 481
304 for my $idx (0 .. $#PLUGIN_EVENT) { 482 #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} 483}
311 484
312sub load_extension { 485sub load_extension {
313 my ($path) = @_; 486 my ($path) = @_;
314 487
345 my ($pkg) = @_; 518 my ($pkg) = @_;
346 519
347 warn "removing extension $pkg\n"; 520 warn "removing extension $pkg\n";
348 521
349 # remove hooks 522 # remove hooks
523 #TODO
350 for my $idx (0 .. $#PLUGIN_EVENT) { 524# for my $idx (0 .. $#PLUGIN_EVENT) {
351 delete $hook[$idx]{$pkg}; 525# delete $hook[$idx]{$pkg};
352 } 526# }
353 527
354 # remove commands 528 # remove commands
355 for my $name (keys %command) { 529 for my $name (keys %command) {
356 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 530 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
357 531
423 Symbol::delete_package $k; 597 Symbol::delete_package $k;
424 } 598 }
425 599
426 # 4. get rid of safe::, as good as possible 600 # 4. get rid of safe::, as good as possible
427 Symbol::delete_package "safe::$_" 601 Symbol::delete_package "safe::$_"
428 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); 602 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
429 603
430 # 5. remove register_script_function callbacks 604 # 5. remove register_script_function callbacks
431 # TODO 605 # TODO
432 606
433 # 6. unload cf.pm "a bit" 607 # 6. unload cf.pm "a bit"
461 warn $_[0]; 635 warn $_[0];
462 $who->message ($_[0]); 636 $who->message ($_[0]);
463 }; 637 };
464 } 638 }
465}; 639};
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 640
482############################################################################# 641#############################################################################
483# extcmd framework, basically convert ext <msg> 642# extcmd framework, basically convert ext <msg>
484# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 643# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
485 644
511 670
512 my $path = $map->tmpname; 671 my $path = $map->tmpname;
513 defined $path or return; 672 defined $path or return;
514 673
515 unlink "$path.cfperl"; 674 unlink "$path.cfperl";
675 unlink "$path.pst";
516}; 676};
517 677
518*cf::mapsupport::on_swapin = 678*cf::mapsupport::on_swapin =
519*cf::mapsupport::on_load = sub { 679*cf::mapsupport::on_load = sub {
520 my ($map) = @_; 680 my ($map) = @_;
531 or return; # too new 691 or return; # too new
532 692
533 $map->_set_obs ($data->{obs}); 693 $map->_set_obs ($data->{obs});
534}; 694};
535 695
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};
562
563attach_to_maps prio => -10000, package => cf::mapsupport::; 696attach_to_maps prio => -10000, package => cf::mapsupport::;
564 697
565############################################################################# 698#############################################################################
566# load/save perl data associated with player->ob objects 699# load/save perl data associated with player->ob objects
567 700
578 $o->set_ob_key_value ("_perl_data"); 711 $o->set_ob_key_value ("_perl_data");
579 712
580 %$o = %{ Storable::thaw pack "H*", $value }; 713 %$o = %{ Storable::thaw pack "H*", $value };
581 } 714 }
582 } 715 }
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 }, 716 },
590; 717;
591 718
592############################################################################# 719#############################################################################
593# core extensions - in perl 720# core extensions - in perl

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines