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.144 by root, Wed May 9 11:46:49 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.98';
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 {
35 use Crossfire::Protocol::Base ();
36 *to_json = \&Crossfire::Protocol::Base::to_json;
37 *from_json = \&Crossfire::Protocol::Base::from_json;
38}
29 39
30my @textures; 40=item guard { BLOCK }
31 41
32sub new_from_file { 42Returns an object that executes the given block as soon as it is destroyed.
33 my ($class, $path) = @_;
34 43
35 open my $fh, "<:raw", $path 44=cut
36 or die "$path: $!"; 45
46sub guard(&) {
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;
37 166
38 local $/; 167 local $/;
39 $class->new_from_data (<$fh>) 168 my $CFG = <$fh>;
40}
41 169
42sub new_from_image { 170 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove
43 my ($class, $image) = @_; 171 require YAML;
44 172 utf8::decode $CFG;
45 my $self = bless { 173 $::CFG = YAML::Load ($CFG);
46 image => $data, 174 } elsif ($CFG =~ /^\{/) {
47 }; 175 $::CFG = from_json $CFG;
48
49 push @textures, $self;
50 Scalar::Util::weaken $textures[-1];
51
52 $self->upload;
53
54 $self
55}
56
57sub upload {
58 my ($self) = @_;
59
60 return unless $SDL::App::USING_OPENGL;
61
62 my ($data, $width, $height);
63
64 if (exists $self->{data}) {
65 ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height});
66 } else { 176 } else {
67 my $pb = new Gtk2::Gdk::PixbufLoader; 177 $::CFG = eval $CFG; ## todo comaptibility cruft
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 } 178 }
179}
76 180
77 ($self->{name}) = @{glGenTextures 1}; 181sub write_cfg {
182 my ($file) = @_;
78 183
79 glBindTexture GL_TEXTURE_2D, $self->{name}; 184 $::CFG->{VERSION} = $::VERSION;
80 185
81 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 186 open my $fh, ">:utf8", $file
82 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 187 or return;
83 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 188 print $fh to_json $::CFG;
84 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;
85 212
86 glTexImage2D GL_TEXTURE_2D, 0, 213 CFPlus::set_proxy;
87 GL_RGBA8,
88 $width, $height,
89 0,
90 GL_RGBA,
91 GL_UNSIGNED_BYTE,
92 $data;
93}
94 214
95sub 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($) {
96 my ($self) = @_; 224 my ($res) = @_;
97 225
98 return unless exists $self->{name}; 226 $res->is_error
227 and die $res->status_line;
99 228
100 glDeleteTextures delete $self->{name}; 229 $res
101} 230}
102 231
103push @::GLINIT, sub { 232sub fh_nonblocking($$) {
104 $_->upload 233 my ($fh, $nb) = @_;
105 for grep $_, @textures; 234
235 if ($^O eq "MSWin32") {
236 $nb = (! ! $nb) + 0;
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;
106}; 248};
107 249
1081; 2501;
109 251
110=back 252=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines