… | |
… | |
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 |
141 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
149 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
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 | } |
|
|
154 | |
|
|
155 | eval "sub TICK() { $TICK } 1" or die; |
146 | |
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; |
… | |
… | |
330 | Wait until the given lock is available. See cf::lock_acquire. |
340 | Wait until the given lock is available. See cf::lock_acquire. |
331 | |
341 | |
332 | =item my $lock = cf::lock_acquire $string |
342 | =item my $lock = cf::lock_acquire $string |
333 | |
343 | |
334 | Wait until the given lock is available and then acquires it and returns |
344 | 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, |
345 | 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 |
346 | for example when the coroutine gets canceled), the lock is automatically |
337 | returned. |
347 | returned. |
338 | |
348 | |
339 | Locks are *not* recursive, locking from the same coro twice results in a |
349 | Locks are *not* recursive, locking from the same coro twice results in a |
340 | deadlocked coro. |
350 | deadlocked coro. |
… | |
… | |
346 | |
356 | |
347 | Return true if the lock is currently active, i.e. somebody has locked it. |
357 | Return true if the lock is currently active, i.e. somebody has locked it. |
348 | |
358 | |
349 | =cut |
359 | =cut |
350 | |
360 | |
351 | our %LOCK; |
361 | our $LOCKS = new Coro::SemaphoreSet; |
352 | our %LOCKER;#d# |
|
|
353 | |
362 | |
354 | sub lock_wait($) { |
363 | sub lock_wait($) { |
355 | my ($key) = @_; |
364 | $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 | } |
365 | } |
369 | |
366 | |
370 | sub lock_acquire($) { |
367 | sub lock_acquire($) { |
371 | my ($key) = @_; |
368 | $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 | } |
369 | } |
385 | |
370 | |
386 | sub lock_active($) { |
371 | sub lock_active($) { |
387 | my ($key) = @_; |
372 | $LOCKS->count ($_[0]) < 1 |
388 | |
|
|
389 | ! ! $LOCK{$key} |
|
|
390 | } |
373 | } |
391 | |
374 | |
392 | sub freeze_mainloop { |
375 | sub freeze_mainloop { |
393 | tick_inhibit_inc; |
376 | tick_inhibit_inc; |
394 | |
377 | |
395 | Coro::guard \&tick_inhibit_dec; |
378 | &Guard::guard (\&tick_inhibit_dec); |
396 | } |
379 | } |
397 | |
380 | |
398 | =item cf::periodic $interval, $cb |
381 | =item cf::periodic $interval, $cb |
399 | |
382 | |
400 | Like EV::periodic, but randomly selects a starting point so that the actions |
383 | Like EV::periodic, but randomly selects a starting point so that the actions |
… | |
… | |
1172 | $decname, length $$rdata, scalar @$objs; |
1155 | $decname, length $$rdata, scalar @$objs; |
1173 | |
1156 | |
1174 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1157 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1175 | aio_chmod $fh, SAVE_MODE; |
1158 | aio_chmod $fh, SAVE_MODE; |
1176 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1159 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1177 | aio_fsync $fh if $cf::USE_FSYNC; |
1160 | if ($cf::USE_FSYNC) { |
|
|
1161 | 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; |
|
|
1162 | aio_fsync $fh; |
|
|
1163 | } |
1178 | aio_close $fh; |
1164 | aio_close $fh; |
1179 | |
1165 | |
1180 | if (@$objs) { |
1166 | if (@$objs) { |
1181 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1167 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1182 | aio_chmod $fh, SAVE_MODE; |
1168 | aio_chmod $fh, SAVE_MODE; |
1183 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1169 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1184 | aio_write $fh, 0, (length $data), $data, 0; |
1170 | aio_write $fh, 0, (length $data), $data, 0; |
1185 | aio_fsync $fh if $cf::USE_FSYNC; |
1171 | if ($cf::USE_FSYNC) { |
|
|
1172 | 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; |
|
|
1173 | aio_fsync $fh; |
|
|
1174 | } |
1186 | aio_close $fh; |
1175 | aio_close $fh; |
1187 | aio_rename "$filename.pst~", "$filename.pst"; |
1176 | aio_rename "$filename.pst~", "$filename.pst"; |
1188 | } |
1177 | } |
1189 | } else { |
1178 | } else { |
1190 | aio_unlink "$filename.pst"; |
1179 | aio_unlink "$filename.pst"; |
… | |
… | |
1193 | aio_rename "$filename~", $filename; |
1182 | aio_rename "$filename~", $filename; |
1194 | |
1183 | |
1195 | $filename =~ s%/[^/]+$%%; |
1184 | $filename =~ s%/[^/]+$%%; |
1196 | aio_pathsync $filename if $cf::USE_FSYNC; |
1185 | aio_pathsync $filename if $cf::USE_FSYNC; |
1197 | } else { |
1186 | } else { |
1198 | warn "FATAL: $filename~: $!\n"; |
1187 | warn "unable to save objects: $filename~: $!\n"; |
1199 | } |
1188 | } |
1200 | } else { |
1189 | } else { |
1201 | aio_unlink $filename; |
1190 | aio_unlink $filename; |
1202 | aio_unlink "$filename.pst"; |
1191 | aio_unlink "$filename.pst"; |
1203 | } |
1192 | } |
… | |
… | |
1341 | |
1330 | |
1342 | # "readahead" all extensions |
1331 | # "readahead" all extensions |
1343 | sub cache_extensions { |
1332 | sub cache_extensions { |
1344 | my $grp = IO::AIO::aio_group; |
1333 | my $grp = IO::AIO::aio_group; |
1345 | |
1334 | |
1346 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
1335 | add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub { |
1347 | for (grep /\.ext$/, @{$_[0]}) { |
1336 | for (grep /\.ext$/, @{$_[0]}) { |
1348 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1337 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1349 | } |
1338 | } |
1350 | }; |
1339 | }; |
1351 | |
1340 | |
… | |
… | |
1887 | |
1876 | |
1888 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1877 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1889 | "$UNIQUEDIR/$path" |
1878 | "$UNIQUEDIR/$path" |
1890 | } |
1879 | } |
1891 | |
1880 | |
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 { |
1881 | sub decay_objects { |
1902 | my ($self) = @_; |
1882 | my ($self) = @_; |
1903 | |
1883 | |
1904 | return if $self->{deny_reset}; |
1884 | return if $self->{deny_reset}; |
1905 | |
1885 | |
… | |
… | |
1987 | sub find { |
1967 | sub find { |
1988 | my ($path, $origin) = @_; |
1968 | my ($path, $origin) = @_; |
1989 | |
1969 | |
1990 | $path = normalise $path, $origin && $origin->path; |
1970 | $path = normalise $path, $origin && $origin->path; |
1991 | |
1971 | |
1992 | cf::lock_wait "map_data:$path";#d#remove |
1972 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
1993 | cf::lock_wait "map_find:$path"; |
1973 | my $guard2 = cf::lock_acquire "map_find:$path"; |
1994 | |
1974 | |
1995 | $cf::MAP{$path} || do { |
1975 | $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 |
1976 | my $map = new_from_path cf::map $path |
2000 | or return; |
1977 | or return; |
2001 | |
1978 | |
2002 | $map->{last_save} = $cf::RUNTIME; |
1979 | $map->{last_save} = $cf::RUNTIME; |
2003 | |
1980 | |
… | |
… | |
2070 | |
2047 | |
2071 | unless ($self->{deny_activate}) { |
2048 | unless ($self->{deny_activate}) { |
2072 | $self->decay_objects; |
2049 | $self->decay_objects; |
2073 | $self->fix_auto_apply; |
2050 | $self->fix_auto_apply; |
2074 | $self->update_buttons; |
2051 | $self->update_buttons; |
2075 | cf::cede_to_tick; |
|
|
2076 | $self->set_darkness_map; |
|
|
2077 | cf::cede_to_tick; |
2052 | cf::cede_to_tick; |
2078 | $self->activate; |
2053 | $self->activate; |
2079 | } |
2054 | } |
2080 | |
2055 | |
2081 | $self->{last_save} = $cf::RUNTIME; |
2056 | $self->{last_save} = $cf::RUNTIME; |
… | |
… | |
2423 | |
2398 | |
2424 | our $SAY_CHANNEL = { |
2399 | our $SAY_CHANNEL = { |
2425 | id => "say", |
2400 | id => "say", |
2426 | title => "Map", |
2401 | title => "Map", |
2427 | reply => "say ", |
2402 | reply => "say ", |
2428 | tooltip => "Things said to and replied from npcs near you and other players on the same map only.", |
2403 | tooltip => "Things said to and replied from NPCs near you and other players on the same map only.", |
2429 | }; |
2404 | }; |
2430 | |
2405 | |
2431 | our $CHAT_CHANNEL = { |
2406 | our $CHAT_CHANNEL = { |
2432 | id => "chat", |
2407 | id => "chat", |
2433 | title => "Chat", |
2408 | title => "Chat", |
… | |
… | |
2808 | id => "infobox", |
2783 | id => "infobox", |
2809 | title => "Body Parts", |
2784 | title => "Body Parts", |
2810 | reply => undef, |
2785 | reply => undef, |
2811 | tooltip => "Shows which body parts you posess and are available", |
2786 | tooltip => "Shows which body parts you posess and are available", |
2812 | }, |
2787 | }, |
|
|
2788 | "c/statistics" => { |
|
|
2789 | id => "infobox", |
|
|
2790 | title => "Statistics", |
|
|
2791 | reply => undef, |
|
|
2792 | tooltip => "Shows your primary statistics", |
|
|
2793 | }, |
2813 | "c/skills" => { |
2794 | "c/skills" => { |
2814 | id => "infobox", |
2795 | id => "infobox", |
2815 | title => "Skills", |
2796 | title => "Skills", |
2816 | reply => undef, |
2797 | reply => undef, |
2817 | tooltip => "Shows your experience per skill and item power", |
2798 | tooltip => "Shows your experience per skill and item power", |
|
|
2799 | }, |
|
|
2800 | "c/shopitems" => { |
|
|
2801 | id => "infobox", |
|
|
2802 | title => "Shop Items", |
|
|
2803 | reply => undef, |
|
|
2804 | tooltip => "Shows the items currently for sale in this shop", |
|
|
2805 | }, |
|
|
2806 | "c/resistances" => { |
|
|
2807 | id => "infobox", |
|
|
2808 | title => "Resistances", |
|
|
2809 | reply => undef, |
|
|
2810 | tooltip => "Shows your resistances", |
|
|
2811 | }, |
|
|
2812 | "c/pets" => { |
|
|
2813 | id => "infobox", |
|
|
2814 | title => "Pets", |
|
|
2815 | reply => undef, |
|
|
2816 | tooltip => "Shows information abotu your pets/a specific pet", |
|
|
2817 | }, |
|
|
2818 | "c/perceiveself" => { |
|
|
2819 | id => "infobox", |
|
|
2820 | title => "Perceive Self", |
|
|
2821 | reply => undef, |
|
|
2822 | tooltip => "You gained detailed knowledge about yourself", |
2818 | }, |
2823 | }, |
2819 | "c/uptime" => { |
2824 | "c/uptime" => { |
2820 | id => "infobox", |
2825 | id => "infobox", |
2821 | title => "Uptime", |
2826 | title => "Uptime", |
2822 | reply => undef, |
2827 | reply => undef, |
… | |
… | |
2832 | id => "party", |
2837 | id => "party", |
2833 | title => "Party", |
2838 | title => "Party", |
2834 | reply => "gsay ", |
2839 | reply => "gsay ", |
2835 | tooltip => "Messages and chat related to your party", |
2840 | tooltip => "Messages and chat related to your party", |
2836 | }, |
2841 | }, |
|
|
2842 | "c/death" => { |
|
|
2843 | id => "death", |
|
|
2844 | title => "Death", |
|
|
2845 | reply => undef, |
|
|
2846 | tooltip => "Reason for and more info about your most recent death", |
|
|
2847 | }, |
|
|
2848 | "c/say" => $SAY_CHANNEL, |
|
|
2849 | "c/chat" => $CHAT_CHANNEL, |
2837 | ); |
2850 | ); |
2838 | |
2851 | |
2839 | sub cf::client::send_msg { |
2852 | sub cf::client::send_msg { |
2840 | my ($self, $channel, $msg, $color, @extra) = @_; |
2853 | my ($self, $channel, $msg, $color, @extra) = @_; |
2841 | |
2854 | |
… | |
… | |
2846 | |
2859 | |
2847 | # check predefined channels, for the benefit of C |
2860 | # check predefined channels, for the benefit of C |
2848 | if ($CHANNEL{$channel}) { |
2861 | if ($CHANNEL{$channel}) { |
2849 | $channel = $CHANNEL{$channel}; |
2862 | $channel = $CHANNEL{$channel}; |
2850 | |
2863 | |
2851 | $self->ext_msg (channel_info => $channel) |
2864 | $self->ext_msg (channel_info => $channel); |
2852 | if $self->can_msg; |
|
|
2853 | |
|
|
2854 | $channel = $channel->{id}; |
2865 | $channel = $channel->{id}; |
2855 | |
2866 | |
2856 | } elsif (ref $channel) { |
2867 | } elsif (ref $channel) { |
2857 | # send meta info to client, if not yet sent |
2868 | # send meta info to client, if not yet sent |
2858 | unless (exists $self->{channel}{$channel->{id}}) { |
2869 | unless (exists $self->{channel}{$channel->{id}}) { |
2859 | $self->{channel}{$channel->{id}} = $channel; |
2870 | $self->{channel}{$channel->{id}} = $channel; |
2860 | $self->ext_msg (channel_info => $channel) |
2871 | $self->ext_msg (channel_info => $channel); |
2861 | if $self->can_msg; |
|
|
2862 | } |
2872 | } |
2863 | |
2873 | |
2864 | $channel = $channel->{id}; |
2874 | $channel = $channel->{id}; |
2865 | } |
2875 | } |
2866 | |
2876 | |
2867 | return unless @extra || length $msg; |
2877 | return unless @extra || length $msg; |
2868 | |
2878 | |
2869 | if ($self->can_msg) { |
|
|
2870 | # default colour, mask it out |
2879 | # default colour, mask it out |
2871 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2880 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2872 | if $color & cf::NDI_DEF; |
2881 | if $color & cf::NDI_DEF; |
2873 | |
2882 | |
2874 | my $pkt = "msg " |
2883 | my $pkt = "msg " |
2875 | . $self->{json_coder}->encode ( |
2884 | . $self->{json_coder}->encode ( |
2876 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2885 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2877 | ); |
2886 | ); |
2878 | |
2887 | |
2879 | # try lzf for large packets |
2888 | # try lzf for large packets |
2880 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2889 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2881 | if 1024 <= length $pkt and $self->{can_lzf}; |
2890 | if 1024 <= length $pkt and $self->{can_lzf}; |
2882 | |
2891 | |
2883 | # split very large packets |
2892 | # split very large packets |
2884 | if (8192 < length $pkt and $self->{can_lzf}) { |
2893 | if (8192 < length $pkt and $self->{can_lzf}) { |
2885 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2894 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2886 | $pkt = "frag"; |
2895 | $pkt = "frag"; |
2887 | } |
2896 | } |
2888 | |
2897 | |
2889 | $self->send_packet ($pkt); |
2898 | $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 | } |
2899 | } |
2916 | |
2900 | |
2917 | =item $client->ext_msg ($type, @msg) |
2901 | =item $client->ext_msg ($type, @msg) |
2918 | |
2902 | |
2919 | Sends an ext event to the client. |
2903 | Sends an ext event to the client. |
… | |
… | |
3094 | The following functions and methods are available within a safe environment: |
3078 | The following functions and methods are available within a safe environment: |
3095 | |
3079 | |
3096 | cf::object |
3080 | cf::object |
3097 | contr pay_amount pay_player map x y force_find force_add destroy |
3081 | contr pay_amount pay_player map x y force_find force_add destroy |
3098 | insert remove name archname title slaying race decrease split |
3082 | insert remove name archname title slaying race decrease split |
|
|
3083 | value |
3099 | |
3084 | |
3100 | cf::object::player |
3085 | cf::object::player |
3101 | player |
3086 | player |
3102 | |
3087 | |
3103 | cf::player |
3088 | cf::player |
… | |
… | |
3109 | =cut |
3094 | =cut |
3110 | |
3095 | |
3111 | for ( |
3096 | for ( |
3112 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3097 | ["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 |
3098 | insert remove inv nrof name archname title slaying race |
3114 | decrease split destroy change_exp)], |
3099 | decrease split destroy change_exp value msg lore send_msg)], |
3115 | ["cf::object::player" => qw(player)], |
3100 | ["cf::object::player" => qw(player)], |
3116 | ["cf::player" => qw(peaceful)], |
3101 | ["cf::player" => qw(peaceful send_msg)], |
3117 | ["cf::map" => qw(trigger)], |
3102 | ["cf::map" => qw(trigger)], |
3118 | ) { |
3103 | ) { |
3119 | no strict 'refs'; |
3104 | no strict 'refs'; |
3120 | my ($pkg, @funs) = @$_; |
3105 | my ($pkg, @funs) = @$_; |
3121 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3106 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
… | |
… | |
3138 | my ($code, %vars) = @_; |
3123 | my ($code, %vars) = @_; |
3139 | |
3124 | |
3140 | my $qcode = $code; |
3125 | my $qcode = $code; |
3141 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3126 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3142 | $qcode =~ s/\n/\\n/g; |
3127 | $qcode =~ s/\n/\\n/g; |
|
|
3128 | |
|
|
3129 | %vars = (_dummy => 0) unless %vars; |
3143 | |
3130 | |
3144 | local $_; |
3131 | local $_; |
3145 | local @safe::cf::_safe_eval_args = values %vars; |
3132 | local @safe::cf::_safe_eval_args = values %vars; |
3146 | |
3133 | |
3147 | my $eval = |
3134 | my $eval = |
… | |
… | |
3417 | |
3404 | |
3418 | seek $fh, 0, 0; |
3405 | seek $fh, 0, 0; |
3419 | print $fh $$; |
3406 | print $fh $$; |
3420 | } |
3407 | } |
3421 | |
3408 | |
|
|
3409 | sub main_loop { |
|
|
3410 | warn "EV::loop starting\n"; |
|
|
3411 | if (1) { |
|
|
3412 | EV::loop; |
|
|
3413 | } |
|
|
3414 | warn "EV::loop returned\n"; |
|
|
3415 | goto &main_loop unless $REALLY_UNLOOP; |
|
|
3416 | } |
|
|
3417 | |
3422 | sub main { |
3418 | sub main { |
3423 | cf::init_globals; # initialise logging |
3419 | cf::init_globals; # initialise logging |
3424 | |
3420 | |
3425 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3421 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3426 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3422 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
… | |
… | |
3429 | |
3425 | |
3430 | cf::init_experience; |
3426 | cf::init_experience; |
3431 | cf::init_anim; |
3427 | cf::init_anim; |
3432 | cf::init_attackmess; |
3428 | cf::init_attackmess; |
3433 | cf::init_dynamic; |
3429 | cf::init_dynamic; |
3434 | cf::init_block; |
|
|
3435 | |
3430 | |
3436 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3431 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3437 | |
3432 | |
3438 | # we must not ever block the main coroutine |
3433 | # we must not ever block the main coroutine |
3439 | local $Coro::idle = sub { |
3434 | local $Coro::idle = sub { |
… | |
… | |
3470 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3465 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3471 | |
3466 | |
3472 | (pop @POST_INIT)->(0) while @POST_INIT; |
3467 | (pop @POST_INIT)->(0) while @POST_INIT; |
3473 | }; |
3468 | }; |
3474 | |
3469 | |
3475 | EV::loop; |
3470 | main_loop; |
3476 | } |
3471 | } |
3477 | |
3472 | |
3478 | ############################################################################# |
3473 | ############################################################################# |
3479 | # initialisation and cleanup |
3474 | # initialisation and cleanup |
3480 | |
3475 | |
… | |
… | |
3736 | clear_package "safe::$_" |
3731 | clear_package "safe::$_" |
3737 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3732 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3738 | |
3733 | |
3739 | warn "unloading cf.pm \"a bit\""; |
3734 | warn "unloading cf.pm \"a bit\""; |
3740 | delete $INC{"cf.pm"}; |
3735 | delete $INC{"cf.pm"}; |
3741 | delete $INC{"cf/pod.pm"}; |
3736 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3742 | |
3737 | |
3743 | # don't, removes xs symbols, too, |
3738 | # don't, removes xs symbols, too, |
3744 | # and global variables created in xs |
3739 | # and global variables created in xs |
3745 | #clear_package __PACKAGE__; |
3740 | #clear_package __PACKAGE__; |
3746 | |
3741 | |
… | |
… | |
3963 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3958 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3964 | } |
3959 | } |
3965 | } |
3960 | } |
3966 | |
3961 | |
3967 | # load additional modules |
3962 | # load additional modules |
3968 | use cf::pod; |
3963 | require "cf/$_.pm" for @EXTRA_MODULES; |
3969 | |
3964 | |
3970 | END { cf::emergency_save } |
3965 | END { cf::emergency_save } |
3971 | |
3966 | |
3972 | 1 |
3967 | 1 |
3973 | |
3968 | |