ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.207
Committed: Tue Sep 15 18:49:55 2009 UTC (14 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-2_05
Changes since 1.206: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.169 DC - undocumented utility garbage for our deliantra client
4 root 1.1
5     =head1 SYNOPSIS
6    
7 root 1.169 use DC;
8 root 1.1
9     =head1 DESCRIPTION
10    
11     =over 4
12    
13     =cut
14    
15 root 1.169 package DC;
16 root 1.1
17 root 1.121 use Carp ();
18    
19 root 1.187 our $VERSION;
20    
21 root 1.1 BEGIN {
22 root 1.207 $VERSION = '2.05';
23 root 1.1
24 root 1.2 use XSLoader;
25 root 1.168 XSLoader::load "Deliantra::Client", $VERSION;
26 root 1.1 }
27    
28 root 1.62 use utf8;
29 root 1.187 use strict qw(vars subs);
30 root 1.62
31 root 1.200 use Socket ();
32 root 1.52 use AnyEvent ();
33 root 1.200 use AnyEvent::Util ();
34 root 1.89 use Pod::POM ();
35 root 1.135 use File::Path ();
36 root 1.89 use Storable (); # finally
37 root 1.141 use Fcntl ();
38 root 1.159 use JSON::XS qw(encode_json decode_json);
39 root 1.202 use Guard qw(guard);
40 root 1.103
41 root 1.133 =item shorten $string[, $maxlength]
42    
43     =cut
44    
45     sub shorten($;$) {
46     my ($str, $len) = @_;
47     substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
48     $str
49     }
50    
51 root 1.105 sub asxml($) {
52     local $_ = $_[0];
53 root 1.89
54 root 1.105 s/&/&amp;/g;
55     s/>/&gt;/g;
56     s/</&lt;/g;
57 root 1.89
58 root 1.105 $_
59 root 1.89 }
60    
61 root 1.127 sub background(&;&) {
62     my ($bg, $cb) = @_;
63 root 1.123
64 root 1.200 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
65     or die "unable to create background socketpair: $!";
66 root 1.123
67     my $pid = fork;
68    
69     if (defined $pid && !$pid) {
70 root 1.124 local $SIG{__DIE__};
71 root 1.123
72     open STDOUT, ">&", $fh_w;
73     open STDERR, ">&", $fh_w;
74     close $fh_r;
75     close $fh_w;
76    
77     $| = 1;
78    
79 root 1.127 eval { $bg->() };
80 root 1.124
81     if ($@) {
82     my $msg = $@;
83     $msg =~ s/\n+/\n/;
84     warn "FATAL: $msg";
85 root 1.169 DC::_exit 1;
86 root 1.124 }
87 root 1.123
88     # win32 is fucked up, of course. exit will clean stuff up,
89     # which destroys our database etc. _exit will exit ALL
90     # forked processes, because of the dreaded fork emulation.
91 root 1.169 DC::_exit 0;
92 root 1.123 }
93    
94     close $fh_w;
95    
96     my $buffer;
97    
98 root 1.126 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
99 root 1.123 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
100 root 1.126 undef $w;
101 root 1.127 $cb->();
102     return;
103 root 1.123 }
104    
105     while ($buffer =~ s/^(.*)\n//) {
106     my $line = $1;
107 root 1.124 $line =~ s/\s+$//;
108 root 1.123 utf8::decode $line;
109 root 1.127 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
110 root 1.139 $cb->(JSON::XS->new->allow_nonref->decode ($1));
111 root 1.127 } else {
112     ::message ({
113 root 1.169 markup => "background($pid): " . DC::asxml $line,
114 root 1.127 });
115     }
116 root 1.123 }
117     });
118     }
119    
120 root 1.127 sub background_msg {
121     my ($msg) = @_;
122    
123 root 1.139 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
124 root 1.127 $msg =~ s/\n//g;
125     utf8::encode $msg;
126     print $msg, "\n";
127     }
128    
129 root 1.169 package DC;
130 root 1.52
131 root 1.191 our $RC_THEME;
132 root 1.192 our %THEME;
133 root 1.191 our @RC_PATH;
134 root 1.187 our $RC_BASE;
135    
136     for (grep !ref, @INC) {
137     $RC_BASE = "$_/Deliantra/Client/private/resources";
138     last if -d $RC_BASE;
139     }
140    
141 root 1.5 sub find_rcfile($) {
142     my $path;
143    
144 root 1.191 for (@RC_PATH, "") {
145 root 1.189 $path = "$RC_BASE/$_/$_[0]";
146 root 1.205 return $path if -e $path;
147 root 1.189 }
148 root 1.5
149 root 1.187 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
150 root 1.5 }
151    
152 root 1.192 sub load_json($) {
153     my ($file) = @_;
154    
155     open my $fh, $file
156     or return;
157    
158     local $/;
159 root 1.203 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
160 root 1.192 }
161    
162 root 1.191 sub set_theme($) {
163     return if $RC_THEME eq $_[0];
164     $RC_THEME = $_[0];
165    
166 root 1.192 # kind of hacky, find the main theme file, then load all theme files and merge them
167    
168     %THEME = ();
169 root 1.191 @RC_PATH = "theme-$RC_THEME";
170    
171 root 1.192 my $theme = load_json find_rcfile "theme.json"
172     or die "FATAL: theme resource file not found";
173 root 1.191
174 root 1.192 @RC_PATH = @{ $theme->{path} } if $theme->{path};
175    
176     for (@RC_PATH, "") {
177     my $theme = load_json "$RC_BASE/$_/theme.json"
178     or next;
179    
180     %THEME = ( %$theme, %THEME );
181 root 1.191 }
182     }
183    
184 root 1.5 sub read_cfg {
185     my ($file) = @_;
186    
187 root 1.201 $::CFG = (load_json $file) || (load_json "$file.bak");
188 root 1.5 }
189    
190     sub write_cfg {
191 root 1.178 my $file = "$Deliantra::VARDIR/client.cf";
192 root 1.5
193 root 1.107 $::CFG->{VERSION} = $::VERSION;
194 root 1.201 $::CFG->{layout} = DC::UI::get_layout ();
195 root 1.107
196 root 1.201 open my $fh, ">:utf8", "$file~"
197 root 1.5 or return;
198 root 1.180 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
199 root 1.201 close $fh;
200    
201     rename $file, "$file.bak";
202     rename "$file~", $file;
203 root 1.5 }
204    
205 root 1.122 sub http_proxy {
206     my @proxy = win32_proxy_info;
207    
208     if (@proxy) {
209     "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
210     } elsif (exists $ENV{http_proxy}) {
211     $ENV{http_proxy}
212     } else {
213     ()
214     }
215     }
216    
217     sub set_proxy {
218     my $proxy = http_proxy
219     or return;
220    
221     $ENV{http_proxy} = $proxy;
222     }
223    
224 root 1.127 sub lwp_useragent {
225     require LWP::UserAgent;
226    
227 root 1.169 DC::set_proxy;
228 root 1.127
229     my $ua = LWP::UserAgent->new (
230 root 1.171 agent => "deliantra $VERSION",
231 root 1.127 keep_alive => 1,
232     env_proxy => 1,
233     timeout => 30,
234     );
235     }
236    
237     sub lwp_check($) {
238     my ($res) = @_;
239    
240     $res->is_error
241     and die $res->status_line;
242    
243     $res
244     }
245    
246 root 1.141 sub fh_nonblocking($$) {
247     my ($fh, $nb) = @_;
248    
249 root 1.142 if ($^O eq "MSWin32") {
250     $nb = (! ! $nb) + 0;
251 root 1.143 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
252 root 1.141 } else {
253     fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
254     }
255     }
256    
257 root 1.169 package DC::Layout;
258 root 1.97
259 root 1.169 $DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
260 root 1.164 glyph_cache_restore;
261     };
262    
263 root 1.169 $DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
264 root 1.164 glyph_cache_backup;
265 root 1.97 };
266    
267 root 1.1 1;
268    
269     =back
270    
271     =head1 AUTHOR
272    
273     Marc Lehmann <schmorp@schmorp.de>
274     http://home.schmorp.de/
275    
276     =cut
277