… | |
… | |
54 | our %EXT_CORO = (); # coroutines bound to extensions |
54 | our %EXT_CORO = (); # coroutines bound to extensions |
55 | our %EXT_MAP = (); # pluggable maps |
55 | our %EXT_MAP = (); # pluggable maps |
56 | |
56 | |
57 | our $RELOAD; # number of reloads so far |
57 | our $RELOAD; # number of reloads so far |
58 | our @EVENT; |
58 | our @EVENT; |
59 | our $LIBDIR = datadir . "/ext"; |
59 | |
|
|
60 | our $CONFDIR = confdir; |
|
|
61 | our $DATADIR = datadir; |
|
|
62 | our $LIBDIR = "$DATADIR/ext"; |
|
|
63 | our $PODDIR = "$DATADIR/pod"; |
|
|
64 | our $MAPDIR = "$DATADIR/" . mapdir; |
|
|
65 | our $LOCALDIR = localdir; |
|
|
66 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
|
|
67 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
|
|
68 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
|
|
69 | our $RANDOMDIR = "$LOCALDIR/random"; |
|
|
70 | our $BDBDIR = "$LOCALDIR/db"; |
60 | |
71 | |
61 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
72 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
62 | our $TICK_WATCHER; |
73 | our $TICK_WATCHER; |
63 | our $AIO_POLL_WATCHER; |
74 | our $AIO_POLL_WATCHER; |
64 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
75 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
… | |
… | |
75 | our $RUNTIME; |
86 | our $RUNTIME; |
76 | |
87 | |
77 | our %PLAYER; # all users |
88 | our %PLAYER; # all users |
78 | our %MAP; # all maps |
89 | our %MAP; # all maps |
79 | our $LINK_MAP; # the special {link} map, which is always available |
90 | our $LINK_MAP; # the special {link} map, which is always available |
80 | our $RANDOM_MAPS = cf::localdir . "/random"; |
|
|
81 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
|
|
82 | |
91 | |
83 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
92 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
84 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
93 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
85 | |
94 | |
86 | binmode STDOUT; |
95 | binmode STDOUT; |
87 | binmode STDERR; |
96 | binmode STDERR; |
88 | |
97 | |
89 | # read virtual server time, if available |
98 | # read virtual server time, if available |
90 | unless ($RUNTIME || !-e cf::localdir . "/runtime") { |
99 | unless ($RUNTIME || !-e "$LOCALDIR/runtime") { |
91 | open my $fh, "<", cf::localdir . "/runtime" |
100 | open my $fh, "<", "$LOCALDIR/runtime" |
92 | or die "unable to read runtime file: $!"; |
101 | or die "unable to read runtime file: $!"; |
93 | $RUNTIME = <$fh> + 0.; |
102 | $RUNTIME = <$fh> + 0.; |
94 | } |
103 | } |
95 | |
104 | |
96 | mkdir cf::localdir; |
105 | mkdir $_ |
97 | mkdir cf::localdir . "/" . cf::playerdir; |
106 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
98 | mkdir cf::localdir . "/" . cf::tmpdir; |
|
|
99 | mkdir cf::localdir . "/" . cf::uniquedir; |
|
|
100 | mkdir $RANDOM_MAPS; |
|
|
101 | mkdir $BDB_ENV_DIR; |
|
|
102 | |
107 | |
103 | our $EMERGENCY_POSITION; |
108 | our $EMERGENCY_POSITION; |
104 | |
109 | |
105 | sub cf::map::normalise; |
110 | sub cf::map::normalise; |
106 | |
111 | |
… | |
… | |
117 | =item $cf::RUNTIME |
122 | =item $cf::RUNTIME |
118 | |
123 | |
119 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
124 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
120 | every server tick. |
125 | every server tick. |
121 | |
126 | |
122 | =item $cf::LIBDIR |
127 | =item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR |
|
|
128 | $cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR |
|
|
129 | $cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR |
123 | |
130 | |
124 | The perl library directory, where extensions and cf-specific modules can |
131 | Various directories - "/etc", read-only install directory, perl-library |
125 | be found. It will be added to C<@INC> automatically. |
132 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
|
|
133 | unique-items directory, player file directory, random maps directory and |
|
|
134 | database environment. |
126 | |
135 | |
127 | =item $cf::NOW |
136 | =item $cf::NOW |
128 | |
137 | |
129 | The time of the last (current) server tick. |
138 | The time of the last (current) server tick. |
130 | |
139 | |
… | |
… | |
234 | returned. |
243 | returned. |
235 | |
244 | |
236 | Lock names should begin with a unique identifier (for example, cf::map::find |
245 | Lock names should begin with a unique identifier (for example, cf::map::find |
237 | uses map_find and cf::map::load uses map_load). |
246 | uses map_find and cf::map::load uses map_load). |
238 | |
247 | |
|
|
248 | =item $locked = cf::lock_active $string |
|
|
249 | |
|
|
250 | Return true if the lock is currently active, i.e. somebody has locked it. |
|
|
251 | |
239 | =cut |
252 | =cut |
240 | |
253 | |
241 | our %LOCK; |
254 | our %LOCK; |
242 | |
255 | |
243 | sub lock_wait($) { |
256 | sub lock_wait($) { |
… | |
… | |
260 | |
273 | |
261 | Coro::guard { |
274 | Coro::guard { |
262 | # wake up all waiters, to be on the safe side |
275 | # wake up all waiters, to be on the safe side |
263 | $_->ready for @{ delete $LOCK{$key} }; |
276 | $_->ready for @{ delete $LOCK{$key} }; |
264 | } |
277 | } |
|
|
278 | } |
|
|
279 | |
|
|
280 | sub lock_active($) { |
|
|
281 | my ($key) = @_; |
|
|
282 | |
|
|
283 | ! ! $LOCK{$key} |
265 | } |
284 | } |
266 | |
285 | |
267 | sub freeze_mainloop { |
286 | sub freeze_mainloop { |
268 | return unless $TICK_WATCHER->is_active; |
287 | return unless $TICK_WATCHER->is_active; |
269 | |
288 | |
… | |
… | |
350 | } |
369 | } |
351 | |
370 | |
352 | sub write_runtime { |
371 | sub write_runtime { |
353 | my $guard = cf::lock_acquire "write_runtime"; |
372 | my $guard = cf::lock_acquire "write_runtime"; |
354 | |
373 | |
355 | my $runtime = cf::localdir . "/runtime"; |
374 | my $runtime = "$LOCALDIR/runtime"; |
356 | |
375 | |
357 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
376 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
358 | or return; |
377 | or return; |
359 | |
378 | |
360 | my $value = $cf::RUNTIME + 90 + 10; |
379 | my $value = $cf::RUNTIME + 90 + 10; |
… | |
… | |
955 | Returns the given player object, loading it if necessary (might block). |
974 | Returns the given player object, loading it if necessary (might block). |
956 | |
975 | |
957 | =cut |
976 | =cut |
958 | |
977 | |
959 | sub playerdir($) { |
978 | sub playerdir($) { |
960 | cf::localdir |
979 | "$PLAYERDIR/" |
961 | . "/" |
|
|
962 | . cf::playerdir |
|
|
963 | . "/" |
|
|
964 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
980 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
965 | } |
981 | } |
966 | |
982 | |
967 | sub path($) { |
983 | sub path($) { |
968 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
984 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
… | |
… | |
1088 | while and may block, so not sync_job-capable, ever. |
1104 | while and may block, so not sync_job-capable, ever. |
1089 | |
1105 | |
1090 | =cut |
1106 | =cut |
1091 | |
1107 | |
1092 | sub list_logins { |
1108 | sub list_logins { |
1093 | my $dirs = aio_readdir cf::localdir . "/" . cf::playerdir |
1109 | my $dirs = aio_readdir $PLAYERDIR |
1094 | or return []; |
1110 | or return []; |
1095 | |
1111 | |
1096 | my @logins; |
1112 | my @logins; |
1097 | |
1113 | |
1098 | for my $login (@$dirs) { |
1114 | for my $login (@$dirs) { |
… | |
… | |
1336 | |
1352 | |
1337 | # the original (read-only) location |
1353 | # the original (read-only) location |
1338 | sub load_path { |
1354 | sub load_path { |
1339 | my ($self) = @_; |
1355 | my ($self) = @_; |
1340 | |
1356 | |
1341 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1357 | "$MAPDIR($self->{path}" |
1342 | } |
1358 | } |
1343 | |
1359 | |
1344 | # the temporary/swap location |
1360 | # the temporary/swap location |
1345 | sub save_path { |
1361 | sub save_path { |
1346 | my ($self) = @_; |
1362 | my ($self) = @_; |
1347 | |
1363 | |
1348 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1364 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1349 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1365 | "$TMPDIR/$path" |
1350 | } |
1366 | } |
1351 | |
1367 | |
1352 | # the unique path, undef == no special unique path |
1368 | # the unique path, undef == no special unique path |
1353 | sub uniq_path { |
1369 | sub uniq_path { |
1354 | my ($self) = @_; |
1370 | my ($self) = @_; |
1355 | |
1371 | |
1356 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1372 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1357 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $path |
1373 | "$UNIQUEDIR/$path" |
1358 | } |
1374 | } |
1359 | |
1375 | |
1360 | # and all this just because we cannot iterate over |
1376 | # and all this just because we cannot iterate over |
1361 | # all maps in C++... |
1377 | # all maps in C++... |
1362 | sub change_all_map_light { |
1378 | sub change_all_map_light { |
… | |
… | |
1721 | instantiated unique items. May block. |
1737 | instantiated unique items. May block. |
1722 | |
1738 | |
1723 | =cut |
1739 | =cut |
1724 | |
1740 | |
1725 | sub unique_maps() { |
1741 | sub unique_maps() { |
1726 | my $files = aio_readdir cf::localdir . "/" . cf::uniquedir |
1742 | my $files = aio_readdir $UNIQUEDIR |
1727 | or return; |
1743 | or return; |
1728 | |
1744 | |
1729 | my @paths; |
1745 | my @paths; |
1730 | |
1746 | |
1731 | for (@$files) { |
1747 | for (@$files) { |
… | |
… | |
1987 | |
2003 | |
1988 | $rmp->{random_seed} ||= $exit->random_seed; |
2004 | $rmp->{random_seed} ||= $exit->random_seed; |
1989 | |
2005 | |
1990 | my $data = cf::to_json $rmp; |
2006 | my $data = cf::to_json $rmp; |
1991 | my $md5 = Digest::MD5::md5_hex $data; |
2007 | my $md5 = Digest::MD5::md5_hex $data; |
1992 | my $meta = "$cf::RANDOM_MAPS/$md5.meta"; |
2008 | my $meta = "$RANDOMDIR/$md5.meta"; |
1993 | |
2009 | |
1994 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2010 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
1995 | aio_write $fh, 0, (length $data), $data, 0; |
2011 | aio_write $fh, 0, (length $data), $data, 0; |
1996 | undef $fh; |
2012 | undef $fh; |
1997 | aio_rename "$meta~", $meta; |
2013 | aio_rename "$meta~", $meta; |
… | |
… | |
2373 | |
2389 | |
2374 | return db_get cache => "$id/data"; |
2390 | return db_get cache => "$id/data"; |
2375 | } |
2391 | } |
2376 | } |
2392 | } |
2377 | |
2393 | |
|
|
2394 | my $t1 = Time::HiRes::time; |
2378 | my $data = $process->(\@data); |
2395 | my $data = $process->(\@data); |
|
|
2396 | my $t2 = Time::HiRes::time; |
|
|
2397 | |
|
|
2398 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
2379 | |
2399 | |
2380 | db_put cache => "$id/data", $data; |
2400 | db_put cache => "$id/data", $data; |
2381 | db_put cache => "$id/md5" , $md5; |
2401 | db_put cache => "$id/md5" , $md5; |
2382 | db_put cache => "$id/meta", $meta; |
2402 | db_put cache => "$id/meta", $meta; |
2383 | |
2403 | |
… | |
… | |
2414 | |
2434 | |
2415 | die $$res unless "ARRAY" eq ref $res; |
2435 | die $$res unless "ARRAY" eq ref $res; |
2416 | |
2436 | |
2417 | return wantarray ? @$res : $res->[-1]; |
2437 | return wantarray ? @$res : $res->[-1]; |
2418 | } else { |
2438 | } else { |
|
|
2439 | local $SIG{__WARN__}; |
2419 | eval { |
2440 | eval { |
2420 | local $SIG{__DIE__}; |
2441 | local $SIG{__DIE__}; |
2421 | local $SIG{__WARN__}; |
|
|
2422 | close $fh1; |
2442 | close $fh1; |
2423 | |
2443 | |
2424 | my @res = eval { $cb->(@args) }; |
2444 | my @res = eval { $cb->(@args) }; |
2425 | syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); |
2445 | syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); |
2426 | }; |
2446 | }; |
2427 | |
2447 | |
|
|
2448 | warn $@ if $@; |
2428 | _exit 0; |
2449 | _exit 0; |
2429 | } |
2450 | } |
2430 | } |
2451 | } |
2431 | |
2452 | |
2432 | |
2453 | |
… | |
… | |
2484 | } |
2505 | } |
2485 | |
2506 | |
2486 | 1 |
2507 | 1 |
2487 | } |
2508 | } |
2488 | |
2509 | |
|
|
2510 | sub reload_regions { |
|
|
2511 | load_resource_file "$MAPDIR/regions" |
|
|
2512 | or die "unable to load regions file\n"; |
|
|
2513 | } |
|
|
2514 | |
2489 | sub reload_facedata { |
2515 | sub reload_facedata { |
2490 | load_facedata sprintf "%s/facedata", cf::datadir |
2516 | load_facedata "$DATADIR/facedata" |
2491 | or die "unable to load facedata\n"; |
2517 | or die "unable to load facedata\n"; |
2492 | } |
2518 | } |
2493 | |
2519 | |
2494 | sub reload_regions { |
|
|
2495 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
|
|
2496 | or die "unable to load regions file\n"; |
|
|
2497 | } |
|
|
2498 | |
|
|
2499 | sub reload_archetypes { |
2520 | sub reload_archetypes { |
2500 | load_resource_file sprintf "%s/archetypes", cf::datadir |
2521 | load_resource_file "$DATADIR/archetypes" |
2501 | or die "unable to load archetypes\n"; |
2522 | or die "unable to load archetypes\n"; |
2502 | } |
2523 | } |
2503 | |
2524 | |
2504 | sub reload_treasures { |
2525 | sub reload_treasures { |
2505 | load_resource_file sprintf "%s/treasures", cf::datadir |
2526 | load_resource_file "$DATADIR/treasures" |
2506 | or die "unable to load treasurelists\n"; |
2527 | or die "unable to load treasurelists\n"; |
2507 | } |
2528 | } |
2508 | |
2529 | |
2509 | sub reload_resources { |
2530 | sub reload_resources { |
2510 | warn "reloading resource files...\n"; |
2531 | warn "reloading resource files...\n"; |
… | |
… | |
2520 | sub init { |
2541 | sub init { |
2521 | reload_resources; |
2542 | reload_resources; |
2522 | } |
2543 | } |
2523 | |
2544 | |
2524 | sub cfg_load { |
2545 | sub cfg_load { |
2525 | open my $fh, "<:utf8", cf::confdir . "/config" |
2546 | open my $fh, "<:utf8", "$CONFDIR/config" |
2526 | or return; |
2547 | or return; |
2527 | |
2548 | |
2528 | local $/; |
2549 | local $/; |
2529 | *CFG = YAML::Syck::Load <$fh>; |
2550 | *CFG = YAML::Syck::Load <$fh>; |
2530 | |
2551 | |
… | |
… | |
2700 | Symbol::delete_package "safe::$_" |
2721 | Symbol::delete_package "safe::$_" |
2701 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2722 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2702 | |
2723 | |
2703 | warn "unloading cf.pm \"a bit\""; |
2724 | warn "unloading cf.pm \"a bit\""; |
2704 | delete $INC{"cf.pm"}; |
2725 | delete $INC{"cf.pm"}; |
|
|
2726 | delete $INC{"cf/pod.pm"}; |
2705 | |
2727 | |
2706 | # don't, removes xs symbols, too, |
2728 | # don't, removes xs symbols, too, |
2707 | # and global variables created in xs |
2729 | # and global variables created in xs |
2708 | #Symbol::delete_package __PACKAGE__; |
2730 | #Symbol::delete_package __PACKAGE__; |
2709 | |
2731 | |
… | |
… | |
2869 | |
2891 | |
2870 | cf::sync_job { |
2892 | cf::sync_job { |
2871 | eval { |
2893 | eval { |
2872 | BDB::db_env_open |
2894 | BDB::db_env_open |
2873 | $DB_ENV, |
2895 | $DB_ENV, |
2874 | $BDB_ENV_DIR, |
2896 | $BDBDIR, |
2875 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2897 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2876 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2898 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2877 | 0666; |
2899 | 0666; |
2878 | |
2900 | |
2879 | cf::cleanup "db_env_open($BDB_ENV_DIR): $!" if $!; |
2901 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
2880 | |
2902 | |
2881 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2903 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2882 | $DB_ENV->set_lk_detect; |
2904 | $DB_ENV->set_lk_detect; |
2883 | }; |
2905 | }; |
2884 | |
2906 | |