… | |
… | |
11 | use Event; |
11 | use 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 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
16 | _reload_1; |
16 | _init_vars; |
17 | |
17 | |
18 | our %COMMAND = (); |
18 | our %COMMAND = (); |
19 | our @EVENT; |
19 | our @EVENT; |
20 | our %PROP_TYPE; |
|
|
21 | our %PROP_IDX; |
|
|
22 | our $LIBDIR = maps_directory "perl"; |
20 | our $LIBDIR = maps_directory "perl"; |
23 | |
21 | |
24 | our $TICK = MAX_TIME * 1e-6; |
22 | our $TICK = MAX_TIME * 1e-6; |
25 | our $TICK_WATCHER; |
23 | our $TICK_WATCHER; |
26 | our $NEXT_TICK; |
24 | our $NEXT_TICK; |
… | |
… | |
34 | print STDERR "cfperl: $msg"; |
32 | print STDERR "cfperl: $msg"; |
35 | LOG llevError, "cfperl: $msg"; |
33 | LOG llevError, "cfperl: $msg"; |
36 | }; |
34 | }; |
37 | } |
35 | } |
38 | |
36 | |
39 | my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! |
|
|
40 | |
|
|
41 | # generate property mutators |
|
|
42 | sub prop_gen { |
|
|
43 | my ($prefix, $class) = @_; |
|
|
44 | |
|
|
45 | no strict 'refs'; |
|
|
46 | |
|
|
47 | for my $prop (keys %PROP_TYPE) { |
|
|
48 | $prop =~ /^\Q$prefix\E_(.*$)/ or next; |
|
|
49 | my $sub = lc $1; |
|
|
50 | |
|
|
51 | my $type = $PROP_TYPE{$prop}; |
|
|
52 | my $idx = $PROP_IDX {$prop}; |
|
|
53 | |
|
|
54 | *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { |
|
|
55 | $_[0]->get_property ($type, $idx) |
|
|
56 | }; |
|
|
57 | |
|
|
58 | *{"$class\::set_$sub"} = sub { |
|
|
59 | $_[0]->set_property ($type, $idx, $_[1]); |
|
|
60 | } unless $ignore_set{$prop}; |
|
|
61 | } |
|
|
62 | } |
|
|
63 | |
|
|
64 | # auto-generate most of the API |
|
|
65 | |
|
|
66 | prop_gen OBJECT_PROP => "cf::object"; |
|
|
67 | # CFAPI_OBJECT_ANIMATION? |
|
|
68 | prop_gen PLAYER_PROP => "cf::object::player"; |
|
|
69 | |
|
|
70 | prop_gen MAP_PROP => "cf::map"; |
|
|
71 | prop_gen ARCH_PROP => "cf::arch"; |
|
|
72 | |
|
|
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
37 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
74 | |
38 | |
75 | # 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 |
76 | # within the Safe compartment. |
40 | # within the Safe compartment. |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
41 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
… | |
… | |
105 | } |
69 | } |
106 | |
70 | |
107 | ############################################################################# |
71 | ############################################################################# |
108 | # "new" plug-in system |
72 | # "new" plug-in system |
109 | |
73 | |
|
|
74 | =head3 EVENTS AND OBJECT ATTACHMENTS |
|
|
75 | |
|
|
76 | =over 4 |
|
|
77 | |
110 | =item $object->attach ($attachment, key => $value...) |
78 | =item $object->attach ($attachment, key => $value...) |
111 | |
79 | |
112 | =item $object->detach ($attachment) |
80 | =item $object->detach ($attachment) |
113 | |
81 | |
114 | Attach/detach a pre-registered attachment to an object. |
82 | Attach/detach a pre-registered attachment to an object. |
… | |
… | |
122 | =item $map->attach ($attachment, key => $value...) |
90 | =item $map->attach ($attachment, key => $value...) |
123 | |
91 | |
124 | =item $map->detach ($attachment) |
92 | =item $map->detach ($attachment) |
125 | |
93 | |
126 | Attach/detach a pre-registered attachment to a map. |
94 | Attach/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 | |
|
|
102 | Checks wether the named attachment is currently attached to the object. |
127 | |
103 | |
128 | =item cf::attach_global ... |
104 | =item cf::attach_global ... |
129 | |
105 | |
130 | Attach handlers for global events. |
106 | Attach handlers for global events. |
131 | |
107 | |
… | |
… | |
181 | Attach handlers to all maps. |
157 | Attach handlers to all maps. |
182 | |
158 | |
183 | =item cf:register_attachment $name, ... |
159 | =item cf:register_attachment $name, ... |
184 | |
160 | |
185 | Register an attachment by name through which objects can refer to this |
161 | Register an attachment by name through which objects can refer to this |
|
|
162 | attachment. |
|
|
163 | |
|
|
164 | =item cf:register_player_attachment $name, ... |
|
|
165 | |
|
|
166 | Register an attachment by name through which players can refer to this |
186 | attachment. |
167 | attachment. |
187 | |
168 | |
188 | =item cf:register_map_attachment $name, ... |
169 | =item cf:register_map_attachment $name, ... |
189 | |
170 | |
190 | Register an attachment by name through which maps can refer to this |
171 | Register an attachment by name through which maps can refer to this |
… | |
… | |
261 | } |
242 | } |
262 | |
243 | |
263 | sub _attach_attachment { |
244 | sub _attach_attachment { |
264 | my ($obj, $name, %arg) = @_; |
245 | my ($obj, $name, %arg) = @_; |
265 | |
246 | |
|
|
247 | return if exists $obj->{_attachment}{$name}; |
|
|
248 | |
266 | my $res; |
249 | my $res; |
267 | |
250 | |
268 | if (my $attach = $attachment{$name}) { |
251 | if (my $attach = $attachment{$name}) { |
269 | my $registry = $obj->registry; |
252 | my $registry = $obj->registry; |
270 | |
253 | |
… | |
… | |
282 | |
265 | |
283 | $res->{attachment} = $name; |
266 | $res->{attachment} = $name; |
284 | $res |
267 | $res |
285 | } |
268 | } |
286 | |
269 | |
287 | sub cf::object::attach { |
270 | *cf::object::attach = |
|
|
271 | *cf::player::attach = |
|
|
272 | *cf::map::attach = sub { |
288 | my ($obj, $name, %arg) = @_; |
273 | my ($obj, $name, %arg) = @_; |
289 | |
274 | |
290 | _attach_attachment $obj, $name, %arg; |
275 | _attach_attachment $obj, $name, %arg; |
291 | } |
276 | }; |
292 | |
277 | |
293 | sub cf::player::attach { |
278 | # all those should be optimised |
294 | my ($obj, $name, %arg) = @_; |
|
|
295 | |
|
|
296 | _attach_attachment $obj, $name, %arg; |
|
|
297 | } |
|
|
298 | |
|
|
299 | sub cf::map::attach { |
|
|
300 | my ($obj, $name, %arg) = @_; |
|
|
301 | |
|
|
302 | _attach_attachment $obj, $name, %arg; |
|
|
303 | } |
|
|
304 | |
|
|
305 | sub cf::object::detach { |
279 | *cf::object::detach = |
|
|
280 | *cf::player::detach = |
|
|
281 | *cf::map::detach = sub { |
306 | my ($obj, $name) = @_; |
282 | my ($obj, $name) = @_; |
307 | die;#d# |
283 | |
308 | } |
284 | delete $obj->{_attachment}{$name}; |
309 | sub cf::player::detach { |
285 | reattach ($obj); |
|
|
286 | }; |
|
|
287 | |
|
|
288 | *cf::object::attached = |
|
|
289 | *cf::player::attached = |
|
|
290 | *cf::map::attached = sub { |
310 | my ($obj, $name) = @_; |
291 | my ($obj, $name) = @_; |
311 | die;#d# |
292 | |
312 | } |
293 | exists $obj->{_attachment}{$name} |
313 | sub cf::map::detach { |
294 | }; |
314 | my ($obj, $name) = @_; |
|
|
315 | die;#d# |
|
|
316 | } |
|
|
317 | |
295 | |
318 | sub attach_global { |
296 | sub attach_global { |
319 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
297 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
320 | } |
298 | } |
321 | |
299 | |
… | |
… | |
342 | my $name = shift; |
320 | my $name = shift; |
343 | |
321 | |
344 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
322 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
345 | } |
323 | } |
346 | |
324 | |
|
|
325 | sub register_player_attachment { |
|
|
326 | my $name = shift; |
|
|
327 | |
|
|
328 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
329 | } |
|
|
330 | |
347 | sub register_map_attachment { |
331 | sub register_map_attachment { |
348 | my $name = shift; |
332 | my $name = shift; |
349 | |
333 | |
350 | $attachment{$name} = [[KLASS_MAP, @_]]; |
334 | $attachment{$name} = [[KLASS_MAP, @_]]; |
351 | } |
335 | } |
… | |
… | |
369 | for (@$callbacks) { |
353 | for (@$callbacks) { |
370 | eval { &{$_->[1]} }; |
354 | eval { &{$_->[1]} }; |
371 | |
355 | |
372 | if ($@) { |
356 | if ($@) { |
373 | warn "$@"; |
357 | warn "$@"; |
374 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
358 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
375 | override; |
359 | override; |
376 | } |
360 | } |
377 | |
361 | |
378 | return 1 if $override; |
362 | return 1 if $override; |
379 | } |
363 | } |
380 | |
364 | |
381 | 0 |
365 | 0 |
382 | } |
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 | |
|
|
376 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
377 | |
|
|
378 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
379 | removed in future versions), and there is no public API to access override |
|
|
380 | results (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 | |
|
|
394 | Just because you have a perl object does not mean that the corresponding |
|
|
395 | C-level object still exists. If you try to access an object that has no |
|
|
396 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
397 | can be used to test for existence of the C object part without causing an |
|
|
398 | exception. |
|
|
399 | |
|
|
400 | =back |
|
|
401 | |
|
|
402 | =cut |
|
|
403 | |
|
|
404 | *cf::object::valid = |
|
|
405 | *cf::player::valid = |
|
|
406 | *cf::map::valid = \&cf::_valid; |
383 | |
407 | |
384 | ############################################################################# |
408 | ############################################################################# |
385 | # object support |
409 | # object support |
386 | |
410 | |
387 | sub instantiate { |
411 | sub instantiate { |
… | |
… | |
416 | } |
440 | } |
417 | } |
441 | } |
418 | } |
442 | } |
419 | |
443 | |
420 | sub object_freezer_save { |
444 | sub object_freezer_save { |
421 | my ($filename, $objs) = @_; |
445 | my ($filename, $rdata, $objs) = @_; |
422 | |
446 | |
423 | if (@$objs) { |
447 | if (length $$rdata) { |
|
|
448 | warn sprintf "saving %s (%d,%d)\n", |
|
|
449 | $filename, length $$rdata, scalar @$objs; |
|
|
450 | |
424 | open my $fh, ">:raw", "$filename.pst~"; |
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; |
425 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
458 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
426 | close $fh; |
459 | close $fh; |
427 | chmod SAVE_MODE, "$filename.pst~"; |
|
|
428 | rename "$filename.pst~", "$filename.pst"; |
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 | } |
429 | } else { |
469 | } else { |
|
|
470 | unlink $filename; |
430 | unlink "$filename.pst"; |
471 | unlink "$filename.pst"; |
431 | } |
472 | } |
432 | |
|
|
433 | chmod SAVE_MODE, "$filename~"; |
|
|
434 | rename "$filename~", $filename; |
|
|
435 | } |
473 | } |
436 | |
474 | |
437 | sub object_thawer_load { |
475 | sub object_thawer_load { |
438 | my ($filename) = @_; |
476 | my ($filename) = @_; |
439 | |
477 | |
|
|
478 | local $/; |
|
|
479 | |
|
|
480 | my $av; |
|
|
481 | |
|
|
482 | #TODO: use sysread etc. |
|
|
483 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
484 | $data = <$data>; |
440 | open my $fh, "<:raw:perlio", "$filename.pst" |
485 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
441 | or return; |
486 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
487 | } |
|
|
488 | return ($data, $av); |
|
|
489 | } |
442 | |
490 | |
443 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
491 | () |
444 | } |
492 | } |
445 | |
493 | |
446 | attach_to_objects |
494 | attach_to_objects |
447 | prio => -1000000, |
495 | prio => -1000000, |
448 | on_clone => sub { |
496 | on_clone => sub { |
… | |
… | |
594 | load_extension $ext; |
642 | load_extension $ext; |
595 | 1 |
643 | 1 |
596 | } or warn "$ext not loaded: $@"; |
644 | } or warn "$ext not loaded: $@"; |
597 | } |
645 | } |
598 | } |
646 | } |
599 | |
|
|
600 | sub _perl_reload(&) { |
|
|
601 | my ($msg) = @_; |
|
|
602 | |
|
|
603 | $msg->("reloading..."); |
|
|
604 | |
|
|
605 | eval { |
|
|
606 | # 1. cancel all watchers |
|
|
607 | $_->cancel for Event::all_watchers; |
|
|
608 | |
|
|
609 | # 2. unload all extensions |
|
|
610 | for (@exts) { |
|
|
611 | $msg->("unloading <$_>"); |
|
|
612 | unload_extension $_; |
|
|
613 | } |
|
|
614 | |
|
|
615 | # 3. unload all modules loaded from $LIBDIR |
|
|
616 | while (my ($k, $v) = each %INC) { |
|
|
617 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
618 | |
|
|
619 | $msg->("removing <$k>"); |
|
|
620 | delete $INC{$k}; |
|
|
621 | |
|
|
622 | $k =~ s/\.pm$//; |
|
|
623 | $k =~ s/\//::/g; |
|
|
624 | |
|
|
625 | if (my $cb = $k->can ("unload_module")) { |
|
|
626 | $cb->(); |
|
|
627 | } |
|
|
628 | |
|
|
629 | Symbol::delete_package $k; |
|
|
630 | } |
|
|
631 | |
|
|
632 | # 4. get rid of safe::, as good as possible |
|
|
633 | Symbol::delete_package "safe::$_" |
|
|
634 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
635 | |
|
|
636 | # 5. remove register_script_function callbacks |
|
|
637 | # TODO |
|
|
638 | |
|
|
639 | # 6. unload cf.pm "a bit" |
|
|
640 | delete $INC{"cf.pm"}; |
|
|
641 | |
|
|
642 | # don't, removes xs symbols, too, |
|
|
643 | # and global variables created in xs |
|
|
644 | #Symbol::delete_package __PACKAGE__; |
|
|
645 | |
|
|
646 | # 7. reload cf.pm |
|
|
647 | $msg->("reloading cf.pm"); |
|
|
648 | require cf; |
|
|
649 | }; |
|
|
650 | $msg->($@) if $@; |
|
|
651 | |
|
|
652 | $msg->("reloaded"); |
|
|
653 | }; |
|
|
654 | |
|
|
655 | sub perl_reload() { |
|
|
656 | _perl_reload { |
|
|
657 | warn $_[0]; |
|
|
658 | print "$_[0]\n"; |
|
|
659 | }; |
|
|
660 | } |
|
|
661 | |
|
|
662 | register_command "perl-reload", 0, sub { |
|
|
663 | my ($who, $arg) = @_; |
|
|
664 | |
|
|
665 | if ($who->flag (FLAG_WIZ)) { |
|
|
666 | _perl_reload { |
|
|
667 | warn $_[0]; |
|
|
668 | $who->message ($_[0]); |
|
|
669 | }; |
|
|
670 | } |
|
|
671 | }; |
|
|
672 | |
647 | |
673 | ############################################################################# |
648 | ############################################################################# |
674 | # extcmd framework, basically convert ext <msg> |
649 | # extcmd framework, basically convert ext <msg> |
675 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
650 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
676 | |
651 | |
… | |
… | |
704 | defined $path or return; |
679 | defined $path or return; |
705 | |
680 | |
706 | unlink "$path.pst"; |
681 | unlink "$path.pst"; |
707 | }; |
682 | }; |
708 | |
683 | |
709 | # old style persistent data, TODO: remove #d# |
|
|
710 | *cf::mapsupport::on_swapin = |
|
|
711 | *cf::mapsupport::on_load = sub { |
|
|
712 | my ($map) = @_; |
|
|
713 | |
|
|
714 | my $path = $map->tmpname; |
|
|
715 | $path = $map->path unless defined $path; |
|
|
716 | |
|
|
717 | open my $fh, "<:raw", "$path.cfperl" |
|
|
718 | or return; # no perl data |
|
|
719 | |
|
|
720 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
721 | |
|
|
722 | $data->{version} <= 1 |
|
|
723 | or return; # too new |
|
|
724 | |
|
|
725 | $map->_set_obs ($data->{obs}); |
|
|
726 | $map->invoke (EVENT_MAP_UPGRADE); |
|
|
727 | }; |
|
|
728 | |
|
|
729 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
684 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
730 | |
685 | |
731 | ############################################################################# |
686 | ############################################################################# |
732 | # load/save perl data associated with player->ob objects |
687 | # load/save perl data associated with player->ob objects |
733 | |
688 | |
734 | sub all_objects(@) { |
689 | sub all_objects(@) { |
735 | @_, map all_objects ($_->inv), @_ |
690 | @_, map all_objects ($_->inv), @_ |
736 | } |
691 | } |
737 | |
692 | |
|
|
693 | # TODO: compatibility cruft, remove when no longer needed |
738 | attach_to_players |
694 | attach_to_players |
739 | on_load => sub { |
695 | on_load => sub { |
740 | my ($pl, $path) = @_; |
696 | my ($pl, $path) = @_; |
741 | |
697 | |
742 | for my $o (all_objects $pl->ob) { |
698 | for my $o (all_objects $pl->ob) { |
… | |
… | |
860 | no strict 'refs'; |
816 | no strict 'refs'; |
861 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
862 | } |
818 | } |
863 | |
819 | |
864 | ############################################################################# |
820 | ############################################################################# |
|
|
821 | |
|
|
822 | =head2 EXTENSION DATABASE SUPPORT |
|
|
823 | |
|
|
824 | Crossfire maintains a very simple database for extension use. It can |
|
|
825 | currently store anything that can be serialised using Storable, which |
|
|
826 | excludes objects. |
|
|
827 | |
|
|
828 | The parameter C<$family> should best start with the name of the extension |
|
|
829 | using it, it should be unique. |
|
|
830 | |
|
|
831 | =over 4 |
|
|
832 | |
|
|
833 | =item $hashref = cf::db_get $family |
|
|
834 | |
|
|
835 | Return a hashref for use by the extension C<$family>, which can be |
|
|
836 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
837 | C<cf::db_sync>. |
|
|
838 | |
|
|
839 | =item $value = cf::db_get $family => $key |
|
|
840 | |
|
|
841 | Returns a single value from the database |
|
|
842 | |
|
|
843 | =item cf::db_put $family => $hashref |
|
|
844 | |
|
|
845 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
846 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
847 | |
|
|
848 | =item cf::db_put $family => $key => $value |
|
|
849 | |
|
|
850 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
|
|
851 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
852 | |
|
|
853 | =item cf::db_dirty |
|
|
854 | |
|
|
855 | Marks the database as dirty, to be updated at a later time. |
|
|
856 | |
|
|
857 | =item cf::db_sync |
|
|
858 | |
|
|
859 | Immediately 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 | ############################################################################# |
865 | # the server's main() |
926 | # the server's main() |
866 | |
927 | |
867 | sub main { |
928 | sub main { |
|
|
929 | db_load; |
|
|
930 | load_extensions; |
868 | Event::loop; |
931 | Event::loop; |
869 | } |
932 | } |
870 | |
933 | |
871 | ############################################################################# |
934 | ############################################################################# |
872 | # initialisation |
935 | # initialisation |
873 | |
936 | |
|
|
937 | sub _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 | |
|
|
1006 | sub perl_reload() { |
|
|
1007 | _perl_reload { |
|
|
1008 | warn $_[0]; |
|
|
1009 | print "$_[0]\n"; |
|
|
1010 | }; |
|
|
1011 | } |
|
|
1012 | |
|
|
1013 | register_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 | |
874 | register "<global>", __PACKAGE__; |
1024 | register "<global>", __PACKAGE__; |
875 | |
1025 | |
876 | unshift @INC, $LIBDIR; |
1026 | unshift @INC, $LIBDIR; |
877 | |
|
|
878 | load_extensions; |
|
|
879 | |
1027 | |
880 | $TICK_WATCHER = Event->timer ( |
1028 | $TICK_WATCHER = Event->timer ( |
881 | prio => 1, |
1029 | prio => 1, |
882 | at => $NEXT_TICK || 1, |
1030 | at => $NEXT_TICK || 1, |
883 | cb => sub { |
1031 | cb => sub { |
… | |
… | |
892 | $TICK_WATCHER->at ($NEXT_TICK); |
1040 | $TICK_WATCHER->at ($NEXT_TICK); |
893 | $TICK_WATCHER->start; |
1041 | $TICK_WATCHER->start; |
894 | }, |
1042 | }, |
895 | ); |
1043 | ); |
896 | |
1044 | |
897 | _reload_2; |
|
|
898 | |
|
|
899 | 1 |
1045 | 1 |
900 | |
1046 | |