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.134 by root, Thu Jan 4 17:28:49 2007 UTC vs.
Revision 1.137 by root, Fri Jan 5 10:23:05 2007 UTC

179sub to_json($) { 179sub to_json($) {
180 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs 180 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
181 JSON::Syck::Dump $_[0] 181 JSON::Syck::Dump $_[0]
182} 182}
183 183
184=item my $guard = cf::guard { BLOCK }
185
186Run the given callback when the guard object gets destroyed (useful for
187coroutine cancellations).
188
189You can call C<< ->cancel >> on the guard object to stop the block from
190being executed.
191
192=cut
193
194sub guard(&) {
195 bless \(my $cb = $_[0]), cf::guard::;
196}
197
198sub cf::guard::cancel {
199 ${$_[0]} = sub { };
200}
201
202sub cf::guard::DESTROY {
203 ${$_[0]}->();
204}
205
206=item cf::lock_wait $string 184=item cf::lock_wait $string
207 185
208Wait until the given lock is available. See cf::lock_acquire. 186Wait until the given lock is available. See cf::lock_acquire.
209 187
210=item my $lock = cf::lock_acquire $string 188=item my $lock = cf::lock_acquire $string
211 189
212Wait until the given lock is available and then acquires it and returns 190Wait until the given lock is available and then acquires it and returns
213a guard object. If the guard object gets destroyed (goes out of scope, 191a Coro::guard object. If the guard object gets destroyed (goes out of scope,
214for example when the coroutine gets canceled), the lock is automatically 192for example when the coroutine gets canceled), the lock is automatically
215returned. 193returned.
216 194
217Lock names should begin with a unique identifier (for example, cf::map::find 195Lock names should begin with a unique identifier (for example, cf::map::find
218uses map_find and cf::map::load uses map_load). 196uses map_find and cf::map::load uses map_load).
237 # wait, to be sure we are not locked 215 # wait, to be sure we are not locked
238 lock_wait $key; 216 lock_wait $key;
239 217
240 $LOCK{$key} = []; 218 $LOCK{$key} = [];
241 219
242 cf::guard { 220 Coro::guard {
243 # wake up all waiters, to be on the safe side 221 # wake up all waiters, to be on the safe side
244 $_->ready for @{ delete $LOCK{$key} }; 222 $_->ready for @{ delete $LOCK{$key} };
245 } 223 }
246} 224}
247 225
262} 240}
263 241
264sub freeze_mainloop { 242sub freeze_mainloop {
265 return unless $TICK_WATCHER->is_active; 243 return unless $TICK_WATCHER->is_active;
266 244
267 my $guard = guard { $TICK_WATCHER->start }; 245 my $guard = Coro::guard { $TICK_WATCHER->start };
268 $TICK_WATCHER->stop; 246 $TICK_WATCHER->stop;
269 $guard 247 $guard
270} 248}
271 249
272=item cf::sync_job { BLOCK } 250=item cf::sync_job { BLOCK }
1184 or return; 1162 or return;
1185 1163
1186 my $map = cf::map::new 1164 my $map = cf::map::new
1187 or return; 1165 or return;
1188 1166
1167 # for better error messages only, will be overwritten
1168 $map->path ($path);
1169
1189 $map->load_header ($path) 1170 $map->load_header ($path)
1190 or return; 1171 or return;
1191 1172
1192 $map->{load_path} = $path; 1173 $map->{load_path} = $path;
1193 1174
1315} 1296}
1316 1297
1317sub save { 1298sub save {
1318 my ($self) = @_; 1299 my ($self) = @_;
1319 1300
1301 my $lock = cf::lock_acquire "map_data:" . $self->path;
1302
1320 $self->{last_save} = $cf::RUNTIME; 1303 $self->{last_save} = $cf::RUNTIME;
1321 1304
1322 return unless $self->dirty; 1305 return unless $self->dirty;
1323 1306
1324 my $save = $self->{path}->save_path; utf8::encode $save; 1307 my $save = $self->{path}->save_path; utf8::encode $save;
1342 my ($self) = @_; 1325 my ($self) = @_;
1343 1326
1344 # save first because save cedes 1327 # save first because save cedes
1345 $self->save; 1328 $self->save;
1346 1329
1330 my $lock = cf::lock_acquire "map_data:" . $self->path;
1331
1347 return if $self->players; 1332 return if $self->players;
1348 return if $self->in_memory != cf::MAP_IN_MEMORY; 1333 return if $self->in_memory != cf::MAP_IN_MEMORY;
1349 return if $self->{deny_save}; 1334 return if $self->{deny_save};
1350 1335
1351 $self->clear; 1336 $self->clear;
1392 $self->save; 1377 $self->save;
1393} 1378}
1394 1379
1395sub reset { 1380sub reset {
1396 my ($self) = @_; 1381 my ($self) = @_;
1382
1383 my $lock = cf::lock_acquire "map_data:" . $self->path;
1397 1384
1398 return if $self->players; 1385 return if $self->players;
1399 return if $self->{path}{user_rel};#d# 1386 return if $self->{path}{user_rel};#d#
1400 1387
1401 warn "resetting map ", $self->path;#d# 1388 warn "resetting map ", $self->path;#d#
1578 }; 1565 };
1579 } 1566 }
1580 }, 1567 },
1581); 1568);
1582 1569
1583=item $player_object->goto_map ($path, $x, $y) 1570=item $player_object->goto ($path, $x, $y)
1584 1571
1585=cut 1572=cut
1586 1573
1587sub cf::object::player::goto_map { 1574sub cf::object::player::goto {
1588 my ($self, $path, $x, $y) = @_; 1575 my ($self, $path, $x, $y) = @_;
1589 1576
1590 $self->enter_link; 1577 $self->enter_link;
1591 1578
1592 (cf::async { 1579 (cf::async {
1666 unless (eval { 1653 unless (eval {
1667 prepare_random_map $exit 1654 prepare_random_map $exit
1668 if $exit->slaying eq "/!"; 1655 if $exit->slaying eq "/!";
1669 1656
1670 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1657 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
1671 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); 1658 $self->goto ($path, $exit->stats->hp, $exit->stats->sp);
1672 1659
1673 1; 1660 1;
1674 }) { 1661 }) {
1675 $self->message ("Something went wrong deep within the crossfire server. " 1662 $self->message ("Something went wrong deep within the crossfire server. "
1676 . "I'll try to bring you back to the map you were before. " 1663 . "I'll try to bring you back to the map you were before. "

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines