1 | package cf; |
1 | package cf; |
|
|
2 | |
|
|
3 | use utf8; |
|
|
4 | use strict; |
2 | |
5 | |
3 | use Symbol; |
6 | use Symbol; |
4 | use List::Util; |
7 | use List::Util; |
5 | use Storable; |
8 | use Storable; |
6 | use Opcode; |
9 | use Opcode; |
7 | use Safe; |
10 | use Safe; |
8 | use Safe::Hole; |
11 | use Safe::Hole; |
9 | |
12 | |
|
|
13 | use Coro 3.3; |
|
|
14 | use Coro::Event; |
|
|
15 | use Coro::Timer; |
|
|
16 | use Coro::Signal; |
|
|
17 | use Coro::Semaphore; |
|
|
18 | use Coro::AIO; |
|
|
19 | |
|
|
20 | use Fcntl; |
10 | use IO::AIO (); |
21 | use IO::AIO 2.31 (); |
11 | use YAML::Syck (); |
22 | use YAML::Syck (); |
12 | use Time::HiRes; |
23 | use Time::HiRes; |
13 | use Event; |
24 | |
14 | $Event::Eval = 1; # no idea why this is required, but it is |
25 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
15 | |
26 | |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
27 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
17 | $YAML::Syck::ImplicitUnicode = 1; |
28 | $YAML::Syck::ImplicitUnicode = 1; |
18 | |
29 | |
19 | use strict; |
30 | $Coro::main->prio (2); # run main coroutine ("the server") with very high priority |
20 | |
31 | |
21 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
32 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
22 | |
33 | |
23 | our %COMMAND = (); |
34 | our %COMMAND = (); |
24 | our %COMMAND_TIME = (); |
35 | our %COMMAND_TIME = (); |
25 | our %EXTCMD = (); |
36 | our %EXTCMD = (); |
26 | |
37 | |
27 | _init_vars; |
|
|
28 | |
|
|
29 | our @EVENT; |
38 | our @EVENT; |
30 | our $LIBDIR = datadir . "/ext"; |
39 | our $LIBDIR = datadir . "/ext"; |
31 | |
40 | |
32 | our $TICK = MAX_TIME * 1e-6; |
41 | our $TICK = MAX_TIME * 1e-6; |
33 | our $TICK_WATCHER; |
42 | our $TICK_WATCHER; |
34 | our $NEXT_TICK; |
43 | our $NEXT_TICK; |
|
|
44 | our $NOW; |
35 | |
45 | |
36 | our %CFG; |
46 | our %CFG; |
37 | |
47 | |
38 | our $UPTIME; $UPTIME ||= time; |
48 | our $UPTIME; $UPTIME ||= time; |
|
|
49 | our $RUNTIME; |
|
|
50 | |
|
|
51 | our %MAP; # all maps |
|
|
52 | our $LINK_MAP; # the special {link} map |
|
|
53 | our $FREEZE; |
|
|
54 | |
|
|
55 | binmode STDOUT; |
|
|
56 | binmode STDERR; |
|
|
57 | |
|
|
58 | # read virtual server time, if available |
|
|
59 | unless ($RUNTIME || !-e cf::localdir . "/runtime") { |
|
|
60 | open my $fh, "<", cf::localdir . "/runtime" |
|
|
61 | or die "unable to read runtime file: $!"; |
|
|
62 | $RUNTIME = <$fh> + 0.; |
|
|
63 | } |
|
|
64 | |
|
|
65 | mkdir cf::localdir; |
|
|
66 | mkdir cf::localdir . "/" . cf::playerdir; |
|
|
67 | mkdir cf::localdir . "/" . cf::tmpdir; |
|
|
68 | mkdir cf::localdir . "/" . cf::uniquedir; |
|
|
69 | |
|
|
70 | our %EXT_CORO; |
39 | |
71 | |
40 | ############################################################################# |
72 | ############################################################################# |
41 | |
73 | |
42 | =head2 GLOBAL VARIABLES |
74 | =head2 GLOBAL VARIABLES |
43 | |
75 | |
44 | =over 4 |
76 | =over 4 |
45 | |
77 | |
46 | =item $cf::UPTIME |
78 | =item $cf::UPTIME |
47 | |
79 | |
48 | The timestamp of the server start (so not actually an uptime). |
80 | The timestamp of the server start (so not actually an uptime). |
|
|
81 | |
|
|
82 | =item $cf::RUNTIME |
|
|
83 | |
|
|
84 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
|
|
85 | every server tick. |
49 | |
86 | |
50 | =item $cf::LIBDIR |
87 | =item $cf::LIBDIR |
51 | |
88 | |
52 | The perl library directory, where extensions and cf-specific modules can |
89 | The perl library directory, where extensions and cf-specific modules can |
53 | be found. It will be added to C<@INC> automatically. |
90 | be found. It will be added to C<@INC> automatically. |
|
|
91 | |
|
|
92 | =item $cf::NOW |
|
|
93 | |
|
|
94 | The time of the last (current) server tick. |
54 | |
95 | |
55 | =item $cf::TICK |
96 | =item $cf::TICK |
56 | |
97 | |
57 | The interval between server ticks, in seconds. |
98 | The interval between server ticks, in seconds. |
58 | |
99 | |
… | |
… | |
66 | =cut |
107 | =cut |
67 | |
108 | |
68 | BEGIN { |
109 | BEGIN { |
69 | *CORE::GLOBAL::warn = sub { |
110 | *CORE::GLOBAL::warn = sub { |
70 | my $msg = join "", @_; |
111 | my $msg = join "", @_; |
|
|
112 | utf8::encode $msg; |
|
|
113 | |
71 | $msg .= "\n" |
114 | $msg .= "\n" |
72 | unless $msg =~ /\n$/; |
115 | unless $msg =~ /\n$/; |
73 | |
116 | |
74 | print STDERR "cfperl: $msg"; |
|
|
75 | LOG llevError, "cfperl: $msg"; |
117 | LOG llevError, "cfperl: $msg"; |
76 | }; |
118 | }; |
77 | } |
119 | } |
78 | |
120 | |
79 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
121 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
… | |
… | |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
126 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
85 | |
127 | |
86 | # we bless all objects into (empty) derived classes to force a method lookup |
128 | # we bless all objects into (empty) derived classes to force a method lookup |
87 | # within the Safe compartment. |
129 | # within the Safe compartment. |
88 | for my $pkg (qw( |
130 | for my $pkg (qw( |
89 | cf::global |
131 | cf::global cf::attachable |
90 | cf::object cf::object::player |
132 | cf::object cf::object::player |
91 | cf::client cf::player |
133 | cf::client cf::player |
92 | cf::arch cf::living |
134 | cf::arch cf::living |
93 | cf::map cf::party cf::region |
135 | cf::map cf::party cf::region |
94 | )) { |
136 | )) { |
… | |
… | |
130 | =cut |
172 | =cut |
131 | |
173 | |
132 | sub to_json($) { |
174 | sub to_json($) { |
133 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
175 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
134 | JSON::Syck::Dump $_[0] |
176 | JSON::Syck::Dump $_[0] |
|
|
177 | } |
|
|
178 | |
|
|
179 | # main coro must never ever "block" except in Event |
|
|
180 | # sync_job ensures this by running the job in a coroutine |
|
|
181 | # and waiting in Event while the server is otherwise frozen |
|
|
182 | sub sync_job(&) { |
|
|
183 | my ($job) = @_; |
|
|
184 | |
|
|
185 | my $busy = 1; |
|
|
186 | my @res; |
|
|
187 | |
|
|
188 | local $FREEZE = 1; |
|
|
189 | |
|
|
190 | my $coro = Coro::async { |
|
|
191 | @res = eval { $job->() }; |
|
|
192 | warn $@ if $@; |
|
|
193 | undef $busy; |
|
|
194 | }; |
|
|
195 | |
|
|
196 | if ($Coro::current == $Coro::main) { |
|
|
197 | $coro->prio (Coro::PRIO_MAX); |
|
|
198 | while ($busy) { |
|
|
199 | Coro::cede_notself; |
|
|
200 | Event::one_event unless Coro::nready; |
|
|
201 | } |
|
|
202 | } else { |
|
|
203 | $coro->join; |
|
|
204 | } |
|
|
205 | |
|
|
206 | wantarray ? @res : $res[0] |
|
|
207 | } |
|
|
208 | |
|
|
209 | =item $coro = cf::coro { BLOCK } |
|
|
210 | |
|
|
211 | Creates and returns a new coro. This coro is automcatially being canceled |
|
|
212 | when the extension calling this is being unloaded. |
|
|
213 | |
|
|
214 | =cut |
|
|
215 | |
|
|
216 | sub coro(&) { |
|
|
217 | my $cb = shift; |
|
|
218 | |
|
|
219 | my $coro; $coro = async { |
|
|
220 | eval { |
|
|
221 | $cb->(); |
|
|
222 | }; |
|
|
223 | warn $@ if $@; |
|
|
224 | }; |
|
|
225 | |
|
|
226 | $coro->on_destroy (sub { |
|
|
227 | delete $EXT_CORO{$coro+0}; |
|
|
228 | }); |
|
|
229 | $EXT_CORO{$coro+0} = $coro; |
|
|
230 | |
|
|
231 | $coro |
135 | } |
232 | } |
136 | |
233 | |
137 | =back |
234 | =back |
138 | |
235 | |
139 | =cut |
236 | =cut |
… | |
… | |
249 | my ($self, $victim) = @_; |
346 | my ($self, $victim) = @_; |
250 | ... |
347 | ... |
251 | } |
348 | } |
252 | } |
349 | } |
253 | |
350 | |
|
|
351 | =item $attachable->valid |
|
|
352 | |
|
|
353 | Just because you have a perl object does not mean that the corresponding |
|
|
354 | C-level object still exists. If you try to access an object that has no |
|
|
355 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
356 | can be used to test for existence of the C object part without causing an |
|
|
357 | exception. |
|
|
358 | |
254 | =cut |
359 | =cut |
255 | |
360 | |
256 | # the following variables are defined in .xs and must not be re-created |
361 | # the following variables are defined in .xs and must not be re-created |
257 | our @CB_GLOBAL = (); # registry for all global events |
362 | our @CB_GLOBAL = (); # registry for all global events |
|
|
363 | our @CB_ATTACHABLE = (); # registry for all attachables |
258 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
364 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
259 | our @CB_PLAYER = (); |
365 | our @CB_PLAYER = (); |
260 | our @CB_CLIENT = (); |
366 | our @CB_CLIENT = (); |
261 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
367 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
262 | our @CB_MAP = (); |
368 | our @CB_MAP = (); |
263 | |
369 | |
264 | my %attachment; |
370 | my %attachment; |
265 | |
371 | |
266 | sub _attach_cb($$$$) { |
372 | sub _attach_cb($$$$) { |
267 | my ($registry, $event, $prio, $cb) = @_; |
373 | my ($registry, $event, $prio, $cb) = @_; |
… | |
… | |
272 | |
378 | |
273 | @{$registry->[$event]} = sort |
379 | @{$registry->[$event]} = sort |
274 | { $a->[0] cmp $b->[0] } |
380 | { $a->[0] cmp $b->[0] } |
275 | @{$registry->[$event] || []}, $cb; |
381 | @{$registry->[$event] || []}, $cb; |
276 | } |
382 | } |
|
|
383 | |
|
|
384 | # hack |
|
|
385 | my %attachable_klass = map +($_ => 1), KLASS_OBJECT, KLASS_CLIENT, KLASS_PLAYER, KLASS_MAP; |
277 | |
386 | |
278 | # attach handles attaching event callbacks |
387 | # attach handles attaching event callbacks |
279 | # the only thing the caller has to do is pass the correct |
388 | # the only thing the caller has to do is pass the correct |
280 | # registry (== where the callback attaches to). |
389 | # registry (== where the callback attaches to). |
281 | sub _attach { |
390 | sub _attach { |
… | |
… | |
283 | |
392 | |
284 | my $object_type; |
393 | my $object_type; |
285 | my $prio = 0; |
394 | my $prio = 0; |
286 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
395 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
287 | |
396 | |
|
|
397 | #TODO: get rid of this hack |
|
|
398 | if ($attachable_klass{$klass}) { |
|
|
399 | %cb_id = (%cb_id, map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == KLASS_ATTACHABLE, 0 .. $#EVENT); |
|
|
400 | } |
|
|
401 | |
288 | while (@arg) { |
402 | while (@arg) { |
289 | my $type = shift @arg; |
403 | my $type = shift @arg; |
290 | |
404 | |
291 | if ($type eq "prio") { |
405 | if ($type eq "prio") { |
292 | $prio = shift @arg; |
406 | $prio = shift @arg; |
… | |
… | |
367 | my ($obj, $name) = @_; |
481 | my ($obj, $name) = @_; |
368 | |
482 | |
369 | exists $obj->{_attachment}{$name} |
483 | exists $obj->{_attachment}{$name} |
370 | } |
484 | } |
371 | |
485 | |
372 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
486 | for my $klass (qw(ATTACHABLE GLOBAL OBJECT PLAYER CLIENT MAP)) { |
373 | eval "#line " . __LINE__ . " 'cf.pm' |
487 | eval "#line " . __LINE__ . " 'cf.pm' |
374 | sub cf::\L$klass\E::_attach_registry { |
488 | sub cf::\L$klass\E::_attach_registry { |
375 | (\\\@CB_$klass, KLASS_$klass) |
489 | (\\\@CB_$klass, KLASS_$klass) |
376 | } |
490 | } |
377 | |
491 | |
… | |
… | |
413 | } |
527 | } |
414 | |
528 | |
415 | 0 |
529 | 0 |
416 | } |
530 | } |
417 | |
531 | |
418 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
532 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
419 | |
533 | |
420 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
421 | |
|
|
422 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
423 | |
|
|
424 | =item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) |
534 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
425 | |
535 | |
426 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
427 | |
|
|
428 | Generate a global/object/player/map-specific event with the given arguments. |
536 | Generate an object-specific event with the given arguments. |
429 | |
537 | |
430 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
538 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
431 | removed in future versions), and there is no public API to access override |
539 | removed in future versions), and there is no public API to access override |
432 | results (if you must, access C<@cf::invoke_results> directly). |
540 | results (if you must, access C<@cf::invoke_results> directly). |
433 | |
541 | |
434 | =back |
542 | =back |
435 | |
543 | |
436 | =cut |
544 | =cut |
437 | |
545 | |
438 | ############################################################################# |
546 | ############################################################################# |
439 | |
|
|
440 | =head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS |
|
|
441 | |
|
|
442 | Attachable objects includes objects, players, clients and maps. |
|
|
443 | |
|
|
444 | =over 4 |
|
|
445 | |
|
|
446 | =item $object->valid |
|
|
447 | |
|
|
448 | Just because you have a perl object does not mean that the corresponding |
|
|
449 | C-level object still exists. If you try to access an object that has no |
|
|
450 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
451 | can be used to test for existence of the C object part without causing an |
|
|
452 | exception. |
|
|
453 | |
|
|
454 | =back |
|
|
455 | |
|
|
456 | =cut |
|
|
457 | |
|
|
458 | ############################################################################# |
|
|
459 | # object support |
547 | # object support |
460 | |
548 | |
461 | sub instantiate { |
|
|
462 | my ($obj, $data) = @_; |
|
|
463 | |
|
|
464 | $data = from_json $data; |
|
|
465 | |
|
|
466 | for (@$data) { |
|
|
467 | my ($name, $args) = @$_; |
|
|
468 | |
|
|
469 | $obj->attach ($name, %{$args || {} }); |
|
|
470 | } |
|
|
471 | } |
|
|
472 | |
|
|
473 | # basically do the same as instantiate, without calling instantiate |
|
|
474 | sub reattach { |
549 | sub reattach { |
|
|
550 | # basically do the same as instantiate, without calling instantiate |
475 | my ($obj) = @_; |
551 | my ($obj) = @_; |
|
|
552 | |
476 | my $registry = $obj->registry; |
553 | my $registry = $obj->registry; |
477 | |
554 | |
478 | @$registry = (); |
555 | @$registry = (); |
479 | |
556 | |
480 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
557 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
… | |
… | |
489 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
566 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
490 | } |
567 | } |
491 | } |
568 | } |
492 | } |
569 | } |
493 | |
570 | |
494 | sub object_freezer_save { |
571 | cf::attachable->attach ( |
495 | my ($filename, $rdata, $objs) = @_; |
|
|
496 | |
|
|
497 | if (length $$rdata) { |
|
|
498 | warn sprintf "saving %s (%d,%d)\n", |
|
|
499 | $filename, length $$rdata, scalar @$objs; |
|
|
500 | |
|
|
501 | if (open my $fh, ">:raw", "$filename~") { |
|
|
502 | chmod SAVE_MODE, $fh; |
|
|
503 | syswrite $fh, $$rdata; |
|
|
504 | close $fh; |
|
|
505 | |
|
|
506 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
507 | chmod SAVE_MODE, $fh; |
|
|
508 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
509 | close $fh; |
|
|
510 | rename "$filename.pst~", "$filename.pst"; |
|
|
511 | } else { |
|
|
512 | unlink "$filename.pst"; |
|
|
513 | } |
|
|
514 | |
|
|
515 | rename "$filename~", $filename; |
|
|
516 | } else { |
|
|
517 | warn "FATAL: $filename~: $!\n"; |
|
|
518 | } |
|
|
519 | } else { |
|
|
520 | unlink $filename; |
|
|
521 | unlink "$filename.pst"; |
|
|
522 | } |
|
|
523 | } |
|
|
524 | |
|
|
525 | sub object_freezer_as_string { |
|
|
526 | my ($rdata, $objs) = @_; |
|
|
527 | |
|
|
528 | use Data::Dumper; |
|
|
529 | |
|
|
530 | $$rdata . Dumper $objs |
|
|
531 | } |
|
|
532 | |
|
|
533 | sub object_thawer_load { |
|
|
534 | my ($filename) = @_; |
|
|
535 | |
|
|
536 | local $/; |
|
|
537 | |
|
|
538 | my $av; |
|
|
539 | |
|
|
540 | #TODO: use sysread etc. |
|
|
541 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
542 | $data = <$data>; |
|
|
543 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
|
|
544 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
545 | } |
|
|
546 | return ($data, $av); |
|
|
547 | } |
|
|
548 | |
|
|
549 | () |
|
|
550 | } |
|
|
551 | |
|
|
552 | cf::object->attach ( |
|
|
553 | prio => -1000000, |
572 | prio => -1000000, |
|
|
573 | on_instantiate => sub { |
|
|
574 | my ($obj, $data) = @_; |
|
|
575 | |
|
|
576 | $data = from_json $data; |
|
|
577 | |
|
|
578 | for (@$data) { |
|
|
579 | my ($name, $args) = @$_; |
|
|
580 | |
|
|
581 | $obj->attach ($name, %{$args || {} }); |
|
|
582 | } |
|
|
583 | }, |
|
|
584 | on_reattach => \&reattach, |
554 | on_clone => sub { |
585 | on_clone => sub { |
555 | my ($src, $dst) = @_; |
586 | my ($src, $dst) = @_; |
556 | |
587 | |
557 | @{$dst->registry} = @{$src->registry}; |
588 | @{$dst->registry} = @{$src->registry}; |
558 | |
589 | |
… | |
… | |
560 | |
591 | |
561 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
592 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
562 | if exists $src->{_attachment}; |
593 | if exists $src->{_attachment}; |
563 | }, |
594 | }, |
564 | ); |
595 | ); |
|
|
596 | |
|
|
597 | sub object_freezer_save { |
|
|
598 | my ($filename, $rdata, $objs) = @_; |
|
|
599 | |
|
|
600 | sync_job { |
|
|
601 | if (length $$rdata) { |
|
|
602 | warn sprintf "saving %s (%d,%d)\n", |
|
|
603 | $filename, length $$rdata, scalar @$objs; |
|
|
604 | |
|
|
605 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
|
|
606 | chmod SAVE_MODE, $fh; |
|
|
607 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
|
|
608 | aio_fsync $fh; |
|
|
609 | close $fh; |
|
|
610 | |
|
|
611 | if (@$objs) { |
|
|
612 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
|
|
613 | chmod SAVE_MODE, $fh; |
|
|
614 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
|
|
615 | aio_write $fh, 0, (length $data), $data, 0; |
|
|
616 | aio_fsync $fh; |
|
|
617 | close $fh; |
|
|
618 | aio_rename "$filename.pst~", "$filename.pst"; |
|
|
619 | } |
|
|
620 | } else { |
|
|
621 | aio_unlink "$filename.pst"; |
|
|
622 | } |
|
|
623 | |
|
|
624 | aio_rename "$filename~", $filename; |
|
|
625 | } else { |
|
|
626 | warn "FATAL: $filename~: $!\n"; |
|
|
627 | } |
|
|
628 | } else { |
|
|
629 | aio_unlink $filename; |
|
|
630 | aio_unlink "$filename.pst"; |
|
|
631 | } |
|
|
632 | } |
|
|
633 | } |
|
|
634 | |
|
|
635 | sub object_freezer_as_string { |
|
|
636 | my ($rdata, $objs) = @_; |
|
|
637 | |
|
|
638 | use Data::Dumper; |
|
|
639 | |
|
|
640 | $$rdata . Dumper $objs |
|
|
641 | } |
|
|
642 | |
|
|
643 | sub object_thawer_load { |
|
|
644 | my ($filename) = @_; |
|
|
645 | |
|
|
646 | my ($data, $av); |
|
|
647 | |
|
|
648 | (aio_load $filename, $data) >= 0 |
|
|
649 | or return; |
|
|
650 | |
|
|
651 | unless (aio_stat "$filename.pst") { |
|
|
652 | (aio_load "$filename.pst", $av) >= 0 |
|
|
653 | or return; |
|
|
654 | $av = eval { (Storable::thaw <$av>)->{objs} }; |
|
|
655 | } |
|
|
656 | |
|
|
657 | return ($data, $av); |
|
|
658 | } |
565 | |
659 | |
566 | ############################################################################# |
660 | ############################################################################# |
567 | # command handling &c |
661 | # command handling &c |
568 | |
662 | |
569 | =item cf::register_command $name => \&callback($ob,$args); |
663 | =item cf::register_command $name => \&callback($ob,$args); |
… | |
… | |
758 | |
852 | |
759 | =head2 CORE EXTENSIONS |
853 | =head2 CORE EXTENSIONS |
760 | |
854 | |
761 | Functions and methods that extend core crossfire objects. |
855 | Functions and methods that extend core crossfire objects. |
762 | |
856 | |
|
|
857 | =head3 cf::player |
|
|
858 | |
763 | =over 4 |
859 | =over 4 |
764 | |
860 | |
765 | =item cf::player::exists $login |
861 | =item cf::player::exists $login |
766 | |
862 | |
767 | Returns true when the given account exists. |
863 | Returns true when the given account exists. |
… | |
… | |
771 | sub cf::player::exists($) { |
867 | sub cf::player::exists($) { |
772 | cf::player::find $_[0] |
868 | cf::player::find $_[0] |
773 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
869 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
774 | } |
870 | } |
775 | |
871 | |
|
|
872 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
873 | |
|
|
874 | Sends an ext reply to the player. |
|
|
875 | |
|
|
876 | =cut |
|
|
877 | |
|
|
878 | sub cf::player::ext_reply($$$%) { |
|
|
879 | my ($self, $id, %msg) = @_; |
|
|
880 | |
|
|
881 | $msg{msgid} = $id; |
|
|
882 | |
|
|
883 | $self->send ("ext " . to_json \%msg); |
|
|
884 | } |
|
|
885 | |
|
|
886 | =back |
|
|
887 | |
|
|
888 | =head3 cf::object::player |
|
|
889 | |
|
|
890 | =over 4 |
|
|
891 | |
776 | =item $player_object->reply ($npc, $msg[, $flags]) |
892 | =item $player_object->reply ($npc, $msg[, $flags]) |
777 | |
893 | |
778 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
894 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
779 | can be C<undef>. Does the right thing when the player is currently in a |
895 | can be C<undef>. Does the right thing when the player is currently in a |
780 | dialogue with the given NPC character. |
896 | dialogue with the given NPC character. |
781 | |
897 | |
782 | =cut |
898 | =cut |
783 | |
899 | |
784 | # rough implementation of a future "reply" method that works |
900 | # rough implementation of a future "reply" method that works |
785 | # with dialog boxes. |
901 | # with dialog boxes. |
|
|
902 | #TODO: the first argument must go, split into a $npc->reply_to ( method |
786 | sub cf::object::player::reply($$$;$) { |
903 | sub cf::object::player::reply($$$;$) { |
787 | my ($self, $npc, $msg, $flags) = @_; |
904 | my ($self, $npc, $msg, $flags) = @_; |
788 | |
905 | |
789 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
906 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
790 | |
907 | |
… | |
… | |
794 | $msg = $npc->name . " says: $msg" if $npc; |
911 | $msg = $npc->name . " says: $msg" if $npc; |
795 | $self->message ($msg, $flags); |
912 | $self->message ($msg, $flags); |
796 | } |
913 | } |
797 | } |
914 | } |
798 | |
915 | |
799 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
800 | |
|
|
801 | Sends an ext reply to the player. |
|
|
802 | |
|
|
803 | =cut |
|
|
804 | |
|
|
805 | sub cf::player::ext_reply($$$%) { |
|
|
806 | my ($self, $id, %msg) = @_; |
|
|
807 | |
|
|
808 | $msg{msgid} = $id; |
|
|
809 | |
|
|
810 | $self->send ("ext " . to_json \%msg); |
|
|
811 | } |
|
|
812 | |
|
|
813 | =item $player_object->may ("access") |
916 | =item $player_object->may ("access") |
814 | |
917 | |
815 | Returns wether the given player is authorized to access resource "access" |
918 | Returns wether the given player is authorized to access resource "access" |
816 | (e.g. "command_wizcast"). |
919 | (e.g. "command_wizcast"). |
817 | |
920 | |
… | |
… | |
824 | (ref $cf::CFG{"may_$access"} |
927 | (ref $cf::CFG{"may_$access"} |
825 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
928 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
826 | : $cf::CFG{"may_$access"}) |
929 | : $cf::CFG{"may_$access"}) |
827 | } |
930 | } |
828 | |
931 | |
829 | =cut |
932 | =head3 cf::client |
830 | |
933 | |
831 | ############################################################################# |
934 | =over 4 |
|
|
935 | |
|
|
936 | =item $client->send_drawinfo ($text, $flags) |
|
|
937 | |
|
|
938 | Sends a drawinfo packet to the client. Circumvents output buffering so |
|
|
939 | should not be used under normal circumstances. |
|
|
940 | |
|
|
941 | =cut |
|
|
942 | |
|
|
943 | sub cf::client::send_drawinfo { |
|
|
944 | my ($self, $text, $flags) = @_; |
|
|
945 | |
|
|
946 | utf8::encode $text; |
|
|
947 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
|
|
948 | } |
|
|
949 | |
|
|
950 | |
|
|
951 | =item $success = $client->query ($flags, "text", \&cb) |
|
|
952 | |
|
|
953 | Queues a query to the client, calling the given callback with |
|
|
954 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
|
|
955 | C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. |
|
|
956 | |
|
|
957 | Queries can fail, so check the return code. Or don't, as queries will become |
|
|
958 | reliable at some point in the future. |
|
|
959 | |
|
|
960 | =cut |
|
|
961 | |
|
|
962 | sub cf::client::query { |
|
|
963 | my ($self, $flags, $text, $cb) = @_; |
|
|
964 | |
|
|
965 | return unless $self->state == ST_PLAYING |
|
|
966 | || $self->state == ST_SETUP |
|
|
967 | || $self->state == ST_CUSTOM; |
|
|
968 | |
|
|
969 | $self->state (ST_CUSTOM); |
|
|
970 | |
|
|
971 | utf8::encode $text; |
|
|
972 | push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; |
|
|
973 | |
|
|
974 | $self->send_packet ($self->{query_queue}[0][0]) |
|
|
975 | if @{ $self->{query_queue} } == 1; |
|
|
976 | } |
|
|
977 | |
|
|
978 | cf::client->attach ( |
|
|
979 | on_reply => sub { |
|
|
980 | my ($ns, $msg) = @_; |
|
|
981 | |
|
|
982 | # this weird shuffling is so that direct followup queries |
|
|
983 | # get handled first |
|
|
984 | my $queue = delete $ns->{query_queue}; |
|
|
985 | |
|
|
986 | (shift @$queue)->[1]->($msg); |
|
|
987 | |
|
|
988 | push @{ $ns->{query_queue} }, @$queue; |
|
|
989 | |
|
|
990 | if (@{ $ns->{query_queue} } == @$queue) { |
|
|
991 | if (@$queue) { |
|
|
992 | $ns->send_packet ($ns->{query_queue}[0][0]); |
|
|
993 | } else { |
|
|
994 | $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; |
|
|
995 | } |
|
|
996 | } |
|
|
997 | }, |
|
|
998 | ); |
|
|
999 | |
|
|
1000 | =item $client->coro (\&cb) |
|
|
1001 | |
|
|
1002 | Create a new coroutine, running the specified callback. The coroutine will |
|
|
1003 | be automatically cancelled when the client gets destroyed (e.g. on logout, |
|
|
1004 | or loss of connection). |
|
|
1005 | |
|
|
1006 | =cut |
|
|
1007 | |
|
|
1008 | sub cf::client::coro { |
|
|
1009 | my ($self, $cb) = @_; |
|
|
1010 | |
|
|
1011 | my $coro; $coro = async { |
|
|
1012 | eval { |
|
|
1013 | $cb->(); |
|
|
1014 | }; |
|
|
1015 | warn $@ if $@; |
|
|
1016 | }; |
|
|
1017 | |
|
|
1018 | $coro->on_destroy (sub { |
|
|
1019 | delete $self->{_coro}{$coro+0}; |
|
|
1020 | }); |
|
|
1021 | |
|
|
1022 | $self->{_coro}{$coro+0} = $coro; |
|
|
1023 | |
|
|
1024 | $coro |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | cf::client->attach ( |
|
|
1028 | on_destroy => sub { |
|
|
1029 | my ($ns) = @_; |
|
|
1030 | |
|
|
1031 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
|
|
1032 | }, |
|
|
1033 | ); |
|
|
1034 | |
|
|
1035 | =back |
|
|
1036 | |
832 | |
1037 | |
833 | =head2 SAFE SCRIPTING |
1038 | =head2 SAFE SCRIPTING |
834 | |
1039 | |
835 | Functions that provide a safe environment to compile and execute |
1040 | Functions that provide a safe environment to compile and execute |
836 | snippets of perl code without them endangering the safety of the server |
1041 | snippets of perl code without them endangering the safety of the server |
… | |
… | |
1065 | } |
1270 | } |
1066 | |
1271 | |
1067 | ############################################################################# |
1272 | ############################################################################# |
1068 | # initialisation |
1273 | # initialisation |
1069 | |
1274 | |
1070 | sub _perl_reload(&) { |
1275 | sub _perl_reload() { |
1071 | my ($msg) = @_; |
1276 | warn "reloading..."; |
1072 | |
|
|
1073 | $msg->("reloading..."); |
|
|
1074 | |
1277 | |
1075 | eval { |
1278 | eval { |
|
|
1279 | local $FREEZE = 1; |
|
|
1280 | |
|
|
1281 | cf::emergency_save; |
|
|
1282 | |
1076 | # cancel all watchers |
1283 | # cancel all watchers |
1077 | for (Event::all_watchers) { |
1284 | for (Event::all_watchers) { |
1078 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1285 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1079 | } |
1286 | } |
1080 | |
1287 | |
|
|
1288 | # cancel all extension coros |
|
|
1289 | $_->cancel for values %EXT_CORO; |
|
|
1290 | %EXT_CORO = (); |
|
|
1291 | |
1081 | # unload all extensions |
1292 | # unload all extensions |
1082 | for (@exts) { |
1293 | for (@exts) { |
1083 | $msg->("unloading <$_>"); |
1294 | warn "unloading <$_>"; |
1084 | unload_extension $_; |
1295 | unload_extension $_; |
1085 | } |
1296 | } |
1086 | |
1297 | |
1087 | # unload all modules loaded from $LIBDIR |
1298 | # unload all modules loaded from $LIBDIR |
1088 | while (my ($k, $v) = each %INC) { |
1299 | while (my ($k, $v) = each %INC) { |
1089 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
1300 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
1090 | |
1301 | |
1091 | $msg->("removing <$k>"); |
1302 | warn "removing <$k>"; |
1092 | delete $INC{$k}; |
1303 | delete $INC{$k}; |
1093 | |
1304 | |
1094 | $k =~ s/\.pm$//; |
1305 | $k =~ s/\.pm$//; |
1095 | $k =~ s/\//::/g; |
1306 | $k =~ s/\//::/g; |
1096 | |
1307 | |
… | |
… | |
1101 | Symbol::delete_package $k; |
1312 | Symbol::delete_package $k; |
1102 | } |
1313 | } |
1103 | |
1314 | |
1104 | # sync database to disk |
1315 | # sync database to disk |
1105 | cf::db_sync; |
1316 | cf::db_sync; |
|
|
1317 | IO::AIO::flush; |
1106 | |
1318 | |
1107 | # get rid of safe::, as good as possible |
1319 | # get rid of safe::, as good as possible |
1108 | Symbol::delete_package "safe::$_" |
1320 | Symbol::delete_package "safe::$_" |
1109 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
1321 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
1110 | |
1322 | |
1111 | # remove register_script_function callbacks |
1323 | # remove register_script_function callbacks |
1112 | # TODO |
1324 | # TODO |
1113 | |
1325 | |
1114 | # unload cf.pm "a bit" |
1326 | # unload cf.pm "a bit" |
… | |
… | |
1117 | # don't, removes xs symbols, too, |
1329 | # don't, removes xs symbols, too, |
1118 | # and global variables created in xs |
1330 | # and global variables created in xs |
1119 | #Symbol::delete_package __PACKAGE__; |
1331 | #Symbol::delete_package __PACKAGE__; |
1120 | |
1332 | |
1121 | # reload cf.pm |
1333 | # reload cf.pm |
1122 | $msg->("reloading cf.pm"); |
1334 | warn "reloading cf.pm"; |
1123 | require cf; |
1335 | require cf; |
|
|
1336 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
1124 | |
1337 | |
1125 | # load config and database again |
1338 | # load config and database again |
1126 | cf::cfg_load; |
1339 | cf::cfg_load; |
1127 | cf::db_load; |
1340 | cf::db_load; |
1128 | |
1341 | |
1129 | # load extensions |
1342 | # load extensions |
1130 | $msg->("load extensions"); |
1343 | warn "load extensions"; |
1131 | cf::load_extensions; |
1344 | cf::load_extensions; |
1132 | |
1345 | |
1133 | # reattach attachments to objects |
1346 | # reattach attachments to objects |
1134 | $msg->("reattach"); |
1347 | warn "reattach"; |
1135 | _global_reattach; |
1348 | _global_reattach; |
1136 | }; |
1349 | }; |
1137 | $msg->($@) if $@; |
1350 | warn $@ if $@; |
1138 | |
1351 | |
1139 | $msg->("reloaded"); |
1352 | warn "reloaded"; |
1140 | }; |
1353 | }; |
1141 | |
1354 | |
1142 | sub perl_reload() { |
1355 | sub perl_reload() { |
1143 | _perl_reload { |
1356 | _perl_reload; |
1144 | warn $_[0]; |
|
|
1145 | print "$_[0]\n"; |
|
|
1146 | }; |
|
|
1147 | } |
1357 | } |
1148 | |
1358 | |
1149 | register "<global>", __PACKAGE__; |
1359 | register "<global>", __PACKAGE__; |
1150 | |
1360 | |
1151 | register_command "perl-reload" => sub { |
1361 | register_command "perl-reload" => sub { |
1152 | my ($who, $arg) = @_; |
1362 | my ($who, $arg) = @_; |
1153 | |
1363 | |
1154 | if ($who->flag (FLAG_WIZ)) { |
1364 | if ($who->flag (FLAG_WIZ)) { |
|
|
1365 | $who->message ("reloading..."); |
1155 | _perl_reload { |
1366 | _perl_reload; |
1156 | warn $_[0]; |
|
|
1157 | $who->message ($_[0]); |
|
|
1158 | }; |
|
|
1159 | } |
1367 | } |
1160 | }; |
1368 | }; |
1161 | |
1369 | |
1162 | unshift @INC, $LIBDIR; |
1370 | unshift @INC, $LIBDIR; |
1163 | |
1371 | |
1164 | $TICK_WATCHER = Event->timer ( |
1372 | $TICK_WATCHER = Event->timer ( |
|
|
1373 | reentrant => 0, |
1165 | prio => 0, |
1374 | prio => 0, |
1166 | at => $NEXT_TICK || 1, |
1375 | at => $NEXT_TICK || $TICK, |
1167 | data => WF_AUTOCANCEL, |
1376 | data => WF_AUTOCANCEL, |
1168 | cb => sub { |
1377 | cb => sub { |
|
|
1378 | unless ($FREEZE) { |
1169 | cf::server_tick; # one server iteration |
1379 | cf::server_tick; # one server iteration |
|
|
1380 | $RUNTIME += $TICK; |
|
|
1381 | } |
1170 | |
1382 | |
1171 | my $NOW = Event::time; |
|
|
1172 | $NEXT_TICK += $TICK; |
1383 | $NEXT_TICK += $TICK; |
1173 | |
1384 | |
1174 | # if we are delayed by four ticks or more, skip them all |
1385 | # if we are delayed by four ticks or more, skip them all |
1175 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1386 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
1176 | |
1387 | |
1177 | $TICK_WATCHER->at ($NEXT_TICK); |
1388 | $TICK_WATCHER->at ($NEXT_TICK); |
1178 | $TICK_WATCHER->start; |
1389 | $TICK_WATCHER->start; |
1179 | }, |
1390 | }, |
1180 | ); |
1391 | ); |
… | |
… | |
1185 | poll => 'r', |
1396 | poll => 'r', |
1186 | prio => 5, |
1397 | prio => 5, |
1187 | data => WF_AUTOCANCEL, |
1398 | data => WF_AUTOCANCEL, |
1188 | cb => \&IO::AIO::poll_cb); |
1399 | cb => \&IO::AIO::poll_cb); |
1189 | |
1400 | |
|
|
1401 | # we must not ever block the main coroutine |
|
|
1402 | $Coro::idle = sub { |
|
|
1403 | #Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d# |
|
|
1404 | warn "FATAL: Coro::idle was called, major BUG\n"; |
|
|
1405 | (Coro::unblock_sub { |
|
|
1406 | Event::one_event; |
|
|
1407 | })->(); |
|
|
1408 | }; |
|
|
1409 | |
1190 | 1 |
1410 | 1 |
1191 | |
1411 | |