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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines