… | |
… | |
21 | |
21 | |
22 | package cf; |
22 | package cf; |
23 | |
23 | |
24 | use 5.10.0; |
24 | use 5.10.0; |
25 | use utf8; |
25 | use utf8; |
26 | use strict "vars", "subs"; |
26 | use strict qw(vars subs); |
27 | |
27 | |
28 | use Symbol; |
28 | use Symbol; |
29 | use List::Util; |
29 | use List::Util; |
30 | use Socket; |
30 | use Socket; |
31 | use EV; |
31 | use EV; |
32 | use Opcode; |
32 | use Opcode; |
33 | use Safe; |
33 | use Safe; |
34 | use Safe::Hole; |
34 | use Safe::Hole; |
35 | use Storable (); |
35 | use Storable (); |
36 | |
36 | |
|
|
37 | use Guard (); |
37 | use Coro (); |
38 | use Coro (); |
38 | use Coro::State; |
39 | use Coro::State; |
39 | use Coro::Handle; |
40 | use Coro::Handle; |
40 | use Coro::EV; |
41 | use Coro::EV; |
41 | use Coro::AnyEvent; |
42 | use Coro::AnyEvent; |
42 | use Coro::Timer; |
43 | use Coro::Timer; |
43 | use Coro::Signal; |
44 | use Coro::Signal; |
44 | use Coro::Semaphore; |
45 | use Coro::Semaphore; |
|
|
46 | use Coro::SemaphoreSet; |
45 | use Coro::AnyEvent; |
47 | use Coro::AnyEvent; |
46 | use Coro::AIO; |
48 | use Coro::AIO; |
47 | use Coro::BDB 1.6; |
49 | use Coro::BDB 1.6; |
48 | use Coro::Storable; |
50 | use Coro::Storable; |
49 | use Coro::Util (); |
51 | use Coro::Util (); |
… | |
… | |
69 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
71 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
70 | |
72 | |
71 | # make sure c-lzf reinitialises itself |
73 | # make sure c-lzf reinitialises itself |
72 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
74 | 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 |
75 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
|
|
76 | |
|
|
77 | # strictly for debugging |
|
|
78 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
74 | |
79 | |
75 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
80 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
76 | |
81 | |
77 | our %COMMAND = (); |
82 | our %COMMAND = (); |
78 | our %COMMAND_TIME = (); |
83 | our %COMMAND_TIME = (); |
… | |
… | |
103 | our %RESOURCE; |
108 | our %RESOURCE; |
104 | |
109 | |
105 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
110 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
106 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
111 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
107 | our $NEXT_TICK; |
112 | our $NEXT_TICK; |
108 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
113 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
109 | |
114 | |
110 | our $BDB_DEADLOCK_WATCHER; |
115 | our $BDB_DEADLOCK_WATCHER; |
111 | our $BDB_CHECKPOINT_WATCHER; |
116 | our $BDB_CHECKPOINT_WATCHER; |
112 | our $BDB_TRICKLE_WATCHER; |
117 | our $BDB_TRICKLE_WATCHER; |
113 | our $DB_ENV; |
118 | our $DB_ENV; |
114 | |
119 | |
|
|
120 | our @EXTRA_MODULES = qw(pod mapscript); |
|
|
121 | |
115 | our %CFG; |
122 | our %CFG; |
116 | |
123 | |
117 | our $UPTIME; $UPTIME ||= time; |
124 | our $UPTIME; $UPTIME ||= time; |
118 | our $RUNTIME; |
125 | our $RUNTIME; |
119 | our $NOW; |
126 | our $NOW; |
… | |
… | |
130 | our $JITTER; # average jitter |
137 | our $JITTER; # average jitter |
131 | our $TICK_START; # for load detecting purposes |
138 | our $TICK_START; # for load detecting purposes |
132 | |
139 | |
133 | our @POST_INIT; |
140 | our @POST_INIT; |
134 | |
141 | |
135 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
142 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
|
|
143 | our $REALLY_UNLOOP; # never set to true, please :) |
136 | |
144 | |
137 | binmode STDOUT; |
145 | binmode STDOUT; |
138 | binmode STDERR; |
146 | binmode STDERR; |
139 | |
147 | |
140 | # read virtual server time, if available |
148 | # read virtual server time, if available |
… | |
… | |
142 | open my $fh, "<", $RUNTIMEFILE |
150 | open my $fh, "<", $RUNTIMEFILE |
143 | or die "unable to read $RUNTIMEFILE file: $!"; |
151 | or die "unable to read $RUNTIMEFILE file: $!"; |
144 | $RUNTIME = <$fh> + 0.; |
152 | $RUNTIME = <$fh> + 0.; |
145 | } |
153 | } |
146 | |
154 | |
|
|
155 | eval "sub TICK() { $TICK } 1" or die; |
|
|
156 | |
147 | mkdir $_ |
157 | mkdir $_ |
148 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
158 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
149 | |
159 | |
150 | our $EMERGENCY_POSITION; |
160 | our $EMERGENCY_POSITION; |
151 | |
161 | |
… | |
… | |
212 | |
222 | |
213 | =back |
223 | =back |
214 | |
224 | |
215 | =cut |
225 | =cut |
216 | |
226 | |
217 | BEGIN { |
227 | $Coro::State::WARNHOOK = sub { |
218 | *CORE::GLOBAL::warn = sub { |
|
|
219 | my $msg = join "", @_; |
228 | my $msg = join "", @_; |
220 | |
229 | |
221 | $msg .= "\n" |
230 | $msg .= "\n" |
222 | unless $msg =~ /\n$/; |
231 | unless $msg =~ /\n$/; |
223 | |
232 | |
224 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
233 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
225 | |
234 | |
226 | LOG llevError, $msg; |
235 | LOG llevError, $msg; |
227 | }; |
236 | }; |
228 | } |
|
|
229 | |
237 | |
230 | $Coro::State::DIEHOOK = sub { |
238 | $Coro::State::DIEHOOK = sub { |
231 | return unless $^S eq 0; # "eq", not "==" |
239 | return unless $^S eq 0; # "eq", not "==" |
|
|
240 | |
|
|
241 | warn Carp::longmess $_[0]; |
232 | |
242 | |
233 | if ($Coro::current == $Coro::main) {#d# |
243 | if ($Coro::current == $Coro::main) {#d# |
234 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
244 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
235 | return;#d# |
245 | return;#d# |
236 | }#d# |
246 | }#d# |
237 | |
247 | |
238 | # kill coroutine otherwise |
248 | # kill coroutine otherwise |
239 | warn Carp::longmess $_[0]; |
|
|
240 | Coro::terminate |
249 | Coro::terminate |
241 | }; |
250 | }; |
242 | |
|
|
243 | $SIG{__DIE__} = sub { }; #d#? |
|
|
244 | |
251 | |
245 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
252 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
246 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
253 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
247 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
254 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
248 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
255 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
330 | Wait until the given lock is available. See cf::lock_acquire. |
337 | Wait until the given lock is available. See cf::lock_acquire. |
331 | |
338 | |
332 | =item my $lock = cf::lock_acquire $string |
339 | =item my $lock = cf::lock_acquire $string |
333 | |
340 | |
334 | Wait until the given lock is available and then acquires it and returns |
341 | 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, |
342 | 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 |
343 | for example when the coroutine gets canceled), the lock is automatically |
337 | returned. |
344 | returned. |
338 | |
345 | |
339 | Locks are *not* recursive, locking from the same coro twice results in a |
346 | Locks are *not* recursive, locking from the same coro twice results in a |
340 | deadlocked coro. |
347 | deadlocked coro. |
… | |
… | |
346 | |
353 | |
347 | Return true if the lock is currently active, i.e. somebody has locked it. |
354 | Return true if the lock is currently active, i.e. somebody has locked it. |
348 | |
355 | |
349 | =cut |
356 | =cut |
350 | |
357 | |
351 | our %LOCK; |
358 | our $LOCKS = new Coro::SemaphoreSet; |
352 | our %LOCKER;#d# |
|
|
353 | |
359 | |
354 | sub lock_wait($) { |
360 | sub lock_wait($) { |
355 | my ($key) = @_; |
361 | $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 | } |
362 | } |
369 | |
363 | |
370 | sub lock_acquire($) { |
364 | sub lock_acquire($) { |
371 | my ($key) = @_; |
365 | $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 | } |
366 | } |
385 | |
367 | |
386 | sub lock_active($) { |
368 | sub lock_active($) { |
387 | my ($key) = @_; |
369 | $LOCKS->count ($_[0]) < 1 |
388 | |
|
|
389 | ! ! $LOCK{$key} |
|
|
390 | } |
370 | } |
391 | |
371 | |
392 | sub freeze_mainloop { |
372 | sub freeze_mainloop { |
393 | tick_inhibit_inc; |
373 | tick_inhibit_inc; |
394 | |
374 | |
395 | Coro::guard \&tick_inhibit_dec; |
375 | &Guard::guard (\&tick_inhibit_dec); |
396 | } |
376 | } |
397 | |
377 | |
398 | =item cf::periodic $interval, $cb |
378 | =item cf::periodic $interval, $cb |
399 | |
379 | |
400 | Like EV::periodic, but randomly selects a starting point so that the actions |
380 | Like EV::periodic, but randomly selects a starting point so that the actions |
… | |
… | |
1172 | $decname, length $$rdata, scalar @$objs; |
1152 | $decname, length $$rdata, scalar @$objs; |
1173 | |
1153 | |
1174 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1154 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1175 | aio_chmod $fh, SAVE_MODE; |
1155 | aio_chmod $fh, SAVE_MODE; |
1176 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1156 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1177 | aio_fsync $fh if $cf::USE_FSYNC; |
1157 | if ($cf::USE_FSYNC) { |
|
|
1158 | 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; |
|
|
1159 | aio_fsync $fh; |
|
|
1160 | } |
1178 | aio_close $fh; |
1161 | aio_close $fh; |
1179 | |
1162 | |
1180 | if (@$objs) { |
1163 | if (@$objs) { |
1181 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1164 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1182 | aio_chmod $fh, SAVE_MODE; |
1165 | aio_chmod $fh, SAVE_MODE; |
1183 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1166 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1184 | aio_write $fh, 0, (length $data), $data, 0; |
1167 | aio_write $fh, 0, (length $data), $data, 0; |
1185 | aio_fsync $fh if $cf::USE_FSYNC; |
1168 | if ($cf::USE_FSYNC) { |
|
|
1169 | 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; |
|
|
1170 | aio_fsync $fh; |
|
|
1171 | } |
1186 | aio_close $fh; |
1172 | aio_close $fh; |
1187 | aio_rename "$filename.pst~", "$filename.pst"; |
1173 | aio_rename "$filename.pst~", "$filename.pst"; |
1188 | } |
1174 | } |
1189 | } else { |
1175 | } else { |
1190 | aio_unlink "$filename.pst"; |
1176 | aio_unlink "$filename.pst"; |
… | |
… | |
1193 | aio_rename "$filename~", $filename; |
1179 | aio_rename "$filename~", $filename; |
1194 | |
1180 | |
1195 | $filename =~ s%/[^/]+$%%; |
1181 | $filename =~ s%/[^/]+$%%; |
1196 | aio_pathsync $filename if $cf::USE_FSYNC; |
1182 | aio_pathsync $filename if $cf::USE_FSYNC; |
1197 | } else { |
1183 | } else { |
1198 | warn "FATAL: $filename~: $!\n"; |
1184 | warn "unable to save objects: $filename~: $!\n"; |
1199 | } |
1185 | } |
1200 | } else { |
1186 | } else { |
1201 | aio_unlink $filename; |
1187 | aio_unlink $filename; |
1202 | aio_unlink "$filename.pst"; |
1188 | aio_unlink "$filename.pst"; |
1203 | } |
1189 | } |
… | |
… | |
1341 | |
1327 | |
1342 | # "readahead" all extensions |
1328 | # "readahead" all extensions |
1343 | sub cache_extensions { |
1329 | sub cache_extensions { |
1344 | my $grp = IO::AIO::aio_group; |
1330 | my $grp = IO::AIO::aio_group; |
1345 | |
1331 | |
1346 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
1332 | add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub { |
1347 | for (grep /\.ext$/, @{$_[0]}) { |
1333 | for (grep /\.ext$/, @{$_[0]}) { |
1348 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1334 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1349 | } |
1335 | } |
1350 | }; |
1336 | }; |
1351 | |
1337 | |
… | |
… | |
1887 | |
1873 | |
1888 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1874 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1889 | "$UNIQUEDIR/$path" |
1875 | "$UNIQUEDIR/$path" |
1890 | } |
1876 | } |
1891 | |
1877 | |
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 { |
1878 | sub decay_objects { |
1902 | my ($self) = @_; |
1879 | my ($self) = @_; |
1903 | |
1880 | |
1904 | return if $self->{deny_reset}; |
1881 | return if $self->{deny_reset}; |
1905 | |
1882 | |
… | |
… | |
1987 | sub find { |
1964 | sub find { |
1988 | my ($path, $origin) = @_; |
1965 | my ($path, $origin) = @_; |
1989 | |
1966 | |
1990 | $path = normalise $path, $origin && $origin->path; |
1967 | $path = normalise $path, $origin && $origin->path; |
1991 | |
1968 | |
1992 | cf::lock_wait "map_data:$path";#d#remove |
1969 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
1993 | cf::lock_wait "map_find:$path"; |
1970 | my $guard2 = cf::lock_acquire "map_find:$path"; |
1994 | |
1971 | |
1995 | $cf::MAP{$path} || do { |
1972 | $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 |
1973 | my $map = new_from_path cf::map $path |
2000 | or return; |
1974 | or return; |
2001 | |
1975 | |
2002 | $map->{last_save} = $cf::RUNTIME; |
1976 | $map->{last_save} = $cf::RUNTIME; |
2003 | |
1977 | |
… | |
… | |
2070 | |
2044 | |
2071 | unless ($self->{deny_activate}) { |
2045 | unless ($self->{deny_activate}) { |
2072 | $self->decay_objects; |
2046 | $self->decay_objects; |
2073 | $self->fix_auto_apply; |
2047 | $self->fix_auto_apply; |
2074 | $self->update_buttons; |
2048 | $self->update_buttons; |
2075 | cf::cede_to_tick; |
|
|
2076 | $self->set_darkness_map; |
|
|
2077 | cf::cede_to_tick; |
2049 | cf::cede_to_tick; |
2078 | $self->activate; |
2050 | $self->activate; |
2079 | } |
2051 | } |
2080 | |
2052 | |
2081 | $self->{last_save} = $cf::RUNTIME; |
2053 | $self->{last_save} = $cf::RUNTIME; |
… | |
… | |
2423 | |
2395 | |
2424 | our $SAY_CHANNEL = { |
2396 | our $SAY_CHANNEL = { |
2425 | id => "say", |
2397 | id => "say", |
2426 | title => "Map", |
2398 | title => "Map", |
2427 | reply => "say ", |
2399 | reply => "say ", |
2428 | tooltip => "Things said to and replied from npcs near you and other players on the same map only.", |
2400 | tooltip => "Things said to and replied from NPCs near you and other players on the same map only.", |
2429 | }; |
2401 | }; |
2430 | |
2402 | |
2431 | our $CHAT_CHANNEL = { |
2403 | our $CHAT_CHANNEL = { |
2432 | id => "chat", |
2404 | id => "chat", |
2433 | title => "Chat", |
2405 | title => "Chat", |
… | |
… | |
2559 | |
2531 | |
2560 | $map->load; |
2532 | $map->load; |
2561 | $map->load_neighbours; |
2533 | $map->load_neighbours; |
2562 | |
2534 | |
2563 | return unless $self->contr->active; |
2535 | return unless $self->contr->active; |
2564 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
|
|
2565 | $self->activate_recursive; |
|
|
2566 | |
2536 | |
2567 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2537 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2568 | $self->enter_map ($map, $x, $y); |
2538 | $self->enter_map ($map, $x, $y); |
|
|
2539 | |
|
|
2540 | # only activate afterwards, to support waiting in hooks |
|
|
2541 | $self->activate_recursive; |
2569 | } |
2542 | } |
2570 | |
2543 | |
2571 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2544 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2572 | |
2545 | |
2573 | Moves the player to the given map-path and coordinates by first freezing |
2546 | Moves the player to the given map-path and coordinates by first freezing |
… | |
… | |
2808 | id => "infobox", |
2781 | id => "infobox", |
2809 | title => "Body Parts", |
2782 | title => "Body Parts", |
2810 | reply => undef, |
2783 | reply => undef, |
2811 | tooltip => "Shows which body parts you posess and are available", |
2784 | tooltip => "Shows which body parts you posess and are available", |
2812 | }, |
2785 | }, |
|
|
2786 | "c/statistics" => { |
|
|
2787 | id => "infobox", |
|
|
2788 | title => "Statistics", |
|
|
2789 | reply => undef, |
|
|
2790 | tooltip => "Shows your primary statistics", |
|
|
2791 | }, |
2813 | "c/skills" => { |
2792 | "c/skills" => { |
2814 | id => "infobox", |
2793 | id => "infobox", |
2815 | title => "Skills", |
2794 | title => "Skills", |
2816 | reply => undef, |
2795 | reply => undef, |
2817 | tooltip => "Shows your experience per skill and item power", |
2796 | tooltip => "Shows your experience per skill and item power", |
|
|
2797 | }, |
|
|
2798 | "c/shopitems" => { |
|
|
2799 | id => "infobox", |
|
|
2800 | title => "Shop Items", |
|
|
2801 | reply => undef, |
|
|
2802 | tooltip => "Shows the items currently for sale in this shop", |
|
|
2803 | }, |
|
|
2804 | "c/resistances" => { |
|
|
2805 | id => "infobox", |
|
|
2806 | title => "Resistances", |
|
|
2807 | reply => undef, |
|
|
2808 | tooltip => "Shows your resistances", |
|
|
2809 | }, |
|
|
2810 | "c/pets" => { |
|
|
2811 | id => "infobox", |
|
|
2812 | title => "Pets", |
|
|
2813 | reply => undef, |
|
|
2814 | tooltip => "Shows information abotu your pets/a specific pet", |
|
|
2815 | }, |
|
|
2816 | "c/perceiveself" => { |
|
|
2817 | id => "infobox", |
|
|
2818 | title => "Perceive Self", |
|
|
2819 | reply => undef, |
|
|
2820 | tooltip => "You gained detailed knowledge about yourself", |
2818 | }, |
2821 | }, |
2819 | "c/uptime" => { |
2822 | "c/uptime" => { |
2820 | id => "infobox", |
2823 | id => "infobox", |
2821 | title => "Uptime", |
2824 | title => "Uptime", |
2822 | reply => undef, |
2825 | reply => undef, |
… | |
… | |
2832 | id => "party", |
2835 | id => "party", |
2833 | title => "Party", |
2836 | title => "Party", |
2834 | reply => "gsay ", |
2837 | reply => "gsay ", |
2835 | tooltip => "Messages and chat related to your party", |
2838 | tooltip => "Messages and chat related to your party", |
2836 | }, |
2839 | }, |
|
|
2840 | "c/death" => { |
|
|
2841 | id => "death", |
|
|
2842 | title => "Death", |
|
|
2843 | reply => undef, |
|
|
2844 | tooltip => "Reason for and more info about your most recent death", |
|
|
2845 | }, |
|
|
2846 | "c/say" => $SAY_CHANNEL, |
|
|
2847 | "c/chat" => $CHAT_CHANNEL, |
2837 | ); |
2848 | ); |
2838 | |
2849 | |
2839 | sub cf::client::send_msg { |
2850 | sub cf::client::send_msg { |
2840 | my ($self, $channel, $msg, $color, @extra) = @_; |
2851 | my ($self, $channel, $msg, $color, @extra) = @_; |
2841 | |
2852 | |
… | |
… | |
2846 | |
2857 | |
2847 | # check predefined channels, for the benefit of C |
2858 | # check predefined channels, for the benefit of C |
2848 | if ($CHANNEL{$channel}) { |
2859 | if ($CHANNEL{$channel}) { |
2849 | $channel = $CHANNEL{$channel}; |
2860 | $channel = $CHANNEL{$channel}; |
2850 | |
2861 | |
2851 | $self->ext_msg (channel_info => $channel) |
2862 | $self->ext_msg (channel_info => $channel); |
2852 | if $self->can_msg; |
|
|
2853 | |
|
|
2854 | $channel = $channel->{id}; |
2863 | $channel = $channel->{id}; |
2855 | |
2864 | |
2856 | } elsif (ref $channel) { |
2865 | } elsif (ref $channel) { |
2857 | # send meta info to client, if not yet sent |
2866 | # send meta info to client, if not yet sent |
2858 | unless (exists $self->{channel}{$channel->{id}}) { |
2867 | unless (exists $self->{channel}{$channel->{id}}) { |
2859 | $self->{channel}{$channel->{id}} = $channel; |
2868 | $self->{channel}{$channel->{id}} = $channel; |
2860 | $self->ext_msg (channel_info => $channel) |
2869 | $self->ext_msg (channel_info => $channel); |
2861 | if $self->can_msg; |
|
|
2862 | } |
2870 | } |
2863 | |
2871 | |
2864 | $channel = $channel->{id}; |
2872 | $channel = $channel->{id}; |
2865 | } |
2873 | } |
2866 | |
2874 | |
2867 | return unless @extra || length $msg; |
2875 | return unless @extra || length $msg; |
2868 | |
2876 | |
2869 | if ($self->can_msg) { |
|
|
2870 | # default colour, mask it out |
2877 | # default colour, mask it out |
2871 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2878 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2872 | if $color & cf::NDI_DEF; |
2879 | if $color & cf::NDI_DEF; |
2873 | |
2880 | |
2874 | my $pkt = "msg " |
2881 | my $pkt = "msg " |
2875 | . $self->{json_coder}->encode ( |
2882 | . $self->{json_coder}->encode ( |
2876 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2883 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2877 | ); |
2884 | ); |
2878 | |
2885 | |
2879 | # try lzf for large packets |
2886 | # try lzf for large packets |
2880 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2887 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2881 | if 1024 <= length $pkt and $self->{can_lzf}; |
2888 | if 1024 <= length $pkt and $self->{can_lzf}; |
2882 | |
2889 | |
2883 | # split very large packets |
2890 | # split very large packets |
2884 | if (8192 < length $pkt and $self->{can_lzf}) { |
2891 | if (8192 < length $pkt and $self->{can_lzf}) { |
2885 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2892 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2886 | $pkt = "frag"; |
2893 | $pkt = "frag"; |
2887 | } |
2894 | } |
2888 | |
2895 | |
2889 | $self->send_packet ($pkt); |
2896 | $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 | } |
2897 | } |
2916 | |
2898 | |
2917 | =item $client->ext_msg ($type, @msg) |
2899 | =item $client->ext_msg ($type, @msg) |
2918 | |
2900 | |
2919 | Sends an ext event to the client. |
2901 | Sends an ext event to the client. |
… | |
… | |
3094 | The following functions and methods are available within a safe environment: |
3076 | The following functions and methods are available within a safe environment: |
3095 | |
3077 | |
3096 | cf::object |
3078 | cf::object |
3097 | contr pay_amount pay_player map x y force_find force_add destroy |
3079 | contr pay_amount pay_player map x y force_find force_add destroy |
3098 | insert remove name archname title slaying race decrease split |
3080 | insert remove name archname title slaying race decrease split |
|
|
3081 | value |
3099 | |
3082 | |
3100 | cf::object::player |
3083 | cf::object::player |
3101 | player |
3084 | player |
3102 | |
3085 | |
3103 | cf::player |
3086 | cf::player |
… | |
… | |
3109 | =cut |
3092 | =cut |
3110 | |
3093 | |
3111 | for ( |
3094 | for ( |
3112 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3095 | ["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 |
3096 | insert remove inv nrof name archname title slaying race |
3114 | decrease split destroy change_exp)], |
3097 | decrease split destroy change_exp value msg lore send_msg)], |
3115 | ["cf::object::player" => qw(player)], |
3098 | ["cf::object::player" => qw(player)], |
3116 | ["cf::player" => qw(peaceful)], |
3099 | ["cf::player" => qw(peaceful send_msg)], |
3117 | ["cf::map" => qw(trigger)], |
3100 | ["cf::map" => qw(trigger)], |
3118 | ) { |
3101 | ) { |
3119 | no strict 'refs'; |
3102 | no strict 'refs'; |
3120 | my ($pkg, @funs) = @$_; |
3103 | my ($pkg, @funs) = @$_; |
3121 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3104 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
… | |
… | |
3138 | my ($code, %vars) = @_; |
3121 | my ($code, %vars) = @_; |
3139 | |
3122 | |
3140 | my $qcode = $code; |
3123 | my $qcode = $code; |
3141 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3124 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3142 | $qcode =~ s/\n/\\n/g; |
3125 | $qcode =~ s/\n/\\n/g; |
|
|
3126 | |
|
|
3127 | %vars = (_dummy => 0) unless %vars; |
3143 | |
3128 | |
3144 | local $_; |
3129 | local $_; |
3145 | local @safe::cf::_safe_eval_args = values %vars; |
3130 | local @safe::cf::_safe_eval_args = values %vars; |
3146 | |
3131 | |
3147 | my $eval = |
3132 | my $eval = |
… | |
… | |
3417 | |
3402 | |
3418 | seek $fh, 0, 0; |
3403 | seek $fh, 0, 0; |
3419 | print $fh $$; |
3404 | print $fh $$; |
3420 | } |
3405 | } |
3421 | |
3406 | |
|
|
3407 | sub main_loop { |
|
|
3408 | warn "EV::loop starting\n"; |
|
|
3409 | if (1) { |
|
|
3410 | EV::loop; |
|
|
3411 | } |
|
|
3412 | warn "EV::loop returned\n"; |
|
|
3413 | goto &main_loop unless $REALLY_UNLOOP; |
|
|
3414 | } |
|
|
3415 | |
3422 | sub main { |
3416 | sub main { |
3423 | cf::init_globals; # initialise logging |
3417 | cf::init_globals; # initialise logging |
3424 | |
3418 | |
3425 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3419 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3426 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3420 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
… | |
… | |
3429 | |
3423 | |
3430 | cf::init_experience; |
3424 | cf::init_experience; |
3431 | cf::init_anim; |
3425 | cf::init_anim; |
3432 | cf::init_attackmess; |
3426 | cf::init_attackmess; |
3433 | cf::init_dynamic; |
3427 | cf::init_dynamic; |
3434 | cf::init_block; |
|
|
3435 | |
3428 | |
3436 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3429 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3437 | |
3430 | |
3438 | # we must not ever block the main coroutine |
3431 | # we must not ever block the main coroutine |
3439 | local $Coro::idle = sub { |
3432 | local $Coro::idle = sub { |
… | |
… | |
3465 | load_extensions; |
3458 | load_extensions; |
3466 | |
3459 | |
3467 | utime time, time, $RUNTIMEFILE; |
3460 | utime time, time, $RUNTIMEFILE; |
3468 | |
3461 | |
3469 | # no (long-running) fork's whatsoever before this point(!) |
3462 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3463 | use POSIX (); |
3470 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3464 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3471 | |
3465 | |
3472 | (pop @POST_INIT)->(0) while @POST_INIT; |
3466 | (pop @POST_INIT)->(0) while @POST_INIT; |
3473 | }; |
3467 | }; |
3474 | |
3468 | |
3475 | EV::loop; |
3469 | main_loop; |
3476 | } |
3470 | } |
3477 | |
3471 | |
3478 | ############################################################################# |
3472 | ############################################################################# |
3479 | # initialisation and cleanup |
3473 | # initialisation and cleanup |
3480 | |
3474 | |
… | |
… | |
3736 | clear_package "safe::$_" |
3730 | clear_package "safe::$_" |
3737 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3731 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3738 | |
3732 | |
3739 | warn "unloading cf.pm \"a bit\""; |
3733 | warn "unloading cf.pm \"a bit\""; |
3740 | delete $INC{"cf.pm"}; |
3734 | delete $INC{"cf.pm"}; |
3741 | delete $INC{"cf/pod.pm"}; |
3735 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3742 | |
3736 | |
3743 | # don't, removes xs symbols, too, |
3737 | # don't, removes xs symbols, too, |
3744 | # and global variables created in xs |
3738 | # and global variables created in xs |
3745 | #clear_package __PACKAGE__; |
3739 | #clear_package __PACKAGE__; |
3746 | |
3740 | |
… | |
… | |
3963 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3957 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3964 | } |
3958 | } |
3965 | } |
3959 | } |
3966 | |
3960 | |
3967 | # load additional modules |
3961 | # load additional modules |
3968 | use cf::pod; |
3962 | require "cf/$_.pm" for @EXTRA_MODULES; |
3969 | |
3963 | |
3970 | END { cf::emergency_save } |
3964 | END { cf::emergency_save } |
3971 | |
3965 | |
3972 | 1 |
3966 | 1 |
3973 | |
3967 | |