1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFClient - 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 CFClient; |
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 CFClient; |
15 | package DC; |
|
|
16 | |
|
|
17 | use Carp (); |
|
|
18 | |
|
|
19 | our $VERSION; |
16 | |
20 | |
17 | BEGIN { |
21 | BEGIN { |
18 | $VERSION = '0.1'; |
22 | $VERSION = '0.9976'; |
19 | |
23 | |
20 | use XSLoader; |
24 | use XSLoader; |
21 | XSLoader::load "CFClient", $VERSION; |
25 | XSLoader::load "Deliantra::Client", $VERSION; |
22 | } |
26 | } |
23 | |
27 | |
24 | our %GL_EXT; |
28 | use utf8; |
25 | our $GL_VERSION; |
29 | use strict qw(vars subs); |
26 | |
30 | |
27 | our $GL_NPOT; |
31 | use AnyEvent (); |
|
|
32 | use Pod::POM (); |
|
|
33 | use File::Path (); |
|
|
34 | use Storable (); # finally |
|
|
35 | use Fcntl (); |
|
|
36 | use JSON::XS qw(encode_json decode_json); |
28 | |
37 | |
29 | sub gl_init { |
38 | =item guard { BLOCK } |
30 | $GL_VERSION = gl_version * 1; |
|
|
31 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
|
|
32 | |
39 | |
33 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
40 | Returns an object that executes the given block as soon as it is destroyed. |
34 | |
41 | |
35 | CFClient::Texture::restore_state (); |
42 | =cut |
|
|
43 | |
|
|
44 | sub guard(&) { |
|
|
45 | bless \(my $cb = $_[0]), "DC::Guard" |
|
|
46 | } |
|
|
47 | |
|
|
48 | sub DC::Guard::DESTROY { |
|
|
49 | ${$_[0]}->() |
|
|
50 | } |
|
|
51 | |
|
|
52 | =item shorten $string[, $maxlength] |
|
|
53 | |
|
|
54 | =cut |
|
|
55 | |
|
|
56 | sub shorten($;$) { |
|
|
57 | my ($str, $len) = @_; |
|
|
58 | substr $str, $len, (length $str), "..." if $len + 3 <= length $str; |
|
|
59 | $str |
|
|
60 | } |
|
|
61 | |
|
|
62 | sub asxml($) { |
|
|
63 | local $_ = $_[0]; |
|
|
64 | |
|
|
65 | s/&/&/g; |
|
|
66 | s/>/>/g; |
|
|
67 | s/</</g; |
|
|
68 | |
|
|
69 | $_ |
|
|
70 | } |
|
|
71 | |
|
|
72 | sub socketpipe() { |
|
|
73 | socketpair my $fh1, my $fh2, &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC |
|
|
74 | or die "cannot establish bidirectional pipe: $!\n"; |
|
|
75 | |
|
|
76 | ($fh1, $fh2) |
|
|
77 | } |
|
|
78 | |
|
|
79 | sub background(&;&) { |
|
|
80 | my ($bg, $cb) = @_; |
|
|
81 | |
|
|
82 | my ($fh_r, $fh_w) = DC::socketpipe; |
|
|
83 | |
|
|
84 | my $pid = fork; |
|
|
85 | |
|
|
86 | if (defined $pid && !$pid) { |
|
|
87 | local $SIG{__DIE__}; |
|
|
88 | |
|
|
89 | open STDOUT, ">&", $fh_w; |
|
|
90 | open STDERR, ">&", $fh_w; |
|
|
91 | close $fh_r; |
|
|
92 | close $fh_w; |
|
|
93 | |
|
|
94 | $| = 1; |
|
|
95 | |
|
|
96 | eval { $bg->() }; |
|
|
97 | |
|
|
98 | if ($@) { |
|
|
99 | my $msg = $@; |
|
|
100 | $msg =~ s/\n+/\n/; |
|
|
101 | warn "FATAL: $msg"; |
|
|
102 | DC::_exit 1; |
|
|
103 | } |
|
|
104 | |
|
|
105 | # win32 is fucked up, of course. exit will clean stuff up, |
|
|
106 | # which destroys our database etc. _exit will exit ALL |
|
|
107 | # forked processes, because of the dreaded fork emulation. |
|
|
108 | DC::_exit 0; |
|
|
109 | } |
|
|
110 | |
|
|
111 | close $fh_w; |
|
|
112 | |
|
|
113 | my $buffer; |
|
|
114 | |
|
|
115 | my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub { |
|
|
116 | unless (sysread $fh_r, $buffer, 4096, length $buffer) { |
|
|
117 | undef $w; |
|
|
118 | $cb->(); |
|
|
119 | return; |
|
|
120 | } |
|
|
121 | |
|
|
122 | while ($buffer =~ s/^(.*)\n//) { |
|
|
123 | my $line = $1; |
|
|
124 | $line =~ s/\s+$//; |
|
|
125 | utf8::decode $line; |
|
|
126 | if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
|
|
127 | $cb->(JSON::XS->new->allow_nonref->decode ($1)); |
|
|
128 | } else { |
|
|
129 | ::message ({ |
|
|
130 | markup => "background($pid): " . DC::asxml $line, |
|
|
131 | }); |
|
|
132 | } |
|
|
133 | } |
|
|
134 | }); |
|
|
135 | } |
|
|
136 | |
|
|
137 | sub background_msg { |
|
|
138 | my ($msg) = @_; |
|
|
139 | |
|
|
140 | $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg); |
|
|
141 | $msg =~ s/\n//g; |
|
|
142 | utf8::encode $msg; |
|
|
143 | print $msg, "\n"; |
|
|
144 | } |
|
|
145 | |
|
|
146 | package DC; |
|
|
147 | |
|
|
148 | our $RC_THEME; |
|
|
149 | our %THEME; |
|
|
150 | our @RC_PATH; |
|
|
151 | our $RC_BASE; |
|
|
152 | |
|
|
153 | for (grep !ref, @INC) { |
|
|
154 | $RC_BASE = "$_/Deliantra/Client/private/resources"; |
|
|
155 | last if -d $RC_BASE; |
36 | } |
156 | } |
37 | |
157 | |
38 | sub find_rcfile($) { |
158 | sub find_rcfile($) { |
39 | my $path; |
159 | my $path; |
40 | |
160 | |
41 | for (@INC) { |
161 | for (@RC_PATH, "") { |
42 | $path = "$_/CFClient/resources/$_[0]"; |
162 | $path = "$RC_BASE/$_/$_[0]"; |
43 | return $path if -r $path; |
163 | return $path if -r $path; |
44 | } |
164 | } |
45 | |
165 | |
46 | die "FATAL: can't find required file $_[0]\n"; |
166 | die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; |
|
|
167 | } |
|
|
168 | |
|
|
169 | sub load_json($) { |
|
|
170 | my ($file) = @_; |
|
|
171 | |
|
|
172 | open my $fh, $file |
|
|
173 | or return; |
|
|
174 | |
|
|
175 | local $/; |
|
|
176 | JSON::XS->new->utf8->relaxed->decode (<$fh>) |
|
|
177 | } |
|
|
178 | |
|
|
179 | sub set_theme($) { |
|
|
180 | return if $RC_THEME eq $_[0]; |
|
|
181 | $RC_THEME = $_[0]; |
|
|
182 | |
|
|
183 | # kind of hacky, find the main theme file, then load all theme files and merge them |
|
|
184 | |
|
|
185 | %THEME = (); |
|
|
186 | @RC_PATH = "theme-$RC_THEME"; |
|
|
187 | |
|
|
188 | my $theme = load_json find_rcfile "theme.json" |
|
|
189 | or die "FATAL: theme resource file not found"; |
|
|
190 | |
|
|
191 | @RC_PATH = @{ $theme->{path} } if $theme->{path}; |
|
|
192 | |
|
|
193 | for (@RC_PATH, "") { |
|
|
194 | my $theme = load_json "$RC_BASE/$_/theme.json" |
|
|
195 | or next; |
|
|
196 | |
|
|
197 | %THEME = ( %$theme, %THEME ); |
|
|
198 | } |
47 | } |
199 | } |
48 | |
200 | |
49 | sub read_cfg { |
201 | sub read_cfg { |
50 | my ($file) = @_; |
202 | my ($file) = @_; |
51 | |
203 | |
52 | open CFG, $file |
204 | $::CFG = load_json $file; |
|
|
205 | } |
|
|
206 | |
|
|
207 | sub write_cfg { |
|
|
208 | my $file = "$Deliantra::VARDIR/client.cf"; |
|
|
209 | |
|
|
210 | $::CFG->{VERSION} = $::VERSION; |
|
|
211 | |
|
|
212 | open my $fh, ">:utf8", $file |
53 | or return; |
213 | or return; |
54 | |
214 | print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
55 | my $CFG; |
|
|
56 | |
|
|
57 | local $/; |
|
|
58 | $CFG = eval <CFG>; |
|
|
59 | |
|
|
60 | $::CFG = $CFG; |
|
|
61 | |
|
|
62 | close CFG; |
|
|
63 | } |
215 | } |
64 | |
216 | |
65 | sub write_cfg { |
217 | sub http_proxy { |
66 | my ($file) = @_; |
218 | my @proxy = win32_proxy_info; |
67 | |
219 | |
68 | open CFG, ">$file" |
220 | if (@proxy) { |
|
|
221 | "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0] |
|
|
222 | } elsif (exists $ENV{http_proxy}) { |
|
|
223 | $ENV{http_proxy} |
|
|
224 | } else { |
|
|
225 | () |
|
|
226 | } |
|
|
227 | } |
|
|
228 | |
|
|
229 | sub set_proxy { |
|
|
230 | my $proxy = http_proxy |
69 | or return; |
231 | or return; |
70 | |
232 | |
|
|
233 | $ENV{http_proxy} = $proxy; |
|
|
234 | } |
|
|
235 | |
|
|
236 | sub lwp_useragent { |
|
|
237 | require LWP::UserAgent; |
71 | { |
238 | |
72 | require Data::Dumper; |
239 | DC::set_proxy; |
73 | local $Data::Dumper::Purity = 1; |
|
|
74 | $::CFG->{VERSION} = $::VERSION; |
|
|
75 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
|
|
76 | } |
|
|
77 | |
240 | |
78 | close CFG; |
241 | my $ua = LWP::UserAgent->new ( |
79 | } |
242 | agent => "deliantra $VERSION", |
80 | |
243 | keep_alive => 1, |
81 | package CFClient::Texture; |
244 | env_proxy => 1, |
82 | |
245 | timeout => 30, |
83 | use Scalar::Util; |
|
|
84 | |
|
|
85 | use SDL::OpenGL; |
|
|
86 | |
|
|
87 | my @textures; |
|
|
88 | |
|
|
89 | sub new { |
|
|
90 | my ($class, %data) = @_; |
|
|
91 | |
|
|
92 | my $self = bless { |
|
|
93 | internalformat => GL_RGBA, |
|
|
94 | format => GL_RGBA, |
|
|
95 | %data, |
|
|
96 | }, $class; |
|
|
97 | |
|
|
98 | push @textures, $self; |
|
|
99 | Scalar::Util::weaken $textures[-1]; |
|
|
100 | |
|
|
101 | $self->upload; |
|
|
102 | |
|
|
103 | $self |
|
|
104 | } |
|
|
105 | |
|
|
106 | sub new_from_image { |
|
|
107 | my ($class, $image) = @_; |
|
|
108 | |
|
|
109 | $class->new (image => $image) |
|
|
110 | } |
|
|
111 | |
|
|
112 | sub new_from_file { |
|
|
113 | my ($class, $path) = @_; |
|
|
114 | |
|
|
115 | open my $fh, "<:raw", $path |
|
|
116 | or die "$path: $!"; |
|
|
117 | |
|
|
118 | local $/; |
|
|
119 | $class->new_from_image (<$fh>) |
|
|
120 | } |
|
|
121 | |
|
|
122 | #sub new_from_surface { |
|
|
123 | # my ($class, $surface) = @_; |
|
|
124 | # |
|
|
125 | # $surface->rgba; |
|
|
126 | # |
|
|
127 | # $class->new ( |
|
|
128 | # data => $surface->pixels, |
|
|
129 | # w => $surface->width, |
|
|
130 | # h => $surface->height, |
|
|
131 | # ) |
|
|
132 | #} |
|
|
133 | |
|
|
134 | sub new_from_layout { |
|
|
135 | my ($class, $layout) = @_; |
|
|
136 | |
|
|
137 | my ($w, $h, $data) = $layout->render; |
|
|
138 | |
|
|
139 | $class->new ( |
|
|
140 | w => $w, |
|
|
141 | h => $h, |
|
|
142 | data => $data, |
|
|
143 | internalformat => GL_ALPHA4, |
|
|
144 | format => GL_ALPHA, |
|
|
145 | ) |
246 | ); |
146 | } |
247 | } |
147 | |
248 | |
148 | sub new_from_opengl { |
249 | sub lwp_check($) { |
149 | my ($class, $w, $h, $cb) = @_; |
|
|
150 | |
|
|
151 | $class->new (w => $w, h => $h, render_cb => $cb) |
|
|
152 | } |
|
|
153 | |
|
|
154 | sub topot { |
|
|
155 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
|
|
156 | } |
|
|
157 | |
|
|
158 | sub upload { |
|
|
159 | my ($self) = @_; |
250 | my ($res) = @_; |
160 | |
251 | |
161 | return unless $SDL::App::USING_OPENGL; |
252 | $res->is_error |
|
|
253 | and die $res->status_line; |
162 | |
254 | |
163 | my $data; |
255 | $res |
|
|
256 | } |
164 | |
257 | |
165 | if (exists $self->{data}) { |
258 | sub fh_nonblocking($$) { |
166 | $data = $self->{data}; |
259 | my ($fh, $nb) = @_; |
167 | } elsif (exists $self->{render_cb}) { |
|
|
168 | glViewport 0, 0, $self->{w}, $self->{h}; |
|
|
169 | glMatrixMode GL_PROJECTION; |
|
|
170 | glLoadIdentity; |
|
|
171 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
|
|
172 | glMatrixMode GL_MODELVIEW; |
|
|
173 | glPushmatrix; |
|
|
174 | glLoadIdentity; |
|
|
175 | glClear GL_COLOR_BUFFER_BIT; |
|
|
176 | |
260 | |
177 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
261 | if ($^O eq "MSWin32") { |
|
|
262 | $nb = (! ! $nb) + 0; |
|
|
263 | ioctl $fh, 0x8004667e, \$nb; # FIONBIO |
178 | } else { |
264 | } else { |
179 | my $pb = new Gtk2::Gdk::PixbufLoader; |
265 | fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
180 | $pb->write ($self->{image}); |
|
|
181 | $pb->close; |
|
|
182 | |
|
|
183 | $pb = $pb->get_pixbuf; |
|
|
184 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
185 | |
|
|
186 | $self->{w} = $pb->get_width; |
|
|
187 | $self->{h} = $pb->get_height; |
|
|
188 | |
|
|
189 | $data = $pb->get_pixels; |
|
|
190 | } |
|
|
191 | |
|
|
192 | my ($tw, $th) = @$self{qw(w h)}; |
|
|
193 | |
|
|
194 | unless ($tw || $th) { |
|
|
195 | $tw = $th = 1; |
|
|
196 | $data = "\x00" x 64; |
|
|
197 | } |
|
|
198 | |
|
|
199 | unless ($GL_NPOT) { |
|
|
200 | # TODO: does not work for zero-sized textures |
|
|
201 | $tw = topot $tw; |
|
|
202 | $th = topot $th; |
|
|
203 | |
|
|
204 | if (defined $data) { |
|
|
205 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
|
|
206 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
207 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
|
|
208 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
|
|
209 | } |
|
|
210 | } |
|
|
211 | |
|
|
212 | $self->{s} = $self->{w} / $tw; |
|
|
213 | $self->{t} = $self->{h} / $th; |
|
|
214 | |
|
|
215 | $self->{name} ||= (glGenTextures 1)->[0]; |
|
|
216 | |
|
|
217 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
|
|
218 | |
|
|
219 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
220 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
|
|
221 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
|
|
222 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
223 | |
266 | } |
224 | glGetError; |
|
|
225 | if (defined $data) { |
|
|
226 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
227 | $self->{internalformat}, |
|
|
228 | $tw, $th, # need to pad texture first |
|
|
229 | 0, |
|
|
230 | $self->{format}, |
|
|
231 | GL_UNSIGNED_BYTE, |
|
|
232 | $data; |
|
|
233 | glGetError and die; |
|
|
234 | } else { |
|
|
235 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
236 | $self->{internalformat}, |
|
|
237 | 0, 0, |
|
|
238 | $tw, $th, |
|
|
239 | 0; |
|
|
240 | glPopmatrix; |
|
|
241 | } |
|
|
242 | } |
267 | } |
243 | |
268 | |
244 | sub DESTROY { |
269 | package DC::Layout; |
245 | my ($self) = @_; |
|
|
246 | |
270 | |
247 | return unless exists $self->{name}; |
271 | $DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub { |
|
|
272 | glyph_cache_restore; |
|
|
273 | }; |
248 | |
274 | |
249 | glDeleteTextures delete $self->{name}; |
275 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub { |
250 | } |
276 | glyph_cache_backup; |
251 | |
|
|
252 | sub restore_state{ |
|
|
253 | $_->upload |
|
|
254 | for grep $_, @textures; |
|
|
255 | }; |
277 | }; |
256 | |
278 | |
257 | 1; |
279 | 1; |
258 | |
280 | |
259 | =back |
281 | =back |