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.139 by root, Sun Apr 1 15:08:14 2007 UTC vs.
Revision 1.215 by root, Wed Jan 18 13:42:19 2012 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
19our $VERSION;
20
19BEGIN { 21BEGIN {
20 $VERSION = '0.97'; 22 $VERSION = '3.0';
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;
29use strict qw(vars subs);
27 30
31use Socket ();
28use AnyEvent (); 32use AnyEvent ();
29use BerkeleyDB; 33use AnyEvent::Util ();
30use Pod::POM (); 34use Pod::POM ();
31use File::Path (); 35use File::Path ();
32use Storable (); # finally 36use Storable (); # finally
37use Fcntl ();
38use JSON::XS qw(encode_json decode_json);
39use Guard qw(guard);
33 40
34BEGIN { 41# modules to support other DC::* packages
35 use Crossfire::Protocol::Base (); 42use List::Util ();
36 *to_json = \&Crossfire::Protocol::Base::to_json; 43use IO::AIO ();
37 *from_json = \&Crossfire::Protocol::Base::from_json; 44use Coro::AIO ();
38} 45use AnyEvent::AIO ();
39 46
40=item guard { BLOCK } 47use Deliantra::Protocol::Constants ();
41
42Returns an object that executes the given block as soon as it is destroyed.
43
44=cut
45
46sub guard(&) {
47 bless \(my $cb = $_[0]), "CFPlus::Guard"
48}
49
50sub CFPlus::Guard::DESTROY {
51 ${$_[0]}->()
52}
53 48
54=item shorten $string[, $maxlength] 49=item shorten $string[, $maxlength]
55 50
56=cut 51=cut
57 52
69 s/</&lt;/g; 64 s/</&lt;/g;
70 65
71 $_ 66 $_
72} 67}
73 68
74sub socketpipe() {
75 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
76 or die "cannot establish bidiretcional pipe: $!\n";
77
78 ($fh1, $fh2)
79}
80
81sub background(&;&) { 69sub background(&;&) {
82 my ($bg, $cb) = @_; 70 my ($bg, $cb) = @_;
83 71
84 my ($fh_r, $fh_w) = CFPlus::socketpipe; 72 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
73 or die "unable to create background socketpair: $!";
85 74
86 my $pid = fork; 75 my $pid = fork;
87 76
88 if (defined $pid && !$pid) { 77 if (defined $pid && !$pid) {
89 local $SIG{__DIE__}; 78 local $SIG{__DIE__};
99 88
100 if ($@) { 89 if ($@) {
101 my $msg = $@; 90 my $msg = $@;
102 $msg =~ s/\n+/\n/; 91 $msg =~ s/\n+/\n/;
103 warn "FATAL: $msg"; 92 warn "FATAL: $msg";
104 CFPlus::_exit 1; 93 DC::_exit 1;
105 } 94 }
106 95
107 # win32 is fucked up, of course. exit will clean stuff up, 96 # win32 is fucked up, of course. exit will clean stuff up,
108 # which destroys our database etc. _exit will exit ALL 97 # which destroys our database etc. _exit will exit ALL
109 # forked processes, because of the dreaded fork emulation. 98 # forked processes, because of the dreaded fork emulation.
110 CFPlus::_exit 0; 99 DC::_exit 0;
111 } 100 }
112 101
113 close $fh_w; 102 close $fh_w;
114 103
115 my $buffer; 104 my $buffer;
127 utf8::decode $line; 116 utf8::decode $line;
128 if ($line =~ /^\x{e877}json_msg (.*)$/s) { 117 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
129 $cb->(JSON::XS->new->allow_nonref->decode ($1)); 118 $cb->(JSON::XS->new->allow_nonref->decode ($1));
130 } else { 119 } else {
131 ::message ({ 120 ::message ({
132 markup => "background($pid): " . CFPlus::asxml $line, 121 markup => "background($pid): " . DC::asxml $line,
133 }); 122 });
134 } 123 }
135 } 124 }
136 }); 125 });
137} 126}
143 $msg =~ s/\n//g; 132 $msg =~ s/\n//g;
144 utf8::encode $msg; 133 utf8::encode $msg;
145 print $msg, "\n"; 134 print $msg, "\n";
146} 135}
147 136
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 => 30, 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; 137package DC;
138
139our $RC_THEME;
140our %THEME;
141our @RC_PATH;
142our $RC_BASE;
143
144for (grep !ref, @INC) {
145 $RC_BASE = "$_/Deliantra/Client/private/resources";
146 last if -d $RC_BASE;
147}
176 148
177sub find_rcfile($) { 149sub find_rcfile($) {
178 my $path; 150 my $path;
179 151
180 for (grep !ref, @INC) { 152 for (@RC_PATH, "") {
181 $path = "$_/CFPlus/resources/$_[0]"; 153 $path = "$RC_BASE/$_/$_[0]";
182 return $path if -r $path; 154 return $path if -e $path;
183 } 155 }
184 156
185 die "FATAL: can't find required file $_[0]\n"; 157 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
186} 158}
187 159
188sub read_cfg { 160sub load_json($) {
189 my ($file) = @_; 161 my ($file) = @_;
190 162
191 open my $fh, $file 163 open my $fh, $file
192 or return; 164 or return;
193 165
194 local $/; 166 local $/;
195 my $CFG = <$fh>; 167 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
168}
196 169
197 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove 170sub set_theme($) {
198 require YAML; 171 return if $RC_THEME eq $_[0];
199 utf8::decode $CFG; 172 $RC_THEME = $_[0];
200 $::CFG = YAML::Load ($CFG); 173
201 } elsif ($CFG =~ /^\{/) { 174 # kind of hacky, find the main theme file, then load all theme files and merge them
202 $::CFG = from_json $CFG; 175
176 %THEME = ();
177 @RC_PATH = "theme-$RC_THEME";
178
179 my $theme = load_json find_rcfile "theme.json"
180 or die "FATAL: theme resource file not found";
181
182 @RC_PATH = @{ $theme->{path} } if $theme->{path};
183
184 for (@RC_PATH, "") {
185 my $theme = load_json "$RC_BASE/$_/theme.json"
186 or next;
187
188 %THEME = ( %$theme, %THEME );
189 }
190}
191
192sub read_cfg($) {
193 my ($file) = @_;
194
195 $::CFG = (load_json $file) || (load_json "$file.bak");
196}
197
198sub write_cfg($) {
199 my $file = "$Deliantra::VARDIR/client.cf";
200
201 $::CFG->{VERSION} = $::VERSION;
202 $::CFG->{layout} = DC::UI::get_layout ();
203
204 open my $fh, ">:utf8", "$file~"
205 or return;
206 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
207 close $fh;
208
209 rename $file, "$file.bak";
210 rename "$file~", $file;
211}
212
213sub load_cfg() {
214 if (-e "$Deliantra::VARDIR/client.cf") {
215 DC::read_cfg "$Deliantra::VARDIR/client.cf";
203 } else { 216 } else {
204 $::CFG = eval $CFG; ## todo comaptibility cruft 217 $::CFG = {};
205 } 218 }
206} 219}
207 220
208sub write_cfg { 221sub save_cfg() {
209 my ($file) = @_; 222 write_cfg "$Deliantra::VARDIR/client.cf";
223}
210 224
211 $::CFG->{VERSION} = $::VERSION; 225sub upgrade_cfg() {
226 my %DEF_CFG = (
227 config_autosave => 1,
228 sdl_mode => undef,
229 fullscreen => 1,
230 fast => 0,
231 force_opengl11 => undef,
232 disable_alpha => 0,
233 smooth_movement => 1,
234 smooth_transitions => 1,
235 texture_compression => 1,
236 map_scale => 1,
237 fow_enable => 1,
238 fow_intensity => 0,
239 fow_texture => 0,
240 map_smoothing => 1,
241 gui_fontsize => 1,
242 log_fontsize => 0.7,
243 gauge_fontsize => 1,
244 gauge_size => 0.35,
245 stat_fontsize => 0.7,
246 mapsize => 100,
247 audio_enable => 1,
248 audio_hw_channels => 0,
249 audio_hw_frequency => 0,
250 audio_hw_chunksize => 0,
251 audio_mix_channels => 8,
252 effects_enable => 1,
253 effects_volume => 1,
254 bgm_enable => 1,
255 bgm_volume => 0.5,
256 output_rate => "",
257 pickup => Deliantra::Protocol::Constants::PICKUP_SPELLBOOK
258 | Deliantra::Protocol::Constants::PICKUP_SKILLSCROLL
259 | Deliantra::Protocol::Constants::PICKUP_VALUABLES,
260 inv_sort => "mtime",
261 default => "profile", # default profile
262 show_tips => 1,
263 logview_max_par => 1000,
264 shift_fire_stop => 0,
265 uitheme => "wood",
266 map_shift_x => -24, # arbitrary
267 map_shift_y => +24, # arbitrary
268 #db_schema => 0,
269 );
212 270
213 open my $fh, ">:utf8", $file 271 while (my ($k, $v) = each %DEF_CFG) {
214 or return; 272 $::CFG->{$k} = $v unless exists $::CFG->{$k};
215 print $fh to_json $::CFG; 273 }
216} 274}
217 275
218sub http_proxy { 276sub http_proxy {
219 my @proxy = win32_proxy_info; 277 my @proxy = win32_proxy_info;
220 278
235} 293}
236 294
237sub lwp_useragent { 295sub lwp_useragent {
238 require LWP::UserAgent; 296 require LWP::UserAgent;
239 297
240 CFPlus::set_proxy; 298 DC::set_proxy;
241 299
242 my $ua = LWP::UserAgent->new ( 300 my $ua = LWP::UserAgent->new (
243 agent => "cfplus $VERSION", 301 agent => "deliantra $VERSION",
244 keep_alive => 1, 302 keep_alive => 1,
245 env_proxy => 1, 303 env_proxy => 1,
246 timeout => 30, 304 timeout => 30,
247 ); 305 );
248} 306}
254 and die $res->status_line; 312 and die $res->status_line;
255 313
256 $res 314 $res
257} 315}
258 316
259our $DB_ENV; 317sub fh_nonblocking($$) {
260our $DB_STATE;
261
262sub db_table($) {
263 my ($table) = @_; 318 my ($fh, $nb) = @_;
264 319
265 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 320 if ($^O eq "MSWin32") {
266 321 $nb = (! ! $nb) + 0;
267 new CFPlus::Database 322 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
268 -Env => $DB_ENV, 323 } else {
269 -Filename => $table, 324 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 => 8_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 325 }
300} 326}
301 327
302unless (eval { open_db }) {
303 File::Path::rmtree $DB_HOME;
304 open_db;
305}
306
307package CFPlus::Layout; 328package DC::Layout;
308 329
330$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
331 glyph_cache_restore;
332};
333
309$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { 334$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
310 reset_glyph_cache; 335 glyph_cache_backup;
311}; 336};
312 337
3131; 3381;
314 339
315=back 340=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines