--- deliantra/server/lib/cf.pm 2006/07/20 07:22:40 1.25 +++ deliantra/server/lib/cf.pm 2006/10/02 00:22:01 1.75 @@ -7,19 +7,53 @@ use Safe; use Safe::Hole; +use YAML::Syck (); +use Time::HiRes; use Event; $Event::Eval = 1; # no idea why this is required, but it is +# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? +$YAML::Syck::ImplicitUnicode = 1; + use strict; -our %COMMAND; +_init_vars; + +our %COMMAND = (); our @EVENT; -our %PROP_TYPE; -our %PROP_IDX; +our $LIBDIR = maps_directory "perl"; -BEGIN { - @EVENT = map lc, @EVENT; +our $TICK = MAX_TIME * 1e-6; +our $TICK_WATCHER; +our $NEXT_TICK; + +our %CFG; + +############################################################################# + +=head2 GLOBAL VARIABLES + +=over 4 + +=item $cf::LIBDIR + +The perl library directory, where extensions and cf-specific modules can +be found. It will be added to C<@INC> automatically. + +=item $cf::TICK + +The interval between server ticks, in seconds. + +=item %cf::CFG +Configuration for the server, loaded from C, or +from wherever your confdir points to. + +=back + +=cut + +BEGIN { *CORE::GLOBAL::warn = sub { my $msg = join "", @_; $msg .= "\n" @@ -30,61 +64,497 @@ }; } -my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! - -# generate property mutators -sub prop_gen { - my ($prefix, $class) = @_; +@safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; +# we bless all objects into (empty) derived classes to force a method lookup +# within the Safe compartment. +for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { no strict 'refs'; + @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; +} + +$Event::DIED = sub { + warn "error in event callback: @_"; +}; - for my $prop (keys %PROP_TYPE) { - $prop =~ /^\Q$prefix\E_(.*$)/ or next; - my $sub = lc $1; +my %ext_pkg; +my @exts; +my @hook; +my %command; +my %extcmd; - my $type = $PROP_TYPE{$prop}; - my $idx = $PROP_IDX {$prop}; +=head2 UTILITY FUNCTIONS - *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { - $_[0]->get_property ($type, $idx) - }; +=over 4 + +=cut + +use JSON::Syck (); # TODO# replace by JSON::PC once working + +=item $ref = cf::from_json $json + +Converts a JSON string into the corresponding perl data structure. + +=cut + +sub from_json($) { + $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs + JSON::Syck::Load $_[0] +} + +=item $json = cf::to_json $ref + +Converts a perl data structure into its JSON representation. + +=cut + +sub to_json($) { + $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs + JSON::Syck::Dump $_[0] +} + +=back + +=cut + +############################################################################# + +=head2 EVENTS AND OBJECT ATTACHMENTS + +=over 4 + +=item $object->attach ($attachment, key => $value...) + +=item $object->detach ($attachment) + +Attach/detach a pre-registered attachment to an object. + +=item $player->attach ($attachment, key => $value...) + +=item $player->detach ($attachment) + +Attach/detach a pre-registered attachment to a player. + +=item $map->attach ($attachment, key => $value...) + +=item $map->detach ($attachment) + +Attach/detach a pre-registered attachment to a map. + +=item $bool = $object->attached ($name) + +=item $bool = $player->attached ($name) + +=item $bool = $map->attached ($name) + +Checks wether the named attachment is currently attached to the object. + +=item cf::attach_global ... + +Attach handlers for global events. + +This and all following C-functions expect any number of the +following handler/hook descriptions: + +=over 4 + +=item prio => $number + +Set the priority for all following handlers/hooks (unless overwritten +by another C setting). Lower priority handlers get executed +earlier. The default priority is C<0>, and many built-in handlers are +registered at priority C<-1000>, so lower priorities should not be used +unless you know what you are doing. + +=item on_I => \&cb + +Call the given code reference whenever the named event happens (event is +something like C, C, C and so on, and which +handlers are recognised generally depends on the type of object these +handlers attach to). + +See F for the full list of events supported, and their +class. + +=item package => package:: + +Look for sub functions of the name C<< on_I >> in the given +package and register them. Only handlers for eevents supported by the +object/class are recognised. + +=back + +=item cf::attach_to_type $object_type, $subtype, ... + +Attach handlers for a specific object type (e.g. TRANSPORT) and +subtype. If C<$subtype> is zero or undef, matches all objects of the given +type. + +=item cf::attach_to_objects ... + +Attach handlers to all objects. Do not use this except for debugging or +very rare events, as handlers are (obviously) called for I objects in +the game. + +=item cf::attach_to_players ... + +Attach handlers to all players. + +=item cf::attach_to_maps ... + +Attach handlers to all maps. + +=item cf:register_attachment $name, ... + +Register an attachment by name through which objects can refer to this +attachment. + +=item cf:register_player_attachment $name, ... + +Register an attachment by name through which players can refer to this +attachment. + +=item cf:register_map_attachment $name, ... + +Register an attachment by name through which maps can refer to this +attachment. + +=cut + +# the following variables are defined in .xs and must not be re-created +our @CB_GLOBAL = (); # registry for all global events +our @CB_OBJECT = (); # all objects (should not be used except in emergency) +our @CB_PLAYER = (); +our @CB_TYPE = (); # registry for type (cf-object class) based events +our @CB_MAP = (); + +my %attachment; + +sub _attach_cb($\%$$$) { + my ($registry, $undo, $event, $prio, $cb) = @_; + + use sort 'stable'; + + $cb = [$prio, $cb]; + + @{$registry->[$event]} = sort + { $a->[0] cmp $b->[0] } + @{$registry->[$event] || []}, $cb; - *{"$class\::set_$sub"} = sub { - $_[0]->set_property ($type, $idx, $_[1]); - } unless $ignore_set{$prop}; + push @{$undo->{cb}}, [$event, $cb]; +} + +# attach handles attaching event callbacks +# the only thing the caller has to do is pass the correct +# registry (== where the callback attaches to). +sub _attach(\@$@) { + my ($registry, $klass, @arg) = @_; + + my $prio = 0; + + my %undo = ( + registry => $registry, + cb => [], + ); + + my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; + + while (@arg) { + my $type = shift @arg; + + if ($type eq "prio") { + $prio = shift @arg; + + } elsif ($type eq "package") { + my $pkg = shift @arg; + + while (my ($name, $id) = each %cb_id) { + if (my $cb = $pkg->can ($name)) { + _attach_cb $registry, %undo, $id, $prio, $cb; + } + } + + } elsif (exists $cb_id{$type}) { + _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; + + } elsif (ref $type) { + warn "attaching objects not supported, ignoring.\n"; + + } else { + shift @arg; + warn "attach argument '$type' not supported, ignoring.\n"; + } } + + \%undo } -# auto-generate most of the API +sub _attach_attachment { + my ($obj, $name, %arg) = @_; -prop_gen OBJECT_PROP => "cf::object"; -# CFAPI_OBJECT_ANIMATION? -prop_gen PLAYER_PROP => "cf::object::player"; + return if exists $obj->{_attachment}{$name}; -prop_gen MAP_PROP => "cf::map"; -prop_gen ARCH_PROP => "cf::arch"; + my $res; -# guessed hierarchies + if (my $attach = $attachment{$name}) { + my $registry = $obj->registry; -@ext::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; -@ext::cf::object::map::ISA = @cf::object::map::ISA = 'cf::object'; + for (@$attach) { + my ($klass, @attach) = @$_; + $res = _attach @$registry, $klass, @attach; + } -# we bless all objects into derived classes to force a method lookup -# within the Safe compartment. -for my $pkg (qw(cf::object cf::object::map cf::object::player cf::player cf::map cf::party cf::region)) { - no strict 'refs'; - @{"ext::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; + $obj->{$name} = \%arg; + } else { + warn "object uses attachment '$name' that is not available, postponing.\n"; + } + + $obj->{_attachment}{$name} = undef; + + $res->{attachment} = $name; + $res } -$Event::DIED = sub { - warn "error in event callback: @_"; +*cf::object::attach = +*cf::player::attach = +*cf::map::attach = sub { + my ($obj, $name, %arg) = @_; + + _attach_attachment $obj, $name, %arg; }; -my %ext_pkg; -my @exts; -my @hook; -my %command; -my %extcmd; +# all those should be optimised +*cf::object::detach = +*cf::player::detach = +*cf::map::detach = sub { + my ($obj, $name) = @_; + + delete $obj->{_attachment}{$name}; + reattach ($obj); +}; + +*cf::object::attached = +*cf::player::attached = +*cf::map::attached = sub { + my ($obj, $name) = @_; + + exists $obj->{_attachment}{$name} +}; + +sub attach_global { + _attach @CB_GLOBAL, KLASS_GLOBAL, @_ +} + +sub attach_to_type { + my $type = shift; + my $subtype = shift; + + _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ +} + +sub attach_to_objects { + _attach @CB_OBJECT, KLASS_OBJECT, @_ +} + +sub attach_to_players { + _attach @CB_PLAYER, KLASS_PLAYER, @_ +} + +sub attach_to_maps { + _attach @CB_MAP, KLASS_MAP, @_ +} + +sub register_attachment { + my $name = shift; + + $attachment{$name} = [[KLASS_OBJECT, @_]]; +} + +sub register_player_attachment { + my $name = shift; + + $attachment{$name} = [[KLASS_PLAYER, @_]]; +} + +sub register_map_attachment { + my $name = shift; + + $attachment{$name} = [[KLASS_MAP, @_]]; +} + +our $override; +our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? + +sub override { + $override = 1; + @invoke_results = (); +} + +sub do_invoke { + my $event = shift; + my $callbacks = shift; + + @invoke_results = (); + + local $override; + + for (@$callbacks) { + eval { &{$_->[1]} }; + + if ($@) { + warn "$@"; + warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; + override; + } + + return 1 if $override; + } + + 0 +} + +=item $bool = cf::invoke EVENT_GLOBAL_XXX, ... + +=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) + +=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) + +=item $bool = $map->invoke (EVENT_MAP_XXX, ...) + +Generate a global/object/player/map-specific event with the given arguments. + +This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be +removed in future versions), and there is no public API to access override +results (if you must, access C<@cf::invoke_results> directly). + +=back + +=cut + +############################################################################# + +=head2 METHODS VALID FOR ALL CORE OBJECTS + +=over 4 + +=item $object->valid, $player->valid, $map->valid + +Just because you have a perl object does not mean that the corresponding +C-level object still exists. If you try to access an object that has no +valid C counterpart anymore you get an exception at runtime. This method +can be used to test for existence of the C object part without causing an +exception. + +=back + +=cut + +*cf::object::valid = +*cf::player::valid = +*cf::map::valid = \&cf::_valid; + +############################################################################# +# object support + +sub instantiate { + my ($obj, $data) = @_; + + $data = from_json $data; + + for (@$data) { + my ($name, $args) = @$_; + + $obj->attach ($name, %{$args || {} }); + } +} + +# basically do the same as instantiate, without calling instantiate +sub reattach { + my ($obj) = @_; + my $registry = $obj->registry; + + @$registry = (); + + delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; + + for my $name (keys %{ $obj->{_attachment} || {} }) { + if (my $attach = $attachment{$name}) { + for (@$attach) { + my ($klass, @attach) = @$_; + _attach @$registry, $klass, @attach; + } + } else { + warn "object uses attachment '$name' that is not available, postponing.\n"; + } + } +} + +sub object_freezer_save { + my ($filename, $rdata, $objs) = @_; + + if (length $$rdata) { + warn sprintf "saving %s (%d,%d)\n", + $filename, length $$rdata, scalar @$objs; + + if (open my $fh, ">:raw", "$filename~") { + chmod SAVE_MODE, $fh; + syswrite $fh, $$rdata; + close $fh; + + if (@$objs && open my $fh, ">:raw", "$filename.pst~") { + chmod SAVE_MODE, $fh; + syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; + close $fh; + rename "$filename.pst~", "$filename.pst"; + } else { + unlink "$filename.pst"; + } + + rename "$filename~", $filename; + } else { + warn "FATAL: $filename~: $!\n"; + } + } else { + unlink $filename; + unlink "$filename.pst"; + } +} + +sub object_thawer_load { + my ($filename) = @_; + + local $/; + + my $av; + + #TODO: use sysread etc. + if (open my $data, "<:raw:perlio", $filename) { + $data = <$data>; + if (open my $pst, "<:raw:perlio", "$filename.pst") { + $av = eval { (Storable::thaw <$pst>)->{objs} }; + } + return ($data, $av); + } + + () +} + +attach_to_objects + prio => -1000000, + on_clone => sub { + my ($src, $dst) = @_; + + @{$dst->registry} = @{$src->registry}; + + %$dst = %$src; + + %{$dst->{_attachment}} = %{$src->{_attachment}} + if exists $src->{_attachment}; + }, +; + +############################################################################# +# old plug-in events sub inject_event { my $extension = shift; @@ -137,12 +607,7 @@ sub register { my ($base, $pkg) = @_; - for my $idx (0 .. $#EVENT) { - if (my $ref = $pkg->can ("on_$EVENT[$idx]")) { - #warn "registering $EVENT[$idx] hook to '$pkg'\n"; - $hook[$idx]{$base} = $ref; - } - } + #TODO } sub load_extension { @@ -152,7 +617,7 @@ my $base = $1; my $pkg = $1; $pkg =~ s/[^[:word:]]/_/g; - $pkg = "cf::ext::$pkg"; + $pkg = "ext::$pkg"; warn "loading '$path' into '$pkg'\n"; @@ -182,14 +647,11 @@ warn "removing extension $pkg\n"; - if (my $cb = $pkg->can ("on_unload")) { - $cb->($pkg); - } - # remove hooks - for my $idx (0 .. $#EVENT) { - delete $hook[$idx]{$pkg}; - } + #TODO +# for my $idx (0 .. $#PLUGIN_EVENT) { +# delete $hook[$idx]{$pkg}; +# } # remove commands for my $name (keys %command) { @@ -209,6 +671,13 @@ delete $extcmd{$name}; } + if (my $cb = $pkg->can ("unload")) { + eval { + $cb->($pkg); + 1 + } or warn "$pkg unloaded, but with errors: $@"; + } + Symbol::delete_package $pkg; } @@ -224,143 +693,73 @@ } } -register_command "perl-reload", 0, sub { - my ($who, $arg) = @_; - - if ($who->flag (FLAG_WIZ)) { - $who->message ("reloading..."); - - warn "reloading...\n"; - eval { - $_->cancel for Event::all_watchers; - - unload_extension $_ for @exts; - delete $INC{"cf.pm"}; - - # don't, removes xs symbols, too - #Symbol::delete_package __PACKAGE__; - - require cf; - }; - warn $@ if $@; - $who->message ($@) if $@; - warn "reloaded\n"; - - $who->message ("reloaded"); - } else { - $who->message ("Intruder Alert!"); - } -}; - ############################################################################# -# extcmd framework, basically convert ext arg1 args +# extcmd framework, basically convert ext # into pkg::->on_extcmd_arg1 (...) while shortcutting a few -sub on_extcmd { - my ($pl, $buf) = @_; - - my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; +attach_to_players + on_extcmd => sub { + my ($pl, $buf) = @_; + + my $msg = eval { from_json $buf }; + + if (ref $msg) { + if (my $cb = $extcmd{$msg->{msgtype}}) { + if (my %reply = $cb->[0]->($pl, $msg)) { + $pl->ext_reply ($msg->{msgid}, %reply); + } + } + } else { + warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; + } - $extcmd{$type}[0]->($pl, $buf) - if $extcmd{$type}; -} + cf::override; + }, +; ############################################################################# # load/save/clean perl data associated with a map -*on_mapclean = sub { +*cf::mapsupport::on_clean = sub { my ($map) = @_; my $path = $map->tmpname; defined $path or return; - unlink "$path.cfperl"; + unlink "$path.pst"; }; -*on_mapin = -*on_mapload = sub { - my ($map) = @_; - - my $path = $map->tmpname; - $path = $map->path unless defined $path; - - open my $fh, "<:raw", "$path.cfperl" - or return; # no perl data - - my $data = Storable::thaw do { local $/; <$fh> }; - - $data->{version} <= 1 - or return; # too new - - $map->_set_obs ($data->{obs}); -}; - -*on_mapout = sub { - my ($map) = @_; - - my $path = $map->tmpname; - $path = $map->path unless defined $path; - - my $obs = $map->_get_obs; - - if (defined $obs) { - open my $fh, ">:raw", "$path.cfperl" - or die "$path.cfperl: $!"; - - stat $path; - - print $fh Storable::nfreeze { - size => (stat _)[7], - time => (stat _)[9], - version => 1, - obs => $obs, - }; - - chmod SAVE_MODE, "$path.cfperl"; # very racy, but cf-compatible *g* - } else { - unlink "$path.cfperl"; - } -}; +attach_to_maps prio => -10000, package => cf::mapsupport::; ############################################################################# # load/save perl data associated with player->ob objects -*on_player_load = sub { - my ($ob, $path) = @_; - - if (open my $fh, "<:raw", "$path.cfperl") { - - #d##TODO#remove - - my $data = Storable::thaw do { local $/; <$fh> }; - - $data->{version} <= 1 - or return; # too new - - %$ob = %{$data->{ob}}; - return; - } +sub all_objects(@) { + @_, map all_objects ($_->inv), @_ +} - for my $o ($ob, $ob->inv) { - if (my $value = $o->get_ob_key_value ("_perl_data")) { - $o->set_ob_key_value ("_perl_data"); +# TODO: compatibility cruft, remove when no longer needed +attach_to_players + on_load => sub { + my ($pl, $path) = @_; + + for my $o (all_objects $pl->ob) { + if (my $value = $o->get_ob_key_value ("_perl_data")) { + $o->set_ob_key_value ("_perl_data"); - %$o = %{ Storable::thaw pack "H*", $value }; + %$o = %{ Storable::thaw pack "H*", $value }; + } } - } -}; + }, +; -*on_player_save = sub { - my ($ob, $path) = @_; +############################################################################# - $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) - for grep %$_, $ob, $ob->inv; +=head2 CORE EXTENSIONS - unlink "$path.cfperl";#d##TODO#remove -}; +Functions and methods that extend core crossfire objects. -############################################################################# -# core extensions - in perl +=over 4 =item cf::player::exists $login @@ -373,6 +772,14 @@ or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; } +=item $object->reply ($npc, $msg[, $flags]) + +Sends a message to the player, as if the npc C<$npc> replied. C<$npc> +can be C. Does the right thing when the player is currently in a +dialogue with the given NPC character. + +=cut + # rough implementation of a future "reply" method that works # with dialog boxes. sub cf::object::player::reply($$$;$) { @@ -388,10 +795,37 @@ } } +=item $player->ext_reply ($msgid, $msgtype, %msg) + +Sends an ext reply to the player. + +=cut + +sub cf::player::ext_reply($$$%) { + my ($self, $id, %msg) = @_; + + $msg{msgid} = $id; + + $self->send ("ext " . to_json \%msg); +} + +=back + +=cut + ############################################################################# -# map scripting support -our $safe = new Safe "ext"; +=head2 SAFE SCRIPTING + +Functions that provide a safe environment to compile and execute +snippets of perl code without them endangering the safety of the server +itself. Looping constructs, I/O operators and other built-in functionality +is not available in the safe scripting environment, and the number of +functions and methods that cna be called is greatly reduced. + +=cut + +our $safe = new Safe "safe"; our $safe_hole = new Safe::Hole; $SIG{FPE} = 'IGNORE'; @@ -400,17 +834,39 @@ # here we export the classes and methods available to script code +=pod + +The following fucntions and emthods are available within a safe environment: + + cf::object contr pay_amount pay_player + cf::object::player player + cf::player peaceful + +=cut + for ( - ["cf::object" => qw(contr)], + ["cf::object" => qw(contr pay_amount pay_player)], ["cf::object::player" => qw(player)], ["cf::player" => qw(peaceful)], ) { no strict 'refs'; my ($pkg, @funs) = @$_; - *{"ext::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) + *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) for @funs; } +=over 4 + +=item @retval = safe_eval $code, [var => value, ...] + +Compiled and executes the given perl code snippet. additional var/value +pairs result in temporary local (my) scalar variables of the given name +that are available in the code snippet. Example: + + my $five = safe_eval '$first + $second', first => 1, second => 4; + +=cut + sub safe_eval($;@) { my ($code, %vars) = @_; @@ -419,9 +875,9 @@ $qcode =~ s/\n/\\n/g; local $_; - local @ext::cf::_safe_eval_args = values %vars; + local @safe::cf::_safe_eval_args = values %vars; - $code = + my $eval = "do {\n" . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" . "#line 0 \"{$qcode}\"\n" @@ -430,27 +886,277 @@ ; sub_generation_inc; - my @res = wantarray ? $safe->reval ($code) : scalar $safe->reval ($code); + my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); sub_generation_inc; + if ($@) { + warn "$@"; + warn "while executing safe code '$code'\n"; + warn "with arguments " . (join " ", %vars) . "\n"; + } + wantarray ? @res : $res[0] } +=item cf::register_script_function $function => $cb + +Register a function that can be called from within map/npc scripts. The +function should be reasonably secure and should be put into a package name +like the extension. + +Example: register a function that gets called whenever a map script calls +C, as used by the C extension. + + cf::register_script_function "rent::overview" => sub { + ... + }; + +=cut + sub register_script_function { my ($fun, $cb) = @_; no strict 'refs'; - *{"ext::$fun"} = $safe_hole->wrap ($cb); + *{"safe::$fun"} = $safe_hole->wrap ($cb); +} + +=back + +=cut + +############################################################################# + +=head2 EXTENSION DATABASE SUPPORT + +Crossfire maintains a very simple database for extension use. It can +currently store anything that can be serialised using Storable, which +excludes objects. + +The parameter C<$family> should best start with the name of the extension +using it, it should be unique. + +=over 4 + +=item $hashref = cf::db_get $family + +Return a hashref for use by the extension C<$family>, which can be +modified. After modifications, you have to call C or +C. + +=item $value = cf::db_get $family => $key + +Returns a single value from the database + +=item cf::db_put $family => $hashref + +Stores the given family hashref into the database. Updates are delayed, if +you want the data to be synced to disk immediately, use C. + +=item cf::db_put $family => $key => $value + +Stores the given C<$value> in the family hash. Updates are delayed, if you +want the data to be synced to disk immediately, use C. + +=item cf::db_dirty + +Marks the database as dirty, to be updated at a later time. + +=item cf::db_sync + +Immediately write the database to disk I. + +=cut + +{ + my $db; + my $path = cf::localdir . "/database.pst"; + + sub db_load() { + warn "loading database $path\n";#d# remove later + $db = stat $path ? Storable::retrieve $path : { }; + } + + my $pid; + + sub db_save() { + warn "saving database $path\n";#d# remove later + waitpid $pid, 0 if $pid; + if (0 == ($pid = fork)) { + $db->{_meta}{version} = 1; + Storable::nstore $db, "$path~"; + rename "$path~", $path; + cf::_exit 0 if defined $pid; + } + } + + my $dirty; + + sub db_sync() { + db_save if $dirty; + undef $dirty; + } + + my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { + db_sync; + }); + + sub db_dirty() { + $dirty = 1; + $idle->start; + } + + sub db_get($;$) { + @_ >= 2 + ? $db->{$_[0]}{$_[1]} + : ($db->{$_[0]} ||= { }) + } + + sub db_put($$;$) { + if (@_ >= 3) { + $db->{$_[0]}{$_[1]} = $_[2]; + } else { + $db->{$_[0]} = $_[1]; + } + db_dirty; + } + + attach_global + prio => 10000, + on_cleanup => sub { + db_sync; + }, + ; +} + +############################################################################# +# the server's main() + +sub cfg_load { + open my $fh, "<:utf8", cf::confdir . "/config" + or return; + + local $/; + *CFG = YAML::Syck::Load <$fh>; +} + +sub main { + cfg_load; + db_load; + load_extensions; + Event::loop; } ############################################################################# # initialisation +sub _perl_reload(&) { + my ($msg) = @_; + + $msg->("reloading..."); + + eval { + # cancel all watchers + $_->cancel for Event::all_watchers; + + # unload all extensions + for (@exts) { + $msg->("unloading <$_>"); + unload_extension $_; + } + + # unload all modules loaded from $LIBDIR + while (my ($k, $v) = each %INC) { + next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; + + $msg->("removing <$k>"); + delete $INC{$k}; + + $k =~ s/\.pm$//; + $k =~ s/\//::/g; + + if (my $cb = $k->can ("unload_module")) { + $cb->(); + } + + Symbol::delete_package $k; + } + + # sync database to disk + cf::db_sync; + + # get rid of safe::, as good as possible + Symbol::delete_package "safe::$_" + for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); + + # remove register_script_function callbacks + # TODO + + # unload cf.pm "a bit" + delete $INC{"cf.pm"}; + + # don't, removes xs symbols, too, + # and global variables created in xs + #Symbol::delete_package __PACKAGE__; + + # reload cf.pm + $msg->("reloading cf.pm"); + require cf; + + # load config and database again + cf::cfg_load; + cf::db_load; + + # load extensions + $msg->("load extensions"); + cf::load_extensions; + + # reattach attachments to objects + $msg->("reattach"); + _global_reattach; + }; + $msg->($@) if $@; + + $msg->("reloaded"); +}; + +sub perl_reload() { + _perl_reload { + warn $_[0]; + print "$_[0]\n"; + }; +} + +register_command "perl-reload", 0, sub { + my ($who, $arg) = @_; + + if ($who->flag (FLAG_WIZ)) { + _perl_reload { + warn $_[0]; + $who->message ($_[0]); + }; + } +}; + register "", __PACKAGE__; -unshift @INC, maps_directory "perl"; +unshift @INC, $LIBDIR; -load_extensions; +$TICK_WATCHER = Event->timer ( + prio => 1, + at => $NEXT_TICK || 1, + cb => sub { + cf::server_tick; # one server iteration + + my $NOW = Event::time; + $NEXT_TICK += $TICK; + + # if we are delayed by four ticks, skip them all + $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; + + $TICK_WATCHER->at ($NEXT_TICK); + $TICK_WATCHER->start; + }, +); 1