--- deliantra/Deliantra-Client/DC.pm 2006/04/07 20:34:44 1.6 +++ deliantra/Deliantra-Client/DC.pm 2007/12/08 19:17:02 1.162 @@ -1,10 +1,10 @@ =head1 NAME -Crossfire::Client - undocumented utility garbage for our crossfire client +CFPlus - undocumented utility garbage for our crossfire client =head1 SYNOPSIS - use Crossfire::Client; + use CFPlus; =head1 DESCRIPTION @@ -12,169 +12,234 @@ =cut -package Crossfire::Client; +package CFPlus; + +use Carp (); BEGIN { - $VERSION = '0.1'; + $VERSION = '0.9959'; use XSLoader; - XSLoader::load "Crossfire::Client", $VERSION; + XSLoader::load "CFPlus", $VERSION; } -sub find_rcfile($) { - my $path; +use utf8; - for (@INC) { - $path = "$_/Crossfire/resources/$_[0]"; - return $path if -r $path; - } +use AnyEvent (); +use Pod::POM (); +use File::Path (); +use Storable (); # finally +use Fcntl (); +use JSON::XS qw(encode_json decode_json); - die "FATAL: can't find required file $_[0]\n"; -} +=item guard { BLOCK } -sub read_cfg { - my ($file) = @_; +Returns an object that executes the given block as soon as it is destroyed. - open CFG, $file - or return; +=cut - my $CFG; +sub guard(&) { + bless \(my $cb = $_[0]), "CFPlus::Guard" +} - local $/; - $CFG = eval ; +sub CFPlus::Guard::DESTROY { + ${$_[0]}->() +} - $::CFG = $CFG; +=item shorten $string[, $maxlength] - close CFG; -} +=cut -sub write_cfg { - my ($file) = @_; +sub shorten($;$) { + my ($str, $len) = @_; + substr $str, $len, (length $str), "..." if $len + 3 <= length $str; + $str +} - open CFG, ">$file" - or return; +sub asxml($) { + local $_ = $_[0]; - { - local $Data::Dumper::Purity = 1; - $::CFG->{VERSION} = $::VERSION; - print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); - } + s/&/&/g; + s/>/>/g; + s/&", $fh_w; + open STDERR, ">&", $fh_w; + close $fh_r; + close $fh_w; - $self->upload; + $| = 1; - $self -} + eval { $bg->() }; -sub new_from_image { - my ($class, $image) = @_; + if ($@) { + my $msg = $@; + $msg =~ s/\n+/\n/; + warn "FATAL: $msg"; + CFPlus::_exit 1; + } - $class->_new (image => $image) -} + # win32 is fucked up, of course. exit will clean stuff up, + # which destroys our database etc. _exit will exit ALL + # forked processes, because of the dreaded fork emulation. + CFPlus::_exit 0; + } -sub new_from_file { - my ($class, $path) = @_; + close $fh_w; - open my $fh, "<:raw", $path - or die "$path: $!"; + my $buffer; - local $/; - $class->new_from_image (<$fh>) + my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub { + unless (sysread $fh_r, $buffer, 4096, length $buffer) { + undef $w; + $cb->(); + return; + } + + while ($buffer =~ s/^(.*)\n//) { + my $line = $1; + $line =~ s/\s+$//; + utf8::decode $line; + if ($line =~ /^\x{e877}json_msg (.*)$/s) { + $cb->(JSON::XS->new->allow_nonref->decode ($1)); + } else { + ::message ({ + markup => "background($pid): " . CFPlus::asxml $line, + }); + } + } + }); +} + +sub background_msg { + my ($msg) = @_; + + $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg); + $msg =~ s/\n//g; + utf8::encode $msg; + print $msg, "\n"; } -sub new_from_surface { - my ($class, $surface) = @_; +package CFPlus; - $surface->rgba; +sub find_rcfile($) { + my $path; - $class->_new ( - data => $surface->pixels, - width => $surface->width, - height => $surface->height, - ) + for (grep !ref, @INC) { + $path = "$_/CFPlus/resources/$_[0]"; + return $path if -r $path; + } + + die "FATAL: can't find required file $_[0]\n"; } -sub new_from_ttf { - my ($class, $ttf, $text) = @_; +sub read_cfg { + my ($file) = @_; - utf8::upgrade $text; + open my $fh, $file + or return; - my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, - (new SDL::Color -r => 255, -g => 255, -b => 255); + local $/; + my $CFG = <$fh>; - $class->new_from_surface ($surface) + if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove + require YAML; + utf8::decode $CFG; + $::CFG = YAML::Load ($CFG); + } elsif ($CFG =~ /^\{/) { + $::CFG = decode_json $CFG; + } else { + $::CFG = eval $CFG; ## todo comaptibility cruft + } } -sub upload { - my ($self) = @_; +sub write_cfg { + my ($file) = @_; - return unless $SDL::App::USING_OPENGL; + $::CFG->{VERSION} = $::VERSION; - my $data; + open my $fh, ">:utf8", $file + or return; + print $fh encode_json $::CFG; +} - if (exists $self->{data}) { - $data = $self->{data}; +sub http_proxy { + my @proxy = win32_proxy_info; + + if (@proxy) { + "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0] + } elsif (exists $ENV{http_proxy}) { + $ENV{http_proxy} } else { - my $pb = new Gtk2::Gdk::PixbufLoader; - $pb->write ($self->{image}); - $pb->close; + () + } +} - $pb = $pb->get_pixbuf; - $pb = $pb->add_alpha (0, 0, 0, 0); +sub set_proxy { + my $proxy = http_proxy + or return; - $self->{width} = $pb->get_width; - $self->{height} = $pb->get_height; + $ENV{http_proxy} = $proxy; +} - $data = $pb->get_pixels; - } +sub lwp_useragent { + require LWP::UserAgent; + + CFPlus::set_proxy; - ($self->{name}) = @{glGenTextures 1}; + my $ua = LWP::UserAgent->new ( + agent => "cfplus $VERSION", + keep_alive => 1, + env_proxy => 1, + timeout => 30, + ); +} - glBindTexture GL_TEXTURE_2D, $self->{name}; +sub lwp_check($) { + my ($res) = @_; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; - - glTexImage2D GL_TEXTURE_2D, 0, - GL_RGBA8, - $self->{width}, $self->{height}, - 0, - GL_RGBA, - GL_UNSIGNED_BYTE, - $data; + $res->is_error + and die $res->status_line; + + $res } -sub DESTROY { - my ($self) = @_; +sub fh_nonblocking($$) { + my ($fh, $nb) = @_; - return unless exists $self->{name}; + if ($^O eq "MSWin32") { + $nb = (! ! $nb) + 0; + ioctl $fh, 0x8004667e, \$nb; # FIONBIO + } else { + fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; + } - glDeleteTextures delete $self->{name}; } -push @::GLINIT, sub { - $_->upload - for grep $_, @textures; +package CFPlus::Layout; + +$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { + reset_glyph_cache; }; 1;