… | |
… | |
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 | |
21 | our $VERSION; |
19 | our $VERSION; |
22 | |
20 | |
23 | BEGIN { |
21 | BEGIN { |
24 | $VERSION = '0.9975'; |
22 | $VERSION = '3.0'; |
25 | |
23 | |
26 | use XSLoader; |
24 | use XSLoader; |
27 | XSLoader::load "Deliantra::Client", $VERSION; |
25 | XSLoader::load "Deliantra::Client", $VERSION; |
28 | } |
26 | } |
29 | |
27 | |
30 | use utf8; |
28 | use utf8; |
31 | use strict qw(vars subs); |
29 | use strict qw(vars subs); |
32 | |
30 | |
|
|
31 | use Socket (); |
33 | use AnyEvent (); |
32 | use AnyEvent (); |
|
|
33 | use AnyEvent::Util (); |
34 | use Pod::POM (); |
34 | use Pod::POM (); |
35 | use File::Path (); |
35 | use File::Path (); |
36 | use Storable (); # finally |
36 | use Storable (); # finally |
37 | use Fcntl (); |
37 | use Fcntl (); |
38 | use JSON::XS qw(encode_json decode_json); |
38 | use JSON::XS qw(encode_json decode_json); |
|
|
39 | use Guard qw(guard); |
39 | |
40 | |
40 | =item guard { BLOCK } |
41 | # modules to support other DC::* packages |
41 | |
42 | use List::Util (); |
42 | Returns an object that executes the given block as soon as it is destroyed. |
43 | use IO::AIO (); |
43 | |
44 | use Coro::AIO (); |
44 | =cut |
45 | use AnyEvent::AIO (); |
45 | |
|
|
46 | sub guard(&) { |
|
|
47 | bless \(my $cb = $_[0]), "DC::Guard" |
|
|
48 | } |
|
|
49 | |
|
|
50 | sub DC::Guard::DESTROY { |
|
|
51 | ${$_[0]}->() |
|
|
52 | } |
|
|
53 | |
46 | |
54 | =item shorten $string[, $maxlength] |
47 | =item shorten $string[, $maxlength] |
55 | |
48 | |
56 | =cut |
49 | =cut |
57 | |
50 | |
… | |
… | |
69 | s/</</g; |
62 | s/</</g; |
70 | |
63 | |
71 | $_ |
64 | $_ |
72 | } |
65 | } |
73 | |
66 | |
74 | sub 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 | |
|
|
81 | sub background(&;&) { |
67 | sub background(&;&) { |
82 | my ($bg, $cb) = @_; |
68 | my ($bg, $cb) = @_; |
83 | |
69 | |
84 | 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: $!"; |
85 | |
72 | |
86 | my $pid = fork; |
73 | my $pid = fork; |
87 | |
74 | |
88 | if (defined $pid && !$pid) { |
75 | if (defined $pid && !$pid) { |
89 | local $SIG{__DIE__}; |
76 | local $SIG{__DIE__}; |
… | |
… | |
145 | print $msg, "\n"; |
132 | print $msg, "\n"; |
146 | } |
133 | } |
147 | |
134 | |
148 | package DC; |
135 | package DC; |
149 | |
136 | |
150 | our $RC_THEME = "."; |
137 | our $RC_THEME; |
|
|
138 | our %THEME; |
|
|
139 | our @RC_PATH; |
151 | our $RC_BASE; |
140 | our $RC_BASE; |
152 | |
141 | |
153 | for (grep !ref, @INC) { |
142 | for (grep !ref, @INC) { |
154 | $RC_BASE = "$_/Deliantra/Client/private/resources"; |
143 | $RC_BASE = "$_/Deliantra/Client/private/resources"; |
155 | last if -d $RC_BASE; |
144 | last if -d $RC_BASE; |
156 | } |
145 | } |
157 | |
146 | |
158 | sub find_rcfile($) { |
147 | sub find_rcfile($) { |
159 | my $path; |
148 | my $path; |
160 | |
149 | |
|
|
150 | for (@RC_PATH, "") { |
161 | $path = "$RC_BASE/$RC_THEME/$_[0]"; |
151 | $path = "$RC_BASE/$_/$_[0]"; |
162 | return $path if -r $path; |
152 | return $path if -e $path; |
163 | |
153 | } |
164 | $path = "$RC_BASE/$_[0]"; |
|
|
165 | return $path if -r $path; |
|
|
166 | |
154 | |
167 | 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"; |
|
|
156 | } |
|
|
157 | |
|
|
158 | sub load_json($) { |
|
|
159 | my ($file) = @_; |
|
|
160 | |
|
|
161 | open my $fh, $file |
|
|
162 | or return; |
|
|
163 | |
|
|
164 | local $/; |
|
|
165 | eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) } |
|
|
166 | } |
|
|
167 | |
|
|
168 | sub set_theme($) { |
|
|
169 | return if $RC_THEME eq $_[0]; |
|
|
170 | $RC_THEME = $_[0]; |
|
|
171 | |
|
|
172 | # kind of hacky, find the main theme file, then load all theme files and merge them |
|
|
173 | |
|
|
174 | %THEME = (); |
|
|
175 | @RC_PATH = "theme-$RC_THEME"; |
|
|
176 | |
|
|
177 | my $theme = load_json find_rcfile "theme.json" |
|
|
178 | or die "FATAL: theme resource file not found"; |
|
|
179 | |
|
|
180 | @RC_PATH = @{ $theme->{path} } if $theme->{path}; |
|
|
181 | |
|
|
182 | for (@RC_PATH, "") { |
|
|
183 | my $theme = load_json "$RC_BASE/$_/theme.json" |
|
|
184 | or next; |
|
|
185 | |
|
|
186 | %THEME = ( %$theme, %THEME ); |
|
|
187 | } |
168 | } |
188 | } |
169 | |
189 | |
170 | sub read_cfg { |
190 | sub read_cfg { |
171 | my ($file) = @_; |
191 | my ($file) = @_; |
172 | |
192 | |
173 | open my $fh, $file |
193 | $::CFG = (load_json $file) || (load_json "$file.bak"); |
174 | or return; |
|
|
175 | |
|
|
176 | local $/; |
|
|
177 | my $CFG = <$fh>; |
|
|
178 | |
|
|
179 | $::CFG = decode_json $CFG; |
|
|
180 | } |
194 | } |
181 | |
195 | |
182 | sub write_cfg { |
196 | sub write_cfg { |
183 | my $file = "$Deliantra::VARDIR/client.cf"; |
197 | my $file = "$Deliantra::VARDIR/client.cf"; |
184 | |
198 | |
185 | $::CFG->{VERSION} = $::VERSION; |
199 | $::CFG->{VERSION} = $::VERSION; |
|
|
200 | $::CFG->{layout} = DC::UI::get_layout (); |
186 | |
201 | |
187 | open my $fh, ">:utf8", $file |
202 | open my $fh, ">:utf8", "$file~" |
188 | or return; |
203 | or return; |
189 | 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; |
190 | } |
209 | } |
191 | |
210 | |
192 | sub http_proxy { |
211 | sub http_proxy { |
193 | my @proxy = win32_proxy_info; |
212 | my @proxy = win32_proxy_info; |
194 | |
213 | |