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.35 by root, Thu Aug 24 13:35:48 2006 UTC vs.
Revision 1.41 by root, Fri Aug 25 15:25:12 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
16our %COMMAND; 16our %COMMAND = ();
17our @EVENT; 17our @EVENT;
18our @PLUGIN_EVENT;
18our %PROP_TYPE; 19our %PROP_TYPE;
19our %PROP_IDX; 20our %PROP_IDX;
20our $LIBDIR = maps_directory "perl"; 21our $LIBDIR = maps_directory "perl";
21 22
22our $TICK = MAX_TIME * 1e-6; 23our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER; 24our $TICK_WATCHER;
24our $NEXT_TICK; 25our $NEXT_TICK;
25 26
26BEGIN { 27BEGIN {
27 @EVENT = map lc, @EVENT; 28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
28 29
29 *CORE::GLOBAL::warn = sub { 30 *CORE::GLOBAL::warn = sub {
30 my $msg = join "", @_; 31 my $msg = join "", @_;
31 $msg .= "\n" 32 $msg .= "\n"
32 unless $msg =~ /\n$/; 33 unless $msg =~ /\n$/;
70prop_gen MAP_PROP => "cf::map"; 71prop_gen MAP_PROP => "cf::map";
71prop_gen ARCH_PROP => "cf::arch"; 72prop_gen ARCH_PROP => "cf::arch";
72 73
73# guessed hierarchies 74# guessed hierarchies
74 75
75@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 76@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
76@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; 77@safe::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
77 78
78# we bless all objects into derived classes to force a method lookup 79# we bless all objects into (empty) derived classes to force a method lookup
79# within the Safe compartment. 80# within the Safe compartment.
80for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { 81for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) {
81 no strict 'refs'; 82 no strict 'refs';
82 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 83 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
83} 84}
84 85
85$Event::DIED = sub { 86$Event::DIED = sub {
86 warn "error in event callback: @_"; 87 warn "error in event callback: @_";
87}; 88};
90my @exts; 91my @exts;
91my @hook; 92my @hook;
92my %command; 93my %command;
93my %extcmd; 94my %extcmd;
94 95
96#############################################################################
97# "new" plug-in system
98
99=item cf::object::attach ... # NYI
100
101=item cf::attach_global ...
102
103=item cf::attach_to_type ...
104
105=item cf::attach_to_objects ...
106
107=item cf::attach_to_players ...
108
109=item cf::attach_to_maps ...
110
111 prio => $number, # lower is earlier
112 on_xxx => \&cb,
113 package => package::,
114
115=cut
116
117# the following variables are defined in .xs and must not be re-created
118our @CB_GLOBAL = (); # registry for all global events
119our @CB_OBJECT = ();
120our @CB_PLAYER = ();
121our @CB_TYPE = (); # registry for type (cf-object class) based events
122our @CB_MAP = ();
123
124sub _attach_cb($\%$$$) {
125 my ($registry, $undo, $event, $prio, $cb) = @_;
126
127 use sort 'stable';
128
129 $cb = [$prio, $cb];
130
131 @{$registry->[$event]} = sort
132 { $a->[0] cmp $b->[0] }
133 @{$registry->[$event] || []}, $cb;
134
135 push @{$undo->{cb}}, [$event, $cb];
136}
137
138# attach handles attaching event callbacks
139# the only thing the caller has to do is pass the correct
140# registry (== where the callback attaches to).
141sub _attach(\@$\@) {
142 my ($registry, $klass, $arg) = @_;
143
144 my $prio = 0;
145
146 my %undo = (
147 registry => $registry,
148 cb => [],
149 );
150
151 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
152
153 while (@$arg) {
154 my $type = shift @$arg;
155
156 if ($type eq "prio") {
157 $prio = shift @$arg;
158
159 } elsif ($type eq "package") {
160 my $pkg = shift @$arg;
161
162 while (my ($name, $id) = each %cb_id) {
163 if (my $cb = $pkg->can ($name)) {
164 _attach_cb $registry, %undo, $id, $prio, $cb;
165 }
166 }
167
168 } elsif (exists $cb_id{$type}) {
169 _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @$arg;
170
171 } elsif (ref $type) {
172 warn "attaching objects not supported, ignoring.\n";
173
174 } else {
175 shift @$arg;
176 warn "attach argument '$type' not supported, ignoring.\n";
177 }
178 }
179
180 \%undo
181}
182
183sub cf::object::attach {
184 die;
185}
186
187sub attach_global {
188 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
189}
190
191sub attach_to_type {
192 my $type = shift;
193 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_
194}
195
196sub attach_to_objects {
197 _attach @CB_OBJECT, KLASS_OBJECT, @_
198}
199
200sub attach_to_players {
201 _attach @CB_PLAYER, KLASS_PLAYER, @_
202}
203
204sub attach_to_maps {
205 _attach @CB_MAP, KLASS_MAP, @_
206}
207
208our $override;
209
210sub override() {
211 $override = 1
212}
213
214sub invoke {
215 my $event = shift;
216 my $callbacks = shift;
217
218 local $override;
219
220 for (@$callbacks) {
221 eval { &{$_->[1]} };
222
223 if ($@) {
224 warn "$@";
225 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n";
226 override;
227 }
228
229 return 1 if $override;
230 }
231
232 0
233}
234
235#############################################################################
236# old plug-in events
237
95sub inject_event { 238sub inject_event {
96 my $extension = shift; 239 my $extension = shift;
97 my $event_code = shift; 240 my $event_code = shift;
98 241
99 my $cb = $hook[$event_code]{$extension} 242 my $cb = $hook[$event_code]{$extension}
141} 284}
142 285
143sub register { 286sub register {
144 my ($base, $pkg) = @_; 287 my ($base, $pkg) = @_;
145 288
146 for my $idx (0 .. $#EVENT) { 289 for my $idx (0 .. $#PLUGIN_EVENT) {
147 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 290 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
148 #warn "registering $EVENT[$idx] hook to '$pkg'\n"; 291 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
149 $hook[$idx]{$base} = $ref; 292 $hook[$idx]{$base} = $ref;
150 } 293 }
151 } 294 }
152} 295}
153 296
156 299
157 $path =~ /([^\/\\]+)\.ext$/ or die "$path"; 300 $path =~ /([^\/\\]+)\.ext$/ or die "$path";
158 my $base = $1; 301 my $base = $1;
159 my $pkg = $1; 302 my $pkg = $1;
160 $pkg =~ s/[^[:word:]]/_/g; 303 $pkg =~ s/[^[:word:]]/_/g;
161 $pkg = "cf::ext::$pkg"; 304 $pkg = "ext::$pkg";
162 305
163 warn "loading '$path' into '$pkg'\n"; 306 warn "loading '$path' into '$pkg'\n";
164 307
165 open my $fh, "<:utf8", $path 308 open my $fh, "<:utf8", $path
166 or die "$path: $!"; 309 or die "$path: $!";
187 my ($pkg) = @_; 330 my ($pkg) = @_;
188 331
189 warn "removing extension $pkg\n"; 332 warn "removing extension $pkg\n";
190 333
191 # remove hooks 334 # remove hooks
192 for my $idx (0 .. $#EVENT) { 335 for my $idx (0 .. $#PLUGIN_EVENT) {
193 delete $hook[$idx]{$pkg}; 336 delete $hook[$idx]{$pkg};
194 } 337 }
195 338
196 # remove commands 339 # remove commands
197 for my $name (keys %command) { 340 for my $name (keys %command) {
231 1 374 1
232 } or warn "$ext not loaded: $@"; 375 } or warn "$ext not loaded: $@";
233 } 376 }
234} 377}
235 378
379sub _perl_reload(&) {
380 my ($msg) = @_;
381
382 $msg->("reloading...");
383
384 eval {
385 # 1. cancel all watchers
386 $_->cancel for Event::all_watchers;
387
388 # 2. unload all extensions
389 for (@exts) {
390 $msg->("unloading <$_>");
391 unload_extension $_;
392 }
393
394 # 3. unload all modules loaded from $LIBDIR
395 while (my ($k, $v) = each %INC) {
396 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
397
398 $msg->("removing <$k>");
399 delete $INC{$k};
400
401 $k =~ s/\.pm$//;
402 $k =~ s/\//::/g;
403
404 if (my $cb = $k->can ("unload_module")) {
405 $cb->();
406 }
407
408 Symbol::delete_package $k;
409 }
410
411 # 4. get rid of safe::, as good as possible
412 Symbol::delete_package "safe::$_"
413 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
414
415 # 5. remove register_script_function callbacks
416 # TODO
417
418 # 6. unload cf.pm "a bit"
419 delete $INC{"cf.pm"};
420
421 # don't, removes xs symbols, too,
422 # and global variables created in xs
423 #Symbol::delete_package __PACKAGE__;
424
425 # 7. reload cf.pm
426 $msg->("reloading cf.pm");
427 require cf;
428 };
429 $msg->($@) if $@;
430
431 $msg->("reloaded");
432};
433
434sub perl_reload() {
435 _perl_reload {
436 warn $_[0];
437 print "$_[0]\n";
438 };
439}
440
236register_command "perl-reload", 0, sub { 441register_command "perl-reload", 0, sub {
237 my ($who, $arg) = @_; 442 my ($who, $arg) = @_;
238 443
239 if ($who->flag (FLAG_WIZ)) { 444 if ($who->flag (FLAG_WIZ)) {
240 $who->message ("reloading..."); 445 _perl_reload {
241 446 warn $_[0];
242 warn "reloading...\n"; 447 $who->message ($_[0]);
243 eval {
244 # 1. cancel all watchers
245 $_->cancel for Event::all_watchers;
246
247 # 2. unload all extensions
248 for (@exts) {
249 $who->message ("unloading <$_>");
250 unload_extension $_;
251 }
252
253 # 3. unload all modules loaded from $LIBDIR
254 while (my ($k, $v) = each %INC) {
255 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
256
257 $who->message ("removing <$k>");
258 delete $INC{$k};
259
260 $k =~ s/\.pm$//;
261 $k =~ s/\//::/g;
262
263 if (my $cb = $k->can ("unload_module")) {
264 $cb->();
265 }
266
267 Symbol::delete_package $k;
268 }
269
270 # 4. get rid of ext::, as good as possible
271 Symbol::delete_package "ext::$_"
272 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
273
274 # 5. remove register_script_function callbacks
275 # TODO
276
277 # 6. unload cf.pm "a bit"
278 delete $INC{"cf.pm"};
279
280 # don't, removes xs symbols, too
281 #Symbol::delete_package __PACKAGE__;
282
283 # 7. reload cf.pm
284 $who->message ("reloading cf.pm");
285 require cf;
286 }; 448 };
287 warn $@ if $@;
288 $who->message ($@) if $@;
289 warn "reloaded\n";
290
291 $who->message ("reloaded");
292 } else {
293 $who->message ("Intruder Alert!");
294 } 449 }
295}; 450};
296 451
297############################################################################# 452#############################################################################
298# utility functions 453# utility functions
332} 487}
333 488
334############################################################################# 489#############################################################################
335# load/save/clean perl data associated with a map 490# load/save/clean perl data associated with a map
336 491
337*on_mapclean = sub { 492*cf::mapsupport::on_clean = sub {
338 my ($map) = @_; 493 my ($map) = @_;
339 494
340 my $path = $map->tmpname; 495 my $path = $map->tmpname;
341 defined $path or return; 496 defined $path or return;
342 497
343 unlink "$path.cfperl"; 498 unlink "$path.cfperl";
344}; 499};
345 500
346*on_mapin = 501*cf::mapsupport::on_swapin =
347*on_mapload = sub { 502*cf::mapsupport::on_load = sub {
348 my ($map) = @_; 503 my ($map) = @_;
349 504
350 my $path = $map->tmpname; 505 my $path = $map->tmpname;
351 $path = $map->path unless defined $path; 506 $path = $map->path unless defined $path;
352 507
359 or return; # too new 514 or return; # too new
360 515
361 $map->_set_obs ($data->{obs}); 516 $map->_set_obs ($data->{obs});
362}; 517};
363 518
364*on_mapout = sub { 519*cf::mapsupport::on_swapout = sub {
365 my ($map) = @_; 520 my ($map) = @_;
366 521
367 my $path = $map->tmpname; 522 my $path = $map->tmpname;
368 $path = $map->path unless defined $path; 523 $path = $map->path unless defined $path;
369 524
386 } else { 541 } else {
387 unlink "$path.cfperl"; 542 unlink "$path.cfperl";
388 } 543 }
389}; 544};
390 545
546attach_to_maps prio => -10000, package => cf::mapsupport::;
547
391############################################################################# 548#############################################################################
392# load/save perl data associated with player->ob objects 549# load/save perl data associated with player->ob objects
393 550
394sub all_objects(@) { 551sub all_objects(@) {
395 @_, map all_objects ($_->inv), @_ 552 @_, map all_objects ($_->inv), @_
396} 553}
397 554
398*on_player_load = sub { 555attach_to_players
556 on_load => sub {
399 my ($ob, $path) = @_; 557 my ($pl, $path) = @_;
400 558
401 for my $o (all_objects $ob) { 559 for my $o (all_objects $pl->ob) {
402 if (my $value = $o->get_ob_key_value ("_perl_data")) { 560 if (my $value = $o->get_ob_key_value ("_perl_data")) {
403 $o->set_ob_key_value ("_perl_data"); 561 $o->set_ob_key_value ("_perl_data");
404 562
405 %$o = %{ Storable::thaw pack "H*", $value }; 563 %$o = %{ Storable::thaw pack "H*", $value };
564 }
406 } 565 }
407 } 566 },
408}; 567 on_save => sub {
409
410*on_player_save = sub {
411 my ($ob, $path) = @_; 568 my ($pl, $path) = @_;
412 569
413 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 570 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
414 for grep %$_, all_objects $ob; 571 for grep %$_, all_objects $pl->ob;
415}; 572 },
573;
416 574
417############################################################################# 575#############################################################################
418# core extensions - in perl 576# core extensions - in perl
419 577
420=item cf::player::exists $login 578=item cf::player::exists $login
482 ["cf::object::player" => qw(player)], 640 ["cf::object::player" => qw(player)],
483 ["cf::player" => qw(peaceful)], 641 ["cf::player" => qw(peaceful)],
484) { 642) {
485 no strict 'refs'; 643 no strict 'refs';
486 my ($pkg, @funs) = @$_; 644 my ($pkg, @funs) = @$_;
487 *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 645 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
488 for @funs; 646 for @funs;
489} 647}
490 648
491sub safe_eval($;@) { 649sub safe_eval($;@) {
492 my ($code, %vars) = @_; 650 my ($code, %vars) = @_;
494 my $qcode = $code; 652 my $qcode = $code;
495 $qcode =~ s/"/‟/g; # not allowed in #line filenames 653 $qcode =~ s/"/‟/g; # not allowed in #line filenames
496 $qcode =~ s/\n/\\n/g; 654 $qcode =~ s/\n/\\n/g;
497 655
498 local $_; 656 local $_;
499 local @ext::cf::_safe_eval_args = values %vars; 657 local @safe::cf::_safe_eval_args = values %vars;
500 658
501 $code = 659 $code =
502 "do {\n" 660 "do {\n"
503 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 661 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
504 . "#line 0 \"{$qcode}\"\n" 662 . "#line 0 \"{$qcode}\"\n"
515 673
516sub register_script_function { 674sub register_script_function {
517 my ($fun, $cb) = @_; 675 my ($fun, $cb) = @_;
518 676
519 no strict 'refs'; 677 no strict 'refs';
520 *{"ext::$fun"} = $safe_hole->wrap ($cb); 678 *{"safe::$fun"} = $safe_hole->wrap ($cb);
521} 679}
522 680
523############################################################################# 681#############################################################################
524# the server's main() 682# the server's main()
525 683
526sub run { 684sub main {
527 Event::loop; 685 Event::loop;
528} 686}
529 687
530############################################################################# 688#############################################################################
531# initialisation 689# initialisation
543 cf::server_tick; # one server iteration 701 cf::server_tick; # one server iteration
544 702
545 my $NOW = Event::time; 703 my $NOW = Event::time;
546 $NEXT_TICK += $TICK; 704 $NEXT_TICK += $TICK;
547 705
548 # if we are delayed by > 0.25 second, skip ticks 706 # if we are delayed by four ticks, skip them all
549 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25; 707 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
550 708
551 $TICK_WATCHER->at ($NEXT_TICK); 709 $TICK_WATCHER->at ($NEXT_TICK);
552 $TICK_WATCHER->start; 710 $TICK_WATCHER->start;
553 }, 711 },
554); 712);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines