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.70 by root, Fri May 26 18:28:23 2006 UTC vs.
Revision 1.200 by root, Sun Jan 4 10:22:19 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFClient - 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 CFClient; 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 CFClient; 15package DC;
16
17use Carp ();
18
19our $VERSION;
16 20
17BEGIN { 21BEGIN {
18 $VERSION = '0.1'; 22 $VERSION = '2.02';
19 23
20 use XSLoader; 24 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 25 XSLoader::load "Deliantra::Client", $VERSION;
22} 26}
23 27
24use utf8; 28use utf8;
29use strict qw(vars subs);
25 30
26use Carp (); 31use Socket ();
27use AnyEvent (); 32use AnyEvent ();
28use BerkeleyDB; 33use AnyEvent::Util ();
34use Pod::POM ();
35use File::Path ();
36use Storable (); # finally
37use Fcntl ();
38use JSON::XS qw(encode_json decode_json);
39
40=item guard { BLOCK }
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]), "DC::Guard"
48}
49
50sub DC::Guard::DESTROY {
51 ${$_[0]}->()
52}
53
54=item shorten $string[, $maxlength]
55
56=cut
57
58sub shorten($;$) {
59 my ($str, $len) = @_;
60 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
61 $str
62}
63
64sub asxml($) {
65 local $_ = $_[0];
66
67 s/&/&amp;/g;
68 s/>/&gt;/g;
69 s/</&lt;/g;
70
71 $_
72}
73
74sub background(&;&) {
75 my ($bg, $cb) = @_;
76
77 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
78 or die "unable to create background socketpair: $!";
79
80 my $pid = fork;
81
82 if (defined $pid && !$pid) {
83 local $SIG{__DIE__};
84
85 open STDOUT, ">&", $fh_w;
86 open STDERR, ">&", $fh_w;
87 close $fh_r;
88 close $fh_w;
89
90 $| = 1;
91
92 eval { $bg->() };
93
94 if ($@) {
95 my $msg = $@;
96 $msg =~ s/\n+/\n/;
97 warn "FATAL: $msg";
98 DC::_exit 1;
99 }
100
101 # win32 is fucked up, of course. exit will clean stuff up,
102 # which destroys our database etc. _exit will exit ALL
103 # forked processes, because of the dreaded fork emulation.
104 DC::_exit 0;
105 }
106
107 close $fh_w;
108
109 my $buffer;
110
111 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
112 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
113 undef $w;
114 $cb->();
115 return;
116 }
117
118 while ($buffer =~ s/^(.*)\n//) {
119 my $line = $1;
120 $line =~ s/\s+$//;
121 utf8::decode $line;
122 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
123 $cb->(JSON::XS->new->allow_nonref->decode ($1));
124 } else {
125 ::message ({
126 markup => "background($pid): " . DC::asxml $line,
127 });
128 }
129 }
130 });
131}
132
133sub background_msg {
134 my ($msg) = @_;
135
136 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
137 $msg =~ s/\n//g;
138 utf8::encode $msg;
139 print $msg, "\n";
140}
141
142package DC;
143
144our $RC_THEME;
145our %THEME;
146our @RC_PATH;
147our $RC_BASE;
148
149for (grep !ref, @INC) {
150 $RC_BASE = "$_/Deliantra/Client/private/resources";
151 last if -d $RC_BASE;
152}
29 153
30sub find_rcfile($) { 154sub find_rcfile($) {
31 my $path; 155 my $path;
32 156
33 for (grep !ref, @INC) { 157 for (@RC_PATH, "") {
34 $path = "$_/CFClient/resources/$_[0]"; 158 $path = "$RC_BASE/$_/$_[0]";
35 return $path if -r $path; 159 return $path if -r $path;
36 } 160 }
37 161
38 die "FATAL: can't find required file $_[0]\n"; 162 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
163}
164
165sub load_json($) {
166 my ($file) = @_;
167
168 open my $fh, $file
169 or return;
170
171 local $/;
172 JSON::XS->new->utf8->relaxed->decode (<$fh>)
173}
174
175sub set_theme($) {
176 return if $RC_THEME eq $_[0];
177 $RC_THEME = $_[0];
178
179 # kind of hacky, find the main theme file, then load all theme files and merge them
180
181 %THEME = ();
182 @RC_PATH = "theme-$RC_THEME";
183
184 my $theme = load_json find_rcfile "theme.json"
185 or die "FATAL: theme resource file not found";
186
187 @RC_PATH = @{ $theme->{path} } if $theme->{path};
188
189 for (@RC_PATH, "") {
190 my $theme = load_json "$RC_BASE/$_/theme.json"
191 or next;
192
193 %THEME = ( %$theme, %THEME );
194 }
39} 195}
40 196
41sub read_cfg { 197sub read_cfg {
42 my ($file) = @_; 198 my ($file) = @_;
43 199
44 open CFG, $file 200 $::CFG = load_json $file;
201}
202
203sub write_cfg {
204 my $file = "$Deliantra::VARDIR/client.cf";
205
206 $::CFG->{VERSION} = $::VERSION;
207
208 open my $fh, ">:utf8", $file
45 or return; 209 or return;
46 210 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
47 my $CFG;
48
49 local $/;
50 $CFG = eval <CFG>;
51
52 $::CFG = $CFG;
53
54 close CFG;
55} 211}
56 212
57sub write_cfg { 213sub http_proxy {
58 my ($file) = @_; 214 my @proxy = win32_proxy_info;
59 215
60 open CFG, ">$file" 216 if (@proxy) {
217 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
218 } elsif (exists $ENV{http_proxy}) {
219 $ENV{http_proxy}
220 } else {
221 ()
222 }
223}
224
225sub set_proxy {
226 my $proxy = http_proxy
61 or return; 227 or return;
62 228
63 { 229 $ENV{http_proxy} = $proxy;
64 require Data::Dumper;
65 local $Data::Dumper::Purity = 1;
66 $::CFG->{VERSION} = $::VERSION;
67 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
68 }
69
70 close CFG;
71} 230}
72 231
73mkdir "$Crossfire::VARDIR/pclient", 0777; 232sub lwp_useragent {
74 233 require LWP::UserAgent;
75our $DB_ENV = new BerkeleyDB::Env
76 -Home => "$Crossfire::VARDIR/pclient",
77 -Cachesize => 1_000_000,
78 -ErrFile => "$Crossfire::VARDIR/pclient/errorlog.txt",
79# -ErrPrefix => "DATABASE",
80 -Verbose => 1,
81 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN,
82 or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error";
83
84sub db_table($) {
85 my ($table) = @_;
86
87 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
88 234
89 new CFClient::Database 235 DC::set_proxy;
90 -Env => $DB_ENV,
91 -Filename => $table,
92# -Filename => "database",
93# -Subname => $table,
94 -Property => DB_CHKSUM,
95 -Flags => DB_CREATE | DB_UPGRADE,
96 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error";
97}
98 236
99sub pod_to_pango($) { 237 my $ua = LWP::UserAgent->new (
238 agent => "deliantra $VERSION",
239 keep_alive => 1,
240 env_proxy => 1,
241 timeout => 30,
242 );
243}
244
245sub lwp_check($) {
100 my ($pom) = @_; 246 my ($res) = @_;
101 247
102 $pom->present ("CFClient::PodToPango") 248 $res->is_error
103} 249 and die $res->status_line;
104 250
105sub pod_to_pango_list($) { 251 $res
252}
253
254sub fh_nonblocking($$) {
106 my ($pom) = @_; 255 my ($fh, $nb) = @_;
107 256
257 if ($^O eq "MSWin32") {
258 $nb = (! ! $nb) + 0;
259 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
260 } else {
261 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
108 [ 262 }
109 map s/^(\s*)// && [40 * length $1, length $_ ? $_ : " "],
110 split /\n/, $pom->present ("CFClient::PodToPango")
111 ]
112} 263}
113 264
114package CFClient::PodToPango; 265package DC::Layout;
115 266
116use base Pod::POM::View::Text; 267$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
117 268 glyph_cache_restore;
118our $indent = 0;
119
120*view_seq_code =
121*view_seq_bold = sub { "<b>$_[1]</b>" };
122*view_seq_italic = sub { "<i>$_[1]</i>" };
123*view_seq_space =
124*view_seq_link =
125*view_seq_index = sub { CFClient::UI::Label::escape ($_[1]) };
126
127sub view_seq_text {
128 my $text = $_[1];
129 $text =~ s/\s+/ /g;
130 CFClient::UI::Label::escape ($text)
131}
132
133sub view_item {
134 ("\t" x ($indent / 4))
135 . $_[1]->title->present ($_[0])
136 . "\n"
137 . $_[1]->content->present ($_[0])
138}
139
140sub view_verbatim {
141 (join "",
142 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n",
143 split /\n/, CFClient::UI::Label::escape ($_[1]))
144 . "\n"
145}
146
147sub view_textblock {
148 ("\t" x ($indent / 2)) . "$_[1]\n\n"
149}
150
151sub view_head1 {
152 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
153 . $_[1]->content->present ($_[0])
154}; 269};
155 270
156sub view_head2 { 271$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
157 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 272 glyph_cache_backup;
158 . $_[1]->content->present ($_[0])
159}; 273};
160
161sub view_head3 {
162 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
163 . $_[1]->content->present ($_[0])
164};
165
166sub view_over {
167 local $indent = $indent + $_[1]->indent;
168 $_[1]->content->present ($_[0])
169}
170
171package CFClient::Database;
172
173our @ISA = BerkeleyDB::Btree::;
174
175sub get($$) {
176 my $data;
177
178 $_[0]->db_get ($_[1], $data) == 0
179 ? $data
180 : ()
181}
182
183my %DB_SYNC;
184
185sub put($$$) {
186 my ($db, $key, $data) = @_;
187
188 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
189
190 $db->db_put ($key => $data)
191}
192
193package CFClient::Item;
194
195sub desc_string {
196 my ($self) = @_;
197
198 my $desc =
199 $self->{nrof} < 2
200 ? $self->{name}
201 : "$self->{nrof} × $self->{name_pl}";
202
203 $self->{flags} & Crossfire::Protocol::F_OPEN
204 and $desc .= " (open)";
205 $self->{flags} & Crossfire::Protocol::F_APPLIED
206 and $desc .= " (applied)";
207 $self->{flags} & Crossfire::Protocol::F_UNPAID
208 and $desc .= " (unpaid)";
209 $self->{flags} & Crossfire::Protocol::F_MAGIC
210 and $desc .= " (magic)";
211 $self->{flags} & Crossfire::Protocol::F_CURSED
212 and $desc .= " (cursed)";
213 $self->{flags} & Crossfire::Protocol::F_DAMNED
214 and $desc .= " (damned)";
215 $self->{flags} & Crossfire::Protocol::F_LOCKED
216 and $desc .= " *";
217
218 $desc
219}
220
221sub weight_string {
222 my ($self) = @_;
223
224 my $weight = ($self->{nrof} || 1) * $self->{weight};
225
226 $weight < 0 ? "?" : $weight * 0.001
227}
228
229sub update_widgets {
230 my ($self) = @_;
231
232 my $button_cb = sub {
233 my (undef, $ev, $x, $y) = @_;
234
235 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
236 my $targ = $::CONN->{player}{tag};
237
238 if ($self->{container} == $::CONN->{player}{tag}) {
239 $targ = $::CONN->{open_container};
240 }
241
242 $::CONN->send ("move $targ $self->{tag} 0");
243 } elsif ($ev->{button} == 1) {
244 $::CONN->send ("examine $self->{tag}");
245 } elsif ($ev->{button} == 2) {
246 $::CONN->send ("apply $self->{tag}");
247 } elsif ($ev->{button} == 3) {
248 my @menu_items = (
249 ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
250 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }],
251 ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
252 (
253 $self->{flags} & Crossfire::Protocol::F_LOCKED
254 ? (
255 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
256 )
257 : (
258 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
259 ["drop", sub { $::CONN->send ("move $::CONN->{open_container} $self->{tag} 0") }],
260 )
261 ),
262 );
263
264 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
265 }
266
267 1
268 };
269
270 my $tooltip_std = "<small>"
271 . "Left click - examine item\n"
272 . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n"
273 . "Middle click - apply\n"
274 . "Right click - further options"
275 . "</small>\n";
276
277 $self->{face_widget} ||= new CFClient::UI::Face
278 can_events => 1,
279 can_hover => 1,
280 anim => $self->{anim},
281 animspeed => $self->{animspeed}, # TODO# must be set at creation time
282 connect_button_down => $button_cb,
283 ;
284 $self->{face_widget}{face} = $self->{face};
285 $self->{face_widget}{anim} = $self->{anim};
286 $self->{face_widget}{animspeed} = $self->{animspeed};
287 $self->{face_widget}->set_tooltip (
288 "<b>Face/Animation.</b>\n"
289 . "Item uses face #$self->{face}. "
290 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ")
291 . "\n\n$tooltip_std"
292 );
293
294 $self->{desc_widget} ||= new CFClient::UI::Label
295 can_events => 1,
296 can_hover => 1,
297 ellipsise => 2,
298 align => -1,
299 connect_button_down => $button_cb,
300 ;
301 my $desc = CFClient::Item::desc_string $self;
302 $self->{desc_widget}->set_text ($desc);
303 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std");
304
305 $self->{weight_widget} ||= new CFClient::UI::Label
306 can_events => 1,
307 can_hover => 1,
308 ellipsise => 0,
309 align => 0,
310 connect_button_down => $button_cb,
311 ;
312 $self->{weight_widget}->set_text (CFClient::Item::weight_string $self);
313
314 $self->{weight_widget}->set_tooltip (
315 "<b>Weight</b>.\n"
316 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ")
317 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
318 . "\n\n$tooltip_std"
319 );
320}
321 274
3221; 2751;
323 276
324=back 277=back
325 278

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines