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.101 by elmex, Sat Jul 22 12:18:01 2006 UTC vs.
Revision 1.122 by root, Sun Oct 1 14:48:50 2006 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFClient - undocumented utility garbage for our crossfire client 3CFPlus - undocumented utility garbage for our crossfire client
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use CFClient; 7 use CFPlus;
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 CFClient; 15package CFPlus;
16
17use Carp ();
16 18
17BEGIN { 19BEGIN {
18 $VERSION = '0.1'; 20 $VERSION = '0.52';
19 21
20 use XSLoader; 22 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 23 XSLoader::load "CFPlus", $VERSION;
24}
25
26BEGIN {
27 $SIG{__DIE__} = sub {
28 return if CFPlus::in_destruct;
29 #CFPlus::fatal $_[0];#d#
30 CFPlus::error Carp::longmess $_[0];#d#
31 die;#d#
32 };
22} 33}
23 34
24use utf8; 35use utf8;
25 36
26use Carp ();
27use AnyEvent (); 37use AnyEvent ();
28use BerkeleyDB; 38use BerkeleyDB;
29use Pod::POM (); 39use Pod::POM ();
30use Scalar::Util (); 40use Scalar::Util ();
31use Storable (); # finally 41use Storable (); # finally
32 42
33package CFClient::PodToPango; 43=item guard { BLOCK }
34 44
35use base Pod::POM::View::Text; 45Returns an object that executes the given block as soon as it is destroyed.
36 46
37our $VERSION = 1; # bump if resultant formatting changes 47=cut
38 48
39our $indent = 0; 49sub guard(&) {
40 50 bless \(my $cb = $_[0]), "CFPlus::Guard"
41*view_seq_code =
42*view_seq_bold = sub { "<b>$_[1]</b>" };
43*view_seq_italic = sub { "<i>$_[1]</i>" };
44*view_seq_space =
45*view_seq_link =
46*view_seq_index = sub { CFClient::UI::Label::escape ($_[1]) };
47
48sub view_seq_text {
49 my $text = $_[1];
50 $text =~ s/\s+/ /g;
51 CFClient::UI::Label::escape ($text)
52} 51}
53 52
54sub view_item { 53sub CFPlus::Guard::DESTROY {
55 ("\t" x ($indent / 4)) 54 ${$_[0]}->()
56 . $_[1]->title->present ($_[0])
57 . "\n\n"
58 . $_[1]->content->present ($_[0])
59} 55}
60 56
61sub view_verbatim { 57sub asxml($) {
62 (join "", 58 local $_ = $_[0];
63 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n",
64 split /\n/, CFClient::UI::Label::escape ($_[1]))
65 . "\n"
66}
67 59
68sub view_textblock { 60 s/&/&amp;/g;
69 ("\t" x ($indent / 2)) . "$_[1]\n\n" 61 s/>/&gt;/g;
70} 62 s/</&lt;/g;
71 63
72sub view_head1 { 64 $_
73 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
74 . $_[1]->content->present ($_[0])
75};
76
77sub view_head2 {
78 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
79 . $_[1]->content->present ($_[0])
80};
81
82sub view_head3 {
83 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
84 . $_[1]->content->present ($_[0])
85};
86
87sub view_over {
88 local $indent = $indent + $_[1]->indent;
89 $_[1]->content->present ($_[0])
90} 65}
91 66
92package CFClient::Database; 67package CFPlus::Database;
93 68
94our @ISA = BerkeleyDB::Btree::; 69our @ISA = BerkeleyDB::Btree::;
95 70
96sub get($$) { 71sub get($$) {
97 my $data; 72 my $data;
104my %DB_SYNC; 79my %DB_SYNC;
105 80
106sub put($$$) { 81sub put($$$) {
107 my ($db, $key, $data) = @_; 82 my ($db, $key, $data) = @_;
108 83
84 my $hkey = $db + 0;
85 Scalar::Util::weaken $db;
109 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync }); 86 $DB_SYNC{$hkey} ||= AnyEvent->timer (after => 5, cb => sub {
87 delete $DB_SYNC{$hkey};
88 $db->db_sync if $db;
89 });
110 90
111 $db->db_put ($key => $data) 91 $db->db_put ($key => $data)
112} 92}
113 93
114package CFClient; 94package CFPlus;
115 95
116sub find_rcfile($) { 96sub find_rcfile($) {
117 my $path; 97 my $path;
118 98
119 for (grep !ref, @INC) { 99 for (grep !ref, @INC) {
120 $path = "$_/CFClient/resources/$_[0]"; 100 $path = "$_/CFPlus/resources/$_[0]";
121 return $path if -r $path; 101 return $path if -r $path;
122 } 102 }
123 103
124 die "FATAL: can't find required file $_[0]\n"; 104 die "FATAL: can't find required file $_[0]\n";
125} 105}
126 106
107BEGIN {
108 use Crossfire::Protocol::Base ();
109 *to_json = \&Crossfire::Protocol::Base::to_json;
110 *from_json = \&Crossfire::Protocol::Base::from_json;
111}
112
127sub read_cfg { 113sub read_cfg {
128 my ($file) = @_; 114 my ($file) = @_;
129 115
130 open CFG, $file 116 open my $fh, $file
131 or return; 117 or return;
132 118
133 my $CFG;
134
135 local $/; 119 local $/;
136 $CFG = eval <CFG>; 120 my $CFG = <$fh>;
137 121
138 $::CFG = $CFG; 122 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove
139 123 require YAML;
140 close CFG; 124 utf8::decode $CFG;
125 $::CFG = YAML::Load ($CFG);
126 } elsif ($CFG =~ /^\{/) {
127 $::CFG = from_json $CFG;
128 } else {
129 $::CFG = eval $CFG; ## todo comaptibility cruft
130 }
141} 131}
142 132
143sub write_cfg { 133sub write_cfg {
144 my ($file) = @_; 134 my ($file) = @_;
145 135
146 open CFG, ">$file" 136 $::CFG->{VERSION} = $::VERSION;
137
138 open my $fh, ">:utf8", $file
147 or return; 139 or return;
140 print $fh to_json $::CFG;
141}
148 142
149 { 143sub http_proxy {
150 require Data::Dumper; 144 my @proxy = win32_proxy_info;
151 local $Data::Dumper::Purity = 1; 145
152 $::CFG->{VERSION} = $::VERSION; 146 if (@proxy) {
153 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); 147 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
148 } elsif (exists $ENV{http_proxy}) {
149 $ENV{http_proxy}
150 } else {
151 ()
154 } 152 }
153}
155 154
156 close CFG; 155sub set_proxy {
156 my $proxy = http_proxy
157 or return;
158
159 $ENV{http_proxy} = $proxy;
157} 160}
158 161
159our $DB_ENV; 162our $DB_ENV;
163our $DB_STATE;
164
165sub db_table($) {
166 my ($table) = @_;
167
168 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
169
170 new CFPlus::Database
171 -Env => $DB_ENV,
172 -Filename => $table,
173# -Filename => "database",
174# -Subname => $table,
175 -Property => DB_CHKSUM,
176 -Flags => DB_CREATE | DB_UPGRADE,
177 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
178}
160 179
161{ 180{
162 use strict; 181 use strict;
163 182
164 mkdir "$Crossfire::VARDIR/cfplus", 0777; 183 mkdir "$Crossfire::VARDIR/cfplus", 0777;
173# -ErrPrefix => "DATABASE", 192# -ErrPrefix => "DATABASE",
174 -Verbose => 1, 193 -Verbose => 1,
175 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, 194 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
176 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, 195 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
177 or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error"; 196 or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error";
178}
179 197
180sub db_table($) { 198 $DB_STATE = db_table "state";
181 my ($table) = @_;
182
183 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
184
185 new CFClient::Database
186 -Env => $DB_ENV,
187 -Filename => $table,
188# -Filename => "database",
189# -Subname => $table,
190 -Property => DB_CHKSUM,
191 -Flags => DB_CREATE | DB_UPGRADE,
192 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
193} 199}
194 200
195my $pod_cache = db_table "pod_cache";
196
197sub load_pod($$$$) {
198 my ($path, $filtertype, $filterversion, $filtercb) = @_;
199
200 stat $path
201 or die "$path: $!";
202
203 my $phash = join ",", $filterversion, $CFClient::PodToPango::VERSION, (stat _)[7,9];
204
205 my ($chash, $pom) = eval { @{ Storable::thaw $pod_cache->get ("$path/$filtertype") } };
206
207 return $pom if $chash eq $phash;
208
209 my $pod = do {
210 local $/;
211 open my $pod, "<:utf8", $_[0]
212 or die "$_[0]: $!";
213 <$pod>
214 };
215
216 #utf8::downgrade $pod;
217
218 $pom = $filtercb-> (Pod::POM->new->parse_text ($pod));
219
220 $pod_cache->put ("$path/$filtertype" => Storable::nfreeze [$phash, $pom]);
221
222 $pom
223}
224
225sub pod_to_pango($) {
226 my ($pom) = @_;
227
228 $pom->present ("CFClient::PodToPango")
229}
230
231sub pod_to_pango_list($) {
232 my ($pom) = @_;
233
234 [
235 map s/^(\s*)// && [40 * length $1, length $_ ? $_ : " "],
236 split /\n/, $pom->present ("CFClient::PodToPango")
237 ]
238}
239
240package CFClient::Layout; 201package CFPlus::Layout;
241 202
242$CFClient::OpenGL::SHUTDOWN_HOOK{"CFClient::Layout"} = sub { 203$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub {
243 reset_glyph_cache; 204 reset_glyph_cache;
244}; 205};
245 206
246package CFClient::Item; 207package CFPlus::Item;
247 208
248use strict; 209use strict;
249use Crossfire::Protocol::Constants; 210use Crossfire::Protocol::Constants;
250 211
251my $last_enter_count = 1; 212my $last_enter_count = 1;
285} 246}
286 247
287sub do_n_dialog { 248sub do_n_dialog {
288 my ($cb) = @_; 249 my ($cb) = @_;
289 250
290 my $w = new CFClient::UI::FancyFrame 251 my $w = new CFPlus::UI::Toplevel
291 on_delete => sub { $_[0]->destroy; 1 }, 252 on_delete => sub { $_[0]->destroy; 1 },
292 has_close_button => 1, 253 has_close_button => 1,
293 ; 254 ;
294 255
295 $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center"); 256 $w->add (my $vb = new CFPlus::UI::VBox x => "center", y => "center");
296 $vb->add (new CFClient::UI::Label text => "Enter item count:"); 257 $vb->add (new CFPlus::UI::Label text => "Enter item count:");
297 $vb->add (my $entry = new CFClient::UI::Entry 258 $vb->add (my $entry = new CFPlus::UI::Entry
298 text => $last_enter_count, 259 text => $last_enter_count,
299 on_activate => sub { 260 on_activate => sub {
300 my ($entry) = @_; 261 my ($entry) = @_;
301 $last_enter_count = $entry->get_text; 262 $last_enter_count = $entry->get_text;
302 $cb->($last_enter_count); 263 $cb->($last_enter_count);
324 285
325 if ($self->{container} == $::CONN->{player}{tag}) { 286 if ($self->{container} == $::CONN->{player}{tag}) {
326 $targ = $::CONN->{open_container}; 287 $targ = $::CONN->{open_container};
327 } 288 }
328 289
329 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { 290 if (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 1) {
330 $::CONN->send ("move $targ $self->{tag} 0") 291 $::CONN->send ("move $targ $self->{tag} 0")
331 if $targ || !($self->{flags} & F_LOCKED); 292 if $targ || !($self->{flags} & F_LOCKED);
332 } elsif (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 2) { 293 } elsif (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 2) {
333 $self->{flags} & F_LOCKED 294 $self->{flags} & F_LOCKED
334 ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) 295 ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag})
335 : $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) 296 : $::CONN->send ("lock " . pack "CN", 1, $self->{tag})
336 } elsif ($ev->{button} == 1) { 297 } elsif ($ev->{button} == 1) {
337 $::CONN->send ("examine $self->{tag}"); 298 $::CONN->send ("examine $self->{tag}");
350 sub { 311 sub {
351 $::CONN->send ("mark ". pack "N", $self->{tag}); 312 $::CONN->send ("mark ". pack "N", $self->{tag});
352 $::CONN->send ("command apply flint and steel"); 313 $::CONN->send ("command apply flint and steel");
353 } 314 }
354 ], 315 ],
316 ["inscribe", # first try of an easier use of flint&steel
317 sub {
318 &::open_string_query ("Text to inscribe", sub {
319 my ($entry, $txt) = @_;
320 $::CONN->send ("mark ". pack "N", $self->{tag});
321 $::CONN->send ("command use_skill inscription $txt");
322 });
323 }
324 ],
325 ["rename", # first try of an easier use of flint&steel
326 sub {
327 &::open_string_query ("Rename item to:", sub {
328 my ($entry, $txt) = @_;
329 $::CONN->send ("mark ". pack "N", $self->{tag});
330 $::CONN->send ("command rename to <$txt>");
331 }, $self->{name},
332 "If you input no name or erase the current custom name, the custom name will be unset");
333 }
334 ],
355 ["apply", sub { $::CONN->send ("apply $self->{tag}") }], 335 ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
356 ( 336 (
357 $self->{flags} & F_LOCKED 337 $self->{flags} & F_LOCKED
358 ? ( 338 ? (
359 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], 339 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
360 ) 340 )
361 : ( 341 : (
362 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], 342 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
363 ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }], 343 ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }],
364 ["$move_prefix n", 344 ["$move_prefix &lt;n&gt;",
365 sub { 345 sub {
366 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") }) 346 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") })
367 } 347 }
368 ] 348 ]
369 ) 349 )
370 ), 350 ),
371 ); 351 );
372 352
373 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 353 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
374 } 354 }
375 355
376 1 356 1
377 }; 357 };
378 358
382 . "Middle click - apply\n" 362 . "Middle click - apply\n"
383 . "Shift-Middle click - lock/unlock\n" 363 . "Shift-Middle click - lock/unlock\n"
384 . "Right click - further options" 364 . "Right click - further options"
385 . "</small>\n"; 365 . "</small>\n";
386 366
367 my $bg = $self->{flags} & F_CURSED ? [1 , 0 , 0, 0.5]
368 : $self->{flags} & F_MAGIC ? [0.2, 0.2, 1, 0.5]
369 : undef;
370
387 $self->{face_widget} ||= new CFClient::UI::Face 371 $self->{face_widget} ||= new CFPlus::UI::Face
388 can_events => 1, 372 can_events => 1,
389 can_hover => 1, 373 can_hover => 1,
390 anim => $self->{anim}, 374 anim => $self->{anim},
391 animspeed => $self->{animspeed}, # TODO# must be set at creation time 375 animspeed => $self->{animspeed}, # TODO# must be set at creation time
392 on_button_down => $button_cb, 376 on_button_down => $button_cb,
393 ; 377 ;
378 $self->{face_widget}{bg} = $bg;
394 $self->{face_widget}{face} = $self->{face}; 379 $self->{face_widget}{face} = $self->{face};
395 $self->{face_widget}{anim} = $self->{anim}; 380 $self->{face_widget}{anim} = $self->{anim};
396 $self->{face_widget}{animspeed} = $self->{animspeed}; 381 $self->{face_widget}{animspeed} = $self->{animspeed};
397 $self->{face_widget}->set_tooltip ( 382 $self->{face_widget}->set_tooltip (
398 "<b>Face/Animation.</b>\n" 383 "<b>Face/Animation.</b>\n"
399 . "Item uses face #$self->{face}. " 384 . "Item uses face #$self->{face}. "
400 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ") 385 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ")
401 . "\n\n$tooltip_std" 386 . "\n\n$tooltip_std"
402 ); 387 );
403 388
404 $self->{desc_widget} ||= new CFClient::UI::Label 389 $self->{desc_widget} ||= new CFPlus::UI::Label
405 can_events => 1, 390 can_events => 1,
406 can_hover => 1, 391 can_hover => 1,
407 ellipsise => 2, 392 ellipsise => 2,
408 align => -1, 393 align => -1,
409 on_button_down => $button_cb, 394 on_button_down => $button_cb,
410 ; 395 ;
411 my $desc = CFClient::Item::desc_string $self; 396 my $desc = CFPlus::Item::desc_string $self;
397 $self->{desc_widget}{bg} = $bg;
412 $self->{desc_widget}->set_text ($desc); 398 $self->{desc_widget}->set_text ($desc);
413 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std"); 399 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std");
414 400
415 $self->{weight_widget} ||= new CFClient::UI::Label 401 $self->{weight_widget} ||= new CFPlus::UI::Label
416 can_events => 1, 402 can_events => 1,
417 can_hover => 1, 403 can_hover => 1,
418 ellipsise => 0, 404 ellipsise => 0,
419 align => 0, 405 align => 0,
420 on_button_down => $button_cb, 406 on_button_down => $button_cb,
421 ; 407 ;
408 $self->{weight_widget}{bg} = $bg;
422 $self->{weight_widget}->set_text (CFClient::Item::weight_string $self); 409 $self->{weight_widget}->set_text (CFPlus::Item::weight_string $self);
423
424 $self->{weight_widget}->set_tooltip ( 410 $self->{weight_widget}->set_tooltip (
425 "<b>Weight</b>.\n" 411 "<b>Weight</b>.\n"
426 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ") 412 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ")
427 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") 413 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
428 . "\n\n$tooltip_std" 414 . "\n\n$tooltip_std"

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines