1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
root |
1.169 |
DC - undocumented utility garbage for our deliantra client |
4 |
root |
1.1 |
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
root |
1.169 |
use DC; |
8 |
root |
1.1 |
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
=over 4 |
12 |
|
|
|
13 |
|
|
=cut |
14 |
|
|
|
15 |
root |
1.169 |
package DC; |
16 |
root |
1.1 |
|
17 |
root |
1.121 |
use Carp (); |
18 |
|
|
|
19 |
root |
1.187 |
our $VERSION; |
20 |
|
|
|
21 |
root |
1.1 |
BEGIN { |
22 |
root |
1.213 |
$VERSION = '3.0'; |
23 |
root |
1.1 |
|
24 |
root |
1.2 |
use XSLoader; |
25 |
root |
1.168 |
XSLoader::load "Deliantra::Client", $VERSION; |
26 |
root |
1.1 |
} |
27 |
|
|
|
28 |
root |
1.62 |
use utf8; |
29 |
root |
1.187 |
use strict qw(vars subs); |
30 |
root |
1.62 |
|
31 |
root |
1.200 |
use Socket (); |
32 |
root |
1.52 |
use AnyEvent (); |
33 |
root |
1.200 |
use AnyEvent::Util (); |
34 |
root |
1.89 |
use Pod::POM (); |
35 |
root |
1.135 |
use File::Path (); |
36 |
root |
1.89 |
use Storable (); # finally |
37 |
root |
1.141 |
use Fcntl (); |
38 |
root |
1.159 |
use JSON::XS qw(encode_json decode_json); |
39 |
root |
1.202 |
use Guard qw(guard); |
40 |
root |
1.103 |
|
41 |
root |
1.212 |
# modules to support other DC::* packages |
42 |
|
|
use List::Util (); |
43 |
|
|
use IO::AIO (); |
44 |
|
|
use Coro::AIO (); |
45 |
|
|
use AnyEvent::AIO (); |
46 |
|
|
|
47 |
root |
1.133 |
=item shorten $string[, $maxlength] |
48 |
|
|
|
49 |
|
|
=cut |
50 |
|
|
|
51 |
|
|
sub shorten($;$) { |
52 |
|
|
my ($str, $len) = @_; |
53 |
|
|
substr $str, $len, (length $str), "..." if $len + 3 <= length $str; |
54 |
|
|
$str |
55 |
|
|
} |
56 |
|
|
|
57 |
root |
1.105 |
sub asxml($) { |
58 |
|
|
local $_ = $_[0]; |
59 |
root |
1.89 |
|
60 |
root |
1.105 |
s/&/&/g; |
61 |
|
|
s/>/>/g; |
62 |
|
|
s/</</g; |
63 |
root |
1.89 |
|
64 |
root |
1.105 |
$_ |
65 |
root |
1.89 |
} |
66 |
|
|
|
67 |
root |
1.127 |
sub background(&;&) { |
68 |
|
|
my ($bg, $cb) = @_; |
69 |
root |
1.123 |
|
70 |
root |
1.200 |
my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair |
71 |
|
|
or die "unable to create background socketpair: $!"; |
72 |
root |
1.123 |
|
73 |
|
|
my $pid = fork; |
74 |
|
|
|
75 |
|
|
if (defined $pid && !$pid) { |
76 |
root |
1.124 |
local $SIG{__DIE__}; |
77 |
root |
1.123 |
|
78 |
|
|
open STDOUT, ">&", $fh_w; |
79 |
|
|
open STDERR, ">&", $fh_w; |
80 |
|
|
close $fh_r; |
81 |
|
|
close $fh_w; |
82 |
|
|
|
83 |
|
|
$| = 1; |
84 |
|
|
|
85 |
root |
1.127 |
eval { $bg->() }; |
86 |
root |
1.124 |
|
87 |
|
|
if ($@) { |
88 |
|
|
my $msg = $@; |
89 |
|
|
$msg =~ s/\n+/\n/; |
90 |
|
|
warn "FATAL: $msg"; |
91 |
root |
1.169 |
DC::_exit 1; |
92 |
root |
1.124 |
} |
93 |
root |
1.123 |
|
94 |
|
|
# win32 is fucked up, of course. exit will clean stuff up, |
95 |
|
|
# which destroys our database etc. _exit will exit ALL |
96 |
|
|
# forked processes, because of the dreaded fork emulation. |
97 |
root |
1.169 |
DC::_exit 0; |
98 |
root |
1.123 |
} |
99 |
|
|
|
100 |
|
|
close $fh_w; |
101 |
|
|
|
102 |
|
|
my $buffer; |
103 |
|
|
|
104 |
root |
1.126 |
my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub { |
105 |
root |
1.123 |
unless (sysread $fh_r, $buffer, 4096, length $buffer) { |
106 |
root |
1.126 |
undef $w; |
107 |
root |
1.127 |
$cb->(); |
108 |
|
|
return; |
109 |
root |
1.123 |
} |
110 |
|
|
|
111 |
|
|
while ($buffer =~ s/^(.*)\n//) { |
112 |
|
|
my $line = $1; |
113 |
root |
1.124 |
$line =~ s/\s+$//; |
114 |
root |
1.123 |
utf8::decode $line; |
115 |
root |
1.127 |
if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
116 |
root |
1.139 |
$cb->(JSON::XS->new->allow_nonref->decode ($1)); |
117 |
root |
1.127 |
} else { |
118 |
|
|
::message ({ |
119 |
root |
1.169 |
markup => "background($pid): " . DC::asxml $line, |
120 |
root |
1.127 |
}); |
121 |
|
|
} |
122 |
root |
1.123 |
} |
123 |
|
|
}); |
124 |
|
|
} |
125 |
|
|
|
126 |
root |
1.127 |
sub background_msg { |
127 |
|
|
my ($msg) = @_; |
128 |
|
|
|
129 |
root |
1.139 |
$msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg); |
130 |
root |
1.127 |
$msg =~ s/\n//g; |
131 |
|
|
utf8::encode $msg; |
132 |
|
|
print $msg, "\n"; |
133 |
|
|
} |
134 |
|
|
|
135 |
root |
1.169 |
package DC; |
136 |
root |
1.52 |
|
137 |
root |
1.191 |
our $RC_THEME; |
138 |
root |
1.192 |
our %THEME; |
139 |
root |
1.191 |
our @RC_PATH; |
140 |
root |
1.187 |
our $RC_BASE; |
141 |
|
|
|
142 |
|
|
for (grep !ref, @INC) { |
143 |
|
|
$RC_BASE = "$_/Deliantra/Client/private/resources"; |
144 |
|
|
last if -d $RC_BASE; |
145 |
|
|
} |
146 |
|
|
|
147 |
root |
1.5 |
sub find_rcfile($) { |
148 |
|
|
my $path; |
149 |
|
|
|
150 |
root |
1.191 |
for (@RC_PATH, "") { |
151 |
root |
1.189 |
$path = "$RC_BASE/$_/$_[0]"; |
152 |
root |
1.205 |
return $path if -e $path; |
153 |
root |
1.189 |
} |
154 |
root |
1.5 |
|
155 |
root |
1.187 |
die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; |
156 |
root |
1.5 |
} |
157 |
|
|
|
158 |
root |
1.192 |
sub load_json($) { |
159 |
|
|
my ($file) = @_; |
160 |
|
|
|
161 |
|
|
open my $fh, $file |
162 |
|
|
or return; |
163 |
|
|
|
164 |
|
|
local $/; |
165 |
root |
1.203 |
eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) } |
166 |
root |
1.192 |
} |
167 |
|
|
|
168 |
root |
1.191 |
sub set_theme($) { |
169 |
|
|
return if $RC_THEME eq $_[0]; |
170 |
|
|
$RC_THEME = $_[0]; |
171 |
|
|
|
172 |
root |
1.192 |
# kind of hacky, find the main theme file, then load all theme files and merge them |
173 |
|
|
|
174 |
|
|
%THEME = (); |
175 |
root |
1.191 |
@RC_PATH = "theme-$RC_THEME"; |
176 |
|
|
|
177 |
root |
1.192 |
my $theme = load_json find_rcfile "theme.json" |
178 |
|
|
or die "FATAL: theme resource file not found"; |
179 |
root |
1.191 |
|
180 |
root |
1.192 |
@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 |
root |
1.191 |
} |
188 |
|
|
} |
189 |
|
|
|
190 |
root |
1.5 |
sub read_cfg { |
191 |
|
|
my ($file) = @_; |
192 |
|
|
|
193 |
root |
1.201 |
$::CFG = (load_json $file) || (load_json "$file.bak"); |
194 |
root |
1.5 |
} |
195 |
|
|
|
196 |
|
|
sub write_cfg { |
197 |
root |
1.178 |
my $file = "$Deliantra::VARDIR/client.cf"; |
198 |
root |
1.5 |
|
199 |
root |
1.107 |
$::CFG->{VERSION} = $::VERSION; |
200 |
root |
1.201 |
$::CFG->{layout} = DC::UI::get_layout (); |
201 |
root |
1.107 |
|
202 |
root |
1.201 |
open my $fh, ">:utf8", "$file~" |
203 |
root |
1.5 |
or return; |
204 |
root |
1.180 |
print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
205 |
root |
1.201 |
close $fh; |
206 |
|
|
|
207 |
|
|
rename $file, "$file.bak"; |
208 |
|
|
rename "$file~", $file; |
209 |
root |
1.5 |
} |
210 |
|
|
|
211 |
root |
1.122 |
sub http_proxy { |
212 |
|
|
my @proxy = win32_proxy_info; |
213 |
|
|
|
214 |
|
|
if (@proxy) { |
215 |
|
|
"http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0] |
216 |
|
|
} elsif (exists $ENV{http_proxy}) { |
217 |
|
|
$ENV{http_proxy} |
218 |
|
|
} else { |
219 |
|
|
() |
220 |
|
|
} |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
sub set_proxy { |
224 |
|
|
my $proxy = http_proxy |
225 |
|
|
or return; |
226 |
|
|
|
227 |
|
|
$ENV{http_proxy} = $proxy; |
228 |
|
|
} |
229 |
|
|
|
230 |
root |
1.127 |
sub lwp_useragent { |
231 |
|
|
require LWP::UserAgent; |
232 |
|
|
|
233 |
root |
1.169 |
DC::set_proxy; |
234 |
root |
1.127 |
|
235 |
|
|
my $ua = LWP::UserAgent->new ( |
236 |
root |
1.171 |
agent => "deliantra $VERSION", |
237 |
root |
1.127 |
keep_alive => 1, |
238 |
|
|
env_proxy => 1, |
239 |
|
|
timeout => 30, |
240 |
|
|
); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
sub lwp_check($) { |
244 |
|
|
my ($res) = @_; |
245 |
|
|
|
246 |
|
|
$res->is_error |
247 |
|
|
and die $res->status_line; |
248 |
|
|
|
249 |
|
|
$res |
250 |
|
|
} |
251 |
|
|
|
252 |
root |
1.141 |
sub fh_nonblocking($$) { |
253 |
|
|
my ($fh, $nb) = @_; |
254 |
|
|
|
255 |
root |
1.142 |
if ($^O eq "MSWin32") { |
256 |
|
|
$nb = (! ! $nb) + 0; |
257 |
root |
1.143 |
ioctl $fh, 0x8004667e, \$nb; # FIONBIO |
258 |
root |
1.141 |
} else { |
259 |
|
|
fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
260 |
|
|
} |
261 |
|
|
} |
262 |
|
|
|
263 |
root |
1.169 |
package DC::Layout; |
264 |
root |
1.97 |
|
265 |
root |
1.169 |
$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub { |
266 |
root |
1.164 |
glyph_cache_restore; |
267 |
|
|
}; |
268 |
|
|
|
269 |
root |
1.169 |
$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub { |
270 |
root |
1.164 |
glyph_cache_backup; |
271 |
root |
1.97 |
}; |
272 |
|
|
|
273 |
root |
1.1 |
1; |
274 |
|
|
|
275 |
|
|
=back |
276 |
|
|
|
277 |
|
|
=head1 AUTHOR |
278 |
|
|
|
279 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
280 |
|
|
http://home.schmorp.de/ |
281 |
|
|
|
282 |
|
|
=cut |
283 |
|
|
|