--- deliantra/Deliantra-Client/DC.pm 2009/01/04 10:22:19 1.200 +++ deliantra/Deliantra-Client/DC.pm 2012/11/17 10:40:08 1.216 @@ -19,7 +19,7 @@ our $VERSION; BEGIN { - $VERSION = '2.02'; + $VERSION = '3.0'; use XSLoader; XSLoader::load "Deliantra::Client", $VERSION; @@ -36,20 +36,16 @@ use Storable (); # finally use Fcntl (); use JSON::XS qw(encode_json decode_json); +use Guard qw(guard); -=item guard { BLOCK } +# modules to support other DC::* packages +use List::Util (); +use IO::AIO (); +use Coro::AIO (); +use AnyEvent::AIO (); -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 Deliantra::Util (); +use Deliantra::Protocol::Constants (); =item shorten $string[, $maxlength] @@ -156,7 +152,7 @@ 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"; @@ -169,7 +165,7 @@ or return; local $/; - JSON::XS->new->utf8->relaxed->decode (<$fh>) + eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) } } sub set_theme($) { @@ -194,20 +190,94 @@ } } -sub read_cfg { +sub read_cfg($) { my ($file) = @_; - $::CFG = load_json $file; + $::CFG = (load_json $file) || (load_json "$file.bak"); } -sub write_cfg { +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 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 {