--- deliantra/Deliantra-Client/DC.pm 2007/09/06 15:06:25 1.152 +++ deliantra/Deliantra-Client/DC.pm 2012/11/17 10:40:08 1.216 @@ -1,10 +1,10 @@ =head1 NAME -CFPlus - undocumented utility garbage for our crossfire client +DC - undocumented utility garbage for our deliantra client =head1 SYNOPSIS - use CFPlus; + use DC; =head1 DESCRIPTION @@ -12,39 +12,40 @@ =cut -package CFPlus; +package DC; use Carp (); +our $VERSION; + BEGIN { - $VERSION = '0.991'; + $VERSION = '3.0'; use XSLoader; - XSLoader::load "CFPlus", $VERSION; + XSLoader::load "Deliantra::Client", $VERSION; } use utf8; +use strict qw(vars subs); +use Socket (); use AnyEvent (); +use AnyEvent::Util (); use Pod::POM (); use File::Path (); use Storable (); # finally use Fcntl (); -use JSON::XS qw(to_json from_json); - -=item guard { BLOCK } - -Returns an object that executes the given block as soon as it is destroyed. +use JSON::XS qw(encode_json decode_json); +use Guard qw(guard); -=cut +# modules to support other DC::* packages +use List::Util (); +use IO::AIO (); +use Coro::AIO (); +use AnyEvent::AIO (); -sub guard(&) { - bless \(my $cb = $_[0]), "CFPlus::Guard" -} - -sub CFPlus::Guard::DESTROY { - ${$_[0]}->() -} +use Deliantra::Util (); +use Deliantra::Protocol::Constants (); =item shorten $string[, $maxlength] @@ -66,17 +67,11 @@ $_ } -sub socketpipe() { - socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC - or die "cannot establish bidirectional pipe: $!\n"; - - ($fh1, $fh2) -} - sub background(&;&) { my ($bg, $cb) = @_; - my ($fh_r, $fh_w) = CFPlus::socketpipe; + my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair + or die "unable to create background socketpair: $!"; my $pid = fork; @@ -96,13 +91,13 @@ my $msg = $@; $msg =~ s/\n+/\n/; warn "FATAL: $msg"; - CFPlus::_exit 1; + DC::_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; + DC::_exit 0; } close $fh_w; @@ -124,7 +119,7 @@ $cb->(JSON::XS->new->allow_nonref->decode ($1)); } else { ::message ({ - markup => "background($pid): " . CFPlus::asxml $line, + markup => "background($pid): " . DC::asxml $line, }); } } @@ -140,47 +135,149 @@ print $msg, "\n"; } -package CFPlus; +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 find_rcfile($) { my $path; - for (grep !ref, @INC) { - $path = "$_/CFPlus/resources/$_[0]"; - return $path if -r $path; + for (@RC_PATH, "") { + $path = "$RC_BASE/$_/$_[0]"; + return $path if -e $path; } - die "FATAL: can't find required file $_[0]\n"; + die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; } -sub read_cfg { +sub load_json($) { my ($file) = @_; open my $fh, $file or return; local $/; - my $CFG = <$fh>; + eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) } +} - 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 set_theme($) { + return if $RC_THEME eq $_[0]; + $RC_THEME = $_[0]; + + # kind of hacky, find the main theme file, then load all theme files and merge them + + %THEME = (); + @RC_PATH = "theme-$RC_THEME"; + + my $theme = load_json find_rcfile "theme.json" + or die "FATAL: theme resource file not found"; + + @RC_PATH = @{ $theme->{path} } if $theme->{path}; + + for (@RC_PATH, "") { + my $theme = load_json "$RC_BASE/$_/theme.json" + or next; + + %THEME = ( %$theme, %THEME ); } } -sub write_cfg { +sub read_cfg($) { my ($file) = @_; + $::CFG = (load_json $file) || (load_json "$file.bak"); +} + +sub write_cfg($) { + my $file = "$Deliantra::VARDIR/client.cf"; + $::CFG->{VERSION} = $::VERSION; + $::CFG->{layout} = DC::UI::get_layout (); - open my $fh, ">:utf8", $file + open my $fh, ">:utf8", "$file~" or return; - print $fh to_json $::CFG; + print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); + close $fh; + + rename $file, "$file.bak"; + rename "$file~", $file; +} + +sub load_cfg() { + if (-e "$Deliantra::VARDIR/client.cf") { + DC::read_cfg "$Deliantra::VARDIR/client.cf"; + } else { + $::CFG = { cfg_schema => 1, db_schema => 1 }; + } +} + +sub save_cfg() { + write_cfg "$Deliantra::VARDIR/client.cf"; +} + +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 + ); + + while (my ($k, $v) = each %DEF_CFG) { + $::CFG->{$k} = $v unless exists $::CFG->{$k}; + } + + 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; + } } sub http_proxy { @@ -205,10 +302,10 @@ sub lwp_useragent { require LWP::UserAgent; - CFPlus::set_proxy; + DC::set_proxy; my $ua = LWP::UserAgent->new ( - agent => "cfplus $VERSION", + agent => "deliantra $VERSION", keep_alive => 1, env_proxy => 1, timeout => 30, @@ -233,13 +330,16 @@ } else { fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; } - } -package CFPlus::Layout; +package DC::Layout; + +$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub { + glyph_cache_restore; +}; -$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { - reset_glyph_cache; +$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub { + glyph_cache_backup; }; 1;