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.213 by root, Sat Jan 7 15:24:41 2012 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 = '3.0';
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 Socket ();
32use AnyEvent ();
33use AnyEvent::Util ();
34use Pod::POM ();
35use File::Path ();
36use Storable (); # finally
37use Fcntl ();
38use JSON::XS qw(encode_json decode_json);
39use Guard qw(guard);
27 40
28use SDL::OpenGL; 41# modules to support other DC::* packages
42use List::Util ();
43use IO::AIO ();
44use Coro::AIO ();
45use AnyEvent::AIO ();
29 46
30my @textures; 47=item shorten $string[, $maxlength]
31 48
32sub _new { 49=cut
33 my ($class, %data) = @_;
34 50
35 my $self = bless \%data, $class; 51sub shorten($;$) {
36 52 my ($str, $len) = @_;
37 push @textures, $self; 53 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
38 Scalar::Util::weaken $textures[-1]; 54 $str
39
40 $self->upload;
41
42 $self
43} 55}
44 56
45sub new_from_image { 57sub asxml($) {
46 my ($class, $image) = @_; 58 local $_ = $_[0];
47 59
48 $class->_new (image => $image) 60 s/&/&amp;/g;
49} 61 s/>/&gt;/g;
62 s/</&lt;/g;
50 63
51sub new_from_file { 64 $_
52 my ($class, $path) = @_; 65}
53 66
54 open my $fh, "<:raw", $path 67sub background(&;&) {
55 or die "$path: $!"; 68 my ($bg, $cb) = @_;
69
70 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
71 or die "unable to create background socketpair: $!";
72
73 my $pid = fork;
74
75 if (defined $pid && !$pid) {
76 local $SIG{__DIE__};
77
78 open STDOUT, ">&", $fh_w;
79 open STDERR, ">&", $fh_w;
80 close $fh_r;
81 close $fh_w;
82
83 $| = 1;
84
85 eval { $bg->() };
86
87 if ($@) {
88 my $msg = $@;
89 $msg =~ s/\n+/\n/;
90 warn "FATAL: $msg";
91 DC::_exit 1;
92 }
93
94 # win32 is fucked up, of course. exit will clean stuff up,
95 # which destroys our database etc. _exit will exit ALL
96 # forked processes, because of the dreaded fork emulation.
97 DC::_exit 0;
98 }
99
100 close $fh_w;
101
102 my $buffer;
103
104 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
105 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
106 undef $w;
107 $cb->();
108 return;
109 }
110
111 while ($buffer =~ s/^(.*)\n//) {
112 my $line = $1;
113 $line =~ s/\s+$//;
114 utf8::decode $line;
115 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
116 $cb->(JSON::XS->new->allow_nonref->decode ($1));
117 } else {
118 ::message ({
119 markup => "background($pid): " . DC::asxml $line,
120 });
121 }
122 }
123 });
124}
125
126sub background_msg {
127 my ($msg) = @_;
128
129 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
130 $msg =~ s/\n//g;
131 utf8::encode $msg;
132 print $msg, "\n";
133}
134
135package DC;
136
137our $RC_THEME;
138our %THEME;
139our @RC_PATH;
140our $RC_BASE;
141
142for (grep !ref, @INC) {
143 $RC_BASE = "$_/Deliantra/Client/private/resources";
144 last if -d $RC_BASE;
145}
146
147sub find_rcfile($) {
148 my $path;
149
150 for (@RC_PATH, "") {
151 $path = "$RC_BASE/$_/$_[0]";
152 return $path if -e $path;
153 }
154
155 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
156}
157
158sub load_json($) {
159 my ($file) = @_;
160
161 open my $fh, $file
162 or return;
56 163
57 local $/; 164 local $/;
58 $class->new_from_image (<$fh>) 165 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
59} 166}
60 167
61sub new_from_surface { 168sub set_theme($) {
62 my ($class, $surface) = @_; 169 return if $RC_THEME eq $_[0];
170 $RC_THEME = $_[0];
63 171
64 $surface->rgba; 172 # kind of hacky, find the main theme file, then load all theme files and merge them
65 173
66 $class->_new ( 174 %THEME = ();
67 data => $surface->pixels, 175 @RC_PATH = "theme-$RC_THEME";
68 width => $surface->width, 176
69 height => $surface->height, 177 my $theme = load_json find_rcfile "theme.json"
178 or die "FATAL: theme resource file not found";
179
180 @RC_PATH = @{ $theme->{path} } if $theme->{path};
181
182 for (@RC_PATH, "") {
183 my $theme = load_json "$RC_BASE/$_/theme.json"
184 or next;
185
186 %THEME = ( %$theme, %THEME );
70 ) 187 }
71} 188}
72 189
73sub upload { 190sub read_cfg {
74 my ($self) = @_; 191 my ($file) = @_;
75 192
76 return unless $SDL::App::USING_OPENGL; 193 $::CFG = (load_json $file) || (load_json "$file.bak");
194}
77 195
78 my ($data, $width, $height); 196sub write_cfg {
197 my $file = "$Deliantra::VARDIR/client.cf";
79 198
80 if (exists $self->{data}) { 199 $::CFG->{VERSION} = $::VERSION;
81 ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); 200 $::CFG->{layout} = DC::UI::get_layout ();
201
202 open my $fh, ">:utf8", "$file~"
203 or return;
204 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
205 close $fh;
206
207 rename $file, "$file.bak";
208 rename "$file~", $file;
209}
210
211sub http_proxy {
212 my @proxy = win32_proxy_info;
213
214 if (@proxy) {
215 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
216 } elsif (exists $ENV{http_proxy}) {
217 $ENV{http_proxy}
82 } else { 218 } else {
83 my $pb = new Gtk2::Gdk::PixbufLoader; 219 ()
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 } 220 }
221}
92 222
93 ($self->{name}) = @{glGenTextures 1}; 223sub set_proxy {
224 my $proxy = http_proxy
225 or return;
94 226
95 glBindTexture GL_TEXTURE_2D, $self->{name}; 227 $ENV{http_proxy} = $proxy;
228}
96 229
97 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 230sub lwp_useragent {
98 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 231 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 232
102 glTexImage2D GL_TEXTURE_2D, 0, 233 DC::set_proxy;
103 GL_RGBA8,
104 $width, $height,
105 0,
106 GL_RGBA,
107 GL_UNSIGNED_BYTE,
108 $data;
109}
110 234
111sub DESTROY { 235 my $ua = LWP::UserAgent->new (
236 agent => "deliantra $VERSION",
237 keep_alive => 1,
238 env_proxy => 1,
239 timeout => 30,
240 );
241}
242
243sub lwp_check($) {
112 my ($self) = @_; 244 my ($res) = @_;
113 245
114 return unless exists $self->{name}; 246 $res->is_error
247 and die $res->status_line;
115 248
116 glDeleteTextures delete $self->{name}; 249 $res
117} 250}
118 251
119push @::GLINIT, sub { 252sub fh_nonblocking($$) {
120 $_->upload 253 my ($fh, $nb) = @_;
121 for grep $_, @textures; 254
255 if ($^O eq "MSWin32") {
256 $nb = (! ! $nb) + 0;
257 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
258 } else {
259 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
260 }
261}
262
263package DC::Layout;
264
265$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
266 glyph_cache_restore;
267};
268
269$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
270 glyph_cache_backup;
122}; 271};
123 272
1241; 2731;
125 274
126=back 275=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines