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.191 by root, Wed Sep 3 06:07:39 2008 UTC vs.
Revision 1.210 by root, Fri Jan 15 15:19:47 2010 UTC

10 10
11=over 4 11=over 4
12 12
13=cut 13=cut
14 14
15package Deliantra::Client; # work around CPAN breakage
16package App::Deliantra; # try to reserve namespace
17package DC; 15package DC;
18 16
19use Carp (); 17use Carp ();
20 18
21our $VERSION; 19our $VERSION;
22 20
23BEGIN { 21BEGIN {
24 $VERSION = '0.9975'; 22 $VERSION = '2.10';
25 23
26 use XSLoader; 24 use XSLoader;
27 XSLoader::load "Deliantra::Client", $VERSION; 25 XSLoader::load "Deliantra::Client", $VERSION;
28} 26}
29 27
30use utf8; 28use utf8;
31use strict qw(vars subs); 29use strict qw(vars subs);
32 30
31use Socket ();
33use AnyEvent (); 32use AnyEvent ();
33use AnyEvent::Util ();
34use Pod::POM (); 34use Pod::POM ();
35use File::Path (); 35use File::Path ();
36use Storable (); # finally 36use Storable (); # finally
37use Fcntl (); 37use Fcntl ();
38use JSON::XS qw(encode_json decode_json); 38use JSON::XS qw(encode_json decode_json);
39 39use Guard qw(guard);
40=item guard { BLOCK }
41
42Returns an object that executes the given block as soon as it is destroyed.
43
44=cut
45
46sub guard(&) {
47 bless \(my $cb = $_[0]), "DC::Guard"
48}
49
50sub DC::Guard::DESTROY {
51 ${$_[0]}->()
52}
53 40
54=item shorten $string[, $maxlength] 41=item shorten $string[, $maxlength]
55 42
56=cut 43=cut
57 44
69 s/</&lt;/g; 56 s/</&lt;/g;
70 57
71 $_ 58 $_
72} 59}
73 60
74sub socketpipe() {
75 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
76 or die "cannot establish bidirectional pipe: $!\n";
77
78 ($fh1, $fh2)
79}
80
81sub background(&;&) { 61sub background(&;&) {
82 my ($bg, $cb) = @_; 62 my ($bg, $cb) = @_;
83 63
84 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: $!";
85 66
86 my $pid = fork; 67 my $pid = fork;
87 68
88 if (defined $pid && !$pid) { 69 if (defined $pid && !$pid) {
89 local $SIG{__DIE__}; 70 local $SIG{__DIE__};
146} 127}
147 128
148package DC; 129package DC;
149 130
150our $RC_THEME; 131our $RC_THEME;
151our $THEME; 132our %THEME;
152our @RC_PATH; 133our @RC_PATH;
153our $RC_BASE; 134our $RC_BASE;
154 135
155for (grep !ref, @INC) { 136for (grep !ref, @INC) {
156 $RC_BASE = "$_/Deliantra/Client/private/resources"; 137 $RC_BASE = "$_/Deliantra/Client/private/resources";
160sub find_rcfile($) { 141sub find_rcfile($) {
161 my $path; 142 my $path;
162 143
163 for (@RC_PATH, "") { 144 for (@RC_PATH, "") {
164 $path = "$RC_BASE/$_/$_[0]"; 145 $path = "$RC_BASE/$_/$_[0]";
165 return $path if -r $path; 146 return $path if -e $path;
166 } 147 }
167 148
168 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; 149 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
150}
151
152sub load_json($) {
153 my ($file) = @_;
154
155 open my $fh, $file
156 or return;
157
158 local $/;
159 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
169} 160}
170 161
171sub set_theme($) { 162sub set_theme($) {
172 return if $RC_THEME eq $_[0]; 163 return if $RC_THEME eq $_[0];
173 $RC_THEME = $_[0]; 164 $RC_THEME = $_[0];
174 165
166 # kind of hacky, find the main theme file, then load all theme files and merge them
167
168 %THEME = ();
175 @RC_PATH = "theme-$RC_THEME"; 169 @RC_PATH = "theme-$RC_THEME";
176 170
177 { 171 my $theme = load_json find_rcfile "theme.json"
178 open my $fh, "<:raw", find_rcfile "theme.json" 172 or die "FATAL: theme resource file not found";
179 or die "cannot open theme description file";
180 173
181 local $/;
182 $THEME = JSON::XS->new->utf8->relaxed->decode (<$fh>);
183 }
184
185 @RC_PATH = @{ $THEME->{path} } if $THEME->{path}; 174 @RC_PATH = @{ $theme->{path} } if $theme->{path};
175
176 for (@RC_PATH, "") {
177 my $theme = load_json "$RC_BASE/$_/theme.json"
178 or next;
179
180 %THEME = ( %$theme, %THEME );
181 }
186} 182}
187 183
188sub read_cfg { 184sub read_cfg {
189 my ($file) = @_; 185 my ($file) = @_;
190 186
191 open my $fh, $file 187 $::CFG = (load_json $file) || (load_json "$file.bak");
192 or return;
193
194 local $/;
195 my $CFG = <$fh>;
196
197 $::CFG = decode_json $CFG;
198} 188}
199 189
200sub write_cfg { 190sub write_cfg {
201 my $file = "$Deliantra::VARDIR/client.cf"; 191 my $file = "$Deliantra::VARDIR/client.cf";
202 192
203 $::CFG->{VERSION} = $::VERSION; 193 $::CFG->{VERSION} = $::VERSION;
194 $::CFG->{layout} = DC::UI::get_layout ();
204 195
205 open my $fh, ">:utf8", $file 196 open my $fh, ">:utf8", "$file~"
206 or return; 197 or return;
207 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;
208} 203}
209 204
210sub http_proxy { 205sub http_proxy {
211 my @proxy = win32_proxy_info; 206 my @proxy = win32_proxy_info;
212 207

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines