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.31 by elmex, Mon Aug 14 04:22:04 2006 UTC vs.
Revision 1.38 by root, Thu Aug 24 17:29:30 2006 UTC

5use Storable; 5use Storable;
6use Opcode; 6use Opcode;
7use Safe; 7use Safe;
8use Safe::Hole; 8use Safe::Hole;
9 9
10use Time::HiRes;
10use Event; 11use Event;
11$Event::Eval = 1; # no idea why this is required, but it is 12$Event::Eval = 1; # no idea why this is required, but it is
12 13
13use strict; 14use strict;
14 15
15our %COMMAND; 16our %COMMAND;
16our @EVENT; 17our @EVENT;
18our @PLUGIN_EVENT;
17our %PROP_TYPE; 19our %PROP_TYPE;
18our %PROP_IDX; 20our %PROP_IDX;
19our $LIBDIR = maps_directory "perl"; 21our $LIBDIR = maps_directory "perl";
20 22
23our $TICK = MAX_TIME * 1e-6;
24our $TICK_WATCHER;
25our $NEXT_TICK;
26
21BEGIN { 27BEGIN {
22 @EVENT = map lc, @EVENT; 28 @PLUGIN_EVENT = map lc, @PLUGIN_EVENT;
23 29
24 *CORE::GLOBAL::warn = sub { 30 *CORE::GLOBAL::warn = sub {
25 my $msg = join "", @_; 31 my $msg = join "", @_;
26 $msg .= "\n" 32 $msg .= "\n"
27 unless $msg =~ /\n$/; 33 unless $msg =~ /\n$/;
136} 142}
137 143
138sub register { 144sub register {
139 my ($base, $pkg) = @_; 145 my ($base, $pkg) = @_;
140 146
141 for my $idx (0 .. $#EVENT) { 147 for my $idx (0 .. $#PLUGIN_EVENT) {
142 if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { 148 if (my $ref = $pkg->can ("on_$PLUGIN_EVENT[$idx]")) {
143 #warn "registering $EVENT[$idx] hook to '$pkg'\n"; 149 #warn "registering $PLUGIN_EVENT[$idx] hook to '$pkg'\n";
144 $hook[$idx]{$base} = $ref; 150 $hook[$idx]{$base} = $ref;
145 } 151 }
146 } 152 }
147} 153}
148 154
182 my ($pkg) = @_; 188 my ($pkg) = @_;
183 189
184 warn "removing extension $pkg\n"; 190 warn "removing extension $pkg\n";
185 191
186 # remove hooks 192 # remove hooks
187 for my $idx (0 .. $#EVENT) { 193 for my $idx (0 .. $#PLUGIN_EVENT) {
188 delete $hook[$idx]{$pkg}; 194 delete $hook[$idx]{$pkg};
189 } 195 }
190 196
191 # remove commands 197 # remove commands
192 for my $name (keys %command) { 198 for my $name (keys %command) {
226 1 232 1
227 } or warn "$ext not loaded: $@"; 233 } or warn "$ext not loaded: $@";
228 } 234 }
229} 235}
230 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
231register_command "perl-reload", 0, sub { 298register_command "perl-reload", 0, sub {
232 my ($who, $arg) = @_; 299 my ($who, $arg) = @_;
233 300
234 if ($who->flag (FLAG_WIZ)) { 301 if ($who->flag (FLAG_WIZ)) {
235 $who->message ("reloading..."); 302 _perl_reload {
236 303 warn $_[0];
237 warn "reloading...\n"; 304 $who->message ($_[0]);
238 eval {
239 # 1. cancel all watchers
240 $_->cancel for Event::all_watchers;
241
242 # 2. unload all extensions
243 for (@exts) {
244 $who->message ("unloading <$_>");
245 unload_extension $_;
246 }
247
248 # 3. unload all modules loaded from $LIBDIR
249 while (my ($k, $v) = each %INC) {
250 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
251
252 $who->message ("removing <$k>");
253 delete $INC{$k};
254
255 $k =~ s/\.pm$//;
256 $k =~ s/\//::/g;
257
258 if (my $cb = $k->can ("unload_module")) {
259 $cb->();
260 }
261
262 Symbol::delete_package $k;
263 }
264
265 # 4. get rid of ext::, as good as possible
266 Symbol::delete_package "ext::$_"
267 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
268
269 # 5. remove register_script_function callbacks
270 # TODO
271
272 # 6. unload cf.pm "a bit"
273 delete $INC{"cf.pm"};
274
275 # don't, removes xs symbols, too
276 #Symbol::delete_package __PACKAGE__;
277
278 # 7. reload cf.pm
279 $who->message ("reloading cf.pm");
280 require cf;
281 }; 305 };
282 warn $@ if $@;
283 $who->message ($@) if $@;
284 warn "reloaded\n";
285
286 $who->message ("reloaded");
287 } else {
288 $who->message ("Intruder Alert!");
289 } 306 }
290}; 307};
291 308
292############################################################################# 309#############################################################################
293# utility functions 310# utility functions
384}; 401};
385 402
386############################################################################# 403#############################################################################
387# load/save perl data associated with player->ob objects 404# load/save perl data associated with player->ob objects
388 405
406sub all_objects(@) {
407 @_, map all_objects ($_->inv), @_
408}
409
389*on_player_load = sub { 410*on_player_load = sub {
390 my ($ob, $path) = @_; 411 my ($ob, $path) = @_;
391 412
392 for my $o ($ob, $ob->inv) { 413 for my $o (all_objects $ob) {
393 if (my $value = $o->get_ob_key_value ("_perl_data")) { 414 if (my $value = $o->get_ob_key_value ("_perl_data")) {
394 $o->set_ob_key_value ("_perl_data"); 415 $o->set_ob_key_value ("_perl_data");
395 416
396 %$o = %{ Storable::thaw pack "H*", $value }; 417 %$o = %{ Storable::thaw pack "H*", $value };
397 } 418 }
400 421
401*on_player_save = sub { 422*on_player_save = sub {
402 my ($ob, $path) = @_; 423 my ($ob, $path) = @_;
403 424
404 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 425 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
405 for grep %$_, $ob, $ob->inv; 426 for grep %$_, all_objects $ob;
406}; 427};
407 428
408############################################################################# 429#############################################################################
409# core extensions - in perl 430# core extensions - in perl
410 431
510 no strict 'refs'; 531 no strict 'refs';
511 *{"ext::$fun"} = $safe_hole->wrap ($cb); 532 *{"ext::$fun"} = $safe_hole->wrap ($cb);
512} 533}
513 534
514############################################################################# 535#############################################################################
536# the server's main()
537
538sub run {
539 Event::loop;
540}
541
542#############################################################################
515# initialisation 543# initialisation
516 544
517register "<global>", __PACKAGE__; 545register "<global>", __PACKAGE__;
518 546
519unshift @INC, $LIBDIR; 547unshift @INC, $LIBDIR;
520 548
521load_extensions; 549load_extensions;
522 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
5231 5681
524 569

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines