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.198 by root, Fri Dec 12 15:06:00 2008 UTC vs.
Revision 1.213 by root, Sat Jan 7 15:24:41 2012 UTC

17use Carp (); 17use Carp ();
18 18
19our $VERSION; 19our $VERSION;
20 20
21BEGIN { 21BEGIN {
22 $VERSION = '2.01'; 22 $VERSION = '3.0';
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);
39use Guard qw(guard);
37 40
38=item guard { BLOCK } 41# modules to support other DC::* packages
39 42use List::Util ();
40Returns an object that executes the given block as soon as it is destroyed. 43use IO::AIO ();
41 44use Coro::AIO ();
42=cut 45use AnyEvent::AIO ();
43
44sub guard(&) {
45 bless \(my $cb = $_[0]), "DC::Guard"
46}
47
48sub DC::Guard::DESTROY {
49 ${$_[0]}->()
50}
51 46
52=item shorten $string[, $maxlength] 47=item shorten $string[, $maxlength]
53 48
54=cut 49=cut
55 50
67 s/</&lt;/g; 62 s/</&lt;/g;
68 63
69 $_ 64 $_
70} 65}
71 66
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(&;&) { 67sub background(&;&) {
80 my ($bg, $cb) = @_; 68 my ($bg, $cb) = @_;
81 69
82 my ($fh_r, $fh_w) = DC::socketpipe; 70 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
71 or die "unable to create background socketpair: $!";
83 72
84 my $pid = fork; 73 my $pid = fork;
85 74
86 if (defined $pid && !$pid) { 75 if (defined $pid && !$pid) {
87 local $SIG{__DIE__}; 76 local $SIG{__DIE__};
158sub find_rcfile($) { 147sub find_rcfile($) {
159 my $path; 148 my $path;
160 149
161 for (@RC_PATH, "") { 150 for (@RC_PATH, "") {
162 $path = "$RC_BASE/$_/$_[0]"; 151 $path = "$RC_BASE/$_/$_[0]";
163 return $path if -r $path; 152 return $path if -e $path;
164 } 153 }
165 154
166 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; 155 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
167} 156}
168 157
171 160
172 open my $fh, $file 161 open my $fh, $file
173 or return; 162 or return;
174 163
175 local $/; 164 local $/;
176 JSON::XS->new->utf8->relaxed->decode (<$fh>) 165 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
177} 166}
178 167
179sub set_theme($) { 168sub set_theme($) {
180 return if $RC_THEME eq $_[0]; 169 return if $RC_THEME eq $_[0];
181 $RC_THEME = $_[0]; 170 $RC_THEME = $_[0];
199} 188}
200 189
201sub read_cfg { 190sub read_cfg {
202 my ($file) = @_; 191 my ($file) = @_;
203 192
204 $::CFG = load_json $file; 193 $::CFG = (load_json $file) || (load_json "$file.bak");
205} 194}
206 195
207sub write_cfg { 196sub write_cfg {
208 my $file = "$Deliantra::VARDIR/client.cf"; 197 my $file = "$Deliantra::VARDIR/client.cf";
209 198
210 $::CFG->{VERSION} = $::VERSION; 199 $::CFG->{VERSION} = $::VERSION;
200 $::CFG->{layout} = DC::UI::get_layout ();
211 201
212 open my $fh, ">:utf8", $file 202 open my $fh, ">:utf8", "$file~"
213 or return; 203 or return;
214 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); 204 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
205 close $fh;
206
207 rename $file, "$file.bak";
208 rename "$file~", $file;
215} 209}
216 210
217sub http_proxy { 211sub http_proxy {
218 my @proxy = win32_proxy_info; 212 my @proxy = win32_proxy_info;
219 213

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines