ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.212
Committed: Wed Jan 4 11:23:23 2012 UTC (12 years, 4 months ago) by root
Branch: MAIN
Changes since 1.211: +6 -0 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.11';
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 use Guard qw(guard);
40
41 # modules to support other DC::* packages
42 use List::Util ();
43 use IO::AIO ();
44 use Coro::AIO ();
45 use AnyEvent::AIO ();
46
47 =item shorten $string[, $maxlength]
48
49 =cut
50
51 sub shorten($;$) {
52 my ($str, $len) = @_;
53 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
54 $str
55 }
56
57 sub asxml($) {
58 local $_ = $_[0];
59
60 s/&/&amp;/g;
61 s/>/&gt;/g;
62 s/</&lt;/g;
63
64 $_
65 }
66
67 sub 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
126 sub 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
135 package DC;
136
137 our $RC_THEME;
138 our %THEME;
139 our @RC_PATH;
140 our $RC_BASE;
141
142 for (grep !ref, @INC) {
143 $RC_BASE = "$_/Deliantra/Client/private/resources";
144 last if -d $RC_BASE;
145 }
146
147 sub 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
158 sub load_json($) {
159 my ($file) = @_;
160
161 open my $fh, $file
162 or return;
163
164 local $/;
165 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
166 }
167
168 sub set_theme($) {
169 return if $RC_THEME eq $_[0];
170 $RC_THEME = $_[0];
171
172 # kind of hacky, find the main theme file, then load all theme files and merge them
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 );
187 }
188 }
189
190 sub read_cfg {
191 my ($file) = @_;
192
193 $::CFG = (load_json $file) || (load_json "$file.bak");
194 }
195
196 sub write_cfg {
197 my $file = "$Deliantra::VARDIR/client.cf";
198
199 $::CFG->{VERSION} = $::VERSION;
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
211 sub 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}
218 } else {
219 ()
220 }
221 }
222
223 sub set_proxy {
224 my $proxy = http_proxy
225 or return;
226
227 $ENV{http_proxy} = $proxy;
228 }
229
230 sub lwp_useragent {
231 require LWP::UserAgent;
232
233 DC::set_proxy;
234
235 my $ua = LWP::UserAgent->new (
236 agent => "deliantra $VERSION",
237 keep_alive => 1,
238 env_proxy => 1,
239 timeout => 30,
240 );
241 }
242
243 sub lwp_check($) {
244 my ($res) = @_;
245
246 $res->is_error
247 and die $res->status_line;
248
249 $res
250 }
251
252 sub fh_nonblocking($$) {
253 my ($fh, $nb) = @_;
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
263 package 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;
271 };
272
273 1;
274
275 =back
276
277 =head1 AUTHOR
278
279 Marc Lehmann <schmorp@schmorp.de>
280 http://home.schmorp.de/
281
282 =cut
283