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.141 by root, Fri Apr 6 21:53:56 2007 UTC

1=head1 NAME 1=head1 NAME
2 2
3Crossfire::Client - undocumented utility garbage for our crossfire client 3CFPlus - undocumented utility garbage for our crossfire client
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Crossfire::Client; 7 use CFPlus;
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 CFPlus;
16
17use Carp ();
16 18
17BEGIN { 19BEGIN {
18 $VERSION = '0.1'; 20 $VERSION = '0.97';
19 21
20 use XSLoader; 22 use XSLoader;
21 XSLoader::load "Crossfire::Client", $VERSION; 23 XSLoader::load "CFPlus", $VERSION;
22} 24}
23 25
24package Crossfire::Client::Texture; 26use utf8;
25 27
26use Scalar::Util; 28use AnyEvent ();
29use Pod::POM ();
30use File::Path ();
31use Storable (); # finally
32use Fcntl ();
27 33
28use SDL::OpenGL; 34BEGIN {
29 35 use Crossfire::Protocol::Base ();
30my @textures; 36 *to_json = \&Crossfire::Protocol::Base::to_json;
31 37 *from_json = \&Crossfire::Protocol::Base::from_json;
32sub _new {
33 my ($class, %data) = @_;
34
35 my $self = bless \%data, $class;
36
37 push @textures, $self;
38 Scalar::Util::weaken $textures[-1];
39
40 $self->upload;
41
42 $self
43} 38}
44 39
45sub new_from_image { 40=item guard { BLOCK }
46 my ($class, $image) = @_;
47 41
48 $class->_new (image => $image) 42Returns an object that executes the given block as soon as it is destroyed.
49}
50 43
51sub new_from_file { 44=cut
52 my ($class, $path) = @_;
53 45
54 open my $fh, "<:raw", $path 46sub guard(&) {
55 or die "$path: $!"; 47 bless \(my $cb = $_[0]), "CFPlus::Guard"
48}
49
50sub CFPlus::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 socketpipe() {
75 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
76 or die "cannot establish bidirectional pipe: $!\n";
77
78 ($fh1, $fh2)
79}
80
81sub background(&;&) {
82 my ($bg, $cb) = @_;
83
84 my ($fh_r, $fh_w) = CFPlus::socketpipe;
85
86 my $pid = fork;
87
88 if (defined $pid && !$pid) {
89 local $SIG{__DIE__};
90
91 open STDOUT, ">&", $fh_w;
92 open STDERR, ">&", $fh_w;
93 close $fh_r;
94 close $fh_w;
95
96 $| = 1;
97
98 eval { $bg->() };
99
100 if ($@) {
101 my $msg = $@;
102 $msg =~ s/\n+/\n/;
103 warn "FATAL: $msg";
104 CFPlus::_exit 1;
105 }
106
107 # win32 is fucked up, of course. exit will clean stuff up,
108 # which destroys our database etc. _exit will exit ALL
109 # forked processes, because of the dreaded fork emulation.
110 CFPlus::_exit 0;
111 }
112
113 close $fh_w;
114
115 my $buffer;
116
117 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
118 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
119 undef $w;
120 $cb->();
121 return;
122 }
123
124 while ($buffer =~ s/^(.*)\n//) {
125 my $line = $1;
126 $line =~ s/\s+$//;
127 utf8::decode $line;
128 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
129 $cb->(JSON::XS->new->allow_nonref->decode ($1));
130 } else {
131 ::message ({
132 markup => "background($pid): " . CFPlus::asxml $line,
133 });
134 }
135 }
136 });
137}
138
139sub background_msg {
140 my ($msg) = @_;
141
142 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
143 $msg =~ s/\n//g;
144 utf8::encode $msg;
145 print $msg, "\n";
146}
147
148package CFPlus;
149
150sub find_rcfile($) {
151 my $path;
152
153 for (grep !ref, @INC) {
154 $path = "$_/CFPlus/resources/$_[0]";
155 return $path if -r $path;
156 }
157
158 die "FATAL: can't find required file $_[0]\n";
159}
160
161sub read_cfg {
162 my ($file) = @_;
163
164 open my $fh, $file
165 or return;
56 166
57 local $/; 167 local $/;
58 $class->new_from_image (<$fh>) 168 my $CFG = <$fh>;
59}
60 169
61sub new_from_surface { 170 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove
62 my ($class, $surface) = @_; 171 require YAML;
63 172 utf8::decode $CFG;
64 $surface->rgba; 173 $::CFG = YAML::Load ($CFG);
65 174 } elsif ($CFG =~ /^\{/) {
66 $class->_new ( 175 $::CFG = from_json $CFG;
67 data => $surface->pixels,
68 width => $surface->width,
69 height => $surface->height,
70 )
71}
72
73sub upload {
74 my ($self) = @_;
75
76 return unless $SDL::App::USING_OPENGL;
77
78 my ($data, $width, $height);
79
80 if (exists $self->{data}) {
81 ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height});
82 } else { 176 } else {
83 my $pb = new Gtk2::Gdk::PixbufLoader; 177 $::CFG = eval $CFG; ## todo comaptibility cruft
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 } 178 }
179}
92 180
93 ($self->{name}) = @{glGenTextures 1}; 181sub write_cfg {
182 my ($file) = @_;
94 183
95 glBindTexture GL_TEXTURE_2D, $self->{name}; 184 $::CFG->{VERSION} = $::VERSION;
96 185
97 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 186 open my $fh, ">:utf8", $file
98 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 187 or return;
99 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 188 print $fh to_json $::CFG;
100 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 189}
190
191sub http_proxy {
192 my @proxy = win32_proxy_info;
193
194 if (@proxy) {
195 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
196 } elsif (exists $ENV{http_proxy}) {
197 $ENV{http_proxy}
198 } else {
199 ()
200 }
201}
202
203sub set_proxy {
204 my $proxy = http_proxy
205 or return;
206
207 $ENV{http_proxy} = $proxy;
208}
209
210sub lwp_useragent {
211 require LWP::UserAgent;
101 212
102 glTexImage2D GL_TEXTURE_2D, 0, 213 CFPlus::set_proxy;
103 GL_RGBA8,
104 $width, $height,
105 0,
106 GL_RGBA,
107 GL_UNSIGNED_BYTE,
108 $data;
109}
110 214
111sub DESTROY { 215 my $ua = LWP::UserAgent->new (
216 agent => "cfplus $VERSION",
217 keep_alive => 1,
218 env_proxy => 1,
219 timeout => 30,
220 );
221}
222
223sub lwp_check($) {
112 my ($self) = @_; 224 my ($res) = @_;
113 225
114 return unless exists $self->{name}; 226 $res->is_error
227 and die $res->status_line;
115 228
116 glDeleteTextures delete $self->{name}; 229 $res
117} 230}
118 231
119push @::GLINIT, sub { 232sub fh_nonblocking($$) {
120 $_->upload 233 my ($fh, $nb) = @_;
121 for grep $_, @textures; 234
235 if ($^O =~ /Win32/) {
236 $nb = ! ! $nb;
237 ioctl $fh, 0x8004667e, $nb; # FIONBIO
238 } else {
239 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
240 }
241
242}
243
244package CFPlus::Layout;
245
246$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub {
247 reset_glyph_cache;
122}; 248};
123 249
1241; 2501;
125 251
126=back 252=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines