1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFPlus - undocumented utility garbage for our crossfire client |
3 | DC - undocumented utility garbage for our deliantra client |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFPlus; |
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 CFPlus; |
17 | package DC; |
16 | |
18 | |
17 | use Carp (); |
19 | use Carp (); |
18 | |
20 | |
19 | BEGIN { |
21 | BEGIN { |
20 | $VERSION = '0.97'; |
22 | $VERSION = '0.9973'; |
21 | |
23 | |
22 | use XSLoader; |
24 | use XSLoader; |
23 | XSLoader::load "CFPlus", $VERSION; |
25 | XSLoader::load "Deliantra::Client", $VERSION; |
24 | } |
26 | } |
25 | |
27 | |
26 | use utf8; |
28 | use utf8; |
27 | |
29 | |
28 | use AnyEvent (); |
30 | use AnyEvent (); |
29 | use Pod::POM (); |
31 | use Pod::POM (); |
30 | use File::Path (); |
32 | use File::Path (); |
31 | use Storable (); # finally |
33 | use Storable (); # finally |
32 | |
34 | use Fcntl (); |
33 | BEGIN { |
35 | use JSON::XS qw(encode_json decode_json); |
34 | use Crossfire::Protocol::Base (); |
|
|
35 | *to_json = \&Crossfire::Protocol::Base::to_json; |
|
|
36 | *from_json = \&Crossfire::Protocol::Base::from_json; |
|
|
37 | } |
|
|
38 | |
36 | |
39 | =item guard { BLOCK } |
37 | =item guard { BLOCK } |
40 | |
38 | |
41 | 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. |
42 | |
40 | |
43 | =cut |
41 | =cut |
44 | |
42 | |
45 | sub guard(&) { |
43 | sub guard(&) { |
46 | bless \(my $cb = $_[0]), "CFPlus::Guard" |
44 | bless \(my $cb = $_[0]), "DC::Guard" |
47 | } |
45 | } |
48 | |
46 | |
49 | sub CFPlus::Guard::DESTROY { |
47 | sub DC::Guard::DESTROY { |
50 | ${$_[0]}->() |
48 | ${$_[0]}->() |
51 | } |
49 | } |
52 | |
50 | |
53 | =item shorten $string[, $maxlength] |
51 | =item shorten $string[, $maxlength] |
54 | |
52 | |
… | |
… | |
78 | } |
76 | } |
79 | |
77 | |
80 | sub background(&;&) { |
78 | sub background(&;&) { |
81 | my ($bg, $cb) = @_; |
79 | my ($bg, $cb) = @_; |
82 | |
80 | |
83 | my ($fh_r, $fh_w) = CFPlus::socketpipe; |
81 | my ($fh_r, $fh_w) = DC::socketpipe; |
84 | |
82 | |
85 | my $pid = fork; |
83 | my $pid = fork; |
86 | |
84 | |
87 | if (defined $pid && !$pid) { |
85 | if (defined $pid && !$pid) { |
88 | local $SIG{__DIE__}; |
86 | local $SIG{__DIE__}; |
… | |
… | |
98 | |
96 | |
99 | if ($@) { |
97 | if ($@) { |
100 | my $msg = $@; |
98 | my $msg = $@; |
101 | $msg =~ s/\n+/\n/; |
99 | $msg =~ s/\n+/\n/; |
102 | warn "FATAL: $msg"; |
100 | warn "FATAL: $msg"; |
103 | CFPlus::_exit 1; |
101 | DC::_exit 1; |
104 | } |
102 | } |
105 | |
103 | |
106 | # win32 is fucked up, of course. exit will clean stuff up, |
104 | # win32 is fucked up, of course. exit will clean stuff up, |
107 | # which destroys our database etc. _exit will exit ALL |
105 | # which destroys our database etc. _exit will exit ALL |
108 | # forked processes, because of the dreaded fork emulation. |
106 | # forked processes, because of the dreaded fork emulation. |
109 | CFPlus::_exit 0; |
107 | DC::_exit 0; |
110 | } |
108 | } |
111 | |
109 | |
112 | close $fh_w; |
110 | close $fh_w; |
113 | |
111 | |
114 | my $buffer; |
112 | my $buffer; |
… | |
… | |
126 | utf8::decode $line; |
124 | utf8::decode $line; |
127 | if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
125 | if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
128 | $cb->(JSON::XS->new->allow_nonref->decode ($1)); |
126 | $cb->(JSON::XS->new->allow_nonref->decode ($1)); |
129 | } else { |
127 | } else { |
130 | ::message ({ |
128 | ::message ({ |
131 | markup => "background($pid): " . CFPlus::asxml $line, |
129 | markup => "background($pid): " . DC::asxml $line, |
132 | }); |
130 | }); |
133 | } |
131 | } |
134 | } |
132 | } |
135 | }); |
133 | }); |
136 | } |
134 | } |
… | |
… | |
142 | $msg =~ s/\n//g; |
140 | $msg =~ s/\n//g; |
143 | utf8::encode $msg; |
141 | utf8::encode $msg; |
144 | print $msg, "\n"; |
142 | print $msg, "\n"; |
145 | } |
143 | } |
146 | |
144 | |
147 | package CFPlus; |
145 | package DC; |
148 | |
146 | |
149 | sub find_rcfile($) { |
147 | sub find_rcfile($) { |
150 | my $path; |
148 | my $path; |
151 | |
149 | |
152 | for (grep !ref, @INC) { |
150 | for (grep !ref, @INC) { |
153 | $path = "$_/CFPlus/resources/$_[0]"; |
151 | $path = "$_/Deliantra/Client/private/resources/$_[0]"; |
154 | return $path if -r $path; |
152 | return $path if -r $path; |
155 | } |
153 | } |
156 | |
154 | |
157 | die "FATAL: can't find required file $_[0]\n"; |
155 | die "FATAL: can't find required file $_[0]\n"; |
158 | } |
156 | } |
… | |
… | |
164 | or return; |
162 | or return; |
165 | |
163 | |
166 | local $/; |
164 | local $/; |
167 | my $CFG = <$fh>; |
165 | my $CFG = <$fh>; |
168 | |
166 | |
169 | if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove |
|
|
170 | require YAML; |
|
|
171 | utf8::decode $CFG; |
|
|
172 | $::CFG = YAML::Load ($CFG); |
|
|
173 | } elsif ($CFG =~ /^\{/) { |
|
|
174 | $::CFG = from_json $CFG; |
167 | $::CFG = decode_json $CFG; |
175 | } else { |
|
|
176 | $::CFG = eval $CFG; ## todo comaptibility cruft |
|
|
177 | } |
|
|
178 | } |
168 | } |
179 | |
169 | |
180 | sub write_cfg { |
170 | sub write_cfg { |
181 | my ($file) = @_; |
171 | my $file = "$Deliantra::VARDIR/client.cf"; |
182 | |
172 | |
183 | $::CFG->{VERSION} = $::VERSION; |
173 | $::CFG->{VERSION} = $::VERSION; |
184 | |
174 | |
185 | open my $fh, ">:utf8", $file |
175 | open my $fh, ">:utf8", $file |
186 | or return; |
176 | or return; |
187 | print $fh to_json $::CFG; |
177 | print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
188 | } |
178 | } |
189 | |
179 | |
190 | sub http_proxy { |
180 | sub http_proxy { |
191 | my @proxy = win32_proxy_info; |
181 | my @proxy = win32_proxy_info; |
192 | |
182 | |
… | |
… | |
207 | } |
197 | } |
208 | |
198 | |
209 | sub lwp_useragent { |
199 | sub lwp_useragent { |
210 | require LWP::UserAgent; |
200 | require LWP::UserAgent; |
211 | |
201 | |
212 | CFPlus::set_proxy; |
202 | DC::set_proxy; |
213 | |
203 | |
214 | my $ua = LWP::UserAgent->new ( |
204 | my $ua = LWP::UserAgent->new ( |
215 | agent => "cfplus $VERSION", |
205 | agent => "deliantra $VERSION", |
216 | keep_alive => 1, |
206 | keep_alive => 1, |
217 | env_proxy => 1, |
207 | env_proxy => 1, |
218 | timeout => 30, |
208 | timeout => 30, |
219 | ); |
209 | ); |
220 | } |
210 | } |
… | |
… | |
226 | and die $res->status_line; |
216 | and die $res->status_line; |
227 | |
217 | |
228 | $res |
218 | $res |
229 | } |
219 | } |
230 | |
220 | |
|
|
221 | sub fh_nonblocking($$) { |
|
|
222 | my ($fh, $nb) = @_; |
|
|
223 | |
|
|
224 | if ($^O eq "MSWin32") { |
|
|
225 | $nb = (! ! $nb) + 0; |
|
|
226 | ioctl $fh, 0x8004667e, \$nb; # FIONBIO |
|
|
227 | } else { |
|
|
228 | fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
|
|
229 | } |
|
|
230 | } |
|
|
231 | |
231 | package CFPlus::Layout; |
232 | package DC::Layout; |
232 | |
233 | |
|
|
234 | $DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub { |
|
|
235 | glyph_cache_restore; |
|
|
236 | }; |
|
|
237 | |
233 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { |
238 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub { |
234 | reset_glyph_cache; |
239 | glyph_cache_backup; |
235 | }; |
240 | }; |
236 | |
241 | |
237 | 1; |
242 | 1; |
238 | |
243 | |
239 | =back |
244 | =back |