1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | dc - undocumented utility garbage for our deliantra client |
3 | DC - undocumented utility garbage for our deliantra client |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use dc; |
7 | use DC; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
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 |
15 | package dc; |
17 | package DC; |
16 | |
18 | |
17 | use Carp (); |
19 | use Carp (); |
18 | |
20 | |
19 | BEGIN { |
21 | BEGIN { |
20 | $VERSION = '0.9962'; |
22 | $VERSION = '0.9974'; |
21 | |
23 | |
22 | use XSLoader; |
24 | use XSLoader; |
23 | XSLoader::load "Deliantra::Client", $VERSION; |
25 | XSLoader::load "Deliantra::Client", $VERSION; |
24 | } |
26 | } |
25 | |
27 | |
… | |
… | |
37 | Returns an object that executes the given block as soon as it is destroyed. |
39 | Returns an object that executes the given block as soon as it is destroyed. |
38 | |
40 | |
39 | =cut |
41 | =cut |
40 | |
42 | |
41 | sub guard(&) { |
43 | sub guard(&) { |
42 | bless \(my $cb = $_[0]), "dc::Guard" |
44 | bless \(my $cb = $_[0]), "DC::Guard" |
43 | } |
45 | } |
44 | |
46 | |
45 | sub dc::Guard::DESTROY { |
47 | sub DC::Guard::DESTROY { |
46 | ${$_[0]}->() |
48 | ${$_[0]}->() |
47 | } |
49 | } |
48 | |
50 | |
49 | =item shorten $string[, $maxlength] |
51 | =item shorten $string[, $maxlength] |
50 | |
52 | |
… | |
… | |
74 | } |
76 | } |
75 | |
77 | |
76 | sub background(&;&) { |
78 | sub background(&;&) { |
77 | my ($bg, $cb) = @_; |
79 | my ($bg, $cb) = @_; |
78 | |
80 | |
79 | my ($fh_r, $fh_w) = dc::socketpipe; |
81 | my ($fh_r, $fh_w) = DC::socketpipe; |
80 | |
82 | |
81 | my $pid = fork; |
83 | my $pid = fork; |
82 | |
84 | |
83 | if (defined $pid && !$pid) { |
85 | if (defined $pid && !$pid) { |
84 | local $SIG{__DIE__}; |
86 | local $SIG{__DIE__}; |
… | |
… | |
94 | |
96 | |
95 | if ($@) { |
97 | if ($@) { |
96 | my $msg = $@; |
98 | my $msg = $@; |
97 | $msg =~ s/\n+/\n/; |
99 | $msg =~ s/\n+/\n/; |
98 | warn "FATAL: $msg"; |
100 | warn "FATAL: $msg"; |
99 | dc::_exit 1; |
101 | DC::_exit 1; |
100 | } |
102 | } |
101 | |
103 | |
102 | # win32 is fucked up, of course. exit will clean stuff up, |
104 | # win32 is fucked up, of course. exit will clean stuff up, |
103 | # which destroys our database etc. _exit will exit ALL |
105 | # which destroys our database etc. _exit will exit ALL |
104 | # forked processes, because of the dreaded fork emulation. |
106 | # forked processes, because of the dreaded fork emulation. |
105 | dc::_exit 0; |
107 | DC::_exit 0; |
106 | } |
108 | } |
107 | |
109 | |
108 | close $fh_w; |
110 | close $fh_w; |
109 | |
111 | |
110 | my $buffer; |
112 | my $buffer; |
… | |
… | |
122 | utf8::decode $line; |
124 | utf8::decode $line; |
123 | if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
125 | if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
124 | $cb->(JSON::XS->new->allow_nonref->decode ($1)); |
126 | $cb->(JSON::XS->new->allow_nonref->decode ($1)); |
125 | } else { |
127 | } else { |
126 | ::message ({ |
128 | ::message ({ |
127 | markup => "background($pid): " . dc::asxml $line, |
129 | markup => "background($pid): " . DC::asxml $line, |
128 | }); |
130 | }); |
129 | } |
131 | } |
130 | } |
132 | } |
131 | }); |
133 | }); |
132 | } |
134 | } |
… | |
… | |
138 | $msg =~ s/\n//g; |
140 | $msg =~ s/\n//g; |
139 | utf8::encode $msg; |
141 | utf8::encode $msg; |
140 | print $msg, "\n"; |
142 | print $msg, "\n"; |
141 | } |
143 | } |
142 | |
144 | |
143 | package dc; |
145 | package DC; |
144 | |
146 | |
145 | sub find_rcfile($) { |
147 | sub find_rcfile($) { |
146 | my $path; |
148 | my $path; |
147 | |
149 | |
148 | for (grep !ref, @INC) { |
150 | for (grep !ref, @INC) { |
… | |
… | |
164 | |
166 | |
165 | $::CFG = decode_json $CFG; |
167 | $::CFG = decode_json $CFG; |
166 | } |
168 | } |
167 | |
169 | |
168 | sub write_cfg { |
170 | sub write_cfg { |
169 | my ($file) = @_; |
171 | my $file = "$Deliantra::VARDIR/client.cf"; |
170 | |
172 | |
171 | $::CFG->{VERSION} = $::VERSION; |
173 | $::CFG->{VERSION} = $::VERSION; |
172 | |
174 | |
173 | open my $fh, ">:utf8", $file |
175 | open my $fh, ">:utf8", $file |
174 | or return; |
176 | or return; |
175 | print $fh encode_json $::CFG; |
177 | print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
176 | } |
178 | } |
177 | |
179 | |
178 | sub http_proxy { |
180 | sub http_proxy { |
179 | my @proxy = win32_proxy_info; |
181 | my @proxy = win32_proxy_info; |
180 | |
182 | |
… | |
… | |
195 | } |
197 | } |
196 | |
198 | |
197 | sub lwp_useragent { |
199 | sub lwp_useragent { |
198 | require LWP::UserAgent; |
200 | require LWP::UserAgent; |
199 | |
201 | |
200 | dc::set_proxy; |
202 | DC::set_proxy; |
201 | |
203 | |
202 | my $ua = LWP::UserAgent->new ( |
204 | my $ua = LWP::UserAgent->new ( |
203 | agent => "cfplus $VERSION", |
205 | agent => "deliantra $VERSION", |
204 | keep_alive => 1, |
206 | keep_alive => 1, |
205 | env_proxy => 1, |
207 | env_proxy => 1, |
206 | timeout => 30, |
208 | timeout => 30, |
207 | ); |
209 | ); |
208 | } |
210 | } |
… | |
… | |
223 | $nb = (! ! $nb) + 0; |
225 | $nb = (! ! $nb) + 0; |
224 | ioctl $fh, 0x8004667e, \$nb; # FIONBIO |
226 | ioctl $fh, 0x8004667e, \$nb; # FIONBIO |
225 | } else { |
227 | } else { |
226 | fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
228 | fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
227 | } |
229 | } |
228 | |
|
|
229 | } |
230 | } |
230 | |
231 | |
231 | package dc::Layout; |
232 | package DC::Layout; |
232 | |
233 | |
233 | $dc::OpenGL::INIT_HOOK{"dc::Layout"} = sub { |
234 | $DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub { |
234 | glyph_cache_restore; |
235 | glyph_cache_restore; |
235 | }; |
236 | }; |
236 | |
237 | |
237 | $dc::OpenGL::SHUTDOWN_HOOK{"dc::Layout"} = sub { |
238 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub { |
238 | glyph_cache_backup; |
239 | glyph_cache_backup; |
239 | }; |
240 | }; |
240 | |
241 | |
241 | 1; |
242 | 1; |
242 | |
243 | |