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.137 by root, Sat Dec 9 21:26:45 2006 UTC vs.
Revision 1.185 by root, Fri Aug 1 13:50:08 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.97'; 22 $VERSION = '0.9974';
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 BerkeleyDB;
30use Pod::POM (); 31use Pod::POM ();
31use File::Path (); 32use File::Path ();
32use Storable (); # finally 33use Storable (); # finally
33 34use Fcntl ();
34BEGIN { 35use JSON::XS qw(encode_json decode_json);
35 use Crossfire::Protocol::Base ();
36 *to_json = \&Crossfire::Protocol::Base::to_json;
37 *from_json = \&Crossfire::Protocol::Base::from_json;
38}
39 36
40=item guard { BLOCK } 37=item guard { BLOCK }
41 38
42Returns 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.
43 40
44=cut 41=cut
45 42
46sub guard(&) { 43sub guard(&) {
47 bless \(my $cb = $_[0]), "CFPlus::Guard" 44 bless \(my $cb = $_[0]), "DC::Guard"
48} 45}
49 46
50sub CFPlus::Guard::DESTROY { 47sub DC::Guard::DESTROY {
51 ${$_[0]}->() 48 ${$_[0]}->()
52} 49}
53 50
54=item shorten $string[, $maxlength] 51=item shorten $string[, $maxlength]
55 52
71 $_ 68 $_
72} 69}
73 70
74sub socketpipe() { 71sub socketpipe() {
75 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC 72 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
76 or die "cannot establish bidiretcional pipe: $!\n"; 73 or die "cannot establish bidirectional pipe: $!\n";
77 74
78 ($fh1, $fh2) 75 ($fh1, $fh2)
79} 76}
80 77
81sub background(&;&) { 78sub background(&;&) {
82 my ($bg, $cb) = @_; 79 my ($bg, $cb) = @_;
83 80
84 my ($fh_r, $fh_w) = CFPlus::socketpipe; 81 my ($fh_r, $fh_w) = DC::socketpipe;
85 82
86 my $pid = fork; 83 my $pid = fork;
87 84
88 if (defined $pid && !$pid) { 85 if (defined $pid && !$pid) {
89 local $SIG{__DIE__}; 86 local $SIG{__DIE__};
99 96
100 if ($@) { 97 if ($@) {
101 my $msg = $@; 98 my $msg = $@;
102 $msg =~ s/\n+/\n/; 99 $msg =~ s/\n+/\n/;
103 warn "FATAL: $msg"; 100 warn "FATAL: $msg";
104 CFPlus::_exit 1; 101 DC::_exit 1;
105 } 102 }
106 103
107 # win32 is fucked up, of course. exit will clean stuff up, 104 # win32 is fucked up, of course. exit will clean stuff up,
108 # which destroys our database etc. _exit will exit ALL 105 # which destroys our database etc. _exit will exit ALL
109 # forked processes, because of the dreaded fork emulation. 106 # forked processes, because of the dreaded fork emulation.
110 CFPlus::_exit 0; 107 DC::_exit 0;
111 } 108 }
112 109
113 close $fh_w; 110 close $fh_w;
114 111
115 my $buffer; 112 my $buffer;
124 while ($buffer =~ s/^(.*)\n//) { 121 while ($buffer =~ s/^(.*)\n//) {
125 my $line = $1; 122 my $line = $1;
126 $line =~ s/\s+$//; 123 $line =~ s/\s+$//;
127 utf8::decode $line; 124 utf8::decode $line;
128 if ($line =~ /^\x{e877}json_msg (.*)$/s) { 125 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
129 $cb->(from_json $1); 126 $cb->(JSON::XS->new->allow_nonref->decode ($1));
130 } else { 127 } else {
131 ::message ({ 128 ::message ({
132 markup => "background($pid): " . CFPlus::asxml $line, 129 markup => "background($pid): " . DC::asxml $line,
133 }); 130 });
134 } 131 }
135 } 132 }
136 }); 133 });
137} 134}
138 135
139sub background_msg { 136sub background_msg {
140 my ($msg) = @_; 137 my ($msg) = @_;
141 138
142 $msg = "\x{e877}json_msg " . to_json $msg; 139 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
143 $msg =~ s/\n//g; 140 $msg =~ s/\n//g;
144 utf8::encode $msg; 141 utf8::encode $msg;
145 print $msg, "\n"; 142 print $msg, "\n";
146} 143}
147 144
148package CFPlus::Database;
149
150our @ISA = BerkeleyDB::Btree::;
151
152sub get($$) {
153 my $data;
154
155 $_[0]->db_get ($_[1], $data) == 0
156 ? $data
157 : ()
158}
159
160my %DB_SYNC;
161
162sub put($$$) {
163 my ($db, $key, $data) = @_;
164
165 my $hkey = $db + 0;
166 CFPlus::weaken $db;
167 $DB_SYNC{$hkey} ||= AnyEvent->timer (after => 5, cb => sub {
168 delete $DB_SYNC{$hkey};
169 $db->db_sync if $db;
170 });
171
172 $db->db_put ($key => $data)
173}
174
175package CFPlus; 145package DC;
176 146
177sub find_rcfile($) { 147sub find_rcfile($) {
178 my $path; 148 my $path;
179 149
180 for (grep !ref, @INC) { 150 for (grep !ref, @INC) {
181 $path = "$_/CFPlus/resources/$_[0]"; 151 $path = "$_/Deliantra/Client/private/resources/$_[0]";
182 return $path if -r $path; 152 return $path if -r $path;
183 } 153 }
184 154
185 die "FATAL: can't find required file $_[0]\n"; 155 die "FATAL: can't find required file $_[0]\n";
186} 156}
192 or return; 162 or return;
193 163
194 local $/; 164 local $/;
195 my $CFG = <$fh>; 165 my $CFG = <$fh>;
196 166
197 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove
198 require YAML;
199 utf8::decode $CFG;
200 $::CFG = YAML::Load ($CFG);
201 } elsif ($CFG =~ /^\{/) {
202 $::CFG = from_json $CFG; 167 $::CFG = decode_json $CFG;
203 } else {
204 $::CFG = eval $CFG; ## todo comaptibility cruft
205 }
206} 168}
207 169
208sub write_cfg { 170sub write_cfg {
209 my ($file) = @_; 171 my $file = "$Deliantra::VARDIR/client.cf";
210 172
211 $::CFG->{VERSION} = $::VERSION; 173 $::CFG->{VERSION} = $::VERSION;
212 174
213 open my $fh, ">:utf8", $file 175 open my $fh, ">:utf8", $file
214 or return; 176 or return;
215 print $fh to_json $::CFG; 177 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
216} 178}
217 179
218sub http_proxy { 180sub http_proxy {
219 my @proxy = win32_proxy_info; 181 my @proxy = win32_proxy_info;
220 182
235} 197}
236 198
237sub lwp_useragent { 199sub lwp_useragent {
238 require LWP::UserAgent; 200 require LWP::UserAgent;
239 201
240 CFPlus::set_proxy; 202 DC::set_proxy;
241 203
242 my $ua = LWP::UserAgent->new ( 204 my $ua = LWP::UserAgent->new (
243 agent => "cfplus $VERSION", 205 agent => "deliantra $VERSION",
244 keep_alive => 1, 206 keep_alive => 1,
245 env_proxy => 1, 207 env_proxy => 1,
246 timeout => 30, 208 timeout => 30,
247 ); 209 );
248} 210}
254 and die $res->status_line; 216 and die $res->status_line;
255 217
256 $res 218 $res
257} 219}
258 220
259our $DB_ENV; 221sub fh_nonblocking($$) {
260our $DB_STATE;
261
262sub db_table($) {
263 my ($table) = @_; 222 my ($fh, $nb) = @_;
264 223
265 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 224 if ($^O eq "MSWin32") {
266 225 $nb = (! ! $nb) + 0;
267 new CFPlus::Database 226 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
268 -Env => $DB_ENV, 227 } else {
269 -Filename => $table, 228 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
270# -Filename => "database",
271# -Subname => $table,
272 -Property => DB_CHKSUM,
273 -Flags => DB_CREATE | DB_UPGRADE,
274 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
275}
276
277our $DB_HOME = "$Crossfire::VARDIR/cfplus";
278
279sub open_db {
280 use strict;
281
282 mkdir $DB_HOME, 0777;
283 my $recover = $BerkeleyDB::db_version >= 4.4
284 ? eval "DB_REGISTER | DB_RECOVER"
285 : 0;
286
287 $DB_ENV = new BerkeleyDB::Env
288 -Home => $DB_HOME,
289 -Cachesize => 1_000_000,
290 -ErrFile => "$DB_HOME/errorlog.txt",
291# -ErrPrefix => "DATABASE",
292 -Verbose => 1,
293 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
294 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
295 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
296
297 $DB_STATE = db_table "state";
298
299 1 229 }
300} 230}
301 231
302unless (eval { open_db }) {
303 File::Path::rmtree $DB_HOME;
304 open_db;
305}
306
307package CFPlus::Layout; 232package DC::Layout;
308 233
234$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
235 glyph_cache_restore;
236};
237
309$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { 238$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
310 reset_glyph_cache; 239 glyph_cache_backup;
311}; 240};
312 241
3131; 2421;
314 243
315=back 244=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines