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