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.156 by root, Sat Oct 20 21:50:59 2007 UTC vs.
Revision 1.186 by root, Tue Aug 19 17:38:29 2008 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFPlus - 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 CFPlus; 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 Deliantra::Client; # work around CPAN breakage
16package App::Deliantra; # try to reserve namespace
15package CFPlus; 17package DC;
16 18
17use Carp (); 19use Carp ();
18 20
19BEGIN { 21BEGIN {
20 $VERSION = '0.995'; 22 $VERSION = '0.9975';
21 23
22 use XSLoader; 24 use XSLoader;
23 XSLoader::load "CFPlus", $VERSION; 25 XSLoader::load "Deliantra::Client", $VERSION;
24} 26}
25 27
26use utf8; 28use utf8;
27 29
28use AnyEvent (); 30use AnyEvent ();
29use Pod::POM (); 31use Pod::POM ();
30use File::Path (); 32use File::Path ();
31use Storable (); # finally 33use Storable (); # finally
32use Fcntl (); 34use Fcntl ();
33use JSON::XS qw(to_json from_json); 35use JSON::XS qw(encode_json decode_json);
34 36
35=item guard { BLOCK } 37=item guard { BLOCK }
36 38
37Returns an object that executes the given block as soon as it is destroyed. 39Returns an object that executes the given block as soon as it is destroyed.
38 40
39=cut 41=cut
40 42
41sub guard(&) { 43sub guard(&) {
42 bless \(my $cb = $_[0]), "CFPlus::Guard" 44 bless \(my $cb = $_[0]), "DC::Guard"
43} 45}
44 46
45sub CFPlus::Guard::DESTROY { 47sub DC::Guard::DESTROY {
46 ${$_[0]}->() 48 ${$_[0]}->()
47} 49}
48 50
49=item shorten $string[, $maxlength] 51=item shorten $string[, $maxlength]
50 52
74} 76}
75 77
76sub background(&;&) { 78sub background(&;&) {
77 my ($bg, $cb) = @_; 79 my ($bg, $cb) = @_;
78 80
79 my ($fh_r, $fh_w) = CFPlus::socketpipe; 81 my ($fh_r, $fh_w) = DC::socketpipe;
80 82
81 my $pid = fork; 83 my $pid = fork;
82 84
83 if (defined $pid && !$pid) { 85 if (defined $pid && !$pid) {
84 local $SIG{__DIE__}; 86 local $SIG{__DIE__};
94 96
95 if ($@) { 97 if ($@) {
96 my $msg = $@; 98 my $msg = $@;
97 $msg =~ s/\n+/\n/; 99 $msg =~ s/\n+/\n/;
98 warn "FATAL: $msg"; 100 warn "FATAL: $msg";
99 CFPlus::_exit 1; 101 DC::_exit 1;
100 } 102 }
101 103
102 # win32 is fucked up, of course. exit will clean stuff up, 104 # win32 is fucked up, of course. exit will clean stuff up,
103 # which destroys our database etc. _exit will exit ALL 105 # which destroys our database etc. _exit will exit ALL
104 # forked processes, because of the dreaded fork emulation. 106 # forked processes, because of the dreaded fork emulation.
105 CFPlus::_exit 0; 107 DC::_exit 0;
106 } 108 }
107 109
108 close $fh_w; 110 close $fh_w;
109 111
110 my $buffer; 112 my $buffer;
122 utf8::decode $line; 124 utf8::decode $line;
123 if ($line =~ /^\x{e877}json_msg (.*)$/s) { 125 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
124 $cb->(JSON::XS->new->allow_nonref->decode ($1)); 126 $cb->(JSON::XS->new->allow_nonref->decode ($1));
125 } else { 127 } else {
126 ::message ({ 128 ::message ({
127 markup => "background($pid): " . CFPlus::asxml $line, 129 markup => "background($pid): " . DC::asxml $line,
128 }); 130 });
129 } 131 }
130 } 132 }
131 }); 133 });
132} 134}
138 $msg =~ s/\n//g; 140 $msg =~ s/\n//g;
139 utf8::encode $msg; 141 utf8::encode $msg;
140 print $msg, "\n"; 142 print $msg, "\n";
141} 143}
142 144
143package CFPlus; 145package DC;
144 146
145sub find_rcfile($) { 147sub find_rcfile($) {
146 my $path; 148 my $path;
147 149
148 for (grep !ref, @INC) { 150 for (grep !ref, @INC) {
149 $path = "$_/CFPlus/resources/$_[0]"; 151 $path = "$_/Deliantra/Client/private/resources/$_[0]";
150 return $path if -r $path; 152 return $path if -r $path;
151 } 153 }
152 154
153 die "FATAL: can't find required file $_[0]\n"; 155 die "FATAL: can't find required file $_[0]\n";
154} 156}
160 or return; 162 or return;
161 163
162 local $/; 164 local $/;
163 my $CFG = <$fh>; 165 my $CFG = <$fh>;
164 166
165 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove
166 require YAML;
167 utf8::decode $CFG;
168 $::CFG = YAML::Load ($CFG);
169 } elsif ($CFG =~ /^\{/) {
170 $::CFG = from_json $CFG; 167 $::CFG = decode_json $CFG;
171 } else {
172 $::CFG = eval $CFG; ## todo comaptibility cruft
173 }
174} 168}
175 169
176sub write_cfg { 170sub write_cfg {
177 my ($file) = @_; 171 my $file = "$Deliantra::VARDIR/client.cf";
178 172
179 $::CFG->{VERSION} = $::VERSION; 173 $::CFG->{VERSION} = $::VERSION;
180 174
181 open my $fh, ">:utf8", $file 175 open my $fh, ">:utf8", $file
182 or return; 176 or return;
183 print $fh to_json $::CFG; 177 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
184} 178}
185 179
186sub http_proxy { 180sub http_proxy {
187 my @proxy = win32_proxy_info; 181 my @proxy = win32_proxy_info;
188 182
203} 197}
204 198
205sub lwp_useragent { 199sub lwp_useragent {
206 require LWP::UserAgent; 200 require LWP::UserAgent;
207 201
208 CFPlus::set_proxy; 202 DC::set_proxy;
209 203
210 my $ua = LWP::UserAgent->new ( 204 my $ua = LWP::UserAgent->new (
211 agent => "cfplus $VERSION", 205 agent => "deliantra $VERSION",
212 keep_alive => 1, 206 keep_alive => 1,
213 env_proxy => 1, 207 env_proxy => 1,
214 timeout => 30, 208 timeout => 30,
215 ); 209 );
216} 210}
231 $nb = (! ! $nb) + 0; 225 $nb = (! ! $nb) + 0;
232 ioctl $fh, 0x8004667e, \$nb; # FIONBIO 226 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
233 } else { 227 } else {
234 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; 228 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
235 } 229 }
236
237} 230}
238 231
239package CFPlus::Layout; 232package DC::Layout;
240 233
234$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
235 glyph_cache_restore;
236};
237
241$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { 238$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
242 reset_glyph_cache; 239 glyph_cache_backup;
243}; 240};
244 241
2451; 2421;
246 243
247=back 244=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines