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.32 by root, Tue Aug 15 18:07:25 2006 UTC vs.
Revision 1.38 by root, Thu Aug 24 17:29:30 2006 UTC

13 13
14use strict; 14use strict;
15 15
16our %COMMAND; 16our %COMMAND;
17our @EVENT; 17our @EVENT;
18our @PLUGIN_EVENT;
18our %PROP_TYPE; 19our %PROP_TYPE;
19our %PROP_IDX; 20our %PROP_IDX;
20our $LIBDIR = maps_directory "perl"; 21our $LIBDIR = maps_directory "perl";
21 22
23our $TICK = MAX_TIME * 1e-6;
24our $TICK_WATCHER;
25our $NEXT_TICK;
26
22BEGIN { 27BEGIN {
23 @EVENT = map lc, @EVENT; 28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
24 29
25 *CORE::GLOBAL::warn = sub { 30 *CORE::GLOBAL::warn = sub {
26 my $msg = join "", @_; 31 my $msg = join "", @_;
27 $msg .= "\n" 32 $msg .= "\n"
28 unless $msg =~ /\n$/; 33 unless $msg =~ /\n$/;
137} 142}
138 143
139sub register { 144sub register {
140 my ($base, $pkg) = @_; 145 my ($base, $pkg) = @_;
141 146
142 for my $idx (0 .. $#EVENT) { 147 for my $idx (0 .. $#PLUGIN_EVENT) {
143 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 148 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
144 #warn "registering $EVENT[$idx] hook to '$pkg'\n"; 149 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
145 $hook[$idx]{$base} = $ref; 150 $hook[$idx]{$base} = $ref;
146 } 151 }
147 } 152 }
148} 153}
149 154
183 my ($pkg) = @_; 188 my ($pkg) = @_;
184 189
185 warn "removing extension $pkg\n"; 190 warn "removing extension $pkg\n";
186 191
187 # remove hooks 192 # remove hooks
188 for my $idx (0 .. $#EVENT) { 193 for my $idx (0 .. $#PLUGIN_EVENT) {
189 delete $hook[$idx]{$pkg}; 194 delete $hook[$idx]{$pkg};
190 } 195 }
191 196
192 # remove commands 197 # remove commands
193 for my $name (keys %command) { 198 for my $name (keys %command) {
227 1 232 1
228 } or warn "$ext not loaded: $@"; 233 } or warn "$ext not loaded: $@";
229 } 234 }
230} 235}
231 236
237sub _perl_reload(&) {
238 my ($msg) = @_;
239
240 $msg->("reloading...");
241
242 eval {
243 # 1. cancel all watchers
244 $_->cancel for Event::all_watchers;
245
246 # 2. unload all extensions
247 for (@exts) {
248 $msg->("unloading <$_>");
249 unload_extension $_;
250 }
251
252 # 3. unload all modules loaded from $LIBDIR
253 while (my ($k, $v) = each %INC) {
254 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
255
256 $msg->("removing <$k>");
257 delete $INC{$k};
258
259 $k =~ s/\.pm$//;
260 $k =~ s/\//::/g;
261
262 if (my $cb = $k->can ("unload_module")) {
263 $cb->();
264 }
265
266 Symbol::delete_package $k;
267 }
268
269 # 4. get rid of ext::, as good as possible
270 Symbol::delete_package "ext::$_"
271 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
272
273 # 5. remove register_script_function callbacks
274 # TODO
275
276 # 6. unload cf.pm "a bit"
277 delete $INC{"cf.pm"};
278
279 # don't, removes xs symbols, too
280 #Symbol::delete_package __PACKAGE__;
281
282 # 7. reload cf.pm
283 $msg->("reloading cf.pm");
284 require cf;
285 };
286 $msg->($@) if $@;
287
288 $msg->("reloaded");
289};
290
291sub perl_reload() {
292 _perl_reload {
293 warn $_[0];
294 print "$_[0]\n";
295 };
296}
297
232register_command "perl-reload", 0, sub { 298register_command "perl-reload", 0, sub {
233 my ($who, $arg) = @_; 299 my ($who, $arg) = @_;
234 300
235 if ($who->flag (FLAG_WIZ)) { 301 if ($who->flag (FLAG_WIZ)) {
236 $who->message ("reloading..."); 302 _perl_reload {
237 303 warn $_[0];
238 warn "reloading...\n"; 304 $who->message ($_[0]);
239 eval {
240 # 1. cancel all watchers
241 $_->cancel for Event::all_watchers;
242
243 # 2. unload all extensions
244 for (@exts) {
245 $who->message ("unloading <$_>");
246 unload_extension $_;
247 }
248
249 # 3. unload all modules loaded from $LIBDIR
250 while (my ($k, $v) = each %INC) {
251 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
252
253 $who->message ("removing <$k>");
254 delete $INC{$k};
255
256 $k =~ s/\.pm$//;
257 $k =~ s/\//::/g;
258
259 if (my $cb = $k->can ("unload_module")) {
260 $cb->();
261 }
262
263 Symbol::delete_package $k;
264 }
265
266 # 4. get rid of ext::, as good as possible
267 Symbol::delete_package "ext::$_"
268 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
269
270 # 5. remove register_script_function callbacks
271 # TODO
272
273 # 6. unload cf.pm "a bit"
274 delete $INC{"cf.pm"};
275
276 # don't, removes xs symbols, too
277 #Symbol::delete_package __PACKAGE__;
278
279 # 7. reload cf.pm
280 $who->message ("reloading cf.pm");
281 require cf;
282 }; 305 };
283 warn $@ if $@;
284 $who->message ($@) if $@;
285 warn "reloaded\n";
286
287 $who->message ("reloaded");
288 } else {
289 $who->message ("Intruder Alert!");
290 } 306 }
291}; 307};
292 308
293############################################################################# 309#############################################################################
294# utility functions 310# utility functions
385}; 401};
386 402
387############################################################################# 403#############################################################################
388# load/save perl data associated with player->ob objects 404# load/save perl data associated with player->ob objects
389 405
406sub all_objects(@) {
407 @_, map all_objects ($_->inv), @_
408}
409
390*on_player_load = sub { 410*on_player_load = sub {
391 my ($ob, $path) = @_; 411 my ($ob, $path) = @_;
392 412
393 for my $o ($ob, $ob->inv) { 413 for my $o (all_objects $ob) {
394 if (my $value = $o->get_ob_key_value ("_perl_data")) { 414 if (my $value = $o->get_ob_key_value ("_perl_data")) {
395 $o->set_ob_key_value ("_perl_data"); 415 $o->set_ob_key_value ("_perl_data");
396 416
397 %$o = %{ Storable::thaw pack "H*", $value }; 417 %$o = %{ Storable::thaw pack "H*", $value };
398 } 418 }
401 421
402*on_player_save = sub { 422*on_player_save = sub {
403 my ($ob, $path) = @_; 423 my ($ob, $path) = @_;
404 424
405 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 425 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
406 for grep %$_, $ob, $ob->inv; 426 for grep %$_, all_objects $ob;
407}; 427};
408 428
409############################################################################# 429#############################################################################
410# core extensions - in perl 430# core extensions - in perl
411
412my $delta_timer = Event->timer (
413 parked => 1,
414 prio => Event::PRIO_HIGH,
415 cb => sub { Event::unloop (undef) },
416);
417
418sub sleep_delta($) {
419 $delta_timer->at (Event::time + $_[0]);
420 $delta_timer->start;
421 Event::loop;
422}
423 431
424=item cf::player::exists $login 432=item cf::player::exists $login
425 433
426Returns true when the given account exists. 434Returns true when the given account exists.
427 435
523 no strict 'refs'; 531 no strict 'refs';
524 *{"ext::$fun"} = $safe_hole->wrap ($cb); 532 *{"ext::$fun"} = $safe_hole->wrap ($cb);
525} 533}
526 534
527############################################################################# 535#############################################################################
536# the server's main()
537
538sub run {
539 Event::loop;
540}
541
542#############################################################################
528# initialisation 543# initialisation
529 544
530register "<global>", __PACKAGE__; 545register "<global>", __PACKAGE__;
531 546
532unshift @INC, $LIBDIR; 547unshift @INC, $LIBDIR;
533 548
534load_extensions; 549load_extensions;
535 550
551$TICK_WATCHER = Event->timer (
552 prio => 1,
553 at => $NEXT_TICK || 1,
554 cb => sub {
555 cf::server_tick; # one server iteration
556
557 my $NOW = Event::time;
558 $NEXT_TICK += $TICK;
559
560 # if we are delayed by four ticks, skip them all
561 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
562
563 $TICK_WATCHER->at ($NEXT_TICK);
564 $TICK_WATCHER->start;
565 },
566);
567
5361 5681
537 569

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines