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.45 by root, Sat Aug 26 23:36:32 2006 UTC vs.
Revision 1.67 by root, Tue Sep 12 23:22:32 2006 UTC

11use Event; 11use Event;
12$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
13 13
14use strict; 14use strict;
15 15
16_init_vars;
17
16our %COMMAND = (); 18our %COMMAND = ();
17our @EVENT; 19our @EVENT;
18our %PROP_TYPE;
19our %PROP_IDX;
20our $LIBDIR = maps_directory "perl"; 20our $LIBDIR = maps_directory "perl";
21 21
22our $TICK = MAX_TIME * 1e-6; 22our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER; 23our $TICK_WATCHER;
24our $NEXT_TICK; 24our $NEXT_TICK;
32 print STDERR "cfperl: $msg"; 32 print STDERR "cfperl: $msg";
33 LOG llevError, "cfperl: $msg"; 33 LOG llevError, "cfperl: $msg";
34 }; 34 };
35} 35}
36 36
37my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply!
38
39# generate property mutators
40sub prop_gen {
41 my ($prefix, $class) = @_;
42
43 no strict 'refs';
44
45 for my $prop (keys %PROP_TYPE) {
46 $prop =~ /^\Q$prefix\E_(.*$)/ or next;
47 my $sub = lc $1;
48
49 my $type = $PROP_TYPE{$prop};
50 my $idx = $PROP_IDX {$prop};
51
52 *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub {
53 $_[0]->get_property ($type, $idx)
54 };
55
56 *{"$class\::set_$sub"} = sub {
57 $_[0]->set_property ($type, $idx, $_[1]);
58 } unless $ignore_set{$prop};
59 }
60}
61
62# auto-generate most of the API
63
64prop_gen OBJECT_PROP => "cf::object";
65# CFAPI_OBJECT_ANIMATION?
66prop_gen PLAYER_PROP => "cf::object::player";
67
68prop_gen MAP_PROP => "cf::map";
69prop_gen ARCH_PROP => "cf::arch";
70
71@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; 37@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object';
72 38
73# we bless all objects into (empty) derived classes to force a method lookup 39# we bless all objects into (empty) derived classes to force a method lookup
74# within the Safe compartment. 40# within the Safe compartment.
75for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { 41for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) {
76 no strict 'refs'; 42 no strict 'refs';
77 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 43 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
78} 44}
79 45
80$Event::DIED = sub { 46$Event::DIED = sub {
103} 69}
104 70
105############################################################################# 71#############################################################################
106# "new" plug-in system 72# "new" plug-in system
107 73
108=item cf::object::attach ... # NYI 74=head3 EVENTS AND OBJECT ATTACHMENTS
75
76=over 4
77
78=item $object->attach ($attachment, key => $value...)
79
80=item $object->detach ($attachment)
81
82Attach/detach a pre-registered attachment to an object.
83
84=item $player->attach ($attachment, key => $value...)
85
86=item $player->detach ($attachment)
87
88Attach/detach a pre-registered attachment to a player.
89
90=item $map->attach ($attachment, key => $value...)
91
92=item $map->detach ($attachment)
93
94Attach/detach a pre-registered attachment to a map.
95
96=item $bool = $object->attached ($name)
97
98=item $bool = $player->attached ($name)
99
100=item $bool = $map->attached ($name)
101
102Checks wether the named attachment is currently attached to the object.
109 103
110=item cf::attach_global ... 104=item cf::attach_global ...
111 105
106Attach handlers for global events.
107
108This and all following C<attach_*>-functions expect any number of the
109following handler/hook descriptions:
110
111=over 4
112
113=item prio => $number
114
115Set the priority for all following handlers/hooks (unless overwritten
116by another C<prio> setting). Lower priority handlers get executed
117earlier. The default priority is C<0>, and many built-in handlers are
118registered at priority C<-1000>, so lower priorities should not be used
119unless you know what you are doing.
120
121=item on_I<event> => \&cb
122
123Call the given code reference whenever the named event happens (event is
124something like C<instantiate>, C<apply>, C<use_skill> and so on, and which
125handlers are recognised generally depends on the type of object these
126handlers attach to).
127
128See F<include/eventinc.h> for the full list of events supported, and their
129class.
130
131=item package => package::
132
133Look for sub functions of the name C<< on_I<event> >> in the given
134package and register them. Only handlers for eevents supported by the
135object/class are recognised.
136
137=back
138
112=item cf::attach_to_type $object_type, ... 139=item cf::attach_to_type $object_type, $subtype, ...
140
141Attach handlers for a specific object type (e.g. TRANSPORT) and
142subtype. If C<$subtype> is zero or undef, matches all objects of the given
143type.
113 144
114=item cf::attach_to_objects ... 145=item cf::attach_to_objects ...
115 146
147Attach handlers to all objects. Do not use this except for debugging or
148very rare events, as handlers are (obviously) called for I<all> objects in
149the game.
150
116=item cf::attach_to_players ... 151=item cf::attach_to_players ...
117 152
153Attach handlers to all players.
154
118=item cf::attach_to_maps ... 155=item cf::attach_to_maps ...
119 156
157Attach handlers to all maps.
158
120=item cf:register_attachment $name, ... 159=item cf:register_attachment $name, ...
121 160
122 prio => $number, # lower is earlier 161Register an attachment by name through which objects can refer to this
123 on_xxx => \&cb, 162attachment.
124 package => package::, 163
164=item cf:register_player_attachment $name, ...
165
166Register an attachment by name through which players can refer to this
167attachment.
168
169=item cf:register_map_attachment $name, ...
170
171Register an attachment by name through which maps can refer to this
172attachment.
125 173
126=cut 174=cut
127 175
128# the following variables are defined in .xs and must not be re-created 176# the following variables are defined in .xs and must not be re-created
129our @CB_GLOBAL = (); # registry for all global events 177our @CB_GLOBAL = (); # registry for all global events
191 } 239 }
192 240
193 \%undo 241 \%undo
194} 242}
195 243
244sub _attach_attachment {
245 my ($obj, $name, %arg) = @_;
246
247 return if exists $obj->{_attachment}{$name};
248
249 my $res;
250
251 if (my $attach = $attachment{$name}) {
252 my $registry = $obj->registry;
253
254 for (@$attach) {
255 my ($klass, @attach) = @$_;
256 $res = _attach @$registry, $klass, @attach;
257 }
258
259 $obj->{$name} = \%arg;
260 } else {
261 warn "object uses attachment '$name' that is not available, postponing.\n";
262 }
263
264 $obj->{_attachment}{$name} = undef;
265
266 $res->{attachment} = $name;
267 $res
268}
269
196sub cf::object::attach { 270*cf::object::attach =
197 die; 271*cf::player::attach =
198} 272*cf::map::attach = sub {
273 my ($obj, $name, %arg) = @_;
274
275 _attach_attachment $obj, $name, %arg;
276};
277
278# all those should be optimised
279*cf::object::detach =
280*cf::player::detach =
281*cf::map::detach = sub {
282 my ($obj, $name) = @_;
283
284 delete $obj->{_attachment}{$name};
285 reattach ($obj);
286};
287
288*cf::object::attached =
289*cf::player::attached =
290*cf::map::attached = sub {
291 my ($obj, $name) = @_;
292
293 exists $obj->{_attachment}{$name}
294};
199 295
200sub attach_global { 296sub attach_global {
201 _attach @CB_GLOBAL, KLASS_GLOBAL, @_ 297 _attach @CB_GLOBAL, KLASS_GLOBAL, @_
202} 298}
203 299
204sub attach_to_type { 300sub attach_to_type {
205 my $type = shift; 301 my $type = shift;
302 my $subtype = shift;
206 303
207 _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ 304 _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_
208} 305}
209 306
210sub attach_to_objects { 307sub attach_to_objects {
211 _attach @CB_OBJECT, KLASS_OBJECT, @_ 308 _attach @CB_OBJECT, KLASS_OBJECT, @_
212} 309}
220} 317}
221 318
222sub register_attachment { 319sub register_attachment {
223 my $name = shift; 320 my $name = shift;
224 321
322 $attachment{$name} = [[KLASS_OBJECT, @_]];
323}
324
325sub register_player_attachment {
326 my $name = shift;
327
328 $attachment{$name} = [[KLASS_PLAYER, @_]];
329}
330
331sub register_map_attachment {
332 my $name = shift;
333
225 $attachment{$name} = [@_]; 334 $attachment{$name} = [[KLASS_MAP, @_]];
226} 335}
227 336
228our $override; 337our $override;
229our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? 338our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals?
230 339
244 for (@$callbacks) { 353 for (@$callbacks) {
245 eval { &{$_->[1]} }; 354 eval { &{$_->[1]} };
246 355
247 if ($@) { 356 if ($@) {
248 warn "$@"; 357 warn "$@";
249 warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; 358 warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n";
250 override; 359 override;
251 } 360 }
252 361
253 return 1 if $override; 362 return 1 if $override;
254 } 363 }
255 364
256 0 365 0
257} 366}
367
368=item $bool = cf::invoke EVENT_GLOBAL_XXX, ...
369
370=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
371
372=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
373
374=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
375
376Generate a global/object/player/map-specific event with the given arguments.
377
378This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be
379removed in future versions), and there is no public API to access override
380results (if you must, access C<@cf::invoke_results> directly).
381
382=back
383
384=head2 methods valid for all pointers
385
386=over 4
387
388=item $object->valid
389
390=item $player->valid
391
392=item $map->valid
393
394Just because you have a perl object does not mean that the corresponding
395C-level object still exists. If you try to access an object that has no
396valid C counterpart anymore you get an exception at runtime. This method
397can be used to test for existence of the C object part without causing an
398exception.
399
400=back
401
402=cut
403
404*cf::object::valid =
405*cf::player::valid =
406*cf::map::valid = \&cf::_valid;
258 407
259############################################################################# 408#############################################################################
260# object support 409# object support
261 410
262sub instantiate { 411sub instantiate {
263 my ($obj, $data) = @_; 412 my ($obj, $data) = @_;
264 my $registry = $obj->registry;
265 413
266 $data = from_json $data; 414 $data = from_json $data;
267 415
268 for (@$data) { 416 for (@$data) {
269 my ($pri, $name, @args) = @$_; 417 my ($name, $args) = @$_;
270 418
271 if (my $attach = $attachment{$name}) { 419 $obj->attach ($name, %{$args || {} });
272 _attach @$registry, KLASS_OBJECT, @$attach;
273
274 if (my $cb = delete $registry->[EVENT_OBJECT_INSTANTIATE]) {
275 for (@$cb) {
276 eval { $_->[1]->($obj, @args); };
277 if ($@) {
278 warn "$@";
279 warn "... while processing '$name' instantiate with args <@args>\n";
280 }
281 }
282 }
283 } else {
284 warn "object uses attachment $name that is not available, postponing.\n";
285 }
286
287 push @{$obj->{_attachment}}, $name;
288 } 420 }
289} 421}
290 422
291# basically do the same as instantiate, without calling instantiate 423# basically do the same as instantiate, without calling instantiate
292sub reattach { 424sub reattach {
293 my ($obj) = @_; 425 my ($obj) = @_;
294 my $registry = $obj->registry; 426 my $registry = $obj->registry;
295 427
296 warn "reattach<@_, $_>\n"; 428 @$registry = ();
429
430 delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} };
431
432 for my $name (keys %{ $obj->{_attachment} || {} }) {
433 if (my $attach = $attachment{$name}) {
434 for (@$attach) {
435 my ($klass, @attach) = @$_;
436 _attach @$registry, $klass, @attach;
437 }
438 } else {
439 warn "object uses attachment '$name' that is not available, postponing.\n";
440 }
441 }
442}
443
444sub object_freezer_save {
445 my ($filename, $rdata, $objs) = @_;
446
447 if (length $$rdata) {
448 warn sprintf "saving %s (%d,%d)\n",
449 $filename, length $$rdata, scalar @$objs;
450
451 if (open my $fh, ">:raw", "$filename~") {
452 chmod SAVE_MODE, $fh;
453 syswrite $fh, $$rdata;
454 close $fh;
455
456 if (@$objs && open my $fh, ">:raw", "$filename.pst~") {
457 chmod SAVE_MODE, $fh;
458 syswrite $fh, Storable::nfreeze { version => 1, objs => $objs };
459 close $fh;
460 rename "$filename.pst~", "$filename.pst";
461 } else {
462 unlink "$filename.pst";
463 }
464
465 rename "$filename~", $filename;
466 } else {
467 warn "FATAL: $filename~: $!\n";
468 }
469 } else {
470 unlink $filename;
471 unlink "$filename.pst";
472 }
473}
474
475sub object_thawer_load {
476 my ($filename) = @_;
477
478 local $/;
479
480 my $av;
481
482 #TODO: use sysread etc.
483 if (open my $data, "<:raw:perlio", $filename) {
484 $data = <$data>;
485 if (open my $pst, "<:raw:perlio", "$filename.pst") {
486 $av = eval { (Storable::thaw <$pst>)->{objs} };
487 }
488 return ($data, $av);
489 }
490
491 ()
297} 492}
298 493
299attach_to_objects 494attach_to_objects
300 prio => -1000000, 495 prio => -1000000,
301 on_clone => sub { 496 on_clone => sub {
302 my ($src, $dst) = @_; 497 my ($src, $dst) = @_;
303 498
304 @{$dst->registry} = @{$src->registry}; 499 @{$dst->registry} = @{$src->registry};
305 warn "registry clone ", join ":", @{$src->registry};#d#
306 500
307 %$dst = %$src; 501 %$dst = %$src;
308 502
309 $dst->{_attachment} = [@{ $src->{_attachment} }] 503 %{$dst->{_attachment}} = %{$src->{_attachment}}
310 if exists $src->{_attachment}; 504 if exists $src->{_attachment};
311
312 warn "clone<@_>\n";#d#
313 }, 505 },
314; 506;
315 507
316############################################################################# 508#############################################################################
317# old plug-in events 509# old plug-in events
450 load_extension $ext; 642 load_extension $ext;
451 1 643 1
452 } or warn "$ext not loaded: $@"; 644 } or warn "$ext not loaded: $@";
453 } 645 }
454} 646}
455
456sub _perl_reload(&) {
457 my ($msg) = @_;
458
459 $msg->("reloading...");
460
461 eval {
462 # 1. cancel all watchers
463 $_->cancel for Event::all_watchers;
464
465 # 2. unload all extensions
466 for (@exts) {
467 $msg->("unloading <$_>");
468 unload_extension $_;
469 }
470
471 # 3. unload all modules loaded from $LIBDIR
472 while (my ($k, $v) = each %INC) {
473 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
474
475 $msg->("removing <$k>");
476 delete $INC{$k};
477
478 $k =~ s/\.pm$//;
479 $k =~ s/\//::/g;
480
481 if (my $cb = $k->can ("unload_module")) {
482 $cb->();
483 }
484
485 Symbol::delete_package $k;
486 }
487
488 # 4. get rid of safe::, as good as possible
489 Symbol::delete_package "safe::$_"
490 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
491
492 # 5. remove register_script_function callbacks
493 # TODO
494
495 # 6. unload cf.pm "a bit"
496 delete $INC{"cf.pm"};
497
498 # don't, removes xs symbols, too,
499 # and global variables created in xs
500 #Symbol::delete_package __PACKAGE__;
501
502 # 7. reload cf.pm
503 $msg->("reloading cf.pm");
504 require cf;
505 };
506 $msg->($@) if $@;
507
508 $msg->("reloaded");
509};
510
511sub perl_reload() {
512 _perl_reload {
513 warn $_[0];
514 print "$_[0]\n";
515 };
516}
517
518register_command "perl-reload", 0, sub {
519 my ($who, $arg) = @_;
520
521 if ($who->flag (FLAG_WIZ)) {
522 _perl_reload {
523 warn $_[0];
524 $who->message ($_[0]);
525 };
526 }
527};
528 647
529############################################################################# 648#############################################################################
530# extcmd framework, basically convert ext <msg> 649# extcmd framework, basically convert ext <msg>
531# into pkg::->on_extcmd_arg1 (...) while shortcutting a few 650# into pkg::->on_extcmd_arg1 (...) while shortcutting a few
532 651
557 my ($map) = @_; 676 my ($map) = @_;
558 677
559 my $path = $map->tmpname; 678 my $path = $map->tmpname;
560 defined $path or return; 679 defined $path or return;
561 680
562 unlink "$path.cfperl"; 681 unlink "$path.pst";
563};
564
565*cf::mapsupport::on_swapin =
566*cf::mapsupport::on_load = sub {
567 my ($map) = @_;
568
569 my $path = $map->tmpname;
570 $path = $map->path unless defined $path;
571
572 open my $fh, "<:raw", "$path.cfperl"
573 or return; # no perl data
574
575 my $data = Storable::thaw do { local $/; <$fh> };
576
577 $data->{version} <= 1
578 or return; # too new
579
580 $map->_set_obs ($data->{obs});
581};
582
583*cf::mapsupport::on_swapout = sub {
584 my ($map) = @_;
585
586 my $path = $map->tmpname;
587 $path = $map->path unless defined $path;
588
589 my $obs = $map->_get_obs;
590
591 if (defined $obs) {
592 open my $fh, ">:raw", "$path.cfperl"
593 or die "$path.cfperl: $!";
594
595 stat $path;
596
597 print $fh Storable::nfreeze {
598 size => (stat _)[7],
599 time => (stat _)[9],
600 version => 1,
601 obs => $obs,
602 };
603
604 chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g*
605 } else {
606 unlink "$path.cfperl";
607 }
608}; 682};
609 683
610attach_to_maps prio => -10000, package => cf::mapsupport::; 684attach_to_maps prio => -10000, package => cf::mapsupport::;
611 685
612############################################################################# 686#############################################################################
614 688
615sub all_objects(@) { 689sub all_objects(@) {
616 @_, map all_objects ($_->inv), @_ 690 @_, map all_objects ($_->inv), @_
617} 691}
618 692
693# TODO: compatibility cruft, remove when no longer needed
619attach_to_players 694attach_to_players
620 on_load => sub { 695 on_load => sub {
621 my ($pl, $path) = @_; 696 my ($pl, $path) = @_;
622 697
623 for my $o (all_objects $pl->ob) { 698 for my $o (all_objects $pl->ob) {
625 $o->set_ob_key_value ("_perl_data"); 700 $o->set_ob_key_value ("_perl_data");
626 701
627 %$o = %{ Storable::thaw pack "H*", $value }; 702 %$o = %{ Storable::thaw pack "H*", $value };
628 } 703 }
629 } 704 }
630 },
631 on_save => sub {
632 my ($pl, $path) = @_;
633
634 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
635 for grep %$_, all_objects $pl->ob;
636 }, 705 },
637; 706;
638 707
639############################################################################# 708#############################################################################
640# core extensions - in perl 709# core extensions - in perl
747 no strict 'refs'; 816 no strict 'refs';
748 *{"safe::$fun"} = $safe_hole->wrap ($cb); 817 *{"safe::$fun"} = $safe_hole->wrap ($cb);
749} 818}
750 819
751############################################################################# 820#############################################################################
821
822=head2 EXTENSION DATABASE SUPPORT
823
824Crossfire maintains a very simple database for extension use. It can
825currently store anything that can be serialised using Storable, which
826excludes objects.
827
828The parameter C<$family> should best start with the name of the extension
829using it, it should be unique.
830
831=over 4
832
833=item $hashref = cf::db_get $family
834
835Return a hashref for use by the extension C<$family>, which can be
836modified. After modifications, you have to call C<cf::db_dirty> or
837C<cf::db_sync>.
838
839=item $value = cf::db_get $family => $key
840
841Returns a single value from the database
842
843=item cf::db_put $family => $hashref
844
845Stores the given family hashref into the database. Updates are delayed, if
846you want the data to be synced to disk immediately, use C<cf::db_sync>.
847
848=item cf::db_put $family => $key => $value
849
850Stores the given C<$value> in the family hash. Updates are delayed, if you
851want the data to be synced to disk immediately, use C<cf::db_sync>.
852
853=item cf::db_dirty
854
855Marks the database as dirty, to be updated at a later time.
856
857=item cf::db_sync
858
859Immediately write the database to disk I<if it is dirty>.
860
861=cut
862
863{
864 my $db;
865 my $path = cf::localdir . "/database.pst";
866
867 sub db_load() {
868 warn "loading database $path\n";#d# remove later
869 $db = stat $path ? Storable::retrieve $path : { };
870 }
871
872 my $pid;
873
874 sub db_save() {
875 warn "saving database $path\n";#d# remove later
876 waitpid $pid, 0 if $pid;
877 if (0 == ($pid = fork)) {
878 $db->{_meta}{version} = 1;
879 Storable::nstore $db, "$path~";
880 rename "$path~", $path;
881 kill 9, $$ if defined $pid; #d# remove when binary updated
882 cf::_exit 0 if defined $pid;
883 }
884 }
885
886 my $dirty;
887
888 sub db_sync() {
889 db_save if $dirty;
890 undef $dirty;
891 }
892
893 my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub {
894 db_sync;
895 });
896
897 sub db_dirty() {
898 $dirty = 1;
899 $idle->start;
900 }
901
902 sub db_get($;$) {
903 @_ >= 2
904 ? $db->{$_[0]}{$_[1]}
905 : ($db->{$_[0]} ||= { })
906 }
907
908 sub db_put($$;$) {
909 if (@_ >= 3) {
910 $db->{$_[0]}{$_[1]} = $_[2];
911 } else {
912 $db->{$_[0]} = $_[1];
913 }
914 db_dirty;
915 }
916
917 attach_global
918 prio => 10000,
919 on_cleanup => sub {
920 db_sync;
921 },
922 ;
923}
924
925#############################################################################
752# the server's main() 926# the server's main()
753 927
754sub main { 928sub main {
929 db_load;
930 load_extensions;
755 Event::loop; 931 Event::loop;
756} 932}
757 933
758############################################################################# 934#############################################################################
759# initialisation 935# initialisation
760 936
937sub _perl_reload(&) {
938 my ($msg) = @_;
939
940 $msg->("reloading...");
941
942 eval {
943 # cancel all watchers
944 $_->cancel for Event::all_watchers;
945
946 # unload all extensions
947 for (@exts) {
948 $msg->("unloading <$_>");
949 unload_extension $_;
950 }
951
952 # unload all modules loaded from $LIBDIR
953 while (my ($k, $v) = each %INC) {
954 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
955
956 $msg->("removing <$k>");
957 delete $INC{$k};
958
959 $k =~ s/\.pm$//;
960 $k =~ s/\//::/g;
961
962 if (my $cb = $k->can ("unload_module")) {
963 $cb->();
964 }
965
966 Symbol::delete_package $k;
967 }
968
969 # sync database to disk
970 cf::db_sync;
971
972 # get rid of safe::, as good as possible
973 Symbol::delete_package "safe::$_"
974 for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region);
975
976 # remove register_script_function callbacks
977 # TODO
978
979 # unload cf.pm "a bit"
980 delete $INC{"cf.pm"};
981
982 # don't, removes xs symbols, too,
983 # and global variables created in xs
984 #Symbol::delete_package __PACKAGE__;
985
986 # reload cf.pm
987 $msg->("reloading cf.pm");
988 require cf;
989
990 # load database again
991 cf::db_load;
992
993 # load extensions
994 $msg->("load extensions");
995 cf::load_extensions;
996
997 # reattach attachments to objects
998 $msg->("reattach");
999 _global_reattach;
1000 };
1001 $msg->($@) if $@;
1002
1003 $msg->("reloaded");
1004};
1005
1006sub perl_reload() {
1007 _perl_reload {
1008 warn $_[0];
1009 print "$_[0]\n";
1010 };
1011}
1012
1013register_command "perl-reload", 0, sub {
1014 my ($who, $arg) = @_;
1015
1016 if ($who->flag (FLAG_WIZ)) {
1017 _perl_reload {
1018 warn $_[0];
1019 $who->message ($_[0]);
1020 };
1021 }
1022};
1023
761register "<global>", __PACKAGE__; 1024register "<global>", __PACKAGE__;
762 1025
763unshift @INC, $LIBDIR; 1026unshift @INC, $LIBDIR;
764
765load_extensions;
766 1027
767$TICK_WATCHER = Event->timer ( 1028$TICK_WATCHER = Event->timer (
768 prio => 1, 1029 prio => 1,
769 at => $NEXT_TICK || 1, 1030 at => $NEXT_TICK || 1,
770 cb => sub { 1031 cb => sub {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines