… | |
… | |
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; |
… | |
… | |
129 | our $LOADAVG; # same thing, but with alpha-smoothing |
136 | our $LOADAVG; # same thing, but with alpha-smoothing |
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; |
|
|
141 | |
|
|
142 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
|
|
143 | our $REALLY_UNLOOP; # never set to true, please :) |
134 | |
144 | |
135 | binmode STDOUT; |
145 | binmode STDOUT; |
136 | binmode STDERR; |
146 | binmode STDERR; |
137 | |
147 | |
138 | # read virtual server time, if available |
148 | # read virtual server time, if available |
139 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
149 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
140 | open my $fh, "<", $RUNTIMEFILE |
150 | open my $fh, "<", $RUNTIMEFILE |
141 | or die "unable to read $RUNTIMEFILE file: $!"; |
151 | or die "unable to read $RUNTIMEFILE file: $!"; |
142 | $RUNTIME = <$fh> + 0.; |
152 | $RUNTIME = <$fh> + 0.; |
143 | } |
153 | } |
|
|
154 | |
|
|
155 | eval "sub TICK() { $TICK } 1" or die; |
144 | |
156 | |
145 | mkdir $_ |
157 | mkdir $_ |
146 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
158 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
147 | |
159 | |
148 | our $EMERGENCY_POSITION; |
160 | our $EMERGENCY_POSITION; |
… | |
… | |
328 | Wait until the given lock is available. See cf::lock_acquire. |
340 | Wait until the given lock is available. See cf::lock_acquire. |
329 | |
341 | |
330 | =item my $lock = cf::lock_acquire $string |
342 | =item my $lock = cf::lock_acquire $string |
331 | |
343 | |
332 | 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 |
333 | 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, |
334 | for example when the coroutine gets canceled), the lock is automatically |
346 | for example when the coroutine gets canceled), the lock is automatically |
335 | returned. |
347 | returned. |
336 | |
348 | |
337 | 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 |
338 | deadlocked coro. |
350 | deadlocked coro. |
… | |
… | |
344 | |
356 | |
345 | 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. |
346 | |
358 | |
347 | =cut |
359 | =cut |
348 | |
360 | |
349 | our %LOCK; |
361 | our $LOCKS = new Coro::SemaphoreSet; |
350 | our %LOCKER;#d# |
|
|
351 | |
362 | |
352 | sub lock_wait($) { |
363 | sub lock_wait($) { |
353 | my ($key) = @_; |
364 | $LOCKS->wait ($_[0]); |
354 | |
|
|
355 | if ($LOCKER{$key} == $Coro::current) {#d# |
|
|
356 | Carp::cluck "lock_wait($key) for already-acquired lock";#d# |
|
|
357 | return;#d# |
|
|
358 | }#d# |
|
|
359 | |
|
|
360 | # wait for lock, if any |
|
|
361 | while ($LOCK{$key}) { |
|
|
362 | #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>"; |
|
|
363 | push @{ $LOCK{$key} }, $Coro::current; |
|
|
364 | Coro::schedule; |
|
|
365 | } |
|
|
366 | } |
365 | } |
367 | |
366 | |
368 | sub lock_acquire($) { |
367 | sub lock_acquire($) { |
369 | my ($key) = @_; |
368 | $LOCKS->guard ($_[0]) |
370 | |
|
|
371 | # wait, to be sure we are not locked |
|
|
372 | lock_wait $key; |
|
|
373 | |
|
|
374 | $LOCK{$key} = []; |
|
|
375 | $LOCKER{$key} = $Coro::current;#d# |
|
|
376 | |
|
|
377 | Coro::guard { |
|
|
378 | delete $LOCKER{$key};#d# |
|
|
379 | # wake up all waiters, to be on the safe side |
|
|
380 | $_->ready for @{ delete $LOCK{$key} }; |
|
|
381 | } |
|
|
382 | } |
369 | } |
383 | |
370 | |
384 | sub lock_active($) { |
371 | sub lock_active($) { |
385 | my ($key) = @_; |
372 | $LOCKS->count ($_[0]) < 1 |
386 | |
|
|
387 | ! ! $LOCK{$key} |
|
|
388 | } |
373 | } |
389 | |
374 | |
390 | sub freeze_mainloop { |
375 | sub freeze_mainloop { |
391 | tick_inhibit_inc; |
376 | tick_inhibit_inc; |
392 | |
377 | |
393 | Coro::guard \&tick_inhibit_dec; |
378 | &Guard::guard (\&tick_inhibit_dec); |
394 | } |
379 | } |
395 | |
380 | |
396 | =item cf::periodic $interval, $cb |
381 | =item cf::periodic $interval, $cb |
397 | |
382 | |
398 | 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 |
… | |
… | |
1170 | $decname, length $$rdata, scalar @$objs; |
1155 | $decname, length $$rdata, scalar @$objs; |
1171 | |
1156 | |
1172 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1157 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1173 | aio_chmod $fh, SAVE_MODE; |
1158 | aio_chmod $fh, SAVE_MODE; |
1174 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1159 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1175 | 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 | } |
1176 | aio_close $fh; |
1164 | aio_close $fh; |
1177 | |
1165 | |
1178 | if (@$objs) { |
1166 | if (@$objs) { |
1179 | 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) { |
1180 | aio_chmod $fh, SAVE_MODE; |
1168 | aio_chmod $fh, SAVE_MODE; |
1181 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1169 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1182 | aio_write $fh, 0, (length $data), $data, 0; |
1170 | aio_write $fh, 0, (length $data), $data, 0; |
1183 | 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 | } |
1184 | aio_close $fh; |
1175 | aio_close $fh; |
1185 | aio_rename "$filename.pst~", "$filename.pst"; |
1176 | aio_rename "$filename.pst~", "$filename.pst"; |
1186 | } |
1177 | } |
1187 | } else { |
1178 | } else { |
1188 | aio_unlink "$filename.pst"; |
1179 | aio_unlink "$filename.pst"; |
1189 | } |
1180 | } |
1190 | |
1181 | |
1191 | aio_rename "$filename~", $filename; |
1182 | aio_rename "$filename~", $filename; |
1192 | |
1183 | |
1193 | $filename =~ s%/[^/]+$%%; |
1184 | $filename =~ s%/[^/]+$%%; |
1194 | aio_pathsync $filename; |
1185 | aio_pathsync $filename if $cf::USE_FSYNC; |
1195 | } else { |
1186 | } else { |
1196 | warn "FATAL: $filename~: $!\n"; |
1187 | warn "unable to save objects: $filename~: $!\n"; |
1197 | } |
1188 | } |
1198 | } else { |
1189 | } else { |
1199 | aio_unlink $filename; |
1190 | aio_unlink $filename; |
1200 | aio_unlink "$filename.pst"; |
1191 | aio_unlink "$filename.pst"; |
1201 | } |
1192 | } |
… | |
… | |
1292 | my ($name, $cb) = @_; |
1283 | my ($name, $cb) = @_; |
1293 | |
1284 | |
1294 | $EXTICMD{$name} = $cb; |
1285 | $EXTICMD{$name} = $cb; |
1295 | } |
1286 | } |
1296 | |
1287 | |
|
|
1288 | use File::Glob (); |
|
|
1289 | |
1297 | cf::player->attach ( |
1290 | cf::player->attach ( |
1298 | on_command => sub { |
1291 | on_command => sub { |
1299 | my ($pl, $name, $params) = @_; |
1292 | my ($pl, $name, $params) = @_; |
1300 | |
1293 | |
1301 | my $cb = $COMMAND{$name} |
1294 | my $cb = $COMMAND{$name} |
… | |
… | |
1332 | } |
1325 | } |
1333 | |
1326 | |
1334 | cf::override; |
1327 | cf::override; |
1335 | }, |
1328 | }, |
1336 | ); |
1329 | ); |
|
|
1330 | |
|
|
1331 | # "readahead" all extensions |
|
|
1332 | sub cache_extensions { |
|
|
1333 | my $grp = IO::AIO::aio_group; |
|
|
1334 | |
|
|
1335 | add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub { |
|
|
1336 | for (grep /\.ext$/, @{$_[0]}) { |
|
|
1337 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
|
|
1338 | } |
|
|
1339 | }; |
|
|
1340 | |
|
|
1341 | $grp |
|
|
1342 | } |
1337 | |
1343 | |
1338 | sub load_extensions { |
1344 | sub load_extensions { |
1339 | cf::sync_job { |
1345 | cf::sync_job { |
1340 | my %todo; |
1346 | my %todo; |
1341 | |
1347 | |
… | |
… | |
1870 | |
1876 | |
1871 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1877 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1872 | "$UNIQUEDIR/$path" |
1878 | "$UNIQUEDIR/$path" |
1873 | } |
1879 | } |
1874 | |
1880 | |
1875 | # and all this just because we cannot iterate over |
|
|
1876 | # all maps in C++... |
|
|
1877 | sub change_all_map_light { |
|
|
1878 | my ($change) = @_; |
|
|
1879 | |
|
|
1880 | $_->change_map_light ($change) |
|
|
1881 | for grep $_->outdoor, values %cf::MAP; |
|
|
1882 | } |
|
|
1883 | |
|
|
1884 | sub decay_objects { |
1881 | sub decay_objects { |
1885 | my ($self) = @_; |
1882 | my ($self) = @_; |
1886 | |
1883 | |
1887 | return if $self->{deny_reset}; |
1884 | return if $self->{deny_reset}; |
1888 | |
1885 | |
… | |
… | |
1970 | sub find { |
1967 | sub find { |
1971 | my ($path, $origin) = @_; |
1968 | my ($path, $origin) = @_; |
1972 | |
1969 | |
1973 | $path = normalise $path, $origin && $origin->path; |
1970 | $path = normalise $path, $origin && $origin->path; |
1974 | |
1971 | |
1975 | cf::lock_wait "map_data:$path";#d#remove |
1972 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
1976 | cf::lock_wait "map_find:$path"; |
1973 | my $guard2 = cf::lock_acquire "map_find:$path"; |
1977 | |
1974 | |
1978 | $cf::MAP{$path} || do { |
1975 | $cf::MAP{$path} || do { |
1979 | my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it |
|
|
1980 | my $guard2 = cf::lock_acquire "map_find:$path"; |
|
|
1981 | |
|
|
1982 | my $map = new_from_path cf::map $path |
1976 | my $map = new_from_path cf::map $path |
1983 | or return; |
1977 | or return; |
1984 | |
1978 | |
1985 | $map->{last_save} = $cf::RUNTIME; |
1979 | $map->{last_save} = $cf::RUNTIME; |
1986 | |
1980 | |
… | |
… | |
2053 | |
2047 | |
2054 | unless ($self->{deny_activate}) { |
2048 | unless ($self->{deny_activate}) { |
2055 | $self->decay_objects; |
2049 | $self->decay_objects; |
2056 | $self->fix_auto_apply; |
2050 | $self->fix_auto_apply; |
2057 | $self->update_buttons; |
2051 | $self->update_buttons; |
2058 | cf::cede_to_tick; |
|
|
2059 | $self->set_darkness_map; |
|
|
2060 | cf::cede_to_tick; |
2052 | cf::cede_to_tick; |
2061 | $self->activate; |
2053 | $self->activate; |
2062 | } |
2054 | } |
2063 | |
2055 | |
2064 | $self->{last_save} = $cf::RUNTIME; |
2056 | $self->{last_save} = $cf::RUNTIME; |
… | |
… | |
2406 | |
2398 | |
2407 | our $SAY_CHANNEL = { |
2399 | our $SAY_CHANNEL = { |
2408 | id => "say", |
2400 | id => "say", |
2409 | title => "Map", |
2401 | title => "Map", |
2410 | reply => "say ", |
2402 | reply => "say ", |
2411 | 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.", |
2412 | }; |
2404 | }; |
2413 | |
2405 | |
2414 | our $CHAT_CHANNEL = { |
2406 | our $CHAT_CHANNEL = { |
2415 | id => "chat", |
2407 | id => "chat", |
2416 | title => "Chat", |
2408 | title => "Chat", |
… | |
… | |
2791 | id => "infobox", |
2783 | id => "infobox", |
2792 | title => "Body Parts", |
2784 | title => "Body Parts", |
2793 | reply => undef, |
2785 | reply => undef, |
2794 | tooltip => "Shows which body parts you posess and are available", |
2786 | tooltip => "Shows which body parts you posess and are available", |
2795 | }, |
2787 | }, |
|
|
2788 | "c/statistics" => { |
|
|
2789 | id => "infobox", |
|
|
2790 | title => "Statistics", |
|
|
2791 | reply => undef, |
|
|
2792 | tooltip => "Shows your primary statistics", |
|
|
2793 | }, |
2796 | "c/skills" => { |
2794 | "c/skills" => { |
2797 | id => "infobox", |
2795 | id => "infobox", |
2798 | title => "Skills", |
2796 | title => "Skills", |
2799 | reply => undef, |
2797 | reply => undef, |
2800 | 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", |
2801 | }, |
2823 | }, |
2802 | "c/uptime" => { |
2824 | "c/uptime" => { |
2803 | id => "infobox", |
2825 | id => "infobox", |
2804 | title => "Uptime", |
2826 | title => "Uptime", |
2805 | reply => undef, |
2827 | reply => undef, |
… | |
… | |
2815 | id => "party", |
2837 | id => "party", |
2816 | title => "Party", |
2838 | title => "Party", |
2817 | reply => "gsay ", |
2839 | reply => "gsay ", |
2818 | tooltip => "Messages and chat related to your party", |
2840 | tooltip => "Messages and chat related to your party", |
2819 | }, |
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, |
2820 | ); |
2850 | ); |
2821 | |
2851 | |
2822 | sub cf::client::send_msg { |
2852 | sub cf::client::send_msg { |
2823 | my ($self, $channel, $msg, $color, @extra) = @_; |
2853 | my ($self, $channel, $msg, $color, @extra) = @_; |
2824 | |
2854 | |
… | |
… | |
2829 | |
2859 | |
2830 | # check predefined channels, for the benefit of C |
2860 | # check predefined channels, for the benefit of C |
2831 | if ($CHANNEL{$channel}) { |
2861 | if ($CHANNEL{$channel}) { |
2832 | $channel = $CHANNEL{$channel}; |
2862 | $channel = $CHANNEL{$channel}; |
2833 | |
2863 | |
2834 | $self->ext_msg (channel_info => $channel) |
2864 | $self->ext_msg (channel_info => $channel); |
2835 | if $self->can_msg; |
|
|
2836 | |
|
|
2837 | $channel = $channel->{id}; |
2865 | $channel = $channel->{id}; |
2838 | |
2866 | |
2839 | } elsif (ref $channel) { |
2867 | } elsif (ref $channel) { |
2840 | # send meta info to client, if not yet sent |
2868 | # send meta info to client, if not yet sent |
2841 | unless (exists $self->{channel}{$channel->{id}}) { |
2869 | unless (exists $self->{channel}{$channel->{id}}) { |
2842 | $self->{channel}{$channel->{id}} = $channel; |
2870 | $self->{channel}{$channel->{id}} = $channel; |
2843 | $self->ext_msg (channel_info => $channel) |
2871 | $self->ext_msg (channel_info => $channel); |
2844 | if $self->can_msg; |
|
|
2845 | } |
2872 | } |
2846 | |
2873 | |
2847 | $channel = $channel->{id}; |
2874 | $channel = $channel->{id}; |
2848 | } |
2875 | } |
2849 | |
2876 | |
2850 | return unless @extra || length $msg; |
2877 | return unless @extra || length $msg; |
2851 | |
2878 | |
2852 | if ($self->can_msg) { |
|
|
2853 | # default colour, mask it out |
2879 | # default colour, mask it out |
2854 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2880 | $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) |
2855 | if $color & cf::NDI_DEF; |
2881 | if $color & cf::NDI_DEF; |
2856 | |
2882 | |
2857 | my $pkt = "msg " |
2883 | my $pkt = "msg " |
2858 | . $self->{json_coder}->encode ( |
2884 | . $self->{json_coder}->encode ( |
2859 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2885 | [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] |
2860 | ); |
2886 | ); |
2861 | |
2887 | |
2862 | # try lzf for large packets |
2888 | # try lzf for large packets |
2863 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2889 | $pkt = "lzf " . Compress::LZF::compress $pkt |
2864 | if 1024 <= length $pkt and $self->{can_lzf}; |
2890 | if 1024 <= length $pkt and $self->{can_lzf}; |
2865 | |
2891 | |
2866 | # split very large packets |
2892 | # split very large packets |
2867 | if (8192 < length $pkt and $self->{can_lzf}) { |
2893 | if (8192 < length $pkt and $self->{can_lzf}) { |
2868 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2894 | $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt; |
2869 | $pkt = "frag"; |
2895 | $pkt = "frag"; |
2870 | } |
2896 | } |
2871 | |
2897 | |
2872 | $self->send_packet ($pkt); |
2898 | $self->send_packet ($pkt); |
2873 | } else { |
|
|
2874 | if ($color >= 0) { |
|
|
2875 | # replace some tags by gcfclient-compatible ones |
|
|
2876 | for ($msg) { |
|
|
2877 | 1 while |
|
|
2878 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
|
|
2879 | || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/ |
|
|
2880 | || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/ |
|
|
2881 | || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ |
|
|
2882 | || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/; |
|
|
2883 | } |
|
|
2884 | |
|
|
2885 | $color &= cf::NDI_COLOR_MASK; |
|
|
2886 | |
|
|
2887 | utf8::encode $msg; |
|
|
2888 | |
|
|
2889 | if (0 && $msg =~ /\[/) { |
|
|
2890 | # COMMAND/INFO |
|
|
2891 | $self->send_packet ("drawextinfo $color 10 8 $msg") |
|
|
2892 | } else { |
|
|
2893 | $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g; |
|
|
2894 | $self->send_packet ("drawinfo $color $msg") |
|
|
2895 | } |
|
|
2896 | } |
|
|
2897 | } |
|
|
2898 | } |
2899 | } |
2899 | |
2900 | |
2900 | =item $client->ext_msg ($type, @msg) |
2901 | =item $client->ext_msg ($type, @msg) |
2901 | |
2902 | |
2902 | Sends an ext event to the client. |
2903 | Sends an ext event to the client. |
… | |
… | |
3077 | The following functions and methods are available within a safe environment: |
3078 | The following functions and methods are available within a safe environment: |
3078 | |
3079 | |
3079 | cf::object |
3080 | cf::object |
3080 | 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 |
3081 | insert remove name archname title slaying race decrease split |
3082 | insert remove name archname title slaying race decrease split |
|
|
3083 | value |
3082 | |
3084 | |
3083 | cf::object::player |
3085 | cf::object::player |
3084 | player |
3086 | player |
3085 | |
3087 | |
3086 | cf::player |
3088 | cf::player |
… | |
… | |
3092 | =cut |
3094 | =cut |
3093 | |
3095 | |
3094 | for ( |
3096 | for ( |
3095 | ["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 |
3096 | insert remove inv nrof name archname title slaying race |
3098 | insert remove inv nrof name archname title slaying race |
3097 | decrease split destroy change_exp)], |
3099 | decrease split destroy change_exp value msg lore send_msg)], |
3098 | ["cf::object::player" => qw(player)], |
3100 | ["cf::object::player" => qw(player)], |
3099 | ["cf::player" => qw(peaceful)], |
3101 | ["cf::player" => qw(peaceful send_msg)], |
3100 | ["cf::map" => qw(trigger)], |
3102 | ["cf::map" => qw(trigger)], |
3101 | ) { |
3103 | ) { |
3102 | no strict 'refs'; |
3104 | no strict 'refs'; |
3103 | my ($pkg, @funs) = @$_; |
3105 | my ($pkg, @funs) = @$_; |
3104 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3106 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
… | |
… | |
3121 | my ($code, %vars) = @_; |
3123 | my ($code, %vars) = @_; |
3122 | |
3124 | |
3123 | my $qcode = $code; |
3125 | my $qcode = $code; |
3124 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3126 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3125 | $qcode =~ s/\n/\\n/g; |
3127 | $qcode =~ s/\n/\\n/g; |
|
|
3128 | |
|
|
3129 | %vars = (_dummy => 0) unless %vars; |
3126 | |
3130 | |
3127 | local $_; |
3131 | local $_; |
3128 | local @safe::cf::_safe_eval_args = values %vars; |
3132 | local @safe::cf::_safe_eval_args = values %vars; |
3129 | |
3133 | |
3130 | my $eval = |
3134 | my $eval = |
… | |
… | |
3400 | |
3404 | |
3401 | seek $fh, 0, 0; |
3405 | seek $fh, 0, 0; |
3402 | print $fh $$; |
3406 | print $fh $$; |
3403 | } |
3407 | } |
3404 | |
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 | |
3405 | sub main { |
3418 | sub main { |
3406 | cf::init_globals; # initialise logging |
3419 | cf::init_globals; # initialise logging |
3407 | |
3420 | |
3408 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3421 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3409 | 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."; |
… | |
… | |
3412 | |
3425 | |
3413 | cf::init_experience; |
3426 | cf::init_experience; |
3414 | cf::init_anim; |
3427 | cf::init_anim; |
3415 | cf::init_attackmess; |
3428 | cf::init_attackmess; |
3416 | cf::init_dynamic; |
3429 | cf::init_dynamic; |
3417 | cf::init_block; |
|
|
3418 | |
3430 | |
3419 | $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 |
3420 | |
3432 | |
3421 | # we must not ever block the main coroutine |
3433 | # we must not ever block the main coroutine |
3422 | local $Coro::idle = sub { |
3434 | local $Coro::idle = sub { |
… | |
… | |
3453 | 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}; |
3454 | |
3466 | |
3455 | (pop @POST_INIT)->(0) while @POST_INIT; |
3467 | (pop @POST_INIT)->(0) while @POST_INIT; |
3456 | }; |
3468 | }; |
3457 | |
3469 | |
3458 | EV::loop; |
3470 | main_loop; |
3459 | } |
3471 | } |
3460 | |
3472 | |
3461 | ############################################################################# |
3473 | ############################################################################# |
3462 | # initialisation and cleanup |
3474 | # initialisation and cleanup |
3463 | |
3475 | |
… | |
… | |
3558 | } |
3570 | } |
3559 | |
3571 | |
3560 | sub emergency_save() { |
3572 | sub emergency_save() { |
3561 | my $freeze_guard = cf::freeze_mainloop; |
3573 | my $freeze_guard = cf::freeze_mainloop; |
3562 | |
3574 | |
3563 | warn "enter emergency perl save\n"; |
3575 | warn "emergency_perl_save: enter\n"; |
3564 | |
3576 | |
3565 | cf::sync_job { |
3577 | cf::sync_job { |
|
|
3578 | # this is a trade-off: we want to be very quick here, so |
|
|
3579 | # save all maps without fsync, and later call a global sync |
|
|
3580 | # (which in turn might be very very slow) |
|
|
3581 | local $USE_FSYNC = 0; |
|
|
3582 | |
3566 | # use a peculiar iteration method to avoid tripping on perl |
3583 | # use a peculiar iteration method to avoid tripping on perl |
3567 | # refcount bugs in for. also avoids problems with players |
3584 | # refcount bugs in for. also avoids problems with players |
3568 | # and maps saved/destroyed asynchronously. |
3585 | # and maps saved/destroyed asynchronously. |
3569 | warn "begin emergency player save\n"; |
3586 | warn "emergency_perl_save: begin player save\n"; |
3570 | for my $login (keys %cf::PLAYER) { |
3587 | for my $login (keys %cf::PLAYER) { |
3571 | my $pl = $cf::PLAYER{$login} or next; |
3588 | my $pl = $cf::PLAYER{$login} or next; |
3572 | $pl->valid or next; |
3589 | $pl->valid or next; |
3573 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3590 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3574 | $pl->save; |
3591 | $pl->save; |
3575 | } |
3592 | } |
3576 | warn "end emergency player save\n"; |
3593 | warn "emergency_perl_save: end player save\n"; |
3577 | |
3594 | |
3578 | warn "begin emergency map save\n"; |
3595 | warn "emergency_perl_save: begin map save\n"; |
3579 | for my $path (keys %cf::MAP) { |
3596 | for my $path (keys %cf::MAP) { |
3580 | my $map = $cf::MAP{$path} or next; |
3597 | my $map = $cf::MAP{$path} or next; |
3581 | $map->valid or next; |
3598 | $map->valid or next; |
3582 | $map->save; |
3599 | $map->save; |
3583 | } |
3600 | } |
3584 | warn "end emergency map save\n"; |
3601 | warn "emergency_perl_save: end map save\n"; |
3585 | |
3602 | |
3586 | warn "begin emergency database checkpoint\n"; |
3603 | warn "emergency_perl_save: begin database checkpoint\n"; |
3587 | BDB::db_env_txn_checkpoint $DB_ENV; |
3604 | BDB::db_env_txn_checkpoint $DB_ENV; |
3588 | warn "end emergency database checkpoint\n"; |
3605 | warn "emergency_perl_save: end database checkpoint\n"; |
3589 | |
3606 | |
3590 | warn "begin write uuid\n"; |
3607 | warn "emergency_perl_save: begin write uuid\n"; |
3591 | write_uuid_sync 1; |
3608 | write_uuid_sync 1; |
3592 | warn "end write uuid\n"; |
3609 | warn "emergency_perl_save: end write uuid\n"; |
3593 | }; |
3610 | }; |
3594 | |
3611 | |
|
|
3612 | warn "emergency_perl_save: starting sync()\n"; |
|
|
3613 | IO::AIO::aio_sync sub { |
|
|
3614 | warn "emergency_perl_save: finished sync()\n"; |
|
|
3615 | }; |
|
|
3616 | |
3595 | warn "leave emergency perl save\n"; |
3617 | warn "emergency_perl_save: leave\n"; |
3596 | } |
3618 | } |
3597 | |
3619 | |
3598 | sub post_cleanup { |
3620 | sub post_cleanup { |
3599 | my ($make_core) = @_; |
3621 | my ($make_core) = @_; |
3600 | |
3622 | |
… | |
… | |
3637 | warn "can only reload from main coroutine"; |
3659 | warn "can only reload from main coroutine"; |
3638 | return; |
3660 | return; |
3639 | } |
3661 | } |
3640 | |
3662 | |
3641 | return if $RELOAD++; |
3663 | return if $RELOAD++; |
|
|
3664 | |
|
|
3665 | my $t1 = EV::time; |
3642 | |
3666 | |
3643 | while ($RELOAD) { |
3667 | while ($RELOAD) { |
3644 | warn "reloading..."; |
3668 | warn "reloading..."; |
3645 | |
3669 | |
3646 | warn "entering sync_job"; |
3670 | warn "entering sync_job"; |
… | |
… | |
3707 | clear_package "safe::$_" |
3731 | clear_package "safe::$_" |
3708 | 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); |
3709 | |
3733 | |
3710 | warn "unloading cf.pm \"a bit\""; |
3734 | warn "unloading cf.pm \"a bit\""; |
3711 | delete $INC{"cf.pm"}; |
3735 | delete $INC{"cf.pm"}; |
3712 | delete $INC{"cf/pod.pm"}; |
3736 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3713 | |
3737 | |
3714 | # don't, removes xs symbols, too, |
3738 | # don't, removes xs symbols, too, |
3715 | # and global variables created in xs |
3739 | # and global variables created in xs |
3716 | #clear_package __PACKAGE__; |
3740 | #clear_package __PACKAGE__; |
3717 | |
3741 | |
… | |
… | |
3725 | cf::reload_config; |
3749 | cf::reload_config; |
3726 | |
3750 | |
3727 | warn "loading extensions"; |
3751 | warn "loading extensions"; |
3728 | cf::load_extensions; |
3752 | cf::load_extensions; |
3729 | |
3753 | |
|
|
3754 | if ($REATTACH_ON_RELOAD) { |
3730 | warn "reattaching attachments to objects/players"; |
3755 | warn "reattaching attachments to objects/players"; |
3731 | _global_reattach; # objects, sockets |
3756 | _global_reattach; # objects, sockets |
3732 | warn "reattaching attachments to maps"; |
3757 | warn "reattaching attachments to maps"; |
3733 | reattach $_ for values %MAP; |
3758 | reattach $_ for values %MAP; |
3734 | warn "reattaching attachments to players"; |
3759 | warn "reattaching attachments to players"; |
3735 | reattach $_ for values %PLAYER; |
3760 | reattach $_ for values %PLAYER; |
|
|
3761 | } |
3736 | |
3762 | |
3737 | warn "running post_load"; |
3763 | warn "running post_init jobs"; |
3738 | (pop @POST_INIT)->(1) while @POST_INIT; |
3764 | (pop @POST_INIT)->(1) while @POST_INIT; |
3739 | |
3765 | |
3740 | warn "leaving sync_job"; |
3766 | warn "leaving sync_job"; |
3741 | |
3767 | |
3742 | 1 |
3768 | 1 |
… | |
… | |
3746 | }; |
3772 | }; |
3747 | |
3773 | |
3748 | warn "reloaded"; |
3774 | warn "reloaded"; |
3749 | --$RELOAD; |
3775 | --$RELOAD; |
3750 | } |
3776 | } |
|
|
3777 | |
|
|
3778 | $t1 = EV::time - $t1; |
|
|
3779 | warn "reload completed in ${t1}s\n"; |
3751 | }; |
3780 | }; |
3752 | |
3781 | |
3753 | our $RELOAD_WATCHER; # used only during reload |
3782 | our $RELOAD_WATCHER; # used only during reload |
3754 | |
3783 | |
3755 | sub reload_perl() { |
3784 | sub reload_perl() { |
3756 | # doing reload synchronously and two reloads happen back-to-back, |
3785 | # doing reload synchronously and two reloads happen back-to-back, |
3757 | # coro crashes during coro_state_free->destroy here. |
3786 | # coro crashes during coro_state_free->destroy here. |
3758 | |
3787 | |
|
|
3788 | $RELOAD_WATCHER ||= cf::async { |
|
|
3789 | Coro::AIO::aio_wait cache_extensions; |
|
|
3790 | |
3759 | $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub { |
3791 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3760 | do_reload_perl; |
3792 | do_reload_perl; |
3761 | undef $RELOAD_WATCHER; |
3793 | undef $RELOAD_WATCHER; |
|
|
3794 | }; |
3762 | }; |
3795 | }; |
3763 | } |
3796 | } |
3764 | |
3797 | |
3765 | register_command "reload" => sub { |
3798 | register_command "reload" => sub { |
3766 | my ($who, $arg) = @_; |
3799 | my ($who, $arg) = @_; |
… | |
… | |
3925 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3958 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3926 | } |
3959 | } |
3927 | } |
3960 | } |
3928 | |
3961 | |
3929 | # load additional modules |
3962 | # load additional modules |
3930 | use cf::pod; |
3963 | require "cf/$_.pm" for @EXTRA_MODULES; |
3931 | |
3964 | |
3932 | END { cf::emergency_save } |
3965 | END { cf::emergency_save } |
3933 | |
3966 | |
3934 | 1 |
3967 | 1 |
3935 | |
3968 | |