ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.201
Committed: Sun Jan 11 03:19:47 2009 UTC (15 years, 4 months ago) by root
Branch: MAIN
Changes since 1.200: +7 -2 lines
Log Message:
*** empty log message ***

File Contents

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