1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFClient - undocumented utility garbage for our crossfire client |
3 | CFPlus - undocumented utility garbage for our crossfire client |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use CFClient; |
7 | use CFPlus; |
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 CFClient; |
15 | package CFPlus; |
|
|
16 | |
|
|
17 | use Carp (); |
16 | |
18 | |
17 | BEGIN { |
19 | BEGIN { |
18 | $VERSION = '0.1'; |
20 | $VERSION = '0.9841'; |
19 | |
21 | |
20 | use XSLoader; |
22 | use XSLoader; |
21 | XSLoader::load "CFClient", $VERSION; |
23 | XSLoader::load "CFPlus", $VERSION; |
22 | } |
24 | } |
23 | |
25 | |
24 | use Carp (); |
26 | use utf8; |
|
|
27 | |
25 | use AnyEvent; |
28 | use AnyEvent (); |
26 | use BerkeleyDB; |
29 | use Pod::POM (); |
27 | use CFClient::OpenGL; |
30 | use File::Path (); |
|
|
31 | use Storable (); # finally |
|
|
32 | use Fcntl (); |
|
|
33 | use JSON::XS qw(to_json from_json); |
28 | |
34 | |
29 | our %GL_EXT; |
35 | =item guard { BLOCK } |
30 | our $GL_VERSION; |
|
|
31 | |
36 | |
32 | our $GL_NPOT; |
37 | Returns an object that executes the given block as soon as it is destroyed. |
33 | |
38 | |
34 | sub gl_init { |
39 | =cut |
35 | $GL_VERSION = gl_version * 1; |
|
|
36 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
|
|
37 | |
40 | |
38 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
41 | sub guard(&) { |
39 | |
42 | bless \(my $cb = $_[0]), "CFPlus::Guard" |
40 | glEnable GL_TEXTURE_2D; |
|
|
41 | glEnable GL_COLOR_MATERIAL; |
|
|
42 | glShadeModel GL_FLAT; |
|
|
43 | glDisable GL_DEPTH_TEST; |
|
|
44 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
45 | |
|
|
46 | glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST; |
|
|
47 | |
|
|
48 | CFClient::Texture::restore_state (); |
|
|
49 | } |
43 | } |
|
|
44 | |
|
|
45 | sub CFPlus::Guard::DESTROY { |
|
|
46 | ${$_[0]}->() |
|
|
47 | } |
|
|
48 | |
|
|
49 | =item shorten $string[, $maxlength] |
|
|
50 | |
|
|
51 | =cut |
|
|
52 | |
|
|
53 | sub shorten($;$) { |
|
|
54 | my ($str, $len) = @_; |
|
|
55 | substr $str, $len, (length $str), "..." if $len + 3 <= length $str; |
|
|
56 | $str |
|
|
57 | } |
|
|
58 | |
|
|
59 | sub asxml($) { |
|
|
60 | local $_ = $_[0]; |
|
|
61 | |
|
|
62 | s/&/&/g; |
|
|
63 | s/>/>/g; |
|
|
64 | s/</</g; |
|
|
65 | |
|
|
66 | $_ |
|
|
67 | } |
|
|
68 | |
|
|
69 | sub socketpipe() { |
|
|
70 | socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
|
|
71 | or die "cannot establish bidirectional pipe: $!\n"; |
|
|
72 | |
|
|
73 | ($fh1, $fh2) |
|
|
74 | } |
|
|
75 | |
|
|
76 | sub background(&;&) { |
|
|
77 | my ($bg, $cb) = @_; |
|
|
78 | |
|
|
79 | my ($fh_r, $fh_w) = CFPlus::socketpipe; |
|
|
80 | |
|
|
81 | my $pid = fork; |
|
|
82 | |
|
|
83 | if (defined $pid && !$pid) { |
|
|
84 | local $SIG{__DIE__}; |
|
|
85 | |
|
|
86 | open STDOUT, ">&", $fh_w; |
|
|
87 | open STDERR, ">&", $fh_w; |
|
|
88 | close $fh_r; |
|
|
89 | close $fh_w; |
|
|
90 | |
|
|
91 | $| = 1; |
|
|
92 | |
|
|
93 | eval { $bg->() }; |
|
|
94 | |
|
|
95 | if ($@) { |
|
|
96 | my $msg = $@; |
|
|
97 | $msg =~ s/\n+/\n/; |
|
|
98 | warn "FATAL: $msg"; |
|
|
99 | CFPlus::_exit 1; |
|
|
100 | } |
|
|
101 | |
|
|
102 | # win32 is fucked up, of course. exit will clean stuff up, |
|
|
103 | # which destroys our database etc. _exit will exit ALL |
|
|
104 | # forked processes, because of the dreaded fork emulation. |
|
|
105 | CFPlus::_exit 0; |
|
|
106 | } |
|
|
107 | |
|
|
108 | close $fh_w; |
|
|
109 | |
|
|
110 | my $buffer; |
|
|
111 | |
|
|
112 | my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub { |
|
|
113 | unless (sysread $fh_r, $buffer, 4096, length $buffer) { |
|
|
114 | undef $w; |
|
|
115 | $cb->(); |
|
|
116 | return; |
|
|
117 | } |
|
|
118 | |
|
|
119 | while ($buffer =~ s/^(.*)\n//) { |
|
|
120 | my $line = $1; |
|
|
121 | $line =~ s/\s+$//; |
|
|
122 | utf8::decode $line; |
|
|
123 | if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
|
|
124 | $cb->(JSON::XS->new->allow_nonref->decode ($1)); |
|
|
125 | } else { |
|
|
126 | ::message ({ |
|
|
127 | markup => "background($pid): " . CFPlus::asxml $line, |
|
|
128 | }); |
|
|
129 | } |
|
|
130 | } |
|
|
131 | }); |
|
|
132 | } |
|
|
133 | |
|
|
134 | sub background_msg { |
|
|
135 | my ($msg) = @_; |
|
|
136 | |
|
|
137 | $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg); |
|
|
138 | $msg =~ s/\n//g; |
|
|
139 | utf8::encode $msg; |
|
|
140 | print $msg, "\n"; |
|
|
141 | } |
|
|
142 | |
|
|
143 | package CFPlus; |
50 | |
144 | |
51 | sub find_rcfile($) { |
145 | sub find_rcfile($) { |
52 | my $path; |
146 | my $path; |
53 | |
147 | |
54 | for (grep !ref, @INC) { |
148 | for (grep !ref, @INC) { |
55 | $path = "$_/CFClient/resources/$_[0]"; |
149 | $path = "$_/CFPlus/resources/$_[0]"; |
56 | return $path if -r $path; |
150 | return $path if -r $path; |
57 | } |
151 | } |
58 | |
152 | |
59 | die "FATAL: can't find required file $_[0]\n"; |
153 | die "FATAL: can't find required file $_[0]\n"; |
60 | } |
154 | } |
61 | |
155 | |
62 | sub read_cfg { |
156 | sub read_cfg { |
63 | my ($file) = @_; |
157 | my ($file) = @_; |
64 | |
158 | |
65 | open CFG, $file |
159 | open my $fh, $file |
66 | or return; |
160 | or return; |
67 | |
161 | |
68 | my $CFG; |
|
|
69 | |
|
|
70 | local $/; |
162 | local $/; |
71 | $CFG = eval <CFG>; |
163 | my $CFG = <$fh>; |
72 | |
164 | |
73 | $::CFG = $CFG; |
165 | if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove |
74 | |
166 | require YAML; |
75 | close CFG; |
167 | utf8::decode $CFG; |
|
|
168 | $::CFG = YAML::Load ($CFG); |
|
|
169 | } elsif ($CFG =~ /^\{/) { |
|
|
170 | $::CFG = from_json $CFG; |
|
|
171 | } else { |
|
|
172 | $::CFG = eval $CFG; ## todo comaptibility cruft |
|
|
173 | } |
76 | } |
174 | } |
77 | |
175 | |
78 | sub write_cfg { |
176 | sub write_cfg { |
79 | my ($file) = @_; |
177 | my ($file) = @_; |
80 | |
178 | |
81 | open CFG, ">$file" |
179 | $::CFG->{VERSION} = $::VERSION; |
|
|
180 | |
|
|
181 | open my $fh, ">:utf8", $file |
82 | or return; |
182 | or return; |
|
|
183 | print $fh to_json $::CFG; |
|
|
184 | } |
83 | |
185 | |
|
|
186 | sub http_proxy { |
|
|
187 | my @proxy = win32_proxy_info; |
|
|
188 | |
|
|
189 | if (@proxy) { |
|
|
190 | "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0] |
|
|
191 | } elsif (exists $ENV{http_proxy}) { |
|
|
192 | $ENV{http_proxy} |
|
|
193 | } else { |
|
|
194 | () |
84 | { |
195 | } |
85 | require Data::Dumper; |
|
|
86 | local $Data::Dumper::Purity = 1; |
|
|
87 | $::CFG->{VERSION} = $::VERSION; |
|
|
88 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
|
|
89 | } |
|
|
90 | |
|
|
91 | close CFG; |
|
|
92 | } |
196 | } |
93 | |
197 | |
94 | mkdir "$Crossfire::VARDIR/pclient", 0777; |
198 | sub set_proxy { |
|
|
199 | my $proxy = http_proxy |
|
|
200 | or return; |
95 | |
201 | |
96 | our $DB_ENV = new BerkeleyDB::Env |
202 | $ENV{http_proxy} = $proxy; |
97 | -Home => "$Crossfire::VARDIR/pclient", |
203 | } |
98 | -Cachesize => 1_000_000, |
|
|
99 | -ErrFile => "$Crossfire::VARDIR/pclient/errorlog.txt", |
|
|
100 | # -ErrPrefix => "DATABASE", |
|
|
101 | -Verbose => 1, |
|
|
102 | -Flags => DB_CREATE | DB_RECOVER_FATAL | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN, |
|
|
103 | or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error"; |
|
|
104 | |
204 | |
105 | sub db_table($) { |
205 | sub lwp_useragent { |
106 | my ($table) = @_; |
206 | require LWP::UserAgent; |
107 | |
|
|
108 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
109 | |
207 | |
110 | new CFClient::Database |
208 | CFPlus::set_proxy; |
111 | -Env => $DB_ENV, |
|
|
112 | -Filename => $table, |
|
|
113 | # -Filename => "database", |
|
|
114 | # -Subname => $table, |
|
|
115 | -Flags => DB_CREATE | DB_UPGRADE, |
|
|
116 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"; |
|
|
117 | } |
|
|
118 | |
209 | |
119 | package CFClient::Database; |
210 | my $ua = LWP::UserAgent->new ( |
120 | |
211 | agent => "cfplus $VERSION", |
121 | our @ISA = BerkeleyDB::Btree::; |
212 | keep_alive => 1, |
122 | |
213 | env_proxy => 1, |
123 | sub get($$) { |
214 | timeout => 30, |
124 | my $data; |
|
|
125 | |
|
|
126 | $_[0]->db_get ($_[1], $data) == 0 |
|
|
127 | ? $data |
|
|
128 | : () |
|
|
129 | } |
|
|
130 | |
|
|
131 | my %DB_SYNC; |
|
|
132 | |
|
|
133 | sub put($$$) { |
|
|
134 | my ($db, $key, $data) = @_; |
|
|
135 | |
|
|
136 | $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync }); |
|
|
137 | |
|
|
138 | $db->db_put ($key => $data) |
|
|
139 | } |
|
|
140 | |
|
|
141 | package CFClient::Texture; |
|
|
142 | |
|
|
143 | use strict; |
|
|
144 | |
|
|
145 | use Scalar::Util; |
|
|
146 | |
|
|
147 | use CFClient::OpenGL; |
|
|
148 | |
|
|
149 | my %TEXTURES; |
|
|
150 | |
|
|
151 | sub new { |
|
|
152 | my ($class, %data) = @_; |
|
|
153 | |
|
|
154 | my $self = bless { |
|
|
155 | internalformat => GL_RGBA, |
|
|
156 | format => GL_RGBA, |
|
|
157 | type => GL_UNSIGNED_BYTE, |
|
|
158 | %data, |
|
|
159 | }, $class; |
|
|
160 | |
|
|
161 | Scalar::Util::weaken ($TEXTURES{$self+0} = $self); |
|
|
162 | |
|
|
163 | $self->upload; |
|
|
164 | |
|
|
165 | $self |
|
|
166 | } |
|
|
167 | |
|
|
168 | sub new_from_image { |
|
|
169 | my ($class, $image, %arg) = @_; |
|
|
170 | |
|
|
171 | $class->new (image => $image, %arg) |
|
|
172 | } |
|
|
173 | |
|
|
174 | sub new_from_file { |
|
|
175 | my ($class, $path, %arg) = @_; |
|
|
176 | |
|
|
177 | open my $fh, "<:raw", $path |
|
|
178 | or die "$path: $!"; |
|
|
179 | |
|
|
180 | local $/; |
|
|
181 | $class->new_from_image (<$fh>, %arg) |
|
|
182 | } |
|
|
183 | |
|
|
184 | #sub new_from_surface { |
|
|
185 | # my ($class, $surface) = @_; |
|
|
186 | # |
|
|
187 | # $surface->rgba; |
|
|
188 | # |
|
|
189 | # $class->new ( |
|
|
190 | # data => $surface->pixels, |
|
|
191 | # w => $surface->width, |
|
|
192 | # h => $surface->height, |
|
|
193 | # ) |
|
|
194 | #} |
|
|
195 | |
|
|
196 | sub new_from_layout { |
|
|
197 | my ($class, $layout, %arg) = @_; |
|
|
198 | |
|
|
199 | my ($w, $h, $data) = $layout->render; |
|
|
200 | |
|
|
201 | $class->new ( |
|
|
202 | w => $w, |
|
|
203 | h => $h, |
|
|
204 | data => $data, |
|
|
205 | format => GL_ALPHA, |
|
|
206 | internalformat => GL_ALPHA, |
|
|
207 | type => GL_UNSIGNED_BYTE, |
|
|
208 | %arg, |
|
|
209 | ) |
215 | ); |
210 | } |
216 | } |
211 | |
217 | |
212 | sub new_from_opengl { |
218 | sub lwp_check($) { |
213 | my ($class, $w, $h, $cb) = @_; |
|
|
214 | |
|
|
215 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
|
|
216 | } |
|
|
217 | |
|
|
218 | sub topot { |
|
|
219 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
|
|
220 | } |
|
|
221 | |
|
|
222 | sub upload { |
|
|
223 | my ($self) = @_; |
219 | my ($res) = @_; |
224 | |
220 | |
225 | return unless $GL_VERSION; |
221 | $res->is_error |
|
|
222 | and die $res->status_line; |
226 | |
223 | |
227 | my $data; |
224 | $res |
|
|
225 | } |
228 | |
226 | |
229 | if (exists $self->{data}) { |
227 | sub fh_nonblocking($$) { |
230 | $data = $self->{data}; |
228 | my ($fh, $nb) = @_; |
231 | |
229 | |
232 | } elsif (exists $self->{render_cb}) { |
230 | if ($^O eq "MSWin32") { |
233 | glViewport 0, 0, $self->{w}, $self->{h}; |
231 | $nb = (! ! $nb) + 0; |
234 | glMatrixMode GL_PROJECTION; |
232 | ioctl $fh, 0x8004667e, \$nb; # FIONBIO |
235 | glLoadIdentity; |
|
|
236 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
|
|
237 | glMatrixMode GL_MODELVIEW; |
|
|
238 | glLoadIdentity; |
|
|
239 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
|
|
240 | |
|
|
241 | } else { |
233 | } else { |
242 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
234 | fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
243 | = CFClient::load_image_inline $self->{image}; |
|
|
244 | } |
|
|
245 | |
|
|
246 | my ($tw, $th) = @$self{qw(w h)}; |
|
|
247 | |
|
|
248 | unless ($tw > 0 && $th > 0) { |
|
|
249 | $tw = $th = 1; |
|
|
250 | $data = "\x00" x 64; |
|
|
251 | } |
|
|
252 | |
|
|
253 | $self->{minified} = [CFClient::average $tw, $th, $data] |
|
|
254 | if $self->{minify}; |
|
|
255 | |
|
|
256 | unless ($GL_NPOT) { |
|
|
257 | # TODO: does not work for zero-sized textures |
|
|
258 | $tw = topot $tw; |
|
|
259 | $th = topot $th; |
|
|
260 | |
|
|
261 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
|
|
262 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
|
|
263 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
264 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
|
|
265 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
|
|
266 | } |
|
|
267 | } |
|
|
268 | |
|
|
269 | $self->{s} = $self->{w} / $tw; |
|
|
270 | $self->{t} = $self->{h} / $th; |
|
|
271 | |
|
|
272 | $self->{name} ||= glGenTexture; |
|
|
273 | |
|
|
274 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
|
|
275 | |
|
|
276 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
|
|
277 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
278 | |
|
|
279 | if ($::FAST) { |
|
|
280 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
281 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
282 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
283 | # alternatively check for 0x8191 |
|
|
284 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
285 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
286 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
287 | } else { |
|
|
288 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
289 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
290 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
|
|
291 | } |
|
|
292 | |
235 | } |
293 | glGetError; |
|
|
294 | |
236 | |
295 | if (defined $data) { |
|
|
296 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
297 | $self->{internalformat}, |
|
|
298 | $tw, $th, # need to pad texture first |
|
|
299 | 0, |
|
|
300 | $self->{format}, |
|
|
301 | $self->{type}, |
|
|
302 | $data; |
|
|
303 | if (my $error = glGetError) { |
|
|
304 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x f=%x t=%x", |
|
|
305 | $error, $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
|
|
306 | } |
|
|
307 | } else { |
|
|
308 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
309 | $self->{internalformat}, |
|
|
310 | 0, 0, |
|
|
311 | $tw, $th, |
|
|
312 | 0; |
|
|
313 | if (my $error = glGetError) { |
|
|
314 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x", |
|
|
315 | $error, $tw, $th, $self->{internalformat}; |
|
|
316 | } |
|
|
317 | } |
|
|
318 | } |
237 | } |
319 | |
238 | |
320 | sub DESTROY { |
239 | package CFPlus::Layout; |
321 | my ($self) = @_; |
|
|
322 | |
240 | |
323 | delete $TEXTURES{$self+0}; |
241 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { |
324 | |
242 | reset_glyph_cache; |
325 | glDeleteTexture delete $self->{name} |
|
|
326 | if $self->{name}; |
|
|
327 | } |
|
|
328 | |
|
|
329 | sub restore_state{ |
|
|
330 | $_->upload |
|
|
331 | for values %TEXTURES; |
|
|
332 | }; |
243 | }; |
333 | |
244 | |
334 | 1; |
245 | 1; |
335 | |
246 | |
336 | =back |
247 | =back |