… | |
… | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Symbol; |
6 | use Symbol; |
7 | use List::Util; |
7 | use List::Util; |
|
|
8 | use Socket; |
8 | use Storable; |
9 | use Storable; |
|
|
10 | use Event; |
9 | use Opcode; |
11 | use Opcode; |
10 | use Safe; |
12 | use Safe; |
11 | use Safe::Hole; |
13 | use Safe::Hole; |
12 | |
14 | |
13 | use Coro 3.5 (); |
15 | use Coro 3.61 (); |
|
|
16 | use Coro::State; |
|
|
17 | use Coro::Handle; |
14 | use Coro::Event; |
18 | use Coro::Event; |
15 | use Coro::Timer; |
19 | use Coro::Timer; |
16 | use Coro::Signal; |
20 | use Coro::Signal; |
17 | use Coro::Semaphore; |
21 | use Coro::Semaphore; |
18 | use Coro::AIO; |
22 | use Coro::AIO; |
|
|
23 | use Coro::Storable; |
19 | |
24 | |
20 | use BDB (); |
25 | use BDB (); |
21 | use Data::Dumper; |
26 | use Data::Dumper; |
22 | use Digest::MD5; |
27 | use Digest::MD5; |
23 | use Fcntl; |
28 | use Fcntl; |
|
|
29 | use YAML::Syck (); |
24 | use IO::AIO 2.32 (); |
30 | use IO::AIO 2.32 (); |
25 | use YAML::Syck (); |
|
|
26 | use Time::HiRes; |
31 | use Time::HiRes; |
27 | use Compress::LZF; |
32 | use Compress::LZF; |
28 | |
33 | |
|
|
34 | # configure various modules to our taste |
|
|
35 | # |
|
|
36 | $Storable::canonical = 1; # reduce rsync transfers |
|
|
37 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
29 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
38 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
30 | |
39 | |
31 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
40 | $Event::Eval = 1; # no idea why this is required, but it is |
32 | |
|
|
33 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
34 | |
41 | |
35 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
42 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
36 | $YAML::Syck::ImplicitUnicode = 1; |
43 | $YAML::Syck::ImplicitUnicode = 1; |
37 | |
44 | |
38 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
45 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
|
|
46 | |
|
|
47 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
39 | |
48 | |
40 | our %COMMAND = (); |
49 | our %COMMAND = (); |
41 | our %COMMAND_TIME = (); |
50 | our %COMMAND_TIME = (); |
42 | |
51 | |
43 | our @EXTS = (); # list of extension package names |
52 | our @EXTS = (); # list of extension package names |
44 | our %EXTCMD = (); |
53 | our %EXTCMD = (); |
45 | our %EXT_CORO = (); # coroutines bound to extensions |
54 | our %EXT_CORO = (); # coroutines bound to extensions |
46 | our %EXT_MAP = (); # pluggable maps |
55 | our %EXT_MAP = (); # pluggable maps |
47 | |
56 | |
|
|
57 | our $RELOAD; # number of reloads so far |
48 | our @EVENT; |
58 | our @EVENT; |
49 | our $LIBDIR = datadir . "/ext"; |
|
|
50 | |
59 | |
51 | our $TICK = MAX_TIME * 1e-6; |
60 | our $CONFDIR = confdir; |
|
|
61 | our $DATADIR = datadir; |
|
|
62 | our $LIBDIR = "$DATADIR/ext"; |
|
|
63 | our $PODDIR = "$DATADIR/pod"; |
|
|
64 | our $MAPDIR = "$DATADIR/" . mapdir; |
|
|
65 | our $LOCALDIR = localdir; |
|
|
66 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
|
|
67 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
|
|
68 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
|
|
69 | our $RANDOMDIR = "$LOCALDIR/random"; |
|
|
70 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
71 | |
|
|
72 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
52 | our $TICK_WATCHER; |
73 | our $TICK_WATCHER; |
53 | our $AIO_POLL_WATCHER; |
74 | our $AIO_POLL_WATCHER; |
54 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
75 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
55 | our $NEXT_TICK; |
76 | our $NEXT_TICK; |
56 | our $NOW; |
77 | our $NOW; |
… | |
… | |
65 | our $RUNTIME; |
86 | our $RUNTIME; |
66 | |
87 | |
67 | our %PLAYER; # all users |
88 | our %PLAYER; # all users |
68 | our %MAP; # all maps |
89 | our %MAP; # all maps |
69 | our $LINK_MAP; # the special {link} map, which is always available |
90 | our $LINK_MAP; # the special {link} map, which is always available |
70 | our $RANDOM_MAPS = cf::localdir . "/random"; |
|
|
71 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
|
|
72 | |
|
|
73 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
|
|
74 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
|
|
75 | |
91 | |
76 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
92 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
77 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
93 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
78 | |
94 | |
79 | binmode STDOUT; |
95 | binmode STDOUT; |
80 | binmode STDERR; |
96 | binmode STDERR; |
81 | |
97 | |
82 | # read virtual server time, if available |
98 | # read virtual server time, if available |
83 | unless ($RUNTIME || !-e cf::localdir . "/runtime") { |
99 | unless ($RUNTIME || !-e "$LOCALDIR/runtime") { |
84 | open my $fh, "<", cf::localdir . "/runtime" |
100 | open my $fh, "<", "$LOCALDIR/runtime" |
85 | or die "unable to read runtime file: $!"; |
101 | or die "unable to read runtime file: $!"; |
86 | $RUNTIME = <$fh> + 0.; |
102 | $RUNTIME = <$fh> + 0.; |
87 | } |
103 | } |
88 | |
104 | |
89 | mkdir cf::localdir; |
105 | mkdir $_ |
90 | mkdir cf::localdir . "/" . cf::playerdir; |
106 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
91 | mkdir cf::localdir . "/" . cf::tmpdir; |
|
|
92 | mkdir cf::localdir . "/" . cf::uniquedir; |
|
|
93 | mkdir $RANDOM_MAPS; |
|
|
94 | mkdir $BDB_ENV_DIR; |
|
|
95 | |
107 | |
96 | our $EMERGENCY_POSITION; |
108 | our $EMERGENCY_POSITION; |
97 | |
109 | |
98 | sub cf::map::normalise; |
110 | sub cf::map::normalise; |
99 | |
111 | |
… | |
… | |
110 | =item $cf::RUNTIME |
122 | =item $cf::RUNTIME |
111 | |
123 | |
112 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
124 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
113 | every server tick. |
125 | every server tick. |
114 | |
126 | |
115 | =item $cf::LIBDIR |
127 | =item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR |
|
|
128 | $cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR |
|
|
129 | $cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR |
116 | |
130 | |
117 | The perl library directory, where extensions and cf-specific modules can |
131 | Various directories - "/etc", read-only install directory, perl-library |
118 | be found. It will be added to C<@INC> automatically. |
132 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
|
|
133 | unique-items directory, player file directory, random maps directory and |
|
|
134 | database environment. |
119 | |
135 | |
120 | =item $cf::NOW |
136 | =item $cf::NOW |
121 | |
137 | |
122 | The time of the last (current) server tick. |
138 | The time of the last (current) server tick. |
123 | |
139 | |
… | |
… | |
128 | =item %cf::CFG |
144 | =item %cf::CFG |
129 | |
145 | |
130 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
146 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
131 | from wherever your confdir points to. |
147 | from wherever your confdir points to. |
132 | |
148 | |
133 | =item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE |
149 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
134 | |
150 | |
135 | These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) |
151 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
136 | or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick |
152 | returns directly I<after> the tick processing (and consequently, can only wake one process |
137 | processing has been done. Call C<< ->wait >> on them to maximise the |
153 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
138 | window of cpu time available, or simply to synchronise to the server tick. |
|
|
139 | |
154 | |
140 | =back |
155 | =back |
141 | |
156 | |
142 | =cut |
157 | =cut |
143 | |
158 | |
144 | BEGIN { |
159 | BEGIN { |
145 | *CORE::GLOBAL::warn = sub { |
160 | *CORE::GLOBAL::warn = sub { |
146 | my $msg = join "", @_; |
161 | my $msg = join "", @_; |
147 | utf8::encode $msg; |
|
|
148 | |
162 | |
149 | $msg .= "\n" |
163 | $msg .= "\n" |
150 | unless $msg =~ /\n$/; |
164 | unless $msg =~ /\n$/; |
151 | |
165 | |
|
|
166 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
|
|
167 | |
|
|
168 | utf8::encode $msg; |
152 | LOG llevError, $msg; |
169 | LOG llevError, $msg; |
153 | }; |
170 | }; |
154 | } |
171 | } |
155 | |
172 | |
156 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
173 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
… | |
… | |
202 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
219 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
203 | $d |
220 | $d |
204 | } || "[unable to dump $_[0]: '$@']"; |
221 | } || "[unable to dump $_[0]: '$@']"; |
205 | } |
222 | } |
206 | |
223 | |
207 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
224 | use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working |
208 | |
225 | |
209 | =item $ref = cf::from_json $json |
226 | =item $ref = cf::from_json $json |
210 | |
227 | |
211 | Converts a JSON string into the corresponding perl data structure. |
228 | Converts a JSON string into the corresponding perl data structure. |
212 | |
229 | |
213 | =cut |
|
|
214 | |
|
|
215 | sub from_json($) { |
|
|
216 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
217 | JSON::Syck::Load $_[0] |
|
|
218 | } |
|
|
219 | |
|
|
220 | =item $json = cf::to_json $ref |
230 | =item $json = cf::to_json $ref |
221 | |
231 | |
222 | Converts a perl data structure into its JSON representation. |
232 | Converts a perl data structure into its JSON representation. |
223 | |
|
|
224 | =cut |
|
|
225 | |
|
|
226 | sub to_json($) { |
|
|
227 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
228 | JSON::Syck::Dump $_[0] |
|
|
229 | } |
|
|
230 | |
233 | |
231 | =item cf::lock_wait $string |
234 | =item cf::lock_wait $string |
232 | |
235 | |
233 | Wait until the given lock is available. See cf::lock_acquire. |
236 | Wait until the given lock is available. See cf::lock_acquire. |
234 | |
237 | |
… | |
… | |
240 | returned. |
243 | returned. |
241 | |
244 | |
242 | Lock names should begin with a unique identifier (for example, cf::map::find |
245 | Lock names should begin with a unique identifier (for example, cf::map::find |
243 | uses map_find and cf::map::load uses map_load). |
246 | uses map_find and cf::map::load uses map_load). |
244 | |
247 | |
|
|
248 | =item $locked = cf::lock_active $string |
|
|
249 | |
|
|
250 | Return true if the lock is currently active, i.e. somebody has locked it. |
|
|
251 | |
245 | =cut |
252 | =cut |
246 | |
253 | |
247 | our %LOCK; |
254 | our %LOCK; |
248 | |
255 | |
249 | sub lock_wait($) { |
256 | sub lock_wait($) { |
… | |
… | |
266 | |
273 | |
267 | Coro::guard { |
274 | Coro::guard { |
268 | # wake up all waiters, to be on the safe side |
275 | # wake up all waiters, to be on the safe side |
269 | $_->ready for @{ delete $LOCK{$key} }; |
276 | $_->ready for @{ delete $LOCK{$key} }; |
270 | } |
277 | } |
|
|
278 | } |
|
|
279 | |
|
|
280 | sub lock_active($) { |
|
|
281 | my ($key) = @_; |
|
|
282 | |
|
|
283 | ! ! $LOCK{$key} |
271 | } |
284 | } |
272 | |
285 | |
273 | sub freeze_mainloop { |
286 | sub freeze_mainloop { |
274 | return unless $TICK_WATCHER->is_active; |
287 | return unless $TICK_WATCHER->is_active; |
275 | |
288 | |
… | |
… | |
354 | |
367 | |
355 | $coro |
368 | $coro |
356 | } |
369 | } |
357 | |
370 | |
358 | sub write_runtime { |
371 | sub write_runtime { |
359 | my $runtime = cf::localdir . "/runtime"; |
372 | my $runtime = "$LOCALDIR/runtime"; |
360 | |
373 | |
|
|
374 | # first touch the runtime file to show we are still running: |
|
|
375 | # the fsync below can take a very very long time. |
|
|
376 | |
|
|
377 | warn "touching runtime...\n";#d# |
|
|
378 | if (my $fh = aio_open $runtime, O_WRONLY, 0) { |
|
|
379 | utime undef, undef, $fh; |
|
|
380 | } |
|
|
381 | |
|
|
382 | my $guard = cf::lock_acquire "write_runtime"; |
|
|
383 | |
|
|
384 | warn "starting to write runtime...\n";#d# |
361 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
385 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
362 | or return; |
386 | or return; |
363 | |
387 | |
364 | my $value = $cf::RUNTIME + 90 + 10; |
388 | my $value = $cf::RUNTIME + 90 + 10; |
365 | # 10 is the runtime save interval, for a monotonic clock |
389 | # 10 is the runtime save interval, for a monotonic clock |
… | |
… | |
370 | |
394 | |
371 | # always fsync - this file is important |
395 | # always fsync - this file is important |
372 | aio_fsync $fh |
396 | aio_fsync $fh |
373 | and return; |
397 | and return; |
374 | |
398 | |
|
|
399 | # touch it again to show we are up-to-date |
|
|
400 | utime undef, undef, $fh; |
|
|
401 | |
375 | close $fh |
402 | close $fh |
376 | or return; |
403 | or return; |
377 | |
404 | |
378 | aio_rename "$runtime~", $runtime |
405 | aio_rename "$runtime~", $runtime |
379 | and return; |
406 | and return; |
380 | |
407 | |
|
|
408 | warn "... done writing runtime.\n";#d# |
|
|
409 | |
381 | 1 |
410 | 1 |
|
|
411 | } |
|
|
412 | |
|
|
413 | =item cf::datalog type => key => value, ... |
|
|
414 | |
|
|
415 | Log a datalog packet of the given type with the given key-value pairs. |
|
|
416 | |
|
|
417 | =cut |
|
|
418 | |
|
|
419 | sub datalog($@) { |
|
|
420 | my ($type, %kv) = @_; |
|
|
421 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
382 | } |
422 | } |
383 | |
423 | |
384 | =back |
424 | =back |
385 | |
425 | |
386 | =cut |
426 | =cut |
… | |
… | |
948 | Returns the given player object, loading it if necessary (might block). |
988 | Returns the given player object, loading it if necessary (might block). |
949 | |
989 | |
950 | =cut |
990 | =cut |
951 | |
991 | |
952 | sub playerdir($) { |
992 | sub playerdir($) { |
953 | cf::localdir |
993 | "$PLAYERDIR/" |
954 | . "/" |
|
|
955 | . cf::playerdir |
|
|
956 | . "/" |
|
|
957 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
994 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
958 | } |
995 | } |
959 | |
996 | |
960 | sub path($) { |
997 | sub path($) { |
961 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
998 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
962 | |
999 | |
963 | (playerdir $login) . "/$login.pl" |
1000 | (playerdir $login) . "/playerdata" |
964 | } |
1001 | } |
965 | |
1002 | |
966 | sub find_active($) { |
1003 | sub find_active($) { |
967 | $cf::PLAYER{$_[0]} |
1004 | $cf::PLAYER{$_[0]} |
968 | and $cf::PLAYER{$_[0]}->active |
1005 | and $cf::PLAYER{$_[0]}->active |
… | |
… | |
981 | my $login = $_[0]; |
1018 | my $login = $_[0]; |
982 | |
1019 | |
983 | my $guard = cf::lock_acquire "user_find:$login"; |
1020 | my $guard = cf::lock_acquire "user_find:$login"; |
984 | |
1021 | |
985 | $cf::PLAYER{$_[0]} || do { |
1022 | $cf::PLAYER{$_[0]} || do { |
|
|
1023 | # rename old playerfiles to new ones |
|
|
1024 | #TODO: remove when no longer required |
|
|
1025 | aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst"; |
|
|
1026 | aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata"; |
|
|
1027 | aio_unlink +(playerdir $login) . "/$login.pl.pst"; |
|
|
1028 | aio_unlink +(playerdir $login) . "/$login.pl"; |
|
|
1029 | |
986 | my $pl = load_pl path $login |
1030 | my $pl = load_pl path $login |
987 | or return; |
1031 | or return; |
988 | $cf::PLAYER{$login} = $pl |
1032 | $cf::PLAYER{$login} = $pl |
989 | } |
1033 | } |
990 | } |
1034 | } |
… | |
… | |
1027 | |
1071 | |
1028 | =cut |
1072 | =cut |
1029 | |
1073 | |
1030 | sub quit_character { |
1074 | sub quit_character { |
1031 | my ($pl) = @_; |
1075 | my ($pl) = @_; |
|
|
1076 | |
|
|
1077 | my $name = $pl->ob->name; |
1032 | |
1078 | |
1033 | $pl->{deny_save} = 1; |
1079 | $pl->{deny_save} = 1; |
1034 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1080 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1035 | |
1081 | |
1036 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1082 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
… | |
… | |
1041 | my $path = playerdir $pl; |
1087 | my $path = playerdir $pl; |
1042 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1088 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1043 | aio_rename $path, $temp; |
1089 | aio_rename $path, $temp; |
1044 | delete $cf::PLAYER{$pl->ob->name}; |
1090 | delete $cf::PLAYER{$pl->ob->name}; |
1045 | $pl->destroy; |
1091 | $pl->destroy; |
|
|
1092 | |
|
|
1093 | my $prefix = qr<^~\Q$name\E/>; |
|
|
1094 | |
|
|
1095 | # nuke player maps |
|
|
1096 | $cf::MAP{$_}->nuke for grep /$prefix/, keys %cf::MAP; |
|
|
1097 | |
1046 | IO::AIO::aio_rmtree $temp; |
1098 | IO::AIO::aio_rmtree $temp; |
|
|
1099 | } |
|
|
1100 | |
|
|
1101 | =item $pl->kick |
|
|
1102 | |
|
|
1103 | Kicks a player out of the game. This destroys the connection. |
|
|
1104 | |
|
|
1105 | =cut |
|
|
1106 | |
|
|
1107 | sub kick { |
|
|
1108 | my ($pl, $kicker) = @_; |
|
|
1109 | |
|
|
1110 | $pl->invoke (cf::EVENT_PLAYER_KICK, $kicker); |
|
|
1111 | $pl->killer ("kicked"); |
|
|
1112 | $pl->ns->destroy; |
1047 | } |
1113 | } |
1048 | |
1114 | |
1049 | =item cf::player::list_logins |
1115 | =item cf::player::list_logins |
1050 | |
1116 | |
1051 | Returns am arrayref of all valid playernames in the system, can take a |
1117 | Returns am arrayref of all valid playernames in the system, can take a |
1052 | while and may block, so not sync_job-capable, ever. |
1118 | while and may block, so not sync_job-capable, ever. |
1053 | |
1119 | |
1054 | =cut |
1120 | =cut |
1055 | |
1121 | |
1056 | sub list_logins { |
1122 | sub list_logins { |
1057 | my $dirs = aio_readdir cf::localdir . "/" . cf::playerdir |
1123 | my $dirs = aio_readdir $PLAYERDIR |
1058 | or return []; |
1124 | or return []; |
1059 | |
1125 | |
1060 | my @logins; |
1126 | my @logins; |
1061 | |
1127 | |
1062 | for my $login (@$dirs) { |
1128 | for my $login (@$dirs) { |
… | |
… | |
1097 | } |
1163 | } |
1098 | |
1164 | |
1099 | \@paths |
1165 | \@paths |
1100 | } |
1166 | } |
1101 | |
1167 | |
1102 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1168 | =item $player->ext_reply ($msgid, %msg) |
1103 | |
1169 | |
1104 | Sends an ext reply to the player. |
1170 | Sends an ext reply to the player. |
1105 | |
1171 | |
1106 | =cut |
1172 | =cut |
1107 | |
1173 | |
1108 | sub ext_reply($$$%) { |
1174 | sub ext_reply($$%) { |
1109 | my ($self, $id, %msg) = @_; |
1175 | my ($self, $id, %msg) = @_; |
1110 | |
1176 | |
1111 | $msg{msgid} = $id; |
1177 | $msg{msgid} = $id; |
1112 | |
1178 | |
1113 | $self->send ("ext " . cf::to_json \%msg); |
1179 | $self->send ("ext " . cf::to_json \%msg); |
1114 | } |
1180 | } |
1115 | |
1181 | |
1116 | package cf; |
1182 | =item $player->ext_event ($type, %msg) |
|
|
1183 | |
|
|
1184 | Sends an ext event to the client. |
|
|
1185 | |
|
|
1186 | =cut |
|
|
1187 | |
|
|
1188 | sub ext_event($$%) { |
|
|
1189 | my ($self, $type, %msg) = @_; |
|
|
1190 | |
|
|
1191 | $self->ns->ext_event ($type, %msg); |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | =head3 cf::region |
|
|
1195 | |
|
|
1196 | =over 4 |
|
|
1197 | |
|
|
1198 | =cut |
|
|
1199 | |
|
|
1200 | package cf::region; |
|
|
1201 | |
|
|
1202 | =item cf::region::find_by_path $path |
|
|
1203 | |
|
|
1204 | Tries to decuce the probable region for a map knowing only its path. |
|
|
1205 | |
|
|
1206 | =cut |
|
|
1207 | |
|
|
1208 | sub find_by_path($) { |
|
|
1209 | my ($path) = @_; |
|
|
1210 | |
|
|
1211 | my ($match, $specificity); |
|
|
1212 | |
|
|
1213 | for my $region (list) { |
|
|
1214 | if ($region->match && $path =~ $region->match) { |
|
|
1215 | ($match, $specificity) = ($region, $region->specificity) |
|
|
1216 | if $region->specificity > $specificity; |
|
|
1217 | } |
|
|
1218 | } |
|
|
1219 | |
|
|
1220 | $match |
|
|
1221 | } |
1117 | |
1222 | |
1118 | =back |
1223 | =back |
1119 | |
|
|
1120 | |
1224 | |
1121 | =head3 cf::map |
1225 | =head3 cf::map |
1122 | |
1226 | |
1123 | =over 4 |
1227 | =over 4 |
1124 | |
1228 | |
… | |
… | |
1262 | |
1366 | |
1263 | # the original (read-only) location |
1367 | # the original (read-only) location |
1264 | sub load_path { |
1368 | sub load_path { |
1265 | my ($self) = @_; |
1369 | my ($self) = @_; |
1266 | |
1370 | |
1267 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1371 | "$MAPDIR/$self->{path}.map" |
1268 | } |
1372 | } |
1269 | |
1373 | |
1270 | # the temporary/swap location |
1374 | # the temporary/swap location |
1271 | sub save_path { |
1375 | sub save_path { |
1272 | my ($self) = @_; |
1376 | my ($self) = @_; |
1273 | |
1377 | |
1274 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1378 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1275 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1379 | "$TMPDIR/$path.map" |
1276 | } |
1380 | } |
1277 | |
1381 | |
1278 | # the unique path, undef == no special unique path |
1382 | # the unique path, undef == no special unique path |
1279 | sub uniq_path { |
1383 | sub uniq_path { |
1280 | my ($self) = @_; |
1384 | my ($self) = @_; |
1281 | |
1385 | |
1282 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1386 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1283 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $path |
1387 | "$UNIQUEDIR/$path" |
1284 | } |
1388 | } |
1285 | |
1389 | |
1286 | # and all this just because we cannot iterate over |
1390 | # and all this just because we cannot iterate over |
1287 | # all maps in C++... |
1391 | # all maps in C++... |
1288 | sub change_all_map_light { |
1392 | sub change_all_map_light { |
… | |
… | |
1355 | $self->load_header_orig |
1459 | $self->load_header_orig |
1356 | or return; |
1460 | or return; |
1357 | $self->prepare_orig; |
1461 | $self->prepare_orig; |
1358 | } |
1462 | } |
1359 | |
1463 | |
|
|
1464 | $self->default_region (cf::region::find_by_path $self->{path}) |
|
|
1465 | unless $self->default_region; |
|
|
1466 | |
1360 | 1 |
1467 | 1 |
1361 | } |
1468 | } |
1362 | |
1469 | |
1363 | sub find; |
1470 | sub find; |
1364 | sub find { |
1471 | sub find { |
… | |
… | |
1397 | my ($self) = @_; |
1504 | my ($self) = @_; |
1398 | |
1505 | |
1399 | local $self->{deny_reset} = 1; # loading can take a long time |
1506 | local $self->{deny_reset} = 1; # loading can take a long time |
1400 | |
1507 | |
1401 | my $path = $self->{path}; |
1508 | my $path = $self->{path}; |
|
|
1509 | |
|
|
1510 | { |
1402 | my $guard = cf::lock_acquire "map_load:$path"; |
1511 | my $guard = cf::lock_acquire "map_load:$path"; |
1403 | |
1512 | |
1404 | return if $self->in_memory != cf::MAP_SWAPPED; |
1513 | return if $self->in_memory != cf::MAP_SWAPPED; |
1405 | |
1514 | |
1406 | $self->in_memory (cf::MAP_LOADING); |
1515 | $self->in_memory (cf::MAP_LOADING); |
1407 | |
1516 | |
1408 | $self->alloc; |
1517 | $self->alloc; |
1409 | |
1518 | |
1410 | $self->pre_load; |
1519 | $self->pre_load; |
1411 | |
|
|
1412 | $self->_load_objects ($self->{load_path}, 1) |
|
|
1413 | or return; |
|
|
1414 | |
|
|
1415 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
|
|
1416 | if delete $self->{load_original}; |
|
|
1417 | |
|
|
1418 | if (my $uniq = $self->uniq_path) { |
|
|
1419 | utf8::encode $uniq; |
|
|
1420 | if (aio_open $uniq, O_RDONLY, 0) { |
|
|
1421 | $self->clear_unique_items; |
|
|
1422 | $self->_load_objects ($uniq, 0); |
|
|
1423 | } |
|
|
1424 | } |
|
|
1425 | |
|
|
1426 | Coro::cede; |
|
|
1427 | |
|
|
1428 | # now do the right thing for maps |
|
|
1429 | $self->link_multipart_objects; |
|
|
1430 | |
|
|
1431 | unless ($self->{deny_activate}) { |
|
|
1432 | $self->decay_objects; |
|
|
1433 | $self->fix_auto_apply; |
|
|
1434 | $self->update_buttons; |
|
|
1435 | Coro::cede; |
1520 | Coro::cede; |
1436 | $self->set_darkness_map; |
1521 | |
|
|
1522 | $self->_load_objects ($self->{load_path}, 1) |
|
|
1523 | or return; |
|
|
1524 | |
|
|
1525 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
|
|
1526 | if delete $self->{load_original}; |
|
|
1527 | |
|
|
1528 | if (my $uniq = $self->uniq_path) { |
|
|
1529 | utf8::encode $uniq; |
|
|
1530 | if (aio_open $uniq, O_RDONLY, 0) { |
|
|
1531 | $self->clear_unique_items; |
|
|
1532 | $self->_load_objects ($uniq, 0); |
|
|
1533 | } |
|
|
1534 | } |
|
|
1535 | |
|
|
1536 | Coro::cede; |
|
|
1537 | # now do the right thing for maps |
|
|
1538 | $self->link_multipart_objects; |
1437 | $self->difficulty ($self->estimate_difficulty) |
1539 | $self->difficulty ($self->estimate_difficulty) |
1438 | unless $self->difficulty; |
1540 | unless $self->difficulty; |
1439 | Coro::cede; |
1541 | Coro::cede; |
|
|
1542 | |
|
|
1543 | unless ($self->{deny_activate}) { |
|
|
1544 | $self->decay_objects; |
|
|
1545 | $self->fix_auto_apply; |
|
|
1546 | $self->update_buttons; |
|
|
1547 | Coro::cede; |
|
|
1548 | $self->set_darkness_map; |
|
|
1549 | Coro::cede; |
1440 | $self->activate; |
1550 | $self->activate; |
|
|
1551 | } |
|
|
1552 | |
|
|
1553 | $self->in_memory (cf::MAP_IN_MEMORY); |
1441 | } |
1554 | } |
1442 | |
1555 | |
1443 | $self->post_load; |
1556 | $self->post_load; |
1444 | |
|
|
1445 | $self->in_memory (cf::MAP_IN_MEMORY); |
|
|
1446 | } |
1557 | } |
1447 | |
1558 | |
1448 | sub customise_for { |
1559 | sub customise_for { |
1449 | my ($self, $ob) = @_; |
1560 | my ($self, $ob) = @_; |
1450 | |
1561 | |
… | |
… | |
1641 | instantiated unique items. May block. |
1752 | instantiated unique items. May block. |
1642 | |
1753 | |
1643 | =cut |
1754 | =cut |
1644 | |
1755 | |
1645 | sub unique_maps() { |
1756 | sub unique_maps() { |
1646 | my $files = aio_readdir cf::localdir . "/" . cf::uniquedir |
1757 | my $files = aio_readdir $UNIQUEDIR |
1647 | or return; |
1758 | or return; |
1648 | |
1759 | |
1649 | my @paths; |
1760 | my @paths; |
1650 | |
1761 | |
1651 | for (@$files) { |
1762 | for (@$files) { |
… | |
… | |
1907 | |
2018 | |
1908 | $rmp->{random_seed} ||= $exit->random_seed; |
2019 | $rmp->{random_seed} ||= $exit->random_seed; |
1909 | |
2020 | |
1910 | my $data = cf::to_json $rmp; |
2021 | my $data = cf::to_json $rmp; |
1911 | my $md5 = Digest::MD5::md5_hex $data; |
2022 | my $md5 = Digest::MD5::md5_hex $data; |
1912 | my $meta = "$cf::RANDOM_MAPS/$md5.meta"; |
2023 | my $meta = "$RANDOMDIR/$md5.meta"; |
1913 | |
2024 | |
1914 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2025 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
1915 | aio_write $fh, 0, (length $data), $data, 0; |
2026 | aio_write $fh, 0, (length $data), $data, 0; |
1916 | undef $fh; |
2027 | undef $fh; |
1917 | aio_rename "$meta~", $meta; |
2028 | aio_rename "$meta~", $meta; |
… | |
… | |
1926 | |
2037 | |
1927 | return unless $self->type == cf::PLAYER; |
2038 | return unless $self->type == cf::PLAYER; |
1928 | |
2039 | |
1929 | if ($exit->slaying eq "/!") { |
2040 | if ($exit->slaying eq "/!") { |
1930 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
2041 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
2042 | # the problem is that $exit might not survive long enough |
|
|
2043 | # so it needs to be done right now, right here |
1931 | cf::sync_job { prepare_random_map $exit }; |
2044 | cf::sync_job { prepare_random_map $exit }; |
1932 | } |
2045 | } |
1933 | |
2046 | |
1934 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
2047 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
1935 | my $hp = $exit->stats->hp; |
2048 | my $hp = $exit->stats->hp; |
… | |
… | |
1943 | $self->goto ($slaying, $hp, $sp); |
2056 | $self->goto ($slaying, $hp, $sp); |
1944 | |
2057 | |
1945 | 1; |
2058 | 1; |
1946 | }) { |
2059 | }) { |
1947 | $self->message ("Something went wrong deep within the crossfire server. " |
2060 | $self->message ("Something went wrong deep within the crossfire server. " |
1948 | . "I'll try to bring you back to the map you were before. " |
2061 | . "I'll try to bring you back to the map you were before. " |
1949 | . "Please report this to the dungeon master!", |
2062 | . "Please report this to the dungeon master!", |
1950 | cf::NDI_UNIQUE | cf::NDI_RED); |
2063 | cf::NDI_UNIQUE | cf::NDI_RED); |
1951 | |
2064 | |
1952 | warn "ERROR in enter_exit: $@"; |
2065 | warn "ERROR in enter_exit: $@"; |
1953 | $self->leave_link; |
2066 | $self->leave_link; |
1954 | } |
2067 | } |
1955 | })->prio (1); |
2068 | })->prio (1); |
… | |
… | |
1968 | |
2081 | |
1969 | sub cf::client::send_drawinfo { |
2082 | sub cf::client::send_drawinfo { |
1970 | my ($self, $text, $flags) = @_; |
2083 | my ($self, $text, $flags) = @_; |
1971 | |
2084 | |
1972 | utf8::encode $text; |
2085 | utf8::encode $text; |
1973 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
2086 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
1974 | } |
2087 | } |
1975 | |
2088 | |
|
|
2089 | =item $client->ext_event ($type, %msg) |
|
|
2090 | |
|
|
2091 | Sends an exti event to the client. |
|
|
2092 | |
|
|
2093 | =cut |
|
|
2094 | |
|
|
2095 | sub cf::client::ext_event($$%) { |
|
|
2096 | my ($self, $type, %msg) = @_; |
|
|
2097 | |
|
|
2098 | $msg{msgtype} = "event_$type"; |
|
|
2099 | $self->send_packet ("ext " . cf::to_json \%msg); |
|
|
2100 | } |
1976 | |
2101 | |
1977 | =item $success = $client->query ($flags, "text", \&cb) |
2102 | =item $success = $client->query ($flags, "text", \&cb) |
1978 | |
2103 | |
1979 | Queues a query to the client, calling the given callback with |
2104 | Queues a query to the client, calling the given callback with |
1980 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2105 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
… | |
… | |
2077 | |
2202 | |
2078 | # here we export the classes and methods available to script code |
2203 | # here we export the classes and methods available to script code |
2079 | |
2204 | |
2080 | =pod |
2205 | =pod |
2081 | |
2206 | |
2082 | The following fucntions and emthods are available within a safe environment: |
2207 | The following functions and methods are available within a safe environment: |
2083 | |
2208 | |
2084 | cf::object contr pay_amount pay_player map |
2209 | cf::object contr pay_amount pay_player map |
2085 | cf::object::player player |
2210 | cf::object::player player |
2086 | cf::player peaceful |
2211 | cf::player peaceful |
2087 | cf::map trigger |
2212 | cf::map trigger |
… | |
… | |
2172 | ############################################################################# |
2297 | ############################################################################# |
2173 | |
2298 | |
2174 | =head2 EXTENSION DATABASE SUPPORT |
2299 | =head2 EXTENSION DATABASE SUPPORT |
2175 | |
2300 | |
2176 | Crossfire maintains a very simple database for extension use. It can |
2301 | Crossfire maintains a very simple database for extension use. It can |
2177 | currently store anything that can be serialised using Storable, which |
2302 | currently store binary data only (use Compress::LZF::sfreeze_cr/sthaw to |
2178 | excludes objects. |
2303 | convert to/from binary). |
2179 | |
2304 | |
2180 | The parameter C<$family> should best start with the name of the extension |
2305 | The parameter C<$family> should best start with the name of the extension |
2181 | using it, it should be unique. |
2306 | using it, it should be unique. |
2182 | |
2307 | |
2183 | =over 4 |
2308 | =over 4 |
… | |
… | |
2206 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
2331 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
2207 | cf::cleanup "db_open(db): $!" if $!; |
2332 | cf::cleanup "db_open(db): $!" if $!; |
2208 | }; |
2333 | }; |
2209 | cf::cleanup "db_open(db): $@" if $@; |
2334 | cf::cleanup "db_open(db): $@" if $@; |
2210 | }; |
2335 | }; |
2211 | |
|
|
2212 | my $path = cf::localdir . "/database.pst"; |
|
|
2213 | if (stat $path) { |
|
|
2214 | cf::sync_job { |
|
|
2215 | my $pst = Storable::retrieve $path; |
|
|
2216 | |
|
|
2217 | cf::db_put (board => data => $pst->{board}); |
|
|
2218 | cf::db_put (guildrules => data => $pst->{guildrules}); |
|
|
2219 | cf::db_put (rent => balance => $pst->{rent}{balance}); |
|
|
2220 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2221 | |
|
|
2222 | unlink $path; |
|
|
2223 | }; |
|
|
2224 | } |
|
|
2225 | } |
2336 | } |
2226 | } |
2337 | } |
2227 | |
2338 | |
2228 | sub db_get($$) { |
2339 | sub db_get($$) { |
2229 | my $key = "$_[0]/$_[1]"; |
2340 | my $key = "$_[0]/$_[1]"; |
2230 | |
2341 | |
2231 | cf::sync_job { |
2342 | cf::sync_job { |
2232 | BDB::db_get $DB, undef, $key, my $data; |
2343 | BDB::db_get $DB, undef, $key, my $data; |
2233 | |
2344 | |
2234 | $! ? () |
2345 | $! ? () |
2235 | : Compress::LZF::sthaw $data |
2346 | : $data |
2236 | } |
2347 | } |
2237 | } |
2348 | } |
2238 | |
2349 | |
2239 | sub db_put($$$) { |
2350 | sub db_put($$$) { |
2240 | BDB::dbreq_pri 4; |
2351 | BDB::dbreq_pri 4; |
2241 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2352 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
2242 | } |
2353 | } |
|
|
2354 | |
|
|
2355 | =item cf::cache $id => [$paths...], $processversion => $process |
|
|
2356 | |
|
|
2357 | Generic caching function that returns the value of the resource $id, |
|
|
2358 | caching and regenerating as required. |
|
|
2359 | |
|
|
2360 | This function can block. |
|
|
2361 | |
|
|
2362 | =cut |
|
|
2363 | |
|
|
2364 | sub cache { |
|
|
2365 | my ($id, $src, $processversion, $process) = @_; |
|
|
2366 | |
|
|
2367 | my $meta = |
|
|
2368 | join "\x00", |
|
|
2369 | $processversion, |
|
|
2370 | map { |
|
|
2371 | aio_stat $_ |
|
|
2372 | and Carp::croak "$_: $!"; |
|
|
2373 | |
|
|
2374 | ($_, (stat _)[7,9]) |
|
|
2375 | } @$src; |
|
|
2376 | |
|
|
2377 | my $dbmeta = db_get cache => "$id/meta"; |
|
|
2378 | if ($dbmeta ne $meta) { |
|
|
2379 | # changed, we may need to process |
|
|
2380 | |
|
|
2381 | my @data; |
|
|
2382 | my $md5; |
|
|
2383 | |
|
|
2384 | for (0 .. $#$src) { |
|
|
2385 | 0 <= aio_load $src->[$_], $data[$_] |
|
|
2386 | or Carp::croak "$src->[$_]: $!"; |
|
|
2387 | } |
|
|
2388 | |
|
|
2389 | # if processing is expensive, check |
|
|
2390 | # checksum first |
|
|
2391 | if (1) { |
|
|
2392 | $md5 = |
|
|
2393 | join "\x00", |
|
|
2394 | $processversion, |
|
|
2395 | map { |
|
|
2396 | Coro::cede; |
|
|
2397 | ($src->[$_], Digest::MD5::md5_hex $data[$_]) |
|
|
2398 | } 0.. $#$src; |
|
|
2399 | |
|
|
2400 | |
|
|
2401 | my $dbmd5 = db_get cache => "$id/md5"; |
|
|
2402 | if ($dbmd5 eq $md5) { |
|
|
2403 | db_put cache => "$id/meta", $meta; |
|
|
2404 | |
|
|
2405 | return db_get cache => "$id/data"; |
|
|
2406 | } |
|
|
2407 | } |
|
|
2408 | |
|
|
2409 | my $t1 = Time::HiRes::time; |
|
|
2410 | my $data = $process->(\@data); |
|
|
2411 | my $t2 = Time::HiRes::time; |
|
|
2412 | |
|
|
2413 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
|
|
2414 | |
|
|
2415 | db_put cache => "$id/data", $data; |
|
|
2416 | db_put cache => "$id/md5" , $md5; |
|
|
2417 | db_put cache => "$id/meta", $meta; |
|
|
2418 | |
|
|
2419 | return $data; |
|
|
2420 | } |
|
|
2421 | |
|
|
2422 | db_get cache => "$id/data" |
|
|
2423 | } |
|
|
2424 | |
|
|
2425 | =item fork_call { }, $args |
|
|
2426 | |
|
|
2427 | Executes the given code block with the given arguments in a seperate |
|
|
2428 | process, returning the results. Everything must be serialisable with |
|
|
2429 | Coro::Storable. May, of course, block. Note that the executed sub may |
|
|
2430 | never block itself or use any form of Event handling. |
|
|
2431 | |
|
|
2432 | =cut |
|
|
2433 | |
|
|
2434 | sub fork_call(&@) { |
|
|
2435 | my ($cb, @args) = @_; |
|
|
2436 | |
|
|
2437 | # socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
|
|
2438 | # or die "socketpair: $!"; |
|
|
2439 | pipe my $fh1, my $fh2 |
|
|
2440 | or die "pipe: $!"; |
|
|
2441 | |
|
|
2442 | if (my $pid = fork) { |
|
|
2443 | close $fh2; |
|
|
2444 | |
|
|
2445 | my $res = (Coro::Handle::unblock $fh1)->readline (undef); |
|
|
2446 | $res = Coro::Storable::thaw $res; |
|
|
2447 | |
|
|
2448 | waitpid $pid, 0; # should not block anymore, we expect the child to simply behave |
|
|
2449 | |
|
|
2450 | die $$res unless "ARRAY" eq ref $res; |
|
|
2451 | |
|
|
2452 | return wantarray ? @$res : $res->[-1]; |
|
|
2453 | } else { |
|
|
2454 | reset_signals; |
|
|
2455 | local $SIG{__WARN__}; |
|
|
2456 | local $SIG{__DIE__}; |
|
|
2457 | eval { |
|
|
2458 | close $fh1; |
|
|
2459 | |
|
|
2460 | my @res = eval { $cb->(@args) }; |
|
|
2461 | syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); |
|
|
2462 | }; |
|
|
2463 | |
|
|
2464 | warn $@ if $@; |
|
|
2465 | _exit 0; |
|
|
2466 | } |
|
|
2467 | } |
|
|
2468 | |
|
|
2469 | |
2243 | |
2470 | |
2244 | ############################################################################# |
2471 | ############################################################################# |
2245 | # the server's init and main functions |
2472 | # the server's init and main functions |
2246 | |
2473 | |
2247 | sub init_resources { |
2474 | sub load_facedata($) { |
2248 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2475 | my ($path) = @_; |
|
|
2476 | |
|
|
2477 | warn "loading facedata from $path\n"; |
|
|
2478 | |
|
|
2479 | my $facedata; |
|
|
2480 | 0 < aio_load $path, $facedata |
|
|
2481 | or die "$path: $!"; |
|
|
2482 | |
|
|
2483 | $facedata = Coro::Storable::thaw $facedata; |
|
|
2484 | |
|
|
2485 | $facedata->{version} == 2 |
|
|
2486 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
|
|
2487 | |
|
|
2488 | { |
|
|
2489 | my $faces = $facedata->{faceinfo}; |
|
|
2490 | |
|
|
2491 | while (my ($face, $info) = each %$faces) { |
|
|
2492 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
2493 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
|
|
2494 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
|
|
2495 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
|
|
2496 | Coro::cede; |
|
|
2497 | } |
|
|
2498 | |
|
|
2499 | while (my ($face, $info) = each %$faces) { |
|
|
2500 | next unless $info->{smooth}; |
|
|
2501 | my $idx = cf::face::find $face |
|
|
2502 | or next; |
|
|
2503 | if (my $smooth = cf::face::find $info->{smooth}) { |
|
|
2504 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
|
|
2505 | } else { |
|
|
2506 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
|
|
2507 | } |
|
|
2508 | Coro::cede; |
|
|
2509 | } |
|
|
2510 | } |
|
|
2511 | |
|
|
2512 | { |
|
|
2513 | my $anims = $facedata->{animinfo}; |
|
|
2514 | |
|
|
2515 | while (my ($anim, $info) = each %$anims) { |
|
|
2516 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
|
|
2517 | Coro::cede; |
|
|
2518 | } |
|
|
2519 | |
|
|
2520 | cf::anim::invalidate_all; # d'oh |
|
|
2521 | } |
|
|
2522 | |
|
|
2523 | 1 |
|
|
2524 | } |
|
|
2525 | |
|
|
2526 | sub reload_regions { |
|
|
2527 | load_resource_file "$MAPDIR/regions" |
2249 | or die "unable to load regions file\n";#d# |
2528 | or die "unable to load regions file\n"; |
|
|
2529 | } |
|
|
2530 | |
|
|
2531 | sub reload_facedata { |
|
|
2532 | load_facedata "$DATADIR/facedata" |
|
|
2533 | or die "unable to load facedata\n"; |
|
|
2534 | } |
|
|
2535 | |
|
|
2536 | sub reload_archetypes { |
|
|
2537 | load_resource_file "$DATADIR/archetypes" |
|
|
2538 | or die "unable to load archetypes\n"; |
|
|
2539 | } |
|
|
2540 | |
|
|
2541 | sub reload_treasures { |
|
|
2542 | load_resource_file "$DATADIR/treasures" |
|
|
2543 | or die "unable to load treasurelists\n"; |
|
|
2544 | } |
|
|
2545 | |
|
|
2546 | sub reload_resources { |
|
|
2547 | warn "reloading resource files...\n"; |
|
|
2548 | |
|
|
2549 | reload_regions; |
|
|
2550 | reload_facedata; |
|
|
2551 | reload_archetypes; |
|
|
2552 | reload_treasures; |
|
|
2553 | |
|
|
2554 | warn "finished reloading resource files\n"; |
|
|
2555 | } |
|
|
2556 | |
|
|
2557 | sub init { |
|
|
2558 | reload_resources; |
2250 | } |
2559 | } |
2251 | |
2560 | |
2252 | sub cfg_load { |
2561 | sub cfg_load { |
2253 | open my $fh, "<:utf8", cf::confdir . "/config" |
2562 | open my $fh, "<:utf8", "$CONFDIR/config" |
2254 | or return; |
2563 | or return; |
2255 | |
2564 | |
2256 | local $/; |
2565 | local $/; |
2257 | *CFG = YAML::Syck::Load <$fh>; |
2566 | *CFG = YAML::Syck::Load <$fh>; |
2258 | |
2567 | |
… | |
… | |
2266 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2575 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2267 | and die "WARNING: m(un)lockall failed: $!\n"; |
2576 | and die "WARNING: m(un)lockall failed: $!\n"; |
2268 | }; |
2577 | }; |
2269 | warn $@ if $@; |
2578 | warn $@ if $@; |
2270 | } |
2579 | } |
2271 | } |
|
|
2272 | |
|
|
2273 | sub init { |
|
|
2274 | init_resources; |
|
|
2275 | } |
2580 | } |
2276 | |
2581 | |
2277 | sub main { |
2582 | sub main { |
2278 | # we must not ever block the main coroutine |
2583 | # we must not ever block the main coroutine |
2279 | local $Coro::idle = sub { |
2584 | local $Coro::idle = sub { |
… | |
… | |
2347 | |
2652 | |
2348 | warn Carp::longmess "post_cleanup backtrace" |
2653 | warn Carp::longmess "post_cleanup backtrace" |
2349 | if $make_core; |
2654 | if $make_core; |
2350 | } |
2655 | } |
2351 | |
2656 | |
2352 | sub reload() { |
2657 | sub do_reload_perl() { |
2353 | # can/must only be called in main |
2658 | # can/must only be called in main |
2354 | if ($Coro::current != $Coro::main) { |
2659 | if ($Coro::current != $Coro::main) { |
2355 | warn "can only reload from main coroutine"; |
2660 | warn "can only reload from main coroutine"; |
2356 | return; |
2661 | return; |
2357 | } |
2662 | } |
… | |
… | |
2382 | Coro::cede; |
2687 | Coro::cede; |
2383 | last unless IO::AIO::nreqs || BDB::nreqs; |
2688 | last unless IO::AIO::nreqs || BDB::nreqs; |
2384 | warn "iterate..."; |
2689 | warn "iterate..."; |
2385 | } |
2690 | } |
2386 | |
2691 | |
|
|
2692 | ++$RELOAD; |
|
|
2693 | |
2387 | warn "cancelling all extension coros"; |
2694 | warn "cancelling all extension coros"; |
2388 | $_->cancel for values %EXT_CORO; |
2695 | $_->cancel for values %EXT_CORO; |
2389 | %EXT_CORO = (); |
2696 | %EXT_CORO = (); |
2390 | |
2697 | |
2391 | warn "removing commands"; |
2698 | warn "removing commands"; |
… | |
… | |
2430 | Symbol::delete_package "safe::$_" |
2737 | Symbol::delete_package "safe::$_" |
2431 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2738 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2432 | |
2739 | |
2433 | warn "unloading cf.pm \"a bit\""; |
2740 | warn "unloading cf.pm \"a bit\""; |
2434 | delete $INC{"cf.pm"}; |
2741 | delete $INC{"cf.pm"}; |
|
|
2742 | delete $INC{"cf/pod.pm"}; |
2435 | |
2743 | |
2436 | # don't, removes xs symbols, too, |
2744 | # don't, removes xs symbols, too, |
2437 | # and global variables created in xs |
2745 | # and global variables created in xs |
2438 | #Symbol::delete_package __PACKAGE__; |
2746 | #Symbol::delete_package __PACKAGE__; |
2439 | |
2747 | |
… | |
… | |
2448 | |
2756 | |
2449 | warn "loading extensions"; |
2757 | warn "loading extensions"; |
2450 | cf::load_extensions; |
2758 | cf::load_extensions; |
2451 | |
2759 | |
2452 | warn "reattaching attachments to objects/players"; |
2760 | warn "reattaching attachments to objects/players"; |
2453 | _global_reattach; |
2761 | _global_reattach; # objects, sockets |
2454 | warn "reattaching attachments to maps"; |
2762 | warn "reattaching attachments to maps"; |
2455 | reattach $_ for values %MAP; |
2763 | reattach $_ for values %MAP; |
2456 | |
2764 | warn "reattaching attachments to players"; |
2457 | warn "loading reloadable resources"; |
2765 | reattach $_ for values %PLAYER; |
2458 | init_resources; |
|
|
2459 | |
2766 | |
2460 | warn "leaving sync_job"; |
2767 | warn "leaving sync_job"; |
2461 | |
2768 | |
2462 | 1 |
2769 | 1 |
2463 | } or do { |
2770 | } or do { |
… | |
… | |
2469 | warn "reloaded"; |
2776 | warn "reloaded"; |
2470 | }; |
2777 | }; |
2471 | |
2778 | |
2472 | our $RELOAD_WATCHER; # used only during reload |
2779 | our $RELOAD_WATCHER; # used only during reload |
2473 | |
2780 | |
|
|
2781 | sub reload_perl() { |
|
|
2782 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2783 | # coro crashes during coro_state_free->destroy here. |
|
|
2784 | |
|
|
2785 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2786 | reentrant => 0, |
|
|
2787 | after => 0, |
|
|
2788 | data => WF_AUTOCANCEL, |
|
|
2789 | cb => sub { |
|
|
2790 | do_reload_perl; |
|
|
2791 | undef $RELOAD_WATCHER; |
|
|
2792 | }, |
|
|
2793 | ); |
|
|
2794 | } |
|
|
2795 | |
2474 | register_command "reload" => sub { |
2796 | register_command "reload" => sub { |
2475 | my ($who, $arg) = @_; |
2797 | my ($who, $arg) = @_; |
2476 | |
2798 | |
2477 | if ($who->flag (FLAG_WIZ)) { |
2799 | if ($who->flag (FLAG_WIZ)) { |
2478 | $who->message ("reloading server."); |
2800 | $who->message ("reloading server."); |
2479 | |
2801 | async { reload_perl }; |
2480 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2481 | # coro crashes during coro_state_free->destroy here. |
|
|
2482 | |
|
|
2483 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2484 | reentrant => 0, |
|
|
2485 | after => 0, |
|
|
2486 | data => WF_AUTOCANCEL, |
|
|
2487 | cb => sub { |
|
|
2488 | reload; |
|
|
2489 | undef $RELOAD_WATCHER; |
|
|
2490 | }, |
|
|
2491 | ); |
|
|
2492 | } |
2802 | } |
2493 | }; |
2803 | }; |
2494 | |
2804 | |
2495 | unshift @INC, $LIBDIR; |
2805 | unshift @INC, $LIBDIR; |
2496 | |
2806 | |
2497 | my $bug_warning = 0; |
2807 | my $bug_warning = 0; |
|
|
2808 | |
|
|
2809 | our @WAIT_FOR_TICK; |
|
|
2810 | our @WAIT_FOR_TICK_BEGIN; |
|
|
2811 | |
|
|
2812 | sub wait_for_tick { |
|
|
2813 | return unless $TICK_WATCHER->is_active; |
|
|
2814 | return if $Coro::current == $Coro::main; |
|
|
2815 | |
|
|
2816 | my $signal = new Coro::Signal; |
|
|
2817 | push @WAIT_FOR_TICK, $signal; |
|
|
2818 | $signal->wait; |
|
|
2819 | } |
|
|
2820 | |
|
|
2821 | sub wait_for_tick_begin { |
|
|
2822 | return unless $TICK_WATCHER->is_active; |
|
|
2823 | return if $Coro::current == $Coro::main; |
|
|
2824 | |
|
|
2825 | my $signal = new Coro::Signal; |
|
|
2826 | push @WAIT_FOR_TICK_BEGIN, $signal; |
|
|
2827 | $signal->wait; |
|
|
2828 | } |
2498 | |
2829 | |
2499 | $TICK_WATCHER = Event->timer ( |
2830 | $TICK_WATCHER = Event->timer ( |
2500 | reentrant => 0, |
2831 | reentrant => 0, |
2501 | parked => 1, |
2832 | parked => 1, |
2502 | prio => 0, |
2833 | prio => 0, |
… | |
… | |
2510 | } |
2841 | } |
2511 | |
2842 | |
2512 | $NOW = Event::time; |
2843 | $NOW = Event::time; |
2513 | |
2844 | |
2514 | cf::server_tick; # one server iteration |
2845 | cf::server_tick; # one server iteration |
|
|
2846 | |
2515 | $RUNTIME += $TICK; |
2847 | $RUNTIME += $TICK; |
2516 | $NEXT_TICK += $TICK; |
2848 | $NEXT_TICK += $TICK; |
2517 | |
2849 | |
2518 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2850 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2519 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2851 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
2521 | write_runtime |
2853 | write_runtime |
2522 | or warn "ERROR: unable to write runtime file: $!"; |
2854 | or warn "ERROR: unable to write runtime file: $!"; |
2523 | }; |
2855 | }; |
2524 | } |
2856 | } |
2525 | |
2857 | |
2526 | |
|
|
2527 | $WAIT_FOR_TICK->broadcast; |
|
|
2528 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
|
|
2529 | |
|
|
2530 | # my $AFTER = Event::time; |
2858 | # my $AFTER = Event::time; |
2531 | # warn $AFTER - $NOW;#d# |
2859 | # warn $AFTER - $NOW;#d# |
2532 | |
2860 | |
2533 | # if we are delayed by four ticks or more, skip them all |
2861 | # if we are delayed by four ticks or more, skip them all |
2534 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2862 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2535 | |
2863 | |
2536 | $TICK_WATCHER->at ($NEXT_TICK); |
2864 | $TICK_WATCHER->at ($NEXT_TICK); |
2537 | $TICK_WATCHER->start; |
2865 | $TICK_WATCHER->start; |
|
|
2866 | |
|
|
2867 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
|
|
2868 | $sig->send; |
|
|
2869 | } |
|
|
2870 | while (my $sig = shift @WAIT_FOR_TICK) { |
|
|
2871 | $sig->send; |
|
|
2872 | } |
|
|
2873 | |
|
|
2874 | _post_tick; |
2538 | }, |
2875 | }, |
2539 | ); |
2876 | ); |
2540 | |
2877 | |
2541 | { |
2878 | { |
2542 | BDB::max_poll_time $TICK * 0.1; |
2879 | BDB::max_poll_time $TICK * 0.1; |
… | |
… | |
2570 | |
2907 | |
2571 | cf::sync_job { |
2908 | cf::sync_job { |
2572 | eval { |
2909 | eval { |
2573 | BDB::db_env_open |
2910 | BDB::db_env_open |
2574 | $DB_ENV, |
2911 | $DB_ENV, |
2575 | $BDB_ENV_DIR, |
2912 | $BDBDIR, |
2576 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2913 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2577 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2914 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2578 | 0666; |
2915 | 0666; |
2579 | |
2916 | |
2580 | cf::cleanup "db_env_open($BDB_ENV_DIR): $!" if $!; |
2917 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
2581 | |
2918 | |
2582 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2919 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2583 | $DB_ENV->set_lk_detect; |
2920 | $DB_ENV->set_lk_detect; |
2584 | }; |
2921 | }; |
2585 | |
2922 | |
… | |
… | |
2601 | prio => 6, |
2938 | prio => 6, |
2602 | cb => \&IO::AIO::poll_cb, |
2939 | cb => \&IO::AIO::poll_cb, |
2603 | ); |
2940 | ); |
2604 | } |
2941 | } |
2605 | |
2942 | |
|
|
2943 | my $_log_backtrace; |
|
|
2944 | |
|
|
2945 | sub _log_backtrace { |
|
|
2946 | my ($msg, @addr) = @_; |
|
|
2947 | |
|
|
2948 | $msg =~ s/\n//; |
|
|
2949 | |
|
|
2950 | # limit the # of concurrent backtraces |
|
|
2951 | if ($_log_backtrace < 2) { |
|
|
2952 | ++$_log_backtrace; |
|
|
2953 | async { |
|
|
2954 | my @bt = fork_call { |
|
|
2955 | @addr = map { sprintf "%x", $_ } @addr; |
|
|
2956 | my $self = (-f "/proc/$$/exe") ? "/proc/$$/exe" : $^X; |
|
|
2957 | open my $fh, "exec addr2line -C -f -i -e \Q$self\E @addr 2>&1 |" |
|
|
2958 | or die "addr2line: $!"; |
|
|
2959 | |
|
|
2960 | my @funcs; |
|
|
2961 | my @res = <$fh>; |
|
|
2962 | chomp for @res; |
|
|
2963 | while (@res) { |
|
|
2964 | my ($func, $line) = splice @res, 0, 2, (); |
|
|
2965 | push @funcs, "[$func] $line"; |
|
|
2966 | } |
|
|
2967 | |
|
|
2968 | @funcs |
|
|
2969 | }; |
|
|
2970 | |
|
|
2971 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
2972 | LOG llevInfo, "[ABT] $_\n" for @bt; |
|
|
2973 | --$_log_backtrace; |
|
|
2974 | }; |
|
|
2975 | } else { |
|
|
2976 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
2977 | LOG llevInfo, "[ABT] [suppressed]\n"; |
|
|
2978 | } |
|
|
2979 | } |
|
|
2980 | |
|
|
2981 | # load additional modules |
|
|
2982 | use cf::pod; |
|
|
2983 | |
2606 | END { cf::emergency_save } |
2984 | END { cf::emergency_save } |
2607 | |
2985 | |
2608 | 1 |
2986 | 1 |
2609 | |
2987 | |