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.4 by root, Fri Apr 7 18:49:52 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 { 44=cut
33 my ($class, %data) = @_;
34 45
35 my $self = bless \%data, $class; 46sub guard(&) {
36 47 bless \(my $cb = $_[0]), "DC::Guard"
37 push @textures, $self;
38 Scalar::Util::weaken $textures[-1];
39
40 $self->upload;
41
42 $self
43} 48}
44 49
45sub new_from_image { 50sub DC::Guard::DESTROY {
46 my ($class, $image) = @_; 51 ${$_[0]}->()
47
48 $class->_new (image => $image)
49} 52}
50 53
51sub new_from_file { 54=item shorten $string[, $maxlength]
52 my ($class, $path) = @_;
53 55
54 open my $fh, "<:raw", $path 56=cut
55 or die "$path: $!"; 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;
56 170
57 local $/; 171 local $/;
58 $class->new_from_image (<$fh>) 172 JSON::XS->new->utf8->relaxed->decode (<$fh>)
59} 173}
60 174
61sub new_from_surface { 175sub set_theme($) {
62 my ($class, $surface) = @_; 176 return if $RC_THEME eq $_[0];
177 $RC_THEME = $_[0];
63 178
64 $surface->rgba; 179 # kind of hacky, find the main theme file, then load all theme files and merge them
65 180
66 $class->_new ( 181 %THEME = ();
67 data => $surface->pixels, 182 @RC_PATH = "theme-$RC_THEME";
68 width => $surface->width, 183
69 height => $surface->height, 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 );
70 ) 194 }
71} 195}
72 196
73sub upload { 197sub read_cfg {
74 my ($self) = @_; 198 my ($file) = @_;
75 199
76 return unless $SDL::App::USING_OPENGL; 200 $::CFG = load_json $file;
201}
77 202
78 my ($data, $width, $height); 203sub write_cfg {
204 my $file = "$Deliantra::VARDIR/client.cf";
79 205
80 if (exists $self->{data}) { 206 $::CFG->{VERSION} = $::VERSION;
81 ($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}
82 } else { 220 } else {
83 my $pb = new Gtk2::Gdk::PixbufLoader; 221 ()
84 $pb->write ($self->{image});
85 $pb->close;
86
87 $pb = $pb->get_pixbuf;
88 $pb = $pb->add_alpha (0, 0, 0, 0);
89
90 ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height);
91 } 222 }
223}
92 224
93 ($self->{name}) = @{glGenTextures 1}; 225sub set_proxy {
226 my $proxy = http_proxy
227 or return;
94 228
95 glBindTexture GL_TEXTURE_2D, $self->{name}; 229 $ENV{http_proxy} = $proxy;
230}
96 231
97 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 232sub lwp_useragent {
98 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 233 require LWP::UserAgent;
99 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
100 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
101 234
102 glTexImage2D GL_TEXTURE_2D, 0, 235 DC::set_proxy;
103 GL_RGBA8,
104 $width, $height,
105 0,
106 GL_RGBA,
107 GL_UNSIGNED_BYTE,
108 $data;
109}
110 236
111sub 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($) {
112 my ($self) = @_; 246 my ($res) = @_;
113 247
114 return unless exists $self->{name}; 248 $res->is_error
249 and die $res->status_line;
115 250
116 glDeleteTextures delete $self->{name}; 251 $res
117} 252}
118 253
119push @::GLINIT, sub { 254sub fh_nonblocking($$) {
120 $_->upload 255 my ($fh, $nb) = @_;
121 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;
122}; 273};
123 274
1241; 2751;
125 276
126=back 277=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines