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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines