--- deliantra/Deliantra-Client/DC.pm 2006/04/12 20:42:52 1.29 +++ deliantra/Deliantra-Client/DC.pm 2006/12/09 21:26:45 1.137 @@ -1,10 +1,10 @@ =head1 NAME -CFClient - undocumented utility garbage for our crossfire client +CFPlus - undocumented utility garbage for our crossfire client =head1 SYNOPSIS - use CFClient; + use CFPlus; =head1 DESCRIPTION @@ -12,252 +12,302 @@ =cut -package CFClient; +package CFPlus; + +use Carp (); BEGIN { - $VERSION = '0.1'; + $VERSION = '0.97'; use XSLoader; - XSLoader::load "CFClient", $VERSION; + XSLoader::load "CFPlus", $VERSION; } -use SDL::OpenGL; +use utf8; -our %GL_EXT; -our $GL_VERSION; +use AnyEvent (); +use BerkeleyDB; +use Pod::POM (); +use File::Path (); +use Storable (); # finally -our $GL_NPOT; +BEGIN { + use Crossfire::Protocol::Base (); + *to_json = \&Crossfire::Protocol::Base::to_json; + *from_json = \&Crossfire::Protocol::Base::from_json; +} -sub gl_init { - $GL_VERSION = gl_version * 1; - %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; +=item guard { BLOCK } - $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; +Returns an object that executes the given block as soon as it is destroyed. - glClearColor 0.45, 0.45, 0.45, 1; +=cut - glEnable GL_TEXTURE_2D; - glEnable GL_COLOR_MATERIAL; - glShadeModel GL_FLAT; - glDisable GL_DEPTH_TEST; - glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; +sub guard(&) { + bless \(my $cb = $_[0]), "CFPlus::Guard" +} - glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST; +sub CFPlus::Guard::DESTROY { + ${$_[0]}->() +} + +=item shorten $string[, $maxlength] + +=cut - CFClient::Texture::restore_state (); +sub shorten($;$) { + my ($str, $len) = @_; + substr $str, $len, (length $str), "..." if $len + 3 <= length $str; + $str } -sub find_rcfile($) { - my $path; +sub asxml($) { + local $_ = $_[0]; - for (@INC) { - $path = "$_/CFClient/resources/$_[0]"; - return $path if -r $path; - } + s/&/&/g; + s/>/>/g; + s/; + my ($fh_r, $fh_w) = CFPlus::socketpipe; - $::CFG = $CFG; + my $pid = fork; - close CFG; -} + if (defined $pid && !$pid) { + local $SIG{__DIE__}; -sub write_cfg { - my ($file) = @_; + open STDOUT, ">&", $fh_w; + open STDERR, ">&", $fh_w; + close $fh_r; + close $fh_w; - open CFG, ">$file" - or return; + $| = 1; - { - require Data::Dumper; - local $Data::Dumper::Purity = 1; - $::CFG->{VERSION} = $::VERSION; - print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); + eval { $bg->() }; + + if ($@) { + my $msg = $@; + $msg =~ s/\n+/\n/; + warn "FATAL: $msg"; + CFPlus::_exit 1; + } + + # 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; } - close CFG; + 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->(from_json $1); + } else { + ::message ({ + markup => "background($pid): " . CFPlus::asxml $line, + }); + } + } + }); } -package CFClient::Texture; +sub background_msg { + my ($msg) = @_; -use strict; + $msg = "\x{e877}json_msg " . to_json $msg; + $msg =~ s/\n//g; + utf8::encode $msg; + print $msg, "\n"; +} -use Scalar::Util; +package CFPlus::Database; -use SDL::OpenGL; +our @ISA = BerkeleyDB::Btree::; -my @textures; +sub get($$) { + my $data; -sub new { - my ($class, %data) = @_; + $_[0]->db_get ($_[1], $data) == 0 + ? $data + : () +} - my $self = bless { - internalformat => GL_RGBA, - format => GL_RGBA, - type => GL_UNSIGNED_BYTE, - %data, - }, $class; +my %DB_SYNC; - push @textures, $self; - Scalar::Util::weaken $textures[-1]; +sub put($$$) { + my ($db, $key, $data) = @_; - $self->upload; + my $hkey = $db + 0; + CFPlus::weaken $db; + $DB_SYNC{$hkey} ||= AnyEvent->timer (after => 5, cb => sub { + delete $DB_SYNC{$hkey}; + $db->db_sync if $db; + }); - $self + $db->db_put ($key => $data) } -sub new_from_image { - my ($class, $image) = @_; +package CFPlus; - $class->new (image => $image) +sub find_rcfile($) { + my $path; + + 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_file { - my ($class, $path) = @_; +sub read_cfg { + my ($file) = @_; - open my $fh, "<:raw", $path - or die "$path: $!"; + open my $fh, $file + or return; local $/; - $class->new_from_image (<$fh>) -} + my $CFG = <$fh>; -#sub new_from_surface { -# my ($class, $surface) = @_; -# -# $surface->rgba; -# -# $class->new ( -# data => $surface->pixels, -# w => $surface->width, -# h => $surface->height, -# ) -#} + if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove + require YAML; + utf8::decode $CFG; + $::CFG = YAML::Load ($CFG); + } elsif ($CFG =~ /^\{/) { + $::CFG = from_json $CFG; + } else { + $::CFG = eval $CFG; ## todo comaptibility cruft + } +} -sub new_from_layout { - my ($class, $layout) = @_; +sub write_cfg { + my ($file) = @_; - my ($w, $h, $data) = $layout->render; + $::CFG->{VERSION} = $::VERSION; - $class->new ( - w => $w, - h => $h, - data => $data, - internalformat => GL_ALPHA4, - format => GL_ALPHA, - type => GL_UNSIGNED_BYTE, - ) + open my $fh, ">:utf8", $file + or return; + print $fh to_json $::CFG; } -sub new_from_opengl { - my ($class, $w, $h, $cb) = @_; +sub http_proxy { + my @proxy = win32_proxy_info; - $class->new (w => $w, h => $h, render_cb => $cb) + 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 topot { - (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] +sub set_proxy { + my $proxy = http_proxy + or return; + + $ENV{http_proxy} = $proxy; } -sub upload { - my ($self) = @_; +sub lwp_useragent { + require LWP::UserAgent; + + CFPlus::set_proxy; - return unless $GL_VERSION; + my $ua = LWP::UserAgent->new ( + agent => "cfplus $VERSION", + keep_alive => 1, + env_proxy => 1, + timeout => 30, + ); +} - my $data; +sub lwp_check($) { + my ($res) = @_; - if (exists $self->{data}) { - $data = $self->{data}; + $res->is_error + and die $res->status_line; - } elsif (exists $self->{render_cb}) { - glViewport 0, 0, $self->{w}, $self->{h}; - glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; - glMatrixMode GL_PROJECTION; - glLoadIdentity; - glMatrixMode GL_MODELVIEW; - glLoadIdentity; - glClear GL_COLOR_BUFFER_BIT; - $self->{render_cb}->($self, $self->{w}, $self->{h}); + $res +} - } else { - ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) - = CFClient::load_image_inline $self->{image}; - } +our $DB_ENV; +our $DB_STATE; - my ($tw, $th) = @$self{qw(w h)}; +sub db_table($) { + my ($table) = @_; - unless ($tw && $th) { - $tw = $th = 1; - $data = "\x00" x 64; - } + $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; - unless ($GL_NPOT) { - # TODO: does not work for zero-sized textures - $tw = topot $tw; - $th = topot $th; - - if ($tw != $self->{w} || $th != $self->{h} && defined $data) { - my $bpp = (length $data) / ($self->{w} * $self->{h}); - $data = pack "(a" . ($tw * $bpp) . ")*", - unpack "(a" . ($self->{w} * $bpp) . ")*", $data; - $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); - } - } + new CFPlus::Database + -Env => $DB_ENV, + -Filename => $table, +# -Filename => "database", +# -Subname => $table, + -Property => DB_CHKSUM, + -Flags => DB_CREATE | DB_UPGRADE, + or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" +} - $self->{s} = $self->{w} / $tw; - $self->{t} = $self->{h} / $th; +our $DB_HOME = "$Crossfire::VARDIR/cfplus"; - $self->{name} ||= (glGenTextures 1)->[0]; +sub open_db { + use strict; - glBindTexture GL_TEXTURE_2D, $self->{name}; + mkdir $DB_HOME, 0777; + my $recover = $BerkeleyDB::db_version >= 4.4 + ? eval "DB_REGISTER | DB_RECOVER" + : 0; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; - - if (defined $data) { - glTexImage2D GL_TEXTURE_2D, 0, - $self->{internalformat}, - $tw, $th, # need to pad texture first - 0, - $self->{format}, - $self->{type}, - $data; - glGetError and die; - } else { - glCopyTexImage2D GL_TEXTURE_2D, 0, - $self->{internalformat}, - 0, 0, - $tw, $th, - 0; - glGetError and die; - } -} + $DB_ENV = new BerkeleyDB::Env + -Home => $DB_HOME, + -Cachesize => 1_000_000, + -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 $DB_HOME: $BerkeleyDB::Error"; -sub DESTROY { - my ($self) = @_; + $DB_STATE = db_table "state"; - return unless exists $self->{name}; + 1 +} - glDeleteTextures delete $self->{name}; +unless (eval { open_db }) { + File::Path::rmtree $DB_HOME; + open_db; } -sub restore_state{ - $_->upload - for grep $_, @textures; +package CFPlus::Layout; + +$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { + reset_glyph_cache; }; 1;