… | |
… | |
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 | |
21 | our $VERSION; |
19 | our $VERSION; |
22 | |
20 | |
23 | BEGIN { |
21 | BEGIN { |
24 | $VERSION = '0.9975'; |
22 | $VERSION = '2.02'; |
25 | |
23 | |
26 | use XSLoader; |
24 | use XSLoader; |
27 | XSLoader::load "Deliantra::Client", $VERSION; |
25 | XSLoader::load "Deliantra::Client", $VERSION; |
28 | } |
26 | } |
29 | |
27 | |
30 | use utf8; |
28 | use utf8; |
31 | use strict qw(vars subs); |
29 | use strict qw(vars subs); |
32 | |
30 | |
|
|
31 | use Socket (); |
33 | use AnyEvent (); |
32 | use AnyEvent (); |
|
|
33 | use AnyEvent::Util (); |
34 | use Pod::POM (); |
34 | use Pod::POM (); |
35 | use File::Path (); |
35 | use File::Path (); |
36 | use Storable (); # finally |
36 | use Storable (); # finally |
37 | use Fcntl (); |
37 | use Fcntl (); |
38 | use JSON::XS qw(encode_json decode_json); |
38 | use JSON::XS qw(encode_json decode_json); |
… | |
… | |
69 | s/</</g; |
69 | s/</</g; |
70 | |
70 | |
71 | $_ |
71 | $_ |
72 | } |
72 | } |
73 | |
73 | |
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(&;&) { |
74 | sub background(&;&) { |
82 | my ($bg, $cb) = @_; |
75 | my ($bg, $cb) = @_; |
83 | |
76 | |
84 | my ($fh_r, $fh_w) = DC::socketpipe; |
77 | my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair |
|
|
78 | or die "unable to create background socketpair: $!"; |
85 | |
79 | |
86 | my $pid = fork; |
80 | my $pid = fork; |
87 | |
81 | |
88 | if (defined $pid && !$pid) { |
82 | if (defined $pid && !$pid) { |
89 | local $SIG{__DIE__}; |
83 | local $SIG{__DIE__}; |
… | |
… | |
145 | print $msg, "\n"; |
139 | print $msg, "\n"; |
146 | } |
140 | } |
147 | |
141 | |
148 | package DC; |
142 | package DC; |
149 | |
143 | |
150 | our @RC_THEME = ("theme-blue", "."); |
144 | our $RC_THEME; |
|
|
145 | our %THEME; |
|
|
146 | our @RC_PATH; |
151 | our $RC_BASE; |
147 | our $RC_BASE; |
152 | |
148 | |
153 | for (grep !ref, @INC) { |
149 | for (grep !ref, @INC) { |
154 | $RC_BASE = "$_/Deliantra/Client/private/resources"; |
150 | $RC_BASE = "$_/Deliantra/Client/private/resources"; |
155 | last if -d $RC_BASE; |
151 | last if -d $RC_BASE; |
156 | } |
152 | } |
157 | |
153 | |
158 | sub find_rcfile($) { |
154 | sub find_rcfile($) { |
159 | my $path; |
155 | my $path; |
160 | |
156 | |
161 | for (@RC_THEME) { |
157 | for (@RC_PATH, "") { |
162 | $path = "$RC_BASE/$_/$_[0]"; |
158 | $path = "$RC_BASE/$_/$_[0]"; |
163 | return $path if -r $path; |
159 | return $path if -r $path; |
164 | } |
160 | } |
165 | |
161 | |
166 | die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; |
162 | die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n"; |
167 | } |
163 | } |
168 | |
164 | |
|
|
165 | sub 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 | |
|
|
175 | sub 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 | } |
|
|
195 | } |
|
|
196 | |
169 | sub read_cfg { |
197 | sub read_cfg { |
170 | my ($file) = @_; |
198 | my ($file) = @_; |
171 | |
199 | |
172 | open my $fh, $file |
200 | $::CFG = load_json $file; |
173 | or return; |
|
|
174 | |
|
|
175 | local $/; |
|
|
176 | my $CFG = <$fh>; |
|
|
177 | |
|
|
178 | $::CFG = decode_json $CFG; |
|
|
179 | } |
201 | } |
180 | |
202 | |
181 | sub write_cfg { |
203 | sub write_cfg { |
182 | my $file = "$Deliantra::VARDIR/client.cf"; |
204 | my $file = "$Deliantra::VARDIR/client.cf"; |
183 | |
205 | |