--- deliantra/Deliantra-Client/DC.pm 2008/09/03 06:07:39 1.191 +++ deliantra/Deliantra-Client/DC.pm 2009/03/29 22:28:12 1.205 @@ -12,8 +12,6 @@ =cut -package Deliantra::Client; # work around CPAN breakage -package App::Deliantra; # try to reserve namespace package DC; use Carp (); @@ -21,7 +19,7 @@ our $VERSION; BEGIN { - $VERSION = '0.9975'; + $VERSION = '2.03'; use XSLoader; XSLoader::load "Deliantra::Client", $VERSION; @@ -30,26 +28,15 @@ 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(encode_json decode_json); - -=item guard { BLOCK } - -Returns an object that executes the given block as soon as it is destroyed. - -=cut - -sub guard(&) { - bless \(my $cb = $_[0]), "DC::Guard" -} - -sub DC::Guard::DESTROY { - ${$_[0]}->() -} +use Guard qw(guard); =item shorten $string[, $maxlength] @@ -71,17 +58,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) = DC::socketpipe; + my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair + or die "unable to create background socketpair: $!"; my $pid = fork; @@ -148,7 +129,7 @@ package DC; our $RC_THEME; -our $THEME; +our %THEME; our @RC_PATH; our $RC_BASE; @@ -162,49 +143,63 @@ for (@RC_PATH, "") { $path = "$RC_BASE/$_/$_[0]"; - return $path if -r $path; + return $path if -e $path; } die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; } +sub load_json($) { + my ($file) = @_; + + open my $fh, $file + or return; + + local $/; + eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) } +} + 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"; - { - open my $fh, "<:raw", find_rcfile "theme.json" - or die "cannot open theme description file"; + my $theme = load_json find_rcfile "theme.json" + or die "FATAL: theme resource file not found"; - local $/; - $THEME = JSON::XS->new->utf8->relaxed->decode (<$fh>); - } + @RC_PATH = @{ $theme->{path} } if $theme->{path}; + + for (@RC_PATH, "") { + my $theme = load_json "$RC_BASE/$_/theme.json" + or next; - @RC_PATH = @{ $THEME->{path} } if $THEME->{path}; + %THEME = ( %$theme, %THEME ); + } } sub read_cfg { my ($file) = @_; - open my $fh, $file - or return; - - local $/; - my $CFG = <$fh>; - - $::CFG = decode_json $CFG; + $::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 JSON::XS->new->utf8->pretty->encode ($::CFG); + close $fh; + + rename $file, "$file.bak"; + rename "$file~", $file; } sub http_proxy {