ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.3 by root, Fri Apr 7 18:20:13 2006 UTC vs.
Revision 1.200 by root, Sun Jan 4 10:22:19 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3Crossfire::Client - undocumented utility garbage for our crossfire client 3DC - 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
15package Crossfire::Client; 15package DC;
16
17use Carp ();
18
19our $VERSION;
16 20
17BEGIN { 21BEGIN {
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
24package Crossfire::Client::Texture; 28use utf8;
29use strict qw(vars subs);
25 30
26use Scalar::Util; 31use Socket ();
32use AnyEvent ();
33use AnyEvent::Util ();
34use Pod::POM ();
35use File::Path ();
36use Storable (); # finally
37use Fcntl ();
38use JSON::XS qw(encode_json decode_json);
27 39
28use SDL::OpenGL; 40=item guard { BLOCK }
29 41
30my @textures; 42Returns an object that executes the given block as soon as it is destroyed.
31 43
32sub new_from_file { 44=cut
33 my ($class, $path) = @_;
34 45
35 open my $fh, "<:raw", $path 46sub guard(&) {
36 or die "$path: $!"; 47 bless \(my $cb = $_[0]), "DC::Guard"
48}
49
50sub DC::Guard::DESTROY {
51 ${$_[0]}->()
52}
53
54=item shorten $string[, $maxlength]
55
56=cut
57
58sub shorten($;$) {
59 my ($str, $len) = @_;
60 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
61 $str
62}
63
64sub asxml($) {
65 local $_ = $_[0];
66
67 s/&/&amp;/g;
68 s/>/&gt;/g;
69 s/</&lt;/g;
70
71 $_
72}
73
74sub 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
133sub 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
142package DC;
143
144our $RC_THEME;
145our %THEME;
146our @RC_PATH;
147our $RC_BASE;
148
149for (grep !ref, @INC) {
150 $RC_BASE = "$_/Deliantra/Client/private/resources";
151 last if -d $RC_BASE;
152}
153
154sub 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
165sub 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
42sub new_from_image { 175sub 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
57sub upload { 197sub read_cfg {
58 my ($self) = @_; 198 my ($file) = @_;
59 199
60 return unless $SDL::App::USING_OPENGL; 200 $::CFG = load_json $file;
201}
61 202
62 my ($data, $width, $height); 203sub 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
208 open my $fh, ">:utf8", $file
209 or return;
210 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
211}
212
213sub http_proxy {
214 my @proxy = win32_proxy_info;
215
216 if (@proxy) {
217 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
218 } elsif (exists $ENV{http_proxy}) {
219 $ENV{http_proxy}
66 } else { 220 } else {
67 my $pb = new Gtk2::Gdk::PixbufLoader; 221 ()
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 } 222 }
223}
76 224
77 ($self->{name}) = @{glGenTextures 1}; 225sub set_proxy {
226 my $proxy = http_proxy
227 or return;
78 228
79 glBindTexture GL_TEXTURE_2D, $self->{name}; 229 $ENV{http_proxy} = $proxy;
230}
80 231
81 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 232sub lwp_useragent {
82 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 233 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 234
86 glTexImage2D GL_TEXTURE_2D, 0, 235 DC::set_proxy;
87 GL_RGBA8,
88 $width, $height,
89 0,
90 GL_RGBA,
91 GL_UNSIGNED_BYTE,
92 $data;
93}
94 236
95sub DESTROY { 237 my $ua = LWP::UserAgent->new (
238 agent => "deliantra $VERSION",
239 keep_alive => 1,
240 env_proxy => 1,
241 timeout => 30,
242 );
243}
244
245sub lwp_check($) {
96 my ($self) = @_; 246 my ($res) = @_;
97 247
98 return unless exists $self->{name}; 248 $res->is_error
249 and die $res->status_line;
99 250
100 glDeleteTextures delete $self->{name}; 251 $res
101} 252}
102 253
103push @::GLINIT, sub { 254sub fh_nonblocking($$) {
104 $_->upload 255 my ($fh, $nb) = @_;
105 for grep $_, @textures; 256
257 if ($^O eq "MSWin32") {
258 $nb = (! ! $nb) + 0;
259 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
260 } else {
261 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
262 }
263}
264
265package DC::Layout;
266
267$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
268 glyph_cache_restore;
269};
270
271$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
272 glyph_cache_backup;
106}; 273};
107 274
1081; 2751;
109 276
110=back 277=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines