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.35 by root, Thu Aug 24 13:35:48 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
22our $TICK = MAX_TIME * 1e-6; 23our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER; 24our $TICK_WATCHER;
24our $NEXT_TICK; 25our $NEXT_TICK;
25 26
26BEGIN { 27BEGIN {
27 @EVENT = map lc, @EVENT; 28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
28 29
29 *CORE::GLOBAL::warn = sub { 30 *CORE::GLOBAL::warn = sub {
30 my $msg = join "", @_; 31 my $msg = join "", @_;
31 $msg .= "\n" 32 $msg .= "\n"
32 unless $msg =~ /\n$/; 33 unless $msg =~ /\n$/;
141} 142}
142 143
143sub register { 144sub register {
144 my ($base, $pkg) = @_; 145 my ($base, $pkg) = @_;
145 146
146 for my $idx (0 .. $#EVENT) { 147 for my $idx (0 .. $#PLUGIN_EVENT) {
147 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 148 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
148 #warn "registering $EVENT[$idx] hook to '$pkg'\n"; 149 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
149 $hook[$idx]{$base} = $ref; 150 $hook[$idx]{$base} = $ref;
150 } 151 }
151 } 152 }
152} 153}
153 154
187 my ($pkg) = @_; 188 my ($pkg) = @_;
188 189
189 warn "removing extension $pkg\n"; 190 warn "removing extension $pkg\n";
190 191
191 # remove hooks 192 # remove hooks
192 for my $idx (0 .. $#EVENT) { 193 for my $idx (0 .. $#PLUGIN_EVENT) {
193 delete $hook[$idx]{$pkg}; 194 delete $hook[$idx]{$pkg};
194 } 195 }
195 196
196 # remove commands 197 # remove commands
197 for my $name (keys %command) { 198 for my $name (keys %command) {
231 1 232 1
232 } or warn "$ext not loaded: $@"; 233 } or warn "$ext not loaded: $@";
233 } 234 }
234} 235}
235 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
236register_command "perl-reload", 0, sub { 298register_command "perl-reload", 0, sub {
237 my ($who, $arg) = @_; 299 my ($who, $arg) = @_;
238 300
239 if ($who->flag (FLAG_WIZ)) { 301 if ($who->flag (FLAG_WIZ)) {
240 $who->message ("reloading..."); 302 _perl_reload {
241 303 warn $_[0];
242 warn "reloading...\n"; 304 $who->message ($_[0]);
243 eval {
244 # 1. cancel all watchers
245 $_->cancel for Event::all_watchers;
246
247 # 2. unload all extensions
248 for (@exts) {
249 $who->message ("unloading <$_>");
250 unload_extension $_;
251 }
252
253 # 3. unload all modules loaded from $LIBDIR
254 while (my ($k, $v) = each %INC) {
255 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
256
257 $who->message ("removing <$k>");
258 delete $INC{$k};
259
260 $k =~ s/\.pm$//;
261 $k =~ s/\//::/g;
262
263 if (my $cb = $k->can ("unload_module")) {
264 $cb->();
265 }
266
267 Symbol::delete_package $k;
268 }
269
270 # 4. get rid of ext::, as good as possible
271 Symbol::delete_package "ext::$_"
272 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
273
274 # 5. remove register_script_function callbacks
275 # TODO
276
277 # 6. unload cf.pm "a bit"
278 delete $INC{"cf.pm"};
279
280 # don't, removes xs symbols, too
281 #Symbol::delete_package __PACKAGE__;
282
283 # 7. reload cf.pm
284 $who->message ("reloading cf.pm");
285 require cf;
286 }; 305 };
287 warn $@ if $@;
288 $who->message ($@) if $@;
289 warn "reloaded\n";
290
291 $who->message ("reloaded");
292 } else {
293 $who->message ("Intruder Alert!");
294 } 306 }
295}; 307};
296 308
297############################################################################# 309#############################################################################
298# utility functions 310# utility functions
543 cf::server_tick; # one server iteration 555 cf::server_tick; # one server iteration
544 556
545 my $NOW = Event::time; 557 my $NOW = Event::time;
546 $NEXT_TICK += $TICK; 558 $NEXT_TICK += $TICK;
547 559
548 # if we are delayed by > 0.25 second, skip ticks 560 # if we are delayed by four ticks, skip them all
549 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + .25; 561 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
550 562
551 $TICK_WATCHER->at ($NEXT_TICK); 563 $TICK_WATCHER->at ($NEXT_TICK);
552 $TICK_WATCHER->start; 564 $TICK_WATCHER->start;
553 }, 565 },
554); 566);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines