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