… | |
… | |
10 | |
10 | |
11 | =over 4 |
11 | =over 4 |
12 | |
12 | |
13 | =cut |
13 | =cut |
14 | |
14 | |
15 | package Deliantra::Client; # work around CPAN breakage |
|
|
16 | package App::Deliantra; # try to reserve namespace |
|
|
17 | package DC; |
15 | package DC; |
18 | |
16 | |
19 | use Carp (); |
17 | use Carp (); |
20 | |
18 | |
|
|
19 | our $VERSION; |
|
|
20 | |
21 | BEGIN { |
21 | BEGIN { |
22 | $VERSION = '0.9973'; |
22 | $VERSION = '3.0'; |
23 | |
23 | |
24 | use XSLoader; |
24 | use XSLoader; |
25 | XSLoader::load "Deliantra::Client", $VERSION; |
25 | XSLoader::load "Deliantra::Client", $VERSION; |
26 | } |
26 | } |
27 | |
27 | |
28 | use utf8; |
28 | use utf8; |
|
|
29 | use strict qw(vars subs); |
29 | |
30 | |
|
|
31 | use Socket (); |
30 | use AnyEvent (); |
32 | use AnyEvent (); |
|
|
33 | use AnyEvent::Util (); |
31 | use Pod::POM (); |
34 | use Pod::POM (); |
32 | use File::Path (); |
35 | use File::Path (); |
33 | use Storable (); # finally |
36 | use Storable (); # finally |
34 | use Fcntl (); |
37 | use Fcntl (); |
35 | use JSON::XS qw(encode_json decode_json); |
38 | use JSON::XS qw(encode_json decode_json); |
|
|
39 | use Guard qw(guard); |
36 | |
40 | |
37 | =item guard { BLOCK } |
41 | # modules to support other DC::* packages |
|
|
42 | use List::Util (); |
|
|
43 | use IO::AIO (); |
|
|
44 | use Coro::AIO (); |
|
|
45 | use AnyEvent::AIO (); |
38 | |
46 | |
39 | Returns an object that executes the given block as soon as it is destroyed. |
47 | use Deliantra::Protocol::Constants (); |
40 | |
|
|
41 | =cut |
|
|
42 | |
|
|
43 | sub guard(&) { |
|
|
44 | bless \(my $cb = $_[0]), "DC::Guard" |
|
|
45 | } |
|
|
46 | |
|
|
47 | sub DC::Guard::DESTROY { |
|
|
48 | ${$_[0]}->() |
|
|
49 | } |
|
|
50 | |
48 | |
51 | =item shorten $string[, $maxlength] |
49 | =item shorten $string[, $maxlength] |
52 | |
50 | |
53 | =cut |
51 | =cut |
54 | |
52 | |
… | |
… | |
66 | s/</</g; |
64 | s/</</g; |
67 | |
65 | |
68 | $_ |
66 | $_ |
69 | } |
67 | } |
70 | |
68 | |
71 | sub socketpipe() { |
|
|
72 | socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
|
|
73 | or die "cannot establish bidirectional pipe: $!\n"; |
|
|
74 | |
|
|
75 | ($fh1, $fh2) |
|
|
76 | } |
|
|
77 | |
|
|
78 | sub background(&;&) { |
69 | sub background(&;&) { |
79 | my ($bg, $cb) = @_; |
70 | my ($bg, $cb) = @_; |
80 | |
71 | |
81 | my ($fh_r, $fh_w) = DC::socketpipe; |
72 | my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair |
|
|
73 | or die "unable to create background socketpair: $!"; |
82 | |
74 | |
83 | my $pid = fork; |
75 | my $pid = fork; |
84 | |
76 | |
85 | if (defined $pid && !$pid) { |
77 | if (defined $pid && !$pid) { |
86 | local $SIG{__DIE__}; |
78 | local $SIG{__DIE__}; |
… | |
… | |
142 | print $msg, "\n"; |
134 | print $msg, "\n"; |
143 | } |
135 | } |
144 | |
136 | |
145 | package DC; |
137 | package DC; |
146 | |
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 | } |
|
|
148 | |
147 | sub find_rcfile($) { |
149 | sub find_rcfile($) { |
148 | my $path; |
150 | my $path; |
149 | |
151 | |
150 | for (grep !ref, @INC) { |
152 | for (@RC_PATH, "") { |
151 | $path = "$_/Deliantra/Client/private/resources/$_[0]"; |
153 | $path = "$RC_BASE/$_/$_[0]"; |
152 | return $path if -r $path; |
154 | return $path if -e $path; |
153 | } |
155 | } |
154 | |
156 | |
155 | 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 | } |
156 | } |
190 | } |
157 | |
191 | |
158 | sub read_cfg { |
192 | sub read_cfg { |
159 | my ($file) = @_; |
193 | my ($file) = @_; |
160 | |
194 | |
161 | open my $fh, $file |
195 | $::CFG = (load_json $file) || (load_json "$file.bak"); |
162 | or return; |
|
|
163 | |
|
|
164 | local $/; |
|
|
165 | my $CFG = <$fh>; |
|
|
166 | |
|
|
167 | $::CFG = decode_json $CFG; |
|
|
168 | } |
196 | } |
169 | |
197 | |
170 | sub write_cfg { |
198 | sub write_cfg { |
171 | my $file = "$Deliantra::VARDIR/client.cf"; |
199 | my $file = "$Deliantra::VARDIR/client.cf"; |
172 | |
200 | |
173 | $::CFG->{VERSION} = $::VERSION; |
201 | $::CFG->{VERSION} = $::VERSION; |
|
|
202 | $::CFG->{layout} = DC::UI::get_layout (); |
174 | |
203 | |
175 | open my $fh, ">:utf8", $file |
204 | open my $fh, ">:utf8", "$file~" |
176 | or return; |
205 | or return; |
177 | print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
206 | print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); |
|
|
207 | close $fh; |
|
|
208 | |
|
|
209 | rename $file, "$file.bak"; |
|
|
210 | rename "$file~", $file; |
|
|
211 | } |
|
|
212 | |
|
|
213 | sub upgrade_cfg() { |
|
|
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 | ); |
|
|
258 | |
|
|
259 | while (my ($k, $v) = each %DEF_CFG) { |
|
|
260 | $::CFG->{$k} = $v unless exists $::CFG->{$k}; |
|
|
261 | } |
178 | } |
262 | } |
179 | |
263 | |
180 | sub http_proxy { |
264 | sub http_proxy { |
181 | my @proxy = win32_proxy_info; |
265 | my @proxy = win32_proxy_info; |
182 | |
266 | |