ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.197 by root, Fri Dec 5 14:44:45 2008 UTC vs.
Revision 1.203 by root, Sun Jan 11 22:16:09 2009 UTC

17use Carp (); 17use Carp ();
18 18
19our $VERSION; 19our $VERSION;
20 20
21BEGIN { 21BEGIN {
22 $VERSION = '2.0'; 22 $VERSION = '2.02';
23 23
24 use XSLoader; 24 use XSLoader;
25 XSLoader::load "Deliantra::Client", $VERSION; 25 XSLoader::load "Deliantra::Client", $VERSION;
26} 26}
27 27
28use utf8; 28use utf8;
29use strict qw(vars subs); 29use strict qw(vars subs);
30 30
31use Socket ();
31use AnyEvent (); 32use AnyEvent ();
33use AnyEvent::Util ();
32use Pod::POM (); 34use Pod::POM ();
33use File::Path (); 35use File::Path ();
34use Storable (); # finally 36use Storable (); # finally
35use Fcntl (); 37use Fcntl ();
36use JSON::XS qw(encode_json decode_json); 38use JSON::XS qw(encode_json decode_json);
37 39use Guard qw(guard);
38=item guard { BLOCK }
39
40Returns an object that executes the given block as soon as it is destroyed.
41
42=cut
43
44sub guard(&) {
45 bless \(my $cb = $_[0]), "DC::Guard"
46}
47
48sub DC::Guard::DESTROY {
49 ${$_[0]}->()
50}
51 40
52=item shorten $string[, $maxlength] 41=item shorten $string[, $maxlength]
53 42
54=cut 43=cut
55 44
67 s/</&lt;/g; 56 s/</&lt;/g;
68 57
69 $_ 58 $_
70} 59}
71 60
72sub socketpipe() {
73 socketpair my $fh1, my $fh2, &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC
74 or die "cannot establish bidirectional pipe: $!\n";
75
76 ($fh1, $fh2)
77}
78
79sub background(&;&) { 61sub background(&;&) {
80 my ($bg, $cb) = @_; 62 my ($bg, $cb) = @_;
81 63
82 my ($fh_r, $fh_w) = DC::socketpipe; 64 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
65 or die "unable to create background socketpair: $!";
83 66
84 my $pid = fork; 67 my $pid = fork;
85 68
86 if (defined $pid && !$pid) { 69 if (defined $pid && !$pid) {
87 local $SIG{__DIE__}; 70 local $SIG{__DIE__};
171 154
172 open my $fh, $file 155 open my $fh, $file
173 or return; 156 or return;
174 157
175 local $/; 158 local $/;
176 JSON::XS->new->utf8->relaxed->decode (<$fh>) 159 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
177} 160}
178 161
179sub set_theme($) { 162sub set_theme($) {
180 return if $RC_THEME eq $_[0]; 163 return if $RC_THEME eq $_[0];
181 $RC_THEME = $_[0]; 164 $RC_THEME = $_[0];
199} 182}
200 183
201sub read_cfg { 184sub read_cfg {
202 my ($file) = @_; 185 my ($file) = @_;
203 186
204 $::CFG = load_json $file; 187 $::CFG = (load_json $file) || (load_json "$file.bak");
205} 188}
206 189
207sub write_cfg { 190sub write_cfg {
208 my $file = "$Deliantra::VARDIR/client.cf"; 191 my $file = "$Deliantra::VARDIR/client.cf";
209 192
210 $::CFG->{VERSION} = $::VERSION; 193 $::CFG->{VERSION} = $::VERSION;
194 $::CFG->{layout} = DC::UI::get_layout ();
211 195
212 open my $fh, ">:utf8", $file 196 open my $fh, ">:utf8", "$file~"
213 or return; 197 or return;
214 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); 198 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
199 close $fh;
200
201 rename $file, "$file.bak";
202 rename "$file~", $file;
215} 203}
216 204
217sub http_proxy { 205sub http_proxy {
218 my @proxy = win32_proxy_info; 206 my @proxy = win32_proxy_info;
219 207

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines