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.184 by root, Mon Jul 7 12:56:07 2008 UTC vs.
Revision 1.215 by root, Wed Jan 18 13:42:19 2012 UTC

10 10
11=over 4 11=over 4
12 12
13=cut 13=cut
14 14
15package Deliantra::Client; # work around CPAN breakage
16package App::Deliantra; # try to reserve namespace
17package DC; 15package DC;
18 16
19use Carp (); 17use Carp ();
20 18
19our $VERSION;
20
21BEGIN { 21BEGIN {
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
28use utf8; 28use utf8;
29use strict qw(vars subs);
29 30
31use Socket ();
30use AnyEvent (); 32use AnyEvent ();
33use AnyEvent::Util ();
31use Pod::POM (); 34use Pod::POM ();
32use File::Path (); 35use File::Path ();
33use Storable (); # finally 36use Storable (); # finally
34use Fcntl (); 37use Fcntl ();
35use JSON::XS qw(encode_json decode_json); 38use JSON::XS qw(encode_json decode_json);
39use Guard qw(guard);
36 40
37=item guard { BLOCK } 41# modules to support other DC::* packages
42use List::Util ();
43use IO::AIO ();
44use Coro::AIO ();
45use AnyEvent::AIO ();
38 46
39Returns an object that executes the given block as soon as it is destroyed. 47use Deliantra::Protocol::Constants ();
40
41=cut
42
43sub guard(&) {
44 bless \(my $cb = $_[0]), "DC::Guard"
45}
46
47sub 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/</&lt;/g; 64 s/</&lt;/g;
67 65
68 $_ 66 $_
69} 67}
70 68
71sub 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
78sub background(&;&) { 69sub 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
145package DC; 137package DC;
146 138
139our $RC_THEME;
140our %THEME;
141our @RC_PATH;
142our $RC_BASE;
143
144for (grep !ref, @INC) {
145 $RC_BASE = "$_/Deliantra/Client/private/resources";
146 last if -d $RC_BASE;
147}
148
147sub find_rcfile($) { 149sub 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";
156} 158}
157 159
158sub read_cfg { 160sub load_json($) {
159 my ($file) = @_; 161 my ($file) = @_;
160 162
161 open my $fh, $file 163 open my $fh, $file
162 or return; 164 or return;
163 165
164 local $/; 166 local $/;
165 my $CFG = <$fh>; 167 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
166
167 $::CFG = decode_json $CFG;
168} 168}
169 169
170sub 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 }
190}
191
192sub read_cfg($) {
193 my ($file) = @_;
194
195 $::CFG = (load_json $file) || (load_json "$file.bak");
196}
197
170sub write_cfg { 198sub 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
213sub load_cfg() {
214 if (-e "$Deliantra::VARDIR/client.cf") {
215 DC::read_cfg "$Deliantra::VARDIR/client.cf";
216 } else {
217 $::CFG = {};
218 }
219}
220
221sub save_cfg() {
222 write_cfg "$Deliantra::VARDIR/client.cf";
223}
224
225sub upgrade_cfg() {
226 my %DEF_CFG = (
227 config_autosave => 1,
228 sdl_mode => undef,
229 fullscreen => 1,
230 fast => 0,
231 force_opengl11 => undef,
232 disable_alpha => 0,
233 smooth_movement => 1,
234 smooth_transitions => 1,
235 texture_compression => 1,
236 map_scale => 1,
237 fow_enable => 1,
238 fow_intensity => 0,
239 fow_texture => 0,
240 map_smoothing => 1,
241 gui_fontsize => 1,
242 log_fontsize => 0.7,
243 gauge_fontsize => 1,
244 gauge_size => 0.35,
245 stat_fontsize => 0.7,
246 mapsize => 100,
247 audio_enable => 1,
248 audio_hw_channels => 0,
249 audio_hw_frequency => 0,
250 audio_hw_chunksize => 0,
251 audio_mix_channels => 8,
252 effects_enable => 1,
253 effects_volume => 1,
254 bgm_enable => 1,
255 bgm_volume => 0.5,
256 output_rate => "",
257 pickup => Deliantra::Protocol::Constants::PICKUP_SPELLBOOK
258 | Deliantra::Protocol::Constants::PICKUP_SKILLSCROLL
259 | Deliantra::Protocol::Constants::PICKUP_VALUABLES,
260 inv_sort => "mtime",
261 default => "profile", # default profile
262 show_tips => 1,
263 logview_max_par => 1000,
264 shift_fire_stop => 0,
265 uitheme => "wood",
266 map_shift_x => -24, # arbitrary
267 map_shift_y => +24, # arbitrary
268 #db_schema => 0,
269 );
270
271 while (my ($k, $v) = each %DEF_CFG) {
272 $::CFG->{$k} = $v unless exists $::CFG->{$k};
273 }
178} 274}
179 275
180sub http_proxy { 276sub http_proxy {
181 my @proxy = win32_proxy_info; 277 my @proxy = win32_proxy_info;
182 278

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines