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