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.93 by root, Thu Dec 21 22:41:35 2006 UTC vs.
Revision 1.104 by root, Sat Dec 30 16:56:16 2006 UTC

1package cf; 1package cf;
2
3use utf8;
4use strict;
2 5
3use Symbol; 6use Symbol;
4use List::Util; 7use List::Util;
5use Storable; 8use Storable;
6use Opcode; 9use Opcode;
7use Safe; 10use Safe;
8use Safe::Hole; 11use Safe::Hole;
9 12
13use Coro 3.3;
14use Coro::Event;
15use Coro::Timer;
16use Coro::Signal;
17use Coro::Semaphore;
18
10use IO::AIO (); 19use IO::AIO 2.3;
11use YAML::Syck (); 20use YAML::Syck ();
12use Time::HiRes; 21use Time::HiRes;
13use Event; 22
14$Event::Eval = 1; # no idea why this is required, but it is 23use Event; $Event::Eval = 1; # no idea why this is required, but it is
15 24
16# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? 25# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode?
17$YAML::Syck::ImplicitUnicode = 1; 26$YAML::Syck::ImplicitUnicode = 1;
18 27
19use strict; 28$Coro::main->prio (2); # run main coroutine ("the server") with very high priority
20 29
21sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 30sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
22 31
23our %COMMAND = (); 32our %COMMAND = ();
24our %COMMAND_TIME = (); 33our %COMMAND_TIME = ();
25our %EXTCMD = (); 34our %EXTCMD = ();
26 35
27_init_vars;
28
29our @EVENT; 36our @EVENT;
30our $LIBDIR = datadir . "/ext"; 37our $LIBDIR = datadir . "/ext";
31 38
32our $TICK = MAX_TIME * 1e-6; 39our $TICK = MAX_TIME * 1e-6;
33our $TICK_WATCHER; 40our $TICK_WATCHER;
34our $NEXT_TICK; 41our $NEXT_TICK;
42our $NOW;
35 43
36our %CFG; 44our %CFG;
37 45
38our $UPTIME; $UPTIME ||= time; 46our $UPTIME; $UPTIME ||= time;
47our $RUNTIME;
48
49our %MAP; # all maps
50our $LINK_MAP; # the special {link} map
51our $FREEZE;
52
53binmode STDOUT;
54binmode STDERR;
55
56# read virtual server time, if available
57unless ($RUNTIME || !-e cf::localdir . "/runtime") {
58 open my $fh, "<", cf::localdir . "/runtime"
59 or die "unable to read runtime file: $!";
60 $RUNTIME = <$fh> + 0.;
61}
62
63mkdir cf::localdir;
64mkdir cf::localdir . "/" . cf::playerdir;
65mkdir cf::localdir . "/" . cf::tmpdir;
66mkdir cf::localdir . "/" . cf::uniquedir;
67
68our %EXT_CORO;
39 69
40############################################################################# 70#############################################################################
41 71
42=head2 GLOBAL VARIABLES 72=head2 GLOBAL VARIABLES
43 73
44=over 4 74=over 4
45 75
46=item $cf::UPTIME 76=item $cf::UPTIME
47 77
48The timestamp of the server start (so not actually an uptime). 78The timestamp of the server start (so not actually an uptime).
79
80=item $cf::RUNTIME
81
82The time this server has run, starts at 0 and is increased by $cf::TICK on
83every server tick.
49 84
50=item $cf::LIBDIR 85=item $cf::LIBDIR
51 86
52The perl library directory, where extensions and cf-specific modules can 87The perl library directory, where extensions and cf-specific modules can
53be found. It will be added to C<@INC> automatically. 88be found. It will be added to C<@INC> automatically.
89
90=item $cf::NOW
91
92The time of the last (current) server tick.
54 93
55=item $cf::TICK 94=item $cf::TICK
56 95
57The interval between server ticks, in seconds. 96The interval between server ticks, in seconds.
58 97
66=cut 105=cut
67 106
68BEGIN { 107BEGIN {
69 *CORE::GLOBAL::warn = sub { 108 *CORE::GLOBAL::warn = sub {
70 my $msg = join "", @_; 109 my $msg = join "", @_;
110 utf8::encode $msg;
111
71 $msg .= "\n" 112 $msg .= "\n"
72 unless $msg =~ /\n$/; 113 unless $msg =~ /\n$/;
73 114
74 print STDERR "cfperl: $msg";
75 LOG llevError, "cfperl: $msg"; 115 LOG llevError, "cfperl: $msg";
76 }; 116 };
77} 117}
78 118
79@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 119@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
84@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 124@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
85 125
86# we bless all objects into (empty) derived classes to force a method lookup 126# we bless all objects into (empty) derived classes to force a method lookup
87# within the Safe compartment. 127# within the Safe compartment.
88for my $pkg (qw( 128for my $pkg (qw(
89 cf::global 129 cf::global cf::attachable
90 cf::object cf::object::player 130 cf::object cf::object::player
91 cf::client cf::player 131 cf::client cf::player
92 cf::arch cf::living 132 cf::arch cf::living
93 cf::map cf::party cf::region 133 cf::map cf::party cf::region
94)) { 134)) {
132sub to_json($) { 172sub to_json($) {
133 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs 173 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
134 JSON::Syck::Dump $_[0] 174 JSON::Syck::Dump $_[0]
135} 175}
136 176
177=item $coro = cf::coro { BLOCK }
178
179Creates and returns a new coro. This coro is automcatially being canceled
180when the extension calling this is being unloaded.
181
182=cut
183
184sub coro(&) {
185 my $cb = shift;
186
187 my $coro; $coro = async {
188 eval {
189 $cb->();
190 };
191 warn $@ if $@;
192 };
193
194 $coro->on_destroy (sub {
195 delete $EXT_CORO{$coro+0};
196 });
197 $EXT_CORO{$coro+0} = $coro;
198
199 $coro
200}
201
137=back 202=back
138 203
139=cut 204=cut
140 205
141############################################################################# 206#############################################################################
142 207
143=head2 ATTACHABLE OBJECTS 208=head2 ATTACHABLE OBJECTS
144 209
145You can define and attach attachments to each "attachable" object in 210Many objects in crossfire are so-called attachable objects. That means you can
146crossfire+ (objects, players, clients, maps and the special "global" 211attach callbacks/event handlers (a collection of which is called an "attachment")
212to it. All such attachable objects support the following methods.
213
147class). In the following description, CLASS can be any of C<global>, 214In the following description, CLASS can be any of C<global>, C<object>
148C<object> C<player>, C<client> or C<map>. 215C<player>, C<client> or C<map> (i.e. the attachable objects in
216crossfire+).
149 217
150=over 4 218=over 4
151 219
220=item $attachable->attach ($attachment, key => $value...)
221
222=item $attachable->detach ($attachment)
223
224Attach/detach a pre-registered attachment to a specific object and give it
225the specified key/value pairs as arguments.
226
227Example, attach a minesweeper attachment to the given object, making it a
22810x10 minesweeper game:
229
230 $obj->attach (minesweeper => width => 10, height => 10);
231
232=item $bool = $attachable->attached ($name)
233
234Checks wether the named attachment is currently attached to the object.
235
236=item cf::CLASS->attach ...
237
238=item cf::CLASS->detach ...
239
240Define an anonymous attachment and attach it to all objects of the given
241CLASS. See the next function for an explanation of its arguments.
242
243You can attach to global events by using the C<cf::global> class.
244
245Example, log all player logins:
246
247 cf::player->attach (
248 on_login => sub {
249 my ($pl) = @_;
250 ...
251 },
252 );
253
254Example, attach to the jeweler skill:
255
256 cf::object->attach (
257 type => cf::SKILL,
258 subtype => cf::SK_JEWELER,
259 on_use_skill => sub {
260 my ($sk, $ob, $part, $dir, $msg) = @_;
261 ...
262 },
263 );
264
152=item cf::CLASS::attachment $name, ... 265=item cf::CLASS::attachment $name, ...
153 266
154Register an attachment by name through which attachable objects can refer 267Register an attachment by C<$name> through which attachable objects of the
155to this attachment. 268given CLASS can refer to this attachment.
156 269
157=item $bool = $attachable->attached ($name) 270Some classes such as crossfire maps and objects can specify attachments
271that are attached at load/instantiate time, thus the need for a name.
158 272
159Checks wether the named attachment is currently attached to the object.
160
161=item $attachable->attach ($attachment, key => $value...)
162
163=item $attachable->detach ($attachment)
164
165Attach/detach a pre-registered attachment either to a specific object
166(C<$attachable>) or all objects of the given class (if C<$attachable> is a
167class in a static method call).
168
169You can attach to global events by using the C<cf::global> class.
170
171These method calls expect any number of the following handler/hook 273These calls expect any number of the following handler/hook descriptions:
172descriptions:
173 274
174=over 4 275=over 4
175 276
176=item prio => $number 277=item prio => $number
177 278
203package and register them. Only handlers for eevents supported by the 304package and register them. Only handlers for eevents supported by the
204object/class are recognised. 305object/class are recognised.
205 306
206=back 307=back
207 308
309Example, define an attachment called "sockpuppet" that calls the given
310event handler when a monster attacks:
311
312 cf::object::attachment sockpuppet =>
313 on_skill_attack => sub {
314 my ($self, $victim) = @_;
315 ...
316 }
317 }
318
319=item $attachable->valid
320
321Just because you have a perl object does not mean that the corresponding
322C-level object still exists. If you try to access an object that has no
323valid C counterpart anymore you get an exception at runtime. This method
324can be used to test for existence of the C object part without causing an
325exception.
326
208=cut 327=cut
209 328
210# the following variables are defined in .xs and must not be re-created 329# the following variables are defined in .xs and must not be re-created
211our @CB_GLOBAL = (); # registry for all global events 330our @CB_GLOBAL = (); # registry for all global events
331our @CB_ATTACHABLE = (); # registry for all attachables
212our @CB_OBJECT = (); # all objects (should not be used except in emergency) 332our @CB_OBJECT = (); # all objects (should not be used except in emergency)
213our @CB_PLAYER = (); 333our @CB_PLAYER = ();
214our @CB_CLIENT = (); 334our @CB_CLIENT = ();
215our @CB_TYPE = (); # registry for type (cf-object class) based events 335our @CB_TYPE = (); # registry for type (cf-object class) based events
216our @CB_MAP = (); 336our @CB_MAP = ();
217 337
218my %attachment; 338my %attachment;
219 339
220sub _attach_cb($$$$) { 340sub _attach_cb($$$$) {
221 my ($registry, $event, $prio, $cb) = @_; 341 my ($registry, $event, $prio, $cb) = @_;
226 346
227 @{$registry->[$event]} = sort 347 @{$registry->[$event]} = sort
228 { $a->[0] cmp $b->[0] } 348 { $a->[0] cmp $b->[0] }
229 @{$registry->[$event] || []}, $cb; 349 @{$registry->[$event] || []}, $cb;
230} 350}
351
352# hack
353my %attachable_klass = map +($_ => 1), KLASS_OBJECT, KLASS_CLIENT, KLASS_PLAYER, KLASS_MAP;
231 354
232# attach handles attaching event callbacks 355# attach handles attaching event callbacks
233# the only thing the caller has to do is pass the correct 356# the only thing the caller has to do is pass the correct
234# registry (== where the callback attaches to). 357# registry (== where the callback attaches to).
235sub _attach { 358sub _attach {
237 360
238 my $object_type; 361 my $object_type;
239 my $prio = 0; 362 my $prio = 0;
240 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; 363 my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT;
241 364
365 #TODO: get rid of this hack
366 if ($attachable_klass{$klass}) {
367 %cb_id = (%cb_id, map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == KLASS_ATTACHABLE, 0 .. $#EVENT);
368 }
369
242 while (@arg) { 370 while (@arg) {
243 my $type = shift @arg; 371 my $type = shift @arg;
244 372
245 if ($type eq "prio") { 373 if ($type eq "prio") {
246 $prio = shift @arg; 374 $prio = shift @arg;
321 my ($obj, $name) = @_; 449 my ($obj, $name) = @_;
322 450
323 exists $obj->{_attachment}{$name} 451 exists $obj->{_attachment}{$name}
324} 452}
325 453
326for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { 454for my $klass (qw(ATTACHABLE GLOBAL OBJECT PLAYER CLIENT MAP)) {
327 eval "#line " . __LINE__ . " 'cf.pm' 455 eval "#line " . __LINE__ . " 'cf.pm'
328 sub cf::\L$klass\E::_attach_registry { 456 sub cf::\L$klass\E::_attach_registry {
329 (\\\@CB_$klass, KLASS_$klass) 457 (\\\@CB_$klass, KLASS_$klass)
330 } 458 }
331 459
367 } 495 }
368 496
369 0 497 0
370} 498}
371 499
372=item $bool = cf::invoke EVENT_GLOBAL_XXX, ... 500=item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...)
373 501
374=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
375
376=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
377
378=item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) 502=item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...)
379 503
380=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
381
382Generate a global/object/player/map-specific event with the given arguments. 504Generate an object-specific event with the given arguments.
383 505
384This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be 506This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be
385removed in future versions), and there is no public API to access override 507removed in future versions), and there is no public API to access override
386results (if you must, access C<@cf::invoke_results> directly). 508results (if you must, access C<@cf::invoke_results> directly).
387 509
388=back 510=back
389 511
390=cut 512=cut
391 513
392############################################################################# 514#############################################################################
393
394=head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS
395
396Attachable objects includes objects, players, clients and maps.
397
398=over 4
399
400=item $object->valid
401
402Just because you have a perl object does not mean that the corresponding
403C-level object still exists. If you try to access an object that has no
404valid C counterpart anymore you get an exception at runtime. This method
405can be used to test for existence of the C object part without causing an
406exception.
407
408=back
409
410=cut
411
412#############################################################################
413# object support 515# object support
414 516
415sub instantiate {
416 my ($obj, $data) = @_;
417
418 $data = from_json $data;
419
420 for (@$data) {
421 my ($name, $args) = @$_;
422
423 $obj->attach ($name, %{$args || {} });
424 }
425}
426
427# basically do the same as instantiate, without calling instantiate
428sub reattach { 517sub reattach {
518 # basically do the same as instantiate, without calling instantiate
429 my ($obj) = @_; 519 my ($obj) = @_;
520
430 my $registry = $obj->registry; 521 my $registry = $obj->registry;
431 522
432 @$registry = (); 523 @$registry = ();
433 524
434 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; 525 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
443 warn "object uses attachment '$name' that is not available, postponing.\n"; 534 warn "object uses attachment '$name' that is not available, postponing.\n";
444 } 535 }
445 } 536 }
446} 537}
447 538
539cf::attachable->attach (
540 prio => -1000000,
541 on_instantiate => sub {
542 my ($obj, $data) = @_;
543
544 $data = from_json $data;
545
546 for (@$data) {
547 my ($name, $args) = @$_;
548
549 $obj->attach ($name, %{$args || {} });
550 }
551 },
552 on_reattach => \&reattach,
553 on_clone => sub {
554 my ($src, $dst) = @_;
555
556 @{$dst->registry} = @{$src->registry};
557
558 %$dst = %$src;
559
560 %{$dst->{_attachment}} = %{$src->{_attachment}}
561 if exists $src->{_attachment};
562 },
563);
564
448sub object_freezer_save { 565sub object_freezer_save {
449 my ($filename, $rdata, $objs) = @_; 566 my ($filename, $rdata, $objs) = @_;
450 567
451 if (length $$rdata) { 568 if (length $$rdata) {
452 warn sprintf "saving %s (%d,%d)\n", 569 warn sprintf "saving %s (%d,%d)\n",
501 } 618 }
502 619
503 () 620 ()
504} 621}
505 622
506cf::object->attach (
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
520############################################################################# 623#############################################################################
521# command handling &c 624# command handling &c
522 625
523=item cf::register_command $name => \&callback($ob,$args); 626=item cf::register_command $name => \&callback($ob,$args);
524 627
712 815
713=head2 CORE EXTENSIONS 816=head2 CORE EXTENSIONS
714 817
715Functions and methods that extend core crossfire objects. 818Functions and methods that extend core crossfire objects.
716 819
820=head3 cf::player
821
717=over 4 822=over 4
718 823
719=item cf::player::exists $login 824=item cf::player::exists $login
720 825
721Returns true when the given account exists. 826Returns true when the given account exists.
725sub cf::player::exists($) { 830sub cf::player::exists($) {
726 cf::player::find $_[0] 831 cf::player::find $_[0]
727 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 832 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
728} 833}
729 834
835=item $player->ext_reply ($msgid, $msgtype, %msg)
836
837Sends an ext reply to the player.
838
839=cut
840
841sub cf::player::ext_reply($$$%) {
842 my ($self, $id, %msg) = @_;
843
844 $msg{msgid} = $id;
845
846 $self->send ("ext " . to_json \%msg);
847}
848
849=back
850
851=head3 cf::object::player
852
853=over 4
854
730=item $player_object->reply ($npc, $msg[, $flags]) 855=item $player_object->reply ($npc, $msg[, $flags])
731 856
732Sends a message to the player, as if the npc C<$npc> replied. C<$npc> 857Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
733can be C<undef>. Does the right thing when the player is currently in a 858can be C<undef>. Does the right thing when the player is currently in a
734dialogue with the given NPC character. 859dialogue with the given NPC character.
735 860
736=cut 861=cut
737 862
738# rough implementation of a future "reply" method that works 863# rough implementation of a future "reply" method that works
739# with dialog boxes. 864# with dialog boxes.
865#TODO: the first argument must go, split into a $npc->reply_to ( method
740sub cf::object::player::reply($$$;$) { 866sub cf::object::player::reply($$$;$) {
741 my ($self, $npc, $msg, $flags) = @_; 867 my ($self, $npc, $msg, $flags) = @_;
742 868
743 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; 869 $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4;
744 870
748 $msg = $npc->name . " says: $msg" if $npc; 874 $msg = $npc->name . " says: $msg" if $npc;
749 $self->message ($msg, $flags); 875 $self->message ($msg, $flags);
750 } 876 }
751} 877}
752 878
753=item $player->ext_reply ($msgid, $msgtype, %msg)
754
755Sends an ext reply to the player.
756
757=cut
758
759sub cf::player::ext_reply($$$%) {
760 my ($self, $id, %msg) = @_;
761
762 $msg{msgid} = $id;
763
764 $self->send ("ext " . to_json \%msg);
765}
766
767=item $player_object->may ("access") 879=item $player_object->may ("access")
768 880
769Returns wether the given player is authorized to access resource "access" 881Returns wether the given player is authorized to access resource "access"
770(e.g. "command_wizcast"). 882(e.g. "command_wizcast").
771 883
778 (ref $cf::CFG{"may_$access"} 890 (ref $cf::CFG{"may_$access"}
779 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} 891 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}}
780 : $cf::CFG{"may_$access"}) 892 : $cf::CFG{"may_$access"})
781} 893}
782 894
783=cut 895=head3 cf::client
784 896
785############################################################################# 897=over 4
898
899=item $client->send_drawinfo ($text, $flags)
900
901Sends a drawinfo packet to the client. Circumvents output buffering so
902should not be used under normal circumstances.
903
904=cut
905
906sub cf::client::send_drawinfo {
907 my ($self, $text, $flags) = @_;
908
909 utf8::encode $text;
910 $self->send_packet (sprintf "drawinfo %d %s", $flags, $text);
911}
912
913
914=item $success = $client->query ($flags, "text", \&cb)
915
916Queues a query to the client, calling the given callback with
917the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>,
918C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>.
919
920Queries can fail, so check the return code. Or don't, as queries will become
921reliable at some point in the future.
922
923=cut
924
925sub cf::client::query {
926 my ($self, $flags, $text, $cb) = @_;
927
928 return unless $self->state == ST_PLAYING
929 || $self->state == ST_SETUP
930 || $self->state == ST_CUSTOM;
931
932 $self->state (ST_CUSTOM);
933
934 utf8::encode $text;
935 push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb];
936
937 $self->send_packet ($self->{query_queue}[0][0])
938 if @{ $self->{query_queue} } == 1;
939}
940
941cf::client->attach (
942 on_reply => sub {
943 my ($ns, $msg) = @_;
944
945 # this weird shuffling is so that direct followup queries
946 # get handled first
947 my $queue = delete $ns->{query_queue};
948
949 (shift @$queue)->[1]->($msg);
950
951 push @{ $ns->{query_queue} }, @$queue;
952
953 if (@{ $ns->{query_queue} } == @$queue) {
954 if (@$queue) {
955 $ns->send_packet ($ns->{query_queue}[0][0]);
956 } else {
957 $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM;
958 }
959 }
960 },
961);
962
963=item $client->coro (\&cb)
964
965Create a new coroutine, running the specified callback. The coroutine will
966be automatically cancelled when the client gets destroyed (e.g. on logout,
967or loss of connection).
968
969=cut
970
971sub cf::client::coro {
972 my ($self, $cb) = @_;
973
974 my $coro; $coro = async {
975 eval {
976 $cb->();
977 };
978 warn $@ if $@;
979 };
980
981 $coro->on_destroy (sub {
982 delete $self->{_coro}{$coro+0};
983 });
984
985 $self->{_coro}{$coro+0} = $coro;
986
987 $coro
988}
989
990cf::client->attach (
991 on_destroy => sub {
992 my ($ns) = @_;
993
994 $_->cancel for values %{ (delete $ns->{_coro}) || {} };
995 },
996);
997
998=back
999
786 1000
787=head2 SAFE SCRIPTING 1001=head2 SAFE SCRIPTING
788 1002
789Functions that provide a safe environment to compile and execute 1003Functions that provide a safe environment to compile and execute
790snippets of perl code without them endangering the safety of the server 1004snippets of perl code without them endangering the safety of the server
1019} 1233}
1020 1234
1021############################################################################# 1235#############################################################################
1022# initialisation 1236# initialisation
1023 1237
1024sub _perl_reload(&) { 1238sub _perl_reload() {
1025 my ($msg) = @_; 1239 warn "reloading...";
1026
1027 $msg->("reloading...");
1028 1240
1029 eval { 1241 eval {
1242 local $FREEZE = 1;
1243
1244 cf::emergency_save;
1245
1030 # cancel all watchers 1246 # cancel all watchers
1031 for (Event::all_watchers) { 1247 for (Event::all_watchers) {
1032 $_->cancel if $_->data & WF_AUTOCANCEL; 1248 $_->cancel if $_->data & WF_AUTOCANCEL;
1033 } 1249 }
1034 1250
1251 # cancel all extension coros
1252 $_->cancel for values %EXT_CORO;
1253 %EXT_CORO = ();
1254
1035 # unload all extensions 1255 # unload all extensions
1036 for (@exts) { 1256 for (@exts) {
1037 $msg->("unloading <$_>"); 1257 warn "unloading <$_>";
1038 unload_extension $_; 1258 unload_extension $_;
1039 } 1259 }
1040 1260
1041 # unload all modules loaded from $LIBDIR 1261 # unload all modules loaded from $LIBDIR
1042 while (my ($k, $v) = each %INC) { 1262 while (my ($k, $v) = each %INC) {
1043 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 1263 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
1044 1264
1045 $msg->("removing <$k>"); 1265 warn "removing <$k>";
1046 delete $INC{$k}; 1266 delete $INC{$k};
1047 1267
1048 $k =~ s/\.pm$//; 1268 $k =~ s/\.pm$//;
1049 $k =~ s/\//::/g; 1269 $k =~ s/\//::/g;
1050 1270
1055 Symbol::delete_package $k; 1275 Symbol::delete_package $k;
1056 } 1276 }
1057 1277
1058 # sync database to disk 1278 # sync database to disk
1059 cf::db_sync; 1279 cf::db_sync;
1280 IO::AIO::flush;
1060 1281
1061 # get rid of safe::, as good as possible 1282 # get rid of safe::, as good as possible
1062 Symbol::delete_package "safe::$_" 1283 Symbol::delete_package "safe::$_"
1063 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); 1284 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
1064 1285
1065 # remove register_script_function callbacks 1286 # remove register_script_function callbacks
1066 # TODO 1287 # TODO
1067 1288
1068 # unload cf.pm "a bit" 1289 # unload cf.pm "a bit"
1071 # don't, removes xs symbols, too, 1292 # don't, removes xs symbols, too,
1072 # and global variables created in xs 1293 # and global variables created in xs
1073 #Symbol::delete_package __PACKAGE__; 1294 #Symbol::delete_package __PACKAGE__;
1074 1295
1075 # reload cf.pm 1296 # reload cf.pm
1076 $msg->("reloading cf.pm"); 1297 warn "reloading cf.pm";
1077 require cf; 1298 require cf;
1299 cf::_connect_to_perl; # nominally unnecessary, but cannot hurt
1078 1300
1079 # load config and database again 1301 # load config and database again
1080 cf::cfg_load; 1302 cf::cfg_load;
1081 cf::db_load; 1303 cf::db_load;
1082 1304
1083 # load extensions 1305 # load extensions
1084 $msg->("load extensions"); 1306 warn "load extensions";
1085 cf::load_extensions; 1307 cf::load_extensions;
1086 1308
1087 # reattach attachments to objects 1309 # reattach attachments to objects
1088 $msg->("reattach"); 1310 warn "reattach";
1089 _global_reattach; 1311 _global_reattach;
1090 }; 1312 };
1091 $msg->($@) if $@; 1313 warn $@ if $@;
1092 1314
1093 $msg->("reloaded"); 1315 warn "reloaded";
1094}; 1316};
1095 1317
1096sub perl_reload() { 1318sub perl_reload() {
1097 _perl_reload { 1319 _perl_reload;
1098 warn $_[0];
1099 print "$_[0]\n";
1100 };
1101} 1320}
1102 1321
1103register "<global>", __PACKAGE__; 1322register "<global>", __PACKAGE__;
1104 1323
1105register_command "perl-reload" => sub { 1324register_command "perl-reload" => sub {
1106 my ($who, $arg) = @_; 1325 my ($who, $arg) = @_;
1107 1326
1108 if ($who->flag (FLAG_WIZ)) { 1327 if ($who->flag (FLAG_WIZ)) {
1328 $who->message ("reloading...");
1109 _perl_reload { 1329 _perl_reload;
1110 warn $_[0];
1111 $who->message ($_[0]);
1112 };
1113 } 1330 }
1114}; 1331};
1115 1332
1116unshift @INC, $LIBDIR; 1333unshift @INC, $LIBDIR;
1117 1334
1118$TICK_WATCHER = Event->timer ( 1335$TICK_WATCHER = Event->timer (
1336 reentrant => 0,
1119 prio => 0, 1337 prio => 0,
1120 at => $NEXT_TICK || 1, 1338 at => $NEXT_TICK || $TICK,
1121 data => WF_AUTOCANCEL, 1339 data => WF_AUTOCANCEL,
1122 cb => sub { 1340 cb => sub {
1341 unless ($FREEZE) {
1123 cf::server_tick; # one server iteration 1342 cf::server_tick; # one server iteration
1343 $RUNTIME += $TICK;
1344 }
1124 1345
1125 my $NOW = Event::time;
1126 $NEXT_TICK += $TICK; 1346 $NEXT_TICK += $TICK;
1127 1347
1128 # if we are delayed by four ticks or more, skip them all 1348 # if we are delayed by four ticks or more, skip them all
1129 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; 1349 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
1130 1350
1131 $TICK_WATCHER->at ($NEXT_TICK); 1351 $TICK_WATCHER->at ($NEXT_TICK);
1132 $TICK_WATCHER->start; 1352 $TICK_WATCHER->start;
1133 }, 1353 },
1134); 1354);
1139 poll => 'r', 1359 poll => 'r',
1140 prio => 5, 1360 prio => 5,
1141 data => WF_AUTOCANCEL, 1361 data => WF_AUTOCANCEL,
1142 cb => \&IO::AIO::poll_cb); 1362 cb => \&IO::AIO::poll_cb);
1143 1363
1364# we must not ever block the main coroutine
1365$Coro::idle = sub {
1366 #Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d#
1367 warn "FATAL: Coro::idle was called, major BUG\n";
1368 (Coro::unblock_sub {
1369 Event::one_event;
1370 })->();
1371};
1372
11441 13731
1145 1374

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines