--- deliantra/Deliantra-Client/DC.pm 2007/12/26 21:03:21 1.169 +++ deliantra/Deliantra-Client/DC.pm 2008/09/29 03:41:58 1.195 @@ -16,14 +16,17 @@ use Carp (); +our $VERSION; + BEGIN { - $VERSION = '0.9962'; + $VERSION = '0.9977'; use XSLoader; XSLoader::load "Deliantra::Client", $VERSION; } use utf8; +use strict qw(vars subs); use AnyEvent (); use Pod::POM (); @@ -67,7 +70,7 @@ } sub socketpipe() { - socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC + socketpair my $fh1, my $fh2, &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC or die "cannot establish bidirectional pipe: $!\n"; ($fh1, $fh2) @@ -142,37 +145,73 @@ 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 = "$_/Deliantra/Client/private/resources/$_[0]"; + for (@RC_PATH, "") { + $path = "$RC_BASE/$_/$_[0]"; return $path if -r $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>; + 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"; + + my $theme = load_json find_rcfile "theme.json" + or die "FATAL: theme resource file not found"; + + @RC_PATH = @{ $theme->{path} } if $theme->{path}; - $::CFG = decode_json $CFG; + 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; +} + +sub write_cfg { + my $file = "$Deliantra::VARDIR/client.cf"; + $::CFG->{VERSION} = $::VERSION; open my $fh, ">:utf8", $file or return; - print $fh encode_json $::CFG; + print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); } sub http_proxy { @@ -200,7 +239,7 @@ DC::set_proxy; my $ua = LWP::UserAgent->new ( - agent => "cfplus $VERSION", + agent => "deliantra $VERSION", keep_alive => 1, env_proxy => 1, timeout => 30, @@ -225,7 +264,6 @@ } else { fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; } - } package DC::Layout;