--- deliantra/Deliantra-Client/DC.pm 2006/04/07 18:20:13 1.3 +++ deliantra/Deliantra-Client/DC.pm 2008/03/30 06:05:52 1.178 @@ -1,10 +1,10 @@ =head1 NAME -Crossfire::Client - undocumented utility garbage for our crossfire client +DC - undocumented utility garbage for our deliantra client =head1 SYNOPSIS - use Crossfire::Client; + use DC; =head1 DESCRIPTION @@ -12,97 +12,230 @@ =cut -package Crossfire::Client; +package DC; + +use Carp (); BEGIN { - $VERSION = '0.1'; + $VERSION = '0.9970'; use XSLoader; - XSLoader::load "Crossfire::Client", $VERSION; + XSLoader::load "Deliantra::Client", $VERSION; } -package Crossfire::Client::Texture; +use utf8; -use Scalar::Util; +use AnyEvent (); +use Pod::POM (); +use File::Path (); +use Storable (); # finally +use Fcntl (); +use JSON::XS qw(encode_json decode_json); -use SDL::OpenGL; +=item guard { BLOCK } -my @textures; +Returns an object that executes the given block as soon as it is destroyed. -sub new_from_file { - my ($class, $path) = @_; +=cut - open my $fh, "<:raw", $path - or die "$path: $!"; +sub guard(&) { + bless \(my $cb = $_[0]), "DC::Guard" +} - local $/; - $class->new_from_data (<$fh>) +sub DC::Guard::DESTROY { + ${$_[0]}->() +} + +=item shorten $string[, $maxlength] + +=cut + +sub shorten($;$) { + my ($str, $len) = @_; + substr $str, $len, (length $str), "..." if $len + 3 <= length $str; + $str } -sub new_from_image { - my ($class, $image) = @_; +sub asxml($) { + local $_ = $_[0]; - my $self = bless { - image => $data, - }; + s/&/&/g; + s/>/>/g; + s/upload; +sub socketpipe() { + socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC + or die "cannot establish bidirectional pipe: $!\n"; - $self + ($fh1, $fh2) } -sub upload { - my ($self) = @_; +sub background(&;&) { + my ($bg, $cb) = @_; - return unless $SDL::App::USING_OPENGL; + my ($fh_r, $fh_w) = DC::socketpipe; - my ($data, $width, $height); + my $pid = fork; - if (exists $self->{data}) { - ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); - } else { - my $pb = new Gtk2::Gdk::PixbufLoader; - $pb->write ($self->{data}); - $pb->close; + if (defined $pid && !$pid) { + local $SIG{__DIE__}; + + open STDOUT, ">&", $fh_w; + open STDERR, ">&", $fh_w; + close $fh_r; + close $fh_w; + + $| = 1; + + eval { $bg->() }; - $pb = $pb->get_pixbuf; - $pb = $pb->add_alpha (0, 0, 0, 0); + if ($@) { + my $msg = $@; + $msg =~ s/\n+/\n/; + warn "FATAL: $msg"; + DC::_exit 1; + } - ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height); + # 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. + DC::_exit 0; } - ($self->{name}) = @{glGenTextures 1}; + close $fh_w; + + my $buffer; + + 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): " . DC::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"; +} + +package DC; + +sub find_rcfile($) { + my $path; + + for (grep !ref, @INC) { + $path = "$_/Deliantra/Client/private/resources/$_[0]"; + return $path if -r $path; + } + + die "FATAL: can't find required file $_[0]\n"; +} + +sub read_cfg { + my ($file) = @_; - glBindTexture GL_TEXTURE_2D, $self->{name}; + open my $fh, $file + or return; - 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; + local $/; + my $CFG = <$fh>; + + $::CFG = decode_json $CFG; +} + +sub write_cfg { + my $file = "$Deliantra::VARDIR/client.cf"; + + $::CFG->{VERSION} = $::VERSION; + + open my $fh, ">:utf8", $file + or return; + print $fh encode_json $::CFG; +} + +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 { + () + } +} + +sub set_proxy { + my $proxy = http_proxy + or return; + + $ENV{http_proxy} = $proxy; +} + +sub lwp_useragent { + require LWP::UserAgent; - glTexImage2D GL_TEXTURE_2D, 0, - GL_RGBA8, - $width, $height, - 0, - GL_RGBA, - GL_UNSIGNED_BYTE, - $data; + DC::set_proxy; + + my $ua = LWP::UserAgent->new ( + agent => "deliantra $VERSION", + keep_alive => 1, + env_proxy => 1, + timeout => 30, + ); } -sub DESTROY { - my ($self) = @_; +sub lwp_check($) { + my ($res) = @_; - return unless exists $self->{name}; + $res->is_error + and die $res->status_line; - glDeleteTextures delete $self->{name}; + $res } -push @::GLINIT, sub { - $_->upload - for grep $_, @textures; +sub fh_nonblocking($$) { + my ($fh, $nb) = @_; + + if ($^O eq "MSWin32") { + $nb = (! ! $nb) + 0; + ioctl $fh, 0x8004667e, \$nb; # FIONBIO + } else { + fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; + } + +} + +package DC::Layout; + +$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub { + glyph_cache_restore; +}; + +$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub { + glyph_cache_backup; }; 1;