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