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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines