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.34 by root, Thu Aug 24 13:13:49 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
518 534
519############################################################################# 535#############################################################################
520# the server's main() 536# the server's main()
521 537
522sub run { 538sub run {
523 my $tick = MAX_TIME * 1e-6; 539 Event::loop;
524 my $next = Event::time; 540}
525 my $timer = Event->timer (at => $next, cb => sub { 541
542#############################################################################
543# initialisation
544
545register "<global>", __PACKAGE__;
546
547unshift @INC, $LIBDIR;
548
549load_extensions;
550
551$TICK_WATCHER = Event->timer (
552 prio => 1,
553 at => $NEXT_TICK || 1,
554 cb => sub {
526 cf::server_tick; # one server iteration 555 cf::server_tick; # one server iteration
527 556
528 $next += $tick;
529 my $NOW = Event::time; 557 my $NOW = Event::time;
558 $NEXT_TICK += $TICK;
530 559
531 # if we are delayd by > 0.25 second, skip ticks 560 # if we are delayed by four ticks, skip them all
532 $next = $NOW if $NOW >= $next + .25; 561 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
533 562
534 $_[0]->w->at ($next); 563 $TICK_WATCHER->at ($NEXT_TICK);
535 $_[0]->w->start; 564 $TICK_WATCHER->start;
536 }); 565 },
537 566);
538 Event::loop;
539}
540
541#############################################################################
542# initialisation
543
544register "<global>", __PACKAGE__;
545
546unshift @INC, $LIBDIR;
547
548load_extensions;
549 567
5501 5681
551 569

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines