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.28 by root, Sun Jul 30 17:42:14 2006 UTC vs.
Revision 1.37 by root, Thu Aug 24 14:05:22 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
16our @EVENT; 17our @EVENT;
17our %PROP_TYPE; 18our %PROP_TYPE;
18our %PROP_IDX; 19our %PROP_IDX;
19our $LIBDIR = maps_directory "perl"; 20our $LIBDIR = maps_directory "perl";
20 21
22our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER;
24our $NEXT_TICK;
25
21BEGIN { 26BEGIN {
22 @EVENT = map lc, @EVENT; 27 @EVENT = map lc, @EVENT;
23 28
24 *CORE::GLOBAL::warn = sub { 29 *CORE::GLOBAL::warn = sub {
25 my $msg = join "", @_; 30 my $msg = join "", @_;
70@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 75@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
71@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; 76@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object';
72 77
73# we bless all objects into derived classes to force a method lookup 78# we bless all objects into derived classes to force a method lookup
74# within the Safe compartment. 79# within the Safe compartment.
75for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { 80for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region cf::arch)) {
76 no strict 'refs'; 81 no strict 'refs';
77 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 82 @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
78} 83}
79 84
80$Event::DIED = sub { 85$Event::DIED = sub {
180 185
181sub unload_extension { 186sub unload_extension {
182 my ($pkg) = @_; 187 my ($pkg) = @_;
183 188
184 warn "removing extension $pkg\n"; 189 warn "removing extension $pkg\n";
185
186 if (my $cb = $pkg->can ("on_unload")) {
187 $cb->($pkg);
188 }
189 190
190 # remove hooks 191 # remove hooks
191 for my $idx (0 .. $#EVENT) { 192 for my $idx (0 .. $#EVENT) {
192 delete $hook[$idx]{$pkg}; 193 delete $hook[$idx]{$pkg};
193 } 194 }
208 # remove extcmds 209 # remove extcmds
209 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { 210 for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) {
210 delete $extcmd{$name}; 211 delete $extcmd{$name};
211 } 212 }
212 213
214 if (my $cb = $pkg->can ("on_unload")) {
215 eval {
216 $cb->($pkg);
217 1
218 } or warn "$pkg unloaded, but with errors: $@";
219 }
220
213 Symbol::delete_package $pkg; 221 Symbol::delete_package $pkg;
214} 222}
215 223
216sub load_extensions { 224sub load_extensions {
217 my $LIBDIR = maps_directory "perl"; 225 my $LIBDIR = maps_directory "perl";
223 1 231 1
224 } or warn "$ext not loaded: $@"; 232 } or warn "$ext not loaded: $@";
225 } 233 }
226} 234}
227 235
236sub _perl_reload(&) {
237 my ($msg) = @_;
238
239 $msg->("reloading...");
240
241 eval {
242 # 1. cancel all watchers
243 $_->cancel for Event::all_watchers;
244
245 # 2. unload all extensions
246 for (@exts) {
247 $msg->("unloading <$_>");
248 unload_extension $_;
249 }
250
251 # 3. unload all modules loaded from $LIBDIR
252 while (my ($k, $v) = each %INC) {
253 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
254
255 $msg->("removing <$k>");
256 delete $INC{$k};
257
258 $k =~ s/\.pm$//;
259 $k =~ s/\//::/g;
260
261 if (my $cb = $k->can ("unload_module")) {
262 $cb->();
263 }
264
265 Symbol::delete_package $k;
266 }
267
268 # 4. get rid of ext::, as good as possible
269 Symbol::delete_package "ext::$_"
270 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
271
272 # 5. remove register_script_function callbacks
273 # TODO
274
275 # 6. unload cf.pm "a bit"
276 delete $INC{"cf.pm"};
277
278 # don't, removes xs symbols, too
279 #Symbol::delete_package __PACKAGE__;
280
281 # 7. reload cf.pm
282 $msg->("reloading cf.pm");
283 require cf;
284 };
285 $msg->($@) if $@;
286
287 $msg->("reloaded");
288};
289
290sub perl_reload() {
291 _perl_reload {
292 warn $_[0];
293 print "$_[0]\n";
294 };
295}
296
228register_command "perl-reload", 0, sub { 297register_command "perl-reload", 0, sub {
229 my ($who, $arg) = @_; 298 my ($who, $arg) = @_;
230 299
231 if ($who->flag (FLAG_WIZ)) { 300 if ($who->flag (FLAG_WIZ)) {
232 $who->message ("reloading..."); 301 _perl_reload {
233 302 warn $_[0];
234 warn "reloading...\n"; 303 $who->message ($_[0]);
235 eval {
236 # 1. cancel all watchers
237 $_->cancel for Event::all_watchers;
238
239 # 2. unload all extensions
240 for (@exts) {
241 $who->message ("unloading <$_>");
242 unload_extension $_;
243 }
244
245 # 3. unload all modules loaded from $LIBDIR
246 while (my ($k, $v) = each %INC) {
247 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
248
249 $who->message ("removing <$k>");
250 delete $INC{$k};
251
252 $k =~ s/\.pm$//;
253 $k =~ s/\//::/g;
254
255 if (my $cb = $k->can ("unload_module")) {
256 $cb->();
257 }
258
259 Symbol::delete_package $k;
260 }
261
262 # 4. get rid of ext::, as good as possible
263 Symbol::delete_package "ext::$_"
264 for qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region);
265
266 # 5. remove register_script_function callbacks
267 # TODO
268
269 # 6. unload cf.pm "a bit"
270 delete $INC{"cf.pm"};
271
272 # don't, removes xs symbols, too
273 #Symbol::delete_package __PACKAGE__;
274
275 # 7. reload cf.pm
276 $who->message ("reloading cf.pm");
277 require cf;
278 }; 304 };
279 warn $@ if $@;
280 $who->message ($@) if $@;
281 warn "reloaded\n";
282
283 $who->message ("reloaded");
284 } else {
285 $who->message ("Intruder Alert!");
286 } 305 }
287}; 306};
288 307
289############################################################################# 308#############################################################################
290# utility functions 309# utility functions
291 310
292use JSON::Syck (); # TODO# replace by JSON::PC once working 311use JSON::Syck (); # TODO# replace by JSON::PC once working
293 312
294$JSON::Syck::ImplicitUnicode = 1;
295
296sub from_json($) { 313sub from_json($) {
314 $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs
297 JSON::Syck::Load $_[0] 315 JSON::Syck::Load $_[0]
298} 316}
299 317
300sub to_json($) { 318sub to_json($) {
319 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
301 JSON::Syck::Dump $_[0] 320 JSON::Syck::Dump $_[0]
302} 321}
303 322
304############################################################################# 323#############################################################################
305# extcmd framework, basically convert ext <msg> 324# extcmd framework, basically convert ext <msg>
381}; 400};
382 401
383############################################################################# 402#############################################################################
384# load/save perl data associated with player->ob objects 403# load/save perl data associated with player->ob objects
385 404
405sub all_objects(@) {
406 @_, map all_objects ($_->inv), @_
407}
408
386*on_player_load = sub { 409*on_player_load = sub {
387 my ($ob, $path) = @_; 410 my ($ob, $path) = @_;
388 411
389 for my $o ($ob, $ob->inv) { 412 for my $o (all_objects $ob) {
390 if (my $value = $o->get_ob_key_value ("_perl_data")) { 413 if (my $value = $o->get_ob_key_value ("_perl_data")) {
391 $o->set_ob_key_value ("_perl_data"); 414 $o->set_ob_key_value ("_perl_data");
392 415
393 %$o = %{ Storable::thaw pack "H*", $value }; 416 %$o = %{ Storable::thaw pack "H*", $value };
394 } 417 }
397 420
398*on_player_save = sub { 421*on_player_save = sub {
399 my ($ob, $path) = @_; 422 my ($ob, $path) = @_;
400 423
401 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 424 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
402 for grep %$_, $ob, $ob->inv; 425 for grep %$_, all_objects $ob;
403}; 426};
404 427
405############################################################################# 428#############################################################################
406# core extensions - in perl 429# core extensions - in perl
407 430
507 no strict 'refs'; 530 no strict 'refs';
508 *{"ext::$fun"} = $safe_hole->wrap ($cb); 531 *{"ext::$fun"} = $safe_hole->wrap ($cb);
509} 532}
510 533
511############################################################################# 534#############################################################################
535# the server's main()
536
537sub run {
538 Event::loop;
539}
540
541#############################################################################
512# initialisation 542# initialisation
513 543
514register "<global>", __PACKAGE__; 544register "<global>", __PACKAGE__;
515 545
516unshift @INC, $LIBDIR; 546unshift @INC, $LIBDIR;
517 547
518load_extensions; 548load_extensions;
519 549
550$TICK_WATCHER = Event->timer (
551 prio => 1,
552 at => $NEXT_TICK || 1,
553 cb => sub {
554 cf::server_tick; # one server iteration
555
556 my $NOW = Event::time;
557 $NEXT_TICK += $TICK;
558
559 # if we are delayed by four ticks, skip them all
560 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
561
562 $TICK_WATCHER->at ($NEXT_TICK);
563 $TICK_WATCHER->start;
564 },
565);
566
5201 5671
521 568

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines