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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines