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.39 by root, Fri Aug 25 13:24:50 2006 UTC vs.
Revision 1.45 by root, Sat Aug 26 23:36:32 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@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 71@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
77@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
78 72
79# we bless all objects into 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 @{"ext::$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 {
87 warn "error in event callback: @_"; 81 warn "error in event callback: @_";
88}; 82};
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 cf::object::attach ... # NYI
100 109
101=item cf::attach_global ... # NYI 110=item cf::attach_global ...
102 111
103=item cf::attach_to_type ... # NYI 112=item cf::attach_to_type $object_type, ...
104 113
105=item cf::attach_to_objects ... # NYI 114=item cf::attach_to_objects ...
106 115
107=item cf::attach_to_players ... # NYI 116=item cf::attach_to_players ...
108 117
109=item cf::attach_to_maps ... # NYI 118=item cf::attach_to_maps ...
110 119
120=item cf:register_attachment $name, ...
121
111 prio => $number, # higehr is earlier 122 prio => $number, # lower is earlier
112 on_xxx => \&cb, 123 on_xxx => \&cb,
113 package => package::, 124 package => package::,
114 125
115=cut 126=cut
116 127
117our %CB_CLASS = (); # registry for class-based events 128# the following variables are defined in .xs and must not be re-created
118our @CB_GLOBAL = (); # registry for all global events 129our @CB_GLOBAL = (); # registry for all global events
130our @CB_OBJECT = (); # all objects (should not be used except in emergency)
131our @CB_PLAYER = ();
119our @CB_TYPE = (); # registry for type (cf-object class) based events 132our @CB_TYPE = (); # registry for type (cf-object class) based events
133our @CB_MAP = ();
134
135my %attachment;
120 136
121sub _attach_cb($\%$$$) { 137sub _attach_cb($\%$$$) {
122 my ($registry, $undo, $event, $prio, $cb) = @_; 138 my ($registry, $undo, $event, $prio, $cb) = @_;
123 139
124 use sort 'stable'; 140 use sort 'stable';
133} 149}
134 150
135# attach handles attaching event callbacks 151# attach handles attaching event callbacks
136# the only thing the caller has to do is pass the correct 152# the only thing the caller has to do is pass the correct
137# registry (== where the callback attaches to). 153# registry (== where the callback attaches to).
138sub _attach(\@$\@) { 154sub _attach(\@$@) {
139 my ($registry, $klass, $arg) = @_; 155 my ($registry, $klass, @arg) = @_;
140 156
141 my $prio = 0; 157 my $prio = 0;
142 158
143 my %undo = ( 159 my %undo = (
144 registry => $registry, 160 registry => $registry,
145 cb => [], 161 cb => [],
146 ); 162 );
147 163
148 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; 164 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
149 165
150 while (@$arg) { 166 while (@arg) {
151 my $type = shift @$arg; 167 my $type = shift @arg;
152 168
153 if ($type eq "prio") { 169 if ($type eq "prio") {
154 $prio = shift @$arg; 170 $prio = shift @arg;
155 171
156 } elsif ($type eq "package") { 172 } elsif ($type eq "package") {
157 my $pkg = shift @$arg; 173 my $pkg = shift @arg;
158 174
159 while (my ($name, $id) = each %cb_id) { 175 while (my ($name, $id) = each %cb_id) {
160 if (my $cb = $pkg->can ($name)) { 176 if (my $cb = $pkg->can ($name)) {
161 _attach_cb $registry, %undo, $id, $prio, $cb; 177 _attach_cb $registry, %undo, $id, $prio, $cb;
162 } 178 }
163 } 179 }
164 180
165 } elsif (exists $cb_id{$type}) { 181 } elsif (exists $cb_id{$type}) {
166 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg; 182 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg;
167 183
168 } elsif (ref $type) { 184 } elsif (ref $type) {
169 warn "attaching objects not supported, ignoring.\n"; 185 warn "attaching objects not supported, ignoring.\n";
170 186
171 } else { 187 } else {
172 shift @$arg; 188 shift @arg;
173 warn "attach argument '$type' not supported, ignoring.\n"; 189 warn "attach argument '$type' not supported, ignoring.\n";
174 } 190 }
175 } 191 }
176 192
177 \%undo 193 \%undo
183 199
184sub attach_global { 200sub attach_global {
185 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 201 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
186} 202}
187 203
188sub attach_type { 204sub attach_to_type {
189 my $type = shift; 205 my $type = shift;
206
190 _attach @{$CB_TYPE[$type]}, KLASS_MAP, @_ 207 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_
191} 208}
192 209
193sub attach_to_objects { 210sub attach_to_objects {
194 _attach @{$CB_CLASS{cf::object::wrap::}}, KLASS_OBJECT, @_ 211 _attach @CB_OBJECT, KLASS_OBJECT, @_
195} 212}
196 213
197sub attach_to_players { 214sub attach_to_players {
198 _attach @{$CB_CLASS{cf::player::wrap::}}, KLASS_PLAYER, @_ 215 _attach @CB_PLAYER, KLASS_PLAYER, @_
199} 216}
200 217
201sub attach_to_maps { 218sub attach_to_maps {
202 _attach @{$CB_CLASS{cf::map::wrap::}}, KLASS_MAP, @_ 219 _attach @CB_MAP, KLASS_MAP, @_
220}
221
222sub register_attachment {
223 my $name = shift;
224
225 $attachment{$name} = [@_];
203} 226}
204 227
205our $override; 228our $override;
229our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
206 230
207sub override() { 231sub override {
208 $override = 1 232 $override = 1;
233 @invoke_results = ();
209} 234}
210 235
211sub invoke { 236sub do_invoke {
212 my $event = shift; 237 my $event = shift;
238 my $callbacks = shift;
213 239
214 my @cb; 240 @invoke_results = ();
215
216 if (my $ref = ref $_[0]) {
217 # 1. object-specific (NYI)
218 # 2. class-specific
219 push @cb, @{$CB_CLASS{$ref}[$event] || []};
220 }
221
222 # global
223 push @cb, @{$CB_GLOBAL[$event] || []};
224
225# warn "invoke id $EVENT[$event][0], args <@_> <=> @cb\n";#d#
226 241
227 local $override; 242 local $override;
228 243
229 for (@cb) { 244 for (@$callbacks) {
230 eval { &{$_->[1]} }; 245 eval { &{$_->[1]} };
231 246
232 if ($@) { 247 if ($@) {
233 warn "$@"; 248 warn "$@";
234 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; 249 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
240 255
241 0 256 0
242} 257}
243 258
244############################################################################# 259#############################################################################
260# object support
261
262sub instantiate {
263 my ($obj, $data) = @_;
264 my $registry = $obj->registry;
265
266 $data = from_json $data;
267
268 for (@$data) {
269 my ($pri, $name, @args) = @$_;
270
271 if (my $attach = $attachment{$name}) {
272 _attach @$registry, KLASS_OBJECT, @$attach;
273
274 if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) {
275 for (@$cb) {
276 eval { $_->[1]->($obj, @args); };
277 if ($@) {
278 warn "$@";
279 warn "... while processing '$name' instantiate with args <@args>\n";
280 }
281 }
282 }
283 } else {
284 warn "object uses attachment $name that is not available, postponing.\n";
285 }
286
287 push @{$obj->{_attachment}}, $name;
288 }
289}
290
291# basically do the same as instantiate, without calling instantiate
292sub reattach {
293 my ($obj) = @_;
294 my $registry = $obj->registry;
295
296 warn "reattach<@_, $_>\n";
297}
298
299attach_to_objects
300 prio => -1000000,
301 on_clone => sub {
302 my ($src, $dst) = @_;
303
304 @{$dst->registry} = @{$src->registry};
305 warn "registry clone ", join ":", @{$src->registry};#d#
306
307 %$dst = %$src;
308
309 $dst->{_attachment} = [@{ $src->{_attachment} }]
310 if exists $src->{_attachment};
311
312 warn "clone<@_>\n";#d#
313 },
314;
315
316#############################################################################
245# old plug-in events 317# old plug-in events
246 318
247sub inject_event { 319sub inject_event {
248 my $extension = shift; 320 my $extension = shift;
249 my $event_code = shift; 321 my $event_code = shift;
293} 365}
294 366
295sub register { 367sub register {
296 my ($base, $pkg) = @_; 368 my ($base, $pkg) = @_;
297 369
298 for my $idx (0 .. $#PLUGIN_EVENT) { 370 #TODO
299 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
300 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
301 $hook[$idx]{$base} = $ref;
302 }
303 }
304} 371}
305 372
306sub load_extension { 373sub load_extension {
307 my ($path) = @_; 374 my ($path) = @_;
308 375
309 $path =~ /([^\/\\]+)\.ext$/ or die "$path"; 376 $path =~ /([^\/\\]+)\.ext$/ or die "$path";
310 my $base = $1; 377 my $base = $1;
311 my $pkg = $1; 378 my $pkg = $1;
312 $pkg =~ s/[^[:word:]]/_/g; 379 $pkg =~ s/[^[:word:]]/_/g;
313 $pkg = "cf::ext::$pkg"; 380 $pkg = "ext::$pkg";
314 381
315 warn "loading '$path' into '$pkg'\n"; 382 warn "loading '$path' into '$pkg'\n";
316 383
317 open my $fh, "<:utf8", $path 384 open my $fh, "<:utf8", $path
318 or die "$path: $!"; 385 or die "$path: $!";
339 my ($pkg) = @_; 406 my ($pkg) = @_;
340 407
341 warn "removing extension $pkg\n"; 408 warn "removing extension $pkg\n";
342 409
343 # remove hooks 410 # remove hooks
411 #TODO
344 for my $idx (0 .. $#PLUGIN_EVENT) { 412# for my $idx (0 .. $#PLUGIN_EVENT) {
345 delete $hook[$idx]{$pkg}; 413# delete $hook[$idx]{$pkg};
346 } 414# }
347 415
348 # remove commands 416 # remove commands
349 for my $name (keys %command) { 417 for my $name (keys %command) {
350 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; 418 my @cb = grep $_->[2] ne $pkg, @{ $command{$name} };
351 419
361 # remove extcmds 429 # remove extcmds
362 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 430 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
363 delete $extcmd{$name}; 431 delete $extcmd{$name};
364 } 432 }
365 433
366 if (my $cb = $pkg->can ("on_unload")) { 434 if (my $cb = $pkg->can ("unload")) {
367 eval { 435 eval {
368 $cb->($pkg); 436 $cb->($pkg);
369 1 437 1
370 } or warn "$pkg unloaded, but with errors: $@"; 438 } or warn "$pkg unloaded, but with errors: $@";
371 } 439 }
415 } 483 }
416 484
417 Symbol::delete_package $k; 485 Symbol::delete_package $k;
418 } 486 }
419 487
420 # 4. get rid of ext::, as good as possible 488 # 4. get rid of safe::, as good as possible
421 Symbol::delete_package "ext::$_" 489 Symbol::delete_package "safe::$_"
422 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region); 490 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
423 491
424 # 5. remove register_script_function callbacks 492 # 5. remove register_script_function callbacks
425 # TODO 493 # TODO
426 494
427 # 6. unload cf.pm "a bit" 495 # 6. unload cf.pm "a bit"
428 delete $INC{"cf.pm"}; 496 delete $INC{"cf.pm"};
429 497
430 # don't, removes xs symbols, too 498 # don't, removes xs symbols, too,
499 # and global variables created in xs
431 #Symbol::delete_package __PACKAGE__; 500 #Symbol::delete_package __PACKAGE__;
432 501
433 # 7. reload cf.pm 502 # 7. reload cf.pm
434 $msg->("reloading cf.pm"); 503 $msg->("reloading cf.pm");
435 require cf; 504 require cf;
456 }; 525 };
457 } 526 }
458}; 527};
459 528
460############################################################################# 529#############################################################################
461# utility functions
462
463use JSON::Syck (); # TODO# replace by JSON::PC once working
464
465sub from_json($) {
466 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
467 JSON::Syck::Load $_[0]
468}
469
470sub to_json($) {
471 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
472 JSON::Syck::Dump $_[0]
473}
474
475#############################################################################
476# extcmd framework, basically convert ext <msg> 530# extcmd framework, basically convert ext <msg>
477# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 531# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
478 532
479sub on_extcmd { 533attach_to_players
534 on_extcmd => sub {
480 my ($pl, $buf) = @_; 535 my ($pl, $buf) = @_;
481 536
482 my $msg = eval { from_json $buf }; 537 my $msg = eval { from_json $buf };
483 538
484 if (ref $msg) { 539 if (ref $msg) {
485 if (my $cb = $extcmd{$msg->{msgtype}}) { 540 if (my $cb = $extcmd{$msg->{msgtype}}) {
486 if (my %reply = $cb->[0]->($pl, $msg)) { 541 if (my %reply = $cb->[0]->($pl, $msg)) {
487 $pl->ext_reply ($msg->{msgid}, %reply); 542 $pl->ext_reply ($msg->{msgid}, %reply);
543 }
488 } 544 }
545 } else {
546 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
489 } 547 }
490 } else { 548
491 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 549 cf::override;
492 } 550 },
493 551;
494 1
495}
496 552
497############################################################################# 553#############################################################################
498# load/save/clean perl data associated with a map 554# load/save/clean perl data associated with a map
499 555
500*cf::mapsupport::on_clean = sub { 556*cf::mapsupport::on_clean = sub {
632} 688}
633 689
634############################################################################# 690#############################################################################
635# map scripting support 691# map scripting support
636 692
637our $safe = new Safe "ext"; 693our $safe = new Safe "safe";
638our $safe_hole = new Safe::Hole; 694our $safe_hole = new Safe::Hole;
639 695
640$SIG{FPE} = 'IGNORE'; 696$SIG{FPE} = 'IGNORE';
641 697
642$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 698$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time));
648 ["cf::object::player" => qw(player)], 704 ["cf::object::player" => qw(player)],
649 ["cf::player" => qw(peaceful)], 705 ["cf::player" => qw(peaceful)],
650) { 706) {
651 no strict 'refs'; 707 no strict 'refs';
652 my ($pkg, @funs) = @$_; 708 my ($pkg, @funs) = @$_;
653 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 709 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
654 for @funs; 710 for @funs;
655} 711}
656 712
657sub safe_eval($;@) { 713sub safe_eval($;@) {
658 my ($code, %vars) = @_; 714 my ($code, %vars) = @_;
660 my $qcode = $code; 716 my $qcode = $code;
661 $qcode =~ s/"/‟/g; # not allowed in #line filenames 717 $qcode =~ s/"/‟/g; # not allowed in #line filenames
662 $qcode =~ s/\n/\\n/g; 718 $qcode =~ s/\n/\\n/g;
663 719
664 local $_; 720 local $_;
665 local @ext::cf::_safe_eval_args = values %vars; 721 local @safe::cf::_safe_eval_args = values %vars;
666 722
667 $code = 723 my $eval =
668 "do {\n" 724 "do {\n"
669 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 725 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
670 . "#line 0 \"{$qcode}\"\n" 726 . "#line 0 \"{$qcode}\"\n"
671 . $code 727 . $code
672 . "\n}" 728 . "\n}"
673 ; 729 ;
674 730
675 sub_generation_inc; 731 sub_generation_inc;
676 my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); 732 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
677 sub_generation_inc; 733 sub_generation_inc;
734
735 if ($@) {
736 warn "$@";
737 warn "while executing safe code '$code'\n";
738 warn "with arguments " . (join " ", %vars) . "\n";
739 }
678 740
679 wantarray ? @res : $res[0] 741 wantarray ? @res : $res[0]
680} 742}
681 743
682sub register_script_function { 744sub register_script_function {
683 my ($fun, $cb) = @_; 745 my ($fun, $cb) = @_;
684 746
685 no strict 'refs'; 747 no strict 'refs';
686 *{"ext::$fun"} = $safe_hole->wrap ($cb); 748 *{"safe::$fun"} = $safe_hole->wrap ($cb);
687} 749}
688 750
689############################################################################# 751#############################################################################
690# the server's main() 752# the server's main()
691 753

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines