ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.170 by root, Sun Jan 14 02:00:37 2007 UTC vs.
Revision 1.178 by root, Thu Jan 18 00:06:55 2007 UTC

8use Storable; 8use Storable;
9use Opcode; 9use Opcode;
10use Safe; 10use Safe;
11use Safe::Hole; 11use Safe::Hole;
12 12
13use Coro 3.3 (); 13use Coro 3.4 ();
14use Coro::Event; 14use Coro::Event;
15use Coro::Timer; 15use Coro::Timer;
16use Coro::Signal; 16use Coro::Signal;
17use Coro::Semaphore; 17use Coro::Semaphore;
18use Coro::AIO; 18use Coro::AIO;
1113 1113
1114use Fcntl; 1114use Fcntl;
1115use Coro::AIO; 1115use Coro::AIO;
1116 1116
1117use overload 1117use overload
1118 '""' => \&as_string; 1118 '""' => \&as_string,
1119 fallback => 1;
1119 1120
1120our $MAX_RESET = 3600; 1121our $MAX_RESET = 3600;
1121our $DEFAULT_RESET = 3000; 1122our $DEFAULT_RESET = 3000;
1122 1123
1123sub generate_random_map { 1124sub generate_random_map {
1124 my ($self, $rmp) = @_; 1125 my ($self, $rmp) = @_;
1125
1126 # mit "rum" bekleckern, nicht 1126 # mit "rum" bekleckern, nicht
1127 $self->_create_random_map ( 1127 $self->_create_random_map (
1128 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, 1128 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1129 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, 1129 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
1130 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, 1130 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1151 1151
1152# also paths starting with '/' 1152# also paths starting with '/'
1153$EXT_MAP{"cf::map"} = qr{^(?=/)}; 1153$EXT_MAP{"cf::map"} = qr{^(?=/)};
1154 1154
1155sub thawer_merge { 1155sub thawer_merge {
1156 my ($self, $merge) = @_;
1157
1156 # we have to keep some variables in memory intact 1158 # we have to keep some variables in memory intact
1157 local $_[0]{path}; 1159 local $self->{path};
1160 local $self->{load_path};
1158 local $_[0]{deny_save}; 1161 local $self->{deny_save};
1159 local $_[0]{deny_reset}; 1162 local $self->{deny_reset};
1160 1163
1161 $_[0]->SUPER::thawer_merge ($_[1]); 1164 $self->SUPER::thawer_merge ($merge);
1162} 1165}
1163 1166
1164sub normalise { 1167sub normalise {
1165 my ($path, $base) = @_; 1168 my ($path, $base) = @_;
1166 1169
1336 1339
1337 $map->load_header 1340 $map->load_header
1338 or return; 1341 or return;
1339 1342
1340 if ($map->should_reset) { 1343 if ($map->should_reset) {
1344 $cf::WAIT_FOR_TICK->wait;
1341 $map->reset; 1345 $map->reset;
1342 undef $guard; 1346 undef $guard;
1343 $map = find $path 1347 $map = find $path
1344 or return; 1348 or return;
1345 } 1349 }
1530 my ($self) = @_; 1534 my ($self) = @_;
1531 1535
1532 $self->reset_at <= $cf::RUNTIME 1536 $self->reset_at <= $cf::RUNTIME
1533} 1537}
1534 1538
1535sub rename {
1536 my ($self, $new_path) = @_;
1537
1538 normalise $new_path;
1539
1540 $self->unlink_save;
1541
1542 delete $cf::MAP{$self->path};
1543 $self->{path} = $new_path; $self->path ($self->{path});
1544 $cf::MAP{$self->path} = $self;
1545
1546 $self->save;
1547}
1548
1549sub reset { 1539sub reset {
1550 my ($self) = @_; 1540 my ($self) = @_;
1551 1541
1552 my $lock = cf::lock_acquire "map_data:$self->{path}"; 1542 my $lock = cf::lock_acquire "map_data:$self->{path}";
1553 1543
1570my $nuke_counter = "aaaa"; 1560my $nuke_counter = "aaaa";
1571 1561
1572sub nuke { 1562sub nuke {
1573 my ($self) = @_; 1563 my ($self) = @_;
1574 1564
1565 delete $cf::MAP{$self->path};
1566
1567 $self->unlink_save;
1568
1569 bless $self, "cf::map";
1570 delete $self->{deny_reset};
1575 $self->{deny_save} = 1; 1571 $self->{deny_save} = 1;
1576 $self->reset_timeout (1); 1572 $self->reset_timeout (1);
1577 $self->rename ("{nuke}/" . ($nuke_counter++)); 1573 $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++));
1574
1575 $cf::MAP{$self->path} = $self;
1576
1578 $self->reset; # polite request, might not happen 1577 $self->reset; # polite request, might not happen
1579} 1578}
1580 1579
1581=item cf::map::unique_maps 1580=item cf::map::unique_maps
1582 1581
1813 1812
1814sub parse_random_map_params { 1813sub parse_random_map_params {
1815 my ($spec) = @_; 1814 my ($spec) = @_;
1816 1815
1817 my $rmp = { # defaults 1816 my $rmp = { # defaults
1818 xsize => 10, 1817 xsize => -1,
1819 ysize => 10, 1818 ysize => -1,
1820 }; 1819 };
1821 1820
1822 for (split /\n/, $spec) { 1821 for (split /\n/, $spec) {
1823 my ($k, $v) = split /\s+/, $_, 2; 1822 my ($k, $v) = split /\s+/, $_, 2;
1824 1823
1846 1845
1847 $rmp->{random_seed} ||= $exit->random_seed; 1846 $rmp->{random_seed} ||= $exit->random_seed;
1848 1847
1849 my $data = cf::to_json $rmp; 1848 my $data = cf::to_json $rmp;
1850 my $md5 = Digest::MD5::md5_hex $data; 1849 my $md5 = Digest::MD5::md5_hex $data;
1850 my $meta = "$cf::RANDOM_MAPS/$md5.meta";
1851 1851
1852 if (my $fh = aio_open "$cf::RANDOM_MAPS/$md5.meta", O_WRONLY | O_CREAT, 0666) { 1852 if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) {
1853 aio_write $fh, 0, (length $data), $data, 0; 1853 aio_write $fh, 0, (length $data), $data, 0;
1854 undef $fh;
1855 aio_rename "$meta~", $meta;
1854 1856
1855 $exit->slaying ("?random/$md5"); 1857 $exit->slaying ("?random/$md5");
1856 $exit->msg (undef); 1858 $exit->msg (undef);
1857 } 1859 }
1858} 1860}
2228 2230
2229sub main { 2231sub main {
2230 # we must not ever block the main coroutine 2232 # we must not ever block the main coroutine
2231 local $Coro::idle = sub { 2233 local $Coro::idle = sub {
2232 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 2234 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
2235 (async {
2233 async { Event::one_event }; 2236 Event::one_event;
2237 })->prio (Coro::PRIO_MAX);
2234 }; 2238 };
2235 2239
2236 cfg_load; 2240 cfg_load;
2237 db_load; 2241 db_load;
2238 load_extensions; 2242 load_extensions;
2386 } 2390 }
2387 2391
2388 warn "reloaded"; 2392 warn "reloaded";
2389}; 2393};
2390 2394
2395our $RELOAD_WATCHER; # used only during reload
2396
2391register_command "reload" => sub { 2397register_command "reload" => sub {
2392 my ($who, $arg) = @_; 2398 my ($who, $arg) = @_;
2393 2399
2394 if ($who->flag (FLAG_WIZ)) { 2400 if ($who->flag (FLAG_WIZ)) {
2395 $who->message ("start of reload."); 2401 $who->message ("reloading server.");
2402
2403 # doing reload synchronously and two reloads happen back-to-back,
2404 # coro crashes during coro_state_free->destroy here.
2405
2406 $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub {
2396 reload; 2407 reload;
2397 $who->message ("end of reload."); 2408 undef $RELOAD_WATCHER;
2409 });
2398 } 2410 }
2399}; 2411};
2400 2412
2401unshift @INC, $LIBDIR; 2413unshift @INC, $LIBDIR;
2402 2414

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines