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.43 by root, Fri Aug 25 17:11:53 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#############################################################################
91# utility functions
92
93use JSON::Syck (); # TODO# replace by JSON::PC once working
94
95sub from_json($) {
96 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
97 JSON::Syck::Load $_[0]
98}
99
100sub to_json($) {
101 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
102 JSON::Syck::Dump $_[0]
103}
104
105#############################################################################
97# "new" plug-in system 106# "new" plug-in system
98 107
99=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.
100 119
101=item cf::attach_global ... 120=item cf::attach_global ...
102 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
103=item cf::attach_to_type ... 155=item cf::attach_to_type $object_type, ...
156
157Attach handlers for a specific object type (e.g. TRANSPORT).
104 158
105=item cf::attach_to_objects ... 159=item cf::attach_to_objects ...
106 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
107=item cf::attach_to_players ... 165=item cf::attach_to_players ...
108 166
167Attach handlers to all players.
168
109=item cf::attach_to_maps ... 169=item cf::attach_to_maps ...
110 170
111 prio => $number, # lower is earlier 171Attach handlers to all maps.
112 on_xxx => \&cb, 172
113 package => package::, 173=item cf:register_attachment $name, ...
114 174
115=cut 175=cut
116 176
117# 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
118our @CB_GLOBAL = (); # registry for all global events 178our @CB_GLOBAL = (); # registry for all global events
119our @CB_OBJECT = (); 179our @CB_OBJECT = (); # all objects (should not be used except in emergency)
120our @CB_PLAYER = (); 180our @CB_PLAYER = ();
121our @CB_TYPE = (); # registry for type (cf-object class) based events 181our @CB_TYPE = (); # registry for type (cf-object class) based events
122our @CB_MAP = (); 182our @CB_MAP = ();
123 183
184my %attachment;
185
124sub _attach_cb($\%$$$) { 186sub _attach_cb($\%$$$) {
125 my ($registry, $undo, $event, $prio, $cb) = @_; 187 my ($registry, $undo, $event, $prio, $cb) = @_;
126 188
127 use sort 'stable'; 189 use sort 'stable';
128 190
136} 198}
137 199
138# attach handles attaching event callbacks 200# attach handles attaching event callbacks
139# 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
140# registry (== where the callback attaches to). 202# registry (== where the callback attaches to).
141sub _attach(\@$\@) { 203sub _attach(\@$@) {
142 my ($registry, $klass, $arg) = @_; 204 my ($registry, $klass, @arg) = @_;
143 205
144 my $prio = 0; 206 my $prio = 0;
145 207
146 my %undo = ( 208 my %undo = (
147 registry => $registry, 209 registry => $registry,
148 cb => [], 210 cb => [],
149 ); 211 );
150 212
151 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;
152 214
153 while (@$arg) { 215 while (@arg) {
154 my $type = shift @$arg; 216 my $type = shift @arg;
155 217
156 if ($type eq "prio") { 218 if ($type eq "prio") {
157 $prio = shift @$arg; 219 $prio = shift @arg;
158 220
159 } elsif ($type eq "package") { 221 } elsif ($type eq "package") {
160 my $pkg = shift @$arg; 222 my $pkg = shift @arg;
161 223
162 while (my ($name, $id) = each %cb_id) { 224 while (my ($name, $id) = each %cb_id) {
163 if (my $cb = $pkg->can ($name)) { 225 if (my $cb = $pkg->can ($name)) {
164 _attach_cb $registry, %undo, $id, $prio, $cb; 226 _attach_cb $registry, %undo, $id, $prio, $cb;
165 } 227 }
166 } 228 }
167 229
168 } elsif (exists $cb_id{$type}) { 230 } elsif (exists $cb_id{$type}) {
169 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; 231 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg;
170 232
171 } elsif (ref $type) { 233 } elsif (ref $type) {
172 warn "attaching objects not supported, ignoring.\n"; 234 warn "attaching objects not supported, ignoring.\n";
173 235
174 } else { 236 } else {
175 shift @$arg; 237 shift @arg;
176 warn "attach argument '$type' not supported, ignoring.\n"; 238 warn "attach argument '$type' not supported, ignoring.\n";
177 } 239 }
178 } 240 }
179 241
180 \%undo 242 \%undo
181} 243}
182 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
183sub cf::object::attach { 274sub cf::object::attach {
184 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;
185} 290}
186 291
187sub attach_global { 292sub attach_global {
188 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 293 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
189} 294}
190 295
191sub attach_to_type { 296sub attach_to_type {
192 my $type = shift; 297 my $type = shift;
298
193 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ 299 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_
194} 300}
195 301
196sub attach_to_objects { 302sub attach_to_objects {
197 _attach @CB_OBJECT, KLASS_OBJECT, @_ 303 _attach @CB_OBJECT, KLASS_OBJECT, @_
203 309
204sub attach_to_maps { 310sub attach_to_maps {
205 _attach @CB_MAP, KLASS_MAP, @_ 311 _attach @CB_MAP, KLASS_MAP, @_
206} 312}
207 313
314sub register_attachment {
315 my $name = shift;
316
317 $attachment{$name} = [@_];
318}
319
208our $override; 320our $override;
321our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
209 322
210sub override() { 323sub override {
211 $override = 1 324 $override = 1;
325 @invoke_results = ();
212} 326}
213 327
214sub invoke { 328sub do_invoke {
215 my $event = shift; 329 my $event = shift;
216 my $callbacks = shift; 330 my $callbacks = shift;
331
332 @invoke_results = ();
217 333
218 local $override; 334 local $override;
219 335
220 for (@$callbacks) { 336 for (@$callbacks) {
221 eval { &{$_->[1]} }; 337 eval { &{$_->[1]} };
231 347
232 0 348 0
233} 349}
234 350
235############################################################################# 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#############################################################################
236# old plug-in events 429# old plug-in events
237 430
238sub inject_event { 431sub inject_event {
239 my $extension = shift; 432 my $extension = shift;
240 my $event_code = shift; 433 my $event_code = shift;
284} 477}
285 478
286sub register { 479sub register {
287 my ($base, $pkg) = @_; 480 my ($base, $pkg) = @_;
288 481
289 for my $idx (0 .. $#PLUGIN_EVENT) { 482 #TODO
290 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
291 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
292 $hook[$idx]{$base} = $ref;
293 }
294 }
295} 483}
296 484
297sub load_extension { 485sub load_extension {
298 my ($path) = @_; 486 my ($path) = @_;
299 487
330 my ($pkg) = @_; 518 my ($pkg) = @_;
331 519
332 warn "removing extension $pkg\n"; 520 warn "removing extension $pkg\n";
333 521
334 # remove hooks 522 # remove hooks
523 #TODO
335 for my $idx (0 .. $#PLUGIN_EVENT) { 524# for my $idx (0 .. $#PLUGIN_EVENT) {
336 delete $hook[$idx]{$pkg}; 525# delete $hook[$idx]{$pkg};
337 } 526# }
338 527
339 # remove commands 528 # remove commands
340 for my $name (keys %command) { 529 for my $name (keys %command) {
341 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 530 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
342 531
408 Symbol::delete_package $k; 597 Symbol::delete_package $k;
409 } 598 }
410 599
411 # 4. get rid of safe::, as good as possible 600 # 4. get rid of safe::, as good as possible
412 Symbol::delete_package "safe::$_" 601 Symbol::delete_package "safe::$_"
413 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);
414 603
415 # 5. remove register_script_function callbacks 604 # 5. remove register_script_function callbacks
416 # TODO 605 # TODO
417 606
418 # 6. unload cf.pm "a bit" 607 # 6. unload cf.pm "a bit"
448 }; 637 };
449 } 638 }
450}; 639};
451 640
452############################################################################# 641#############################################################################
453# utility functions
454
455use JSON::Syck (); # TODO# replace by JSON::PC once working
456
457sub from_json($) {
458 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
459 JSON::Syck::Load $_[0]
460}
461
462sub to_json($) {
463 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
464 JSON::Syck::Dump $_[0]
465}
466
467#############################################################################
468# extcmd framework, basically convert ext <msg> 642# extcmd framework, basically convert ext <msg>
469# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 643# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
470 644
471attach_global 645attach_to_players
472 on_extcmd => sub { 646 on_extcmd => sub {
473 my ($pl, $buf) = @_; 647 my ($pl, $buf) = @_;
474 648
475 my $msg = eval { from_json $buf }; 649 my $msg = eval { from_json $buf };
476 650
496 670
497 my $path = $map->tmpname; 671 my $path = $map->tmpname;
498 defined $path or return; 672 defined $path or return;
499 673
500 unlink "$path.cfperl"; 674 unlink "$path.cfperl";
675 unlink "$path.pst";
501}; 676};
502 677
503*cf::mapsupport::on_swapin = 678*cf::mapsupport::on_swapin =
504*cf::mapsupport::on_load = sub { 679*cf::mapsupport::on_load = sub {
505 my ($map) = @_; 680 my ($map) = @_;
516 or return; # too new 691 or return; # too new
517 692
518 $map->_set_obs ($data->{obs}); 693 $map->_set_obs ($data->{obs});
519}; 694};
520 695
521*cf::mapsupport::on_swapout = sub {
522 my ($map) = @_;
523
524 my $path = $map->tmpname;
525 $path = $map->path unless defined $path;
526
527 my $obs = $map->_get_obs;
528
529 if (defined $obs) {
530 open my $fh, ">:raw", "$path.cfperl"
531 or die "$path.cfperl: $!";
532
533 stat $path;
534
535 print $fh Storable::nfreeze {
536 size => (stat _)[7],
537 time => (stat _)[9],
538 version => 1,
539 obs => $obs,
540 };
541
542 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
543 } else {
544 unlink "$path.cfperl";
545 }
546};
547
548attach_to_maps prio => -10000, package => cf::mapsupport::; 696attach_to_maps prio => -10000, package => cf::mapsupport::;
549 697
550############################################################################# 698#############################################################################
551# load/save perl data associated with player->ob objects 699# load/save perl data associated with player->ob objects
552 700
563 $o->set_ob_key_value ("_perl_data"); 711 $o->set_ob_key_value ("_perl_data");
564 712
565 %$o = %{ Storable::thaw pack "H*", $value }; 713 %$o = %{ Storable::thaw pack "H*", $value };
566 } 714 }
567 } 715 }
568 },
569 on_save => sub {
570 my ($pl, $path) = @_;
571
572 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
573 for grep %$_, all_objects $pl->ob;
574 }, 716 },
575; 717;
576 718
577############################################################################# 719#############################################################################
578# core extensions - in perl 720# core extensions - in perl

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines