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.165 by root, Tue Dec 25 19:23:11 2007 UTC vs.
Revision 1.217 by root, Wed Nov 21 13:23:10 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.9961'; 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 ();
33use AnyEvent::Util ();
29use Pod::POM (); 34use Pod::POM ();
30use File::Path (); 35use File::Path ();
31use Storable (); # finally 36use Storable (); # finally
32use Fcntl (); 37use Fcntl ();
33use JSON::XS qw(encode_json decode_json); 38use JSON::XS qw(encode_json decode_json);
39use Guard qw(guard);
34 40
35=item guard { BLOCK } 41# modules to support other DC::* packages
42use List::Util ();
43use IO::AIO ();
44use Coro::AIO ();
45use AnyEvent::AIO ();
36 46
37Returns an object that executes the given block as soon as it is destroyed. 47use Deliantra::Util ();
38 48use Deliantra::Protocol::Constants ();
39=cut
40
41sub guard(&) {
42 bless \(my $cb = $_[0]), "CFPlus::Guard"
43}
44
45sub CFPlus::Guard::DESTROY {
46 ${$_[0]}->()
47}
48 49
49=item shorten $string[, $maxlength] 50=item shorten $string[, $maxlength]
50 51
51=cut 52=cut
52 53
64 s/</&lt;/g; 65 s/</&lt;/g;
65 66
66 $_ 67 $_
67} 68}
68 69
69sub socketpipe() { 70sub sanitise_cfxml($) {
70 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC 71 local $_ = shift;
71 or die "cannot establish bidirectional pipe: $!\n";
72 72
73 ($fh1, $fh2) 73 # we now weed out all tags we do not support
74 s{ <(?! /?i> | /?u> | /?b> | /?big | /?small | /?s | /?tt | fg\ | /fg>)
75 }{
76 "&lt;"
77 }gex;
78
79 # now all entities
80 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
81
82 # handle some elements
83 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
84 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
85
86 s/\s+$//;
87
88 $_
74} 89}
75 90
76sub background(&;&) { 91sub background(&;&) {
77 my ($bg, $cb) = @_; 92 my ($bg, $cb) = @_;
78 93
79 my ($fh_r, $fh_w) = CFPlus::socketpipe; 94 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
95 or die "unable to create background socketpair: $!";
80 96
81 my $pid = fork; 97 my $pid = fork;
82 98
83 if (defined $pid && !$pid) { 99 if (defined $pid && !$pid) {
84 local $SIG{__DIE__}; 100 local $SIG{__DIE__};
94 110
95 if ($@) { 111 if ($@) {
96 my $msg = $@; 112 my $msg = $@;
97 $msg =~ s/\n+/\n/; 113 $msg =~ s/\n+/\n/;
98 warn "FATAL: $msg"; 114 warn "FATAL: $msg";
99 CFPlus::_exit 1; 115 DC::_exit 1;
100 } 116 }
101 117
102 # win32 is fucked up, of course. exit will clean stuff up, 118 # win32 is fucked up, of course. exit will clean stuff up,
103 # which destroys our database etc. _exit will exit ALL 119 # which destroys our database etc. _exit will exit ALL
104 # forked processes, because of the dreaded fork emulation. 120 # forked processes, because of the dreaded fork emulation.
105 CFPlus::_exit 0; 121 DC::_exit 0;
106 } 122 }
107 123
108 close $fh_w; 124 close $fh_w;
109 125
110 my $buffer; 126 my $buffer;
122 utf8::decode $line; 138 utf8::decode $line;
123 if ($line =~ /^\x{e877}json_msg (.*)$/s) { 139 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
124 $cb->(JSON::XS->new->allow_nonref->decode ($1)); 140 $cb->(JSON::XS->new->allow_nonref->decode ($1));
125 } else { 141 } else {
126 ::message ({ 142 ::message ({
127 markup => "background($pid): " . CFPlus::asxml $line, 143 markup => "background($pid): " . DC::asxml $line,
128 }); 144 });
129 } 145 }
130 } 146 }
131 }); 147 });
132} 148}
138 $msg =~ s/\n//g; 154 $msg =~ s/\n//g;
139 utf8::encode $msg; 155 utf8::encode $msg;
140 print $msg, "\n"; 156 print $msg, "\n";
141} 157}
142 158
143package CFPlus; 159package DC;
160
161our $RC_THEME;
162our %THEME;
163our @RC_PATH;
164our $RC_BASE;
165
166for (grep !ref, @INC) {
167 $RC_BASE = "$_/Deliantra/Client/private/resources";
168 last if -d $RC_BASE;
169}
144 170
145sub find_rcfile($) { 171sub find_rcfile($) {
146 my $path; 172 my $path;
147 173
148 for (grep !ref, @INC) { 174 for (@RC_PATH, "") {
149 $path = "$_/CFPlus/resources/$_[0]"; 175 $path = "$RC_BASE/$_/$_[0]";
150 return $path if -r $path; 176 return $path if -e $path;
151 } 177 }
152 178
153 die "FATAL: can't find required file $_[0]\n"; 179 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
154} 180}
155 181
156sub read_cfg { 182sub load_json($) {
157 my ($file) = @_; 183 my ($file) = @_;
158 184
159 open my $fh, $file 185 open my $fh, $file
160 or return; 186 or return;
161 187
162 local $/; 188 local $/;
163 my $CFG = <$fh>; 189 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
190}
164 191
165 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove 192sub set_theme($) {
166 require YAML; 193 return if $RC_THEME eq $_[0];
167 utf8::decode $CFG; 194 $RC_THEME = $_[0];
168 $::CFG = YAML::Load ($CFG); 195
169 } elsif ($CFG =~ /^\{/) { 196 # kind of hacky, find the main theme file, then load all theme files and merge them
170 $::CFG = decode_json $CFG; 197
198 %THEME = ();
199 @RC_PATH = "theme-$RC_THEME";
200
201 my $theme = load_json find_rcfile "theme.json"
202 or die "FATAL: theme resource file not found";
203
204 @RC_PATH = @{ $theme->{path} } if $theme->{path};
205
206 for (@RC_PATH, "") {
207 my $theme = load_json "$RC_BASE/$_/theme.json"
208 or next;
209
210 %THEME = ( %$theme, %THEME );
211 }
212}
213
214sub read_cfg($) {
215 my ($file) = @_;
216
217 $::CFG = (load_json $file) || (load_json "$file.bak");
218}
219
220sub write_cfg($) {
221 my $file = "$Deliantra::VARDIR/client.cf";
222
223 $::CFG->{VERSION} = $::VERSION;
224 $::CFG->{layout} = DC::UI::get_layout ();
225
226 open my $fh, ">:utf8", "$file~"
227 or return;
228 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
229 close $fh;
230
231 rename $file, "$file.bak";
232 rename "$file~", $file;
233}
234
235sub load_cfg() {
236 if (-e "$Deliantra::VARDIR/client.cf") {
237 DC::read_cfg "$Deliantra::VARDIR/client.cf";
171 } else { 238 } else {
172 $::CFG = eval $CFG; ## todo comaptibility cruft 239 $::CFG = { cfg_schema => 1, db_schema => 1 };
173 } 240 }
174} 241}
175 242
176sub write_cfg { 243sub save_cfg() {
177 my ($file) = @_; 244 write_cfg "$Deliantra::VARDIR/client.cf";
245}
178 246
179 $::CFG->{VERSION} = $::VERSION; 247sub upgrade_cfg() {
248 my %DEF_CFG = (
249 config_autosave => 1,
250 sdl_mode => undef,
251 fullscreen => 1,
252 fast => 0,
253 force_opengl11 => undef,
254 disable_alpha => 0,
255 smooth_movement => 1,
256 smooth_transitions => 1,
257 texture_compression => 1,
258 map_scale => 1,
259 fow_enable => 1,
260 fow_intensity => 0,
261 fow_texture => 0,
262 map_smoothing => 1,
263 gui_fontsize => 1,
264 log_fontsize => 0.7,
265 gauge_fontsize => 1,
266 gauge_size => 0.35,
267 stat_fontsize => 0.7,
268 mapsize => 100,
269 audio_enable => 1,
270 audio_hw_channels => 0,
271 audio_hw_frequency => 0,
272 audio_hw_chunksize => 0,
273 audio_mix_channels => 8,
274 effects_enable => 1,
275 effects_volume => 1,
276 bgm_enable => 1,
277 bgm_volume => 0.5,
278 output_rate => "",
279 pickup => Deliantra::Protocol::Constants::PICKUP_SPELLBOOK
280 | Deliantra::Protocol::Constants::PICKUP_SKILLSCROLL
281 | Deliantra::Protocol::Constants::PICKUP_VALUABLES,
282 inv_sort => "mtime",
283 default => "profile", # default profile
284 show_tips => 1,
285 logview_max_par => 1000,
286 shift_fire_stop => 0,
287 uitheme => "wood",
288 map_shift_x => -24, # arbitrary
289 map_shift_y => +24, # arbitrary
290 );
180 291
181 open my $fh, ">:utf8", $file 292 while (my ($k, $v) = each %DEF_CFG) {
182 or return; 293 $::CFG->{$k} = $v unless exists $::CFG->{$k};
183 print $fh encode_json $::CFG; 294 }
295
296 if ($::CFG->{cfg_schema} < 1) {
297 for my $profile (values %{ $::CFG->{profile} }) {
298 $profile->{password} = unpack "H*", Deliantra::Util::hash_pw $profile->{password};
299 }
300 $::CFG->{cfg_schema} = 1;
301 }
184} 302}
185 303
186sub http_proxy { 304sub http_proxy {
187 my @proxy = win32_proxy_info; 305 my @proxy = win32_proxy_info;
188 306
203} 321}
204 322
205sub lwp_useragent { 323sub lwp_useragent {
206 require LWP::UserAgent; 324 require LWP::UserAgent;
207 325
208 CFPlus::set_proxy; 326 DC::set_proxy;
209 327
210 my $ua = LWP::UserAgent->new ( 328 my $ua = LWP::UserAgent->new (
211 agent => "cfplus $VERSION", 329 agent => "deliantra $VERSION",
212 keep_alive => 1, 330 keep_alive => 1,
213 env_proxy => 1, 331 env_proxy => 1,
214 timeout => 30, 332 timeout => 30,
215 ); 333 );
216} 334}
231 $nb = (! ! $nb) + 0; 349 $nb = (! ! $nb) + 0;
232 ioctl $fh, 0x8004667e, \$nb; # FIONBIO 350 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
233 } else { 351 } else {
234 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; 352 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
235 } 353 }
236
237} 354}
238 355
239package CFPlus::Layout; 356package DC::Layout;
240 357
241$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Layout"} = sub { 358$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
242 glyph_cache_restore; 359 glyph_cache_restore;
243}; 360};
244 361
245$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { 362$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
246 glyph_cache_backup; 363 glyph_cache_backup;
247}; 364};
248 365
2491; 3661;
250 367

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines