--- deliantra/server/lib/cf.pm 2006/08/30 12:08:15 1.58 +++ deliantra/server/lib/cf.pm 2006/12/21 06:42:28 1.92 @@ -7,74 +7,85 @@ use Safe; use Safe::Hole; +use IO::AIO (); +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; -_reload_1; +sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload + +our %COMMAND = (); +our %COMMAND_TIME = (); +our %EXTCMD = (); + +_init_vars; -our %COMMAND = (); our @EVENT; -our %PROP_TYPE; -our %PROP_IDX; -our $LIBDIR = maps_directory "perl"; +our $LIBDIR = datadir . "/ext"; our $TICK = MAX_TIME * 1e-6; our $TICK_WATCHER; our $NEXT_TICK; -BEGIN { - *CORE::GLOBAL::warn = sub { - my $msg = join "", @_; - $msg .= "\n" - unless $msg =~ /\n$/; +our %CFG; - print STDERR "cfperl: $msg"; - LOG llevError, "cfperl: $msg"; - }; -} +our $UPTIME; $UPTIME ||= time; -my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! +############################################################################# -# generate property mutators -sub prop_gen { - my ($prefix, $class) = @_; +=head2 GLOBAL VARIABLES - no strict 'refs'; +=over 4 - for my $prop (keys %PROP_TYPE) { - $prop =~ /^\Q$prefix\E_(.*$)/ or next; - my $sub = lc $1; +=item $cf::UPTIME - my $type = $PROP_TYPE{$prop}; - my $idx = $PROP_IDX {$prop}; +The timestamp of the server start (so not actually an uptime). - *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { - $_[0]->get_property ($type, $idx) - }; +=item $cf::LIBDIR - *{"$class\::set_$sub"} = sub { - $_[0]->set_property ($type, $idx, $_[1]); - } unless $ignore_set{$prop}; - } -} +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 -# auto-generate most of the API +=cut -prop_gen OBJECT_PROP => "cf::object"; -# CFAPI_OBJECT_ANIMATION? -prop_gen PLAYER_PROP => "cf::object::player"; +BEGIN { + *CORE::GLOBAL::warn = sub { + my $msg = join "", @_; + $msg .= "\n" + unless $msg =~ /\n$/; -prop_gen MAP_PROP => "cf::map"; -prop_gen ARCH_PROP => "cf::arch"; + print STDERR "cfperl: $msg"; + LOG llevError, "cfperl: $msg"; + }; +} @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)) { +for my $pkg (qw( + cf::object cf::object::player + cf::client cf::player + cf::arch cf::living + cf::map cf::party cf::region +)) { no strict 'refs'; @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; } @@ -86,28 +97,44 @@ my %ext_pkg; my @exts; my @hook; -my %command; -my %extcmd; -############################################################################# -# utility functions +=head2 UTILITY FUNCTIONS + +=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 + ############################################################################# -# "new" plug-in system -=head3 EVENTS AND OBJECT ATTACHMENTS +=head2 EVENTS AND OBJECT ATTACHMENTS =over 4 @@ -127,12 +154,20 @@ =item $map->detach ($attachment) +Attach/detach a pre-registered attachment to a client. + +=item $client->attach ($attachment, key => $value...) + +=item $client->detach ($attachment) + Attach/detach a pre-registered attachment to a map. =item $bool = $object->attached ($name) =item $bool = $player->attached ($name) +=item $bool = $client->attached ($name) + =item $bool = $map->attached ($name) Checks wether the named attachment is currently attached to the object. @@ -188,6 +223,10 @@ Attach handlers to all players. +=item cf::attach_to_clients ... + +Attach handlers to all players. + =item cf::attach_to_maps ... Attach handlers to all maps. @@ -213,6 +252,7 @@ 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_CLIENT = (); our @CB_TYPE = (); # registry for type (cf-object class) based events our @CB_MAP = (); @@ -305,6 +345,7 @@ *cf::object::attach = *cf::player::attach = +*cf::client::attach = *cf::map::attach = sub { my ($obj, $name, %arg) = @_; @@ -314,6 +355,7 @@ # all those should be optimised *cf::object::detach = *cf::player::detach = +*cf::client::detach = *cf::map::detach = sub { my ($obj, $name) = @_; @@ -323,6 +365,7 @@ *cf::object::attached = *cf::player::attached = +*cf::client::attached = *cf::map::attached = sub { my ($obj, $name) = @_; @@ -348,6 +391,10 @@ _attach @CB_PLAYER, KLASS_PLAYER, @_ } +sub attach_to_clients { + _attach @CB_CLIENT, KLASS_CLIENT, @_ +} + sub attach_to_maps { _attach @CB_MAP, KLASS_MAP, @_ } @@ -364,6 +411,12 @@ $attachment{$name} = [[KLASS_PLAYER, @_]]; } +sub register_client_attachment { + my $name = shift; + + $attachment{$name} = [[KLASS_CLIENT, @_]]; +} + sub register_map_attachment { my $name = shift; @@ -391,7 +444,7 @@ if ($@) { warn "$@"; - warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; + warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; override; } @@ -407,6 +460,8 @@ =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) +=item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) + =item $bool = $map->invoke (EVENT_MAP_XXX, ...) Generate a global/object/player/map-specific event with the given arguments. @@ -417,15 +472,15 @@ =back -=head2 methods valid for all pointers +=cut -=over 4 +############################################################################# -=item $object->valid +=head2 METHODS VALID FOR ALL CORE OBJECTS -=item $player->valid +=over 4 -=item $map->valid +=item $object->valid, $player->valid, $client->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 @@ -439,6 +494,7 @@ *cf::object::valid = *cf::player::valid = +*cf::client::valid = *cf::map::valid = \&cf::_valid; ############################################################################# @@ -478,29 +534,61 @@ } sub object_freezer_save { - my ($filename, $objs) = @_; + my ($filename, $rdata, $objs) = @_; - if (@$objs) { - open my $fh, ">:raw", "$filename.pst~"; - syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; - close $fh; - chmod SAVE_MODE, "$filename.pst~"; - rename "$filename.pst~", "$filename.pst"; + 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_freezer_as_string { + my ($rdata, $objs) = @_; - chmod SAVE_MODE, "$filename~"; - rename "$filename~", $filename; + use Data::Dumper; + + $$rdata . Dumper $objs } sub object_thawer_load { my ($filename) = @_; - open my $fh, "<:raw:perlio", "$filename.pst" - or return; + 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); + } - eval { local $/; (Storable::thaw <$fh>)->{objs} } + () } attach_to_objects @@ -518,46 +606,32 @@ ; ############################################################################# -# old plug-in events +# command handling &c -sub inject_event { - my $extension = shift; - my $event_code = shift; +=item cf::register_command $name => \&callback($ob,$args); - my $cb = $hook[$event_code]{$extension} - or return; +Register a callback for execution when the client sends the user command +$name. - &$cb -} +=cut -sub inject_global_event { - my $event = shift; +sub register_command { + my ($name, $cb) = @_; - my $cb = $hook[$event] - or return; + my $caller = caller; + #warn "registering command '$name/$time' to '$caller'"; - List::Util::max map &$_, values %$cb + push @{ $COMMAND{$name} }, [$caller, $cb]; } -sub inject_command { - my ($name, $obj, $params) = @_; +=item cf::register_extcmd $name => \&callback($pl,$packet); - for my $cmd (@{ $command{$name} }) { - $cmd->[1]->($obj, $params); - } +Register a callbackf ro execution when the client sends an extcmd packet. - -1 -} +If the callback returns something, it is sent back as if reply was being +called. -sub register_command { - my ($name, $time, $cb) = @_; - - my $caller = caller; - #warn "registering command '$name/$time' to '$caller'"; - - push @{ $command{$name} }, [$time, $cb, $caller]; - $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; -} +=cut sub register_extcmd { my ($name, $cb) = @_; @@ -565,9 +639,41 @@ my $caller = caller; #warn "registering extcmd '$name' to '$caller'"; - $extcmd{$name} = [$cb, $caller]; + $EXTCMD{$name} = [$cb, $caller]; } +attach_to_players + on_command => sub { + my ($pl, $name, $params) = @_; + + my $cb = $COMMAND{$name} + or return; + + for my $cmd (@$cb) { + $cmd->[1]->($pl->ob, $params); + } + + cf::override; + }, + 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"; + } + + cf::override; + }, +; + sub register { my ($base, $pkg) = @_; @@ -595,7 +701,8 @@ . "\n};\n1"; eval $source - or die "$path: $@"; + or die $@ ? "$path: $@\n" + : "extension disabled.\n"; push @exts, $pkg; $ext_pkg{$base} = $pkg; @@ -618,21 +725,19 @@ # } # remove commands - for my $name (keys %command) { - my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; + for my $name (keys %COMMAND) { + my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; if (@cb) { - $command{$name} = \@cb; - $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; + $COMMAND{$name} = \@cb; } else { - delete $command{$name}; - delete $COMMAND{"$name\000"}; + delete $COMMAND{$name}; } } # remove extcmds - for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { - delete $extcmd{$name}; + for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { + delete $EXTCMD{$name}; } if (my $cb = $pkg->can ("unload")) { @@ -646,8 +751,6 @@ } sub load_extensions { - my $LIBDIR = maps_directory "perl"; - for my $ext (<$LIBDIR/*.ext>) { next unless -r $ext; eval { @@ -657,103 +760,6 @@ } } -sub _perl_reload(&) { - my ($msg) = @_; - - $msg->("reloading..."); - - eval { - # 1. cancel all watchers - $_->cancel for Event::all_watchers; - - # 2. unload all extensions - for (@exts) { - $msg->("unloading <$_>"); - unload_extension $_; - } - - # 3. 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; - } - - # 4. 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); - - # 5. remove register_script_function callbacks - # TODO - - # 6. 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__; - - # 7. reload cf.pm - $msg->("reloading cf.pm"); - require cf; - }; - $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]); - }; - } -}; - -############################################################################# -# extcmd framework, basically convert ext -# into pkg::->on_extcmd_arg1 (...) while shortcutting a few - -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"; - } - - cf::override; - }, -; - ############################################################################# # load/save/clean perl data associated with a map @@ -775,6 +781,7 @@ @_, map all_objects ($_->inv), @_ } +# TODO: compatibility cruft, remove when no longer needed attach_to_players on_load => sub { my ($pl, $path) = @_; @@ -790,7 +797,12 @@ ; ############################################################################# -# core extensions - in perl + +=head2 CORE EXTENSIONS + +Functions and methods that extend core crossfire objects. + +=over 4 =item cf::player::exists $login @@ -803,7 +815,7 @@ or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; } -=item $player->reply ($npc, $msg[, $flags]) +=item $player_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 @@ -840,8 +852,35 @@ $self->send ("ext " . to_json \%msg); } +=item $player_object->may ("access") + +Returns wether the given player is authorized to access resource "access" +(e.g. "command_wizcast"). + +=cut + +sub cf::object::player::may { + my ($self, $access) = @_; + + $self->flag (cf::FLAG_WIZ) || + (ref $cf::CFG{"may_$access"} + ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} + : $cf::CFG{"may_$access"}) +} + +=cut + ############################################################################# -# map scripting support + +=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 can be called is greatly reduced. + +=cut our $safe = new Safe "safe"; our $safe_hole = new Safe::Hole; @@ -852,10 +891,22 @@ # 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 map + cf::object::player player + cf::player peaceful + cf::map trigger + +=cut + for ( - ["cf::object" => qw(contr pay_amount pay_player)], + ["cf::object" => qw(contr pay_amount pay_player map)], ["cf::object::player" => qw(player)], ["cf::player" => qw(peaceful)], + ["cf::map" => qw(trigger)], ) { no strict 'refs'; my ($pkg, @funs) = @$_; @@ -863,6 +914,18 @@ 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) = @_; @@ -894,6 +957,21 @@ 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) = @_; @@ -901,32 +979,241 @@ *{"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 + +our $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, data => WF_AUTOCANCEL, 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 + for (Event::all_watchers) { + $_->cancel if $_->data & WF_AUTOCANCEL; + } + + # 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 "", __PACKAGE__; -unshift @INC, $LIBDIR; +register_command "perl-reload" => sub { + my ($who, $arg) = @_; -load_extensions; + if ($who->flag (FLAG_WIZ)) { + _perl_reload { + warn $_[0]; + $who->message ($_[0]); + }; + } +}; + +unshift @INC, $LIBDIR; $TICK_WATCHER = Event->timer ( - prio => 1, - at => $NEXT_TICK || 1, - cb => sub { + prio => 0, + at => $NEXT_TICK || 1, + data => WF_AUTOCANCEL, + 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 + # if we are delayed by four ticks or more, skip them all $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; $TICK_WATCHER->at ($NEXT_TICK); @@ -934,7 +1221,13 @@ }, ); -_reload_2; +IO::AIO::max_poll_time $TICK * 0.2; + +Event->io (fd => IO::AIO::poll_fileno, + poll => 'r', + prio => 5, + data => WF_AUTOCANCEL, + cb => \&IO::AIO::poll_cb); 1