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