ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.140
Committed: Fri Apr 6 07:45:33 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.139: +1 -77 lines
Log Message:
- changed most db accesses to be asynchronous
  (this was way more difficult than anticipated)
- face id allocation is still synchronous (and has to be as long as
  we want to allow multiple instances running in parallel)
- facemap is cached locally, so subsequent accesses are fast
- the alternative would be to store faceids, not tileids, in the
  in-memory map, but then mapcache tiles are non-displayable
  unless the server already sent them.
- tile loading and map cache load/store are fully asynchronous
- replaced 32x32 dialogue and noface images by 64 bit versions
- properly scale special dialogue/noface textures to tilesize

File Contents

# Content
1 =head1 NAME
2
3 CFPlus - undocumented utility garbage for our crossfire client
4
5 =head1 SYNOPSIS
6
7 use CFPlus;
8
9 =head1 DESCRIPTION
10
11 =over 4
12
13 =cut
14
15 package CFPlus;
16
17 use Carp ();
18
19 BEGIN {
20 $VERSION = '0.97';
21
22 use XSLoader;
23 XSLoader::load "CFPlus", $VERSION;
24 }
25
26 use utf8;
27
28 use AnyEvent ();
29 use Pod::POM ();
30 use File::Path ();
31 use Storable (); # finally
32
33 BEGIN {
34 use Crossfire::Protocol::Base ();
35 *to_json = \&Crossfire::Protocol::Base::to_json;
36 *from_json = \&Crossfire::Protocol::Base::from_json;
37 }
38
39 =item guard { BLOCK }
40
41 Returns an object that executes the given block as soon as it is destroyed.
42
43 =cut
44
45 sub guard(&) {
46 bless \(my $cb = $_[0]), "CFPlus::Guard"
47 }
48
49 sub CFPlus::Guard::DESTROY {
50 ${$_[0]}->()
51 }
52
53 =item shorten $string[, $maxlength]
54
55 =cut
56
57 sub shorten($;$) {
58 my ($str, $len) = @_;
59 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
60 $str
61 }
62
63 sub asxml($) {
64 local $_ = $_[0];
65
66 s/&/&amp;/g;
67 s/>/&gt;/g;
68 s/</&lt;/g;
69
70 $_
71 }
72
73 sub socketpipe() {
74 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
75 or die "cannot establish bidirectional pipe: $!\n";
76
77 ($fh1, $fh2)
78 }
79
80 sub background(&;&) {
81 my ($bg, $cb) = @_;
82
83 my ($fh_r, $fh_w) = CFPlus::socketpipe;
84
85 my $pid = fork;
86
87 if (defined $pid && !$pid) {
88 local $SIG{__DIE__};
89
90 open STDOUT, ">&", $fh_w;
91 open STDERR, ">&", $fh_w;
92 close $fh_r;
93 close $fh_w;
94
95 $| = 1;
96
97 eval { $bg->() };
98
99 if ($@) {
100 my $msg = $@;
101 $msg =~ s/\n+/\n/;
102 warn "FATAL: $msg";
103 CFPlus::_exit 1;
104 }
105
106 # win32 is fucked up, of course. exit will clean stuff up,
107 # which destroys our database etc. _exit will exit ALL
108 # forked processes, because of the dreaded fork emulation.
109 CFPlus::_exit 0;
110 }
111
112 close $fh_w;
113
114 my $buffer;
115
116 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
117 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
118 undef $w;
119 $cb->();
120 return;
121 }
122
123 while ($buffer =~ s/^(.*)\n//) {
124 my $line = $1;
125 $line =~ s/\s+$//;
126 utf8::decode $line;
127 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
128 $cb->(JSON::XS->new->allow_nonref->decode ($1));
129 } else {
130 ::message ({
131 markup => "background($pid): " . CFPlus::asxml $line,
132 });
133 }
134 }
135 });
136 }
137
138 sub background_msg {
139 my ($msg) = @_;
140
141 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
142 $msg =~ s/\n//g;
143 utf8::encode $msg;
144 print $msg, "\n";
145 }
146
147 package CFPlus;
148
149 sub find_rcfile($) {
150 my $path;
151
152 for (grep !ref, @INC) {
153 $path = "$_/CFPlus/resources/$_[0]";
154 return $path if -r $path;
155 }
156
157 die "FATAL: can't find required file $_[0]\n";
158 }
159
160 sub read_cfg {
161 my ($file) = @_;
162
163 open my $fh, $file
164 or return;
165
166 local $/;
167 my $CFG = <$fh>;
168
169 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove
170 require YAML;
171 utf8::decode $CFG;
172 $::CFG = YAML::Load ($CFG);
173 } elsif ($CFG =~ /^\{/) {
174 $::CFG = from_json $CFG;
175 } else {
176 $::CFG = eval $CFG; ## todo comaptibility cruft
177 }
178 }
179
180 sub write_cfg {
181 my ($file) = @_;
182
183 $::CFG->{VERSION} = $::VERSION;
184
185 open my $fh, ">:utf8", $file
186 or return;
187 print $fh to_json $::CFG;
188 }
189
190 sub http_proxy {
191 my @proxy = win32_proxy_info;
192
193 if (@proxy) {
194 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
195 } elsif (exists $ENV{http_proxy}) {
196 $ENV{http_proxy}
197 } else {
198 ()
199 }
200 }
201
202 sub set_proxy {
203 my $proxy = http_proxy
204 or return;
205
206 $ENV{http_proxy} = $proxy;
207 }
208
209 sub lwp_useragent {
210 require LWP::UserAgent;
211
212 CFPlus::set_proxy;
213
214 my $ua = LWP::UserAgent->new (
215 agent => "cfplus $VERSION",
216 keep_alive => 1,
217 env_proxy => 1,
218 timeout => 30,
219 );
220 }
221
222 sub lwp_check($) {
223 my ($res) = @_;
224
225 $res->is_error
226 and die $res->status_line;
227
228 $res
229 }
230
231 package CFPlus::Layout;
232
233 $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub {
234 reset_glyph_cache;
235 };
236
237 1;
238
239 =back
240
241 =head1 AUTHOR
242
243 Marc Lehmann <schmorp@schmorp.de>
244 http://home.schmorp.de/
245
246 =cut
247