1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | CFPlus - undocumented utility garbage for our crossfire client |
3 | DC - 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 | |
15 | package CFPlus; |
15 | package DC; |
16 | |
16 | |
17 | use Carp (); |
17 | use Carp (); |
18 | |
18 | |
|
|
19 | our $VERSION; |
|
|
20 | |
19 | BEGIN { |
21 | BEGIN { |
20 | $VERSION = '0.97'; |
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 | |
26 | use utf8; |
28 | use utf8; |
|
|
29 | use strict qw(vars subs); |
27 | |
30 | |
|
|
31 | use Socket (); |
28 | use AnyEvent (); |
32 | use AnyEvent (); |
|
|
33 | use AnyEvent::Util (); |
29 | use Pod::POM (); |
34 | use Pod::POM (); |
30 | use File::Path (); |
35 | use File::Path (); |
31 | use Storable (); # finally |
36 | use Storable (); # finally |
32 | use Fcntl (); |
37 | use Fcntl (); |
|
|
38 | use JSON::XS qw(encode_json decode_json); |
|
|
39 | use Guard qw(guard); |
33 | |
40 | |
34 | BEGIN { |
41 | # modules to support other DC::* packages |
35 | use Crossfire::Protocol::Base (); |
42 | use List::Util (); |
36 | *to_json = \&Crossfire::Protocol::Base::to_json; |
43 | use IO::AIO (); |
37 | *from_json = \&Crossfire::Protocol::Base::from_json; |
44 | use Coro::AIO (); |
38 | } |
45 | use AnyEvent::AIO (); |
39 | |
46 | |
40 | =item guard { BLOCK } |
47 | use Deliantra::Protocol::Constants (); |
41 | |
|
|
42 | Returns an object that executes the given block as soon as it is destroyed. |
|
|
43 | |
|
|
44 | =cut |
|
|
45 | |
|
|
46 | sub guard(&) { |
|
|
47 | bless \(my $cb = $_[0]), "CFPlus::Guard" |
|
|
48 | } |
|
|
49 | |
|
|
50 | sub CFPlus::Guard::DESTROY { |
|
|
51 | ${$_[0]}->() |
|
|
52 | } |
|
|
53 | |
48 | |
54 | =item shorten $string[, $maxlength] |
49 | =item shorten $string[, $maxlength] |
55 | |
50 | |
56 | =cut |
51 | =cut |
57 | |
52 | |
… | |
… | |
69 | s/</</g; |
64 | s/</</g; |
70 | |
65 | |
71 | $_ |
66 | $_ |
72 | } |
67 | } |
73 | |
68 | |
74 | sub socketpipe() { |
|
|
75 | socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
|
|
76 | or die "cannot establish bidirectional pipe: $!\n"; |
|
|
77 | |
|
|
78 | ($fh1, $fh2) |
|
|
79 | } |
|
|
80 | |
|
|
81 | sub background(&;&) { |
69 | sub background(&;&) { |
82 | my ($bg, $cb) = @_; |
70 | my ($bg, $cb) = @_; |
83 | |
71 | |
84 | my ($fh_r, $fh_w) = CFPlus::socketpipe; |
72 | my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair |
|
|
73 | or die "unable to create background socketpair: $!"; |
85 | |
74 | |
86 | my $pid = fork; |
75 | my $pid = fork; |
87 | |
76 | |
88 | if (defined $pid && !$pid) { |
77 | if (defined $pid && !$pid) { |
89 | local $SIG{__DIE__}; |
78 | local $SIG{__DIE__}; |
… | |
… | |
99 | |
88 | |
100 | if ($@) { |
89 | if ($@) { |
101 | my $msg = $@; |
90 | my $msg = $@; |
102 | $msg =~ s/\n+/\n/; |
91 | $msg =~ s/\n+/\n/; |
103 | warn "FATAL: $msg"; |
92 | warn "FATAL: $msg"; |
104 | CFPlus::_exit 1; |
93 | DC::_exit 1; |
105 | } |
94 | } |
106 | |
95 | |
107 | # win32 is fucked up, of course. exit will clean stuff up, |
96 | # win32 is fucked up, of course. exit will clean stuff up, |
108 | # which destroys our database etc. _exit will exit ALL |
97 | # which destroys our database etc. _exit will exit ALL |
109 | # forked processes, because of the dreaded fork emulation. |
98 | # forked processes, because of the dreaded fork emulation. |
110 | CFPlus::_exit 0; |
99 | DC::_exit 0; |
111 | } |
100 | } |
112 | |
101 | |
113 | close $fh_w; |
102 | close $fh_w; |
114 | |
103 | |
115 | my $buffer; |
104 | my $buffer; |
… | |
… | |
127 | utf8::decode $line; |
116 | utf8::decode $line; |
128 | if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
117 | if ($line =~ /^\x{e877}json_msg (.*)$/s) { |
129 | $cb->(JSON::XS->new->allow_nonref->decode ($1)); |
118 | $cb->(JSON::XS->new->allow_nonref->decode ($1)); |
130 | } else { |
119 | } else { |
131 | ::message ({ |
120 | ::message ({ |
132 | markup => "background($pid): " . CFPlus::asxml $line, |
121 | markup => "background($pid): " . DC::asxml $line, |
133 | }); |
122 | }); |
134 | } |
123 | } |
135 | } |
124 | } |
136 | }); |
125 | }); |
137 | } |
126 | } |
… | |
… | |
143 | $msg =~ s/\n//g; |
132 | $msg =~ s/\n//g; |
144 | utf8::encode $msg; |
133 | utf8::encode $msg; |
145 | print $msg, "\n"; |
134 | print $msg, "\n"; |
146 | } |
135 | } |
147 | |
136 | |
148 | package CFPlus; |
137 | package DC; |
|
|
138 | |
|
|
139 | our $RC_THEME; |
|
|
140 | our %THEME; |
|
|
141 | our @RC_PATH; |
|
|
142 | our $RC_BASE; |
|
|
143 | |
|
|
144 | for (grep !ref, @INC) { |
|
|
145 | $RC_BASE = "$_/Deliantra/Client/private/resources"; |
|
|
146 | last if -d $RC_BASE; |
|
|
147 | } |
149 | |
148 | |
150 | sub find_rcfile($) { |
149 | sub find_rcfile($) { |
151 | my $path; |
150 | my $path; |
152 | |
151 | |
153 | for (grep !ref, @INC) { |
152 | for (@RC_PATH, "") { |
154 | $path = "$_/CFPlus/resources/$_[0]"; |
153 | $path = "$RC_BASE/$_/$_[0]"; |
155 | return $path if -r $path; |
154 | return $path if -e $path; |
156 | } |
155 | } |
157 | |
156 | |
158 | die "FATAL: can't find required file $_[0]\n"; |
157 | die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; |
|
|
158 | } |
|
|
159 | |
|
|
160 | sub load_json($) { |
|
|
161 | my ($file) = @_; |
|
|
162 | |
|
|
163 | open my $fh, $file |
|
|
164 | or return; |
|
|
165 | |
|
|
166 | local $/; |
|
|
167 | eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) } |
|
|
168 | } |
|
|
169 | |
|
|
170 | sub set_theme($) { |
|
|
171 | return if $RC_THEME eq $_[0]; |
|
|
172 | $RC_THEME = $_[0]; |
|
|
173 | |
|
|
174 | # kind of hacky, find the main theme file, then load all theme files and merge them |
|
|
175 | |
|
|
176 | %THEME = (); |
|
|
177 | @RC_PATH = "theme-$RC_THEME"; |
|
|
178 | |
|
|
179 | my $theme = load_json find_rcfile "theme.json" |
|
|
180 | or die "FATAL: theme resource file not found"; |
|
|
181 | |
|
|
182 | @RC_PATH = @{ $theme->{path} } if $theme->{path}; |
|
|
183 | |
|
|
184 | for (@RC_PATH, "") { |
|
|
185 | my $theme = load_json "$RC_BASE/$_/theme.json" |
|
|
186 | or next; |
|
|
187 | |
|
|
188 | %THEME = ( %$theme, %THEME ); |
|
|
189 | } |
159 | } |
190 | } |
160 | |
191 | |
161 | sub read_cfg { |
192 | sub read_cfg { |
162 | my ($file) = @_; |
193 | my ($file) = @_; |
163 | |
194 | |
|
|
195 | $::CFG = (load_json $file) || (load_json "$file.bak"); |
|
|
196 | } |
|
|
197 | |
|
|
198 | sub write_cfg { |
|
|
199 | my $file = "$Deliantra::VARDIR/client.cf"; |
|
|
200 | |
|
|
201 | $::CFG->{VERSION} = $::VERSION; |
|
|
202 | $::CFG->{layout} = DC::UI::get_layout (); |
|
|
203 | |
164 | open my $fh, $file |
204 | open my $fh, ">:utf8", "$file~" |
165 | or return; |
205 | or return; |
|
|
206 | print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
|
|
207 | close $fh; |
166 | |
208 | |
167 | local $/; |
209 | rename $file, "$file.bak"; |
168 | my $CFG = <$fh>; |
210 | rename "$file~", $file; |
169 | |
|
|
170 | if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove |
|
|
171 | require YAML; |
|
|
172 | utf8::decode $CFG; |
|
|
173 | $::CFG = YAML::Load ($CFG); |
|
|
174 | } elsif ($CFG =~ /^\{/) { |
|
|
175 | $::CFG = from_json $CFG; |
|
|
176 | } else { |
|
|
177 | $::CFG = eval $CFG; ## todo comaptibility cruft |
|
|
178 | } |
|
|
179 | } |
211 | } |
180 | |
212 | |
181 | sub write_cfg { |
213 | sub upgrade_cfg() { |
182 | my ($file) = @_; |
214 | my %DEF_CFG = ( |
|
|
215 | config_autosave => 1, |
|
|
216 | sdl_mode => undef, |
|
|
217 | fullscreen => 1, |
|
|
218 | fast => 0, |
|
|
219 | force_opengl11 => undef, |
|
|
220 | disable_alpha => 0, |
|
|
221 | smooth_movement => 1, |
|
|
222 | smooth_transitions => 1, |
|
|
223 | texture_compression => 1, |
|
|
224 | map_scale => 1, |
|
|
225 | fow_enable => 1, |
|
|
226 | fow_intensity => 0, |
|
|
227 | fow_texture => 0, |
|
|
228 | map_smoothing => 1, |
|
|
229 | gui_fontsize => 1, |
|
|
230 | log_fontsize => 0.7, |
|
|
231 | gauge_fontsize => 1, |
|
|
232 | gauge_size => 0.35, |
|
|
233 | stat_fontsize => 0.7, |
|
|
234 | mapsize => 100, |
|
|
235 | audio_enable => 1, |
|
|
236 | audio_hw_channels => 0, |
|
|
237 | audio_hw_frequency => 0, |
|
|
238 | audio_hw_chunksize => 0, |
|
|
239 | audio_mix_channels => 8, |
|
|
240 | effects_enable => 1, |
|
|
241 | effects_volume => 1, |
|
|
242 | bgm_enable => 1, |
|
|
243 | bgm_volume => 0.5, |
|
|
244 | output_rate => "", |
|
|
245 | pickup => Deliantra::Protocol::Constants::PICKUP_SPELLBOOK |
|
|
246 | | Deliantra::Protocol::Constants::PICKUP_SKILLSCROLL |
|
|
247 | | Deliantra::Protocol::Constants::PICKUP_VALUABLES, |
|
|
248 | inv_sort => "mtime", |
|
|
249 | default => "profile", # default profile |
|
|
250 | show_tips => 1, |
|
|
251 | logview_max_par => 1000, |
|
|
252 | shift_fire_stop => 0, |
|
|
253 | uitheme => "wood", |
|
|
254 | map_shift_x => -24, # arbitrary |
|
|
255 | map_shift_y => +24, # arbitrary |
|
|
256 | #db_schema => 0, |
|
|
257 | ); |
183 | |
258 | |
184 | $::CFG->{VERSION} = $::VERSION; |
259 | while (my ($k, $v) = each %DEF_CFG) { |
185 | |
260 | $::CFG->{$k} = $v unless exists $::CFG->{$k}; |
186 | open my $fh, ">:utf8", $file |
261 | } |
187 | or return; |
|
|
188 | print $fh to_json $::CFG; |
|
|
189 | } |
262 | } |
190 | |
263 | |
191 | sub http_proxy { |
264 | sub http_proxy { |
192 | my @proxy = win32_proxy_info; |
265 | my @proxy = win32_proxy_info; |
193 | |
266 | |
… | |
… | |
208 | } |
281 | } |
209 | |
282 | |
210 | sub lwp_useragent { |
283 | sub lwp_useragent { |
211 | require LWP::UserAgent; |
284 | require LWP::UserAgent; |
212 | |
285 | |
213 | CFPlus::set_proxy; |
286 | DC::set_proxy; |
214 | |
287 | |
215 | my $ua = LWP::UserAgent->new ( |
288 | my $ua = LWP::UserAgent->new ( |
216 | agent => "cfplus $VERSION", |
289 | agent => "deliantra $VERSION", |
217 | keep_alive => 1, |
290 | keep_alive => 1, |
218 | env_proxy => 1, |
291 | env_proxy => 1, |
219 | timeout => 30, |
292 | timeout => 30, |
220 | ); |
293 | ); |
221 | } |
294 | } |
… | |
… | |
232 | sub fh_nonblocking($$) { |
305 | sub fh_nonblocking($$) { |
233 | my ($fh, $nb) = @_; |
306 | my ($fh, $nb) = @_; |
234 | |
307 | |
235 | if ($^O eq "MSWin32") { |
308 | if ($^O eq "MSWin32") { |
236 | $nb = (! ! $nb) + 0; |
309 | $nb = (! ! $nb) + 0; |
237 | ioctl $fh, 0x8004667e, $nb; # FIONBIO |
310 | ioctl $fh, 0x8004667e, \$nb; # FIONBIO |
238 | } else { |
311 | } else { |
239 | fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
312 | fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; |
240 | } |
313 | } |
241 | |
|
|
242 | } |
314 | } |
243 | |
315 | |
244 | package CFPlus::Layout; |
316 | package DC::Layout; |
245 | |
317 | |
|
|
318 | $DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub { |
|
|
319 | glyph_cache_restore; |
|
|
320 | }; |
|
|
321 | |
246 | $CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { |
322 | $DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub { |
247 | reset_glyph_cache; |
323 | glyph_cache_backup; |
248 | }; |
324 | }; |
249 | |
325 | |
250 | 1; |
326 | 1; |
251 | |
327 | |
252 | =back |
328 | =back |