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.96 by root, Sun Jul 2 18:52:04 2006 UTC vs.
Revision 1.121 by root, Fri Sep 29 00:56:05 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;
148 140 print $fh to_json $::CFG;
149 {
150 require Data::Dumper;
151 local $Data::Dumper::Purity = 1;
152 $::CFG->{VERSION} = $::VERSION;
153 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
154 }
155
156 close CFG;
157} 141}
158 142
159our $DB_ENV; 143our $DB_ENV;
144our $DB_STATE;
145
146sub db_table($) {
147 my ($table) = @_;
148
149 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
150
151 new CFPlus::Database
152 -Env => $DB_ENV,
153 -Filename => $table,
154# -Filename => "database",
155# -Subname => $table,
156 -Property => DB_CHKSUM,
157 -Flags => DB_CREATE | DB_UPGRADE,
158 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
159}
160 160
161{ 161{
162 use strict; 162 use strict;
163 163
164 mkdir "$Crossfire::VARDIR/cfplus", 0777; 164 mkdir "$Crossfire::VARDIR/cfplus", 0777;
173# -ErrPrefix => "DATABASE", 173# -ErrPrefix => "DATABASE",
174 -Verbose => 1, 174 -Verbose => 1,
175 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, 175 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
176 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, 176 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
177 or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error"; 177 or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error";
178}
179 178
180sub db_table($) { 179 $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} 180}
194 181
195my $pod_cache = db_table "pod_cache"; 182package CFPlus::Layout;
196 183
197sub load_pod($$$$) { 184$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub {
198 my ($path, $filtertype, $filterversion, $filtercb) = @_; 185 reset_glyph_cache;
186};
199 187
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::Item; 188package CFPlus::Item;
241 189
242use strict; 190use strict;
243use Crossfire::Protocol::Constants; 191use Crossfire::Protocol::Constants;
244 192
245my $last_enter_count = 1; 193my $last_enter_count = 1;
279} 227}
280 228
281sub do_n_dialog { 229sub do_n_dialog {
282 my ($cb) = @_; 230 my ($cb) = @_;
283 231
284 my $w = new CFClient::UI::FancyFrame; 232 my $w = new CFPlus::UI::Toplevel
233 on_delete => sub { $_[0]->destroy; 1 },
234 has_close_button => 1,
235 ;
236
285 $w->add (my $vb = new CFClient::UI::VBox x => "center", y => "center"); 237 $w->add (my $vb = new CFPlus::UI::VBox x => "center", y => "center");
286 $vb->add (new CFClient::UI::Label text => "Enter item count:"); 238 $vb->add (new CFPlus::UI::Label text => "Enter item count:");
287 $vb->add (my $entry = new CFClient::UI::Entry 239 $vb->add (my $entry = new CFPlus::UI::Entry
288 text => $last_enter_count, 240 text => $last_enter_count,
289 on_activate => sub { 241 on_activate => sub {
290 my ($entry) = @_; 242 my ($entry) = @_;
291 $last_enter_count = $entry->get_text; 243 $last_enter_count = $entry->get_text;
292 $cb->($last_enter_count); 244 $cb->($last_enter_count);
293 $w->hide; 245 $w->hide;
294 $w = undef; 246 $w->destroy;
247
248 0
295 } 249 },
250 on_escape => sub { $w->destroy; 1 },
296 ); 251 );
297 $entry->grab_focus; 252 $entry->grab_focus;
298 $w->show; 253 $w->show;
299
300} 254}
301 255
302sub update_widgets { 256sub update_widgets {
303 my ($self) = @_; 257 my ($self) = @_;
304 258
312 266
313 if ($self->{container} == $::CONN->{player}{tag}) { 267 if ($self->{container} == $::CONN->{player}{tag}) {
314 $targ = $::CONN->{open_container}; 268 $targ = $::CONN->{open_container};
315 } 269 }
316 270
317 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { 271 if (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 1) {
318 $::CONN->send ("move $targ $self->{tag} 0") 272 $::CONN->send ("move $targ $self->{tag} 0")
319 if $targ || !($self->{flags} & F_LOCKED); 273 if $targ || !($self->{flags} & F_LOCKED);
320 } elsif (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 2) { 274 } elsif (($ev->{mod} & CFPlus::KMOD_SHIFT) && $ev->{button} == 2) {
321 $self->{flags} & F_LOCKED 275 $self->{flags} & F_LOCKED
322 ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) 276 ? $::CONN->send ("lock " . pack "CN", 0, $self->{tag})
323 : $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) 277 : $::CONN->send ("lock " . pack "CN", 1, $self->{tag})
324 } elsif ($ev->{button} == 1) { 278 } elsif ($ev->{button} == 1) {
325 $::CONN->send ("examine $self->{tag}"); 279 $::CONN->send ("examine $self->{tag}");
326 } elsif ($ev->{button} == 2) { 280 } elsif ($ev->{button} == 2) {
327 $::CONN->send ("apply $self->{tag}"); 281 $::CONN->send ("apply $self->{tag}");
328 } elsif ($ev->{button} == 3) { 282 } elsif ($ev->{button} == 3) {
283 my $move_prefix = $::CONN->{open_container} ? 'put' : 'drop';
284 if ($self->{container} == $::CONN->{open_container}) {
285 $move_prefix = "take";
286 }
287
329 my @menu_items = ( 288 my @menu_items = (
330 ["examine", sub { $::CONN->send ("examine $self->{tag}") }], 289 ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
331 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }], 290 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }],
291 ["ignite/thaw", # first try of an easier use of flint&steel
292 sub {
293 $::CONN->send ("mark ". pack "N", $self->{tag});
294 $::CONN->send ("command apply flint and steel");
295 }
296 ],
297 ["inscribe", # first try of an easier use of flint&steel
298 sub {
299 &::open_string_query ("Text to inscribe", sub {
300 my ($entry, $txt) = @_;
301 $::CONN->send ("mark ". pack "N", $self->{tag});
302 $::CONN->send ("command use_skill inscription $txt");
303 });
304 }
305 ],
306 ["rename", # first try of an easier use of flint&steel
307 sub {
308 &::open_string_query ("Rename item to:", sub {
309 my ($entry, $txt) = @_;
310 $::CONN->send ("mark ". pack "N", $self->{tag});
311 $::CONN->send ("command rename to <$txt>");
312 }, $self->{name},
313 "If you input no name or erase the current custom name, the custom name will be unset");
314 }
315 ],
332 ["apply", sub { $::CONN->send ("apply $self->{tag}") }], 316 ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
333 ( 317 (
334 $self->{flags} & F_LOCKED 318 $self->{flags} & F_LOCKED
335 ? ( 319 ? (
336 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }], 320 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
337 ) 321 )
338 : ( 322 : (
339 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }], 323 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
340 ["drop", sub { $::CONN->send ("move $::CONN->{open_container} $self->{tag} 0") }], 324 ["$move_prefix all", sub { $::CONN->send ("move $targ $self->{tag} 0") }],
341 ["move n", 325 ["$move_prefix &lt;n&gt;",
342 sub { 326 sub {
343 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") }) 327 do_n_dialog (sub { $::CONN->send ("move $targ $self->{tag} $_[0]") })
344 } 328 }
345 ] 329 ]
346 ) 330 )
347 ), 331 ),
348 ); 332 );
349 333
350 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 334 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
351 } 335 }
352 336
353 1 337 1
354 }; 338 };
355 339
359 . "Middle click - apply\n" 343 . "Middle click - apply\n"
360 . "Shift-Middle click - lock/unlock\n" 344 . "Shift-Middle click - lock/unlock\n"
361 . "Right click - further options" 345 . "Right click - further options"
362 . "</small>\n"; 346 . "</small>\n";
363 347
348 my $bg = $self->{flags} & F_CURSED ? [1 , 0 , 0, 0.5]
349 : $self->{flags} & F_MAGIC ? [0.2, 0.2, 1, 0.5]
350 : undef;
351
364 $self->{face_widget} ||= new CFClient::UI::Face 352 $self->{face_widget} ||= new CFPlus::UI::Face
365 can_events => 1, 353 can_events => 1,
366 can_hover => 1, 354 can_hover => 1,
367 anim => $self->{anim}, 355 anim => $self->{anim},
368 animspeed => $self->{animspeed}, # TODO# must be set at creation time 356 animspeed => $self->{animspeed}, # TODO# must be set at creation time
369 on_button_down => $button_cb, 357 on_button_down => $button_cb,
370 ; 358 ;
359 $self->{face_widget}{bg} = $bg;
371 $self->{face_widget}{face} = $self->{face}; 360 $self->{face_widget}{face} = $self->{face};
372 $self->{face_widget}{anim} = $self->{anim}; 361 $self->{face_widget}{anim} = $self->{anim};
373 $self->{face_widget}{animspeed} = $self->{animspeed}; 362 $self->{face_widget}{animspeed} = $self->{animspeed};
374 $self->{face_widget}->set_tooltip ( 363 $self->{face_widget}->set_tooltip (
375 "<b>Face/Animation.</b>\n" 364 "<b>Face/Animation.</b>\n"
376 . "Item uses face #$self->{face}. " 365 . "Item uses face #$self->{face}. "
377 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ") 366 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ")
378 . "\n\n$tooltip_std" 367 . "\n\n$tooltip_std"
379 ); 368 );
380 369
381 $self->{desc_widget} ||= new CFClient::UI::Label 370 $self->{desc_widget} ||= new CFPlus::UI::Label
382 can_events => 1, 371 can_events => 1,
383 can_hover => 1, 372 can_hover => 1,
384 ellipsise => 2, 373 ellipsise => 2,
385 align => -1, 374 align => -1,
386 on_button_down => $button_cb, 375 on_button_down => $button_cb,
387 ; 376 ;
388 my $desc = CFClient::Item::desc_string $self; 377 my $desc = CFPlus::Item::desc_string $self;
378 $self->{desc_widget}{bg} = $bg;
389 $self->{desc_widget}->set_text ($desc); 379 $self->{desc_widget}->set_text ($desc);
390 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std"); 380 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std");
391 381
392 $self->{weight_widget} ||= new CFClient::UI::Label 382 $self->{weight_widget} ||= new CFPlus::UI::Label
393 can_events => 1, 383 can_events => 1,
394 can_hover => 1, 384 can_hover => 1,
395 ellipsise => 0, 385 ellipsise => 0,
396 align => 0, 386 align => 0,
397 on_button_down => $button_cb, 387 on_button_down => $button_cb,
398 ; 388 ;
389 $self->{weight_widget}{bg} = $bg;
399 $self->{weight_widget}->set_text (CFClient::Item::weight_string $self); 390 $self->{weight_widget}->set_text (CFPlus::Item::weight_string $self);
400
401 $self->{weight_widget}->set_tooltip ( 391 $self->{weight_widget}->set_tooltip (
402 "<b>Weight</b>.\n" 392 "<b>Weight</b>.\n"
403 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ") 393 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ")
404 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") 394 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
405 . "\n\n$tooltip_std" 395 . "\n\n$tooltip_std"

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines