--- deliantra/Deliantra-Client/DC.pm 2006/04/08 18:48:34 1.12 +++ deliantra/Deliantra-Client/DC.pm 2012/11/17 10:40:08 1.216 @@ -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,203 +12,334 @@ =cut -package Crossfire::Client; +package DC; + +use Carp (); + +our $VERSION; BEGIN { - $VERSION = '0.1'; + $VERSION = '3.0'; use XSLoader; - XSLoader::load "Crossfire::Client", $VERSION; + XSLoader::load "Deliantra::Client", $VERSION; } -sub find_rcfile($) { - my $path; +use utf8; +use strict qw(vars subs); - for (@INC) { - $path = "$_/Crossfire/resources/$_[0]"; - return $path if -r $path; - } +use Socket (); +use AnyEvent (); +use AnyEvent::Util (); +use Pod::POM (); +use File::Path (); +use Storable (); # finally +use Fcntl (); +use JSON::XS qw(encode_json decode_json); +use Guard qw(guard); + +# modules to support other DC::* packages +use List::Util (); +use IO::AIO (); +use Coro::AIO (); +use AnyEvent::AIO (); - die "FATAL: can't find required file $_[0]\n"; -} +use Deliantra::Util (); +use Deliantra::Protocol::Constants (); -sub read_cfg { - my ($file) = @_; +=item shorten $string[, $maxlength] - open CFG, $file - or return; +=cut - my $CFG; +sub shorten($;$) { + my ($str, $len) = @_; + substr $str, $len, (length $str), "..." if $len + 3 <= length $str; + $str +} - local $/; - $CFG = eval ; +sub asxml($) { + local $_ = $_[0]; - $::CFG = $CFG; + s/&/&/g; + s/>/>/g; + s/$file" - or return; +sub background(&;&) { + my ($bg, $cb) = @_; - { - require Data::Dumper; - local $Data::Dumper::Purity = 1; - $::CFG->{VERSION} = $::VERSION; - print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); - } + my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair + or die "unable to create background socketpair: $!"; - close CFG; -} + my $pid = fork; -package Crossfire::Client::Texture; + if (defined $pid && !$pid) { + local $SIG{__DIE__}; -use Scalar::Util; + open STDOUT, ">&", $fh_w; + open STDERR, ">&", $fh_w; + close $fh_r; + close $fh_w; -use SDL::OpenGL; + $| = 1; -my @textures; + eval { $bg->() }; -sub _new { - my ($class, %data) = @_; + if ($@) { + my $msg = $@; + $msg =~ s/\n+/\n/; + warn "FATAL: $msg"; + DC::_exit 1; + } - my $self = bless \%data, $class; + # 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; + } - push @textures, $self; - Scalar::Util::weaken $textures[-1]; + close $fh_w; - $self->upload; + my $buffer; - $self + 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; + +our $RC_THEME; +our %THEME; +our @RC_PATH; +our $RC_BASE; + +for (grep !ref, @INC) { + $RC_BASE = "$_/Deliantra/Client/private/resources"; + last if -d $RC_BASE; } -sub new_from_image { - my ($class, $image) = @_; +sub find_rcfile($) { + my $path; - $class->_new (image => $image) + for (@RC_PATH, "") { + $path = "$RC_BASE/$_/$_[0]"; + return $path if -e $path; + } + + die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; } -sub new_from_file { - my ($class, $path) = @_; +sub load_json($) { + my ($file) = @_; - open my $fh, "<:raw", $path - or die "$path: $!"; + open my $fh, $file + or return; local $/; - $class->new_from_image (<$fh>) + eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) } } -sub new_from_surface { - my ($class, $surface) = @_; +sub set_theme($) { + return if $RC_THEME eq $_[0]; + $RC_THEME = $_[0]; - $surface->rgba; + # kind of hacky, find the main theme file, then load all theme files and merge them - $class->_new ( - data => $surface->pixels, - width => $surface->width, - height => $surface->height, - ) -} + %THEME = (); + @RC_PATH = "theme-$RC_THEME"; -sub new_from_ttf { - my ($class, $ttf, $text) = @_; + my $theme = load_json find_rcfile "theme.json" + or die "FATAL: theme resource file not found"; - utf8::encode $text; + @RC_PATH = @{ $theme->{path} } if $theme->{path}; - my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, - (new SDL::Color -r => 255, -g => 255, -b => 255); + for (@RC_PATH, "") { + my $theme = load_json "$RC_BASE/$_/theme.json" + or next; - $class->new_from_surface (bless \$surface, SDL::Surface::) + %THEME = ( %$theme, %THEME ); + } } -sub new_from_opengl { - my ($class, $w, $h, $cb) = @_; +sub read_cfg($) { + my ($file) = @_; - $class->_new (width => $w, height => $h, rendercb => $cb) + $::CFG = (load_json $file) || (load_json "$file.bak"); } -sub upload { - my ($self) = @_; +sub write_cfg($) { + my $file = "$Deliantra::VARDIR/client.cf"; - return unless $SDL::App::USING_OPENGL; + $::CFG->{VERSION} = $::VERSION; + $::CFG->{layout} = DC::UI::get_layout (); - my $data; + open my $fh, ">:utf8", "$file~" + or return; + print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); + close $fh; - if (exists $self->{data}) { - $data = $self->{data}; - } elsif (exists $self->{rendercb}) { - glViewport 0, 0, $self->{width}, $self->{height}; - glMatrixMode GL_PROJECTION; - glLoadIdentity; - glOrtho 0, $self->{width}, $self->{height}, 0, -100, 100; - glMatrixMode GL_MODELVIEW; - glPushmatrix; - glLoadIdentity; - glClear GL_COLOR_BUFFER_BIT; + rename $file, "$file.bak"; + rename "$file~", $file; +} - $self->{rendercb}->($self, $self->{width}, $self->{height}); +sub load_cfg() { + if (-e "$Deliantra::VARDIR/client.cf") { + DC::read_cfg "$Deliantra::VARDIR/client.cf"; } else { - my $pb = new Gtk2::Gdk::PixbufLoader; - $pb->write ($self->{image}); - $pb->close; + $::CFG = { cfg_schema => 1, db_schema => 1 }; + } +} - $pb = $pb->get_pixbuf; - $pb = $pb->add_alpha (0, 0, 0, 0); +sub save_cfg() { + write_cfg "$Deliantra::VARDIR/client.cf"; +} - $self->{width} = $pb->get_width; - $self->{height} = $pb->get_height; +sub upgrade_cfg() { + my %DEF_CFG = ( + config_autosave => 1, + sdl_mode => undef, + fullscreen => 1, + fast => 0, + force_opengl11 => undef, + disable_alpha => 0, + smooth_movement => 1, + smooth_transitions => 1, + texture_compression => 1, + map_scale => 1, + fow_enable => 1, + fow_intensity => 0, + fow_texture => 0, + map_smoothing => 1, + gui_fontsize => 1, + log_fontsize => 0.7, + gauge_fontsize => 1, + gauge_size => 0.35, + stat_fontsize => 0.7, + mapsize => 100, + audio_enable => 1, + audio_hw_channels => 0, + audio_hw_frequency => 0, + audio_hw_chunksize => 0, + audio_mix_channels => 8, + effects_enable => 1, + effects_volume => 1, + bgm_enable => 1, + bgm_volume => 0.5, + output_rate => "", + pickup => Deliantra::Protocol::Constants::PICKUP_SPELLBOOK + | Deliantra::Protocol::Constants::PICKUP_SKILLSCROLL + | Deliantra::Protocol::Constants::PICKUP_VALUABLES, + inv_sort => "mtime", + default => "profile", # default profile + show_tips => 1, + logview_max_par => 1000, + shift_fire_stop => 0, + uitheme => "wood", + map_shift_x => -24, # arbitrary + map_shift_y => +24, # arbitrary + ); - $data = $pb->get_pixels; + while (my ($k, $v) = each %DEF_CFG) { + $::CFG->{$k} = $v unless exists $::CFG->{$k}; } - ($self->{name}) = @{glGenTextures 1}; + if ($::CFG->{cfg_schema} < 1) { + for my $profile (values %{ $::CFG->{profile} }) { + $profile->{password} = unpack "H*", Deliantra::Util::hash_pw $profile->{password}; + } + $::CFG->{cfg_schema} = 1; + } +} - glBindTexture GL_TEXTURE_2D, $self->{name}; +sub http_proxy { + my @proxy = win32_proxy_info; - 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; - - if (defined $data) { - glTexImage2D GL_TEXTURE_2D, 0, - GL_RGBA8, - $self->{width}, $self->{height}, - 0, - GL_RGBA, - GL_UNSIGNED_BYTE, - $data; + if (@proxy) { + "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0] + } elsif (exists $ENV{http_proxy}) { + $ENV{http_proxy} } else { - warn "I{DTEDRAW TEX: $self->{width} $self->{height} \n"; - warn "ERR: ". (glGetError). "\n"; - my ($bd, $br) = (glGet (GL_DRAW_BUFFER), glGet (GL_READ_BUFFER)); - warn "BUF:[$bd $br]\n"; - - glCopyTexImage2D GL_TEXTURE_2D, 0, - GL_RGBA8, - 0, 0, - $self->{width}, $self->{height}, - 0; - glPopmatrix; - warn "ERR: ". (glGetError). "\n"; - sleep 1; + () } } -sub DESTROY { - my ($self) = @_; +sub set_proxy { + my $proxy = http_proxy + or return; + + $ENV{http_proxy} = $proxy; +} - return unless exists $self->{name}; +sub lwp_useragent { + require LWP::UserAgent; + + DC::set_proxy; - glDeleteTextures delete $self->{name}; + my $ua = LWP::UserAgent->new ( + agent => "deliantra $VERSION", + keep_alive => 1, + env_proxy => 1, + timeout => 30, + ); } -push @::GLINIT, sub { - $_->upload - for grep $_, @textures; +sub lwp_check($) { + my ($res) = @_; + + $res->is_error + and die $res->status_line; + + $res +} + +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;