1 | # |
1 | # |
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | # |
3 | # |
4 | # Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | # Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | # |
5 | # |
6 | # Deliantra is free software: you can redistribute it and/or modify |
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
7 | # it under the terms of the GNU General Public License as published by |
7 | # the terms of the Affero GNU General Public License as published by the |
8 | # the Free Software Foundation, either version 3 of the License, or |
8 | # Free Software Foundation, either version 3 of the License, or (at your |
9 | # (at your option) any later version. |
9 | # option) any later version. |
10 | # |
10 | # |
11 | # This program is distributed in the hope that it will be useful, |
11 | # This program is distributed in the hope that it will be useful, |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | # GNU General Public License for more details. |
14 | # GNU General Public License for more details. |
15 | # |
15 | # |
16 | # You should have received a copy of the GNU General Public License |
16 | # You should have received a copy of the Affero GNU General Public License |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | # and the GNU General Public License along with this program. If not, see |
|
|
18 | # <http://www.gnu.org/licenses/>. |
18 | # |
19 | # |
19 | # The authors can be reached via e-mail to <support@deliantra.net> |
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
20 | # |
21 | # |
21 | |
22 | |
22 | package cf; |
23 | package cf; |
23 | |
24 | |
24 | use 5.10.0; |
25 | use 5.10.0; |
25 | use utf8; |
26 | use utf8; |
26 | use strict "vars", "subs"; |
27 | use strict qw(vars subs); |
27 | |
28 | |
28 | use Symbol; |
29 | use Symbol; |
29 | use List::Util; |
30 | use List::Util; |
30 | use Socket; |
31 | use Socket; |
31 | use EV; |
32 | use EV; |
32 | use Opcode; |
33 | use Opcode; |
33 | use Safe; |
34 | use Safe; |
34 | use Safe::Hole; |
35 | use Safe::Hole; |
35 | use Storable (); |
36 | use Storable (); |
36 | |
37 | |
|
|
38 | use Guard (); |
37 | use Coro (); |
39 | use Coro (); |
38 | use Coro::State; |
40 | use Coro::State; |
39 | use Coro::Handle; |
41 | use Coro::Handle; |
40 | use Coro::EV; |
42 | use Coro::EV; |
41 | use Coro::AnyEvent; |
43 | use Coro::AnyEvent; |
42 | use Coro::Timer; |
44 | use Coro::Timer; |
43 | use Coro::Signal; |
45 | use Coro::Signal; |
44 | use Coro::Semaphore; |
46 | use Coro::Semaphore; |
|
|
47 | use Coro::SemaphoreSet; |
45 | use Coro::AnyEvent; |
48 | use Coro::AnyEvent; |
46 | use Coro::AIO; |
49 | use Coro::AIO; |
47 | use Coro::BDB 1.6; |
50 | use Coro::BDB 1.6; |
48 | use Coro::Storable; |
51 | use Coro::Storable; |
49 | use Coro::Util (); |
52 | use Coro::Util (); |
… | |
… | |
51 | use JSON::XS 2.01 (); |
54 | use JSON::XS 2.01 (); |
52 | use BDB (); |
55 | use BDB (); |
53 | use Data::Dumper; |
56 | use Data::Dumper; |
54 | use Digest::MD5; |
57 | use Digest::MD5; |
55 | use Fcntl; |
58 | use Fcntl; |
56 | use YAML (); |
59 | use YAML::XS (); |
57 | use IO::AIO (); |
60 | use IO::AIO (); |
58 | use Time::HiRes; |
61 | use Time::HiRes; |
59 | use Compress::LZF; |
62 | use Compress::LZF; |
60 | use Digest::MD5 (); |
63 | use Digest::MD5 (); |
61 | |
64 | |
… | |
… | |
69 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
72 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
70 | |
73 | |
71 | # make sure c-lzf reinitialises itself |
74 | # make sure c-lzf reinitialises itself |
72 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
75 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
73 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
76 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
|
|
77 | |
|
|
78 | # strictly for debugging |
|
|
79 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
74 | |
80 | |
75 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
81 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
76 | |
82 | |
77 | our %COMMAND = (); |
83 | our %COMMAND = (); |
78 | our %COMMAND_TIME = (); |
84 | our %COMMAND_TIME = (); |
… | |
… | |
83 | our %EXT_CORO = (); # coroutines bound to extensions |
89 | our %EXT_CORO = (); # coroutines bound to extensions |
84 | our %EXT_MAP = (); # pluggable maps |
90 | our %EXT_MAP = (); # pluggable maps |
85 | |
91 | |
86 | our $RELOAD; # number of reloads so far, non-zero while in reload |
92 | our $RELOAD; # number of reloads so far, non-zero while in reload |
87 | our @EVENT; |
93 | our @EVENT; |
|
|
94 | our @REFLECT; # set by XS |
|
|
95 | our %REFLECT; # set by us |
88 | |
96 | |
89 | our $CONFDIR = confdir; |
97 | our $CONFDIR = confdir; |
90 | our $DATADIR = datadir; |
98 | our $DATADIR = datadir; |
91 | our $LIBDIR = "$DATADIR/ext"; |
99 | our $LIBDIR = "$DATADIR/ext"; |
92 | our $PODDIR = "$DATADIR/pod"; |
100 | our $PODDIR = "$DATADIR/pod"; |
… | |
… | |
103 | our %RESOURCE; |
111 | our %RESOURCE; |
104 | |
112 | |
105 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
113 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
106 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
114 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
107 | our $NEXT_TICK; |
115 | our $NEXT_TICK; |
108 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
116 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
109 | |
117 | |
110 | our $BDB_DEADLOCK_WATCHER; |
118 | our $BDB_DEADLOCK_WATCHER; |
111 | our $BDB_CHECKPOINT_WATCHER; |
119 | our $BDB_CHECKPOINT_WATCHER; |
112 | our $BDB_TRICKLE_WATCHER; |
120 | our $BDB_TRICKLE_WATCHER; |
113 | our $DB_ENV; |
121 | our $DB_ENV; |
114 | |
122 | |
|
|
123 | our @EXTRA_MODULES = qw(pod match mapscript); |
|
|
124 | |
115 | our %CFG; |
125 | our %CFG; |
116 | |
126 | |
117 | our $UPTIME; $UPTIME ||= time; |
127 | our $UPTIME; $UPTIME ||= time; |
118 | our $RUNTIME; |
128 | our $RUNTIME; |
119 | our $NOW; |
129 | our $NOW; |
… | |
… | |
130 | our $JITTER; # average jitter |
140 | our $JITTER; # average jitter |
131 | our $TICK_START; # for load detecting purposes |
141 | our $TICK_START; # for load detecting purposes |
132 | |
142 | |
133 | our @POST_INIT; |
143 | our @POST_INIT; |
134 | |
144 | |
135 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
145 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
|
|
146 | our $REALLY_UNLOOP; # never set to true, please :) |
136 | |
147 | |
137 | binmode STDOUT; |
148 | binmode STDOUT; |
138 | binmode STDERR; |
149 | binmode STDERR; |
139 | |
150 | |
140 | # read virtual server time, if available |
151 | # read virtual server time, if available |
… | |
… | |
142 | open my $fh, "<", $RUNTIMEFILE |
153 | open my $fh, "<", $RUNTIMEFILE |
143 | or die "unable to read $RUNTIMEFILE file: $!"; |
154 | or die "unable to read $RUNTIMEFILE file: $!"; |
144 | $RUNTIME = <$fh> + 0.; |
155 | $RUNTIME = <$fh> + 0.; |
145 | } |
156 | } |
146 | |
157 | |
|
|
158 | eval "sub TICK() { $TICK } 1" or die; |
|
|
159 | |
147 | mkdir $_ |
160 | mkdir $_ |
148 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
161 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
149 | |
162 | |
150 | our $EMERGENCY_POSITION; |
163 | our $EMERGENCY_POSITION; |
151 | |
164 | |
152 | sub cf::map::normalise; |
165 | sub cf::map::normalise; |
|
|
166 | |
|
|
167 | ############################################################################# |
|
|
168 | |
|
|
169 | %REFLECT = (); |
|
|
170 | for (@REFLECT) { |
|
|
171 | my $reflect = JSON::XS::decode_json $_; |
|
|
172 | $REFLECT{$reflect->{class}} = $reflect; |
|
|
173 | } |
|
|
174 | |
|
|
175 | # this is decidedly evil |
|
|
176 | $REFLECT{object}{flags} = { map +($_ => undef), grep $_, map /^FLAG_([A-Z0-9_]+)$/ && lc $1, keys %{"cf::"} }; |
153 | |
177 | |
154 | ############################################################################# |
178 | ############################################################################# |
155 | |
179 | |
156 | =head2 GLOBAL VARIABLES |
180 | =head2 GLOBAL VARIABLES |
157 | |
181 | |
… | |
… | |
208 | |
232 | |
209 | This array contains the results of the last C<invoke ()> call. When |
233 | This array contains the results of the last C<invoke ()> call. When |
210 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
234 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
211 | that call. |
235 | that call. |
212 | |
236 | |
|
|
237 | =item %cf::REFLECT |
|
|
238 | |
|
|
239 | Contains, for each (C++) class name, a hash reference with information |
|
|
240 | about object members (methods, scalars, arrays and flags) and other |
|
|
241 | metadata, which is useful for introspection. |
|
|
242 | |
213 | =back |
243 | =back |
214 | |
244 | |
215 | =cut |
245 | =cut |
216 | |
246 | |
217 | BEGIN { |
247 | $Coro::State::WARNHOOK = sub { |
218 | *CORE::GLOBAL::warn = sub { |
|
|
219 | my $msg = join "", @_; |
248 | my $msg = join "", @_; |
220 | |
249 | |
221 | $msg .= "\n" |
250 | $msg .= "\n" |
222 | unless $msg =~ /\n$/; |
251 | unless $msg =~ /\n$/; |
223 | |
252 | |
224 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
253 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
225 | |
254 | |
226 | LOG llevError, $msg; |
255 | LOG llevError, $msg; |
227 | }; |
256 | }; |
228 | } |
|
|
229 | |
257 | |
230 | $Coro::State::DIEHOOK = sub { |
258 | $Coro::State::DIEHOOK = sub { |
231 | return unless $^S eq 0; # "eq", not "==" |
259 | return unless $^S eq 0; # "eq", not "==" |
|
|
260 | |
|
|
261 | warn Carp::longmess $_[0]; |
232 | |
262 | |
233 | if ($Coro::current == $Coro::main) {#d# |
263 | if ($Coro::current == $Coro::main) {#d# |
234 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
264 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
235 | return;#d# |
265 | return;#d# |
236 | }#d# |
266 | }#d# |
237 | |
267 | |
238 | # kill coroutine otherwise |
268 | # kill coroutine otherwise |
239 | warn Carp::longmess $_[0]; |
|
|
240 | Coro::terminate |
269 | Coro::terminate |
241 | }; |
270 | }; |
242 | |
|
|
243 | $SIG{__DIE__} = sub { }; #d#? |
|
|
244 | |
271 | |
245 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
272 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
246 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
273 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
247 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
274 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
248 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
275 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
330 | Wait until the given lock is available. See cf::lock_acquire. |
357 | Wait until the given lock is available. See cf::lock_acquire. |
331 | |
358 | |
332 | =item my $lock = cf::lock_acquire $string |
359 | =item my $lock = cf::lock_acquire $string |
333 | |
360 | |
334 | Wait until the given lock is available and then acquires it and returns |
361 | Wait until the given lock is available and then acquires it and returns |
335 | a Coro::guard object. If the guard object gets destroyed (goes out of scope, |
362 | a L<Guard> object. If the guard object gets destroyed (goes out of scope, |
336 | for example when the coroutine gets canceled), the lock is automatically |
363 | for example when the coroutine gets canceled), the lock is automatically |
337 | returned. |
364 | returned. |
338 | |
365 | |
339 | Locks are *not* recursive, locking from the same coro twice results in a |
366 | Locks are *not* recursive, locking from the same coro twice results in a |
340 | deadlocked coro. |
367 | deadlocked coro. |
… | |
… | |
346 | |
373 | |
347 | Return true if the lock is currently active, i.e. somebody has locked it. |
374 | Return true if the lock is currently active, i.e. somebody has locked it. |
348 | |
375 | |
349 | =cut |
376 | =cut |
350 | |
377 | |
351 | our %LOCK; |
378 | our $LOCKS = new Coro::SemaphoreSet; |
352 | our %LOCKER;#d# |
|
|
353 | |
379 | |
354 | sub lock_wait($) { |
380 | sub lock_wait($) { |
355 | my ($key) = @_; |
381 | $LOCKS->wait ($_[0]); |
356 | |
|
|
357 | if ($LOCKER{$key} == $Coro::current) {#d# |
|
|
358 | Carp::cluck "lock_wait($key) for already-acquired lock";#d# |
|
|
359 | return;#d# |
|
|
360 | }#d# |
|
|
361 | |
|
|
362 | # wait for lock, if any |
|
|
363 | while ($LOCK{$key}) { |
|
|
364 | #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>"; |
|
|
365 | push @{ $LOCK{$key} }, $Coro::current; |
|
|
366 | Coro::schedule; |
|
|
367 | } |
|
|
368 | } |
382 | } |
369 | |
383 | |
370 | sub lock_acquire($) { |
384 | sub lock_acquire($) { |
371 | my ($key) = @_; |
385 | $LOCKS->guard ($_[0]) |
372 | |
|
|
373 | # wait, to be sure we are not locked |
|
|
374 | lock_wait $key; |
|
|
375 | |
|
|
376 | $LOCK{$key} = []; |
|
|
377 | $LOCKER{$key} = $Coro::current;#d# |
|
|
378 | |
|
|
379 | Coro::guard { |
|
|
380 | delete $LOCKER{$key};#d# |
|
|
381 | # wake up all waiters, to be on the safe side |
|
|
382 | $_->ready for @{ delete $LOCK{$key} }; |
|
|
383 | } |
|
|
384 | } |
386 | } |
385 | |
387 | |
386 | sub lock_active($) { |
388 | sub lock_active($) { |
387 | my ($key) = @_; |
389 | $LOCKS->count ($_[0]) < 1 |
388 | |
|
|
389 | ! ! $LOCK{$key} |
|
|
390 | } |
390 | } |
391 | |
391 | |
392 | sub freeze_mainloop { |
392 | sub freeze_mainloop { |
393 | tick_inhibit_inc; |
393 | tick_inhibit_inc; |
394 | |
394 | |
395 | Coro::guard \&tick_inhibit_dec; |
395 | &Guard::guard (\&tick_inhibit_dec); |
396 | } |
396 | } |
397 | |
397 | |
398 | =item cf::periodic $interval, $cb |
398 | =item cf::periodic $interval, $cb |
399 | |
399 | |
400 | Like EV::periodic, but randomly selects a starting point so that the actions |
400 | Like EV::periodic, but randomly selects a starting point so that the actions |
… | |
… | |
1172 | $decname, length $$rdata, scalar @$objs; |
1172 | $decname, length $$rdata, scalar @$objs; |
1173 | |
1173 | |
1174 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1174 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1175 | aio_chmod $fh, SAVE_MODE; |
1175 | aio_chmod $fh, SAVE_MODE; |
1176 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1176 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1177 | aio_fsync $fh if $cf::USE_FSYNC; |
1177 | if ($cf::USE_FSYNC) { |
|
|
1178 | aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER; |
|
|
1179 | aio_fsync $fh; |
|
|
1180 | } |
1178 | aio_close $fh; |
1181 | aio_close $fh; |
1179 | |
1182 | |
1180 | if (@$objs) { |
1183 | if (@$objs) { |
1181 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1184 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1182 | aio_chmod $fh, SAVE_MODE; |
1185 | aio_chmod $fh, SAVE_MODE; |
1183 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1186 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1184 | aio_write $fh, 0, (length $data), $data, 0; |
1187 | aio_write $fh, 0, (length $data), $data, 0; |
1185 | aio_fsync $fh if $cf::USE_FSYNC; |
1188 | if ($cf::USE_FSYNC) { |
|
|
1189 | aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER; |
|
|
1190 | aio_fsync $fh; |
|
|
1191 | } |
1186 | aio_close $fh; |
1192 | aio_close $fh; |
1187 | aio_rename "$filename.pst~", "$filename.pst"; |
1193 | aio_rename "$filename.pst~", "$filename.pst"; |
1188 | } |
1194 | } |
1189 | } else { |
1195 | } else { |
1190 | aio_unlink "$filename.pst"; |
1196 | aio_unlink "$filename.pst"; |
… | |
… | |
1193 | aio_rename "$filename~", $filename; |
1199 | aio_rename "$filename~", $filename; |
1194 | |
1200 | |
1195 | $filename =~ s%/[^/]+$%%; |
1201 | $filename =~ s%/[^/]+$%%; |
1196 | aio_pathsync $filename if $cf::USE_FSYNC; |
1202 | aio_pathsync $filename if $cf::USE_FSYNC; |
1197 | } else { |
1203 | } else { |
1198 | warn "FATAL: $filename~: $!\n"; |
1204 | warn "unable to save objects: $filename~: $!\n"; |
1199 | } |
1205 | } |
1200 | } else { |
1206 | } else { |
1201 | aio_unlink $filename; |
1207 | aio_unlink $filename; |
1202 | aio_unlink "$filename.pst"; |
1208 | aio_unlink "$filename.pst"; |
1203 | } |
1209 | } |
… | |
… | |
1341 | |
1347 | |
1342 | # "readahead" all extensions |
1348 | # "readahead" all extensions |
1343 | sub cache_extensions { |
1349 | sub cache_extensions { |
1344 | my $grp = IO::AIO::aio_group; |
1350 | my $grp = IO::AIO::aio_group; |
1345 | |
1351 | |
1346 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
1352 | add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub { |
1347 | for (grep /\.ext$/, @{$_[0]}) { |
1353 | for (grep /\.ext$/, @{$_[0]}) { |
1348 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1354 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1349 | } |
1355 | } |
1350 | }; |
1356 | }; |
1351 | |
1357 | |
… | |
… | |
1887 | |
1893 | |
1888 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1894 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1889 | "$UNIQUEDIR/$path" |
1895 | "$UNIQUEDIR/$path" |
1890 | } |
1896 | } |
1891 | |
1897 | |
1892 | # and all this just because we cannot iterate over |
|
|
1893 | # all maps in C++... |
|
|
1894 | sub change_all_map_light { |
|
|
1895 | my ($change) = @_; |
|
|
1896 | |
|
|
1897 | $_->change_map_light ($change) |
|
|
1898 | for grep $_->outdoor, values %cf::MAP; |
|
|
1899 | } |
|
|
1900 | |
|
|
1901 | sub decay_objects { |
1898 | sub decay_objects { |
1902 | my ($self) = @_; |
1899 | my ($self) = @_; |
1903 | |
1900 | |
1904 | return if $self->{deny_reset}; |
1901 | return if $self->{deny_reset}; |
1905 | |
1902 | |
… | |
… | |
1987 | sub find { |
1984 | sub find { |
1988 | my ($path, $origin) = @_; |
1985 | my ($path, $origin) = @_; |
1989 | |
1986 | |
1990 | $path = normalise $path, $origin && $origin->path; |
1987 | $path = normalise $path, $origin && $origin->path; |
1991 | |
1988 | |
1992 | cf::lock_wait "map_data:$path";#d#remove |
1989 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
1993 | cf::lock_wait "map_find:$path"; |
1990 | my $guard2 = cf::lock_acquire "map_find:$path"; |
1994 | |
1991 | |
1995 | $cf::MAP{$path} || do { |
1992 | $cf::MAP{$path} || do { |
1996 | my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it |
|
|
1997 | my $guard2 = cf::lock_acquire "map_find:$path"; |
|
|
1998 | |
|
|
1999 | my $map = new_from_path cf::map $path |
1993 | my $map = new_from_path cf::map $path |
2000 | or return; |
1994 | or return; |
2001 | |
1995 | |
2002 | $map->{last_save} = $cf::RUNTIME; |
1996 | $map->{last_save} = $cf::RUNTIME; |
2003 | |
1997 | |
… | |
… | |
2071 | unless ($self->{deny_activate}) { |
2065 | unless ($self->{deny_activate}) { |
2072 | $self->decay_objects; |
2066 | $self->decay_objects; |
2073 | $self->fix_auto_apply; |
2067 | $self->fix_auto_apply; |
2074 | $self->update_buttons; |
2068 | $self->update_buttons; |
2075 | cf::cede_to_tick; |
2069 | cf::cede_to_tick; |
2076 | $self->set_darkness_map; |
|
|
2077 | cf::cede_to_tick; |
|
|
2078 | $self->activate; |
2070 | $self->activate; |
2079 | } |
2071 | } |
2080 | |
2072 | |
2081 | $self->{last_save} = $cf::RUNTIME; |
2073 | $self->{last_save} = $cf::RUNTIME; |
2082 | $self->last_access ($cf::RUNTIME); |
2074 | $self->last_access ($cf::RUNTIME); |
… | |
… | |
2248 | |
2240 | |
2249 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2241 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2250 | |
2242 | |
2251 | return if $self->players; |
2243 | return if $self->players; |
2252 | |
2244 | |
2253 | warn "resetting map ", $self->path; |
2245 | warn "resetting map ", $self->path, "\n"; |
2254 | |
2246 | |
2255 | $self->in_memory (cf::MAP_SWAPPED); |
2247 | $self->in_memory (cf::MAP_SWAPPED); |
2256 | |
2248 | |
2257 | # need to save uniques path |
2249 | # need to save uniques path |
2258 | unless ($self->{deny_save}) { |
2250 | unless ($self->{deny_save}) { |
… | |
… | |
2423 | |
2415 | |
2424 | our $SAY_CHANNEL = { |
2416 | our $SAY_CHANNEL = { |
2425 | id => "say", |
2417 | id => "say", |
2426 | title => "Map", |
2418 | title => "Map", |
2427 | reply => "say ", |
2419 | reply => "say ", |
2428 | tooltip => "Things said to and replied from npcs near you and other players on the same map only.", |
2420 | tooltip => "Things said to and replied from NPCs near you and other players on the same map only.", |
2429 | }; |
2421 | }; |
2430 | |
2422 | |
2431 | our $CHAT_CHANNEL = { |
2423 | our $CHAT_CHANNEL = { |
2432 | id => "chat", |
2424 | id => "chat", |
2433 | title => "Chat", |
2425 | title => "Chat", |
… | |
… | |
2559 | |
2551 | |
2560 | $map->load; |
2552 | $map->load; |
2561 | $map->load_neighbours; |
2553 | $map->load_neighbours; |
2562 | |
2554 | |
2563 | return unless $self->contr->active; |
2555 | return unless $self->contr->active; |
2564 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
|
|
2565 | $self->activate_recursive; |
|
|
2566 | |
2556 | |
2567 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2557 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2568 | $self->enter_map ($map, $x, $y); |
2558 | $self->enter_map ($map, $x, $y); |
|
|
2559 | |
|
|
2560 | # only activate afterwards, to support waiting in hooks |
|
|
2561 | $self->activate_recursive; |
2569 | } |
2562 | } |
2570 | |
2563 | |
2571 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2564 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2572 | |
2565 | |
2573 | Moves the player to the given map-path and coordinates by first freezing |
2566 | Moves the player to the given map-path and coordinates by first freezing |
… | |
… | |
2808 | id => "infobox", |
2801 | id => "infobox", |
2809 | title => "Body Parts", |
2802 | title => "Body Parts", |
2810 | reply => undef, |
2803 | reply => undef, |
2811 | tooltip => "Shows which body parts you posess and are available", |
2804 | tooltip => "Shows which body parts you posess and are available", |
2812 | }, |
2805 | }, |
|
|
2806 | "c/statistics" => { |
|
|
2807 | id => "infobox", |
|
|
2808 | title => "Statistics", |
|
|
2809 | reply => undef, |
|
|
2810 | tooltip => "Shows your primary statistics", |
|
|
2811 | }, |
2813 | "c/skills" => { |
2812 | "c/skills" => { |
2814 | id => "infobox", |
2813 | id => "infobox", |
2815 | title => "Skills", |
2814 | title => "Skills", |
2816 | reply => undef, |
2815 | reply => undef, |
2817 | tooltip => "Shows your experience per skill and item power", |
2816 | tooltip => "Shows your experience per skill and item power", |
|
|
2817 | }, |
|
|
2818 | "c/shopitems" => { |
|
|
2819 | id => "infobox", |
|
|
2820 | title => "Shop Items", |
|
|
2821 | reply => undef, |
|
|
2822 | tooltip => "Shows the items currently for sale in this shop", |
|
|
2823 | }, |
|
|
2824 | "c/resistances" => { |
|
|
2825 | id => "infobox", |
|
|
2826 | title => "Resistances", |
|
|
2827 | reply => undef, |
|
|
2828 | tooltip => "Shows your resistances", |
|
|
2829 | }, |
|
|
2830 | "c/pets" => { |
|
|
2831 | id => "infobox", |
|
|
2832 | title => "Pets", |
|
|
2833 | reply => undef, |
|
|
2834 | tooltip => "Shows information abotu your pets/a specific pet", |
|
|
2835 | }, |
|
|
2836 | "c/perceiveself" => { |
|
|
2837 | id => "infobox", |
|
|
2838 | title => "Perceive Self", |
|
|
2839 | reply => undef, |
|
|
2840 | tooltip => "You gained detailed knowledge about yourself", |
2818 | }, |
2841 | }, |
2819 | "c/uptime" => { |
2842 | "c/uptime" => { |
2820 | id => "infobox", |
2843 | id => "infobox", |
2821 | title => "Uptime", |
2844 | title => "Uptime", |
2822 | reply => undef, |
2845 | reply => undef, |
… | |
… | |
2832 | id => "party", |
2855 | id => "party", |
2833 | title => "Party", |
2856 | title => "Party", |
2834 | reply => "gsay ", |
2857 | reply => "gsay ", |
2835 | tooltip => "Messages and chat related to your party", |
2858 | tooltip => "Messages and chat related to your party", |
2836 | }, |
2859 | }, |
|
|
2860 | "c/death" => { |
|
|
2861 | id => "death", |
|
|
2862 | title => "Death", |
|
|
2863 | reply => undef, |
|
|
2864 | tooltip => "Reason for and more info about your most recent death", |
|
|
2865 | }, |
|
|
2866 | "c/say" => $SAY_CHANNEL, |
|
|
2867 | "c/chat" => $CHAT_CHANNEL, |
2837 | ); |
2868 | ); |
2838 | |
2869 | |
2839 | sub cf::client::send_msg { |
2870 | sub cf::client::send_msg { |
2840 | my ($self, $channel, $msg, $color, @extra) = @_; |
2871 | my ($self, $channel, $msg, $color, @extra) = @_; |
2841 | |
2872 | |
… | |
… | |
2846 | |
2877 | |
2847 | # check predefined channels, for the benefit of C |
2878 | # check predefined channels, for the benefit of C |
2848 | if ($CHANNEL{$channel}) { |
2879 | if ($CHANNEL{$channel}) { |
2849 | $channel = $CHANNEL{$channel}; |
2880 | $channel = $CHANNEL{$channel}; |
2850 | |
2881 | |
2851 | $self->ext_msg (channel_info => $channel) |
2882 | $self->ext_msg (channel_info => $channel); |
2852 | if $self->can_msg; |
|
|
2853 | |
|
|
2854 | $channel = $channel->{id}; |
2883 | $channel = $channel->{id}; |
2855 | |
2884 | |
2856 | } elsif (ref $channel) { |
2885 | } elsif (ref $channel) { |
2857 | # send meta info to client, if not yet sent |
2886 | # send meta info to client, if not yet sent |
2858 | unless (exists $self->{channel}{$channel->{id}}) { |
2887 | unless (exists $self->{channel}{$channel->{id}}) { |
2859 | $self->{channel}{$channel->{id}} = $channel; |
2888 | $self->{channel}{$channel->{id}} = $channel; |
2860 | $self->ext_msg (channel_info => $channel) |
2889 | $self->ext_msg (channel_info => $channel); |
2861 | if $self->can_msg; |
|
|
2862 | } |
2890 | } |
2863 | |
2891 | |
2864 | $channel = $channel->{id}; |
2892 | $channel = $channel->{id}; |
2865 | } |
2893 | } |
2866 | |
2894 | |
2867 | return unless @extra || length $msg; |
2895 | return unless @extra || length $msg; |
2868 | |
2896 | |
2869 | if ($self->can_msg) { |
|
|
2870 | # default colour, mask it out |
2897 | # default colour, mask it out |
2871 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2898 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2872 | if $color & cf::NDI_DEF; |
2899 | if $color & cf::NDI_DEF; |
2873 | |
2900 | |
2874 | my $pkt = "msg " |
2901 | my $pkt = "msg " |
2875 | . $self->{json_coder}->encode ( |
2902 | . $self->{json_coder}->encode ( |
2876 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2903 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2877 | ); |
2904 | ); |
2878 | |
2905 | |
2879 | # try lzf for large packets |
2906 | # try lzf for large packets |
2880 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2907 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2881 | if 1024 <= length $pkt and $self->{can_lzf}; |
2908 | if 1024 <= length $pkt and $self->{can_lzf}; |
2882 | |
2909 | |
2883 | # split very large packets |
2910 | # split very large packets |
2884 | if (8192 < length $pkt and $self->{can_lzf}) { |
2911 | if (8192 < length $pkt and $self->{can_lzf}) { |
2885 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2912 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2886 | $pkt = "frag"; |
2913 | $pkt = "frag"; |
2887 | } |
2914 | } |
2888 | |
2915 | |
2889 | $self->send_packet ($pkt); |
2916 | $self->send_packet ($pkt); |
2890 | } else { |
|
|
2891 | if ($color >= 0) { |
|
|
2892 | # replace some tags by gcfclient-compatible ones |
|
|
2893 | for ($msg) { |
|
|
2894 | 1 while |
|
|
2895 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
|
|
2896 | || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/ |
|
|
2897 | || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/ |
|
|
2898 | || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ |
|
|
2899 | || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/; |
|
|
2900 | } |
|
|
2901 | |
|
|
2902 | $color &= cf::NDI_COLOR_MASK; |
|
|
2903 | |
|
|
2904 | utf8::encode $msg; |
|
|
2905 | |
|
|
2906 | if (0 && $msg =~ /\[/) { |
|
|
2907 | # COMMAND/INFO |
|
|
2908 | $self->send_packet ("drawextinfo $color 10 8 $msg") |
|
|
2909 | } else { |
|
|
2910 | $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g; |
|
|
2911 | $self->send_packet ("drawinfo $color $msg") |
|
|
2912 | } |
|
|
2913 | } |
|
|
2914 | } |
|
|
2915 | } |
2917 | } |
2916 | |
2918 | |
2917 | =item $client->ext_msg ($type, @msg) |
2919 | =item $client->ext_msg ($type, @msg) |
2918 | |
2920 | |
2919 | Sends an ext event to the client. |
2921 | Sends an ext event to the client. |
… | |
… | |
3094 | The following functions and methods are available within a safe environment: |
3096 | The following functions and methods are available within a safe environment: |
3095 | |
3097 | |
3096 | cf::object |
3098 | cf::object |
3097 | contr pay_amount pay_player map x y force_find force_add destroy |
3099 | contr pay_amount pay_player map x y force_find force_add destroy |
3098 | insert remove name archname title slaying race decrease split |
3100 | insert remove name archname title slaying race decrease split |
|
|
3101 | value |
3099 | |
3102 | |
3100 | cf::object::player |
3103 | cf::object::player |
3101 | player |
3104 | player |
3102 | |
3105 | |
3103 | cf::player |
3106 | cf::player |
… | |
… | |
3109 | =cut |
3112 | =cut |
3110 | |
3113 | |
3111 | for ( |
3114 | for ( |
3112 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3115 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3113 | insert remove inv nrof name archname title slaying race |
3116 | insert remove inv nrof name archname title slaying race |
3114 | decrease split destroy change_exp)], |
3117 | decrease split destroy change_exp value msg lore send_msg)], |
3115 | ["cf::object::player" => qw(player)], |
3118 | ["cf::object::player" => qw(player)], |
3116 | ["cf::player" => qw(peaceful)], |
3119 | ["cf::player" => qw(peaceful send_msg)], |
3117 | ["cf::map" => qw(trigger)], |
3120 | ["cf::map" => qw(trigger)], |
3118 | ) { |
3121 | ) { |
3119 | no strict 'refs'; |
3122 | no strict 'refs'; |
3120 | my ($pkg, @funs) = @$_; |
3123 | my ($pkg, @funs) = @$_; |
3121 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3124 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
… | |
… | |
3138 | my ($code, %vars) = @_; |
3141 | my ($code, %vars) = @_; |
3139 | |
3142 | |
3140 | my $qcode = $code; |
3143 | my $qcode = $code; |
3141 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3144 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3142 | $qcode =~ s/\n/\\n/g; |
3145 | $qcode =~ s/\n/\\n/g; |
|
|
3146 | |
|
|
3147 | %vars = (_dummy => 0) unless %vars; |
3143 | |
3148 | |
3144 | local $_; |
3149 | local $_; |
3145 | local @safe::cf::_safe_eval_args = values %vars; |
3150 | local @safe::cf::_safe_eval_args = values %vars; |
3146 | |
3151 | |
3147 | my $eval = |
3152 | my $eval = |
… | |
… | |
3382 | sub reload_config { |
3387 | sub reload_config { |
3383 | open my $fh, "<:utf8", "$CONFDIR/config" |
3388 | open my $fh, "<:utf8", "$CONFDIR/config" |
3384 | or return; |
3389 | or return; |
3385 | |
3390 | |
3386 | local $/; |
3391 | local $/; |
3387 | *CFG = YAML::Load <$fh>; |
3392 | *CFG = YAML::XS::Load <$fh>; |
3388 | |
3393 | |
3389 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3394 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3390 | |
3395 | |
3391 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3396 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3392 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3397 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3417 | |
3422 | |
3418 | seek $fh, 0, 0; |
3423 | seek $fh, 0, 0; |
3419 | print $fh $$; |
3424 | print $fh $$; |
3420 | } |
3425 | } |
3421 | |
3426 | |
|
|
3427 | sub main_loop { |
|
|
3428 | warn "EV::loop starting\n"; |
|
|
3429 | if (1) { |
|
|
3430 | EV::loop; |
|
|
3431 | } |
|
|
3432 | warn "EV::loop returned\n"; |
|
|
3433 | goto &main_loop unless $REALLY_UNLOOP; |
|
|
3434 | } |
|
|
3435 | |
3422 | sub main { |
3436 | sub main { |
3423 | cf::init_globals; # initialise logging |
3437 | cf::init_globals; # initialise logging |
3424 | |
3438 | |
3425 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3439 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3426 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3440 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
… | |
… | |
3429 | |
3443 | |
3430 | cf::init_experience; |
3444 | cf::init_experience; |
3431 | cf::init_anim; |
3445 | cf::init_anim; |
3432 | cf::init_attackmess; |
3446 | cf::init_attackmess; |
3433 | cf::init_dynamic; |
3447 | cf::init_dynamic; |
3434 | cf::init_block; |
|
|
3435 | |
3448 | |
3436 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3449 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3437 | |
3450 | |
3438 | # we must not ever block the main coroutine |
3451 | # we must not ever block the main coroutine |
3439 | local $Coro::idle = sub { |
3452 | local $Coro::idle = sub { |
… | |
… | |
3465 | load_extensions; |
3478 | load_extensions; |
3466 | |
3479 | |
3467 | utime time, time, $RUNTIMEFILE; |
3480 | utime time, time, $RUNTIMEFILE; |
3468 | |
3481 | |
3469 | # no (long-running) fork's whatsoever before this point(!) |
3482 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3483 | use POSIX (); |
3470 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3484 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3471 | |
3485 | |
3472 | (pop @POST_INIT)->(0) while @POST_INIT; |
3486 | (pop @POST_INIT)->(0) while @POST_INIT; |
3473 | }; |
3487 | }; |
3474 | |
3488 | |
3475 | EV::loop; |
3489 | main_loop; |
3476 | } |
3490 | } |
3477 | |
3491 | |
3478 | ############################################################################# |
3492 | ############################################################################# |
3479 | # initialisation and cleanup |
3493 | # initialisation and cleanup |
3480 | |
3494 | |
… | |
… | |
3736 | clear_package "safe::$_" |
3750 | clear_package "safe::$_" |
3737 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3751 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3738 | |
3752 | |
3739 | warn "unloading cf.pm \"a bit\""; |
3753 | warn "unloading cf.pm \"a bit\""; |
3740 | delete $INC{"cf.pm"}; |
3754 | delete $INC{"cf.pm"}; |
3741 | delete $INC{"cf/pod.pm"}; |
3755 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3742 | |
3756 | |
3743 | # don't, removes xs symbols, too, |
3757 | # don't, removes xs symbols, too, |
3744 | # and global variables created in xs |
3758 | # and global variables created in xs |
3745 | #clear_package __PACKAGE__; |
3759 | #clear_package __PACKAGE__; |
3746 | |
3760 | |
3747 | warn "unload completed, starting to reload now"; |
3761 | warn "unload completed, starting to reload now"; |
3748 | |
3762 | |
3749 | warn "reloading cf.pm"; |
3763 | warn "reloading cf.pm"; |
3750 | require cf; |
3764 | require cf; |
3751 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
3765 | cf::_connect_to_perl_1; |
3752 | |
3766 | |
3753 | warn "loading config and database again"; |
3767 | warn "loading config and database again"; |
3754 | cf::reload_config; |
3768 | cf::reload_config; |
3755 | |
3769 | |
3756 | warn "loading extensions"; |
3770 | warn "loading extensions"; |
… | |
… | |
3963 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3977 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3964 | } |
3978 | } |
3965 | } |
3979 | } |
3966 | |
3980 | |
3967 | # load additional modules |
3981 | # load additional modules |
3968 | use cf::pod; |
3982 | require "cf/$_.pm" for @EXTRA_MODULES; |
|
|
3983 | cf::_connect_to_perl_2; |
3969 | |
3984 | |
3970 | END { cf::emergency_save } |
3985 | END { cf::emergency_save } |
3971 | |
3986 | |
3972 | 1 |
3987 | 1 |
3973 | |
3988 | |