--- deliantra/Deliantra-Client/DC.pm 2006/10/18 12:53:45 1.125 +++ deliantra/Deliantra-Client/DC.pm 2006/12/07 15:57:13 1.135 @@ -17,29 +17,27 @@ use Carp (); BEGIN { - $VERSION = '0.53'; + $VERSION = '0.97'; use XSLoader; XSLoader::load "CFPlus", $VERSION; } -BEGIN { - $SIG{__DIE__} = sub { - return if CFPlus::in_destruct; - #CFPlus::fatal $_[0];#d# - CFPlus::error Carp::longmess $_[0];#d# - die;#d# - }; -} - use utf8; use AnyEvent (); use BerkeleyDB; use Pod::POM (); use Scalar::Util (); +use File::Path (); use Storable (); # finally +BEGIN { + use Crossfire::Protocol::Base (); + *to_json = \&Crossfire::Protocol::Base::to_json; + *from_json = \&Crossfire::Protocol::Base::from_json; +} + =item guard { BLOCK } Returns an object that executes the given block as soon as it is destroyed. @@ -54,6 +52,16 @@ ${$_[0]}->() } +=item shorten $string[, $maxlength] + +=cut + +sub shorten($;$) { + my ($str, $len) = @_; + substr $str, $len, (length $str), "..." if $len + 3 <= length $str; + $str +} + sub asxml($) { local $_ = $_[0]; @@ -71,8 +79,8 @@ ($fh1, $fh2) } -sub background(&) { - my ($cb) = @_; +sub background(&;&) { + my ($bg, $cb) = @_; my ($fh_r, $fh_w) = CFPlus::socketpipe; @@ -88,7 +96,7 @@ $| = 1; - eval { $cb->() }; + eval { $bg->() }; if ($@) { my $msg = $@; @@ -107,23 +115,37 @@ my $buffer; - Event->io (fd => $fh_r, poll => 'r', cb => sub { + my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub { unless (sysread $fh_r, $buffer, 4096, length $buffer) { - $_[0]->w->cancel; - $buffer .= "done\n"; + undef $w; + $cb->(); + return; } while ($buffer =~ s/^(.*)\n//) { my $line = $1; $line =~ s/\s+$//; utf8::decode $line; - ::message ({ - markup => "editor($pid): " . CFPlus::asxml $line, - }); + if ($line =~ /^\x{e877}json_msg (.*)$/s) { + $cb->(from_json $1); + } else { + ::message ({ + markup => "background($pid): " . CFPlus::asxml $line, + }); + } } }); } +sub background_msg { + my ($msg) = @_; + + $msg = "\x{e877}json_msg " . to_json $msg; + $msg =~ s/\n//g; + utf8::encode $msg; + print $msg, "\n"; +} + package CFPlus::Database; our @ISA = BerkeleyDB::Btree::; @@ -164,12 +186,6 @@ die "FATAL: can't find required file $_[0]\n"; } -BEGIN { - use Crossfire::Protocol::Base (); - *to_json = \&Crossfire::Protocol::Base::to_json; - *from_json = \&Crossfire::Protocol::Base::from_json; -} - sub read_cfg { my ($file) = @_; @@ -219,6 +235,28 @@ $ENV{http_proxy} = $proxy; } +sub lwp_useragent { + require LWP::UserAgent; + + CFPlus::set_proxy; + + my $ua = LWP::UserAgent->new ( + agent => "cfplus $VERSION", + keep_alive => 1, + env_proxy => 1, + timeout => 30, + ); +} + +sub lwp_check($) { + my ($res) = @_; + + $res->is_error + and die $res->status_line; + + $res +} + our $DB_ENV; our $DB_STATE; @@ -237,25 +275,34 @@ or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" } -{ +our $DB_HOME = "$Crossfire::VARDIR/cfplus"; + +sub open_db { use strict; - mkdir "$Crossfire::VARDIR/cfplus", 0777; + mkdir $DB_HOME, 0777; my $recover = $BerkeleyDB::db_version >= 4.4 ? eval "DB_REGISTER | DB_RECOVER" : 0; $DB_ENV = new BerkeleyDB::Env - -Home => "$Crossfire::VARDIR/cfplus", + -Home => $DB_HOME, -Cachesize => 1_000_000, - -ErrFile => "$Crossfire::VARDIR/cfplus/errorlog.txt", + -ErrFile => "$DB_HOME/errorlog.txt", # -ErrPrefix => "DATABASE", -Verbose => 1, -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, - or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error"; + or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error"; $DB_STATE = db_table "state"; + + 1 +} + +unless (eval { open_db }) { + File::Path::rmtree $DB_HOME; + open_db; } package CFPlus::Layout; @@ -364,6 +411,8 @@ $move_prefix = "take"; } + my $shortname = CFPlus::shorten $self->{name}, 14; + my @menu_items = ( ["examine", sub { $::CONN->send ("examine $self->{tag}") }], ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }], @@ -408,6 +457,7 @@ ] ) ), + ["bind apply $shortname to a key" => sub { $::BIND_EDITOR->do_quick_binding (["apply $self->{name}"]) }], ); CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);