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.212 by root, Wed Jan 4 11:23:23 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 = '2.11';
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_from_file { 49=cut
33 my ($class, $path) = @_;
34 50
35 open my $fh, "<:raw", $path 51sub shorten($;$) {
36 or die "$path: $!"; 52 my ($str, $len) = @_;
53 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
54 $str
55}
56
57sub asxml($) {
58 local $_ = $_[0];
59
60 s/&/&amp;/g;
61 s/>/&gt;/g;
62 s/</&lt;/g;
63
64 $_
65}
66
67sub background(&;&) {
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;
37 163
38 local $/; 164 local $/;
39 $class->new_from_data (<$fh>) 165 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
40} 166}
41 167
42sub new_from_image { 168sub set_theme($) {
43 my ($class, $image) = @_; 169 return if $RC_THEME eq $_[0];
170 $RC_THEME = $_[0];
44 171
45 my $self = bless { 172 # kind of hacky, find the main theme file, then load all theme files and merge them
46 image => $data, 173
174 %THEME = ();
175 @RC_PATH = "theme-$RC_THEME";
176
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 );
47 }; 187 }
48
49 push @textures, $self;
50 Scalar::Util::weaken $textures[-1];
51
52 $self->upload;
53
54 $self
55} 188}
56 189
57sub upload { 190sub read_cfg {
58 my ($self) = @_; 191 my ($file) = @_;
59 192
60 return unless $SDL::App::USING_OPENGL; 193 $::CFG = (load_json $file) || (load_json "$file.bak");
194}
61 195
62 my ($data, $width, $height); 196sub write_cfg {
197 my $file = "$Deliantra::VARDIR/client.cf";
63 198
64 if (exists $self->{data}) { 199 $::CFG->{VERSION} = $::VERSION;
65 ($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}
66 } else { 218 } else {
67 my $pb = new Gtk2::Gdk::PixbufLoader; 219 ()
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 } 220 }
221}
76 222
77 ($self->{name}) = @{glGenTextures 1}; 223sub set_proxy {
224 my $proxy = http_proxy
225 or return;
78 226
79 glBindTexture GL_TEXTURE_2D, $self->{name}; 227 $ENV{http_proxy} = $proxy;
228}
80 229
81 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 230sub lwp_useragent {
82 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 231 require LWP::UserAgent;
83 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
84 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
85 232
86 glTexImage2D GL_TEXTURE_2D, 0, 233 DC::set_proxy;
87 GL_RGBA8,
88 $width, $height,
89 0,
90 GL_RGBA,
91 GL_UNSIGNED_BYTE,
92 $data;
93}
94 234
95sub 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($) {
96 my ($self) = @_; 244 my ($res) = @_;
97 245
98 return unless exists $self->{name}; 246 $res->is_error
247 and die $res->status_line;
99 248
100 glDeleteTextures delete $self->{name}; 249 $res
101} 250}
102 251
103push @::GLINIT, sub { 252sub fh_nonblocking($$) {
104 $_->upload 253 my ($fh, $nb) = @_;
105 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;
106}; 271};
107 272
1081; 2731;
109 274
110=back 275=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines