… | |
… | |
10 | |
10 | |
11 | =over 4 |
11 | =over 4 |
12 | |
12 | |
13 | =cut |
13 | =cut |
14 | |
14 | |
15 | package Deliantra::Client; # work around CPAN breakage |
|
|
16 | package App::Deliantra; # try to reserve namespace |
|
|
17 | package DC; |
15 | package DC; |
18 | |
16 | |
19 | use Carp (); |
17 | use Carp (); |
20 | |
18 | |
|
|
19 | our $VERSION; |
|
|
20 | |
21 | BEGIN { |
21 | BEGIN { |
22 | $VERSION = '0.9974'; |
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 | |
28 | use utf8; |
28 | use utf8; |
|
|
29 | use strict qw(vars subs); |
29 | |
30 | |
|
|
31 | use Socket (); |
30 | use AnyEvent (); |
32 | use AnyEvent (); |
|
|
33 | use AnyEvent::Util (); |
31 | use Pod::POM (); |
34 | use Pod::POM (); |
32 | use File::Path (); |
35 | use File::Path (); |
33 | use Storable (); # finally |
36 | use Storable (); # finally |
34 | use Fcntl (); |
37 | use Fcntl (); |
35 | use JSON::XS qw(encode_json decode_json); |
38 | use JSON::XS qw(encode_json decode_json); |
… | |
… | |
66 | s/</</g; |
69 | s/</</g; |
67 | |
70 | |
68 | $_ |
71 | $_ |
69 | } |
72 | } |
70 | |
73 | |
71 | sub socketpipe() { |
|
|
72 | socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
|
|
73 | or die "cannot establish bidirectional pipe: $!\n"; |
|
|
74 | |
|
|
75 | ($fh1, $fh2) |
|
|
76 | } |
|
|
77 | |
|
|
78 | sub background(&;&) { |
74 | sub background(&;&) { |
79 | my ($bg, $cb) = @_; |
75 | my ($bg, $cb) = @_; |
80 | |
76 | |
81 | my ($fh_r, $fh_w) = DC::socketpipe; |
77 | my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair |
|
|
78 | or die "unable to create background socketpair: $!"; |
82 | |
79 | |
83 | my $pid = fork; |
80 | my $pid = fork; |
84 | |
81 | |
85 | if (defined $pid && !$pid) { |
82 | if (defined $pid && !$pid) { |
86 | local $SIG{__DIE__}; |
83 | local $SIG{__DIE__}; |
… | |
… | |
142 | print $msg, "\n"; |
139 | print $msg, "\n"; |
143 | } |
140 | } |
144 | |
141 | |
145 | package DC; |
142 | package DC; |
146 | |
143 | |
|
|
144 | our $RC_THEME; |
|
|
145 | our %THEME; |
|
|
146 | our @RC_PATH; |
|
|
147 | our $RC_BASE; |
|
|
148 | |
|
|
149 | for (grep !ref, @INC) { |
|
|
150 | $RC_BASE = "$_/Deliantra/Client/private/resources"; |
|
|
151 | last if -d $RC_BASE; |
|
|
152 | } |
|
|
153 | |
147 | sub find_rcfile($) { |
154 | sub find_rcfile($) { |
148 | my $path; |
155 | my $path; |
149 | |
156 | |
150 | for (grep !ref, @INC) { |
157 | for (@RC_PATH, "") { |
151 | $path = "$_/Deliantra/Client/private/resources/$_[0]"; |
158 | $path = "$RC_BASE/$_/$_[0]"; |
152 | return $path if -r $path; |
159 | return $path if -r $path; |
153 | } |
160 | } |
154 | |
161 | |
155 | die "FATAL: can't find required file $_[0]\n"; |
162 | die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; |
|
|
163 | } |
|
|
164 | |
|
|
165 | sub load_json($) { |
|
|
166 | my ($file) = @_; |
|
|
167 | |
|
|
168 | open my $fh, $file |
|
|
169 | or return; |
|
|
170 | |
|
|
171 | local $/; |
|
|
172 | JSON::XS->new->utf8->relaxed->decode (<$fh>) |
|
|
173 | } |
|
|
174 | |
|
|
175 | sub set_theme($) { |
|
|
176 | return if $RC_THEME eq $_[0]; |
|
|
177 | $RC_THEME = $_[0]; |
|
|
178 | |
|
|
179 | # kind of hacky, find the main theme file, then load all theme files and merge them |
|
|
180 | |
|
|
181 | %THEME = (); |
|
|
182 | @RC_PATH = "theme-$RC_THEME"; |
|
|
183 | |
|
|
184 | my $theme = load_json find_rcfile "theme.json" |
|
|
185 | or die "FATAL: theme resource file not found"; |
|
|
186 | |
|
|
187 | @RC_PATH = @{ $theme->{path} } if $theme->{path}; |
|
|
188 | |
|
|
189 | for (@RC_PATH, "") { |
|
|
190 | my $theme = load_json "$RC_BASE/$_/theme.json" |
|
|
191 | or next; |
|
|
192 | |
|
|
193 | %THEME = ( %$theme, %THEME ); |
|
|
194 | } |
156 | } |
195 | } |
157 | |
196 | |
158 | sub read_cfg { |
197 | sub read_cfg { |
159 | my ($file) = @_; |
198 | my ($file) = @_; |
160 | |
199 | |
161 | open my $fh, $file |
200 | $::CFG = (load_json $file) || (load_json "$file.bak"); |
162 | or return; |
|
|
163 | |
|
|
164 | local $/; |
|
|
165 | my $CFG = <$fh>; |
|
|
166 | |
|
|
167 | $::CFG = decode_json $CFG; |
|
|
168 | } |
201 | } |
169 | |
202 | |
170 | sub write_cfg { |
203 | sub write_cfg { |
171 | my $file = "$Deliantra::VARDIR/client.cf"; |
204 | my $file = "$Deliantra::VARDIR/client.cf"; |
172 | |
205 | |
173 | $::CFG->{VERSION} = $::VERSION; |
206 | $::CFG->{VERSION} = $::VERSION; |
|
|
207 | $::CFG->{layout} = DC::UI::get_layout (); |
174 | |
208 | |
175 | open my $fh, ">:utf8", $file |
209 | open my $fh, ">:utf8", "$file~" |
176 | or return; |
210 | or return; |
177 | print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
211 | print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
|
|
212 | close $fh; |
|
|
213 | |
|
|
214 | rename $file, "$file.bak"; |
|
|
215 | rename "$file~", $file; |
178 | } |
216 | } |
179 | |
217 | |
180 | sub http_proxy { |
218 | sub http_proxy { |
181 | my @proxy = win32_proxy_info; |
219 | my @proxy = win32_proxy_info; |
182 | |
220 | |