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.194 by root, Wed Sep 24 02:39:47 2008 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 = '0.9976';
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 AnyEvent ();
32use Pod::POM ();
33use File::Path ();
34use Storable (); # finally
35use Fcntl ();
36use JSON::XS qw(encode_json decode_json);
27 37
28use SDL::OpenGL; 38=item guard { BLOCK }
29 39
30my @textures; 40Returns an object that executes the given block as soon as it is destroyed.
31 41
32sub _new { 42=cut
33 my ($class, %data) = @_;
34 43
35 my $self = bless \%data, $class; 44sub guard(&) {
36 45 bless \(my $cb = $_[0]), "DC::Guard"
37 push @textures, $self;
38 Scalar::Util::weaken $textures[-1];
39
40 $self->upload;
41
42 $self
43} 46}
44 47
45sub new_from_image { 48sub DC::Guard::DESTROY {
46 my ($class, $image) = @_; 49 ${$_[0]}->()
47
48 $class->_new (image => $image)
49} 50}
50 51
51sub new_from_file { 52=item shorten $string[, $maxlength]
52 my ($class, $path) = @_;
53 53
54 open my $fh, "<:raw", $path 54=cut
55 or die "$path: $!"; 55
56sub shorten($;$) {
57 my ($str, $len) = @_;
58 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
59 $str
60}
61
62sub asxml($) {
63 local $_ = $_[0];
64
65 s/&/&amp;/g;
66 s/>/&gt;/g;
67 s/</&lt;/g;
68
69 $_
70}
71
72sub 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
79sub 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
137sub 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
146package DC;
147
148our $RC_THEME;
149our %THEME;
150our @RC_PATH;
151our $RC_BASE;
152
153for (grep !ref, @INC) {
154 $RC_BASE = "$_/Deliantra/Client/private/resources";
155 last if -d $RC_BASE;
156}
157
158sub find_rcfile($) {
159 my $path;
160
161 for (@RC_PATH, "") {
162 $path = "$RC_BASE/$_/$_[0]";
163 return $path if -r $path;
164 }
165
166 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
167}
168
169sub load_json($) {
170 my ($file) = @_;
171
172 open my $fh, $file
173 or return;
56 174
57 local $/; 175 local $/;
58 $class->new_from_image (<$fh>) 176 JSON::XS->new->utf8->relaxed->decode (<$fh>)
59} 177}
60 178
61sub new_from_surface { 179sub set_theme($) {
62 my ($class, $surface) = @_; 180 return if $RC_THEME eq $_[0];
181 $RC_THEME = $_[0];
63 182
64 $surface->rgba; 183 # kind of hacky, find the main theme file, then load all theme files and merge them
65 184
66 $class->_new ( 185 %THEME = ();
67 data => $surface->pixels, 186 @RC_PATH = "theme-$RC_THEME";
68 width => $surface->width, 187
69 height => $surface->height, 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 );
70 ) 198 }
71} 199}
72 200
73sub upload { 201sub read_cfg {
74 my ($self) = @_; 202 my ($file) = @_;
75 203
76 return unless $SDL::App::USING_OPENGL; 204 $::CFG = load_json $file;
205}
77 206
78 my ($data, $width, $height); 207sub write_cfg {
208 my $file = "$Deliantra::VARDIR/client.cf";
79 209
80 if (exists $self->{data}) { 210 $::CFG->{VERSION} = $::VERSION;
81 ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); 211
212 open my $fh, ">:utf8", $file
213 or return;
214 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
215}
216
217sub http_proxy {
218 my @proxy = win32_proxy_info;
219
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}
82 } else { 224 } else {
83 my $pb = new Gtk2::Gdk::PixbufLoader; 225 ()
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 } 226 }
227}
92 228
93 ($self->{name}) = @{glGenTextures 1}; 229sub set_proxy {
230 my $proxy = http_proxy
231 or return;
94 232
95 glBindTexture GL_TEXTURE_2D, $self->{name}; 233 $ENV{http_proxy} = $proxy;
234}
96 235
97 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 236sub lwp_useragent {
98 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 237 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 238
102 glTexImage2D GL_TEXTURE_2D, 0, 239 DC::set_proxy;
103 GL_RGBA8,
104 $width, $height,
105 0,
106 GL_RGBA,
107 GL_UNSIGNED_BYTE,
108 $data;
109}
110 240
111sub DESTROY { 241 my $ua = LWP::UserAgent->new (
242 agent => "deliantra $VERSION",
243 keep_alive => 1,
244 env_proxy => 1,
245 timeout => 30,
246 );
247}
248
249sub lwp_check($) {
112 my ($self) = @_; 250 my ($res) = @_;
113 251
114 return unless exists $self->{name}; 252 $res->is_error
253 and die $res->status_line;
115 254
116 glDeleteTextures delete $self->{name}; 255 $res
117} 256}
118 257
119push @::GLINIT, sub { 258sub fh_nonblocking($$) {
120 $_->upload 259 my ($fh, $nb) = @_;
121 for grep $_, @textures; 260
261 if ($^O eq "MSWin32") {
262 $nb = (! ! $nb) + 0;
263 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
264 } else {
265 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
266 }
267}
268
269package DC::Layout;
270
271$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
272 glyph_cache_restore;
273};
274
275$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
276 glyph_cache_backup;
122}; 277};
123 278
1241; 2791;
125 280
126=back 281=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines